[go: nahoru, domu]

blob: e1e762a174c1592fc05c0a350b27220180ff6a90 [file] [log] [blame]
Bo Liueb64cd12023-01-27 23:14:291// Copyright 2023 The Chromium Authors
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/slim/ui_resource_layer.h"
6
7#include <utility>
8
9#include "cc/layers/ui_resource_layer.h"
Bo Liu292429d2023-02-10 19:24:4110#include "cc/slim/features.h"
Bo Liu4fdd8be2023-02-24 16:37:5511#include "cc/slim/frame_data.h"
Bo Liu292429d2023-02-10 19:24:4112#include "cc/slim/layer_tree_impl.h"
Bo Liueb64cd12023-01-27 23:14:2913#include "components/viz/common/quads/compositor_render_pass.h"
Bo Liueb64cd12023-01-27 23:14:2914#include "components/viz/common/quads/texture_draw_quad.h"
Bo Liu84fe45d2023-02-21 16:33:3715#include "components/viz/common/resources/resource_id.h"
Bo Liueb64cd12023-01-27 23:14:2916
17namespace cc::slim {
18
19// static
20scoped_refptr<UIResourceLayer> UIResourceLayer::Create() {
21 scoped_refptr<cc::UIResourceLayer> cc_layer;
Bo Liu292429d2023-02-10 19:24:4122 if (!features::IsSlimCompositorEnabled()) {
23 cc_layer = cc::UIResourceLayer::Create();
24 }
Bo Liueb64cd12023-01-27 23:14:2925 return base::AdoptRef(new UIResourceLayer(std::move(cc_layer)));
26}
27
28UIResourceLayer::UIResourceLayer(scoped_refptr<cc::UIResourceLayer> cc_layer)
29 : Layer(std::move(cc_layer)) {}
30
31UIResourceLayer::~UIResourceLayer() = default;
32
33cc::UIResourceLayer* UIResourceLayer::cc_layer() const {
34 return static_cast<cc::UIResourceLayer*>(cc_layer_.get());
35}
36
Bo Liu292429d2023-02-10 19:24:4137void UIResourceLayer::SetUIResourceId(cc::UIResourceId id) {
38 if (cc_layer()) {
39 cc_layer()->SetUIResourceId(id);
40 return;
41 }
42 bitmap_.reset();
43 if (resource_id_ == id) {
44 return;
45 }
46
47 SetUIResourceIdInternal(id);
Bo Liueb64cd12023-01-27 23:14:2948}
49
50void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) {
Bo Liu292429d2023-02-10 19:24:4151 if (cc_layer()) {
52 cc_layer()->SetBitmap(bitmap);
53 return;
54 }
55 bitmap_ = bitmap;
56 if (!layer_tree()) {
57 return;
58 }
59
60 SetUIResourceIdInternal(static_cast<LayerTreeImpl*>(layer_tree())
61 ->GetUIResourceManager()
62 ->GetOrCreateUIResource(bitmap));
Bo Liueb64cd12023-01-27 23:14:2963}
64
65void UIResourceLayer::SetUV(const gfx::PointF& top_left,
66 const gfx::PointF& bottom_right) {
Bo Liu292429d2023-02-10 19:24:4167 if (cc_layer()) {
68 cc_layer()->SetUV(top_left, bottom_right);
69 return;
70 }
71 if (uv_.origin() == top_left && uv_.bottom_right() == bottom_right) {
72 return;
73 }
74
75 uv_.set_origin(top_left);
76 uv_.set_width(bottom_right.x() - top_left.x());
77 uv_.set_height(bottom_right.y() - top_left.y());
78 DCHECK_EQ(uv_.bottom_right(), bottom_right);
79 NotifyPropertyChanged();
Bo Liueb64cd12023-01-27 23:14:2980}
81
82void UIResourceLayer::SetVertexOpacity(float bottom_left,
83 float top_left,
84 float top_right,
85 float bottom_right) {
Bo Liu292429d2023-02-10 19:24:4186 if (cc_layer()) {
87 cc_layer()->SetVertexOpacity(bottom_left, top_left, top_right,
88 bottom_right);
89 return;
90 }
91 // Indexing according to the quad vertex generation:
92 // 1--2
93 // | |
94 // 0--3
95 auto& opacity = vertex_opacity_;
96 if (opacity[0] == bottom_left && opacity[1] == top_left &&
97 opacity[2] == top_right && opacity[3] == bottom_right) {
98 return;
99 }
100
101 opacity[0] = bottom_left;
102 opacity[1] = top_left;
103 opacity[2] = top_right;
104 opacity[3] = bottom_right;
105 NotifyPropertyChanged();
106}
107
108void UIResourceLayer::SetLayerTree(LayerTree* tree) {
109 if (cc_layer()) {
110 Layer::SetLayerTree(tree);
111 return;
112 }
113 if (tree == layer_tree()) {
114 return;
115 }
116
117 Layer::SetLayerTree(tree);
118 RefreshResource();
Xianzhu Wang58b9efc2023-02-26 15:35:01119 UpdateDrawsContent();
Bo Liu292429d2023-02-10 19:24:41120}
121
122bool UIResourceLayer::HasDrawableContent() const {
123 return resource_id_ && Layer::HasDrawableContent();
124}
125
126void UIResourceLayer::RefreshResource() {
127 if (!bitmap_.empty()) {
128 SetBitmap(bitmap_);
129 }
130}
131
132void UIResourceLayer::SetUIResourceIdInternal(cc::UIResourceId resource_id) {
133 if (resource_id_ == resource_id) {
134 return;
135 }
136 resource_id_ = resource_id;
Xianzhu Wang58b9efc2023-02-26 15:35:01137 UpdateDrawsContent();
Bo Liu292429d2023-02-10 19:24:41138 NotifyPropertyChanged();
Bo Liueb64cd12023-01-27 23:14:29139}
140
Bo Liu84fe45d2023-02-21 16:33:37141void UIResourceLayer::AppendQuads(viz::CompositorRenderPass& render_pass,
Bo Liu4fdd8be2023-02-24 16:37:55142 FrameData& data,
Bo Liuc94996e2023-03-13 15:56:24143 const gfx::Transform& transform_to_root,
144 const gfx::Transform& transform_to_target,
Bo Liudca1a9182023-03-03 19:27:42145 const gfx::Rect* clip_in_target,
Bo Liue5dbd7c2023-03-14 16:58:49146 const gfx::Rect& visible_rect,
147 float opacity) {
Bo Liu84fe45d2023-02-21 16:33:37148 viz::ResourceId viz_resource_id =
149 static_cast<LayerTreeImpl*>(layer_tree())->GetVizResourceId(resource_id_);
150 if (viz_resource_id == viz::kInvalidResourceId) {
151 return;
152 }
153
Bo Liudca1a9182023-03-03 19:27:42154 viz::SharedQuadState* quad_state = CreateAndAppendSharedQuadState(
Bo Liue5dbd7c2023-03-14 16:58:49155 render_pass, transform_to_target, clip_in_target, visible_rect, opacity);
Bo Liu84fe45d2023-02-21 16:33:37156
157 viz::TextureDrawQuad* quad =
158 render_pass.CreateAndAppendDrawQuad<viz::TextureDrawQuad>();
159 constexpr bool kFlipped = false;
160 constexpr bool kNearest = false;
161 constexpr bool kPremultiplied = true;
162 constexpr bool kSecureOutputOnly = false;
163 constexpr auto kVideoType = gfx::ProtectedVideoType::kClear;
164 const bool needs_blending = !static_cast<LayerTreeImpl*>(layer_tree())
165 ->IsUIResourceOpaque(resource_id_);
166 quad->SetNew(quad_state, quad_state->quad_layer_rect,
167 quad_state->visible_quad_layer_rect, needs_blending,
168 viz_resource_id, kPremultiplied, uv_top_left(),
169 uv_bottom_right(), SkColors::kTransparent, vertex_opacity_,
170 kFlipped, kNearest, kSecureOutputOnly, kVideoType);
171}
172
Bo Liueb64cd12023-01-27 23:14:29173} // namespace cc::slim