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/skia/include/core/SkPaint.h"
24 #include "third_party/skia/include/core/SkTypeface.h"
25 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
26 #include "ui/gfx/geometry/point.h"
27 #include "ui/gfx/geometry/size.h"
28 #include "ui/gfx/geometry/size_conversions.h"
29 #include "ui/gfx/hud_font.h"
33 static inline SkPaint
CreatePaint() {
35 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
36 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
37 // swizzle our colors when drawing to the SkCanvas.
38 SkColorMatrix swizzle_matrix
;
39 for (int i
= 0; i
< 20; ++i
)
40 swizzle_matrix
.fMat
[i
] = 0;
41 swizzle_matrix
.fMat
[0 + 5 * 2] = 1;
42 swizzle_matrix
.fMat
[1 + 5 * 1] = 1;
43 swizzle_matrix
.fMat
[2 + 5 * 0] = 1;
44 swizzle_matrix
.fMat
[3 + 5 * 3] = 1;
46 skia::RefPtr
<SkColorMatrixFilter
> filter
=
47 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix
));
48 paint
.setColorFilter(filter
.get());
53 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value
,
54 double start_upper_bound
)
58 current_upper_bound(start_upper_bound
),
59 default_upper_bound(start_upper_bound
),
60 indicator(indicator_value
) {}
62 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
63 double target_upper_bound
= std::max(max
, default_upper_bound
);
64 current_upper_bound
+= (target_upper_bound
- current_upper_bound
) * 0.5;
65 return current_upper_bound
;
68 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl
* tree_impl
,
70 : LayerImpl(tree_impl
, id
),
71 typeface_(gfx::GetHudTypeface()),
72 internal_contents_scale_(1.f
),
73 fps_graph_(60.0, 80.0),
74 paint_time_graph_(16.0, 48.0),
77 typeface_
= skia::AdoptRef(
78 SkTypeface::CreateFromName("monospace", SkTypeface::kBold
));
82 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
84 scoped_ptr
<LayerImpl
> HeadsUpDisplayLayerImpl::CreateLayerImpl(
85 LayerTreeImpl
* tree_impl
) {
86 return HeadsUpDisplayLayerImpl::Create(tree_impl
, id());
89 void HeadsUpDisplayLayerImpl::AcquireResource(
90 ResourceProvider
* resource_provider
) {
91 for (ScopedPtrVector
<ScopedResource
>::iterator it
= resources_
.begin();
92 it
!= resources_
.end();
94 if (!resource_provider
->InUseByConsumer((*it
)->id())) {
95 resources_
.swap(it
, resources_
.end() - 1);
100 scoped_ptr
<ScopedResource
> resource
=
101 ScopedResource::Create(resource_provider
);
102 resource
->Allocate(internal_content_bounds_
,
103 ResourceProvider::TEXTURE_HINT_IMMUTABLE
, RGBA_8888
);
104 resources_
.push_back(resource
.Pass());
107 class ResourceSizeIsEqualTo
{
109 explicit ResourceSizeIsEqualTo(const gfx::Size
& size_
)
110 : compare_size_(size_
) {}
112 bool operator()(const ScopedResource
* resource
) {
113 return resource
->size() == compare_size_
;
117 const gfx::Size compare_size_
;
120 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
121 ResourceProvider
* resource_provider
) {
122 ScopedPtrVector
<ScopedResource
>::iterator it_erase
=
123 resources_
.partition(ResourceSizeIsEqualTo(internal_content_bounds_
));
124 resources_
.erase(it_erase
, resources_
.end());
127 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode
,
128 ResourceProvider
* resource_provider
) {
129 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
)
132 internal_contents_scale_
= draw_properties().ideal_contents_scale
;
133 internal_content_bounds_
=
134 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_
));
136 ReleaseUnmatchedSizeResources(resource_provider
);
137 AcquireResource(resource_provider
);
138 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
141 void HeadsUpDisplayLayerImpl::AppendQuads(
142 RenderPass
* render_pass
,
143 AppendQuadsData
* append_quads_data
) {
144 if (!resources_
.back()->id())
147 SharedQuadState
* shared_quad_state
=
148 render_pass
->CreateAndAppendSharedQuadState();
149 PopulateScaledSharedQuadState(shared_quad_state
, internal_contents_scale_
);
151 gfx::Rect
quad_rect(internal_content_bounds_
);
152 gfx::Rect
opaque_rect(contents_opaque() ? quad_rect
: gfx::Rect());
153 gfx::Rect
visible_quad_rect(quad_rect
);
154 bool premultiplied_alpha
= true;
155 gfx::PointF
uv_top_left(0.f
, 0.f
);
156 gfx::PointF
uv_bottom_right(1.f
, 1.f
);
157 const float vertex_opacity
[] = { 1.f
, 1.f
, 1.f
, 1.f
};
158 bool flipped
= false;
159 bool nearest_neighbor
= false;
160 TextureDrawQuad
* quad
=
161 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
162 // TODO(danakj): crbug.com/455931
163 layer_tree_impl()->resource_provider()->ValidateResource(
164 resources_
.back()->id());
165 quad
->SetNew(shared_quad_state
,
169 resources_
.back()->id(),
179 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
181 ResourceProvider
* resource_provider
) {
182 if (draw_mode
== DRAW_MODE_RESOURCELESS_SOFTWARE
|| !resources_
.back()->id())
187 canvas_size
= hud_surface_
->getCanvas()->getDeviceSize();
189 canvas_size
.set(0, 0);
191 if (canvas_size
.width() != internal_content_bounds_
.width() ||
192 canvas_size
.height() != internal_content_bounds_
.height() ||
194 TRACE_EVENT0("cc", "ResizeHudCanvas");
196 hud_surface_
= skia::AdoptRef(SkSurface::NewRasterN32Premul(
197 internal_content_bounds_
.width(), internal_content_bounds_
.height()));
203 TRACE_EVENT0("cc", "DrawHudContents");
204 hud_surface_
->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
205 hud_surface_
->getCanvas()->save();
206 hud_surface_
->getCanvas()->scale(internal_contents_scale_
,
207 internal_contents_scale_
);
209 DrawHudContents(hud_surface_
->getCanvas());
211 hud_surface_
->getCanvas()->restore();
214 TRACE_EVENT0("cc", "UploadHudTexture");
216 size_t row_bytes
= 0;
217 const void* pixels
= hud_surface_
->getCanvas()->peekPixels(&info
, &row_bytes
);
219 DCHECK(info
.colorType() == kN32_SkColorType
);
220 resource_provider
->CopyToResource(resources_
.back()->id(),
221 static_cast<const uint8_t*>(pixels
),
222 internal_content_bounds_
);
225 void HeadsUpDisplayLayerImpl::ReleaseResources() {
229 gfx::Rect
HeadsUpDisplayLayerImpl::GetEnclosingRectInTargetSpace() const {
230 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_
);
233 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
234 const LayerTreeDebugState
& debug_state
= layer_tree_impl()->debug_state();
236 // Don't update numbers every frame so text is readable.
237 base::TimeTicks now
= layer_tree_impl()->CurrentBeginFrameArgs().frame_time
;
238 if (base::TimeDelta(now
- time_of_last_graph_update_
).InSecondsF() > 0.25f
) {
239 time_of_last_graph_update_
= now
;
241 if (debug_state
.show_fps_counter
) {
242 FrameRateCounter
* fps_counter
= layer_tree_impl()->frame_rate_counter();
243 fps_graph_
.value
= fps_counter
->GetAverageFPS();
244 fps_counter
->GetMinAndMaxFPS(&fps_graph_
.min
, &fps_graph_
.max
);
247 if (debug_state
.continuous_painting
) {
248 PaintTimeCounter
* paint_time_counter
=
249 layer_tree_impl()->paint_time_counter();
250 base::TimeDelta latest
, min
, max
;
252 if (paint_time_counter
->End())
253 latest
= **paint_time_counter
->End();
254 paint_time_counter
->GetMinAndMaxPaintTime(&min
, &max
);
256 paint_time_graph_
.value
= latest
.InMillisecondsF();
257 paint_time_graph_
.min
= min
.InMillisecondsF();
258 paint_time_graph_
.max
= max
.InMillisecondsF();
261 if (debug_state
.ShowMemoryStats()) {
262 MemoryHistory
* memory_history
= layer_tree_impl()->memory_history();
263 if (memory_history
->End())
264 memory_entry_
= **memory_history
->End();
266 memory_entry_
= MemoryHistory::Entry();
270 fps_graph_
.UpdateUpperBound();
271 paint_time_graph_
.UpdateUpperBound();
274 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas
* canvas
) {
275 const LayerTreeDebugState
& debug_state
= layer_tree_impl()->debug_state();
277 if (debug_state
.ShowHudRects()) {
278 DrawDebugRects(canvas
, layer_tree_impl()->debug_rect_history());
279 if (IsAnimatingHUDContents()) {
280 layer_tree_impl()->SetNeedsRedraw();
284 SkRect area
= SkRect::MakeEmpty();
285 if (debug_state
.continuous_painting
) {
286 area
= DrawPaintTimeDisplay(
287 canvas
, layer_tree_impl()->paint_time_counter(), 0, 0);
288 } else if (debug_state
.show_fps_counter
) {
289 // Don't show the FPS display when continuous painting is enabled, because
290 // it would show misleading numbers.
292 DrawFPSDisplay(canvas
, layer_tree_impl()->frame_rate_counter(), 0, 0);
295 if (debug_state
.show_fps_counter
|| debug_state
.continuous_painting
) {
296 area
= DrawGpuRasterizationStatus(canvas
, 0, area
.bottom(),
297 SkMaxScalar(area
.width(), 150));
300 if (debug_state
.ShowMemoryStats())
301 DrawMemoryDisplay(canvas
, 0, area
.bottom(), SkMaxScalar(area
.width(), 150));
303 int HeadsUpDisplayLayerImpl::MeasureText(SkPaint
* paint
,
304 const std::string
& text
,
306 const bool anti_alias
= paint
->isAntiAlias();
307 paint
->setAntiAlias(true);
308 paint
->setTextSize(size
);
309 paint
->setTypeface(typeface_
.get());
310 SkScalar text_width
= paint
->measureText(text
.c_str(), text
.length());
312 paint
->setAntiAlias(anti_alias
);
313 return SkScalarCeilToInt(text_width
);
315 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas
* canvas
,
317 const std::string
& text
,
318 SkPaint::Align align
,
322 const bool anti_alias
= paint
->isAntiAlias();
323 paint
->setAntiAlias(true);
325 paint
->setTextSize(size
);
326 paint
->setTextAlign(align
);
327 paint
->setTypeface(typeface_
.get());
328 canvas
->drawText(text
.c_str(), text
.length(), x
, y
, *paint
);
330 paint
->setAntiAlias(anti_alias
);
333 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas
* canvas
,
335 const std::string
& text
,
336 SkPaint::Align align
,
338 const SkPoint
& pos
) const {
339 DrawText(canvas
, paint
, text
, align
, size
, pos
.x(), pos
.y());
342 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas
* canvas
,
344 const SkRect
& bounds
) const {
345 paint
->setColor(DebugColors::HUDBackgroundColor());
346 canvas
->drawRect(bounds
, *paint
);
349 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas
* canvas
,
351 const SkRect
& bounds
,
352 const Graph
& graph
) const {
353 // Draw top and bottom line.
354 paint
->setColor(DebugColors::HUDSeparatorLineColor());
355 canvas
->drawLine(bounds
.left(),
361 bounds
.left(), bounds
.bottom(), bounds
.right(), bounds
.bottom(), *paint
);
363 // Draw indicator line (additive blend mode to increase contrast when drawn on
365 paint
->setColor(DebugColors::HUDIndicatorLineColor());
366 paint
->setXfermodeMode(SkXfermode::kPlus_Mode
);
367 const double indicator_top
=
368 bounds
.height() * (1.0 - graph
.indicator
/ graph
.current_upper_bound
) -
370 canvas
->drawLine(bounds
.left(),
371 bounds
.top() + indicator_top
,
373 bounds
.top() + indicator_top
,
375 paint
->setXfermode(nullptr);
378 SkRect
HeadsUpDisplayLayerImpl::DrawFPSDisplay(
380 const FrameRateCounter
* fps_counter
,
383 const int kPadding
= 4;
386 const int kFontHeight
= 15;
388 const int kGraphWidth
= fps_counter
->time_stamp_history_size() - 2;
389 const int kGraphHeight
= 40;
391 const int kHistogramWidth
= 37;
393 int width
= kGraphWidth
+ kHistogramWidth
+ 4 * kPadding
;
394 int height
= kFontHeight
+ kGraphHeight
+ 4 * kPadding
+ 2;
395 int left
= bounds().width() - width
- right
;
396 SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
398 SkPaint paint
= CreatePaint();
399 DrawGraphBackground(canvas
, &paint
, area
);
402 SkRect::MakeXYWH(left
+ kPadding
,
404 kGraphWidth
+ kHistogramWidth
+ kGap
+ 2,
406 SkRect graph_bounds
= SkRect::MakeXYWH(left
+ kPadding
,
407 text_bounds
.bottom() + 2 * kPadding
,
410 SkRect histogram_bounds
= SkRect::MakeXYWH(graph_bounds
.right() + kGap
,
415 const std::string value_text
=
416 base::StringPrintf("FPS:%5.1f", fps_graph_
.value
);
417 const std::string min_max_text
=
418 base::StringPrintf("%.0f-%.0f", fps_graph_
.min
, fps_graph_
.max
);
420 VLOG(1) << value_text
;
422 paint
.setColor(DebugColors::FPSDisplayTextAndGraphColor());
426 SkPaint::kLeft_Align
,
429 text_bounds
.bottom());
433 SkPaint::kRight_Align
,
436 text_bounds
.bottom());
438 DrawGraphLines(canvas
, &paint
, graph_bounds
, fps_graph_
);
440 // Collect graph and histogram data.
443 const int kHistogramSize
= 20;
444 double histogram
[kHistogramSize
] = { 1.0 };
445 double max_bucket_value
= 1.0;
447 for (FrameRateCounter::RingBufferType::Iterator it
= --fps_counter
->end(); it
;
449 base::TimeDelta delta
= fps_counter
->RecentFrameInterval(it
.index() + 1);
451 // Skip this particular instantaneous frame rate if it is not likely to have
453 if (!fps_counter
->IsBadFrameInterval(delta
)) {
454 double fps
= 1.0 / delta
.InSecondsF();
456 // Clamp the FPS to the range we want to plot visually.
457 double p
= fps
/ fps_graph_
.current_upper_bound
;
461 // Plot this data point.
463 SkPoint::Make(graph_bounds
.left() + it
.index(),
464 graph_bounds
.bottom() - p
* graph_bounds
.height());
470 // Use the fps value to find the right bucket in the histogram.
471 int bucket_index
= floor(p
* (kHistogramSize
- 1));
473 // Add the delta time to take the time spent at that fps rate into
475 histogram
[bucket_index
] += delta
.InSecondsF();
476 max_bucket_value
= std::max(histogram
[bucket_index
], max_bucket_value
);
480 // Draw FPS histogram.
481 paint
.setColor(DebugColors::HUDSeparatorLineColor());
482 canvas
->drawLine(histogram_bounds
.left() - 1,
483 histogram_bounds
.top() - 1,
484 histogram_bounds
.left() - 1,
485 histogram_bounds
.bottom() + 1,
487 canvas
->drawLine(histogram_bounds
.right() + 1,
488 histogram_bounds
.top() - 1,
489 histogram_bounds
.right() + 1,
490 histogram_bounds
.bottom() + 1,
493 paint
.setColor(DebugColors::FPSDisplayTextAndGraphColor());
494 const double bar_height
= histogram_bounds
.height() / kHistogramSize
;
496 for (int i
= kHistogramSize
- 1; i
>= 0; --i
) {
497 if (histogram
[i
] > 0) {
499 histogram
[i
] / max_bucket_value
* histogram_bounds
.width();
501 SkRect::MakeXYWH(histogram_bounds
.left(),
502 histogram_bounds
.bottom() - (i
+ 1) * bar_height
,
510 paint
.setAntiAlias(true);
511 paint
.setStyle(SkPaint::kStroke_Style
);
512 paint
.setStrokeWidth(1);
513 canvas
->drawPath(path
, paint
);
518 SkRect
HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas
* canvas
,
522 if (!memory_entry_
.total_bytes_used
)
523 return SkRect::MakeEmpty();
525 const int kPadding
= 4;
526 const int kFontHeight
= 13;
528 const int height
= 3 * kFontHeight
+ 4 * kPadding
;
529 const int left
= bounds().width() - width
- right
;
530 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
532 const double kMegabyte
= 1024.0 * 1024.0;
534 SkPaint paint
= CreatePaint();
535 DrawGraphBackground(canvas
, &paint
, area
);
537 SkPoint title_pos
= SkPoint::Make(left
+ kPadding
, top
+ kFontHeight
);
538 SkPoint stat1_pos
= SkPoint::Make(left
+ width
- kPadding
- 1,
539 top
+ kPadding
+ 2 * kFontHeight
);
540 SkPoint stat2_pos
= SkPoint::Make(left
+ width
- kPadding
- 1,
541 top
+ 2 * kPadding
+ 3 * kFontHeight
);
543 paint
.setColor(DebugColors::MemoryDisplayTextColor());
547 SkPaint::kLeft_Align
,
551 std::string text
= base::StringPrintf(
552 "%6.1f MB used", memory_entry_
.total_bytes_used
/ kMegabyte
);
553 DrawText(canvas
, &paint
, text
, SkPaint::kRight_Align
, kFontHeight
, stat1_pos
);
555 if (!memory_entry_
.had_enough_memory
)
556 paint
.setColor(SK_ColorRED
);
557 text
= base::StringPrintf("%6.1f MB max ",
558 memory_entry_
.total_budget_in_bytes
/ kMegabyte
);
559 DrawText(canvas
, &paint
, text
, SkPaint::kRight_Align
, kFontHeight
, stat2_pos
);
564 SkRect
HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas
* canvas
,
569 SkColor color
= SK_ColorRED
;
570 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
571 case GpuRasterizationStatus::ON
:
573 color
= SK_ColorGREEN
;
575 case GpuRasterizationStatus::ON_FORCED
:
576 status
= "on (forced)";
577 color
= SK_ColorGREEN
;
579 case GpuRasterizationStatus::OFF_DEVICE
:
580 status
= "off (device)";
583 case GpuRasterizationStatus::OFF_VIEWPORT
:
584 status
= "off (viewport)";
585 color
= SK_ColorYELLOW
;
587 case GpuRasterizationStatus::OFF_CONTENT
:
588 status
= "off (content)";
589 color
= SK_ColorYELLOW
;
594 return SkRect::MakeEmpty();
596 const int kPadding
= 4;
597 const int kFontHeight
= 13;
599 const int height
= 2 * kFontHeight
+ 3 * kPadding
;
600 const int left
= bounds().width() - width
- right
;
601 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
603 SkPaint paint
= CreatePaint();
604 DrawGraphBackground(canvas
, &paint
, area
);
606 SkPoint gpu_status_pos
= SkPoint::Make(left
+ width
- kPadding
,
607 top
+ 2 * kFontHeight
+ 2 * kPadding
);
609 paint
.setColor(color
);
610 DrawText(canvas
, &paint
, "GPU raster: ", SkPaint::kLeft_Align
, kFontHeight
,
611 left
+ kPadding
, top
+ kFontHeight
+ kPadding
);
612 DrawText(canvas
, &paint
, status
, SkPaint::kRight_Align
, kFontHeight
,
618 SkRect
HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
620 const PaintTimeCounter
* paint_time_counter
,
623 const int kPadding
= 4;
624 const int kFontHeight
= 14;
626 const int kGraphWidth
= paint_time_counter
->HistorySize();
627 const int kGraphHeight
= 40;
629 SkPaint paint
= CreatePaint();
631 const std::string title
= "Compositor frame time (ms)";
632 int title_text_width
= MeasureText(&paint
, title
, kFontHeight
);
633 int contents_width
= std::max(title_text_width
, kGraphWidth
);
635 const int width
= contents_width
+ 2 * kPadding
;
637 kFontHeight
+ kGraphHeight
+ 4 * kPadding
+ 2 + kFontHeight
+ kPadding
;
638 const int left
= bounds().width() - width
- right
;
640 const SkRect area
= SkRect::MakeXYWH(left
, top
, width
, height
);
642 DrawGraphBackground(canvas
, &paint
, area
);
644 SkRect text_bounds
= SkRect::MakeXYWH(left
+ kPadding
, top
+ kPadding
,
645 contents_width
, kFontHeight
);
646 SkRect text_bounds2
=
647 SkRect::MakeXYWH(left
+ kPadding
, text_bounds
.bottom() + kPadding
,
648 contents_width
, kFontHeight
);
649 SkRect graph_bounds
= SkRect::MakeXYWH(left
+ (width
- kGraphWidth
) / 2,
650 text_bounds2
.bottom() + 2 * kPadding
,
651 kGraphWidth
, kGraphHeight
);
653 const std::string value_text
=
654 base::StringPrintf("%.1f", paint_time_graph_
.value
);
655 const std::string min_max_text
= base::StringPrintf(
656 "%.1f-%.1f", paint_time_graph_
.min
, paint_time_graph_
.max
);
658 paint
.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
659 DrawText(canvas
, &paint
, title
, SkPaint::kLeft_Align
, kFontHeight
,
660 text_bounds
.left(), text_bounds
.bottom());
664 SkPaint::kLeft_Align
,
667 text_bounds2
.bottom());
671 SkPaint::kRight_Align
,
673 text_bounds2
.right(),
674 text_bounds2
.bottom());
676 paint
.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
677 for (PaintTimeCounter::RingBufferType::Iterator it
=
678 paint_time_counter
->End();
681 double pt
= it
->InMillisecondsF();
686 double p
= pt
/ paint_time_graph_
.current_upper_bound
;
691 SkRect::MakeXYWH(graph_bounds
.left() + it
.index(),
692 graph_bounds
.bottom() - p
* graph_bounds
.height(),
694 p
* graph_bounds
.height()),
698 DrawGraphLines(canvas
, &paint
, graph_bounds
, paint_time_graph_
);
703 void HeadsUpDisplayLayerImpl::DrawDebugRect(
706 const DebugRect
& rect
,
707 SkColor stroke_color
,
710 const std::string
& label_text
) const {
711 gfx::Rect debug_layer_rect
=
712 gfx::ScaleToEnclosingRect(rect
.rect
, 1.0 / internal_contents_scale_
,
713 1.0 / internal_contents_scale_
);
714 SkIRect sk_rect
= RectToSkIRect(debug_layer_rect
);
715 paint
->setColor(fill_color
);
716 paint
->setStyle(SkPaint::kFill_Style
);
717 canvas
->drawIRect(sk_rect
, *paint
);
719 paint
->setColor(stroke_color
);
720 paint
->setStyle(SkPaint::kStroke_Style
);
721 paint
->setStrokeWidth(SkFloatToScalar(stroke_width
));
722 canvas
->drawIRect(sk_rect
, *paint
);
724 if (label_text
.length()) {
725 const int kFontHeight
= 12;
726 const int kPadding
= 3;
728 // The debug_layer_rect may be huge, and converting to a floating point may
729 // be lossy, so intersect with the HUD layer bounds first to prevent that.
730 gfx::Rect clip_rect
= debug_layer_rect
;
731 clip_rect
.Intersect(gfx::Rect(internal_content_bounds_
));
732 SkRect sk_clip_rect
= RectToSkRect(clip_rect
);
735 canvas
->clipRect(sk_clip_rect
);
736 canvas
->translate(sk_clip_rect
.x(), sk_clip_rect
.y());
738 SkPaint label_paint
= CreatePaint();
739 label_paint
.setTextSize(kFontHeight
);
740 label_paint
.setTypeface(typeface_
.get());
741 label_paint
.setColor(stroke_color
);
743 const SkScalar label_text_width
=
744 label_paint
.measureText(label_text
.c_str(), label_text
.length());
745 canvas
->drawRect(SkRect::MakeWH(label_text_width
+ 2 * kPadding
,
746 kFontHeight
+ 2 * kPadding
),
749 label_paint
.setAntiAlias(true);
750 label_paint
.setColor(SkColorSetARGB(255, 50, 50, 50));
751 canvas
->drawText(label_text
.c_str(),
754 kFontHeight
* 0.8f
+ kPadding
,
761 void HeadsUpDisplayLayerImpl::DrawDebugRects(
763 DebugRectHistory
* debug_rect_history
) {
764 SkPaint paint
= CreatePaint();
766 const std::vector
<DebugRect
>& debug_rects
= debug_rect_history
->debug_rects();
767 std::vector
<DebugRect
> new_paint_rects
;
769 for (size_t i
= 0; i
< debug_rects
.size(); ++i
) {
770 SkColor stroke_color
= 0;
771 SkColor fill_color
= 0;
772 float stroke_width
= 0.f
;
773 std::string label_text
;
775 switch (debug_rects
[i
].type
) {
776 case PAINT_RECT_TYPE
:
777 new_paint_rects
.push_back(debug_rects
[i
]);
779 case PROPERTY_CHANGED_RECT_TYPE
:
780 stroke_color
= DebugColors::PropertyChangedRectBorderColor();
781 fill_color
= DebugColors::PropertyChangedRectFillColor();
782 stroke_width
= DebugColors::PropertyChangedRectBorderWidth();
784 case SURFACE_DAMAGE_RECT_TYPE
:
785 stroke_color
= DebugColors::SurfaceDamageRectBorderColor();
786 fill_color
= DebugColors::SurfaceDamageRectFillColor();
787 stroke_width
= DebugColors::SurfaceDamageRectBorderWidth();
789 case REPLICA_SCREEN_SPACE_RECT_TYPE
:
790 stroke_color
= DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
791 fill_color
= DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
792 stroke_width
= DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
794 case SCREEN_SPACE_RECT_TYPE
:
795 stroke_color
= DebugColors::ScreenSpaceLayerRectBorderColor();
796 fill_color
= DebugColors::ScreenSpaceLayerRectFillColor();
797 stroke_width
= DebugColors::ScreenSpaceLayerRectBorderWidth();
799 case TOUCH_EVENT_HANDLER_RECT_TYPE
:
800 stroke_color
= DebugColors::TouchEventHandlerRectBorderColor();
801 fill_color
= DebugColors::TouchEventHandlerRectFillColor();
802 stroke_width
= DebugColors::TouchEventHandlerRectBorderWidth();
803 label_text
= "touch event listener";
805 case WHEEL_EVENT_HANDLER_RECT_TYPE
:
806 stroke_color
= DebugColors::WheelEventHandlerRectBorderColor();
807 fill_color
= DebugColors::WheelEventHandlerRectFillColor();
808 stroke_width
= DebugColors::WheelEventHandlerRectBorderWidth();
809 label_text
= "mousewheel event listener";
811 case SCROLL_EVENT_HANDLER_RECT_TYPE
:
812 stroke_color
= DebugColors::ScrollEventHandlerRectBorderColor();
813 fill_color
= DebugColors::ScrollEventHandlerRectFillColor();
814 stroke_width
= DebugColors::ScrollEventHandlerRectBorderWidth();
815 label_text
= "scroll event listener";
817 case NON_FAST_SCROLLABLE_RECT_TYPE
:
818 stroke_color
= DebugColors::NonFastScrollableRectBorderColor();
819 fill_color
= DebugColors::NonFastScrollableRectFillColor();
820 stroke_width
= DebugColors::NonFastScrollableRectBorderWidth();
821 label_text
= "repaints on scroll";
823 case ANIMATION_BOUNDS_RECT_TYPE
:
824 stroke_color
= DebugColors::LayerAnimationBoundsBorderColor();
825 fill_color
= DebugColors::LayerAnimationBoundsFillColor();
826 stroke_width
= DebugColors::LayerAnimationBoundsBorderWidth();
827 label_text
= "animation bounds";
831 DrawDebugRect(canvas
,
840 if (new_paint_rects
.size()) {
841 paint_rects_
.swap(new_paint_rects
);
842 fade_step_
= DebugColors::kFadeSteps
;
844 if (fade_step_
> 0) {
846 for (size_t i
= 0; i
< paint_rects_
.size(); ++i
) {
847 DrawDebugRect(canvas
,
850 DebugColors::PaintRectBorderColor(fade_step_
),
851 DebugColors::PaintRectFillColor(fade_step_
),
852 DebugColors::PaintRectBorderWidth(),
858 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
859 return "cc::HeadsUpDisplayLayerImpl";
862 void HeadsUpDisplayLayerImpl::AsValueInto(
863 base::trace_event::TracedValue
* dict
) const {
864 LayerImpl::AsValueInto(dict
);
865 dict
->SetString("layer_name", "Heads Up Display Layer");