summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/compositor_layer_resource.cc
blob: 3507c925d9070cb70515f02b8bc69149e2f90a0c (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2014 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 "ppapi/proxy/compositor_layer_resource.h"

#include <limits>

#include "base/logging.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "ppapi/proxy/compositor_resource.h"
#include "ppapi/shared_impl/ppb_graphics_3d_shared.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_graphics_3d_api.h"
#include "ppapi/thunk/ppb_image_data_api.h"

using gpu::gles2::GLES2Implementation;
using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_ImageData_API;
using ppapi::thunk::PPB_Graphics3D_API;

namespace ppapi {
namespace proxy {

namespace {

float clamp(float value) {
  return std::min(std::max(value, 0.0f), 1.0f);
}

void OnTextureReleased(const ScopedPPResource& layer,
                       const ScopedPPResource& context,
                       uint32_t texture,
                       const scoped_refptr<TrackedCallback>& release_callback,
                       int32_t result,
                       const gpu::SyncToken& sync_token,
                       bool is_lost) {
  if (!TrackedCallback::IsPending(release_callback))
    return;

  if (result != PP_OK) {
    release_callback->Run(result);
    return;
  }

  do {
    if (!sync_token.HasData())
      break;

    EnterResourceNoLock<PPB_Graphics3D_API> enter(context.get(), true);
    if (enter.failed())
      break;

    PPB_Graphics3D_Shared* graphics =
        static_cast<PPB_Graphics3D_Shared*>(enter.object());

    GLES2Implementation* gl = graphics->gles2_impl();
    gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData());
  } while (false);

  release_callback->Run(is_lost ? PP_ERROR_FAILED : PP_OK);
}

void OnImageReleased(const ScopedPPResource& layer,
                     const ScopedPPResource& image,
                     const scoped_refptr<TrackedCallback>& release_callback,
                     int32_t result,
                     const gpu::SyncToken& sync_token,
                     bool is_lost) {
  if (!TrackedCallback::IsPending(release_callback))
    return;
  release_callback->Run(result);
}

}  // namespace

CompositorLayerResource::CompositorLayerResource(
    Connection connection,
    PP_Instance instance,
    const CompositorResource* compositor)
    : PluginResource(connection, instance),
      compositor_(compositor),
      source_size_(PP_MakeFloatSize(0.0f, 0.0f)) {
}

CompositorLayerResource::~CompositorLayerResource() {
  DCHECK(!compositor_);
  DCHECK(release_callback_.is_null());
}

thunk::PPB_CompositorLayer_API*
CompositorLayerResource::AsPPB_CompositorLayer_API() {
  return this;
}

int32_t CompositorLayerResource::SetColor(float red,
                                          float green,
                                          float blue,
                                          float alpha,
                                          const PP_Size* size) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  if (!SetType(TYPE_COLOR))
    return PP_ERROR_BADARGUMENT;
  DCHECK(data_.color);

  if (!size)
    return PP_ERROR_BADARGUMENT;

  data_.color->red = clamp(red);
  data_.color->green = clamp(green);
  data_.color->blue = clamp(blue);
  data_.color->alpha = clamp(alpha);
  data_.common.size = *size;

  return PP_OK;
}

int32_t CompositorLayerResource::SetTexture0_1(
    PP_Resource context,
    uint32_t texture,
    const PP_Size* size,
    const scoped_refptr<TrackedCallback>& release_callback) {
  return SetTexture(context, GL_TEXTURE_2D, texture, size, release_callback);
}

int32_t CompositorLayerResource::SetTexture(
    PP_Resource context,
    uint32_t target,
    uint32_t texture,
    const PP_Size* size,
    const scoped_refptr<TrackedCallback>& release_callback) {
  int32_t rv = CheckForSetTextureAndImage(TYPE_TEXTURE, release_callback);
  if (rv != PP_OK)
    return rv;
  DCHECK(data_.texture);

  EnterResourceNoLock<PPB_Graphics3D_API> enter(context, true);
  if (enter.failed())
    return PP_ERROR_BADRESOURCE;

  if (target != GL_TEXTURE_2D &&
      target != GL_TEXTURE_EXTERNAL_OES &&
      target != GL_TEXTURE_RECTANGLE_ARB) {
    return PP_ERROR_BADARGUMENT;
  }

  if (!size || size->width <= 0 || size->height <= 0)
    return PP_ERROR_BADARGUMENT;

  PPB_Graphics3D_Shared* graphics =
      static_cast<PPB_Graphics3D_Shared*>(enter.object());

  GLES2Implementation* gl = graphics->gles2_impl();

  // Generate a Mailbox for the texture.
  gl->GenMailboxCHROMIUM(
      reinterpret_cast<GLbyte*>(data_.texture->mailbox.name));
  gl->ProduceTextureDirectCHROMIUM(
      texture, target,
      reinterpret_cast<const GLbyte*>(data_.texture->mailbox.name));

  // Set the source size to (1, 1). It will be used to verify the source_rect
  // passed to SetSourceRect().
  source_size_ = PP_MakeFloatSize(1.0f, 1.0f);

  data_.common.size = *size;
  data_.common.resource_id = compositor_->GenerateResourceId();
  data_.texture->target = target;
  data_.texture->sync_token = gpu::SyncToken(gl->InsertSyncPointCHROMIUM());
  data_.texture->source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f);
  data_.texture->source_rect.size = source_size_;

  // If the PP_Resource of this layer is released by the plugin, the
  // release_callback will be aborted immediately, but the texture or image
  // in this layer may still being used by chromium compositor. So we have to
  // use ScopedPPResource to keep this resource alive until the texture or image
  // is released by the chromium compositor.
  release_callback_ = base::Bind(
      &OnTextureReleased,
      ScopedPPResource(pp_resource()), // Keep layer alive.
      ScopedPPResource(context), // Keep context alive
      texture,
      release_callback);

  return PP_OK_COMPLETIONPENDING;
}

int32_t CompositorLayerResource::SetImage(
    PP_Resource image_data,
    const PP_Size* size,
    const scoped_refptr<TrackedCallback>& release_callback) {
  int32_t rv = CheckForSetTextureAndImage(TYPE_IMAGE, release_callback);
  if (rv != PP_OK)
    return rv;
  DCHECK(data_.image);

  EnterResourceNoLock<PPB_ImageData_API> enter(image_data, true);
  if (enter.failed())
    return PP_ERROR_BADRESOURCE;

  PP_ImageDataDesc desc;
  if (!enter.object()->Describe(&desc))
    return PP_ERROR_BADARGUMENT;

  // TODO(penghuang): Support image which width * 4 != stride.
  if (desc.size.width * 4 != desc.stride)
    return PP_ERROR_BADARGUMENT;

  // TODO(penghuang): Support all formats.
  if (desc.format != PP_IMAGEDATAFORMAT_RGBA_PREMUL)
    return PP_ERROR_BADARGUMENT;

  if (size && (size->width <= 0 || size->height <= 0))
    return PP_ERROR_BADARGUMENT;

  // Set the source size to image's size. It will be used to verify
  // the source_rect passed to SetSourceRect().
  source_size_ = PP_MakeFloatSize(desc.size.width, desc.size.height);

  data_.common.size = size ? *size : desc.size;
  data_.common.resource_id = compositor_->GenerateResourceId();
  data_.image->resource = enter.resource()->host_resource().host_resource();
  data_.image->source_rect.point = PP_MakeFloatPoint(0.0f, 0.0f);
  data_.image->source_rect.size = source_size_;

  // If the PP_Resource of this layer is released by the plugin, the
  // release_callback will be aborted immediately, but the texture or image
  // in this layer may still being used by chromium compositor. So we have to
  // use ScopedPPResource to keep this resource alive until the texture or image
  // is released by the chromium compositor.
  release_callback_ = base::Bind(
      &OnImageReleased,
      ScopedPPResource(pp_resource()), // Keep layer alive.
      ScopedPPResource(image_data), // Keep image_data alive.
      release_callback);

  return PP_OK_COMPLETIONPENDING;
}

int32_t CompositorLayerResource::SetClipRect(const PP_Rect* rect) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  data_.common.clip_rect = rect ? *rect : PP_MakeRectFromXYWH(0, 0, 0, 0);
  return PP_OK;
}

int32_t CompositorLayerResource::SetTransform(const float matrix[16]) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  std::copy(matrix, matrix + 16, data_.common.transform.matrix);
  return PP_OK;
}

int32_t CompositorLayerResource::SetOpacity(float opacity) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  data_.common.opacity = clamp(opacity);
  return PP_OK;
}

int32_t CompositorLayerResource::SetBlendMode(PP_BlendMode mode) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  switch (mode) {
    case PP_BLENDMODE_NONE:
    case PP_BLENDMODE_SRC_OVER:
      data_.common.blend_mode = mode;
      return PP_OK;
  }
  return PP_ERROR_BADARGUMENT;
}

int32_t CompositorLayerResource::SetSourceRect(
    const PP_FloatRect* rect) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  const float kEpsilon = std::numeric_limits<float>::epsilon();
  if (!rect ||
      rect->point.x < -kEpsilon ||
      rect->point.y < -kEpsilon ||
      rect->point.x + rect->size.width > source_size_.width + kEpsilon ||
      rect->point.y + rect->size.height > source_size_.height + kEpsilon) {
    return PP_ERROR_BADARGUMENT;
  }

  if (data_.texture) {
    data_.texture->source_rect = *rect;
    return PP_OK;
  }
  if (data_.image) {
    data_.image->source_rect = *rect;
    return PP_OK;
  }
  return PP_ERROR_BADARGUMENT;
}

int32_t CompositorLayerResource::SetPremultipliedAlpha(PP_Bool premult) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  if (data_.texture) {
    data_.texture->premult_alpha = PP_ToBool(premult);
    return PP_OK;
  }
  return PP_ERROR_BADARGUMENT;
}

bool CompositorLayerResource::SetType(LayerType type) {
  if (type == TYPE_COLOR) {
    if (data_.is_null())
      data_.color.reset(new CompositorLayerData::ColorLayer());
    return data_.color;
  }

  if (type == TYPE_TEXTURE) {
    if (data_.is_null())
      data_.texture.reset(new CompositorLayerData::TextureLayer());
    return data_.texture;
  }

  if (type == TYPE_IMAGE) {
    if (data_.is_null())
      data_.image.reset(new CompositorLayerData::ImageLayer());
    return data_.image;
  }

  // Should not be reached.
  DCHECK(false);
  return false;
}

int32_t CompositorLayerResource::CheckForSetTextureAndImage(
    LayerType type,
    const scoped_refptr<TrackedCallback>& release_callback) {
  if (!compositor_)
    return PP_ERROR_BADRESOURCE;

  if (compositor_->IsInProgress())
    return PP_ERROR_INPROGRESS;

  if (!SetType(type))
    return PP_ERROR_BADARGUMENT;

  // The layer's image has been set and it is not committed.
  if (!release_callback_.is_null())
    return PP_ERROR_INPROGRESS;

  // Do not allow using a block callback as a release callback.
  if (release_callback->is_blocking())
    return PP_ERROR_BADARGUMENT;

  return PP_OK;
}

}  // namespace proxy
}  // namespace ppapi