Home | History | Annotate | Download | only in compositor_bindings
      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 "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
     10 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
     11 #include "third_party/WebKit/public/platform/WebFloatRect.h"
     12 #include "third_party/WebKit/public/platform/WebSize.h"
     13 #include "webkit/renderer/compositor_bindings/web_external_bitmap_impl.h"
     14 #include "webkit/renderer/compositor_bindings/web_layer_impl.h"
     15 
     16 using cc::TextureLayer;
     17 using cc::ResourceUpdateQueue;
     18 
     19 namespace webkit {
     20 
     21 WebExternalTextureLayerImpl::WebExternalTextureLayerImpl(
     22     WebKit::WebExternalTextureLayerClient* client)
     23     : client_(client) {
     24   cc::TextureLayerClient* cc_client = client_ ? this : NULL;
     25   scoped_refptr<TextureLayer> layer = TextureLayer::CreateForMailbox(cc_client);
     26   layer->SetIsDrawable(true);
     27   layer_.reset(new WebLayerImpl(layer));
     28 }
     29 
     30 WebExternalTextureLayerImpl::~WebExternalTextureLayerImpl() {
     31   static_cast<TextureLayer*>(layer_->layer())->ClearClient();
     32 }
     33 
     34 WebKit::WebLayer* WebExternalTextureLayerImpl::layer() { return layer_.get(); }
     35 
     36 void WebExternalTextureLayerImpl::clearTexture() {
     37   TextureLayer *layer = static_cast<TextureLayer*>(layer_->layer());
     38   layer->WillModifyTexture();
     39   layer->SetTextureMailbox(cc::TextureMailbox());
     40 }
     41 
     42 void WebExternalTextureLayerImpl::setOpaque(bool opaque) {
     43   static_cast<TextureLayer*>(layer_->layer())->SetContentsOpaque(opaque);
     44 }
     45 
     46 void WebExternalTextureLayerImpl::setPremultipliedAlpha(
     47     bool premultiplied_alpha) {
     48   static_cast<TextureLayer*>(layer_->layer())->SetPremultipliedAlpha(
     49       premultiplied_alpha);
     50 }
     51 
     52 void WebExternalTextureLayerImpl::setBlendBackgroundColor(bool blend) {
     53   static_cast<TextureLayer*>(layer_->layer())->SetBlendBackgroundColor(blend);
     54 }
     55 
     56 void WebExternalTextureLayerImpl::setRateLimitContext(bool rate_limit) {
     57   static_cast<TextureLayer*>(layer_->layer())->SetRateLimitContext(rate_limit);
     58 }
     59 
     60 unsigned WebExternalTextureLayerImpl::PrepareTexture() {
     61   NOTREACHED();
     62   return 0;
     63 }
     64 
     65 WebKit::WebGraphicsContext3D* WebExternalTextureLayerImpl::Context3d() {
     66   DCHECK(client_);
     67   return client_->context();
     68 }
     69 
     70 bool WebExternalTextureLayerImpl::PrepareTextureMailbox(
     71     cc::TextureMailbox* mailbox,
     72     bool use_shared_memory) {
     73   WebKit::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   cc::TextureMailbox::ReleaseCallback callback =
     86       base::Bind(&WebExternalTextureLayerImpl::DidReleaseMailbox,
     87                  this->AsWeakPtr(),
     88                  client_mailbox,
     89                  bitmap);
     90   if (bitmap) {
     91     *mailbox =
     92         cc::TextureMailbox(bitmap->shared_memory(), bitmap->size(), callback);
     93   } else {
     94     *mailbox = cc::TextureMailbox(name, callback, client_mailbox.syncPoint);
     95   }
     96   return true;
     97 }
     98 
     99 WebExternalBitmapImpl* WebExternalTextureLayerImpl::AllocateBitmap() {
    100   if (!free_bitmaps_.empty()) {
    101     WebExternalBitmapImpl* result = free_bitmaps_.back();
    102     free_bitmaps_.weak_erase(free_bitmaps_.end() - 1);
    103     return result;
    104   }
    105   return new WebExternalBitmapImpl;
    106 }
    107 
    108 // static
    109 void WebExternalTextureLayerImpl::DidReleaseMailbox(
    110     base::WeakPtr<WebExternalTextureLayerImpl> layer,
    111     const WebKit::WebExternalTextureMailbox& mailbox,
    112     WebExternalBitmapImpl* bitmap,
    113     unsigned sync_point,
    114     bool lost_resource) {
    115   if (lost_resource || !layer) {
    116     delete bitmap;
    117     return;
    118   }
    119 
    120   WebKit::WebExternalTextureMailbox available_mailbox;
    121   memcpy(available_mailbox.name, mailbox.name, sizeof(available_mailbox.name));
    122   available_mailbox.syncPoint = sync_point;
    123   if (bitmap)
    124     layer->free_bitmaps_.push_back(bitmap);
    125   layer->client_->mailboxReleased(available_mailbox);
    126 }
    127 
    128 }  // namespace webkit
    129