[go: nahoru, domu]

Add slim::UIResourceLayer property methods

Mostly copied directly from cc layers. Includes most things except
AppendQuads.

Bug: 1408128
Change-Id: I6c8ea208ed7d49486b583d60d5a332132ec0c0a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4235467
Reviewed-by: Kyle Charbonneau <kylechar@chromium.org>
Commit-Queue: Bo Liu <boliu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1103972}
diff --git a/cc/slim/ui_resource_layer.cc b/cc/slim/ui_resource_layer.cc
index 97c2a6e2..a6d7c34 100644
--- a/cc/slim/ui_resource_layer.cc
+++ b/cc/slim/ui_resource_layer.cc
@@ -7,6 +7,8 @@
 #include <utility>
 
 #include "cc/layers/ui_resource_layer.h"
+#include "cc/slim/features.h"
+#include "cc/slim/layer_tree_impl.h"
 #include "components/viz/common/quads/compositor_render_pass.h"
 #include "components/viz/common/quads/solid_color_draw_quad.h"
 #include "components/viz/common/quads/texture_draw_quad.h"
@@ -16,7 +18,9 @@
 // static
 scoped_refptr<UIResourceLayer> UIResourceLayer::Create() {
   scoped_refptr<cc::UIResourceLayer> cc_layer;
-  cc_layer = cc::UIResourceLayer::Create();
+  if (!features::IsSlimCompositorEnabled()) {
+    cc_layer = cc::UIResourceLayer::Create();
+  }
   return base::AdoptRef(new UIResourceLayer(std::move(cc_layer)));
 }
 
@@ -29,24 +33,108 @@
   return static_cast<cc::UIResourceLayer*>(cc_layer_.get());
 }
 
-void UIResourceLayer::SetUIResourceId(int id) {
-  cc_layer()->SetUIResourceId(id);
+void UIResourceLayer::SetUIResourceId(cc::UIResourceId id) {
+  if (cc_layer()) {
+    cc_layer()->SetUIResourceId(id);
+    return;
+  }
+  bitmap_.reset();
+  if (resource_id_ == id) {
+    return;
+  }
+
+  SetUIResourceIdInternal(id);
 }
 
 void UIResourceLayer::SetBitmap(const SkBitmap& bitmap) {
-  cc_layer()->SetBitmap(bitmap);
+  if (cc_layer()) {
+    cc_layer()->SetBitmap(bitmap);
+    return;
+  }
+  bitmap_ = bitmap;
+  if (!layer_tree()) {
+    return;
+  }
+
+  SetUIResourceIdInternal(static_cast<LayerTreeImpl*>(layer_tree())
+                              ->GetUIResourceManager()
+                              ->GetOrCreateUIResource(bitmap));
 }
 
 void UIResourceLayer::SetUV(const gfx::PointF& top_left,
                             const gfx::PointF& bottom_right) {
-  cc_layer()->SetUV(top_left, bottom_right);
+  if (cc_layer()) {
+    cc_layer()->SetUV(top_left, bottom_right);
+    return;
+  }
+  if (uv_.origin() == top_left && uv_.bottom_right() == bottom_right) {
+    return;
+  }
+
+  uv_.set_origin(top_left);
+  uv_.set_width(bottom_right.x() - top_left.x());
+  uv_.set_height(bottom_right.y() - top_left.y());
+  DCHECK_EQ(uv_.bottom_right(), bottom_right);
+  NotifyPropertyChanged();
 }
 
 void UIResourceLayer::SetVertexOpacity(float bottom_left,
                                        float top_left,
                                        float top_right,
                                        float bottom_right) {
-  cc_layer()->SetVertexOpacity(bottom_left, top_left, top_right, bottom_right);
+  if (cc_layer()) {
+    cc_layer()->SetVertexOpacity(bottom_left, top_left, top_right,
+                                 bottom_right);
+    return;
+  }
+  // Indexing according to the quad vertex generation:
+  // 1--2
+  // |  |
+  // 0--3
+  auto& opacity = vertex_opacity_;
+  if (opacity[0] == bottom_left && opacity[1] == top_left &&
+      opacity[2] == top_right && opacity[3] == bottom_right) {
+    return;
+  }
+
+  opacity[0] = bottom_left;
+  opacity[1] = top_left;
+  opacity[2] = top_right;
+  opacity[3] = bottom_right;
+  NotifyPropertyChanged();
+}
+
+void UIResourceLayer::SetLayerTree(LayerTree* tree) {
+  if (cc_layer()) {
+    Layer::SetLayerTree(tree);
+    return;
+  }
+  if (tree == layer_tree()) {
+    return;
+  }
+
+  Layer::SetLayerTree(tree);
+  RefreshResource();
+  SetDrawsContent(HasDrawableContent());
+}
+
+bool UIResourceLayer::HasDrawableContent() const {
+  return resource_id_ && Layer::HasDrawableContent();
+}
+
+void UIResourceLayer::RefreshResource() {
+  if (!bitmap_.empty()) {
+    SetBitmap(bitmap_);
+  }
+}
+
+void UIResourceLayer::SetUIResourceIdInternal(cc::UIResourceId resource_id) {
+  if (resource_id_ == resource_id) {
+    return;
+  }
+  resource_id_ = resource_id;
+  SetDrawsContent(HasDrawableContent());
+  NotifyPropertyChanged();
 }
 
 }  // namespace cc::slim