1 // Copyright 2012 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.
5 #include "cc/layers/heads_up_display_layer_impl.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/debug/debug_colors.h"
14 #include "cc/debug/frame_rate_counter.h"
15 #include "cc/debug/paint_time_counter.h"
16 #include "cc/output/begin_frame_args.h"
17 #include "cc/output/renderer.h"
18 #include "cc/quads/texture_draw_quad.h"
19 #include "cc/resources/memory_history.h"
20 #include "cc/trees/layer_tree_host_impl.h"
21 #include "cc/trees/layer_tree_impl.h"
22 #include "skia/ext/platform_canvas.h"
23 #include "third_party/khronos/GLES2/gl2.h"
24 #include "third_party/khronos/GLES2/gl2ext.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkTypeface.h"
28 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
29 #include "ui/gfx/geometry/point.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/geometry/size_conversions.h"
35 static inline SkPaint
CreatePaint() {
37 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
38 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
39 // swizzle our colors when drawing to the SkCanvas.
40 SkColorMatrix swizzle_matrix
;
41 for (int i
= 0; i
< 20; ++i
)
42 swizzle_matrix
.fMat
[i
] = 0;
43 swizzle_matrix
.fMat
[0 + 5 * 2] = 1;
44 swizzle_matrix
.fMat
[1 + 5 * 1] = 1;
45 swizzle_matrix
.fMat
[2 + 5 * 0] = 1;
46 swizzle_matrix
.fMat
[3 + 5 * 3] = 1;
48 skia::RefPtr
<SkColorMatrixFilter
> filter
=
49 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix
));
50 paint
.setColorFilter(filter
.get());
55 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value
,
56 double start_upper_bound
)
60 current_upper_bound(start_upper_bound
),
61 default_upper_bound(start_upper_bound
),
62 indicator(indicator_value
) {}
64 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
65 double target_upper_bound
= std::max(max
, default_upper_bound
);
66 current_upper_bound
+= (target_upper_bound
- current_upper_bound
) * 0.5;
67 return current_upper_bound
;
70 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl
* tree_impl
,
72 : LayerImpl(tree_impl
, id
),
73 typeface_(skia::AdoptRef(
74 SkTypeface::CreateFromName("monospace", SkTypeface::kBold
))),
75 internal_contents_scale_(1.f
),
76 fps_graph_(60.0, 80.0),
77 paint_time_graph_(16.0, 48.0),
81 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
83 scoped_ptr
<LayerImpl
> HeadsUpDisplayLayerImpl::CreateLayerImpl(
84 LayerTreeImpl
* tree_impl
) {
85 return HeadsUpDisplayLayerImpl::Create(tree_impl
, id());
88 void HeadsUpDisplayLayerImpl::AcquireResource(
89 ResourceProvider
* resource_provider
) {
90 for (ScopedPtrVector
<ScopedResource
>::iterator it
= resources_
.begin();
91 it
!= resources_
.end();
93 if (!resource_provider
->InUseByConsumer((*it
)->id())) {
94 resources_
.swap(it
, resources_
.end() - 1);
99 scoped_ptr
<ScopedResource
> resource
=
100 ScopedResource::Create(resource_provider
);
101 resource
->Allocate(internal_content_bounds_
,
102 ResourceProvider::TEXTURE_HINT_IMMUTABLE
, RGBA_8888
);
103 resources_
.push_back(resource
.Pass());
106 class ResourceSizeIsEqualTo
{
108 explicit ResourceSizeIsEqualTo(const gfx::Size
& size_
)
109 : compare_size_(size_
) {}
111 bool operator()(const ScopedResource
* resource
) {
112 return resource
->size() == compare_size_
;
116 const gfx::Size compare_size_
;
119 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
120 ResourceProvider
* resource_provider
) {
121 ScopedPtrVector
<ScopedResource
>::iterator it_erase
=
122 resources_
.partition(ResourceSizeIsEqualTo(internal_content_bounds_
));
123 resources_
.erase(it_erase
, resources_
.end());
126 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode
,
127 ResourceProvider
* resource_provider
) {
128 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
)
131 internal_contents_scale_
= draw_properties().ideal_contents_scale
;
132 internal_content_bounds_
=
133 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_
));
135 ReleaseUnmatchedSizeResources(resource_provider
);
136 AcquireResource(resource_provider
);
137 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
140 void HeadsUpDisplayLayerImpl::AppendQuads(
141 RenderPass
* render_pass
,
142 AppendQuadsData
* append_quads_data
) {
143 if (!resources_
.back()->id())
146 SharedQuadState
* shared_quad_state
=
147 render_pass
->CreateAndAppendSharedQuadState();
148 PopulateScaledSharedQuadState(shared_quad_state
, internal_contents_scale_
);
150 gfx::Rect
quad_rect(internal_content_bounds_
);
151 gfx::Rect
opaque_rect(contents_opaque() ? quad_rect
: gfx::Rect());
152 gfx::Rect
visible_quad_rect(quad_rect
);
153 bool premultiplied_alpha
= true;
154 gfx::PointF
uv_top_left(0.f
, 0.f
);
155 gfx::PointF
uv_bottom_right(1.f
, 1.f
);
156 const float vertex_opacity
[] = { 1.f
, 1.f
, 1.f
, 1.f
};
157 bool flipped
= false;
158 bool nearest_neighbor
= false;
159 TextureDrawQuad
* quad
=
160 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
161 quad
->SetNew(shared_quad_state
,
165 resources_
.back()->id(),
175 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
177 ResourceProvider
* resource_provider
) {
178 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
|| !resources_
.back()->id())
183 canvas_size
= hud_surface_
->getCanvas()->getDeviceSize();
185 canvas_size
.set(0, 0);
187 if (canvas_size
.width() != internal_content_bounds_
.width() ||
188 canvas_size
.height() != internal_content_bounds_
.height() ||
190 TRACE_EVENT0("cc", "ResizeHudCanvas");
192 hud_surface_
= skia::AdoptRef(SkSurface::NewRasterN32Premul(
193 internal_content_bounds_
.width(), internal_content_bounds_
.height()));
199 TRACE_EVENT0("cc", "DrawHudContents");
200 hud_surface_
->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
201 hud_surface_
->getCanvas()->save();
202 hud_surface_
->getCanvas()->scale(internal_contents_scale_
,
203 internal_contents_scale_
);
205 DrawHudContents(hud_surface_
->getCanvas());
207 hud_surface_
->getCanvas()->restore();
210 TRACE_EVENT0("cc", "UploadHudTexture");
212 size_t row_bytes
= 0;
213 const void* pixels
= hud_surface_
->getCanvas()->peekPixels(&info
, &row_bytes
);
215 DCHECK(info
.colorType() == kN32_SkColorType
);
216 resource_provider
->CopyToResource(resources_
.back()->id(),
217 static_cast<const uint8_t*>(pixels
),
218 internal_content_bounds_
);
221 void HeadsUpDisplayLayerImpl::ReleaseResources() {
225 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
226 const LayerTreeDebugState
& debug_state
= layer_tree_impl()->debug_state();
228 // Don't update numbers every frame so text is readable.
229 base::TimeTicks now
= layer_tree_impl()->CurrentBeginFrameArgs().frame_time
;
230 if (base::TimeDelta(now
- time_of_last_graph_update_
).InSecondsF() > 0.25f
) {
231 time_of_last_graph_update_
= now
;
233 if (debug_state
.show_fps_counter
) {
234 FrameRateCounter
* fps_counter
= layer_tree_impl()->frame_rate_counter();
235 fps_graph_
.value
= fps_counter
->GetAverageFPS();
236 fps_counter
->GetMinAndMaxFPS(&fps_graph_
.min
, &fps_graph_
.max
);
239 if (debug_state
.continuous_painting
) {
240 PaintTimeCounter
* paint_time_counter
=
241 layer_tree_impl()->paint_time_counter();
242 base::TimeDelta latest
, min
, max
;
244 if (paint_time_counter
->End())
245 latest
= **paint_time_counter
->End();
246 paint_time_counter
->GetMinAndMaxPaintTime(&min
, &max
);
248 paint_time_graph_
.value
= latest
.InMillisecondsF();
249 paint_time_graph_
.min
= min
.InMillisecondsF();
250 paint_time_graph_
.max
= max
.InMillisecondsF();
253 if (debug_state
.ShowMemoryStats()) {
254 MemoryHistory
* memory_history
= layer_tree_impl()->memory_history();
255 if (memory_history
->End())
256 memory_entry_
= **memory_history
->End();
258 memory_entry_
= MemoryHistory::Entry();
262 fps_graph_
.UpdateUpperBound();
263 paint_time_graph_
.UpdateUpperBound();
266 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas
* canvas
) {
267 const LayerTreeDebugState
& debug_state
= layer_tree_impl()->debug_state();
269 if (debug_state
.ShowHudRects()) {
270 DrawDebugRects(canvas
, layer_tree_impl()->debug_rect_history());
271 if (IsAnimatingHUDContents()) {
272 layer_tree_impl()->SetNeedsRedraw();
276 SkRect area
= SkRect::MakeEmpty();
277 if (debug_state
.continuous_painting
) {
278 area
= DrawPaintTimeDisplay(
279 canvas
, layer_tree_impl()->paint_time_counter(), 0, 0);
280 } else if (debug_state
.show_fps_counter
) {
281 // Don't show the FPS display when continuous painting is enabled, because
282 // it would show misleading numbers.
284 DrawFPSDisplay(canvas
, layer_tree_impl()->frame_rate_counter(), 0, 0);
287 if (debug_state
.show_fps_counter
|| debug_state
.continuous_painting
) {
288 area
= DrawGpuRasterizationStatus(canvas
, 0, area
.bottom(),
289 SkMaxScalar(area
.width(), 150));
292 if (debug_state
.ShowMemoryStats())
293 DrawMemoryDisplay(canvas
, 0, area
.bottom(), SkMaxScalar(area
.width(), 150));
296 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas
* canvas
,
298 const std::string
& text
,
299 SkPaint::Align align
,
303 const bool anti_alias
= paint
->isAntiAlias();
304 paint
->setAntiAlias(true);
306 paint
->setTextSize(size
);
307 paint
->setTextAlign(align
);
308 paint
->setTypeface(typeface_
.get());
309 canvas
->drawText(text
.c_str(), text
.length(), x
, y
, *paint
);
311 paint
->setAntiAlias(anti_alias
);
314 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas
* canvas
,
316 const std::string
& text
,
317 SkPaint::Align align
,
319 const SkPoint
& pos
) const {
320 DrawText(canvas
, paint
, text
, align
, size
, pos
.x(), pos
.y());
323 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas
* canvas
,
325 const SkRect
& bounds
) const {
326 paint
->setColor(DebugColors::HUDBackgroundColor());
327 canvas
->drawRect(bounds
, *paint
);
330 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas
* canvas
,
332 const SkRect
& bounds
,
333 const Graph
& graph
) const {
334 // Draw top and bottom line.
335 paint
->setColor(DebugColors::HUDSeparatorLineColor());
336 canvas
->drawLine(bounds
.left(),
342 bounds
.left(), bounds
.bottom(), bounds
.right(), bounds
.bottom(), *paint
);
344 // Draw indicator line (additive blend mode to increase contrast when drawn on
346 paint
->setColor(DebugColors::HUDIndicatorLineColor());
347 paint
->setXfermodeMode(SkXfermode::kPlus_Mode
);
348 const double indicator_top
=
349 bounds
.height() * (1.0 - graph
.indicator
/ graph
.current_upper_bound
) -
351 canvas
->drawLine(bounds
.left(),
352 bounds
.top() + indicator_top
,
354 bounds
.top() + indicator_top
,
356 paint
->setXfermode(nullptr);
359 SkRect
HeadsUpDisplayLayerImpl::DrawFPSDisplay(
361 const FrameRateCounter
* fps_counter
,
364 const int kPadding
= 4;
367 const int kFontHeight
= 15;
369 const int kGraphWidth
= fps_counter
->time_stamp_history_size() - 2;
370 const int kGraphHeight
= 40;
372 const int kHistogramWidth
= 37;
374 int width
= kGraphWidth
+ kHistogramWidth
+ 4 * kPadding
;
375 int height
= kFontHeight
+ kGraphHeight
+ 4 * kPadding
+ 2;
376 int left
= bounds().width() - width
- right
;
377 SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
379 SkPaint paint
= CreatePaint();
380 DrawGraphBackground(canvas
, &paint
, area
);
383 SkRect::MakeXYWH(left
+ kPadding
,
385 kGraphWidth
+ kHistogramWidth
+ kGap
+ 2,
387 SkRect graph_bounds
= SkRect::MakeXYWH(left
+ kPadding
,
388 text_bounds
.bottom() + 2 * kPadding
,
391 SkRect histogram_bounds
= SkRect::MakeXYWH(graph_bounds
.right() + kGap
,
396 const std::string value_text
=
397 base::StringPrintf("FPS:%5.1f", fps_graph_
.value
);
398 const std::string min_max_text
=
399 base::StringPrintf("%.0f-%.0f", fps_graph_
.min
, fps_graph_
.max
);
401 VLOG(1) << value_text
;
403 paint
.setColor(DebugColors::FPSDisplayTextAndGraphColor());
407 SkPaint::kLeft_Align
,
410 text_bounds
.bottom());
414 SkPaint::kRight_Align
,
417 text_bounds
.bottom());
419 DrawGraphLines(canvas
, &paint
, graph_bounds
, fps_graph_
);
421 // Collect graph and histogram data.
424 const int kHistogramSize
= 20;
425 double histogram
[kHistogramSize
] = { 1.0 };
426 double max_bucket_value
= 1.0;
428 for (FrameRateCounter::RingBufferType::Iterator it
= --fps_counter
->end(); it
;
430 base::TimeDelta delta
= fps_counter
->RecentFrameInterval(it
.index() + 1);
432 // Skip this particular instantaneous frame rate if it is not likely to have
434 if (!fps_counter
->IsBadFrameInterval(delta
)) {
435 double fps
= 1.0 / delta
.InSecondsF();
437 // Clamp the FPS to the range we want to plot visually.
438 double p
= fps
/ fps_graph_
.current_upper_bound
;
442 // Plot this data point.
444 SkPoint::Make(graph_bounds
.left() + it
.index(),
445 graph_bounds
.bottom() - p
* graph_bounds
.height());
451 // Use the fps value to find the right bucket in the histogram.
452 int bucket_index
= floor(p
* (kHistogramSize
- 1));
454 // Add the delta time to take the time spent at that fps rate into
456 histogram
[bucket_index
] += delta
.InSecondsF();
457 max_bucket_value
= std::max(histogram
[bucket_index
], max_bucket_value
);
461 // Draw FPS histogram.
462 paint
.setColor(DebugColors::HUDSeparatorLineColor());
463 canvas
->drawLine(histogram_bounds
.left() - 1,
464 histogram_bounds
.top() - 1,
465 histogram_bounds
.left() - 1,
466 histogram_bounds
.bottom() + 1,
468 canvas
->drawLine(histogram_bounds
.right() + 1,
469 histogram_bounds
.top() - 1,
470 histogram_bounds
.right() + 1,
471 histogram_bounds
.bottom() + 1,
474 paint
.setColor(DebugColors::FPSDisplayTextAndGraphColor());
475 const double bar_height
= histogram_bounds
.height() / kHistogramSize
;
477 for (int i
= kHistogramSize
- 1; i
>= 0; --i
) {
478 if (histogram
[i
] > 0) {
480 histogram
[i
] / max_bucket_value
* histogram_bounds
.width();
482 SkRect::MakeXYWH(histogram_bounds
.left(),
483 histogram_bounds
.bottom() - (i
+ 1) * bar_height
,
491 paint
.setAntiAlias(true);
492 paint
.setStyle(SkPaint::kStroke_Style
);
493 paint
.setStrokeWidth(1);
494 canvas
->drawPath(path
, paint
);
499 SkRect
HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas
* canvas
,
503 if (!memory_entry_
.total_bytes_used
)
504 return SkRect::MakeEmpty();
506 const int kPadding
= 4;
507 const int kFontHeight
= 13;
509 const int height
= 3 * kFontHeight
+ 4 * kPadding
;
510 const int left
= bounds().width() - width
- right
;
511 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
513 const double kMegabyte
= 1024.0 * 1024.0;
515 SkPaint paint
= CreatePaint();
516 DrawGraphBackground(canvas
, &paint
, area
);
518 SkPoint title_pos
= SkPoint::Make(left
+ kPadding
, top
+ kFontHeight
);
519 SkPoint stat1_pos
= SkPoint::Make(left
+ width
- kPadding
- 1,
520 top
+ kPadding
+ 2 * kFontHeight
);
521 SkPoint stat2_pos
= SkPoint::Make(left
+ width
- kPadding
- 1,
522 top
+ 2 * kPadding
+ 3 * kFontHeight
);
524 paint
.setColor(DebugColors::MemoryDisplayTextColor());
528 SkPaint::kLeft_Align
,
532 std::string text
= base::StringPrintf(
533 "%6.1f MB used", memory_entry_
.total_bytes_used
/ kMegabyte
);
534 DrawText(canvas
, &paint
, text
, SkPaint::kRight_Align
, kFontHeight
, stat1_pos
);
536 if (!memory_entry_
.had_enough_memory
)
537 paint
.setColor(SK_ColorRED
);
538 text
= base::StringPrintf("%6.1f MB max ",
539 memory_entry_
.total_budget_in_bytes
/ kMegabyte
);
540 DrawText(canvas
, &paint
, text
, SkPaint::kRight_Align
, kFontHeight
, stat2_pos
);
545 SkRect
HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas
* canvas
,
550 SkColor color
= SK_ColorRED
;
551 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
552 case GpuRasterizationStatus::ON
:
553 status
= "GPU raster: on";
554 color
= SK_ColorGREEN
;
556 case GpuRasterizationStatus::ON_FORCED
:
557 status
= "GPU raster: on (forced)";
558 color
= SK_ColorGREEN
;
560 case GpuRasterizationStatus::OFF_DEVICE
:
561 status
= "GPU raster: off (device)";
564 case GpuRasterizationStatus::OFF_VIEWPORT
:
565 status
= "GPU raster: off (viewport)";
566 color
= SK_ColorYELLOW
;
568 case GpuRasterizationStatus::OFF_CONTENT
:
569 status
= "GPU raster: off (content)";
570 color
= SK_ColorYELLOW
;
575 return SkRect::MakeEmpty();
577 const int kPadding
= 4;
578 const int kFontHeight
= 13;
580 const int height
= kFontHeight
+ 2 * kPadding
;
581 const int left
= bounds().width() - width
- right
;
582 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
584 SkPaint paint
= CreatePaint();
585 DrawGraphBackground(canvas
, &paint
, area
);
587 SkPoint gpu_status_pos
= SkPoint::Make(left
+ kPadding
, top
+ kFontHeight
);
589 paint
.setColor(color
);
590 DrawText(canvas
, &paint
, status
, SkPaint::kLeft_Align
, kFontHeight
,
596 SkRect
HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
598 const PaintTimeCounter
* paint_time_counter
,
601 const int kPadding
= 4;
602 const int kFontHeight
= 15;
604 const int kGraphWidth
= paint_time_counter
->HistorySize();
605 const int kGraphHeight
= 40;
607 const int width
= kGraphWidth
+ 2 * kPadding
;
609 kFontHeight
+ kGraphHeight
+ 4 * kPadding
+ 2 + kFontHeight
+ kPadding
;
610 const int left
= bounds().width() - width
- right
;
612 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
614 SkPaint paint
= CreatePaint();
615 DrawGraphBackground(canvas
, &paint
, area
);
617 SkRect text_bounds
= SkRect::MakeXYWH(
618 left
+ kPadding
, top
+ kPadding
, kGraphWidth
, kFontHeight
);
619 SkRect text_bounds2
= SkRect::MakeXYWH(left
+ kPadding
,
620 text_bounds
.bottom() + kPadding
,
623 SkRect graph_bounds
= SkRect::MakeXYWH(left
+ kPadding
,
624 text_bounds2
.bottom() + 2 * kPadding
,
628 const std::string value_text
=
629 base::StringPrintf("%.1f", paint_time_graph_
.value
);
630 const std::string min_max_text
= base::StringPrintf(
631 "%.1f-%.1f", paint_time_graph_
.min
, paint_time_graph_
.max
);
633 paint
.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
634 DrawText(canvas
, &paint
, "Compositor frame time (ms)", SkPaint::kLeft_Align
,
635 kFontHeight
, text_bounds
.left(), text_bounds
.bottom());
639 SkPaint::kLeft_Align
,
642 text_bounds2
.bottom());
646 SkPaint::kRight_Align
,
648 text_bounds2
.right(),
649 text_bounds2
.bottom());
651 paint
.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
652 for (PaintTimeCounter::RingBufferType::Iterator it
=
653 paint_time_counter
->End();
656 double pt
= it
->InMillisecondsF();
661 double p
= pt
/ paint_time_graph_
.current_upper_bound
;
666 SkRect::MakeXYWH(graph_bounds
.left() + it
.index(),
667 graph_bounds
.bottom() - p
* graph_bounds
.height(),
669 p
* graph_bounds
.height()),
673 DrawGraphLines(canvas
, &paint
, graph_bounds
, paint_time_graph_
);
678 void HeadsUpDisplayLayerImpl::DrawDebugRect(
681 const DebugRect
& rect
,
682 SkColor stroke_color
,
685 const std::string
& label_text
) const {
686 gfx::Rect debug_layer_rect
=
687 gfx::ScaleToEnclosingRect(rect
.rect
, 1.0 / internal_contents_scale_
,
688 1.0 / internal_contents_scale_
);
689 SkIRect sk_rect
= RectToSkIRect(debug_layer_rect
);
690 paint
->setColor(fill_color
);
691 paint
->setStyle(SkPaint::kFill_Style
);
692 canvas
->drawIRect(sk_rect
, *paint
);
694 paint
->setColor(stroke_color
);
695 paint
->setStyle(SkPaint::kStroke_Style
);
696 paint
->setStrokeWidth(SkFloatToScalar(stroke_width
));
697 canvas
->drawIRect(sk_rect
, *paint
);
699 if (label_text
.length()) {
700 const int kFontHeight
= 12;
701 const int kPadding
= 3;
703 // The debug_layer_rect may be huge, and converting to a floating point may
704 // be lossy, so intersect with the HUD layer bounds first to prevent that.
705 gfx::Rect clip_rect
= debug_layer_rect
;
706 clip_rect
.Intersect(gfx::Rect(internal_content_bounds_
));
707 SkRect sk_clip_rect
= RectToSkRect(clip_rect
);
710 canvas
->clipRect(sk_clip_rect
);
711 canvas
->translate(sk_clip_rect
.x(), sk_clip_rect
.y());
713 SkPaint label_paint
= CreatePaint();
714 label_paint
.setTextSize(kFontHeight
);
715 label_paint
.setTypeface(typeface_
.get());
716 label_paint
.setColor(stroke_color
);
718 const SkScalar label_text_width
=
719 label_paint
.measureText(label_text
.c_str(), label_text
.length());
720 canvas
->drawRect(SkRect::MakeWH(label_text_width
+ 2 * kPadding
,
721 kFontHeight
+ 2 * kPadding
),
724 label_paint
.setAntiAlias(true);
725 label_paint
.setColor(SkColorSetARGB(255, 50, 50, 50));
726 canvas
->drawText(label_text
.c_str(),
729 kFontHeight
* 0.8f
+ kPadding
,
736 void HeadsUpDisplayLayerImpl::DrawDebugRects(
738 DebugRectHistory
* debug_rect_history
) {
739 SkPaint paint
= CreatePaint();
741 const std::vector
<DebugRect
>& debug_rects
= debug_rect_history
->debug_rects();
742 std::vector
<DebugRect
> new_paint_rects
;
744 for (size_t i
= 0; i
< debug_rects
.size(); ++i
) {
745 SkColor stroke_color
= 0;
746 SkColor fill_color
= 0;
747 float stroke_width
= 0.f
;
748 std::string label_text
;
750 switch (debug_rects
[i
].type
) {
751 case PAINT_RECT_TYPE
:
752 new_paint_rects
.push_back(debug_rects
[i
]);
754 case PROPERTY_CHANGED_RECT_TYPE
:
755 stroke_color
= DebugColors::PropertyChangedRectBorderColor();
756 fill_color
= DebugColors::PropertyChangedRectFillColor();
757 stroke_width
= DebugColors::PropertyChangedRectBorderWidth();
759 case SURFACE_DAMAGE_RECT_TYPE
:
760 stroke_color
= DebugColors::SurfaceDamageRectBorderColor();
761 fill_color
= DebugColors::SurfaceDamageRectFillColor();
762 stroke_width
= DebugColors::SurfaceDamageRectBorderWidth();
764 case REPLICA_SCREEN_SPACE_RECT_TYPE
:
765 stroke_color
= DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
766 fill_color
= DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
767 stroke_width
= DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
769 case SCREEN_SPACE_RECT_TYPE
:
770 stroke_color
= DebugColors::ScreenSpaceLayerRectBorderColor();
771 fill_color
= DebugColors::ScreenSpaceLayerRectFillColor();
772 stroke_width
= DebugColors::ScreenSpaceLayerRectBorderWidth();
774 case TOUCH_EVENT_HANDLER_RECT_TYPE
:
775 stroke_color
= DebugColors::TouchEventHandlerRectBorderColor();
776 fill_color
= DebugColors::TouchEventHandlerRectFillColor();
777 stroke_width
= DebugColors::TouchEventHandlerRectBorderWidth();
778 label_text
= "touch event listener";
780 case WHEEL_EVENT_HANDLER_RECT_TYPE
:
781 stroke_color
= DebugColors::WheelEventHandlerRectBorderColor();
782 fill_color
= DebugColors::WheelEventHandlerRectFillColor();
783 stroke_width
= DebugColors::WheelEventHandlerRectBorderWidth();
784 label_text
= "mousewheel event listener";
786 case SCROLL_EVENT_HANDLER_RECT_TYPE
:
787 stroke_color
= DebugColors::ScrollEventHandlerRectBorderColor();
788 fill_color
= DebugColors::ScrollEventHandlerRectFillColor();
789 stroke_width
= DebugColors::ScrollEventHandlerRectBorderWidth();
790 label_text
= "scroll event listener";
792 case NON_FAST_SCROLLABLE_RECT_TYPE
:
793 stroke_color
= DebugColors::NonFastScrollableRectBorderColor();
794 fill_color
= DebugColors::NonFastScrollableRectFillColor();
795 stroke_width
= DebugColors::NonFastScrollableRectBorderWidth();
796 label_text
= "repaints on scroll";
798 case ANIMATION_BOUNDS_RECT_TYPE
:
799 stroke_color
= DebugColors::LayerAnimationBoundsBorderColor();
800 fill_color
= DebugColors::LayerAnimationBoundsFillColor();
801 stroke_width
= DebugColors::LayerAnimationBoundsBorderWidth();
802 label_text
= "animation bounds";
806 DrawDebugRect(canvas
,
815 if (new_paint_rects
.size()) {
816 paint_rects_
.swap(new_paint_rects
);
817 fade_step_
= DebugColors::kFadeSteps
;
819 if (fade_step_
> 0) {
821 for (size_t i
= 0; i
< paint_rects_
.size(); ++i
) {
822 DrawDebugRect(canvas
,
825 DebugColors::PaintRectBorderColor(fade_step_
),
826 DebugColors::PaintRectFillColor(fade_step_
),
827 DebugColors::PaintRectBorderWidth(),
833 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
834 return "cc::HeadsUpDisplayLayerImpl";
837 void HeadsUpDisplayLayerImpl::AsValueInto(
838 base::trace_event::TracedValue
* dict
) const {
839 LayerImpl::AsValueInto(dict
);
840 dict
->SetString("layer_name", "Heads Up Display Layer");