[go: nahoru, domu]

blob: 8fb31d659bce21c97f0803f17f591efd60c1ba93 [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
Bo Liu4fdd8be2023-02-24 16:37:559#include "cc/slim/frame_data.h"
Bo Liu292429d2023-02-10 19:24:4110#include "cc/slim/layer_tree_impl.h"
Bo Liueb64cd12023-01-27 23:14:2911#include "components/viz/common/quads/compositor_render_pass.h"
Bo Liueb64cd12023-01-27 23:14:2912#include "components/viz/common/quads/texture_draw_quad.h"
Bo Liu84fe45d2023-02-21 16:33:3713#include "components/viz/common/resources/resource_id.h"
Bo Liueb64cd12023-01-27 23:14:2914
15namespace cc::slim {
16
17// static
18scoped_refptr<UIResourceLayer> UIResourceLayer::Create() {
Bo Liu149c1892024-02-13 22:16:2519 return base::AdoptRef(new UIResourceLayer());
Bo Liueb64cd12023-01-27 23:14:2920}
21
Bo Liu149c1892024-02-13 22:16:2522UIResourceLayer::UIResourceLayer() = default;
Bo Liueb64cd12023-01-27 23:14:2923UIResourceLayer::~UIResourceLayer() = default;
24
Bo Liu292429d2023-02-10 19:24:4125void UIResourceLayer::SetUIResourceId(cc::UIResourceId id) {
Bo Liu292429d2023-02-10 19:24:4126 bitmap_.reset();
27 if (resource_id_ == id) {
28 return;
29 }
30
31 SetUIResourceIdInternal(id);
Bo Liueb64cd12023-01-27 23:14:2932}
33
34void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) {
Bo Liu292429d2023-02-10 19:24:4135 bitmap_ = bitmap;
36 if (!layer_tree()) {
37 return;
38 }
39
40 SetUIResourceIdInternal(static_cast<LayerTreeImpl*>(layer_tree())
41 ->GetUIResourceManager()
42 ->GetOrCreateUIResource(bitmap));
Bo Liueb64cd12023-01-27 23:14:2943}
44
45void UIResourceLayer::SetUV(const gfx::PointF& top_left,
46 const gfx::PointF& bottom_right) {
Bo Liub1943c42023-04-05 18:01:3247 if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right) {
Bo Liu292429d2023-02-10 19:24:4148 return;
49 }
50
Bo Liub1943c42023-04-05 18:01:3251 uv_top_left_ = top_left;
52 uv_bottom_right_ = bottom_right;
Bo Liu292429d2023-02-10 19:24:4153 NotifyPropertyChanged();
Bo Liueb64cd12023-01-27 23:14:2954}
55
56void UIResourceLayer::SetVertexOpacity(float bottom_left,
57 float top_left,
58 float top_right,
59 float bottom_right) {
Bo Liu292429d2023-02-10 19:24:4160 // Indexing according to the quad vertex generation:
61 // 1--2
62 // | |
63 // 0--3
64 auto& opacity = vertex_opacity_;
65 if (opacity[0] == bottom_left && opacity[1] == top_left &&
66 opacity[2] == top_right && opacity[3] == bottom_right) {
67 return;
68 }
69
70 opacity[0] = bottom_left;
71 opacity[1] = top_left;
72 opacity[2] = top_right;
73 opacity[3] = bottom_right;
74 NotifyPropertyChanged();
75}
76
77void UIResourceLayer::SetLayerTree(LayerTree* tree) {
Bo Liu292429d2023-02-10 19:24:4178 if (tree == layer_tree()) {
79 return;
80 }
81
82 Layer::SetLayerTree(tree);
83 RefreshResource();
Xianzhu Wang58b9efc2023-02-26 15:35:0184 UpdateDrawsContent();
Bo Liu292429d2023-02-10 19:24:4185}
86
87bool UIResourceLayer::HasDrawableContent() const {
88 return resource_id_ && Layer::HasDrawableContent();
89}
90
91void UIResourceLayer::RefreshResource() {
92 if (!bitmap_.empty()) {
93 SetBitmap(bitmap_);
94 }
95}
96
97void UIResourceLayer::SetUIResourceIdInternal(cc::UIResourceId resource_id) {
98 if (resource_id_ == resource_id) {
99 return;
100 }
101 resource_id_ = resource_id;
Xianzhu Wang58b9efc2023-02-26 15:35:01102 UpdateDrawsContent();
Bo Liu292429d2023-02-10 19:24:41103 NotifyPropertyChanged();
Bo Liueb64cd12023-01-27 23:14:29104}
105
Bo Liu84fe45d2023-02-21 16:33:37106void UIResourceLayer::AppendQuads(viz::CompositorRenderPass& render_pass,
Bo Liu4fdd8be2023-02-24 16:37:55107 FrameData& data,
Bo Liuc94996e2023-03-13 15:56:24108 const gfx::Transform& transform_to_root,
109 const gfx::Transform& transform_to_target,
Bo Liudca1a9182023-03-03 19:27:42110 const gfx::Rect* clip_in_target,
Bo Liue5dbd7c2023-03-14 16:58:49111 const gfx::Rect& visible_rect,
112 float opacity) {
Bo Liu84fe45d2023-02-21 16:33:37113 viz::ResourceId viz_resource_id =
114 static_cast<LayerTreeImpl*>(layer_tree())->GetVizResourceId(resource_id_);
115 if (viz_resource_id == viz::kInvalidResourceId) {
116 return;
117 }
118
Bo Liuab8b4ffd2023-04-25 22:09:07119 viz::SharedQuadState* quad_state =
120 CreateAndAppendSharedQuadState(render_pass, data, transform_to_target,
121 clip_in_target, visible_rect, opacity);
Bo Liu84fe45d2023-02-21 16:33:37122
123 viz::TextureDrawQuad* quad =
124 render_pass.CreateAndAppendDrawQuad<viz::TextureDrawQuad>();
125 constexpr bool kFlipped = false;
126 constexpr bool kNearest = false;
127 constexpr bool kPremultiplied = true;
128 constexpr bool kSecureOutputOnly = false;
129 constexpr auto kVideoType = gfx::ProtectedVideoType::kClear;
130 const bool needs_blending = !static_cast<LayerTreeImpl*>(layer_tree())
131 ->IsUIResourceOpaque(resource_id_);
132 quad->SetNew(quad_state, quad_state->quad_layer_rect,
133 quad_state->visible_quad_layer_rect, needs_blending,
134 viz_resource_id, kPremultiplied, uv_top_left(),
Peter McNeeley5d32c254a2024-01-03 03:42:32135 uv_bottom_right(), SkColors::kTransparent, kFlipped, kNearest,
136 kSecureOutputOnly, kVideoType);
137 quad->set_vertex_opacity(vertex_opacity_);
Bo Liu84fe45d2023-02-21 16:33:37138}
139
Bo Liueb64cd12023-01-27 23:14:29140} // namespace cc::slim