blob: 364e97c09ed96400b05db6c9d09b02e047a6c34f [file] [log] [blame]
[email protected]efbdb3a2013-10-04 00:35:131// 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
danakj7a3089f2016-01-16 01:20:137#include "base/trace_event/trace_event.h"
[email protected]efbdb3a2013-10-04 00:35:138#include "cc/layers/ui_resource_layer_impl.h"
[email protected]efbdb3a2013-10-04 00:35:139#include "cc/resources/scoped_ui_resource.h"
10#include "cc/resources/ui_resource_bitmap.h"
khushalsagar8ec07402016-09-10 03:13:1911#include "cc/resources/ui_resource_manager.h"
[email protected]efbdb3a2013-10-04 00:35:1312#include "cc/trees/layer_tree_host.h"
13
14namespace cc {
15
loyso0940d412016-03-14 01:30:3116scoped_refptr<UIResourceLayer> UIResourceLayer::Create() {
kylechar973a0412017-09-26 18:40:2917 return base::WrapRefCounted(new UIResourceLayer());
[email protected]efbdb3a2013-10-04 00:35:1318}
19
loyso0940d412016-03-14 01:30:3120UIResourceLayer::UIResourceLayer()
21 : uv_top_left_(0.f, 0.f), uv_bottom_right_(1.f, 1.f) {
[email protected]e95e40f2013-10-15 17:54:5822 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]efbdb3a2013-10-04 00:35:1327
Chris Watkinsf6353292017-12-04 02:36:0528UIResourceLayer::~UIResourceLayer() = default;
[email protected]efbdb3a2013-10-04 00:35:1329
danakj60bc3bc2016-04-09 00:24:4830std::unique_ptr<LayerImpl> UIResourceLayer::CreateLayerImpl(
[email protected]efbdb3a2013-10-04 00:35:1331 LayerTreeImpl* tree_impl) {
danakjf446a072014-09-27 21:55:4832 return UIResourceLayerImpl::Create(tree_impl, id());
[email protected]efbdb3a2013-10-04 00:35:1333}
34
[email protected]14bc5d682014-01-17 07:26:4735void UIResourceLayer::SetUV(const gfx::PointF& top_left,
36 const gfx::PointF& bottom_right) {
[email protected]e95e40f2013-10-15 17:54:5837 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
44void 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]efbdb3a2013-10-04 00:35:1364void UIResourceLayer::SetLayerTreeHost(LayerTreeHost* host) {
65 if (host == layer_tree_host())
66 return;
67
68 Layer::SetLayerTreeHost(host);
69
changwan8a519762014-11-20 23:06:5670 // Recreate the resource held against the new LTH.
estade68ce2eb2017-01-24 20:09:3771 RecreateUIResourceIdFromBitmap();
changwan8a519762014-11-20 23:06:5672
73 UpdateDrawsContent(HasDrawableContent());
[email protected]efbdb3a2013-10-04 00:35:1374}
75
estade68ce2eb2017-01-24 20:09:3776void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) {
77 bitmap_ = bitmap;
khushalsagarb69ba9452017-01-27 22:20:0778 if (!layer_tree_host())
estade68ce2eb2017-01-24 20:09:3779 return;
80 SetUIResourceIdInternal(
81 layer_tree_host()->GetUIResourceManager()->GetOrCreateUIResource(bitmap));
[email protected]efbdb3a2013-10-04 00:35:1382}
83
84void UIResourceLayer::SetUIResourceId(UIResourceId resource_id) {
estade68ce2eb2017-01-24 20:09:3785 // 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]efbdb3a2013-10-04 00:35:1390 return;
estade68ce2eb2017-01-24 20:09:3791 SetUIResourceIdInternal(resource_id);
[email protected]efbdb3a2013-10-04 00:35:1392}
93
[email protected]ad63b2f2014-08-11 17:39:5494bool UIResourceLayer::HasDrawableContent() const {
estade68ce2eb2017-01-24 20:09:3795 return resource_id_ && Layer::HasDrawableContent();
[email protected]efbdb3a2013-10-04 00:35:1396}
97
98void UIResourceLayer::PushPropertiesTo(LayerImpl* layer) {
99 Layer::PushPropertiesTo(layer);
danakj7a3089f2016-01-16 01:20:13100 TRACE_EVENT0("cc", "UIResourceLayer::PushPropertiesTo");
[email protected]efbdb3a2013-10-04 00:35:13101 UIResourceLayerImpl* layer_impl = static_cast<UIResourceLayerImpl*>(layer);
102
estade68ce2eb2017-01-24 20:09:37103 layer_impl->SetUIResourceId(resource_id_);
104 if (resource_id_) {
khushalsagarb69ba9452017-01-27 22:20:07105 DCHECK(layer_tree_host());
[email protected]efbdb3a2013-10-04 00:35:13106
107 gfx::Size image_size =
khushalsagar8297ae992016-09-14 20:51:23108 layer_tree_host()->GetUIResourceManager()->GetUIResourceSize(
estade68ce2eb2017-01-24 20:09:37109 resource_id_);
[email protected]efbdb3a2013-10-04 00:35:13110 layer_impl->SetImageBounds(image_size);
[email protected]e95e40f2013-10-15 17:54:58111 layer_impl->SetUV(uv_top_left_, uv_bottom_right_);
112 layer_impl->SetVertexOpacity(vertex_opacity_);
[email protected]efbdb3a2013-10-04 00:35:13113 }
114}
115
estade68ce2eb2017-01-24 20:09:37116void UIResourceLayer::RecreateUIResourceIdFromBitmap() {
117 if (!bitmap_.empty())
118 SetBitmap(bitmap_);
119}
120
121void UIResourceLayer::SetUIResourceIdInternal(UIResourceId resource_id) {
122 resource_id_ = resource_id;
123 UpdateDrawsContent(HasDrawableContent());
124 SetNeedsCommit();
125}
126
[email protected]efbdb3a2013-10-04 00:35:13127} // namespace cc