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 | |
Bo Liu | 4fdd8be | 2023-02-24 16:37:55 | [diff] [blame] | 9 | #include "cc/slim/frame_data.h" |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 10 | #include "cc/slim/layer_tree_impl.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 11 | #include "components/viz/common/quads/compositor_render_pass.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 12 | #include "components/viz/common/quads/texture_draw_quad.h" |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 13 | #include "components/viz/common/resources/resource_id.h" |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 14 | |
| 15 | namespace cc::slim { |
| 16 | |
| 17 | // static |
| 18 | scoped_refptr<UIResourceLayer> UIResourceLayer::Create() { |
Bo Liu | 149c189 | 2024-02-13 22:16:25 | [diff] [blame] | 19 | return base::AdoptRef(new UIResourceLayer()); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 20 | } |
| 21 | |
Bo Liu | 149c189 | 2024-02-13 22:16:25 | [diff] [blame] | 22 | UIResourceLayer::UIResourceLayer() = default; |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 23 | UIResourceLayer::~UIResourceLayer() = default; |
| 24 | |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 25 | void UIResourceLayer::SetUIResourceId(cc::UIResourceId id) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 26 | bitmap_.reset(); |
| 27 | if (resource_id_ == id) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | SetUIResourceIdInternal(id); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 35 | bitmap_ = bitmap; |
| 36 | if (!layer_tree()) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | SetUIResourceIdInternal(static_cast<LayerTreeImpl*>(layer_tree()) |
| 41 | ->GetUIResourceManager() |
| 42 | ->GetOrCreateUIResource(bitmap)); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void UIResourceLayer::SetUV(const gfx::PointF& top_left, |
| 46 | const gfx::PointF& bottom_right) { |
Bo Liu | b1943c4 | 2023-04-05 18:01:32 | [diff] [blame] | 47 | if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
Bo Liu | b1943c4 | 2023-04-05 18:01:32 | [diff] [blame] | 51 | uv_top_left_ = top_left; |
| 52 | uv_bottom_right_ = bottom_right; |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 53 | NotifyPropertyChanged(); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void UIResourceLayer::SetVertexOpacity(float bottom_left, |
| 57 | float top_left, |
| 58 | float top_right, |
| 59 | float bottom_right) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 60 | // 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 | |
| 77 | void UIResourceLayer::SetLayerTree(LayerTree* tree) { |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 78 | if (tree == layer_tree()) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | Layer::SetLayerTree(tree); |
| 83 | RefreshResource(); |
Xianzhu Wang | 58b9efc | 2023-02-26 15:35:01 | [diff] [blame] | 84 | UpdateDrawsContent(); |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool UIResourceLayer::HasDrawableContent() const { |
| 88 | return resource_id_ && Layer::HasDrawableContent(); |
| 89 | } |
| 90 | |
| 91 | void UIResourceLayer::RefreshResource() { |
| 92 | if (!bitmap_.empty()) { |
| 93 | SetBitmap(bitmap_); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void UIResourceLayer::SetUIResourceIdInternal(cc::UIResourceId resource_id) { |
| 98 | if (resource_id_ == resource_id) { |
| 99 | return; |
| 100 | } |
| 101 | resource_id_ = resource_id; |
Xianzhu Wang | 58b9efc | 2023-02-26 15:35:01 | [diff] [blame] | 102 | UpdateDrawsContent(); |
Bo Liu | 292429d | 2023-02-10 19:24:41 | [diff] [blame] | 103 | NotifyPropertyChanged(); |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 104 | } |
| 105 | |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 106 | void UIResourceLayer::AppendQuads(viz::CompositorRenderPass& render_pass, |
Bo Liu | 4fdd8be | 2023-02-24 16:37:55 | [diff] [blame] | 107 | FrameData& data, |
Bo Liu | c94996e | 2023-03-13 15:56:24 | [diff] [blame] | 108 | const gfx::Transform& transform_to_root, |
| 109 | const gfx::Transform& transform_to_target, |
Bo Liu | dca1a918 | 2023-03-03 19:27:42 | [diff] [blame] | 110 | const gfx::Rect* clip_in_target, |
Bo Liu | e5dbd7c | 2023-03-14 16:58:49 | [diff] [blame] | 111 | const gfx::Rect& visible_rect, |
| 112 | float opacity) { |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 113 | 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 Liu | ab8b4ffd | 2023-04-25 22:09:07 | [diff] [blame] | 119 | viz::SharedQuadState* quad_state = |
| 120 | CreateAndAppendSharedQuadState(render_pass, data, transform_to_target, |
| 121 | clip_in_target, visible_rect, opacity); |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 122 | |
| 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 McNeeley | 5d32c254a | 2024-01-03 03:42:32 | [diff] [blame] | 135 | uv_bottom_right(), SkColors::kTransparent, kFlipped, kNearest, |
| 136 | kSecureOutputOnly, kVideoType); |
| 137 | quad->set_vertex_opacity(vertex_opacity_); |
Bo Liu | 84fe45d | 2023-02-21 16:33:37 | [diff] [blame] | 138 | } |
| 139 | |
Bo Liu | eb64cd1 | 2023-01-27 23:14:29 | [diff] [blame] | 140 | } // namespace cc::slim |