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"
17 class ScopedUIResourceHolder
: public UIResourceLayer::UIResourceHolder
{
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(); }
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
{
35 static scoped_ptr
<SharedUIResourceHolder
> Create(UIResourceId id
) {
36 return make_scoped_ptr(new SharedUIResourceHolder(id
));
39 UIResourceId
id() override
{ return id_
; }
42 explicit SharedUIResourceHolder(UIResourceId id
) : id_(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
)
75 uv_top_left_
= top_left
;
76 uv_bottom_right_
= bottom_right
;
80 void UIResourceLayer::SetVertexOpacity(float bottom_left
,
84 // Indexing according to the quad vertex generation:
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
)
93 vertex_opacity_
[0] = bottom_left
;
94 vertex_opacity_
[1] = top_left
;
95 vertex_opacity_
[2] = top_right
;
96 vertex_opacity_
[3] = bottom_right
;
100 void UIResourceLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
101 if (host
== layer_tree_host())
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())
117 void UIResourceLayer::SetBitmap(const SkBitmap
& skbitmap
) {
119 if (layer_tree_host() && !bitmap_
.empty()) {
120 ui_resource_holder_
=
121 ScopedUIResourceHolder::Create(layer_tree_host(), bitmap_
);
123 ui_resource_holder_
= nullptr;
125 UpdateDrawsContent(HasDrawableContent());
129 void UIResourceLayer::SetUIResourceId(UIResourceId resource_id
) {
130 if (ui_resource_holder_
&& ui_resource_holder_
->id() == resource_id
)
133 if (!bitmap_
.isNull())
137 ui_resource_holder_
= SharedUIResourceHolder::Create(resource_id
);
139 ui_resource_holder_
= nullptr;
141 UpdateDrawsContent(HasDrawableContent());
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);
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_
);