OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/gpu/context_provider_in_process.h" | |
6 | |
7 #include "webkit/gpu/grcontext_for_webgraphicscontext3d.h" | |
8 | |
9 namespace webkit { | |
10 namespace gpu { | |
11 | |
12 class ContextProviderInProcess::LostContextCallbackProxy | |
13 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback { | |
14 public: | |
15 explicit LostContextCallbackProxy(ContextProviderInProcess* provider) | |
16 : provider_(provider) { | |
17 provider_->context3d_->setContextLostCallback(this); | |
18 } | |
19 | |
20 virtual void onContextLost() { | |
21 provider_->OnLostContextInternal(); | |
22 } | |
23 | |
24 private: | |
25 ContextProviderInProcess* provider_; | |
26 }; | |
27 | |
28 class ContextProviderInProcess::MemoryAllocationCallbackProxy | |
29 : public WebKit::WebGraphicsContext3D:: | |
30 WebGraphicsMemoryAllocationChangedCallbackCHROMIUM { | |
31 public: | |
32 explicit MemoryAllocationCallbackProxy(ContextProviderInProcess* provider) | |
33 : provider_(provider) { | |
34 provider_->context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this); | |
35 } | |
36 | |
37 virtual void onMemoryAllocationChanged( | |
38 WebKit::WebGraphicsMemoryAllocation alloc) { | |
39 provider_->OnMemoryAllocationChanged(!!alloc.gpuResourceSizeInBytes); | |
40 } | |
41 | |
42 private: | |
43 ContextProviderInProcess* provider_; | |
44 }; | |
45 | |
46 ContextProviderInProcess::ContextProviderInProcess() | |
47 : destroyed_(false) { | |
48 } | |
49 | |
50 ContextProviderInProcess::~ContextProviderInProcess() {} | |
51 | |
52 bool ContextProviderInProcess::InitializeOnMainThread() { | |
53 DCHECK(!context3d_); | |
54 | |
55 WebKit::WebGraphicsContext3D::Attributes attributes; | |
56 attributes.depth = false; | |
57 attributes.stencil = true; | |
58 attributes.antialias = false; | |
59 attributes.shareResources = true; | |
60 attributes.noAutomaticFlushes = true; | |
61 | |
62 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl; | |
63 context3d_.reset( | |
64 WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext( | |
65 attributes)); | |
66 | |
67 return context3d_; | |
68 } | |
69 | |
70 bool ContextProviderInProcess::BindToCurrentThread() { | |
71 DCHECK(context3d_); | |
72 | |
73 if (lost_context_callback_proxy_) | |
74 return true; | |
75 | |
76 if (!context3d_->makeContextCurrent()) | |
77 return false; | |
78 | |
79 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
80 return true; | |
81 } | |
82 | |
83 WebKit::WebGraphicsContext3D* ContextProviderInProcess::Context3d() { | |
84 DCHECK(context3d_); | |
85 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | |
86 | |
87 return context3d_.get(); | |
88 } | |
89 | |
90 class GrContext* ContextProviderInProcess::GrContext() { | |
91 DCHECK(context3d_); | |
92 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | |
93 | |
94 if (gr_context_) | |
95 return gr_context_->get(); | |
96 | |
97 gr_context_.reset( | |
98 new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_.get())); | |
99 memory_allocation_callback_proxy_.reset( | |
100 new MemoryAllocationCallbackProxy(this)); | |
101 return gr_context_->get(); | |
102 } | |
103 | |
104 void ContextProviderInProcess::VerifyContexts() { | |
105 DCHECK(context3d_); | |
106 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | |
107 | |
108 if (context3d_->isContextLost()) | |
109 OnLostContextInternal(); | |
110 } | |
111 | |
112 void ContextProviderInProcess::OnLostContextInternal() { | |
113 { | |
114 base::AutoLock lock(destroyed_lock_); | |
115 if (destroyed_) | |
116 return; | |
117 destroyed_ = true; | |
118 } | |
119 OnLostContext(); | |
120 } | |
121 | |
122 bool ContextProviderInProcess::DestroyedOnMainThread() { | |
123 base::AutoLock lock(destroyed_lock_); | |
124 return destroyed_; | |
125 } | |
126 | |
127 void ContextProviderInProcess::OnMemoryAllocationChanged( | |
128 bool nonzero_allocation) { | |
129 if (gr_context_) | |
130 gr_context_->SetMemoryLimit(nonzero_allocation); | |
131 } | |
132 | |
133 } // namespace gpu | |
134 } // namespace webkit | |
OLD | NEW |