blob: 8ec17f4597c2c706cc60401f3de5480256987083 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/common/gpu/context_provider_in_process.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "cc/output/managed_memory_policy.h"
#include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h"
#include "webkit/common/gpu/managed_memory_policy_convert.h"
namespace webkit {
namespace gpu {
class ContextProviderInProcess::LostContextCallbackProxy
: public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
public:
explicit LostContextCallbackProxy(ContextProviderInProcess* provider)
: provider_(provider) {
provider_->context3d_->setContextLostCallback(this);
}
virtual ~LostContextCallbackProxy() {
provider_->context3d_->setContextLostCallback(NULL);
}
virtual void onContextLost() {
provider_->OnLostContext();
}
private:
ContextProviderInProcess* provider_;
};
class ContextProviderInProcess::SwapBuffersCompleteCallbackProxy
: public WebKit::WebGraphicsContext3D::
WebGraphicsSwapBuffersCompleteCallbackCHROMIUM {
public:
explicit SwapBuffersCompleteCallbackProxy(ContextProviderInProcess* provider)
: provider_(provider) {
provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(this);
}
virtual ~SwapBuffersCompleteCallbackProxy() {
provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL);
}
virtual void onSwapBuffersComplete() {
provider_->OnSwapBuffersComplete();
}
private:
ContextProviderInProcess* provider_;
};
class ContextProviderInProcess::MemoryAllocationCallbackProxy
: public WebKit::WebGraphicsContext3D::
WebGraphicsMemoryAllocationChangedCallbackCHROMIUM {
public:
explicit MemoryAllocationCallbackProxy(ContextProviderInProcess* provider)
: provider_(provider) {
provider_->context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this);
}
virtual ~MemoryAllocationCallbackProxy() {
provider_->context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL);
}
virtual void onMemoryAllocationChanged(
WebKit::WebGraphicsMemoryAllocation allocation) {
provider_->OnMemoryAllocationChanged(allocation);
}
private:
ContextProviderInProcess* provider_;
};
// static
scoped_refptr<ContextProviderInProcess> ContextProviderInProcess::Create(
scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d) {
if (!context3d)
return NULL;
return new ContextProviderInProcess(context3d.Pass());
}
// static
scoped_refptr<ContextProviderInProcess>
ContextProviderInProcess::CreateOffscreen() {
WebKit::WebGraphicsContext3D::Attributes attributes;
attributes.depth = false;
attributes.stencil = true;
attributes.antialias = false;
attributes.shareResources = true;
attributes.noAutomaticFlushes = true;
return Create(
WebGraphicsContext3DInProcessCommandBufferImpl::CreateOffscreenContext(
attributes));
}
ContextProviderInProcess::ContextProviderInProcess(
scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl> context3d)
: context3d_(context3d.Pass()),
destroyed_(false) {
DCHECK(main_thread_checker_.CalledOnValidThread());
DCHECK(context3d_);
context_thread_checker_.DetachFromThread();
}
ContextProviderInProcess::~ContextProviderInProcess() {
DCHECK(main_thread_checker_.CalledOnValidThread() ||
context_thread_checker_.CalledOnValidThread());
}
bool ContextProviderInProcess::BindToCurrentThread() {
DCHECK(context3d_);
// This is called on the thread the context will be used.
DCHECK(context_thread_checker_.CalledOnValidThread());
if (lost_context_callback_proxy_)
return true;
if (!context3d_->makeContextCurrent())
return false;
lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
swap_buffers_complete_callback_proxy_.reset(
new SwapBuffersCompleteCallbackProxy(this));
memory_allocation_callback_proxy_.reset(
new MemoryAllocationCallbackProxy(this));
return true;
}
cc::ContextProvider::Capabilities
ContextProviderInProcess::ContextCapabilities() {
// We always use a WebGraphicsContext3DInProcessCommandBufferImpl which
// provides the following capabilities:
Capabilities caps;
caps.bind_uniform_location = true;
caps.discard_backbuffer = true;
caps.map_image = true;
caps.map_sub = true;
caps.set_visibility = true;
caps.shallow_flush = true;
caps.texture_format_bgra8888 = true;
caps.texture_rectangle = true;
return caps;
}
WebKit::WebGraphicsContext3D* ContextProviderInProcess::Context3d() {
DCHECK(context3d_);
DCHECK(lost_context_callback_proxy_); // Is bound to thread.
DCHECK(context_thread_checker_.CalledOnValidThread());
return context3d_.get();
}
class GrContext* ContextProviderInProcess::GrContext() {
DCHECK(context3d_);
DCHECK(lost_context_callback_proxy_); // Is bound to thread.
DCHECK(context_thread_checker_.CalledOnValidThread());
if (gr_context_)
return gr_context_->get();
gr_context_.reset(
new webkit::gpu::GrContextForWebGraphicsContext3D(context3d_.get()));
return gr_context_->get();
}
void ContextProviderInProcess::VerifyContexts() {
DCHECK(context3d_);
DCHECK(lost_context_callback_proxy_); // Is bound to thread.
DCHECK(context_thread_checker_.CalledOnValidThread());
if (context3d_->isContextLost())
OnLostContext();
}
void ContextProviderInProcess::OnLostContext() {
DCHECK(context_thread_checker_.CalledOnValidThread());
{
base::AutoLock lock(destroyed_lock_);
if (destroyed_)
return;
destroyed_ = true;
}
if (!lost_context_callback_.is_null())
base::ResetAndReturn(&lost_context_callback_).Run();
}
void ContextProviderInProcess::OnSwapBuffersComplete() {
DCHECK(context_thread_checker_.CalledOnValidThread());
if (!swap_buffers_complete_callback_.is_null())
swap_buffers_complete_callback_.Run();
}
void ContextProviderInProcess::OnMemoryAllocationChanged(
const WebKit::WebGraphicsMemoryAllocation& allocation) {
DCHECK(context_thread_checker_.CalledOnValidThread());
if (gr_context_) {
bool nonzero_allocation = !!allocation.gpuResourceSizeInBytes;
gr_context_->SetMemoryLimit(nonzero_allocation);
}
if (memory_policy_changed_callback_.is_null())
return;
bool discard_backbuffer_when_not_visible;
cc::ManagedMemoryPolicy policy =
ManagedMemoryPolicyConvert::Convert(allocation,
&discard_backbuffer_when_not_visible);
memory_policy_changed_callback_.Run(
policy, discard_backbuffer_when_not_visible);
}
bool ContextProviderInProcess::DestroyedOnMainThread() {
DCHECK(main_thread_checker_.CalledOnValidThread());
base::AutoLock lock(destroyed_lock_);
return destroyed_;
}
void ContextProviderInProcess::SetLostContextCallback(
const LostContextCallback& lost_context_callback) {
DCHECK(context_thread_checker_.CalledOnValidThread());
DCHECK(lost_context_callback_.is_null() ||
lost_context_callback.is_null());
lost_context_callback_ = lost_context_callback;
}
void ContextProviderInProcess::SetSwapBuffersCompleteCallback(
const SwapBuffersCompleteCallback& swap_buffers_complete_callback) {
DCHECK(context_thread_checker_.CalledOnValidThread());
DCHECK(swap_buffers_complete_callback_.is_null() ||
swap_buffers_complete_callback.is_null());
swap_buffers_complete_callback_ = swap_buffers_complete_callback;
}
void ContextProviderInProcess::SetMemoryPolicyChangedCallback(
const MemoryPolicyChangedCallback& memory_policy_changed_callback) {
DCHECK(context_thread_checker_.CalledOnValidThread());
DCHECK(memory_policy_changed_callback_.is_null() ||
memory_policy_changed_callback.is_null());
memory_policy_changed_callback_ = memory_policy_changed_callback;
}
} // namespace gpu
} // namespace webkit
|