ui_resource_layer_impl.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "cc/layers/ui_resource_layer_impl.h"
  5. #include <memory>
  6. #include "base/trace_event/traced_value.h"
  7. #include "cc/base/math_util.h"
  8. #include "cc/trees/layer_tree_impl.h"
  9. #include "cc/trees/occlusion.h"
  10. #include "components/viz/common/quads/texture_draw_quad.h"
  11. #include "ui/gfx/geometry/rect_f.h"
  12. namespace cc {
  13. UIResourceLayerImpl::UIResourceLayerImpl(LayerTreeImpl* tree_impl, int id)
  14. : LayerImpl(tree_impl, id),
  15. ui_resource_id_(0),
  16. uv_top_left_(0.f, 0.f),
  17. uv_bottom_right_(1.f, 1.f) {
  18. vertex_opacity_[0] = 1.0f;
  19. vertex_opacity_[1] = 1.0f;
  20. vertex_opacity_[2] = 1.0f;
  21. vertex_opacity_[3] = 1.0f;
  22. }
  23. UIResourceLayerImpl::~UIResourceLayerImpl() = default;
  24. std::unique_ptr<LayerImpl> UIResourceLayerImpl::CreateLayerImpl(
  25. LayerTreeImpl* tree_impl) const {
  26. return UIResourceLayerImpl::Create(tree_impl, id());
  27. }
  28. void UIResourceLayerImpl::PushPropertiesTo(LayerImpl* layer) {
  29. LayerImpl::PushPropertiesTo(layer);
  30. UIResourceLayerImpl* layer_impl = static_cast<UIResourceLayerImpl*>(layer);
  31. layer_impl->SetUIResourceId(ui_resource_id_);
  32. layer_impl->SetImageBounds(image_bounds_);
  33. layer_impl->SetUV(uv_top_left_, uv_bottom_right_);
  34. layer_impl->SetVertexOpacity(vertex_opacity_);
  35. }
  36. void UIResourceLayerImpl::SetUIResourceId(UIResourceId uid) {
  37. if (uid == ui_resource_id_)
  38. return;
  39. ui_resource_id_ = uid;
  40. NoteLayerPropertyChanged();
  41. }
  42. void UIResourceLayerImpl::SetImageBounds(const gfx::Size& image_bounds) {
  43. // This check imposes an ordering on the call sequence. An UIResource must
  44. // exist before SetImageBounds can be called.
  45. DCHECK(ui_resource_id_);
  46. if (image_bounds_ == image_bounds)
  47. return;
  48. image_bounds_ = image_bounds;
  49. NoteLayerPropertyChanged();
  50. }
  51. void UIResourceLayerImpl::SetUV(const gfx::PointF& top_left,
  52. const gfx::PointF& bottom_right) {
  53. if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right)
  54. return;
  55. uv_top_left_ = top_left;
  56. uv_bottom_right_ = bottom_right;
  57. NoteLayerPropertyChanged();
  58. }
  59. void UIResourceLayerImpl::SetVertexOpacity(const float vertex_opacity[4]) {
  60. if (vertex_opacity_[0] == vertex_opacity[0] &&
  61. vertex_opacity_[1] == vertex_opacity[1] &&
  62. vertex_opacity_[2] == vertex_opacity[2] &&
  63. vertex_opacity_[3] == vertex_opacity[3])
  64. return;
  65. vertex_opacity_[0] = vertex_opacity[0];
  66. vertex_opacity_[1] = vertex_opacity[1];
  67. vertex_opacity_[2] = vertex_opacity[2];
  68. vertex_opacity_[3] = vertex_opacity[3];
  69. NoteLayerPropertyChanged();
  70. }
  71. bool UIResourceLayerImpl::WillDraw(
  72. DrawMode draw_mode,
  73. viz::ClientResourceProvider* resource_provider) {
  74. if (!ui_resource_id_ || draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
  75. return false;
  76. return LayerImpl::WillDraw(draw_mode, resource_provider);
  77. }
  78. void UIResourceLayerImpl::AppendQuads(viz::CompositorRenderPass* render_pass,
  79. AppendQuadsData* append_quads_data) {
  80. DCHECK(!bounds().IsEmpty());
  81. viz::SharedQuadState* shared_quad_state =
  82. render_pass->CreateAndAppendSharedQuadState();
  83. viz::ResourceId resource =
  84. ui_resource_id_
  85. ? layer_tree_impl()->ResourceIdForUIResource(ui_resource_id_)
  86. : viz::kInvalidResourceId;
  87. bool are_contents_opaque =
  88. resource ? (layer_tree_impl()->IsUIResourceOpaque(ui_resource_id_) ||
  89. contents_opaque())
  90. : false;
  91. PopulateSharedQuadState(shared_quad_state, are_contents_opaque);
  92. AppendDebugBorderQuad(render_pass, gfx::Rect(bounds()), shared_quad_state,
  93. append_quads_data);
  94. if (!resource)
  95. return;
  96. static const bool flipped = false;
  97. static const bool nearest_neighbor = false;
  98. static const bool premultiplied_alpha = true;
  99. gfx::Rect quad_rect(bounds());
  100. bool needs_blending = are_contents_opaque ? false : true;
  101. gfx::Rect visible_quad_rect =
  102. draw_properties().occlusion_in_content_space.GetUnoccludedContentRect(
  103. quad_rect);
  104. if (visible_quad_rect.IsEmpty())
  105. return;
  106. auto* quad = render_pass->CreateAndAppendDrawQuad<viz::TextureDrawQuad>();
  107. quad->SetNew(shared_quad_state, quad_rect, visible_quad_rect, needs_blending,
  108. resource, premultiplied_alpha, uv_top_left_, uv_bottom_right_,
  109. SkColors::kTransparent, vertex_opacity_, flipped,
  110. nearest_neighbor,
  111. /*secure_output_only=*/false, gfx::ProtectedVideoType::kClear);
  112. ValidateQuadResources(quad);
  113. }
  114. const char* UIResourceLayerImpl::LayerTypeAsString() const {
  115. return "cc::UIResourceLayerImpl";
  116. }
  117. void UIResourceLayerImpl::AsValueInto(
  118. base::trace_event::TracedValue* state) const {
  119. LayerImpl::AsValueInto(state);
  120. MathUtil::AddToTracedValue("ImageBounds", image_bounds_, state);
  121. state->BeginArray("VertexOpacity");
  122. state->AppendDouble(vertex_opacity_[0]);
  123. state->AppendDouble(vertex_opacity_[1]);
  124. state->AppendDouble(vertex_opacity_[2]);
  125. state->AppendDouble(vertex_opacity_[3]);
  126. state->EndArray();
  127. MathUtil::AddToTracedValue("UVTopLeft", uv_top_left_, state);
  128. MathUtil::AddToTracedValue("UVBottomRight", uv_bottom_right_, state);
  129. }
  130. } // namespace cc