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. 4 5 #include "webkit/renderer/compositor_bindings/web_external_texture_layer_impl.h" 6 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 "webkit/renderer/compositor_bindings/web_external_bitmap_impl.h" 17 #include "webkit/renderer/compositor_bindings/web_layer_impl.h" 18 19 using cc::TextureLayer; 20 using cc::ResourceUpdateQueue; 21 22 namespace webkit { 23 24 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl( 25 blink::WebExternalTextureLayerClient* client) 26 : client_(client) { 27 cc::TextureLayerClient* cc_client = client_ ? this : NULL; 28 scoped_refptr<TextureLayer> layer = TextureLayer::CreateForMailbox(cc_client); 29 layer->SetIsDrawable(true); 30 layer_.reset(new WebLayerImpl(layer)); 31 } 32 33 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() { 34 static_cast<TextureLayer*>(layer_->layer())->ClearClient(); 35 } 36 37 blink::WebLayer* WebExternalTextureLayerImpl::layer() { return layer_.get(); } 38 39 void WebExternalTextureLayerImpl::clearTexture() { 40 TextureLayer *layer = static_cast<TextureLayer*>(layer_->layer()); 41 layer->WillModifyTexture(); 42 layer->SetTextureMailbox(cc::TextureMailbox(), 43 scoped_ptr<cc::SingleReleaseCallback>()); 44 } 45 46 void WebExternalTextureLayerImpl::setOpaque(bool opaque) { 47 static_cast<TextureLayer*>(layer_->layer())->SetContentsOpaque(opaque); 48 } 49 50 void WebExternalTextureLayerImpl::setPremultipliedAlpha( 51 bool premultiplied_alpha) { 52 static_cast<TextureLayer*>(layer_->layer())->SetPremultipliedAlpha( 53 premultiplied_alpha); 54 } 55 56 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend) { 57 static_cast<TextureLayer*>(layer_->layer())->SetBlendBackgroundColor(blend); 58 } 59 60 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit) { 61 static_cast<TextureLayer*>(layer_->layer())->SetRateLimitContext(rate_limit); 62 } 63 64 unsigned WebExternalTextureLayerImpl::PrepareTexture() { 65 NOTREACHED(); 66 return 0; 67 } 68 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 = NULL; 75 76 if (use_shared_memory) 77 bitmap = AllocateBitmap(); 78 if (!client_->prepareMailbox(&client_mailbox, bitmap)) { 79 if (bitmap) 80 free_bitmaps_.push_back(bitmap); 81 return false; 82 } 83 gpu::Mailbox name; 84 name.SetName(client_mailbox.name); 85 if (bitmap) 86 *mailbox = cc::TextureMailbox(bitmap->shared_memory(), bitmap->size()); 87 else 88 *mailbox = cc::TextureMailbox(name, client_mailbox.syncPoint); 89 90 if (mailbox->IsValid()) { 91 *release_callback = cc::SingleReleaseCallback::Create(base::Bind( 92 &WebExternalTextureLayerImpl::DidReleaseMailbox, 93 this->AsWeakPtr(), 94 client_mailbox, 95 bitmap)); 96 } 97 98 return true; 99 } 100 101 WebExternalBitmapImpl* WebExternalTextureLayerImpl::AllocateBitmap() { 102 if (!free_bitmaps_.empty()) { 103 WebExternalBitmapImpl* result = free_bitmaps_.back(); 104 free_bitmaps_.weak_erase(free_bitmaps_.end() - 1); 105 return result; 106 } 107 return new WebExternalBitmapImpl; 108 } 109 110 // static 111 void WebExternalTextureLayerImpl::DidReleaseMailbox( 112 base::WeakPtr<WebExternalTextureLayerImpl> layer, 113 const blink::WebExternalTextureMailbox& mailbox, 114 WebExternalBitmapImpl* bitmap, 115 unsigned sync_point, 116 bool lost_resource) { 117 if (lost_resource || !layer) { 118 delete bitmap; 119 return; 120 } 121 122 blink::WebExternalTextureMailbox available_mailbox; 123 memcpy(available_mailbox.name, mailbox.name, sizeof(available_mailbox.name)); 124 available_mailbox.syncPoint = sync_point; 125 if (bitmap) 126 layer->free_bitmaps_.push_back(bitmap); 127 layer->client_->mailboxReleased(available_mailbox); 128 } 129 130 } // namespace webkit 131