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 "cc/blink/web_external_texture_layer_impl.h"
7 #include "cc/blink/web_external_bitmap_impl.h"
8 #include "cc/blink/web_layer_impl.h"
9 #include "cc/layers/texture_layer.h"
10 #include "cc/resources/resource_update_queue.h"
11 #include "cc/resources/single_release_callback.h"
12 #include "cc/resources/texture_mailbox.h"
13 #include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
14 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
15 #include "third_party/WebKit/public/platform/WebFloatRect.h"
16 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
17 #include "third_party/WebKit/public/platform/WebSize.h"
18 #include "third_party/khronos/GLES2/gl2.h"
20 using cc::TextureLayer
;
21 using cc::ResourceUpdateQueue
;
25 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl(
26 blink::WebExternalTextureLayerClient
* client
)
28 cc::TextureLayerClient
* cc_client
= client_
? this : nullptr;
29 scoped_refptr
<TextureLayer
> layer
=
30 TextureLayer::CreateForMailbox(WebLayerImpl::LayerSettings(), cc_client
);
31 layer
->SetIsDrawable(true);
32 layer_
.reset(new WebLayerImpl(layer
));
35 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() {
36 static_cast<TextureLayer
*>(layer_
->layer())->ClearClient();
39 blink::WebLayer
* WebExternalTextureLayerImpl::layer() {
43 void WebExternalTextureLayerImpl::clearTexture() {
44 TextureLayer
* layer
= static_cast<TextureLayer
*>(layer_
->layer());
45 layer
->ClearTexture();
48 void WebExternalTextureLayerImpl::setOpaque(bool opaque
) {
49 static_cast<TextureLayer
*>(layer_
->layer())->SetContentsOpaque(opaque
);
52 void WebExternalTextureLayerImpl::setPremultipliedAlpha(
53 bool premultiplied_alpha
) {
54 static_cast<TextureLayer
*>(layer_
->layer())
55 ->SetPremultipliedAlpha(premultiplied_alpha
);
58 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend
) {
59 static_cast<TextureLayer
*>(layer_
->layer())->SetBlendBackgroundColor(blend
);
62 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit
) {
63 static_cast<TextureLayer
*>(layer_
->layer())->SetRateLimitContext(rate_limit
);
66 void WebExternalTextureLayerImpl::setNearestNeighbor(bool nearest_neighbor
) {
67 static_cast<TextureLayer
*>(layer_
->layer())
68 ->SetNearestNeighbor(nearest_neighbor
);
71 bool WebExternalTextureLayerImpl::PrepareTextureMailbox(
72 cc::TextureMailbox
* mailbox
,
73 scoped_ptr
<cc::SingleReleaseCallback
>* release_callback
,
74 bool use_shared_memory
) {
75 blink::WebExternalTextureMailbox client_mailbox
;
76 WebExternalBitmapImpl
* bitmap
= nullptr;
78 if (use_shared_memory
)
79 bitmap
= AllocateBitmap();
80 if (!client_
->prepareMailbox(&client_mailbox
, bitmap
)) {
82 free_bitmaps_
.push_back(bitmap
);
86 name
.SetName(client_mailbox
.name
);
88 *mailbox
= cc::TextureMailbox(bitmap
->shared_bitmap(), bitmap
->size());
91 cc::TextureMailbox(name
, GL_TEXTURE_2D
, client_mailbox
.syncPoint
);
93 mailbox
->set_allow_overlay(client_mailbox
.allowOverlay
);
94 mailbox
->set_nearest_neighbor(client_mailbox
.nearestNeighbor
);
96 if (mailbox
->IsValid()) {
97 *release_callback
= cc::SingleReleaseCallback::Create(
98 base::Bind(&WebExternalTextureLayerImpl::DidReleaseMailbox
,
107 WebExternalBitmapImpl
* WebExternalTextureLayerImpl::AllocateBitmap() {
108 if (!free_bitmaps_
.empty()) {
109 WebExternalBitmapImpl
* result
= free_bitmaps_
.back();
110 free_bitmaps_
.weak_erase(free_bitmaps_
.end() - 1);
113 return new WebExternalBitmapImpl
;
117 void WebExternalTextureLayerImpl::DidReleaseMailbox(
118 base::WeakPtr
<WebExternalTextureLayerImpl
> layer
,
119 const blink::WebExternalTextureMailbox
& mailbox
,
120 WebExternalBitmapImpl
* bitmap
,
122 bool lost_resource
) {
124 blink::WebExternalTextureMailbox available_mailbox
;
125 memcpy(available_mailbox
.name
, mailbox
.name
, sizeof(available_mailbox
.name
));
126 available_mailbox
.syncPoint
= sync_point
;
128 layer
->free_bitmaps_
.push_back(bitmap
);
129 layer
->client_
->mailboxReleased(available_mailbox
, lost_resource
);
132 } // namespace cc_blink