image_layer.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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
14namespace cc {
15
16scoped_refptr<ImageLayer> ImageLayer::Create() {
17  return make_scoped_refptr(new ImageLayer());
18}
19
20ImageLayer::ImageLayer() : TiledLayer() {}
21
22ImageLayer::~ImageLayer() {}
23
24void 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  SetNeedsDisplay();
34}
35
36void ImageLayer::SetTexturePriorities(const PriorityCalculator& priority_calc) {
37  // Update the tile data before creating all the layer's tiles.
38  UpdateTileSizeAndTilingOption();
39
40  TiledLayer::SetTexturePriorities(priority_calc);
41}
42
43void ImageLayer::Update(ResourceUpdateQueue* queue,
44                        const OcclusionTracker* occlusion) {
45  CreateUpdaterIfNeeded();
46  if (needs_display_) {
47    updater_->set_bitmap(bitmap_);
48    UpdateTileSizeAndTilingOption();
49    InvalidateContentRect(gfx::Rect(content_bounds()));
50    needs_display_ = false;
51  }
52  TiledLayer::Update(queue, occlusion);
53}
54
55void ImageLayer::CreateUpdaterIfNeeded() {
56  if (updater_.get())
57    return;
58
59  updater_ = ImageLayerUpdater::Create();
60  GLenum texture_format =
61      layer_tree_host()->GetRendererCapabilities().best_texture_format;
62  SetTextureFormat(texture_format);
63}
64
65LayerUpdater* ImageLayer::Updater() const {
66  return updater_.get();
67}
68
69void ImageLayer::CalculateContentsScale(float ideal_contents_scale,
70                                        float device_scale_factor,
71                                        float page_scale_factor,
72                                        bool animating_transform_to_screen,
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
81bool ImageLayer::DrawsContent() const {
82  return !bitmap_.isNull() && TiledLayer::DrawsContent();
83}
84
85float ImageLayer::ImageContentsScaleX() const {
86  if (bounds().IsEmpty() || bitmap_.width() == 0)
87    return 1;
88  return static_cast<float>(bitmap_.width()) / bounds().width();
89}
90
91float ImageLayer::ImageContentsScaleY() const {
92  if (bounds().IsEmpty() || bitmap_.height() == 0)
93    return 1;
94  return static_cast<float>(bitmap_.height()) / bounds().height();
95}
96
97}  // namespace cc
98