1 // Copyright 2011 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 "webkit/renderer/compositor_bindings/web_external_texture_layer_impl.h"
7 #include "cc/layers/texture_layer.h"
8 #include "cc/resources/resource_update_queue.h"
9 #include "cc/resources/single_release_callback.h"
10 #include "cc/resources/texture_mailbox.h"
11 #include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
12 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
13 #include "third_party/WebKit/public/platform/WebFloatRect.h"
14 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
15 #include "third_party/WebKit/public/platform/WebSize.h"
16 #include "third_party/khronos/GLES2/gl2.h"
17 #include "webkit/renderer/compositor_bindings/web_external_bitmap_impl.h"
18 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
20 using cc::TextureLayer;
21 using cc::ResourceUpdateQueue;
25 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl(
26 blink::WebExternalTextureLayerClient* client)
28 cc::TextureLayerClient* cc_client = client_ ? this : NULL;
29 scoped_refptr<TextureLayer> layer = TextureLayer::CreateForMailbox(cc_client);
30 layer->SetIsDrawable(true);
31 layer_.reset(new WebLayerImpl(layer));
34 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() {
35 static_cast<TextureLayer*>(layer_->layer())->ClearClient();
38 blink::WebLayer* WebExternalTextureLayerImpl::layer() { return layer_.get(); }
40 void WebExternalTextureLayerImpl::clearTexture() {
41 TextureLayer *layer = static_cast<TextureLayer*>(layer_->layer());
42 layer->WillModifyTexture();
43 layer->SetTextureMailbox(cc::TextureMailbox(),
44 scoped_ptr<cc::SingleReleaseCallback>());
47 void WebExternalTextureLayerImpl::setOpaque(bool opaque) {
48 static_cast<TextureLayer*>(layer_->layer())->SetContentsOpaque(opaque);
51 void WebExternalTextureLayerImpl::setPremultipliedAlpha(
52 bool premultiplied_alpha) {
53 static_cast<TextureLayer*>(layer_->layer())->SetPremultipliedAlpha(
57 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend) {
58 static_cast<TextureLayer*>(layer_->layer())->SetBlendBackgroundColor(blend);
61 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit) {
62 static_cast<TextureLayer*>(layer_->layer())->SetRateLimitContext(rate_limit);
65 unsigned WebExternalTextureLayerImpl::PrepareTexture() {
70 bool WebExternalTextureLayerImpl::PrepareTextureMailbox(
71 cc::TextureMailbox* mailbox,
72 scoped_ptr<cc::SingleReleaseCallback>* release_callback,
73 bool use_shared_memory) {
74 blink::WebExternalTextureMailbox client_mailbox;
75 WebExternalBitmapImpl* bitmap = NULL;
77 if (use_shared_memory)
78 bitmap = AllocateBitmap();
79 if (!client_->prepareMailbox(&client_mailbox, bitmap)) {
81 free_bitmaps_.push_back(bitmap);
85 name.SetName(client_mailbox.name);
87 *mailbox = cc::TextureMailbox(bitmap->shared_memory(), bitmap->size());
90 cc::TextureMailbox(name, GL_TEXTURE_2D, client_mailbox.syncPoint);
93 if (mailbox->IsValid()) {
94 *release_callback = cc::SingleReleaseCallback::Create(base::Bind(
95 &WebExternalTextureLayerImpl::DidReleaseMailbox,
104 WebExternalBitmapImpl* WebExternalTextureLayerImpl::AllocateBitmap() {
105 if (!free_bitmaps_.empty()) {
106 WebExternalBitmapImpl* result = free_bitmaps_.back();
107 free_bitmaps_.weak_erase(free_bitmaps_.end() - 1);
110 return new WebExternalBitmapImpl;
114 void WebExternalTextureLayerImpl::DidReleaseMailbox(
115 base::WeakPtr<WebExternalTextureLayerImpl> layer,
116 const blink::WebExternalTextureMailbox& mailbox,
117 WebExternalBitmapImpl* bitmap,
119 bool lost_resource) {
120 if (lost_resource || !layer) {
125 blink::WebExternalTextureMailbox available_mailbox;
126 memcpy(available_mailbox.name, mailbox.name, sizeof(available_mailbox.name));
127 available_mailbox.syncPoint = sync_point;
129 layer->free_bitmaps_.push_back(bitmap);
130 layer->client_->mailboxReleased(available_mailbox);
133 } // namespace webkit