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/single_release_callback.h"
11 #include "cc/resources/texture_mailbox.h"
12 #include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
13 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
14 #include "third_party/WebKit/public/platform/WebFloatRect.h"
15 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
16 #include "third_party/WebKit/public/platform/WebSize.h"
17 #include "third_party/khronos/GLES2/gl2.h"
19 using cc::TextureLayer
;
23 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl(
24 blink::WebExternalTextureLayerClient
* client
)
26 cc::TextureLayerClient
* cc_client
= client_
? this : nullptr;
27 scoped_refptr
<TextureLayer
> layer
=
28 TextureLayer::CreateForMailbox(WebLayerImpl::LayerSettings(), cc_client
);
29 layer
->SetIsDrawable(true);
30 layer_
.reset(new WebLayerImpl(layer
));
33 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() {
34 static_cast<TextureLayer
*>(layer_
->layer())->ClearClient();
37 blink::WebLayer
* WebExternalTextureLayerImpl::layer() {
41 void WebExternalTextureLayerImpl::clearTexture() {
42 TextureLayer
* layer
= static_cast<TextureLayer
*>(layer_
->layer());
43 layer
->ClearTexture();
46 void WebExternalTextureLayerImpl::setOpaque(bool opaque
) {
47 static_cast<TextureLayer
*>(layer_
->layer())->SetContentsOpaque(opaque
);
50 void WebExternalTextureLayerImpl::setPremultipliedAlpha(
51 bool premultiplied_alpha
) {
52 static_cast<TextureLayer
*>(layer_
->layer())
53 ->SetPremultipliedAlpha(premultiplied_alpha
);
56 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend
) {
57 static_cast<TextureLayer
*>(layer_
->layer())->SetBlendBackgroundColor(blend
);
60 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit
) {
61 static_cast<TextureLayer
*>(layer_
->layer())->SetRateLimitContext(rate_limit
);
64 void WebExternalTextureLayerImpl::setNearestNeighbor(bool nearest_neighbor
) {
65 static_cast<TextureLayer
*>(layer_
->layer())
66 ->SetNearestNeighbor(nearest_neighbor
);
69 bool WebExternalTextureLayerImpl::PrepareTextureMailbox(
70 cc::TextureMailbox
* mailbox
,
71 scoped_ptr
<cc::SingleReleaseCallback
>* release_callback
,
72 bool use_shared_memory
) {
73 blink::WebExternalTextureMailbox client_mailbox
;
74 WebExternalBitmapImpl
* bitmap
= nullptr;
76 if (use_shared_memory
)
77 bitmap
= AllocateBitmap();
78 if (!client_
->prepareMailbox(&client_mailbox
, bitmap
)) {
80 free_bitmaps_
.push_back(bitmap
);
84 name
.SetName(client_mailbox
.name
);
86 *mailbox
= cc::TextureMailbox(bitmap
->shared_bitmap(), bitmap
->size());
88 // TODO(achaulk): pass a valid size here if allowOverlay is set.
89 *mailbox
= cc::TextureMailbox(name
, GL_TEXTURE_2D
, client_mailbox
.syncPoint
,
90 gfx::Size(), client_mailbox
.allowOverlay
);
92 mailbox
->set_nearest_neighbor(client_mailbox
.nearestNeighbor
);
94 if (mailbox
->IsValid()) {
95 *release_callback
= cc::SingleReleaseCallback::Create(
96 base::Bind(&WebExternalTextureLayerImpl::DidReleaseMailbox
,
105 WebExternalBitmapImpl
* WebExternalTextureLayerImpl::AllocateBitmap() {
106 if (!free_bitmaps_
.empty()) {
107 WebExternalBitmapImpl
* result
= free_bitmaps_
.back();
108 free_bitmaps_
.weak_erase(free_bitmaps_
.end() - 1);
111 return new WebExternalBitmapImpl
;
115 void WebExternalTextureLayerImpl::DidReleaseMailbox(
116 base::WeakPtr
<WebExternalTextureLayerImpl
> layer
,
117 const blink::WebExternalTextureMailbox
& mailbox
,
118 WebExternalBitmapImpl
* bitmap
,
120 bool lost_resource
) {
122 blink::WebExternalTextureMailbox available_mailbox
;
123 memcpy(available_mailbox
.name
, mailbox
.name
, sizeof(available_mailbox
.name
));
124 available_mailbox
.syncPoint
= sync_point
;
126 layer
->free_bitmaps_
.push_back(bitmap
);
127 layer
->client_
->mailboxReleased(available_mailbox
, lost_resource
);
130 } // namespace cc_blink