recording_source.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2014 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/recording_source.h"
  5. #include <stdint.h>
  6. #include <algorithm>
  7. #include "base/numerics/safe_math.h"
  8. #include "base/trace_event/trace_event.h"
  9. #include "cc/base/region.h"
  10. #include "cc/layers/content_layer_client.h"
  11. #include "cc/paint/display_item_list.h"
  12. #include "cc/paint/solid_color_analyzer.h"
  13. #include "cc/raster/raster_source.h"
  14. namespace {
  15. // We don't perform per-layer solid color analysis when there are too many skia
  16. // operations.
  17. const int kMaxOpsToAnalyzeForLayer = 10;
  18. } // namespace
  19. namespace cc {
  20. RecordingSource::RecordingSource() = default;
  21. RecordingSource::~RecordingSource() = default;
  22. void RecordingSource::UpdateInvalidationForNewViewport(
  23. const gfx::Rect& old_recorded_viewport,
  24. const gfx::Rect& new_recorded_viewport,
  25. Region* invalidation) {
  26. // Invalidate newly-exposed and no-longer-exposed areas.
  27. Region newly_exposed_region(new_recorded_viewport);
  28. newly_exposed_region.Subtract(old_recorded_viewport);
  29. invalidation->Union(newly_exposed_region);
  30. Region no_longer_exposed_region(old_recorded_viewport);
  31. no_longer_exposed_region.Subtract(new_recorded_viewport);
  32. invalidation->Union(no_longer_exposed_region);
  33. }
  34. void RecordingSource::FinishDisplayItemListUpdate() {
  35. TRACE_EVENT0("cc", "RecordingSource::FinishDisplayItemListUpdate");
  36. DetermineIfSolidColor();
  37. display_list_->EmitTraceSnapshot();
  38. display_list_->GenerateDiscardableImagesMetadata();
  39. }
  40. void RecordingSource::SetNeedsDisplayRect(const gfx::Rect& layer_rect) {
  41. if (!layer_rect.IsEmpty()) {
  42. // Clamp invalidation to the layer bounds.
  43. invalidation_.Union(gfx::IntersectRects(layer_rect, gfx::Rect(size_)));
  44. }
  45. }
  46. bool RecordingSource::UpdateAndExpandInvalidation(
  47. Region* invalidation,
  48. const gfx::Size& layer_size,
  49. const gfx::Rect& new_recorded_viewport) {
  50. bool updated = false;
  51. if (size_ != layer_size)
  52. size_ = layer_size;
  53. invalidation_.Swap(invalidation);
  54. invalidation_.Clear();
  55. if (new_recorded_viewport != recorded_viewport_) {
  56. UpdateInvalidationForNewViewport(recorded_viewport_, new_recorded_viewport,
  57. invalidation);
  58. recorded_viewport_ = new_recorded_viewport;
  59. updated = true;
  60. }
  61. if (!updated && !invalidation->Intersects(recorded_viewport_))
  62. return false;
  63. if (invalidation->IsEmpty())
  64. return false;
  65. return true;
  66. }
  67. void RecordingSource::UpdateDisplayItemList(
  68. const scoped_refptr<DisplayItemList>& display_list,
  69. float recording_scale_factor) {
  70. recording_scale_factor_ = recording_scale_factor;
  71. if (display_list_ != display_list) {
  72. display_list_ = display_list;
  73. // Do the following only if the display list changes. Though we use
  74. // recording_scale_factor in DetermineIfSolidColor(), change of it doesn't
  75. // affect whether the same display list is solid or not.
  76. FinishDisplayItemListUpdate();
  77. }
  78. }
  79. gfx::Size RecordingSource::GetSize() const {
  80. return size_;
  81. }
  82. void RecordingSource::SetEmptyBounds() {
  83. size_ = gfx::Size();
  84. is_solid_color_ = false;
  85. recorded_viewport_ = gfx::Rect();
  86. display_list_ = nullptr;
  87. }
  88. void RecordingSource::SetSlowdownRasterScaleFactor(int factor) {
  89. slow_down_raster_scale_factor_for_debug_ = factor;
  90. }
  91. void RecordingSource::SetBackgroundColor(SkColor4f background_color) {
  92. background_color_ = background_color;
  93. }
  94. void RecordingSource::SetRequiresClear(bool requires_clear) {
  95. requires_clear_ = requires_clear;
  96. }
  97. scoped_refptr<RasterSource> RecordingSource::CreateRasterSource() const {
  98. return scoped_refptr<RasterSource>(new RasterSource(this));
  99. }
  100. void RecordingSource::DetermineIfSolidColor() {
  101. DCHECK(display_list_);
  102. is_solid_color_ = false;
  103. solid_color_ = SkColors::kTransparent;
  104. if (display_list_->TotalOpCount() > kMaxOpsToAnalyzeForLayer)
  105. return;
  106. TRACE_EVENT1("cc", "RecordingSource::DetermineIfSolidColor", "opcount",
  107. display_list_->TotalOpCount());
  108. is_solid_color_ = display_list_->GetColorIfSolidInRect(
  109. gfx::ScaleToRoundedRect(gfx::Rect(GetSize()), recording_scale_factor_),
  110. &solid_color_, kMaxOpsToAnalyzeForLayer);
  111. }
  112. } // namespace cc