Update comments of TabObserver#onLoadStarted and rename onContentChanged
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob986c332e406c7ac64f6ef5538b212a758c2f1725
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 quad->SetNew(shared_quad_state,
163 quad_rect,
164 opaque_rect,
165 visible_quad_rect,
166 resources_.back()->id(),
167 premultiplied_alpha,
168 uv_top_left,
169 uv_bottom_right,
170 SK_ColorTRANSPARENT,
171 vertex_opacity,
172 flipped,
173 nearest_neighbor);
174 ValidateQuadResources(quad);
177 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
178 DrawMode draw_mode,
179 ResourceProvider* resource_provider) {
180 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
181 return;
183 SkISize canvas_size;
184 if (hud_surface_)
185 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
186 else
187 canvas_size.set(0, 0);
189 if (canvas_size.width() != internal_content_bounds_.width() ||
190 canvas_size.height() != internal_content_bounds_.height() ||
191 !hud_surface_) {
192 TRACE_EVENT0("cc", "ResizeHudCanvas");
194 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
195 internal_content_bounds_.width(), internal_content_bounds_.height()));
198 UpdateHudContents();
201 TRACE_EVENT0("cc", "DrawHudContents");
202 hud_surface_->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
203 hud_surface_->getCanvas()->save();
204 hud_surface_->getCanvas()->scale(internal_contents_scale_,
205 internal_contents_scale_);
207 DrawHudContents(hud_surface_->getCanvas());
209 hud_surface_->getCanvas()->restore();
212 TRACE_EVENT0("cc", "UploadHudTexture");
213 SkImageInfo info;
214 size_t row_bytes = 0;
215 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
216 DCHECK(pixels);
217 DCHECK(info.colorType() == kN32_SkColorType);
218 resource_provider->CopyToResource(resources_.back()->id(),
219 static_cast<const uint8_t*>(pixels),
220 internal_content_bounds_);
223 void HeadsUpDisplayLayerImpl::ReleaseResources() {
224 resources_.clear();
227 gfx::Rect HeadsUpDisplayLayerImpl::GetEnclosingRectInTargetSpace() const {
228 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_);
231 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
232 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
234 // Don't update numbers every frame so text is readable.
235 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
236 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
237 time_of_last_graph_update_ = now;
239 if (debug_state.show_fps_counter) {
240 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
241 fps_graph_.value = fps_counter->GetAverageFPS();
242 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
245 if (debug_state.continuous_painting) {
246 PaintTimeCounter* paint_time_counter =
247 layer_tree_impl()->paint_time_counter();
248 base::TimeDelta latest, min, max;
250 if (paint_time_counter->End())
251 latest = **paint_time_counter->End();
252 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
254 paint_time_graph_.value = latest.InMillisecondsF();
255 paint_time_graph_.min = min.InMillisecondsF();
256 paint_time_graph_.max = max.InMillisecondsF();
259 if (debug_state.ShowMemoryStats()) {
260 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
261 if (memory_history->End())
262 memory_entry_ = **memory_history->End();
263 else
264 memory_entry_ = MemoryHistory::Entry();
268 fps_graph_.UpdateUpperBound();
269 paint_time_graph_.UpdateUpperBound();
272 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
273 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
275 if (debug_state.ShowHudRects()) {
276 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
277 if (IsAnimatingHUDContents()) {
278 layer_tree_impl()->SetNeedsRedraw();
282 SkRect area = SkRect::MakeEmpty();
283 if (debug_state.continuous_painting) {
284 area = DrawPaintTimeDisplay(
285 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
286 } else if (debug_state.show_fps_counter) {
287 // Don't show the FPS display when continuous painting is enabled, because
288 // it would show misleading numbers.
289 area =
290 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
293 if (debug_state.show_fps_counter || debug_state.continuous_painting) {
294 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
295 SkMaxScalar(area.width(), 150));
298 if (debug_state.ShowMemoryStats())
299 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
301 int HeadsUpDisplayLayerImpl::MeasureText(SkPaint* paint,
302 const std::string& text,
303 int size) const {
304 const bool anti_alias = paint->isAntiAlias();
305 paint->setAntiAlias(true);
306 paint->setTextSize(size);
307 paint->setTypeface(typeface_.get());
308 SkScalar text_width = paint->measureText(text.c_str(), text.length());
310 paint->setAntiAlias(anti_alias);
311 return SkScalarCeilToInt(text_width);
313 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
314 SkPaint* paint,
315 const std::string& text,
316 SkPaint::Align align,
317 int size,
318 int x,
319 int y) const {
320 const bool anti_alias = paint->isAntiAlias();
321 paint->setAntiAlias(true);
323 paint->setTextSize(size);
324 paint->setTextAlign(align);
325 paint->setTypeface(typeface_.get());
326 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
328 paint->setAntiAlias(anti_alias);
331 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
332 SkPaint* paint,
333 const std::string& text,
334 SkPaint::Align align,
335 int size,
336 const SkPoint& pos) const {
337 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
340 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
341 SkPaint* paint,
342 const SkRect& bounds) const {
343 paint->setColor(DebugColors::HUDBackgroundColor());
344 canvas->drawRect(bounds, *paint);
347 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
348 SkPaint* paint,
349 const SkRect& bounds,
350 const Graph& graph) const {
351 // Draw top and bottom line.
352 paint->setColor(DebugColors::HUDSeparatorLineColor());
353 canvas->drawLine(bounds.left(),
354 bounds.top() - 1,
355 bounds.right(),
356 bounds.top() - 1,
357 *paint);
358 canvas->drawLine(
359 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
361 // Draw indicator line (additive blend mode to increase contrast when drawn on
362 // top of graph).
363 paint->setColor(DebugColors::HUDIndicatorLineColor());
364 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
365 const double indicator_top =
366 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
367 1.0;
368 canvas->drawLine(bounds.left(),
369 bounds.top() + indicator_top,
370 bounds.right(),
371 bounds.top() + indicator_top,
372 *paint);
373 paint->setXfermode(nullptr);
376 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
377 SkCanvas* canvas,
378 const FrameRateCounter* fps_counter,
379 int right,
380 int top) const {
381 const int kPadding = 4;
382 const int kGap = 6;
384 const int kFontHeight = 15;
386 const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
387 const int kGraphHeight = 40;
389 const int kHistogramWidth = 37;
391 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
392 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
393 int left = bounds().width() - width - right;
394 SkRect area = SkRect::MakeXYWH(left, top, width, height);
396 SkPaint paint = CreatePaint();
397 DrawGraphBackground(canvas, &paint, area);
399 SkRect text_bounds =
400 SkRect::MakeXYWH(left + kPadding,
401 top + kPadding,
402 kGraphWidth + kHistogramWidth + kGap + 2,
403 kFontHeight);
404 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
405 text_bounds.bottom() + 2 * kPadding,
406 kGraphWidth,
407 kGraphHeight);
408 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
409 graph_bounds.top(),
410 kHistogramWidth,
411 kGraphHeight);
413 const std::string value_text =
414 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
415 const std::string min_max_text =
416 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
418 VLOG(1) << value_text;
420 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
421 DrawText(canvas,
422 &paint,
423 value_text,
424 SkPaint::kLeft_Align,
425 kFontHeight,
426 text_bounds.left(),
427 text_bounds.bottom());
428 DrawText(canvas,
429 &paint,
430 min_max_text,
431 SkPaint::kRight_Align,
432 kFontHeight,
433 text_bounds.right(),
434 text_bounds.bottom());
436 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
438 // Collect graph and histogram data.
439 SkPath path;
441 const int kHistogramSize = 20;
442 double histogram[kHistogramSize] = { 1.0 };
443 double max_bucket_value = 1.0;
445 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
446 --it) {
447 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
449 // Skip this particular instantaneous frame rate if it is not likely to have
450 // been valid.
451 if (!fps_counter->IsBadFrameInterval(delta)) {
452 double fps = 1.0 / delta.InSecondsF();
454 // Clamp the FPS to the range we want to plot visually.
455 double p = fps / fps_graph_.current_upper_bound;
456 if (p > 1.0)
457 p = 1.0;
459 // Plot this data point.
460 SkPoint cur =
461 SkPoint::Make(graph_bounds.left() + it.index(),
462 graph_bounds.bottom() - p * graph_bounds.height());
463 if (path.isEmpty())
464 path.moveTo(cur);
465 else
466 path.lineTo(cur);
468 // Use the fps value to find the right bucket in the histogram.
469 int bucket_index = floor(p * (kHistogramSize - 1));
471 // Add the delta time to take the time spent at that fps rate into
472 // account.
473 histogram[bucket_index] += delta.InSecondsF();
474 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
478 // Draw FPS histogram.
479 paint.setColor(DebugColors::HUDSeparatorLineColor());
480 canvas->drawLine(histogram_bounds.left() - 1,
481 histogram_bounds.top() - 1,
482 histogram_bounds.left() - 1,
483 histogram_bounds.bottom() + 1,
484 paint);
485 canvas->drawLine(histogram_bounds.right() + 1,
486 histogram_bounds.top() - 1,
487 histogram_bounds.right() + 1,
488 histogram_bounds.bottom() + 1,
489 paint);
491 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
492 const double bar_height = histogram_bounds.height() / kHistogramSize;
494 for (int i = kHistogramSize - 1; i >= 0; --i) {
495 if (histogram[i] > 0) {
496 double bar_width =
497 histogram[i] / max_bucket_value * histogram_bounds.width();
498 canvas->drawRect(
499 SkRect::MakeXYWH(histogram_bounds.left(),
500 histogram_bounds.bottom() - (i + 1) * bar_height,
501 bar_width,
503 paint);
507 // Draw FPS graph.
508 paint.setAntiAlias(true);
509 paint.setStyle(SkPaint::kStroke_Style);
510 paint.setStrokeWidth(1);
511 canvas->drawPath(path, paint);
513 return area;
516 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
517 int right,
518 int top,
519 int width) const {
520 if (!memory_entry_.total_bytes_used)
521 return SkRect::MakeEmpty();
523 const int kPadding = 4;
524 const int kFontHeight = 13;
526 const int height = 3 * kFontHeight + 4 * kPadding;
527 const int left = bounds().width() - width - right;
528 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
530 const double kMegabyte = 1024.0 * 1024.0;
532 SkPaint paint = CreatePaint();
533 DrawGraphBackground(canvas, &paint, area);
535 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
536 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
537 top + kPadding + 2 * kFontHeight);
538 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
539 top + 2 * kPadding + 3 * kFontHeight);
541 paint.setColor(DebugColors::MemoryDisplayTextColor());
542 DrawText(canvas,
543 &paint,
544 "GPU memory",
545 SkPaint::kLeft_Align,
546 kFontHeight,
547 title_pos);
549 std::string text = base::StringPrintf(
550 "%6.1f MB used", memory_entry_.total_bytes_used / kMegabyte);
551 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
553 if (!memory_entry_.had_enough_memory)
554 paint.setColor(SK_ColorRED);
555 text = base::StringPrintf("%6.1f MB max ",
556 memory_entry_.total_budget_in_bytes / kMegabyte);
557 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
559 return area;
562 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
563 int right,
564 int top,
565 int width) const {
566 std::string status;
567 SkColor color = SK_ColorRED;
568 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
569 case GpuRasterizationStatus::ON:
570 status = "on";
571 color = SK_ColorGREEN;
572 break;
573 case GpuRasterizationStatus::ON_FORCED:
574 status = "on (forced)";
575 color = SK_ColorGREEN;
576 break;
577 case GpuRasterizationStatus::OFF_DEVICE:
578 status = "off (device)";
579 color = SK_ColorRED;
580 break;
581 case GpuRasterizationStatus::OFF_VIEWPORT:
582 status = "off (viewport)";
583 color = SK_ColorYELLOW;
584 break;
585 case GpuRasterizationStatus::MSAA_CONTENT:
586 status = "MSAA (content)";
587 color = SK_ColorCYAN;
588 break;
589 case GpuRasterizationStatus::OFF_CONTENT:
590 status = "off (content)";
591 color = SK_ColorYELLOW;
592 break;
595 if (status.empty())
596 return SkRect::MakeEmpty();
598 const int kPadding = 4;
599 const int kFontHeight = 13;
601 const int height = 2 * kFontHeight + 3 * kPadding;
602 const int left = bounds().width() - width - right;
603 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
605 SkPaint paint = CreatePaint();
606 DrawGraphBackground(canvas, &paint, area);
608 SkPoint gpu_status_pos = SkPoint::Make(left + width - kPadding,
609 top + 2 * kFontHeight + 2 * kPadding);
611 paint.setColor(color);
612 DrawText(canvas, &paint, "GPU raster: ", SkPaint::kLeft_Align, kFontHeight,
613 left + kPadding, top + kFontHeight + kPadding);
614 DrawText(canvas, &paint, status, SkPaint::kRight_Align, kFontHeight,
615 gpu_status_pos);
617 return area;
620 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
621 SkCanvas* canvas,
622 const PaintTimeCounter* paint_time_counter,
623 int right,
624 int top) const {
625 const int kPadding = 4;
626 const int kFontHeight = 14;
628 const int kGraphWidth = paint_time_counter->HistorySize();
629 const int kGraphHeight = 40;
631 SkPaint paint = CreatePaint();
633 const std::string title = "Compositor frame time (ms)";
634 int title_text_width = MeasureText(&paint, title, kFontHeight);
635 int contents_width = std::max(title_text_width, kGraphWidth);
637 const int width = contents_width + 2 * kPadding;
638 const int height =
639 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
640 const int left = bounds().width() - width - right;
642 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
644 DrawGraphBackground(canvas, &paint, area);
646 SkRect text_bounds = SkRect::MakeXYWH(left + kPadding, top + kPadding,
647 contents_width, kFontHeight);
648 SkRect text_bounds2 =
649 SkRect::MakeXYWH(left + kPadding, text_bounds.bottom() + kPadding,
650 contents_width, kFontHeight);
651 SkRect graph_bounds = SkRect::MakeXYWH(left + (width - kGraphWidth) / 2,
652 text_bounds2.bottom() + 2 * kPadding,
653 kGraphWidth, kGraphHeight);
655 const std::string value_text =
656 base::StringPrintf("%.1f", paint_time_graph_.value);
657 const std::string min_max_text = base::StringPrintf(
658 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
660 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
661 DrawText(canvas, &paint, title, SkPaint::kLeft_Align, kFontHeight,
662 text_bounds.left(), text_bounds.bottom());
663 DrawText(canvas,
664 &paint,
665 value_text,
666 SkPaint::kLeft_Align,
667 kFontHeight,
668 text_bounds2.left(),
669 text_bounds2.bottom());
670 DrawText(canvas,
671 &paint,
672 min_max_text,
673 SkPaint::kRight_Align,
674 kFontHeight,
675 text_bounds2.right(),
676 text_bounds2.bottom());
678 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
679 for (PaintTimeCounter::RingBufferType::Iterator it =
680 paint_time_counter->End();
682 --it) {
683 double pt = it->InMillisecondsF();
685 if (pt == 0.0)
686 continue;
688 double p = pt / paint_time_graph_.current_upper_bound;
689 if (p > 1.0)
690 p = 1.0;
692 canvas->drawRect(
693 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
694 graph_bounds.bottom() - p * graph_bounds.height(),
696 p * graph_bounds.height()),
697 paint);
700 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
702 return area;
705 void HeadsUpDisplayLayerImpl::DrawDebugRect(
706 SkCanvas* canvas,
707 SkPaint* paint,
708 const DebugRect& rect,
709 SkColor stroke_color,
710 SkColor fill_color,
711 float stroke_width,
712 const std::string& label_text) const {
713 gfx::Rect debug_layer_rect =
714 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
715 1.0 / internal_contents_scale_);
716 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
717 paint->setColor(fill_color);
718 paint->setStyle(SkPaint::kFill_Style);
719 canvas->drawIRect(sk_rect, *paint);
721 paint->setColor(stroke_color);
722 paint->setStyle(SkPaint::kStroke_Style);
723 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
724 canvas->drawIRect(sk_rect, *paint);
726 if (label_text.length()) {
727 const int kFontHeight = 12;
728 const int kPadding = 3;
730 // The debug_layer_rect may be huge, and converting to a floating point may
731 // be lossy, so intersect with the HUD layer bounds first to prevent that.
732 gfx::Rect clip_rect = debug_layer_rect;
733 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
734 SkRect sk_clip_rect = RectToSkRect(clip_rect);
736 canvas->save();
737 canvas->clipRect(sk_clip_rect);
738 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
740 SkPaint label_paint = CreatePaint();
741 label_paint.setTextSize(kFontHeight);
742 label_paint.setTypeface(typeface_.get());
743 label_paint.setColor(stroke_color);
745 const SkScalar label_text_width =
746 label_paint.measureText(label_text.c_str(), label_text.length());
747 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
748 kFontHeight + 2 * kPadding),
749 label_paint);
751 label_paint.setAntiAlias(true);
752 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
753 canvas->drawText(label_text.c_str(),
754 label_text.length(),
755 kPadding,
756 kFontHeight * 0.8f + kPadding,
757 label_paint);
759 canvas->restore();
763 void HeadsUpDisplayLayerImpl::DrawDebugRects(
764 SkCanvas* canvas,
765 DebugRectHistory* debug_rect_history) {
766 SkPaint paint = CreatePaint();
768 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
769 std::vector<DebugRect> new_paint_rects;
771 for (size_t i = 0; i < debug_rects.size(); ++i) {
772 SkColor stroke_color = 0;
773 SkColor fill_color = 0;
774 float stroke_width = 0.f;
775 std::string label_text;
777 switch (debug_rects[i].type) {
778 case PAINT_RECT_TYPE:
779 new_paint_rects.push_back(debug_rects[i]);
780 continue;
781 case PROPERTY_CHANGED_RECT_TYPE:
782 stroke_color = DebugColors::PropertyChangedRectBorderColor();
783 fill_color = DebugColors::PropertyChangedRectFillColor();
784 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
785 break;
786 case SURFACE_DAMAGE_RECT_TYPE:
787 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
788 fill_color = DebugColors::SurfaceDamageRectFillColor();
789 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
790 break;
791 case REPLICA_SCREEN_SPACE_RECT_TYPE:
792 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
793 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
794 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
795 break;
796 case SCREEN_SPACE_RECT_TYPE:
797 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
798 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
799 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
800 break;
801 case TOUCH_EVENT_HANDLER_RECT_TYPE:
802 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
803 fill_color = DebugColors::TouchEventHandlerRectFillColor();
804 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
805 label_text = "touch event listener";
806 break;
807 case WHEEL_EVENT_HANDLER_RECT_TYPE:
808 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
809 fill_color = DebugColors::WheelEventHandlerRectFillColor();
810 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
811 label_text = "mousewheel event listener";
812 break;
813 case SCROLL_EVENT_HANDLER_RECT_TYPE:
814 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
815 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
816 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
817 label_text = "scroll event listener";
818 break;
819 case NON_FAST_SCROLLABLE_RECT_TYPE:
820 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
821 fill_color = DebugColors::NonFastScrollableRectFillColor();
822 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
823 label_text = "repaints on scroll";
824 break;
825 case ANIMATION_BOUNDS_RECT_TYPE:
826 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
827 fill_color = DebugColors::LayerAnimationBoundsFillColor();
828 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
829 label_text = "animation bounds";
830 break;
833 DrawDebugRect(canvas,
834 &paint,
835 debug_rects[i],
836 stroke_color,
837 fill_color,
838 stroke_width,
839 label_text);
842 if (new_paint_rects.size()) {
843 paint_rects_.swap(new_paint_rects);
844 fade_step_ = DebugColors::kFadeSteps;
846 if (fade_step_ > 0) {
847 fade_step_--;
848 for (size_t i = 0; i < paint_rects_.size(); ++i) {
849 DrawDebugRect(canvas,
850 &paint,
851 paint_rects_[i],
852 DebugColors::PaintRectBorderColor(fade_step_),
853 DebugColors::PaintRectFillColor(fade_step_),
854 DebugColors::PaintRectBorderWidth(),
855 "");
860 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
861 return "cc::HeadsUpDisplayLayerImpl";
864 void HeadsUpDisplayLayerImpl::AsValueInto(
865 base::trace_event::TracedValue* dict) const {
866 LayerImpl::AsValueInto(dict);
867 dict->SetString("layer_name", "Heads Up Display Layer");
870 } // namespace cc