[go: nahoru, domu]

blob: c199311ec93f1fad6ee1325fc3889253fbc523c9 [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();
119 SetDrawsContent(HasDrawableContent());
120}
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;
137 SetDrawsContent(HasDrawableContent());
138 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 Liu84fe45d2023-02-21 16:33:37143 const gfx::Transform& transform,
144 const gfx::Rect* clip) {
145 viz::ResourceId viz_resource_id =
146 static_cast<LayerTreeImpl*>(layer_tree())->GetVizResourceId(resource_id_);
147 if (viz_resource_id == viz::kInvalidResourceId) {
148 return;
149 }
150
151 viz::SharedQuadState* quad_state =
152 CreateAndAppendSharedQuadState(render_pass, transform, clip);
153
154 viz::TextureDrawQuad* quad =
155 render_pass.CreateAndAppendDrawQuad<viz::TextureDrawQuad>();
156 constexpr bool kFlipped = false;
157 constexpr bool kNearest = false;
158 constexpr bool kPremultiplied = true;
159 constexpr bool kSecureOutputOnly = false;
160 constexpr auto kVideoType = gfx::ProtectedVideoType::kClear;
161 const bool needs_blending = !static_cast<LayerTreeImpl*>(layer_tree())
162 ->IsUIResourceOpaque(resource_id_);
163 quad->SetNew(quad_state, quad_state->quad_layer_rect,
164 quad_state->visible_quad_layer_rect, needs_blending,
165 viz_resource_id, kPremultiplied, uv_top_left(),
166 uv_bottom_right(), SkColors::kTransparent, vertex_opacity_,
167 kFlipped, kNearest, kSecureOutputOnly, kVideoType);
168}
169
Bo Liueb64cd12023-01-27 23:14:29170} // namespace cc::slim