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/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"
20 class ScopedUIResourceHolder
: public UIResourceLayer::UIResourceHolder
{
22 static scoped_ptr
<ScopedUIResourceHolder
> Create(LayerTreeHost
* host
,
23 const SkBitmap
& skbitmap
) {
24 return make_scoped_ptr(new ScopedUIResourceHolder(host
, skbitmap
));
26 UIResourceId
id() override
{ return resource_
->id(); }
29 ScopedUIResourceHolder(LayerTreeHost
* host
, const SkBitmap
& skbitmap
) {
30 resource_
= ScopedUIResource::Create(host
, UIResourceBitmap(skbitmap
));
33 scoped_ptr
<ScopedUIResource
> resource_
;
36 class SharedUIResourceHolder
: public UIResourceLayer::UIResourceHolder
{
38 static scoped_ptr
<SharedUIResourceHolder
> Create(UIResourceId id
) {
39 return make_scoped_ptr(new SharedUIResourceHolder(id
));
42 UIResourceId
id() override
{ return id_
; }
45 explicit SharedUIResourceHolder(UIResourceId id
) : id_(id
) {}
50 } // anonymous namespace
52 UIResourceLayer::UIResourceHolder::~UIResourceHolder() {}
54 scoped_refptr
<UIResourceLayer
> UIResourceLayer::Create(
55 const LayerSettings
& settings
) {
56 return make_scoped_refptr(new UIResourceLayer(settings
));
59 UIResourceLayer::UIResourceLayer(const LayerSettings
& settings
)
60 : Layer(settings
), uv_top_left_(0.f
, 0.f
), uv_bottom_right_(1.f
, 1.f
) {
61 vertex_opacity_
[0] = 1.0f
;
62 vertex_opacity_
[1] = 1.0f
;
63 vertex_opacity_
[2] = 1.0f
;
64 vertex_opacity_
[3] = 1.0f
;
67 UIResourceLayer::~UIResourceLayer() {}
69 scoped_ptr
<LayerImpl
> UIResourceLayer::CreateLayerImpl(
70 LayerTreeImpl
* tree_impl
) {
71 return UIResourceLayerImpl::Create(tree_impl
, id());
74 void UIResourceLayer::SetUV(const gfx::PointF
& top_left
,
75 const gfx::PointF
& bottom_right
) {
76 if (uv_top_left_
== top_left
&& uv_bottom_right_
== bottom_right
)
78 uv_top_left_
= top_left
;
79 uv_bottom_right_
= bottom_right
;
83 void UIResourceLayer::SetVertexOpacity(float bottom_left
,
87 // Indexing according to the quad vertex generation:
91 if (vertex_opacity_
[0] == bottom_left
&&
92 vertex_opacity_
[1] == top_left
&&
93 vertex_opacity_
[2] == top_right
&&
94 vertex_opacity_
[3] == bottom_right
)
96 vertex_opacity_
[0] = bottom_left
;
97 vertex_opacity_
[1] = top_left
;
98 vertex_opacity_
[2] = top_right
;
99 vertex_opacity_
[3] = bottom_right
;
103 void UIResourceLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
104 if (host
== layer_tree_host())
107 Layer::SetLayerTreeHost(host
);
109 // Recreate the resource held against the new LTH.
110 RecreateUIResourceHolder();
112 UpdateDrawsContent(HasDrawableContent());
115 void UIResourceLayer::RecreateUIResourceHolder() {
116 if (!bitmap_
.empty())
120 void UIResourceLayer::SetBitmap(const SkBitmap
& skbitmap
) {
122 if (layer_tree_host() && !bitmap_
.empty()) {
123 ui_resource_holder_
=
124 ScopedUIResourceHolder::Create(layer_tree_host(), bitmap_
);
126 ui_resource_holder_
= nullptr;
128 UpdateDrawsContent(HasDrawableContent());
132 void UIResourceLayer::SetUIResourceId(UIResourceId resource_id
) {
133 if (ui_resource_holder_
&& ui_resource_holder_
->id() == resource_id
)
136 if (!bitmap_
.isNull())
140 ui_resource_holder_
= SharedUIResourceHolder::Create(resource_id
);
142 ui_resource_holder_
= nullptr;
144 UpdateDrawsContent(HasDrawableContent());
148 bool UIResourceLayer::HasDrawableContent() const {
149 return ui_resource_holder_
&& ui_resource_holder_
->id() &&
150 Layer::HasDrawableContent();
153 void UIResourceLayer::PushPropertiesTo(LayerImpl
* layer
) {
154 Layer::PushPropertiesTo(layer
);
155 UIResourceLayerImpl
* layer_impl
= static_cast<UIResourceLayerImpl
*>(layer
);
157 if (!ui_resource_holder_
) {
158 layer_impl
->SetUIResourceId(0);
160 DCHECK(layer_tree_host());
162 gfx::Size image_size
=
163 layer_tree_host()->GetUIResourceSize(ui_resource_holder_
->id());
164 layer_impl
->SetUIResourceId(ui_resource_holder_
->id());
165 layer_impl
->SetImageBounds(image_size
);
166 layer_impl
->SetUV(uv_top_left_
, uv_bottom_right_
);
167 layer_impl
->SetVertexOpacity(vertex_opacity_
);