Delete unused downloads page asset.
[chromium-blink-merge.git] / cc / layers / ui_resource_layer.cc
blob87512ebd016a213452c553766e8e7ccacba84bc2
1 // Copyright 2013 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.
5 #include "cc/layers/ui_resource_layer.h"
7 #include "cc/layers/ui_resource_layer_impl.h"
8 #include "cc/resources/scoped_ui_resource.h"
9 #include "cc/resources/ui_resource_bitmap.h"
10 #include "cc/trees/layer_tree_host.h"
12 namespace cc {
15 namespace {
17 class ScopedUIResourceHolder : public UIResourceLayer::UIResourceHolder {
18 public:
19 static scoped_ptr<ScopedUIResourceHolder> Create(LayerTreeHost* host,
20 const SkBitmap& skbitmap) {
21 return make_scoped_ptr(new ScopedUIResourceHolder(host, skbitmap));
23 UIResourceId id() override { return resource_->id(); }
25 private:
26 ScopedUIResourceHolder(LayerTreeHost* host, const SkBitmap& skbitmap) {
27 resource_ = ScopedUIResource::Create(host, UIResourceBitmap(skbitmap));
30 scoped_ptr<ScopedUIResource> resource_;
33 class SharedUIResourceHolder : public UIResourceLayer::UIResourceHolder {
34 public:
35 static scoped_ptr<SharedUIResourceHolder> Create(UIResourceId id) {
36 return make_scoped_ptr(new SharedUIResourceHolder(id));
39 UIResourceId id() override { return id_; }
41 private:
42 explicit SharedUIResourceHolder(UIResourceId id) : id_(id) {}
44 UIResourceId id_;
47 } // anonymous namespace
49 UIResourceLayer::UIResourceHolder::~UIResourceHolder() {}
51 scoped_refptr<UIResourceLayer> UIResourceLayer::Create(
52 const LayerSettings& settings) {
53 return make_scoped_refptr(new UIResourceLayer(settings));
56 UIResourceLayer::UIResourceLayer(const LayerSettings& settings)
57 : Layer(settings), uv_top_left_(0.f, 0.f), uv_bottom_right_(1.f, 1.f) {
58 vertex_opacity_[0] = 1.0f;
59 vertex_opacity_[1] = 1.0f;
60 vertex_opacity_[2] = 1.0f;
61 vertex_opacity_[3] = 1.0f;
64 UIResourceLayer::~UIResourceLayer() {}
66 scoped_ptr<LayerImpl> UIResourceLayer::CreateLayerImpl(
67 LayerTreeImpl* tree_impl) {
68 return UIResourceLayerImpl::Create(tree_impl, id());
71 void UIResourceLayer::SetUV(const gfx::PointF& top_left,
72 const gfx::PointF& bottom_right) {
73 if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right)
74 return;
75 uv_top_left_ = top_left;
76 uv_bottom_right_ = bottom_right;
77 SetNeedsCommit();
80 void UIResourceLayer::SetVertexOpacity(float bottom_left,
81 float top_left,
82 float top_right,
83 float bottom_right) {
84 // Indexing according to the quad vertex generation:
85 // 1--2
86 // | |
87 // 0--3
88 if (vertex_opacity_[0] == bottom_left &&
89 vertex_opacity_[1] == top_left &&
90 vertex_opacity_[2] == top_right &&
91 vertex_opacity_[3] == bottom_right)
92 return;
93 vertex_opacity_[0] = bottom_left;
94 vertex_opacity_[1] = top_left;
95 vertex_opacity_[2] = top_right;
96 vertex_opacity_[3] = bottom_right;
97 SetNeedsCommit();
100 void UIResourceLayer::SetLayerTreeHost(LayerTreeHost* host) {
101 if (host == layer_tree_host())
102 return;
104 Layer::SetLayerTreeHost(host);
106 // Recreate the resource held against the new LTH.
107 RecreateUIResourceHolder();
109 UpdateDrawsContent(HasDrawableContent());
112 void UIResourceLayer::RecreateUIResourceHolder() {
113 if (!bitmap_.empty())
114 SetBitmap(bitmap_);
117 void UIResourceLayer::SetBitmap(const SkBitmap& skbitmap) {
118 bitmap_ = skbitmap;
119 if (layer_tree_host() && !bitmap_.empty()) {
120 ui_resource_holder_ =
121 ScopedUIResourceHolder::Create(layer_tree_host(), bitmap_);
122 } else {
123 ui_resource_holder_ = nullptr;
125 UpdateDrawsContent(HasDrawableContent());
126 SetNeedsCommit();
129 void UIResourceLayer::SetUIResourceId(UIResourceId resource_id) {
130 if (ui_resource_holder_ && ui_resource_holder_->id() == resource_id)
131 return;
133 if (!bitmap_.isNull())
134 bitmap_.reset();
136 if (resource_id)
137 ui_resource_holder_ = SharedUIResourceHolder::Create(resource_id);
138 else
139 ui_resource_holder_ = nullptr;
141 UpdateDrawsContent(HasDrawableContent());
142 SetNeedsCommit();
145 bool UIResourceLayer::HasDrawableContent() const {
146 return ui_resource_holder_ && ui_resource_holder_->id() &&
147 Layer::HasDrawableContent();
150 void UIResourceLayer::PushPropertiesTo(LayerImpl* layer) {
151 Layer::PushPropertiesTo(layer);
152 UIResourceLayerImpl* layer_impl = static_cast<UIResourceLayerImpl*>(layer);
154 if (!ui_resource_holder_) {
155 layer_impl->SetUIResourceId(0);
156 } else {
157 DCHECK(layer_tree_host());
159 gfx::Size image_size =
160 layer_tree_host()->GetUIResourceSize(ui_resource_holder_->id());
161 layer_impl->SetUIResourceId(ui_resource_holder_->id());
162 layer_impl->SetImageBounds(image_size);
163 layer_impl->SetUV(uv_top_left_, uv_bottom_right_);
164 layer_impl->SetVertexOpacity(vertex_opacity_);
168 } // namespace cc