OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/compositor_layer_resource.h" | 5 #include "ppapi/proxy/compositor_layer_resource.h" |
6 | 6 |
7 #include "base/logging.h" | |
8 #include "gpu/command_buffer/client/gles2_implementation.h" | |
9 #include "gpu/command_buffer/common/mailbox.h" | |
10 #include "ppapi/proxy/compositor_resource.h" | |
11 #include "ppapi/shared_impl/ppb_graphics_3d_shared.h" | |
12 #include "ppapi/thunk/enter.h" | |
13 #include "ppapi/thunk/ppb_graphics_3d_api.h" | |
14 #include "ppapi/thunk/ppb_image_data_api.h" | |
15 | |
16 using gpu::gles2::GLES2Implementation; | |
17 using ppapi::thunk::EnterResourceNoLock; | |
18 using ppapi::thunk::PPB_ImageData_API; | |
19 using ppapi::thunk::PPB_Graphics3D_API; | |
20 | |
7 namespace ppapi { | 21 namespace ppapi { |
8 namespace proxy { | 22 namespace proxy { |
9 | 23 |
10 CompositorLayerResource::CompositorLayerResource(Connection connection, | 24 namespace { |
11 PP_Instance instance) | 25 |
12 : PluginResource(connection, instance) { | 26 class Scoped2DTextureBinder { |
27 public: | |
28 Scoped2DTextureBinder(GLES2Implementation* gl, uint32_t id) | |
29 : gl_(gl), old_id_(-1) { | |
30 gl_->GetIntegerv(GL_TEXTURE_BINDING_2D, &old_id_); | |
31 gl_->BindTexture(GL_TEXTURE_2D, id); | |
32 } | |
33 | |
34 ~Scoped2DTextureBinder() { | |
35 gl_->BindTexture(GL_TEXTURE_2D, old_id_); | |
36 } | |
37 | |
38 private: | |
39 GLES2Implementation* gl_; | |
40 int32_t old_id_; | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 CompositorLayerResource::CompositorLayerResource( | |
46 Connection connection, | |
47 PP_Instance instance, | |
48 const CompositorResource* compositor) | |
49 : PluginResource(connection, instance), | |
50 compositor_(compositor), | |
51 source_size_(PP_MakeFloatSize(0.0f, 0.0f)) { | |
13 } | 52 } |
14 | 53 |
15 CompositorLayerResource::~CompositorLayerResource() { | 54 CompositorLayerResource::~CompositorLayerResource() { |
55 DCHECK(!compositor_); | |
16 } | 56 } |
17 | 57 |
18 thunk::PPB_CompositorLayer_API* | 58 thunk::PPB_CompositorLayer_API* |
19 CompositorLayerResource::AsPPB_CompositorLayer_API() { | 59 CompositorLayerResource::AsPPB_CompositorLayer_API() { |
20 return this; | 60 return this; |
21 } | 61 } |
22 | 62 |
23 int32_t CompositorLayerResource::SetColor(uint8_t red, | 63 int32_t CompositorLayerResource::SetColor( |
24 uint8_t green, | 64 uint8_t red, |
25 uint8_t blue, | 65 uint8_t green, |
26 uint8_t alpha, | 66 uint8_t blue, |
27 const struct PP_Size* size) { | 67 uint8_t alpha, |
28 return PP_ERROR_NOTSUPPORTED; | 68 const struct PP_Size* size) { |
raymes
2014/06/04 01:11:38
nit: the indentation style you had before is more
Peng
2014/06/05 00:50:38
Done.
| |
69 if (!compositor_) | |
70 return PP_ERROR_BADRESOURCE; | |
71 | |
72 if (compositor_->IsInProgress()) | |
73 return PP_ERROR_INPROGRESS; | |
74 | |
75 if (!SetType(CompositorLayerData::TYPE_COLOR)) | |
76 return PP_ERROR_BADARGUMENT; | |
77 | |
78 if (!size) | |
79 return PP_ERROR_BADARGUMENT; | |
80 | |
81 data_.color.red = red; | |
82 data_.color.green = green; | |
83 data_.color.blue = blue; | |
84 data_.color.alpha = alpha; | |
85 data_.size = *size; | |
86 | |
87 return PP_OK; | |
29 } | 88 } |
30 | 89 |
31 int32_t CompositorLayerResource::SetTexture( | 90 int32_t CompositorLayerResource::SetTexture( |
32 PP_Resource context, | 91 PP_Resource context, |
33 uint32_t texture, | 92 uint32_t texture, |
34 const struct PP_Size* size, | 93 const struct PP_Size* size, |
35 const scoped_refptr<ppapi::TrackedCallback>& callback) { | 94 const scoped_refptr<ppapi::TrackedCallback>& release_callback) { |
36 return PP_ERROR_NOTSUPPORTED; | 95 if (!compositor_) |
96 return PP_ERROR_BADRESOURCE; | |
97 | |
98 if (compositor_->IsInProgress()) | |
99 return PP_ERROR_INPROGRESS; | |
100 | |
101 if (!SetType(CompositorLayerData::TYPE_TEXTURE)) | |
102 return PP_ERROR_BADARGUMENT; | |
103 | |
104 // The layer's texture has been set and it is not committed. | |
105 if (!release_callback_.is_null()) | |
106 return PP_ERROR_INPROGRESS; | |
107 | |
108 EnterResourceNoLock<PPB_Graphics3D_API> enter(context, true); | |
109 if (enter.failed()) | |
110 return PP_ERROR_BADRESOURCE; | |
111 | |
112 if (!size || size->width <= 0 || size->height <= 0) | |
113 return PP_ERROR_BADARGUMENT; | |
114 | |
115 // Do not allow using a block callback as a release callback. | |
116 if (release_callback->is_blocking()) | |
117 return PP_ERROR_BADARGUMENT; | |
118 | |
119 PPB_Graphics3D_Shared* graphics = | |
120 static_cast<PPB_Graphics3D_Shared*>(enter.object()); | |
121 | |
122 GLES2Implementation* gl = graphics->gles2_impl(); | |
123 Scoped2DTextureBinder scoped_2d_texture_binder(gl, texture); | |
124 | |
125 // Generate a Mailbox for the texture. | |
126 gl->GenMailboxCHROMIUM(data_.texture.mailbox); | |
127 gl->ProduceTextureCHROMIUM(GL_TEXTURE_2D, data_.texture.mailbox); | |
128 | |
129 // Set the source size to (1, 1). It will be used to verify the source_rect | |
130 // passed to SetSourceRect(). | |
131 source_size_ = PP_MakeFloatSize(1.0f, 1.0f); | |
132 | |
133 data_.resource_id = compositor_->GenerateResourceId(); | |
134 data_.texture.sync_point = gl->InsertSyncPointCHROMIUM(); | |
135 data_.size = *size; | |
136 data_.texture.source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f); | |
137 data_.texture.source_rect.size = source_size_; | |
138 | |
139 release_callback_ = base::Bind( | |
140 &CompositorLayerResource::OnTextureReleased, | |
141 this, | |
142 ScopedPPResource(context), // Keep context alive | |
143 texture, | |
144 release_callback); | |
145 | |
146 return PP_OK_COMPLETIONPENDING; | |
37 } | 147 } |
38 | 148 |
39 int32_t CompositorLayerResource::SetImage( | 149 int32_t CompositorLayerResource::SetImage( |
40 PP_Resource image_data, | 150 PP_Resource image_data, |
41 const struct PP_Size* size, | 151 const struct PP_Size* size, |
42 const scoped_refptr<ppapi::TrackedCallback>& callback) { | 152 const scoped_refptr<ppapi::TrackedCallback>& release_callback) { |
43 return PP_ERROR_NOTSUPPORTED; | 153 if (!compositor_) |
154 return PP_ERROR_BADRESOURCE; | |
155 | |
156 if (compositor_->IsInProgress()) | |
157 return PP_ERROR_INPROGRESS; | |
158 | |
159 if (!SetType(CompositorLayerData::TYPE_IMAGE)) | |
160 return PP_ERROR_BADARGUMENT; | |
161 | |
162 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); | |
163 if (enter.failed()) | |
164 return PP_ERROR_BADRESOURCE; | |
165 | |
166 // The layer's image has been set and it is not committed. | |
167 if (!release_callback_.is_null()) | |
168 return PP_ERROR_INPROGRESS; | |
169 | |
170 PP_ImageDataDesc desc; | |
171 if (!enter.object()->Describe(&desc)) | |
172 return PP_ERROR_BADARGUMENT; | |
173 | |
174 // TODO(penghuang): Support image which width * 4 != stride. | |
175 if (desc.size.width * 4 != desc.stride) | |
176 return PP_ERROR_BADARGUMENT; | |
177 | |
178 // TODO(penghuang): Support all formats. | |
179 if (desc.format != PP_IMAGEDATAFORMAT_RGBA_PREMUL) | |
180 return PP_ERROR_BADARGUMENT; | |
181 | |
182 if (!size || size->width <= 0 || size->height <= 0) | |
183 return PP_ERROR_BADARGUMENT; | |
184 | |
185 // Do not allow using a block callback as a release callback. | |
186 if (release_callback->is_blocking()) | |
187 return PP_ERROR_BADARGUMENT; | |
raymes
2014/06/04 01:11:38
nit: I'm tempted to ask to group the common checks
Peng
2014/06/05 00:50:38
Done.
| |
188 | |
189 // Set the source size to image's size. It will be used to verify | |
190 // the source_rect passed to SetSourceRect(). | |
191 source_size_ = PP_MakeFloatSize(desc.size.width, desc.size.height); | |
192 | |
193 data_.size = size ? *size : desc.size; | |
194 data_.resource_id = compositor_->GenerateResourceId(); | |
195 data_.image.instance = enter.resource()->host_resource().instance(); | |
196 data_.image.host_resource = enter.resource()->host_resource().host_resource(); | |
197 data_.image.source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f); | |
198 data_.image.source_rect.size = source_size_; | |
199 | |
200 release_callback_ = base::Bind( | |
201 &CompositorLayerResource::OnImageReleased, | |
202 this, | |
203 ScopedPPResource(image_data), // Keep image_data alive. | |
204 release_callback); | |
205 | |
206 return PP_OK_COMPLETIONPENDING; | |
44 } | 207 } |
45 | 208 |
46 int32_t CompositorLayerResource::SetClipRect(const struct PP_Rect* rect) { | 209 int32_t CompositorLayerResource::SetClipRect(const struct PP_Rect* rect) { |
47 return PP_ERROR_NOTSUPPORTED; | 210 if (!compositor_) |
211 return PP_ERROR_BADRESOURCE; | |
212 | |
213 if (compositor_->IsInProgress()) | |
214 return PP_ERROR_INPROGRESS; | |
215 | |
216 data_.clip_rect = rect ? *rect : PP_MakeRectFromXYWH(0, 0, 0, 0); | |
217 return PP_OK; | |
48 } | 218 } |
49 | 219 |
50 int32_t CompositorLayerResource::SetTransform(const float matrix[16]) { | 220 int32_t CompositorLayerResource::SetTransform(const float matrix[16]) { |
51 return PP_ERROR_NOTSUPPORTED; | 221 if (!compositor_) |
222 return PP_ERROR_BADRESOURCE; | |
223 | |
224 if (compositor_->IsInProgress()) | |
225 return PP_ERROR_INPROGRESS; | |
226 | |
227 std::copy(matrix, matrix + 16, data_.transform); | |
228 return PP_OK; | |
52 } | 229 } |
53 | 230 |
54 int32_t CompositorLayerResource::SetOpacity(uint8_t opacity) { | 231 int32_t CompositorLayerResource::SetOpacity(uint8_t opacity) { |
55 return PP_ERROR_NOTSUPPORTED; | 232 if (!compositor_) |
233 return PP_ERROR_BADRESOURCE; | |
234 | |
235 if (compositor_->IsInProgress()) | |
236 return PP_ERROR_INPROGRESS; | |
237 | |
238 data_.opacity = opacity; | |
239 return PP_OK; | |
56 } | 240 } |
57 | 241 |
58 int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode) { | 242 int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode) { |
59 return PP_ERROR_NOTSUPPORTED; | 243 if (!compositor_) |
244 return PP_ERROR_BADRESOURCE; | |
245 | |
246 if (compositor_->IsInProgress()) | |
247 return PP_ERROR_INPROGRESS; | |
248 | |
249 switch (mode) { | |
250 case PP_BLENDMODE_NONE: | |
251 case PP_BLENDMODE_SRC_OVER: | |
252 data_.blend_mode = mode; | |
253 return PP_OK; | |
254 default: | |
raymes
2014/06/04 01:11:38
as mentioned, let's get rid of all the default cas
Peng
2014/06/05 00:50:38
Done.
| |
255 break; | |
256 } | |
257 return PP_ERROR_BADARGUMENT; | |
60 } | 258 } |
61 | 259 |
62 int32_t CompositorLayerResource::SetSourceRect( | 260 int32_t CompositorLayerResource::SetSourceRect( |
63 const struct PP_FloatRect* rect) { | 261 const struct PP_FloatRect* rect) { |
64 return PP_ERROR_NOTSUPPORTED; | 262 if (!compositor_) |
263 return PP_ERROR_BADRESOURCE; | |
264 | |
265 if (compositor_->IsInProgress()) | |
266 return PP_ERROR_INPROGRESS; | |
267 | |
268 if (!rect || | |
269 rect->point.x < 0.0f || | |
270 rect->point.y < 0.0f || | |
271 rect->point.x + rect->size.width > source_size_.width || | |
272 rect->point.y + rect->size.height > source_size_.height) | |
273 return PP_ERROR_BADARGUMENT; | |
raymes
2014/06/04 01:11:38
nit: add {}
Peng
2014/06/05 00:50:38
Done.
| |
274 | |
275 switch (data_.type) { | |
276 case CompositorLayerData::TYPE_TEXTURE: | |
277 data_.texture.source_rect = *rect; | |
278 return PP_OK; | |
279 case CompositorLayerData::TYPE_IMAGE: | |
280 data_.image.source_rect = *rect; | |
281 return PP_OK; | |
282 default: | |
283 break; | |
284 } | |
285 return PP_ERROR_BADARGUMENT; | |
65 } | 286 } |
66 | 287 |
67 int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult) { | 288 int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult) { |
68 return PP_ERROR_NOTSUPPORTED; | 289 if (!compositor_) |
290 return PP_ERROR_BADRESOURCE; | |
291 | |
292 if (compositor_->IsInProgress()) | |
293 return PP_ERROR_INPROGRESS; | |
294 | |
295 if (data_.type != CompositorLayerData::TYPE_TEXTURE) | |
296 return PP_ERROR_BADARGUMENT; | |
297 | |
298 data_.texture.premult_alpha = PP_FromBool(premult); | |
299 return PP_OK; | |
300 } | |
301 | |
302 bool CompositorLayerResource::SetType(CompositorLayerData::Type type) { | |
303 DCHECK(type == CompositorLayerData::TYPE_COLOR || | |
304 type == CompositorLayerData::TYPE_TEXTURE || | |
305 type == CompositorLayerData::TYPE_IMAGE); | |
raymes
2014/06/04 01:11:38
We might as well just test this with an if stateme
Peng
2014/06/05 00:50:38
Done.
| |
306 if (type != data_.type) { | |
307 if (data_.type != CompositorLayerData::TYPE_UNKNOWN) | |
308 return false; | |
309 data_.type = type; | |
310 } | |
311 return true; | |
312 } | |
313 | |
314 void CompositorLayerResource::OnTextureReleased( | |
315 const ScopedPPResource& context, | |
316 uint32_t texture, | |
317 const scoped_refptr<TrackedCallback>& release_callback, | |
318 uint32_t sync_point, | |
319 bool is_lost) { | |
320 if (!TrackedCallback::IsPending(release_callback)) | |
321 return; | |
322 | |
323 do { | |
324 if (!sync_point) | |
325 break; | |
326 | |
327 EnterResourceNoLock<PPB_Graphics3D_API> enter(context.get(), true); | |
328 if (enter.failed()) | |
329 break; | |
330 | |
331 PPB_Graphics3D_Shared* graphics = | |
332 static_cast<PPB_Graphics3D_Shared*>(enter.object()); | |
333 | |
334 GLES2Implementation* gl = graphics->gles2_impl(); | |
335 gl->WaitSyncPointCHROMIUM(sync_point); | |
336 } while (false); | |
337 | |
338 release_callback->Run(PP_OK); | |
piman
2014/06/04 13:19:43
if is_lost is set, you should return failure here.
Peng
2014/06/05 00:50:38
When is_list is false? Does it means the Graphics3
| |
339 } | |
340 | |
341 void CompositorLayerResource::OnImageReleased( | |
342 const ScopedPPResource& image, | |
343 const scoped_refptr<TrackedCallback>& release_callback, | |
344 uint32_t sync_point, | |
345 bool is_lost) { | |
346 if (!TrackedCallback::IsPending(release_callback)) | |
347 return; | |
348 release_callback->Run(PP_OK); | |
69 } | 349 } |
70 | 350 |
71 } // namespace proxy | 351 } // namespace proxy |
72 } // namespace ppapi | 352 } // namespace ppapi |
OLD | NEW |