Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blobad3522e3e62036927e6b02e4ac1ff72f72191b4a
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/numerics/safe_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "cc/debug/debug_colors.h"
15 #include "cc/debug/frame_rate_counter.h"
16 #include "cc/debug/paint_time_counter.h"
17 #include "cc/output/begin_frame_args.h"
18 #include "cc/output/renderer.h"
19 #include "cc/quads/texture_draw_quad.h"
20 #include "cc/resources/memory_history.h"
21 #include "cc/trees/layer_tree_host_impl.h"
22 #include "cc/trees/layer_tree_impl.h"
23 #include "skia/ext/platform_canvas.h"
24 #include "third_party/skia/include/core/SkPaint.h"
25 #include "third_party/skia/include/core/SkPath.h"
26 #include "third_party/skia/include/core/SkTypeface.h"
27 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
28 #include "ui/gfx/geometry/point.h"
29 #include "ui/gfx/geometry/size.h"
30 #include "ui/gfx/geometry/size_conversions.h"
31 #include "ui/gfx/hud_font.h"
33 namespace cc {
35 static inline SkPaint CreatePaint() {
36 SkPaint paint;
37 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
38 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
39 // swizzle our colors when drawing to the SkCanvas.
40 SkColorMatrix swizzle_matrix;
41 for (int i = 0; i < 20; ++i)
42 swizzle_matrix.fMat[i] = 0;
43 swizzle_matrix.fMat[0 + 5 * 2] = 1;
44 swizzle_matrix.fMat[1 + 5 * 1] = 1;
45 swizzle_matrix.fMat[2 + 5 * 0] = 1;
46 swizzle_matrix.fMat[3 + 5 * 3] = 1;
48 skia::RefPtr<SkColorMatrixFilter> filter =
49 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
50 paint.setColorFilter(filter.get());
51 #endif
52 return paint;
55 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
56 double start_upper_bound)
57 : value(0.0),
58 min(0.0),
59 max(0.0),
60 current_upper_bound(start_upper_bound),
61 default_upper_bound(start_upper_bound),
62 indicator(indicator_value) {}
64 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
65 double target_upper_bound = std::max(max, default_upper_bound);
66 current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
67 return current_upper_bound;
70 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
71 int id)
72 : LayerImpl(tree_impl, id),
73 typeface_(gfx::GetHudTypeface()),
74 internal_contents_scale_(1.f),
75 fps_graph_(60.0, 80.0),
76 paint_time_graph_(16.0, 48.0),
77 fade_step_(0) {
78 if (!typeface_) {
79 typeface_ = skia::AdoptRef(
80 SkTypeface::CreateFromName("monospace", SkTypeface::kBold));
84 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
86 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
87 LayerTreeImpl* tree_impl) {
88 return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
91 void HeadsUpDisplayLayerImpl::AcquireResource(
92 ResourceProvider* resource_provider) {
93 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
94 it != resources_.end();
95 ++it) {
96 if (!resource_provider->InUseByConsumer((*it)->id())) {
97 resources_.swap(it, resources_.end() - 1);
98 return;
102 scoped_ptr<ScopedResource> resource =
103 ScopedResource::Create(resource_provider);
104 resource->Allocate(internal_content_bounds_,
105 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
106 resources_.push_back(resource.Pass());
109 class ResourceSizeIsEqualTo {
110 public:
111 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
112 : compare_size_(size_) {}
114 bool operator()(const ScopedResource* resource) {
115 return resource->size() == compare_size_;
118 private:
119 const gfx::Size compare_size_;
122 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
123 ResourceProvider* resource_provider) {
124 ScopedPtrVector<ScopedResource>::iterator it_erase =
125 resources_.partition(ResourceSizeIsEqualTo(internal_content_bounds_));
126 resources_.erase(it_erase, resources_.end());
129 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
130 ResourceProvider* resource_provider) {
131 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
132 return false;
134 internal_contents_scale_ = GetIdealContentsScale();
135 internal_content_bounds_ =
136 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_));
138 ReleaseUnmatchedSizeResources(resource_provider);
139 AcquireResource(resource_provider);
140 return LayerImpl::WillDraw(draw_mode, resource_provider);
143 void HeadsUpDisplayLayerImpl::AppendQuads(
144 RenderPass* render_pass,
145 AppendQuadsData* append_quads_data) {
146 if (!resources_.back()->id())
147 return;
149 SharedQuadState* shared_quad_state =
150 render_pass->CreateAndAppendSharedQuadState();
151 PopulateScaledSharedQuadState(shared_quad_state, internal_contents_scale_);
153 gfx::Rect quad_rect(internal_content_bounds_);
154 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
155 gfx::Rect visible_quad_rect(quad_rect);
156 bool premultiplied_alpha = true;
157 gfx::PointF uv_top_left(0.f, 0.f);
158 gfx::PointF uv_bottom_right(1.f, 1.f);
159 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
160 bool flipped = false;
161 bool nearest_neighbor = false;
162 TextureDrawQuad* quad =
163 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
164 quad->SetNew(shared_quad_state,
165 quad_rect,
166 opaque_rect,
167 visible_quad_rect,
168 resources_.back()->id(),
169 premultiplied_alpha,
170 uv_top_left,
171 uv_bottom_right,
172 SK_ColorTRANSPARENT,
173 vertex_opacity,
174 flipped,
175 nearest_neighbor);
176 ValidateQuadResources(quad);
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 DCHECK_GT(internal_contents_scale_, 0.f);
231 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_);
234 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
235 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
237 // Don't update numbers every frame so text is readable.
238 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
239 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
240 time_of_last_graph_update_ = now;
242 if (debug_state.show_fps_counter) {
243 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
244 fps_graph_.value = fps_counter->GetAverageFPS();
245 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
248 if (debug_state.continuous_painting) {
249 PaintTimeCounter* paint_time_counter =
250 layer_tree_impl()->paint_time_counter();
251 base::TimeDelta latest, min, max;
253 if (paint_time_counter->End())
254 latest = **paint_time_counter->End();
255 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
257 paint_time_graph_.value = latest.InMillisecondsF();
258 paint_time_graph_.min = min.InMillisecondsF();
259 paint_time_graph_.max = max.InMillisecondsF();
262 if (debug_state.ShowMemoryStats()) {
263 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
264 if (memory_history->End())
265 memory_entry_ = **memory_history->End();
266 else
267 memory_entry_ = MemoryHistory::Entry();
271 fps_graph_.UpdateUpperBound();
272 paint_time_graph_.UpdateUpperBound();
275 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
276 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
278 if (debug_state.ShowHudRects()) {
279 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
280 if (IsAnimatingHUDContents()) {
281 layer_tree_impl()->SetNeedsRedraw();
285 SkRect area = SkRect::MakeEmpty();
286 if (debug_state.continuous_painting) {
287 area = DrawPaintTimeDisplay(
288 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
289 } else if (debug_state.show_fps_counter) {
290 // Don't show the FPS display when continuous painting is enabled, because
291 // it would show misleading numbers.
292 area =
293 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
296 if (debug_state.show_fps_counter || debug_state.continuous_painting) {
297 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
298 SkMaxScalar(area.width(), 150));
301 if (debug_state.ShowMemoryStats())
302 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
304 int HeadsUpDisplayLayerImpl::MeasureText(SkPaint* paint,
305 const std::string& text,
306 int size) const {
307 const bool anti_alias = paint->isAntiAlias();
308 paint->setAntiAlias(true);
309 paint->setTextSize(size);
310 paint->setTypeface(typeface_.get());
311 SkScalar text_width = paint->measureText(text.c_str(), text.length());
313 paint->setAntiAlias(anti_alias);
314 return SkScalarCeilToInt(text_width);
316 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
317 SkPaint* paint,
318 const std::string& text,
319 SkPaint::Align align,
320 int size,
321 int x,
322 int y) const {
323 const bool anti_alias = paint->isAntiAlias();
324 paint->setAntiAlias(true);
326 paint->setTextSize(size);
327 paint->setTextAlign(align);
328 paint->setTypeface(typeface_.get());
329 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
331 paint->setAntiAlias(anti_alias);
334 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
335 SkPaint* paint,
336 const std::string& text,
337 SkPaint::Align align,
338 int size,
339 const SkPoint& pos) const {
340 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
343 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
344 SkPaint* paint,
345 const SkRect& bounds) const {
346 paint->setColor(DebugColors::HUDBackgroundColor());
347 canvas->drawRect(bounds, *paint);
350 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
351 SkPaint* paint,
352 const SkRect& bounds,
353 const Graph& graph) const {
354 // Draw top and bottom line.
355 paint->setColor(DebugColors::HUDSeparatorLineColor());
356 canvas->drawLine(bounds.left(),
357 bounds.top() - 1,
358 bounds.right(),
359 bounds.top() - 1,
360 *paint);
361 canvas->drawLine(
362 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
364 // Draw indicator line (additive blend mode to increase contrast when drawn on
365 // top of graph).
366 paint->setColor(DebugColors::HUDIndicatorLineColor());
367 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
368 const double indicator_top =
369 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
370 1.0;
371 canvas->drawLine(bounds.left(),
372 bounds.top() + indicator_top,
373 bounds.right(),
374 bounds.top() + indicator_top,
375 *paint);
376 paint->setXfermode(nullptr);
379 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
380 SkCanvas* canvas,
381 const FrameRateCounter* fps_counter,
382 int right,
383 int top) const {
384 const int kPadding = 4;
385 const int kGap = 6;
387 const int kFontHeight = 15;
389 const int kGraphWidth =
390 base::saturated_cast<int>(fps_counter->time_stamp_history_size()) - 2;
391 const int kGraphHeight = 40;
393 const int kHistogramWidth = 37;
395 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
396 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
397 int left = bounds().width() - width - right;
398 SkRect area = SkRect::MakeXYWH(left, top, width, height);
400 SkPaint paint = CreatePaint();
401 DrawGraphBackground(canvas, &paint, area);
403 SkRect text_bounds =
404 SkRect::MakeXYWH(left + kPadding,
405 top + kPadding,
406 kGraphWidth + kHistogramWidth + kGap + 2,
407 kFontHeight);
408 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
409 text_bounds.bottom() + 2 * kPadding,
410 kGraphWidth,
411 kGraphHeight);
412 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
413 graph_bounds.top(),
414 kHistogramWidth,
415 kGraphHeight);
417 const std::string value_text =
418 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
419 const std::string min_max_text =
420 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
422 VLOG(1) << value_text;
424 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
425 DrawText(canvas,
426 &paint,
427 value_text,
428 SkPaint::kLeft_Align,
429 kFontHeight,
430 text_bounds.left(),
431 text_bounds.bottom());
432 DrawText(canvas,
433 &paint,
434 min_max_text,
435 SkPaint::kRight_Align,
436 kFontHeight,
437 text_bounds.right(),
438 text_bounds.bottom());
440 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
442 // Collect graph and histogram data.
443 SkPath path;
445 const int kHistogramSize = 20;
446 double histogram[kHistogramSize] = { 1.0 };
447 double max_bucket_value = 1.0;
449 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
450 --it) {
451 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
453 // Skip this particular instantaneous frame rate if it is not likely to have
454 // been valid.
455 if (!fps_counter->IsBadFrameInterval(delta)) {
456 double fps = 1.0 / delta.InSecondsF();
458 // Clamp the FPS to the range we want to plot visually.
459 double p = fps / fps_graph_.current_upper_bound;
460 if (p > 1.0)
461 p = 1.0;
463 // Plot this data point.
464 SkPoint cur =
465 SkPoint::Make(graph_bounds.left() + it.index(),
466 graph_bounds.bottom() - p * graph_bounds.height());
467 if (path.isEmpty())
468 path.moveTo(cur);
469 else
470 path.lineTo(cur);
472 // Use the fps value to find the right bucket in the histogram.
473 int bucket_index = floor(p * (kHistogramSize - 1));
475 // Add the delta time to take the time spent at that fps rate into
476 // account.
477 histogram[bucket_index] += delta.InSecondsF();
478 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
482 // Draw FPS histogram.
483 paint.setColor(DebugColors::HUDSeparatorLineColor());
484 canvas->drawLine(histogram_bounds.left() - 1,
485 histogram_bounds.top() - 1,
486 histogram_bounds.left() - 1,
487 histogram_bounds.bottom() + 1,
488 paint);
489 canvas->drawLine(histogram_bounds.right() + 1,
490 histogram_bounds.top() - 1,
491 histogram_bounds.right() + 1,
492 histogram_bounds.bottom() + 1,
493 paint);
495 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
496 const double bar_height = histogram_bounds.height() / kHistogramSize;
498 for (int i = kHistogramSize - 1; i >= 0; --i) {
499 if (histogram[i] > 0) {
500 double bar_width =
501 histogram[i] / max_bucket_value * histogram_bounds.width();
502 canvas->drawRect(
503 SkRect::MakeXYWH(histogram_bounds.left(),
504 histogram_bounds.bottom() - (i + 1) * bar_height,
505 bar_width,
507 paint);
511 // Draw FPS graph.
512 paint.setAntiAlias(true);
513 paint.setStyle(SkPaint::kStroke_Style);
514 paint.setStrokeWidth(1);
515 canvas->drawPath(path, paint);
517 return area;
520 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
521 int right,
522 int top,
523 int width) const {
524 if (!memory_entry_.total_bytes_used)
525 return SkRect::MakeEmpty();
527 const int kPadding = 4;
528 const int kFontHeight = 13;
530 const int height = 3 * kFontHeight + 4 * kPadding;
531 const int left = bounds().width() - width - right;
532 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
534 const double kMegabyte = 1024.0 * 1024.0;
536 SkPaint paint = CreatePaint();
537 DrawGraphBackground(canvas, &paint, area);
539 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
540 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
541 top + kPadding + 2 * kFontHeight);
542 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
543 top + 2 * kPadding + 3 * kFontHeight);
545 paint.setColor(DebugColors::MemoryDisplayTextColor());
546 DrawText(canvas,
547 &paint,
548 "GPU memory",
549 SkPaint::kLeft_Align,
550 kFontHeight,
551 title_pos);
553 std::string text = base::StringPrintf(
554 "%6.1f MB used", memory_entry_.total_bytes_used / kMegabyte);
555 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
557 if (!memory_entry_.had_enough_memory)
558 paint.setColor(SK_ColorRED);
559 text = base::StringPrintf("%6.1f MB max ",
560 memory_entry_.total_budget_in_bytes / kMegabyte);
561 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
563 return area;
566 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
567 int right,
568 int top,
569 int width) const {
570 std::string status;
571 SkColor color = SK_ColorRED;
572 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
573 case GpuRasterizationStatus::ON:
574 status = "on";
575 color = SK_ColorGREEN;
576 break;
577 case GpuRasterizationStatus::ON_FORCED:
578 status = "on (forced)";
579 color = SK_ColorGREEN;
580 break;
581 case GpuRasterizationStatus::OFF_DEVICE:
582 status = "off (device)";
583 color = SK_ColorRED;
584 break;
585 case GpuRasterizationStatus::OFF_VIEWPORT:
586 status = "off (viewport)";
587 color = SK_ColorYELLOW;
588 break;
589 case GpuRasterizationStatus::MSAA_CONTENT:
590 status = "MSAA (content)";
591 color = SK_ColorCYAN;
592 break;
593 case GpuRasterizationStatus::OFF_CONTENT:
594 status = "off (content)";
595 color = SK_ColorYELLOW;
596 break;
599 if (status.empty())
600 return SkRect::MakeEmpty();
602 const int kPadding = 4;
603 const int kFontHeight = 13;
605 const int height = 2 * kFontHeight + 3 * kPadding;
606 const int left = bounds().width() - width - right;
607 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
609 SkPaint paint = CreatePaint();
610 DrawGraphBackground(canvas, &paint, area);
612 SkPoint gpu_status_pos = SkPoint::Make(left + width - kPadding,
613 top + 2 * kFontHeight + 2 * kPadding);
615 paint.setColor(color);
616 DrawText(canvas, &paint, "GPU raster: ", SkPaint::kLeft_Align, kFontHeight,
617 left + kPadding, top + kFontHeight + kPadding);
618 DrawText(canvas, &paint, status, SkPaint::kRight_Align, kFontHeight,
619 gpu_status_pos);
621 return area;
624 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
625 SkCanvas* canvas,
626 const PaintTimeCounter* paint_time_counter,
627 int right,
628 int top) const {
629 const int kPadding = 4;
630 const int kFontHeight = 14;
632 const int kGraphWidth =
633 base::saturated_cast<int>(paint_time_counter->HistorySize());
634 const int kGraphHeight = 40;
636 SkPaint paint = CreatePaint();
638 const std::string title = "Compositor frame time (ms)";
639 int title_text_width = MeasureText(&paint, title, kFontHeight);
640 int contents_width = std::max(title_text_width, kGraphWidth);
642 const int width = contents_width + 2 * kPadding;
643 const int height =
644 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
645 const int left = bounds().width() - width - right;
647 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
649 DrawGraphBackground(canvas, &paint, area);
651 SkRect text_bounds = SkRect::MakeXYWH(left + kPadding, top + kPadding,
652 contents_width, kFontHeight);
653 SkRect text_bounds2 =
654 SkRect::MakeXYWH(left + kPadding, text_bounds.bottom() + kPadding,
655 contents_width, kFontHeight);
656 SkRect graph_bounds = SkRect::MakeXYWH(left + (width - kGraphWidth) / 2,
657 text_bounds2.bottom() + 2 * kPadding,
658 kGraphWidth, kGraphHeight);
660 const std::string value_text =
661 base::StringPrintf("%.1f", paint_time_graph_.value);
662 const std::string min_max_text = base::StringPrintf(
663 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
665 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
666 DrawText(canvas, &paint, title, SkPaint::kLeft_Align, kFontHeight,
667 text_bounds.left(), text_bounds.bottom());
668 DrawText(canvas,
669 &paint,
670 value_text,
671 SkPaint::kLeft_Align,
672 kFontHeight,
673 text_bounds2.left(),
674 text_bounds2.bottom());
675 DrawText(canvas,
676 &paint,
677 min_max_text,
678 SkPaint::kRight_Align,
679 kFontHeight,
680 text_bounds2.right(),
681 text_bounds2.bottom());
683 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
684 for (PaintTimeCounter::RingBufferType::Iterator it =
685 paint_time_counter->End();
687 --it) {
688 double pt = it->InMillisecondsF();
690 if (pt == 0.0)
691 continue;
693 double p = pt / paint_time_graph_.current_upper_bound;
694 if (p > 1.0)
695 p = 1.0;
697 canvas->drawRect(
698 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
699 graph_bounds.bottom() - p * graph_bounds.height(),
701 p * graph_bounds.height()),
702 paint);
705 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
707 return area;
710 void HeadsUpDisplayLayerImpl::DrawDebugRect(
711 SkCanvas* canvas,
712 SkPaint* paint,
713 const DebugRect& rect,
714 SkColor stroke_color,
715 SkColor fill_color,
716 float stroke_width,
717 const std::string& label_text) const {
718 gfx::Rect debug_layer_rect =
719 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
720 1.0 / internal_contents_scale_);
721 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
722 paint->setColor(fill_color);
723 paint->setStyle(SkPaint::kFill_Style);
724 canvas->drawIRect(sk_rect, *paint);
726 paint->setColor(stroke_color);
727 paint->setStyle(SkPaint::kStroke_Style);
728 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
729 canvas->drawIRect(sk_rect, *paint);
731 if (label_text.length()) {
732 const int kFontHeight = 12;
733 const int kPadding = 3;
735 // The debug_layer_rect may be huge, and converting to a floating point may
736 // be lossy, so intersect with the HUD layer bounds first to prevent that.
737 gfx::Rect clip_rect = debug_layer_rect;
738 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
739 SkRect sk_clip_rect = RectToSkRect(clip_rect);
741 canvas->save();
742 canvas->clipRect(sk_clip_rect);
743 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
745 SkPaint label_paint = CreatePaint();
746 label_paint.setTextSize(kFontHeight);
747 label_paint.setTypeface(typeface_.get());
748 label_paint.setColor(stroke_color);
750 const SkScalar label_text_width =
751 label_paint.measureText(label_text.c_str(), label_text.length());
752 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
753 kFontHeight + 2 * kPadding),
754 label_paint);
756 label_paint.setAntiAlias(true);
757 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
758 canvas->drawText(label_text.c_str(),
759 label_text.length(),
760 kPadding,
761 kFontHeight * 0.8f + kPadding,
762 label_paint);
764 canvas->restore();
768 void HeadsUpDisplayLayerImpl::DrawDebugRects(
769 SkCanvas* canvas,
770 DebugRectHistory* debug_rect_history) {
771 SkPaint paint = CreatePaint();
773 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
774 std::vector<DebugRect> new_paint_rects;
776 for (size_t i = 0; i < debug_rects.size(); ++i) {
777 SkColor stroke_color = 0;
778 SkColor fill_color = 0;
779 float stroke_width = 0.f;
780 std::string label_text;
782 switch (debug_rects[i].type) {
783 case PAINT_RECT_TYPE:
784 new_paint_rects.push_back(debug_rects[i]);
785 continue;
786 case PROPERTY_CHANGED_RECT_TYPE:
787 stroke_color = DebugColors::PropertyChangedRectBorderColor();
788 fill_color = DebugColors::PropertyChangedRectFillColor();
789 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
790 break;
791 case SURFACE_DAMAGE_RECT_TYPE:
792 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
793 fill_color = DebugColors::SurfaceDamageRectFillColor();
794 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
795 break;
796 case REPLICA_SCREEN_SPACE_RECT_TYPE:
797 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
798 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
799 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
800 break;
801 case SCREEN_SPACE_RECT_TYPE:
802 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
803 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
804 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
805 break;
806 case TOUCH_EVENT_HANDLER_RECT_TYPE:
807 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
808 fill_color = DebugColors::TouchEventHandlerRectFillColor();
809 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
810 label_text = "touch event listener";
811 break;
812 case WHEEL_EVENT_HANDLER_RECT_TYPE:
813 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
814 fill_color = DebugColors::WheelEventHandlerRectFillColor();
815 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
816 label_text = "mousewheel event listener";
817 break;
818 case SCROLL_EVENT_HANDLER_RECT_TYPE:
819 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
820 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
821 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
822 label_text = "scroll event listener";
823 break;
824 case NON_FAST_SCROLLABLE_RECT_TYPE:
825 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
826 fill_color = DebugColors::NonFastScrollableRectFillColor();
827 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
828 label_text = "repaints on scroll";
829 break;
830 case ANIMATION_BOUNDS_RECT_TYPE:
831 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
832 fill_color = DebugColors::LayerAnimationBoundsFillColor();
833 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
834 label_text = "animation bounds";
835 break;
838 DrawDebugRect(canvas,
839 &paint,
840 debug_rects[i],
841 stroke_color,
842 fill_color,
843 stroke_width,
844 label_text);
847 if (new_paint_rects.size()) {
848 paint_rects_.swap(new_paint_rects);
849 fade_step_ = DebugColors::kFadeSteps;
851 if (fade_step_ > 0) {
852 fade_step_--;
853 for (size_t i = 0; i < paint_rects_.size(); ++i) {
854 DrawDebugRect(canvas,
855 &paint,
856 paint_rects_[i],
857 DebugColors::PaintRectBorderColor(fade_step_),
858 DebugColors::PaintRectFillColor(fade_step_),
859 DebugColors::PaintRectBorderWidth(),
860 "");
865 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
866 return "cc::HeadsUpDisplayLayerImpl";
869 void HeadsUpDisplayLayerImpl::AsValueInto(
870 base::trace_event::TracedValue* dict) const {
871 LayerImpl::AsValueInto(dict);
872 dict->SetString("layer_name", "Heads Up Display Layer");
875 } // namespace cc