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
3 // found in the LICENSE file.
5 #include "ppapi/proxy/compositor_layer_resource.h"
7 #include "base/logging.h"
8 #include "gpu/GLES2/gl2extchromium.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "gpu/command_buffer/common/mailbox.h"
11 #include "ppapi/proxy/compositor_resource.h"
12 #include "ppapi/shared_impl/ppb_graphics_3d_shared.h"
13 #include "ppapi/thunk/enter.h"
14 #include "ppapi/thunk/ppb_graphics_3d_api.h"
15 #include "ppapi/thunk/ppb_image_data_api.h"
17 using gpu::gles2::GLES2Implementation
;
18 using ppapi::thunk::EnterResourceNoLock
;
19 using ppapi::thunk::PPB_ImageData_API
;
20 using ppapi::thunk::PPB_Graphics3D_API
;
27 float clamp(float value
) {
28 return std::min(std::max(value
, 0.0f
), 1.0f
);
31 void OnTextureReleased(
32 const ScopedPPResource
& layer
,
33 const ScopedPPResource
& context
,
35 const scoped_refptr
<TrackedCallback
>& release_callback
,
39 if (!TrackedCallback::IsPending(release_callback
))
42 if (result
!= PP_OK
) {
43 release_callback
->Run(result
);
51 EnterResourceNoLock
<PPB_Graphics3D_API
> enter(context
.get(), true);
55 PPB_Graphics3D_Shared
* graphics
=
56 static_cast<PPB_Graphics3D_Shared
*>(enter
.object());
58 GLES2Implementation
* gl
= graphics
->gles2_impl();
59 gl
->WaitSyncPointCHROMIUM(sync_point
);
62 release_callback
->Run(is_lost
? PP_ERROR_FAILED
: PP_OK
);
66 const ScopedPPResource
& layer
,
67 const ScopedPPResource
& image
,
68 const scoped_refptr
<TrackedCallback
>& release_callback
,
72 if (!TrackedCallback::IsPending(release_callback
))
74 release_callback
->Run(result
);
79 CompositorLayerResource::CompositorLayerResource(
80 Connection connection
,
82 const CompositorResource
* compositor
)
83 : PluginResource(connection
, instance
),
84 compositor_(compositor
),
85 source_size_(PP_MakeFloatSize(0.0f
, 0.0f
)) {
88 CompositorLayerResource::~CompositorLayerResource() {
90 DCHECK(release_callback_
.is_null());
93 thunk::PPB_CompositorLayer_API
*
94 CompositorLayerResource::AsPPB_CompositorLayer_API() {
98 int32_t CompositorLayerResource::SetColor(float red
,
102 const PP_Size
* size
) {
104 return PP_ERROR_BADRESOURCE
;
106 if (compositor_
->IsInProgress())
107 return PP_ERROR_INPROGRESS
;
109 if (!SetType(TYPE_COLOR
))
110 return PP_ERROR_BADARGUMENT
;
114 return PP_ERROR_BADARGUMENT
;
116 data_
.color
->red
= clamp(red
);
117 data_
.color
->green
= clamp(green
);
118 data_
.color
->blue
= clamp(blue
);
119 data_
.color
->alpha
= clamp(alpha
);
120 data_
.common
.size
= *size
;
125 int32_t CompositorLayerResource::SetTexture0_1(
129 const scoped_refptr
<TrackedCallback
>& release_callback
) {
130 return SetTexture(context
, GL_TEXTURE_2D
, texture
, size
, release_callback
);
133 int32_t CompositorLayerResource::SetTexture(
138 const scoped_refptr
<TrackedCallback
>& release_callback
) {
139 int32_t rv
= CheckForSetTextureAndImage(TYPE_TEXTURE
, release_callback
);
142 DCHECK(data_
.texture
);
144 EnterResourceNoLock
<PPB_Graphics3D_API
> enter(context
, true);
146 return PP_ERROR_BADRESOURCE
;
148 if (target
!= GL_TEXTURE_2D
&&
149 target
!= GL_TEXTURE_EXTERNAL_OES
&&
150 target
!= GL_TEXTURE_RECTANGLE_ARB
) {
151 return PP_ERROR_BADARGUMENT
;
154 if (!size
|| size
->width
<= 0 || size
->height
<= 0)
155 return PP_ERROR_BADARGUMENT
;
157 PPB_Graphics3D_Shared
* graphics
=
158 static_cast<PPB_Graphics3D_Shared
*>(enter
.object());
160 GLES2Implementation
* gl
= graphics
->gles2_impl();
162 // Generate a Mailbox for the texture.
163 gl
->GenMailboxCHROMIUM(
164 reinterpret_cast<GLbyte
*>(data_
.texture
->mailbox
.name
));
165 gl
->ProduceTextureDirectCHROMIUM(
167 reinterpret_cast<const GLbyte
*>(data_
.texture
->mailbox
.name
));
169 // Set the source size to (1, 1). It will be used to verify the source_rect
170 // passed to SetSourceRect().
171 source_size_
= PP_MakeFloatSize(1.0f
, 1.0f
);
173 data_
.common
.size
= *size
;
174 data_
.common
.resource_id
= compositor_
->GenerateResourceId();
175 data_
.texture
->target
= target
;
176 data_
.texture
->sync_point
= gl
->InsertSyncPointCHROMIUM();
177 data_
.texture
->source_rect
.point
= PP_MakeFloatPoint(0.0f
, 0.0f
);
178 data_
.texture
->source_rect
.size
= source_size_
;
180 // If the PP_Resource of this layer is released by the plugin, the
181 // release_callback will be aborted immediately, but the texture or image
182 // in this layer may still being used by chromium compositor. So we have to
183 // use ScopedPPResource to keep this resource alive until the texture or image
184 // is released by the chromium compositor.
185 release_callback_
= base::Bind(
187 ScopedPPResource(pp_resource()), // Keep layer alive.
188 ScopedPPResource(context
), // Keep context alive
192 return PP_OK_COMPLETIONPENDING
;
195 int32_t CompositorLayerResource::SetImage(
196 PP_Resource image_data
,
198 const scoped_refptr
<TrackedCallback
>& release_callback
) {
199 int32_t rv
= CheckForSetTextureAndImage(TYPE_IMAGE
, release_callback
);
204 EnterResourceNoLock
<PPB_ImageData_API
> enter(image_data
, true);
206 return PP_ERROR_BADRESOURCE
;
208 PP_ImageDataDesc desc
;
209 if (!enter
.object()->Describe(&desc
))
210 return PP_ERROR_BADARGUMENT
;
212 // TODO(penghuang): Support image which width * 4 != stride.
213 if (desc
.size
.width
* 4 != desc
.stride
)
214 return PP_ERROR_BADARGUMENT
;
216 // TODO(penghuang): Support all formats.
217 if (desc
.format
!= PP_IMAGEDATAFORMAT_RGBA_PREMUL
)
218 return PP_ERROR_BADARGUMENT
;
220 if (!size
|| size
->width
<= 0 || size
->height
<= 0)
221 return PP_ERROR_BADARGUMENT
;
223 // Set the source size to image's size. It will be used to verify
224 // the source_rect passed to SetSourceRect().
225 source_size_
= PP_MakeFloatSize(desc
.size
.width
, desc
.size
.height
);
227 data_
.common
.size
= size
? *size
: desc
.size
;
228 data_
.common
.resource_id
= compositor_
->GenerateResourceId();
229 data_
.image
->resource
= enter
.resource()->host_resource().host_resource();
230 data_
.image
->source_rect
.point
= PP_MakeFloatPoint(0.0f
, 0.0f
);
231 data_
.image
->source_rect
.size
= source_size_
;
233 // If the PP_Resource of this layer is released by the plugin, the
234 // release_callback will be aborted immediately, but the texture or image
235 // in this layer may still being used by chromium compositor. So we have to
236 // use ScopedPPResource to keep this resource alive until the texture or image
237 // is released by the chromium compositor.
238 release_callback_
= base::Bind(
240 ScopedPPResource(pp_resource()), // Keep layer alive.
241 ScopedPPResource(image_data
), // Keep image_data alive.
244 return PP_OK_COMPLETIONPENDING
;
247 int32_t CompositorLayerResource::SetClipRect(const PP_Rect
* rect
) {
249 return PP_ERROR_BADRESOURCE
;
251 if (compositor_
->IsInProgress())
252 return PP_ERROR_INPROGRESS
;
254 data_
.common
.clip_rect
= rect
? *rect
: PP_MakeRectFromXYWH(0, 0, 0, 0);
258 int32_t CompositorLayerResource::SetTransform(const float matrix
[16]) {
260 return PP_ERROR_BADRESOURCE
;
262 if (compositor_
->IsInProgress())
263 return PP_ERROR_INPROGRESS
;
265 std::copy(matrix
, matrix
+ 16, data_
.common
.transform
.matrix
);
269 int32_t CompositorLayerResource::SetOpacity(float opacity
) {
271 return PP_ERROR_BADRESOURCE
;
273 if (compositor_
->IsInProgress())
274 return PP_ERROR_INPROGRESS
;
276 data_
.common
.opacity
= clamp(opacity
);
280 int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode
) {
282 return PP_ERROR_BADRESOURCE
;
284 if (compositor_
->IsInProgress())
285 return PP_ERROR_INPROGRESS
;
288 case PP_BLENDMODE_NONE
:
289 case PP_BLENDMODE_SRC_OVER
:
290 data_
.common
.blend_mode
= mode
;
293 return PP_ERROR_BADARGUMENT
;
296 int32_t CompositorLayerResource::SetSourceRect(
297 const PP_FloatRect
* rect
) {
299 return PP_ERROR_BADRESOURCE
;
301 if (compositor_
->IsInProgress())
302 return PP_ERROR_INPROGRESS
;
305 rect
->point
.x
< 0.0f
||
306 rect
->point
.y
< 0.0f
||
307 rect
->point
.x
+ rect
->size
.width
> source_size_
.width
||
308 rect
->point
.y
+ rect
->size
.height
> source_size_
.height
) {
309 return PP_ERROR_BADARGUMENT
;
313 data_
.texture
->source_rect
= *rect
;
317 data_
.image
->source_rect
= *rect
;
320 return PP_ERROR_BADARGUMENT
;
323 int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult
) {
325 return PP_ERROR_BADRESOURCE
;
327 if (compositor_
->IsInProgress())
328 return PP_ERROR_INPROGRESS
;
331 data_
.texture
->premult_alpha
= PP_ToBool(premult
);
334 return PP_ERROR_BADARGUMENT
;
337 bool CompositorLayerResource::SetType(LayerType type
) {
338 if (type
== TYPE_COLOR
) {
340 data_
.color
.reset(new CompositorLayerData::ColorLayer());
344 if (type
== TYPE_TEXTURE
) {
346 data_
.texture
.reset(new CompositorLayerData::TextureLayer());
347 return data_
.texture
;
350 if (type
== TYPE_IMAGE
) {
352 data_
.image
.reset(new CompositorLayerData::ImageLayer());
356 // Should not be reached.
361 int32_t CompositorLayerResource::CheckForSetTextureAndImage(
363 const scoped_refptr
<TrackedCallback
>& release_callback
) {
365 return PP_ERROR_BADRESOURCE
;
367 if (compositor_
->IsInProgress())
368 return PP_ERROR_INPROGRESS
;
371 return PP_ERROR_BADARGUMENT
;
373 // The layer's image has been set and it is not committed.
374 if (!release_callback_
.is_null())
375 return PP_ERROR_INPROGRESS
;
377 // Do not allow using a block callback as a release callback.
378 if (release_callback
->is_blocking())
379 return PP_ERROR_BADARGUMENT
;