Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob3eacbbde7df7f0acbb138dde3b44465b1e5e0e8b
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"
7 #include <algorithm>
8 #include <vector>
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"
31 namespace cc {
33 static inline SkPaint CreatePaint() {
34 SkPaint paint;
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());
49 #endif
50 return paint;
53 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
54 double start_upper_bound)
55 : value(0.0),
56 min(0.0),
57 max(0.0),
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,
69 int id)
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),
75 fade_step_(0) {
76 if (!typeface_) {
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();
93 ++it) {
94 if (!resource_provider->InUseByConsumer((*it)->id())) {
95 resources_.swap(it, resources_.end() - 1);
96 return;
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 {
108 public:
109 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
110 : compare_size_(size_) {}
112 bool operator()(const ScopedResource* resource) {
113 return resource->size() == compare_size_;
116 private:
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)
130 return false;
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())
145 return;
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,
166 quad_rect,
167 opaque_rect,
168 visible_quad_rect,
169 resources_.back()->id(),
170 premultiplied_alpha,
171 uv_top_left,
172 uv_bottom_right,
173 SK_ColorTRANSPARENT,
174 vertex_opacity,
175 flipped,
176 nearest_neighbor);
179 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
180 DrawMode draw_mode,
181 ResourceProvider* resource_provider) {
182 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
183 return;
185 SkISize canvas_size;
186 if (hud_surface_)
187 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
188 else
189 canvas_size.set(0, 0);
191 if (canvas_size.width() != internal_content_bounds_.width() ||
192 canvas_size.height() != internal_content_bounds_.height() ||
193 !hud_surface_) {
194 TRACE_EVENT0("cc", "ResizeHudCanvas");
196 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
197 internal_content_bounds_.width(), internal_content_bounds_.height()));
200 UpdateHudContents();
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");
215 SkImageInfo info;
216 size_t row_bytes = 0;
217 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
218 DCHECK(pixels);
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() {
226 resources_.clear();
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();
265 else
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.
291 area =
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,
305 int size) const {
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,
316 SkPaint* paint,
317 const std::string& text,
318 SkPaint::Align align,
319 int size,
320 int x,
321 int y) const {
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,
334 SkPaint* paint,
335 const std::string& text,
336 SkPaint::Align align,
337 int size,
338 const SkPoint& pos) const {
339 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
342 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
343 SkPaint* paint,
344 const SkRect& bounds) const {
345 paint->setColor(DebugColors::HUDBackgroundColor());
346 canvas->drawRect(bounds, *paint);
349 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
350 SkPaint* paint,
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(),
356 bounds.top() - 1,
357 bounds.right(),
358 bounds.top() - 1,
359 *paint);
360 canvas->drawLine(
361 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
363 // Draw indicator line (additive blend mode to increase contrast when drawn on
364 // top of graph).
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) -
369 1.0;
370 canvas->drawLine(bounds.left(),
371 bounds.top() + indicator_top,
372 bounds.right(),
373 bounds.top() + indicator_top,
374 *paint);
375 paint->setXfermode(nullptr);
378 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
379 SkCanvas* canvas,
380 const FrameRateCounter* fps_counter,
381 int right,
382 int top) const {
383 const int kPadding = 4;
384 const int kGap = 6;
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);
401 SkRect text_bounds =
402 SkRect::MakeXYWH(left + kPadding,
403 top + kPadding,
404 kGraphWidth + kHistogramWidth + kGap + 2,
405 kFontHeight);
406 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
407 text_bounds.bottom() + 2 * kPadding,
408 kGraphWidth,
409 kGraphHeight);
410 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
411 graph_bounds.top(),
412 kHistogramWidth,
413 kGraphHeight);
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());
423 DrawText(canvas,
424 &paint,
425 value_text,
426 SkPaint::kLeft_Align,
427 kFontHeight,
428 text_bounds.left(),
429 text_bounds.bottom());
430 DrawText(canvas,
431 &paint,
432 min_max_text,
433 SkPaint::kRight_Align,
434 kFontHeight,
435 text_bounds.right(),
436 text_bounds.bottom());
438 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
440 // Collect graph and histogram data.
441 SkPath path;
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;
448 --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
452 // been valid.
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;
458 if (p > 1.0)
459 p = 1.0;
461 // Plot this data point.
462 SkPoint cur =
463 SkPoint::Make(graph_bounds.left() + it.index(),
464 graph_bounds.bottom() - p * graph_bounds.height());
465 if (path.isEmpty())
466 path.moveTo(cur);
467 else
468 path.lineTo(cur);
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
474 // account.
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,
486 paint);
487 canvas->drawLine(histogram_bounds.right() + 1,
488 histogram_bounds.top() - 1,
489 histogram_bounds.right() + 1,
490 histogram_bounds.bottom() + 1,
491 paint);
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) {
498 double bar_width =
499 histogram[i] / max_bucket_value * histogram_bounds.width();
500 canvas->drawRect(
501 SkRect::MakeXYWH(histogram_bounds.left(),
502 histogram_bounds.bottom() - (i + 1) * bar_height,
503 bar_width,
505 paint);
509 // Draw FPS graph.
510 paint.setAntiAlias(true);
511 paint.setStyle(SkPaint::kStroke_Style);
512 paint.setStrokeWidth(1);
513 canvas->drawPath(path, paint);
515 return area;
518 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
519 int right,
520 int top,
521 int width) const {
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());
544 DrawText(canvas,
545 &paint,
546 "GPU memory",
547 SkPaint::kLeft_Align,
548 kFontHeight,
549 title_pos);
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);
561 return area;
564 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
565 int right,
566 int top,
567 int width) const {
568 std::string status;
569 SkColor color = SK_ColorRED;
570 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
571 case GpuRasterizationStatus::ON:
572 status = "on";
573 color = SK_ColorGREEN;
574 break;
575 case GpuRasterizationStatus::ON_FORCED:
576 status = "on (forced)";
577 color = SK_ColorGREEN;
578 break;
579 case GpuRasterizationStatus::OFF_DEVICE:
580 status = "off (device)";
581 color = SK_ColorRED;
582 break;
583 case GpuRasterizationStatus::OFF_VIEWPORT:
584 status = "off (viewport)";
585 color = SK_ColorYELLOW;
586 break;
587 case GpuRasterizationStatus::OFF_CONTENT:
588 status = "off (content)";
589 color = SK_ColorYELLOW;
590 break;
593 if (status.empty())
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,
613 gpu_status_pos);
615 return area;
618 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
619 SkCanvas* canvas,
620 const PaintTimeCounter* paint_time_counter,
621 int right,
622 int top) const {
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;
636 const int height =
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());
661 DrawText(canvas,
662 &paint,
663 value_text,
664 SkPaint::kLeft_Align,
665 kFontHeight,
666 text_bounds2.left(),
667 text_bounds2.bottom());
668 DrawText(canvas,
669 &paint,
670 min_max_text,
671 SkPaint::kRight_Align,
672 kFontHeight,
673 text_bounds2.right(),
674 text_bounds2.bottom());
676 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
677 for (PaintTimeCounter::RingBufferType::Iterator it =
678 paint_time_counter->End();
680 --it) {
681 double pt = it->InMillisecondsF();
683 if (pt == 0.0)
684 continue;
686 double p = pt / paint_time_graph_.current_upper_bound;
687 if (p > 1.0)
688 p = 1.0;
690 canvas->drawRect(
691 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
692 graph_bounds.bottom() - p * graph_bounds.height(),
694 p * graph_bounds.height()),
695 paint);
698 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
700 return area;
703 void HeadsUpDisplayLayerImpl::DrawDebugRect(
704 SkCanvas* canvas,
705 SkPaint* paint,
706 const DebugRect& rect,
707 SkColor stroke_color,
708 SkColor fill_color,
709 float stroke_width,
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);
734 canvas->save();
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),
747 label_paint);
749 label_paint.setAntiAlias(true);
750 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
751 canvas->drawText(label_text.c_str(),
752 label_text.length(),
753 kPadding,
754 kFontHeight * 0.8f + kPadding,
755 label_paint);
757 canvas->restore();
761 void HeadsUpDisplayLayerImpl::DrawDebugRects(
762 SkCanvas* canvas,
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]);
778 continue;
779 case PROPERTY_CHANGED_RECT_TYPE:
780 stroke_color = DebugColors::PropertyChangedRectBorderColor();
781 fill_color = DebugColors::PropertyChangedRectFillColor();
782 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
783 break;
784 case SURFACE_DAMAGE_RECT_TYPE:
785 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
786 fill_color = DebugColors::SurfaceDamageRectFillColor();
787 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
788 break;
789 case REPLICA_SCREEN_SPACE_RECT_TYPE:
790 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
791 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
792 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
793 break;
794 case SCREEN_SPACE_RECT_TYPE:
795 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
796 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
797 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
798 break;
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";
804 break;
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";
810 break;
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";
816 break;
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";
822 break;
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";
828 break;
831 DrawDebugRect(canvas,
832 &paint,
833 debug_rects[i],
834 stroke_color,
835 fill_color,
836 stroke_width,
837 label_text);
840 if (new_paint_rects.size()) {
841 paint_rects_.swap(new_paint_rects);
842 fade_step_ = DebugColors::kFadeSteps;
844 if (fade_step_ > 0) {
845 fade_step_--;
846 for (size_t i = 0; i < paint_rects_.size(); ++i) {
847 DrawDebugRect(canvas,
848 &paint,
849 paint_rects_[i],
850 DebugColors::PaintRectBorderColor(fade_step_),
851 DebugColors::PaintRectFillColor(fade_step_),
852 DebugColors::PaintRectBorderWidth(),
853 "");
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");
868 } // namespace cc