Home | History | Annotate | Download | only in layers
      1 // Copyright 2010 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/image_layer.h"
      6 
      7 #include "base/compiler_specific.h"
      8 #include "cc/resources/image_layer_updater.h"
      9 #include "cc/resources/layer_updater.h"
     10 #include "cc/resources/prioritized_resource.h"
     11 #include "cc/resources/resource_update_queue.h"
     12 #include "cc/trees/layer_tree_host.h"
     13 
     14 namespace cc {
     15 
     16 scoped_refptr<ImageLayer> ImageLayer::Create() {
     17   return make_scoped_refptr(new ImageLayer());
     18 }
     19 
     20 ImageLayer::ImageLayer() : TiledLayer() {}
     21 
     22 ImageLayer::~ImageLayer() {}
     23 
     24 void ImageLayer::SetBitmap(const SkBitmap& bitmap) {
     25   // SetBitmap() currently gets called whenever there is any
     26   // style change that affects the layer even if that change doesn't
     27   // affect the actual contents of the image (e.g. a CSS animation).
     28   // With this check in place we avoid unecessary texture uploads.
     29   if (bitmap.pixelRef() && bitmap.pixelRef() == bitmap_.pixelRef())
     30     return;
     31 
     32   bitmap_ = bitmap;
     33   UpdateDrawsContent(HasDrawableContent());
     34   SetNeedsDisplay();
     35 }
     36 
     37 bool ImageLayer::HasDrawableContent() const {
     38   return !bitmap_.isNull() && TiledLayer::HasDrawableContent();
     39 }
     40 
     41 void ImageLayer::SetTexturePriorities(const PriorityCalculator& priority_calc) {
     42   // Update the tile data before creating all the layer's tiles.
     43   UpdateTileSizeAndTilingOption();
     44 
     45   TiledLayer::SetTexturePriorities(priority_calc);
     46 }
     47 
     48 bool ImageLayer::Update(ResourceUpdateQueue* queue,
     49                         const OcclusionTracker<Layer>* occlusion) {
     50   CreateUpdaterIfNeeded();
     51   if (!updater_->UsingBitmap(bitmap_)) {
     52     updater_->SetBitmap(bitmap_);
     53     UpdateTileSizeAndTilingOption();
     54     InvalidateContentRect(gfx::Rect(content_bounds()));
     55   }
     56   return TiledLayer::Update(queue, occlusion);
     57 }
     58 
     59 void ImageLayer::CreateUpdaterIfNeeded() {
     60   if (updater_.get())
     61     return;
     62 
     63   updater_ = ImageLayerUpdater::Create();
     64   SetTextureFormat(
     65       layer_tree_host()->GetRendererCapabilities().best_texture_format);
     66 }
     67 
     68 LayerUpdater* ImageLayer::Updater() const {
     69   return updater_.get();
     70 }
     71 
     72 void ImageLayer::CalculateContentsScale(float ideal_contents_scale,
     73                                         float* contents_scale_x,
     74                                         float* contents_scale_y,
     75                                         gfx::Size* content_bounds) {
     76   *contents_scale_x = ImageContentsScaleX();
     77   *contents_scale_y = ImageContentsScaleY();
     78   *content_bounds = gfx::Size(bitmap_.width(), bitmap_.height());
     79 }
     80 
     81 void ImageLayer::OnOutputSurfaceCreated() {
     82   SetTextureFormat(
     83       layer_tree_host()->GetRendererCapabilities().best_texture_format);
     84   TiledLayer::OnOutputSurfaceCreated();
     85 }
     86 
     87 float ImageLayer::ImageContentsScaleX() const {
     88   if (bounds().IsEmpty() || bitmap_.width() == 0)
     89     return 1;
     90   return static_cast<float>(bitmap_.width()) / bounds().width();
     91 }
     92 
     93 float ImageLayer::ImageContentsScaleY() const {
     94   if (bounds().IsEmpty() || bitmap_.height() == 0)
     95     return 1;
     96   return static_cast<float>(bitmap_.height()) / bounds().height();
     97 }
     98 
     99 }  // namespace cc
    100