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