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 | |
21 namespace ppapi { | 7 namespace ppapi { |
22 namespace proxy { | 8 namespace proxy { |
23 | 9 |
24 namespace { | 10 CompositorLayerResource::CompositorLayerResource(Connection connection, |
25 | 11 PP_Instance instance) |
26 class Scoped2DTextureBinder { | 12 : PluginResource(connection, instance) { |
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 float clamp(float value) { | |
44 return std::min(std::max(value, 0.0f), 1.0f); | |
45 } | |
46 | |
47 void OnTextureReleased( | |
48 const ScopedPPResource& layer, | |
49 const ScopedPPResource& context, | |
50 uint32_t texture, | |
51 const scoped_refptr<TrackedCallback>& release_callback, | |
52 uint32_t sync_point, | |
53 bool is_lost) { | |
54 if (!TrackedCallback::IsPending(release_callback)) | |
55 return; | |
56 | |
57 do { | |
58 if (!sync_point) | |
59 break; | |
60 | |
61 EnterResourceNoLock<PPB_Graphics3D_API> enter(context.get(), true); | |
62 if (enter.failed()) | |
63 break; | |
64 | |
65 PPB_Graphics3D_Shared* graphics = | |
66 static_cast<PPB_Graphics3D_Shared*>(enter.object()); | |
67 | |
68 GLES2Implementation* gl = graphics->gles2_impl(); | |
69 gl->WaitSyncPointCHROMIUM(sync_point); | |
70 } while (false); | |
71 | |
72 release_callback->Run(is_lost ? PP_ERROR_FAILED : PP_OK); | |
73 } | |
74 | |
75 void OnImageReleased( | |
76 const ScopedPPResource& layer, | |
77 const ScopedPPResource& image, | |
78 const scoped_refptr<TrackedCallback>& release_callback, | |
79 uint32_t sync_point, | |
80 bool is_lost) { | |
81 if (!TrackedCallback::IsPending(release_callback)) | |
82 return; | |
83 release_callback->Run(PP_OK); | |
84 } | |
85 | |
86 } // namespace | |
87 | |
88 CompositorLayerResource::CompositorLayerResource( | |
89 Connection connection, | |
90 PP_Instance instance, | |
91 const CompositorResource* compositor) | |
92 : PluginResource(connection, instance), | |
93 compositor_(compositor), | |
94 source_size_(PP_MakeFloatSize(0.0f, 0.0f)) { | |
95 } | 13 } |
96 | 14 |
97 CompositorLayerResource::~CompositorLayerResource() { | 15 CompositorLayerResource::~CompositorLayerResource() { |
98 DCHECK(!compositor_); | |
99 DCHECK(release_callback_.is_null()); | |
100 } | 16 } |
101 | 17 |
102 thunk::PPB_CompositorLayer_API* | 18 thunk::PPB_CompositorLayer_API* |
103 CompositorLayerResource::AsPPB_CompositorLayer_API() { | 19 CompositorLayerResource::AsPPB_CompositorLayer_API() { |
104 return this; | 20 return this; |
105 } | 21 } |
106 | 22 |
107 int32_t CompositorLayerResource::SetColor(float red, | 23 int32_t CompositorLayerResource::SetColor(float red, |
108 float green, | 24 float green, |
109 float blue, | 25 float blue, |
110 float alpha, | 26 float alpha, |
111 const PP_Size* size) { | 27 const PP_Size* size) { |
112 if (!compositor_) | 28 return PP_ERROR_NOTSUPPORTED; |
113 return PP_ERROR_BADRESOURCE; | |
114 | |
115 if (compositor_->IsInProgress()) | |
116 return PP_ERROR_INPROGRESS; | |
117 | |
118 if (!SetType(TYPE_COLOR)) | |
119 return PP_ERROR_BADARGUMENT; | |
120 DCHECK(data_.color); | |
121 | |
122 if (!size) | |
123 return PP_ERROR_BADARGUMENT; | |
124 | |
125 | |
126 data_.color->red = clamp(red); | |
127 data_.color->green = clamp(green); | |
128 data_.color->blue = clamp(blue); | |
129 data_.color->alpha = clamp(alpha); | |
130 data_.common.size = *size; | |
131 | |
132 return PP_OK; | |
133 } | 29 } |
134 | 30 |
135 int32_t CompositorLayerResource::SetTexture( | 31 int32_t CompositorLayerResource::SetTexture( |
136 PP_Resource context, | 32 PP_Resource context, |
137 uint32_t texture, | 33 uint32_t texture, |
138 const PP_Size* size, | 34 const PP_Size* size, |
139 const scoped_refptr<TrackedCallback>& release_callback) { | 35 const scoped_refptr<ppapi::TrackedCallback>& callback) { |
140 int32_t rv = CheckForSetTextureAndImage(TYPE_TEXTURE, release_callback); | 36 return PP_ERROR_NOTSUPPORTED; |
141 if (rv != PP_OK) | |
142 return rv; | |
143 DCHECK(data_.texture); | |
144 | |
145 EnterResourceNoLock<PPB_Graphics3D_API> enter(context, true); | |
146 if (enter.failed()) | |
147 return PP_ERROR_BADRESOURCE; | |
148 | |
149 if (!size || size->width <= 0 || size->height <= 0) | |
150 return PP_ERROR_BADARGUMENT; | |
151 | |
152 PPB_Graphics3D_Shared* graphics = | |
153 static_cast<PPB_Graphics3D_Shared*>(enter.object()); | |
154 | |
155 GLES2Implementation* gl = graphics->gles2_impl(); | |
156 Scoped2DTextureBinder scoped_2d_texture_binder(gl, texture); | |
157 | |
158 // Generate a Mailbox for the texture. | |
159 gl->GenMailboxCHROMIUM( | |
160 reinterpret_cast<GLbyte*>(data_.texture->mailbox.name)); | |
161 gl->ProduceTextureCHROMIUM( | |
162 GL_TEXTURE_2D, | |
163 reinterpret_cast<const GLbyte*>(data_.texture->mailbox.name)); | |
164 | |
165 // Set the source size to (1, 1). It will be used to verify the source_rect | |
166 // passed to SetSourceRect(). | |
167 source_size_ = PP_MakeFloatSize(1.0f, 1.0f); | |
168 | |
169 data_.common.size = *size; | |
170 data_.common.resource_id = compositor_->GenerateResourceId(); | |
171 data_.texture->sync_point = gl->InsertSyncPointCHROMIUM(); | |
172 data_.texture->source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f); | |
173 data_.texture->source_rect.size = source_size_; | |
174 | |
175 // If the PP_Resource of this layer is released by the plugin, the | |
176 // release_callback will be aborted immediately, but the texture or image | |
177 // in this layer may still being used by chromium compositor. So we have to | |
178 // use ScopedPPResource to keep this resource alive until the texture or image | |
179 // is released by the chromium compositor. | |
180 release_callback_ = base::Bind( | |
181 &OnTextureReleased, | |
182 ScopedPPResource(pp_resource()), // Keep layer alive. | |
183 ScopedPPResource(context), // Keep context alive | |
184 texture, | |
185 release_callback); | |
186 | |
187 return PP_OK_COMPLETIONPENDING; | |
188 } | 37 } |
189 | 38 |
190 int32_t CompositorLayerResource::SetImage( | 39 int32_t CompositorLayerResource::SetImage( |
191 PP_Resource image_data, | 40 PP_Resource image_data, |
192 const PP_Size* size, | 41 const PP_Size* size, |
193 const scoped_refptr<TrackedCallback>& release_callback) { | 42 const scoped_refptr<ppapi::TrackedCallback>& callback) { |
194 int32_t rv = CheckForSetTextureAndImage(TYPE_IMAGE, release_callback); | 43 return PP_ERROR_NOTSUPPORTED; |
195 if (rv != PP_OK) | |
196 return rv; | |
197 DCHECK(data_.image); | |
198 | |
199 EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true); | |
200 if (enter.failed()) | |
201 return PP_ERROR_BADRESOURCE; | |
202 | |
203 PP_ImageDataDesc desc; | |
204 if (!enter.object()->Describe(&desc)) | |
205 return PP_ERROR_BADARGUMENT; | |
206 | |
207 // TODO(penghuang): Support image which width * 4 != stride. | |
208 if (desc.size.width * 4 != desc.stride) | |
209 return PP_ERROR_BADARGUMENT; | |
210 | |
211 // TODO(penghuang): Support all formats. | |
212 if (desc.format != PP_IMAGEDATAFORMAT_RGBA_PREMUL) | |
213 return PP_ERROR_BADARGUMENT; | |
214 | |
215 if (!size || size->width <= 0 || size->height <= 0) | |
216 return PP_ERROR_BADARGUMENT; | |
217 | |
218 // Set the source size to image's size. It will be used to verify | |
219 // the source_rect passed to SetSourceRect(). | |
220 source_size_ = PP_MakeFloatSize(desc.size.width, desc.size.height); | |
221 | |
222 data_.common.size = size ? *size : desc.size; | |
223 data_.common.resource_id = compositor_->GenerateResourceId(); | |
224 data_.image->resource = enter.resource()->host_resource().host_resource(); | |
225 data_.image->source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f); | |
226 data_.image->source_rect.size = source_size_; | |
227 | |
228 release_callback_ = base::Bind( | |
229 &OnImageReleased, | |
230 ScopedPPResource(pp_resource()), // Keep layer alive. | |
231 ScopedPPResource(image_data), // Keep image_data alive. | |
232 release_callback); | |
233 | |
234 return PP_OK_COMPLETIONPENDING; | |
235 } | 44 } |
236 | 45 |
237 int32_t CompositorLayerResource::SetClipRect(const PP_Rect* rect) { | 46 int32_t CompositorLayerResource::SetClipRect(const PP_Rect* rect) { |
238 if (!compositor_) | 47 return PP_ERROR_NOTSUPPORTED; |
239 return PP_ERROR_BADRESOURCE; | |
240 | |
241 if (compositor_->IsInProgress()) | |
242 return PP_ERROR_INPROGRESS; | |
243 | |
244 data_.common.clip_rect = rect ? *rect : PP_MakeRectFromXYWH(0, 0, 0, 0); | |
245 return PP_OK; | |
246 } | 48 } |
247 | 49 |
248 int32_t CompositorLayerResource::SetTransform(const float matrix[16]) { | 50 int32_t CompositorLayerResource::SetTransform(const float matrix[16]) { |
249 if (!compositor_) | 51 return PP_ERROR_NOTSUPPORTED; |
250 return PP_ERROR_BADRESOURCE; | |
251 | |
252 if (compositor_->IsInProgress()) | |
253 return PP_ERROR_INPROGRESS; | |
254 | |
255 std::copy(matrix, matrix + 16, data_.common.transform.matrix); | |
256 return PP_OK; | |
257 } | 52 } |
258 | 53 |
259 int32_t CompositorLayerResource::SetOpacity(float opacity) { | 54 int32_t CompositorLayerResource::SetOpacity(float opacity) { |
260 if (!compositor_) | 55 return PP_ERROR_NOTSUPPORTED; |
261 return PP_ERROR_BADRESOURCE; | |
262 | |
263 if (compositor_->IsInProgress()) | |
264 return PP_ERROR_INPROGRESS; | |
265 | |
266 data_.common.opacity = clamp(opacity); | |
267 return PP_OK; | |
268 } | 56 } |
269 | 57 |
270 int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode) { | 58 int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode) { |
271 if (!compositor_) | 59 return PP_ERROR_NOTSUPPORTED; |
272 return PP_ERROR_BADRESOURCE; | |
273 | |
274 if (compositor_->IsInProgress()) | |
275 return PP_ERROR_INPROGRESS; | |
276 | |
277 switch (mode) { | |
278 case PP_BLENDMODE_NONE: | |
279 case PP_BLENDMODE_SRC_OVER: | |
280 data_.common.blend_mode = mode; | |
281 return PP_OK; | |
282 } | |
283 return PP_ERROR_BADARGUMENT; | |
284 } | 60 } |
285 | 61 |
286 int32_t CompositorLayerResource::SetSourceRect( | 62 int32_t CompositorLayerResource::SetSourceRect( |
287 const PP_FloatRect* rect) { | 63 const PP_FloatRect* rect) { |
288 if (!compositor_) | 64 return PP_ERROR_NOTSUPPORTED; |
289 return PP_ERROR_BADRESOURCE; | |
290 | |
291 if (compositor_->IsInProgress()) | |
292 return PP_ERROR_INPROGRESS; | |
293 | |
294 if (!rect || | |
295 rect->point.x < 0.0f || | |
296 rect->point.y < 0.0f || | |
297 rect->point.x + rect->size.width > source_size_.width || | |
298 rect->point.y + rect->size.height > source_size_.height) { | |
299 return PP_ERROR_BADARGUMENT; | |
300 } | |
301 | |
302 if (data_.texture) { | |
303 data_.texture->source_rect = *rect; | |
304 return PP_OK; | |
305 } | |
306 if (data_.image) { | |
307 data_.image->source_rect = *rect; | |
308 return PP_OK; | |
309 } | |
310 return PP_ERROR_BADARGUMENT; | |
311 } | 65 } |
312 | 66 |
313 int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult) { | 67 int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult) { |
314 if (!compositor_) | 68 return PP_ERROR_NOTSUPPORTED; |
315 return PP_ERROR_BADRESOURCE; | |
316 | |
317 if (compositor_->IsInProgress()) | |
318 return PP_ERROR_INPROGRESS; | |
319 | |
320 if (data_.texture) { | |
321 data_.texture->premult_alpha = PP_ToBool(premult); | |
322 return PP_OK; | |
323 } | |
324 return PP_ERROR_BADARGUMENT; | |
325 } | |
326 | |
327 bool CompositorLayerResource::SetType(LayerType type) { | |
328 if (type == TYPE_COLOR) { | |
329 if (data_.is_null()) | |
330 data_.color.reset(new CompositorLayerData::ColorLayer()); | |
331 return data_.color; | |
332 } | |
333 | |
334 if (type == TYPE_TEXTURE) { | |
335 if (data_.is_null()) | |
336 data_.texture.reset(new CompositorLayerData::TextureLayer()); | |
337 return data_.texture; | |
338 } | |
339 | |
340 if (type == TYPE_IMAGE) { | |
341 if (data_.is_null()) | |
342 data_.image.reset(new CompositorLayerData::ImageLayer()); | |
343 return data_.image; | |
344 } | |
345 | |
346 // Should not be reached. | |
347 DCHECK(false); | |
348 return false; | |
349 } | |
350 | |
351 int32_t CompositorLayerResource::CheckForSetTextureAndImage( | |
352 LayerType type, | |
353 const scoped_refptr<TrackedCallback>& release_callback) { | |
354 if (!compositor_) | |
355 return PP_ERROR_BADRESOURCE; | |
356 | |
357 if (compositor_->IsInProgress()) | |
358 return PP_ERROR_INPROGRESS; | |
359 | |
360 if (!SetType(type)) | |
361 return PP_ERROR_BADARGUMENT; | |
362 | |
363 // The layer's image has been set and it is not committed. | |
364 if (!release_callback_.is_null()) | |
365 return PP_ERROR_INPROGRESS; | |
366 | |
367 // Do not allow using a block callback as a release callback. | |
368 if (release_callback->is_blocking()) | |
369 return PP_ERROR_BADARGUMENT; | |
370 | |
371 return PP_OK; | |
372 } | 69 } |
373 | 70 |
374 } // namespace proxy | 71 } // namespace proxy |
375 } // namespace ppapi | 72 } // namespace ppapi |
OLD | NEW |