[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "cc/layers/ui_resource_layer.h" |
| 6 | |
| 7 | #include "cc/layers/ui_resource_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/resources/scoped_ui_resource.h" |
| 12 | #include "cc/resources/ui_resource_bitmap.h" |
| 13 | #include "cc/trees/layer_tree_host.h" |
| 14 | |
| 15 | namespace cc { |
| 16 | |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class ScopedUIResourceHolder : public UIResourceLayer::UIResourceHolder { |
| 21 | public: |
| 22 | static scoped_ptr<ScopedUIResourceHolder> Create(LayerTreeHost* host, |
| 23 | const SkBitmap& skbitmap) { |
| 24 | return make_scoped_ptr(new ScopedUIResourceHolder(host, skbitmap)); |
| 25 | } |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 26 | UIResourceId id() override { return resource_->id(); } |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 27 | |
| 28 | private: |
| 29 | ScopedUIResourceHolder(LayerTreeHost* host, const SkBitmap& skbitmap) { |
| 30 | resource_ = ScopedUIResource::Create(host, UIResourceBitmap(skbitmap)); |
| 31 | } |
| 32 | |
| 33 | scoped_ptr<ScopedUIResource> resource_; |
| 34 | }; |
| 35 | |
| 36 | class SharedUIResourceHolder : public UIResourceLayer::UIResourceHolder { |
| 37 | public: |
| 38 | static scoped_ptr<SharedUIResourceHolder> Create(UIResourceId id) { |
| 39 | return make_scoped_ptr(new SharedUIResourceHolder(id)); |
| 40 | } |
| 41 | |
dcheng | 716bedf | 2014-10-21 09:51:08 | [diff] [blame] | 42 | UIResourceId id() override { return id_; } |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 43 | |
| 44 | private: |
| 45 | explicit SharedUIResourceHolder(UIResourceId id) : id_(id) {} |
| 46 | |
| 47 | UIResourceId id_; |
| 48 | }; |
| 49 | |
| 50 | } // anonymous namespace |
| 51 | |
| 52 | UIResourceLayer::UIResourceHolder::~UIResourceHolder() {} |
| 53 | |
| 54 | scoped_refptr<UIResourceLayer> UIResourceLayer::Create() { |
| 55 | return make_scoped_refptr(new UIResourceLayer()); |
| 56 | } |
| 57 | |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 58 | UIResourceLayer::UIResourceLayer() |
| 59 | : Layer(), |
| 60 | uv_top_left_(0.f, 0.f), |
| 61 | uv_bottom_right_(1.f, 1.f) { |
| 62 | vertex_opacity_[0] = 1.0f; |
| 63 | vertex_opacity_[1] = 1.0f; |
| 64 | vertex_opacity_[2] = 1.0f; |
| 65 | vertex_opacity_[3] = 1.0f; |
| 66 | } |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 67 | |
| 68 | UIResourceLayer::~UIResourceLayer() {} |
| 69 | |
| 70 | scoped_ptr<LayerImpl> UIResourceLayer::CreateLayerImpl( |
| 71 | LayerTreeImpl* tree_impl) { |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 72 | return UIResourceLayerImpl::Create(tree_impl, id()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 14bc5d68 | 2014-01-17 07:26:47 | [diff] [blame] | 75 | void UIResourceLayer::SetUV(const gfx::PointF& top_left, |
| 76 | const gfx::PointF& bottom_right) { |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 77 | if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right) |
| 78 | return; |
| 79 | uv_top_left_ = top_left; |
| 80 | uv_bottom_right_ = bottom_right; |
| 81 | SetNeedsCommit(); |
| 82 | } |
| 83 | |
| 84 | void UIResourceLayer::SetVertexOpacity(float bottom_left, |
| 85 | float top_left, |
| 86 | float top_right, |
| 87 | float bottom_right) { |
| 88 | // Indexing according to the quad vertex generation: |
| 89 | // 1--2 |
| 90 | // | | |
| 91 | // 0--3 |
| 92 | if (vertex_opacity_[0] == bottom_left && |
| 93 | vertex_opacity_[1] == top_left && |
| 94 | vertex_opacity_[2] == top_right && |
| 95 | vertex_opacity_[3] == bottom_right) |
| 96 | return; |
| 97 | vertex_opacity_[0] = bottom_left; |
| 98 | vertex_opacity_[1] = top_left; |
| 99 | vertex_opacity_[2] = top_right; |
| 100 | vertex_opacity_[3] = bottom_right; |
| 101 | SetNeedsCommit(); |
| 102 | } |
| 103 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 104 | void UIResourceLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 105 | if (host == layer_tree_host()) |
| 106 | return; |
| 107 | |
| 108 | Layer::SetLayerTreeHost(host); |
| 109 | |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame^] | 110 | // Recreate the resource held against the new LTH. |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 111 | RecreateUIResourceHolder(); |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame^] | 112 | |
| 113 | UpdateDrawsContent(HasDrawableContent()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void UIResourceLayer::RecreateUIResourceHolder() { |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame^] | 117 | if (!bitmap_.empty()) |
| 118 | SetBitmap(bitmap_); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void UIResourceLayer::SetBitmap(const SkBitmap& skbitmap) { |
| 122 | bitmap_ = skbitmap; |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame^] | 123 | if (layer_tree_host() && !bitmap_.empty()) { |
| 124 | ui_resource_holder_ = |
| 125 | ScopedUIResourceHolder::Create(layer_tree_host(), bitmap_); |
| 126 | } else { |
| 127 | ui_resource_holder_ = nullptr; |
| 128 | } |
| 129 | UpdateDrawsContent(HasDrawableContent()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 130 | SetNeedsCommit(); |
| 131 | } |
| 132 | |
| 133 | void UIResourceLayer::SetUIResourceId(UIResourceId resource_id) { |
| 134 | if (ui_resource_holder_ && ui_resource_holder_->id() == resource_id) |
| 135 | return; |
| 136 | |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 137 | if (resource_id) |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 138 | ui_resource_holder_ = SharedUIResourceHolder::Create(resource_id); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 139 | else |
| 140 | ui_resource_holder_ = nullptr; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 141 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 142 | UpdateDrawsContent(HasDrawableContent()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 143 | SetNeedsCommit(); |
| 144 | } |
| 145 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 146 | bool UIResourceLayer::HasDrawableContent() const { |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 147 | return ui_resource_holder_ && ui_resource_holder_->id() && |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 148 | Layer::HasDrawableContent(); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void UIResourceLayer::PushPropertiesTo(LayerImpl* layer) { |
| 152 | Layer::PushPropertiesTo(layer); |
| 153 | UIResourceLayerImpl* layer_impl = static_cast<UIResourceLayerImpl*>(layer); |
| 154 | |
| 155 | if (!ui_resource_holder_) { |
| 156 | layer_impl->SetUIResourceId(0); |
| 157 | } else { |
| 158 | DCHECK(layer_tree_host()); |
| 159 | |
| 160 | gfx::Size image_size = |
| 161 | layer_tree_host()->GetUIResourceSize(ui_resource_holder_->id()); |
| 162 | layer_impl->SetUIResourceId(ui_resource_holder_->id()); |
| 163 | layer_impl->SetImageBounds(image_size); |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 164 | layer_impl->SetUV(uv_top_left_, uv_bottom_right_); |
| 165 | layer_impl->SetVertexOpacity(vertex_opacity_); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | } // namespace cc |