1 // Copyright 2019 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
5 #include "components/viz/service/display/damage_frame_annotator.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "cc/base/math_util.h"
11 #include "components/viz/common/quads/aggregated_render_pass.h"
12 #include "components/viz/common/quads/debug_border_draw_quad.h"
13 #include "components/viz/common/quads/shared_quad_state.h"
14 #include "components/viz/service/display/aggregated_frame.h"
15
16 namespace viz {
17
18 DamageFrameAnnotator::DamageFrameAnnotator() = default;
19 DamageFrameAnnotator::~DamageFrameAnnotator() = default;
20
AnnotateAggregatedFrame(AggregatedFrame * frame)21 void DamageFrameAnnotator::AnnotateAggregatedFrame(AggregatedFrame* frame) {
22 DCHECK(frame);
23 auto* root_render_pass = frame->render_pass_list.back().get();
24
25 const gfx::Rect& damage_rect = root_render_pass->damage_rect;
26 gfx::Transform transform;
27 transform.Translate(damage_rect.x(), damage_rect.y());
28
29 annotations_.push_back(
30 AnnotationData{gfx::Rect(damage_rect.size()), transform,
31 Highlight{SkColorSetARGB(128, 255, 0, 0), 4}});
32
33 AnnotateRootRenderPass(root_render_pass);
34 annotations_.clear();
35 }
36
AnnotateRootRenderPass(AggregatedRenderPass * render_pass)37 void DamageFrameAnnotator::AnnotateRootRenderPass(
38 AggregatedRenderPass* render_pass) {
39 const size_t num_quads_to_add = annotations_.size();
40
41 // Insert |num_quads_to_add| new DebugBorderDrawQuad at start of list. The
42 // quads will be drawn on top of the original quads.
43 auto quad_iter =
44 render_pass->quad_list
45 .InsertBeforeAndInvalidateAllPointers<DebugBorderDrawQuad>(
46 render_pass->quad_list.begin(), num_quads_to_add);
47
48 // Initialize the SharedQuadStates and DebugBorderDrawQuads.
49 for (auto& annotation : annotations_) {
50 gfx::Rect output_rect = cc::MathUtil::MapEnclosingClippedRect(
51 annotation.transform, annotation.rect);
52
53 SharedQuadState* new_sqs = render_pass->shared_quad_state_list
54 .AllocateAndConstruct<SharedQuadState>();
55 new_sqs->SetAll(annotation.transform, output_rect, output_rect,
56 gfx::MaskFilterInfo(), output_rect, true, false, 1.f,
57 SkBlendMode::kSrcOver, 0);
58
59 DebugBorderDrawQuad* new_quad =
60 static_cast<DebugBorderDrawQuad*>(*quad_iter);
61 new_quad->SetNew(new_sqs, annotation.rect, annotation.rect,
62 annotation.highlight.color, annotation.highlight.width);
63
64 ++quad_iter;
65 }
66
67 // Set the entire frame as damaged.
68 render_pass->damage_rect = render_pass->output_rect;
69 }
70
71 } // namespace viz
72