Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 1 | // 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 Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 10 | #include "cc/slim/features.h" |
Bo Liu | 4fdd8be | 2023-02-24 16:37:55 | [diff] [blame] | 11 | #include "cc/slim/frame_data.h" |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 12 | #include "cc/slim/layer_tree_impl.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 13 | #include "components/viz/common/quads/compositor_render_pass.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 14 | #include "components/viz/common/quads/texture_draw_quad.h" |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 15 | #include "components/viz/common/resources/resource_id.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 16 | |
| 17 | namespace cc::slim { |
| 18 | |
| 19 | // static |
| 20 | scoped_refptr<UIResourceLayer> UIResourceLayer::Create() { |
| 21 | scoped_refptr<cc::UIResourceLayer> cc_layer; |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 22 | if (!features::IsSlimCompositorEnabled()) { |
| 23 | cc_layer = cc::UIResourceLayer::Create(); |
| 24 | } |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 25 | return base::AdoptRef(new UIResourceLayer(std::move(cc_layer))); |
| 26 | } |
| 27 | |
| 28 | UIResourceLayer::UIResourceLayer(scoped_refptr<cc::UIResourceLayer> cc_layer) |
| 29 | : Layer(std::move(cc_layer)) {} |
| 30 | |
| 31 | UIResourceLayer::~UIResourceLayer() = default; |
| 32 | |
| 33 | cc::UIResourceLayer* UIResourceLayer::cc_layer() const { |
| 34 | return static_cast<cc::UIResourceLayer*>(cc_layer_.get()); |
| 35 | } |
| 36 | |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 37 | void 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 Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 51 | 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 Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void UIResourceLayer::SetUV(const gfx::PointF& top_left, |
| 66 | const gfx::PointF& bottom_right) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 67 | 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 Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void UIResourceLayer::SetVertexOpacity(float bottom_left, |
| 83 | float top_left, |
| 84 | float top_right, |
| 85 | float bottom_right) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 86 | 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 | |
| 108 | void 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 Wang | 58b9efc | 2023-02-26 15:35:01 | [diff] [blame] | 119 | UpdateDrawsContent(); |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | bool UIResourceLayer::HasDrawableContent() const { |
| 123 | return resource_id_ && Layer::HasDrawableContent(); |
| 124 | } |
| 125 | |
| 126 | void UIResourceLayer::RefreshResource() { |
| 127 | if (!bitmap_.empty()) { |
| 128 | SetBitmap(bitmap_); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void UIResourceLayer::SetUIResourceIdInternal(cc::UIResourceId resource_id) { |
| 133 | if (resource_id_ == resource_id) { |
| 134 | return; |
| 135 | } |
| 136 | resource_id_ = resource_id; |
Xianzhu Wang | 58b9efc | 2023-02-26 15:35:01 | [diff] [blame] | 137 | UpdateDrawsContent(); |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 138 | NotifyPropertyChanged(); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 139 | } |
| 140 | |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 141 | void UIResourceLayer::AppendQuads(viz::CompositorRenderPass& render_pass, |
Bo Liu | 4fdd8be | 2023-02-24 16:37:55 | [diff] [blame] | 142 | FrameData& data, |
Bo Liu | c94996e | 2023-03-13 15:56:24 | [diff] [blame] | 143 | const gfx::Transform& transform_to_root, |
| 144 | const gfx::Transform& transform_to_target, |
Bo Liu | dca1a918 | 2023-03-03 19:27:42 | [diff] [blame] | 145 | const gfx::Rect* clip_in_target, |
Bo Liu | e5dbd7c | 2023-03-14 16:58:49 | [diff] [blame^] | 146 | const gfx::Rect& visible_rect, |
| 147 | float opacity) { |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 148 | 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 Liu | dca1a918 | 2023-03-03 19:27:42 | [diff] [blame] | 154 | viz::SharedQuadState* quad_state = CreateAndAppendSharedQuadState( |
Bo Liu | e5dbd7c | 2023-03-14 16:58:49 | [diff] [blame^] | 155 | render_pass, transform_to_target, clip_in_target, visible_rect, opacity); |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 156 | |
| 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 Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 173 | } // namespace cc::slim |