Home | History | Annotate | Download | only in layers
      1 // Copyright 2012 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 "cc/layers/nine_patch_layer.h"
      6 
      7 #include "cc/layers/nine_patch_layer_impl.h"
      8 #include "cc/resources/prioritized_resource.h"
      9 #include "cc/resources/resource_update.h"
     10 #include "cc/resources/resource_update_queue.h"
     11 #include "cc/trees/layer_tree_host.h"
     12 
     13 namespace cc {
     14 
     15 scoped_refptr<NinePatchLayer> NinePatchLayer::Create() {
     16   return make_scoped_refptr(new NinePatchLayer());
     17 }
     18 
     19 NinePatchLayer::NinePatchLayer()
     20     : bitmap_dirty_(false) {}
     21 
     22 NinePatchLayer::~NinePatchLayer() {}
     23 
     24 scoped_ptr<LayerImpl> NinePatchLayer::CreateLayerImpl(
     25     LayerTreeImpl* tree_impl) {
     26   return NinePatchLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
     27 }
     28 
     29 void NinePatchLayer::SetTexturePriorities(
     30     const PriorityCalculator& priority_calc) {
     31   if (resource_ && !resource_->texture()->resource_manager()) {
     32     // Release the resource here, as it is no longer tied to a resource manager.
     33     resource_.reset();
     34     if (!bitmap_.isNull())
     35       CreateResource();
     36   } else if (bitmap_dirty_ && DrawsContent()) {
     37     CreateResource();
     38   }
     39 
     40   if (resource_) {
     41     resource_->texture()->set_request_priority(
     42         PriorityCalculator::UIPriority(true));
     43     GLenum texture_format =
     44         layer_tree_host()->GetRendererCapabilities().best_texture_format;
     45     resource_->texture()->SetDimensions(
     46         gfx::Size(bitmap_.width(), bitmap_.height()), texture_format);
     47   }
     48 }
     49 
     50 void NinePatchLayer::SetBitmap(const SkBitmap& bitmap, gfx::Rect aperture) {
     51   bitmap_ = bitmap;
     52   image_aperture_ = aperture;
     53   bitmap_dirty_ = true;
     54   SetNeedsDisplay();
     55 }
     56 
     57 bool NinePatchLayer::Update(ResourceUpdateQueue* queue,
     58                             const OcclusionTracker* occlusion) {
     59   bool updated = Layer::Update(queue, occlusion);
     60 
     61   CreateUpdaterIfNeeded();
     62 
     63   if (resource_ &&
     64       (bitmap_dirty_ || resource_->texture()->resource_id() == 0)) {
     65     gfx::Rect content_rect(0, 0, bitmap_.width(), bitmap_.height());
     66     ResourceUpdate upload = ResourceUpdate::Create(resource_->texture(),
     67                                                    &bitmap_,
     68                                                    content_rect,
     69                                                    content_rect,
     70                                                    gfx::Vector2d());
     71     queue->AppendFullUpload(upload);
     72     bitmap_dirty_ = false;
     73     updated = true;
     74   }
     75   return updated;
     76 }
     77 
     78 void NinePatchLayer::CreateUpdaterIfNeeded() {
     79   if (updater_.get())
     80     return;
     81 
     82   updater_ = ImageLayerUpdater::Create();
     83 }
     84 
     85 void NinePatchLayer::CreateResource() {
     86   DCHECK(!bitmap_.isNull());
     87   CreateUpdaterIfNeeded();
     88   updater_->SetBitmap(bitmap_);
     89 
     90   if (!resource_) {
     91     resource_ = updater_->CreateResource(
     92         layer_tree_host()->contents_texture_manager());
     93   }
     94 }
     95 
     96 bool NinePatchLayer::DrawsContent() const {
     97   bool draws = !bitmap_.isNull() &&
     98                Layer::DrawsContent() &&
     99                bitmap_.width() &&
    100                bitmap_.height();
    101   return draws;
    102 }
    103 
    104 void NinePatchLayer::PushPropertiesTo(LayerImpl* layer) {
    105   Layer::PushPropertiesTo(layer);
    106   NinePatchLayerImpl* layer_impl = static_cast<NinePatchLayerImpl*>(layer);
    107 
    108   if (resource_) {
    109     DCHECK(!bitmap_.isNull());
    110     layer_impl->SetResourceId(resource_->texture()->resource_id());
    111     layer_impl->SetLayout(
    112         gfx::Size(bitmap_.width(), bitmap_.height()), image_aperture_);
    113   }
    114 }
    115 
    116 }  // namespace cc
    117