[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 | |
danakj | 7a3089f | 2016-01-16 01:20:13 | [diff] [blame] | 7 | #include "base/trace_event/trace_event.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 8 | #include "cc/layers/ui_resource_layer_impl.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 9 | #include "cc/resources/scoped_ui_resource.h" |
| 10 | #include "cc/resources/ui_resource_bitmap.h" |
khushalsagar | 8ec0740 | 2016-09-10 03:13:19 | [diff] [blame] | 11 | #include "cc/resources/ui_resource_manager.h" |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 12 | #include "cc/trees/layer_tree_host.h" |
| 13 | |
| 14 | namespace cc { |
| 15 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 16 | scoped_refptr<UIResourceLayer> UIResourceLayer::Create() { |
kylechar | 973a041 | 2017-09-26 18:40:29 | [diff] [blame] | 17 | return base::WrapRefCounted(new UIResourceLayer()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 18 | } |
| 19 | |
loyso | 0940d41 | 2016-03-14 01:30:31 | [diff] [blame] | 20 | UIResourceLayer::UIResourceLayer() |
| 21 | : uv_top_left_(0.f, 0.f), uv_bottom_right_(1.f, 1.f) { |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 22 | vertex_opacity_[0] = 1.0f; |
| 23 | vertex_opacity_[1] = 1.0f; |
| 24 | vertex_opacity_[2] = 1.0f; |
| 25 | vertex_opacity_[3] = 1.0f; |
| 26 | } |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 27 | |
Chris Watkins | f635329 | 2017-12-04 02:36:05 | [diff] [blame] | 28 | UIResourceLayer::~UIResourceLayer() = default; |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 29 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 30 | std::unique_ptr<LayerImpl> UIResourceLayer::CreateLayerImpl( |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 31 | LayerTreeImpl* tree_impl) { |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 32 | return UIResourceLayerImpl::Create(tree_impl, id()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 14bc5d68 | 2014-01-17 07:26:47 | [diff] [blame] | 35 | void UIResourceLayer::SetUV(const gfx::PointF& top_left, |
| 36 | const gfx::PointF& bottom_right) { |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 37 | if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right) |
| 38 | return; |
| 39 | uv_top_left_ = top_left; |
| 40 | uv_bottom_right_ = bottom_right; |
| 41 | SetNeedsCommit(); |
| 42 | } |
| 43 | |
| 44 | void UIResourceLayer::SetVertexOpacity(float bottom_left, |
| 45 | float top_left, |
| 46 | float top_right, |
| 47 | float bottom_right) { |
| 48 | // Indexing according to the quad vertex generation: |
| 49 | // 1--2 |
| 50 | // | | |
| 51 | // 0--3 |
| 52 | if (vertex_opacity_[0] == bottom_left && |
| 53 | vertex_opacity_[1] == top_left && |
| 54 | vertex_opacity_[2] == top_right && |
| 55 | vertex_opacity_[3] == bottom_right) |
| 56 | return; |
| 57 | vertex_opacity_[0] = bottom_left; |
| 58 | vertex_opacity_[1] = top_left; |
| 59 | vertex_opacity_[2] = top_right; |
| 60 | vertex_opacity_[3] = bottom_right; |
| 61 | SetNeedsCommit(); |
| 62 | } |
| 63 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 64 | void UIResourceLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 65 | if (host == layer_tree_host()) |
| 66 | return; |
| 67 | |
| 68 | Layer::SetLayerTreeHost(host); |
| 69 | |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame] | 70 | // Recreate the resource held against the new LTH. |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 71 | RecreateUIResourceIdFromBitmap(); |
changwan | 8a51976 | 2014-11-20 23:06:56 | [diff] [blame] | 72 | |
| 73 | UpdateDrawsContent(HasDrawableContent()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 74 | } |
| 75 | |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 76 | void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) { |
| 77 | bitmap_ = bitmap; |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 78 | if (!layer_tree_host()) |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 79 | return; |
| 80 | SetUIResourceIdInternal( |
| 81 | layer_tree_host()->GetUIResourceManager()->GetOrCreateUIResource(bitmap)); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void UIResourceLayer::SetUIResourceId(UIResourceId resource_id) { |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 85 | // Even if the ID is not changing we should drop the bitmap. The ID is 0 when |
| 86 | // there's no layer tree. When setting an id (even if to 0), we should no |
| 87 | // longer keep the bitmap. |
| 88 | bitmap_.reset(); |
| 89 | if (resource_id_ == resource_id) |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 90 | return; |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 91 | SetUIResourceIdInternal(resource_id); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | ad63b2f | 2014-08-11 17:39:54 | [diff] [blame] | 94 | bool UIResourceLayer::HasDrawableContent() const { |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 95 | return resource_id_ && Layer::HasDrawableContent(); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void UIResourceLayer::PushPropertiesTo(LayerImpl* layer) { |
| 99 | Layer::PushPropertiesTo(layer); |
danakj | 7a3089f | 2016-01-16 01:20:13 | [diff] [blame] | 100 | TRACE_EVENT0("cc", "UIResourceLayer::PushPropertiesTo"); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 101 | UIResourceLayerImpl* layer_impl = static_cast<UIResourceLayerImpl*>(layer); |
| 102 | |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 103 | layer_impl->SetUIResourceId(resource_id_); |
| 104 | if (resource_id_) { |
khushalsagar | b69ba945 | 2017-01-27 22:20:07 | [diff] [blame] | 105 | DCHECK(layer_tree_host()); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 106 | |
| 107 | gfx::Size image_size = |
khushalsagar | 8297ae99 | 2016-09-14 20:51:23 | [diff] [blame] | 108 | layer_tree_host()->GetUIResourceManager()->GetUIResourceSize( |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 109 | resource_id_); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 110 | layer_impl->SetImageBounds(image_size); |
[email protected] | e95e40f | 2013-10-15 17:54:58 | [diff] [blame] | 111 | layer_impl->SetUV(uv_top_left_, uv_bottom_right_); |
| 112 | layer_impl->SetVertexOpacity(vertex_opacity_); |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
estade | 68ce2eb | 2017-01-24 20:09:37 | [diff] [blame] | 116 | void UIResourceLayer::RecreateUIResourceIdFromBitmap() { |
| 117 | if (!bitmap_.empty()) |
| 118 | SetBitmap(bitmap_); |
| 119 | } |
| 120 | |
| 121 | void UIResourceLayer::SetUIResourceIdInternal(UIResourceId resource_id) { |
| 122 | resource_id_ = resource_id; |
| 123 | UpdateDrawsContent(HasDrawableContent()); |
| 124 | SetNeedsCommit(); |
| 125 | } |
| 126 | |
[email protected] | efbdb3a | 2013-10-04 00:35:13 | [diff] [blame] | 127 | } // namespace cc |