Fix CandidateSessionConfig copy constructor to copy all fields.
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob7d449b7f8dd8d3405b0f4063791fda4cdf1c065a
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::OFF_CONTENT:
586 status = "off (content)";
587 color = SK_ColorYELLOW;
588 break;
591 if (status.empty())
592 return SkRect::MakeEmpty();
594 const int kPadding = 4;
595 const int kFontHeight = 13;
597 const int height = 2 * kFontHeight + 3 * kPadding;
598 const int left = bounds().width() - width - right;
599 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
601 SkPaint paint = CreatePaint();
602 DrawGraphBackground(canvas, &paint, area);
604 SkPoint gpu_status_pos = SkPoint::Make(left + width - kPadding,
605 top + 2 * kFontHeight + 2 * kPadding);
607 paint.setColor(color);
608 DrawText(canvas, &paint, "GPU raster: ", SkPaint::kLeft_Align, kFontHeight,
609 left + kPadding, top + kFontHeight + kPadding);
610 DrawText(canvas, &paint, status, SkPaint::kRight_Align, kFontHeight,
611 gpu_status_pos);
613 return area;
616 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
617 SkCanvas* canvas,
618 const PaintTimeCounter* paint_time_counter,
619 int right,
620 int top) const {
621 const int kPadding = 4;
622 const int kFontHeight = 14;
624 const int kGraphWidth = paint_time_counter->HistorySize();
625 const int kGraphHeight = 40;
627 SkPaint paint = CreatePaint();
629 const std::string title = "Compositor frame time (ms)";
630 int title_text_width = MeasureText(&paint, title, kFontHeight);
631 int contents_width = std::max(title_text_width, kGraphWidth);
633 const int width = contents_width + 2 * kPadding;
634 const int height =
635 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
636 const int left = bounds().width() - width - right;
638 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
640 DrawGraphBackground(canvas, &paint, area);
642 SkRect text_bounds = SkRect::MakeXYWH(left + kPadding, top + kPadding,
643 contents_width, kFontHeight);
644 SkRect text_bounds2 =
645 SkRect::MakeXYWH(left + kPadding, text_bounds.bottom() + kPadding,
646 contents_width, kFontHeight);
647 SkRect graph_bounds = SkRect::MakeXYWH(left + (width - kGraphWidth) / 2,
648 text_bounds2.bottom() + 2 * kPadding,
649 kGraphWidth, kGraphHeight);
651 const std::string value_text =
652 base::StringPrintf("%.1f", paint_time_graph_.value);
653 const std::string min_max_text = base::StringPrintf(
654 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
656 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
657 DrawText(canvas, &paint, title, SkPaint::kLeft_Align, kFontHeight,
658 text_bounds.left(), text_bounds.bottom());
659 DrawText(canvas,
660 &paint,
661 value_text,
662 SkPaint::kLeft_Align,
663 kFontHeight,
664 text_bounds2.left(),
665 text_bounds2.bottom());
666 DrawText(canvas,
667 &paint,
668 min_max_text,
669 SkPaint::kRight_Align,
670 kFontHeight,
671 text_bounds2.right(),
672 text_bounds2.bottom());
674 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
675 for (PaintTimeCounter::RingBufferType::Iterator it =
676 paint_time_counter->End();
678 --it) {
679 double pt = it->InMillisecondsF();
681 if (pt == 0.0)
682 continue;
684 double p = pt / paint_time_graph_.current_upper_bound;
685 if (p > 1.0)
686 p = 1.0;
688 canvas->drawRect(
689 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
690 graph_bounds.bottom() - p * graph_bounds.height(),
692 p * graph_bounds.height()),
693 paint);
696 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
698 return area;
701 void HeadsUpDisplayLayerImpl::DrawDebugRect(
702 SkCanvas* canvas,
703 SkPaint* paint,
704 const DebugRect& rect,
705 SkColor stroke_color,
706 SkColor fill_color,
707 float stroke_width,
708 const std::string& label_text) const {
709 gfx::Rect debug_layer_rect =
710 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
711 1.0 / internal_contents_scale_);
712 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
713 paint->setColor(fill_color);
714 paint->setStyle(SkPaint::kFill_Style);
715 canvas->drawIRect(sk_rect, *paint);
717 paint->setColor(stroke_color);
718 paint->setStyle(SkPaint::kStroke_Style);
719 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
720 canvas->drawIRect(sk_rect, *paint);
722 if (label_text.length()) {
723 const int kFontHeight = 12;
724 const int kPadding = 3;
726 // The debug_layer_rect may be huge, and converting to a floating point may
727 // be lossy, so intersect with the HUD layer bounds first to prevent that.
728 gfx::Rect clip_rect = debug_layer_rect;
729 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
730 SkRect sk_clip_rect = RectToSkRect(clip_rect);
732 canvas->save();
733 canvas->clipRect(sk_clip_rect);
734 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
736 SkPaint label_paint = CreatePaint();
737 label_paint.setTextSize(kFontHeight);
738 label_paint.setTypeface(typeface_.get());
739 label_paint.setColor(stroke_color);
741 const SkScalar label_text_width =
742 label_paint.measureText(label_text.c_str(), label_text.length());
743 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
744 kFontHeight + 2 * kPadding),
745 label_paint);
747 label_paint.setAntiAlias(true);
748 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
749 canvas->drawText(label_text.c_str(),
750 label_text.length(),
751 kPadding,
752 kFontHeight * 0.8f + kPadding,
753 label_paint);
755 canvas->restore();
759 void HeadsUpDisplayLayerImpl::DrawDebugRects(
760 SkCanvas* canvas,
761 DebugRectHistory* debug_rect_history) {
762 SkPaint paint = CreatePaint();
764 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
765 std::vector<DebugRect> new_paint_rects;
767 for (size_t i = 0; i < debug_rects.size(); ++i) {
768 SkColor stroke_color = 0;
769 SkColor fill_color = 0;
770 float stroke_width = 0.f;
771 std::string label_text;
773 switch (debug_rects[i].type) {
774 case PAINT_RECT_TYPE:
775 new_paint_rects.push_back(debug_rects[i]);
776 continue;
777 case PROPERTY_CHANGED_RECT_TYPE:
778 stroke_color = DebugColors::PropertyChangedRectBorderColor();
779 fill_color = DebugColors::PropertyChangedRectFillColor();
780 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
781 break;
782 case SURFACE_DAMAGE_RECT_TYPE:
783 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
784 fill_color = DebugColors::SurfaceDamageRectFillColor();
785 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
786 break;
787 case REPLICA_SCREEN_SPACE_RECT_TYPE:
788 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
789 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
790 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
791 break;
792 case SCREEN_SPACE_RECT_TYPE:
793 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
794 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
795 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
796 break;
797 case TOUCH_EVENT_HANDLER_RECT_TYPE:
798 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
799 fill_color = DebugColors::TouchEventHandlerRectFillColor();
800 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
801 label_text = "touch event listener";
802 break;
803 case WHEEL_EVENT_HANDLER_RECT_TYPE:
804 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
805 fill_color = DebugColors::WheelEventHandlerRectFillColor();
806 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
807 label_text = "mousewheel event listener";
808 break;
809 case SCROLL_EVENT_HANDLER_RECT_TYPE:
810 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
811 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
812 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
813 label_text = "scroll event listener";
814 break;
815 case NON_FAST_SCROLLABLE_RECT_TYPE:
816 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
817 fill_color = DebugColors::NonFastScrollableRectFillColor();
818 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
819 label_text = "repaints on scroll";
820 break;
821 case ANIMATION_BOUNDS_RECT_TYPE:
822 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
823 fill_color = DebugColors::LayerAnimationBoundsFillColor();
824 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
825 label_text = "animation bounds";
826 break;
829 DrawDebugRect(canvas,
830 &paint,
831 debug_rects[i],
832 stroke_color,
833 fill_color,
834 stroke_width,
835 label_text);
838 if (new_paint_rects.size()) {
839 paint_rects_.swap(new_paint_rects);
840 fade_step_ = DebugColors::kFadeSteps;
842 if (fade_step_ > 0) {
843 fade_step_--;
844 for (size_t i = 0; i < paint_rects_.size(); ++i) {
845 DrawDebugRect(canvas,
846 &paint,
847 paint_rects_[i],
848 DebugColors::PaintRectBorderColor(fade_step_),
849 DebugColors::PaintRectFillColor(fade_step_),
850 DebugColors::PaintRectBorderWidth(),
851 "");
856 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
857 return "cc::HeadsUpDisplayLayerImpl";
860 void HeadsUpDisplayLayerImpl::AsValueInto(
861 base::trace_event::TracedValue* dict) const {
862 LayerImpl::AsValueInto(dict);
863 dict->SetString("layer_name", "Heads Up Display Layer");
866 } // namespace cc