Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blobfad85a09ee9eee2a9b97711e28e7474be89966be
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/SkTypeface.h"
26 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/size.h"
29 #include "ui/gfx/geometry/size_conversions.h"
30 #include "ui/gfx/hud_font.h"
32 namespace cc {
34 static inline SkPaint CreatePaint() {
35 SkPaint paint;
36 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
37 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
38 // swizzle our colors when drawing to the SkCanvas.
39 SkColorMatrix swizzle_matrix;
40 for (int i = 0; i < 20; ++i)
41 swizzle_matrix.fMat[i] = 0;
42 swizzle_matrix.fMat[0 + 5 * 2] = 1;
43 swizzle_matrix.fMat[1 + 5 * 1] = 1;
44 swizzle_matrix.fMat[2 + 5 * 0] = 1;
45 swizzle_matrix.fMat[3 + 5 * 3] = 1;
47 skia::RefPtr<SkColorMatrixFilter> filter =
48 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
49 paint.setColorFilter(filter.get());
50 #endif
51 return paint;
54 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
55 double start_upper_bound)
56 : value(0.0),
57 min(0.0),
58 max(0.0),
59 current_upper_bound(start_upper_bound),
60 default_upper_bound(start_upper_bound),
61 indicator(indicator_value) {}
63 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
64 double target_upper_bound = std::max(max, default_upper_bound);
65 current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
66 return current_upper_bound;
69 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
70 int id)
71 : LayerImpl(tree_impl, id),
72 typeface_(gfx::GetHudTypeface()),
73 internal_contents_scale_(1.f),
74 fps_graph_(60.0, 80.0),
75 paint_time_graph_(16.0, 48.0),
76 fade_step_(0) {
77 if (!typeface_) {
78 typeface_ = skia::AdoptRef(
79 SkTypeface::CreateFromName("monospace", SkTypeface::kBold));
83 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
85 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
86 LayerTreeImpl* tree_impl) {
87 return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
90 void HeadsUpDisplayLayerImpl::AcquireResource(
91 ResourceProvider* resource_provider) {
92 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
93 it != resources_.end();
94 ++it) {
95 if (!resource_provider->InUseByConsumer((*it)->id())) {
96 resources_.swap(it, resources_.end() - 1);
97 return;
101 scoped_ptr<ScopedResource> resource =
102 ScopedResource::Create(resource_provider);
103 resource->Allocate(internal_content_bounds_,
104 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
105 resources_.push_back(resource.Pass());
108 class ResourceSizeIsEqualTo {
109 public:
110 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
111 : compare_size_(size_) {}
113 bool operator()(const ScopedResource* resource) {
114 return resource->size() == compare_size_;
117 private:
118 const gfx::Size compare_size_;
121 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
122 ResourceProvider* resource_provider) {
123 ScopedPtrVector<ScopedResource>::iterator it_erase =
124 resources_.partition(ResourceSizeIsEqualTo(internal_content_bounds_));
125 resources_.erase(it_erase, resources_.end());
128 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
129 ResourceProvider* resource_provider) {
130 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
131 return false;
133 internal_contents_scale_ = draw_properties().ideal_contents_scale;
134 internal_content_bounds_ =
135 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_));
137 ReleaseUnmatchedSizeResources(resource_provider);
138 AcquireResource(resource_provider);
139 return LayerImpl::WillDraw(draw_mode, resource_provider);
142 void HeadsUpDisplayLayerImpl::AppendQuads(
143 RenderPass* render_pass,
144 AppendQuadsData* append_quads_data) {
145 if (!resources_.back()->id())
146 return;
148 SharedQuadState* shared_quad_state =
149 render_pass->CreateAndAppendSharedQuadState();
150 PopulateScaledSharedQuadState(shared_quad_state, internal_contents_scale_);
152 gfx::Rect quad_rect(internal_content_bounds_);
153 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
154 gfx::Rect visible_quad_rect(quad_rect);
155 bool premultiplied_alpha = true;
156 gfx::PointF uv_top_left(0.f, 0.f);
157 gfx::PointF uv_bottom_right(1.f, 1.f);
158 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
159 bool flipped = false;
160 bool nearest_neighbor = false;
161 TextureDrawQuad* quad =
162 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
163 quad->SetNew(shared_quad_state,
164 quad_rect,
165 opaque_rect,
166 visible_quad_rect,
167 resources_.back()->id(),
168 premultiplied_alpha,
169 uv_top_left,
170 uv_bottom_right,
171 SK_ColorTRANSPARENT,
172 vertex_opacity,
173 flipped,
174 nearest_neighbor);
175 ValidateQuadResources(quad);
178 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
179 DrawMode draw_mode,
180 ResourceProvider* resource_provider) {
181 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
182 return;
184 SkISize canvas_size;
185 if (hud_surface_)
186 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
187 else
188 canvas_size.set(0, 0);
190 if (canvas_size.width() != internal_content_bounds_.width() ||
191 canvas_size.height() != internal_content_bounds_.height() ||
192 !hud_surface_) {
193 TRACE_EVENT0("cc", "ResizeHudCanvas");
195 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
196 internal_content_bounds_.width(), internal_content_bounds_.height()));
199 UpdateHudContents();
202 TRACE_EVENT0("cc", "DrawHudContents");
203 hud_surface_->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
204 hud_surface_->getCanvas()->save();
205 hud_surface_->getCanvas()->scale(internal_contents_scale_,
206 internal_contents_scale_);
208 DrawHudContents(hud_surface_->getCanvas());
210 hud_surface_->getCanvas()->restore();
213 TRACE_EVENT0("cc", "UploadHudTexture");
214 SkImageInfo info;
215 size_t row_bytes = 0;
216 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
217 DCHECK(pixels);
218 DCHECK(info.colorType() == kN32_SkColorType);
219 resource_provider->CopyToResource(resources_.back()->id(),
220 static_cast<const uint8_t*>(pixels),
221 internal_content_bounds_);
224 void HeadsUpDisplayLayerImpl::ReleaseResources() {
225 resources_.clear();
228 gfx::Rect HeadsUpDisplayLayerImpl::GetEnclosingRectInTargetSpace() const {
229 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_);
232 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
233 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
235 // Don't update numbers every frame so text is readable.
236 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
237 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
238 time_of_last_graph_update_ = now;
240 if (debug_state.show_fps_counter) {
241 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
242 fps_graph_.value = fps_counter->GetAverageFPS();
243 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
246 if (debug_state.continuous_painting) {
247 PaintTimeCounter* paint_time_counter =
248 layer_tree_impl()->paint_time_counter();
249 base::TimeDelta latest, min, max;
251 if (paint_time_counter->End())
252 latest = **paint_time_counter->End();
253 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
255 paint_time_graph_.value = latest.InMillisecondsF();
256 paint_time_graph_.min = min.InMillisecondsF();
257 paint_time_graph_.max = max.InMillisecondsF();
260 if (debug_state.ShowMemoryStats()) {
261 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
262 if (memory_history->End())
263 memory_entry_ = **memory_history->End();
264 else
265 memory_entry_ = MemoryHistory::Entry();
269 fps_graph_.UpdateUpperBound();
270 paint_time_graph_.UpdateUpperBound();
273 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
274 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
276 if (debug_state.ShowHudRects()) {
277 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
278 if (IsAnimatingHUDContents()) {
279 layer_tree_impl()->SetNeedsRedraw();
283 SkRect area = SkRect::MakeEmpty();
284 if (debug_state.continuous_painting) {
285 area = DrawPaintTimeDisplay(
286 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
287 } else if (debug_state.show_fps_counter) {
288 // Don't show the FPS display when continuous painting is enabled, because
289 // it would show misleading numbers.
290 area =
291 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
294 if (debug_state.show_fps_counter || debug_state.continuous_painting) {
295 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
296 SkMaxScalar(area.width(), 150));
299 if (debug_state.ShowMemoryStats())
300 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
302 int HeadsUpDisplayLayerImpl::MeasureText(SkPaint* paint,
303 const std::string& text,
304 int size) const {
305 const bool anti_alias = paint->isAntiAlias();
306 paint->setAntiAlias(true);
307 paint->setTextSize(size);
308 paint->setTypeface(typeface_.get());
309 SkScalar text_width = paint->measureText(text.c_str(), text.length());
311 paint->setAntiAlias(anti_alias);
312 return SkScalarCeilToInt(text_width);
314 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
315 SkPaint* paint,
316 const std::string& text,
317 SkPaint::Align align,
318 int size,
319 int x,
320 int y) const {
321 const bool anti_alias = paint->isAntiAlias();
322 paint->setAntiAlias(true);
324 paint->setTextSize(size);
325 paint->setTextAlign(align);
326 paint->setTypeface(typeface_.get());
327 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
329 paint->setAntiAlias(anti_alias);
332 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
333 SkPaint* paint,
334 const std::string& text,
335 SkPaint::Align align,
336 int size,
337 const SkPoint& pos) const {
338 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
341 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
342 SkPaint* paint,
343 const SkRect& bounds) const {
344 paint->setColor(DebugColors::HUDBackgroundColor());
345 canvas->drawRect(bounds, *paint);
348 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
349 SkPaint* paint,
350 const SkRect& bounds,
351 const Graph& graph) const {
352 // Draw top and bottom line.
353 paint->setColor(DebugColors::HUDSeparatorLineColor());
354 canvas->drawLine(bounds.left(),
355 bounds.top() - 1,
356 bounds.right(),
357 bounds.top() - 1,
358 *paint);
359 canvas->drawLine(
360 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
362 // Draw indicator line (additive blend mode to increase contrast when drawn on
363 // top of graph).
364 paint->setColor(DebugColors::HUDIndicatorLineColor());
365 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
366 const double indicator_top =
367 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
368 1.0;
369 canvas->drawLine(bounds.left(),
370 bounds.top() + indicator_top,
371 bounds.right(),
372 bounds.top() + indicator_top,
373 *paint);
374 paint->setXfermode(nullptr);
377 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
378 SkCanvas* canvas,
379 const FrameRateCounter* fps_counter,
380 int right,
381 int top) const {
382 const int kPadding = 4;
383 const int kGap = 6;
385 const int kFontHeight = 15;
387 const int kGraphWidth =
388 base::saturated_cast<int>(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::MSAA_CONTENT:
588 status = "MSAA (content)";
589 color = SK_ColorCYAN;
590 break;
591 case GpuRasterizationStatus::OFF_CONTENT:
592 status = "off (content)";
593 color = SK_ColorYELLOW;
594 break;
597 if (status.empty())
598 return SkRect::MakeEmpty();
600 const int kPadding = 4;
601 const int kFontHeight = 13;
603 const int height = 2 * kFontHeight + 3 * kPadding;
604 const int left = bounds().width() - width - right;
605 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
607 SkPaint paint = CreatePaint();
608 DrawGraphBackground(canvas, &paint, area);
610 SkPoint gpu_status_pos = SkPoint::Make(left + width - kPadding,
611 top + 2 * kFontHeight + 2 * kPadding);
613 paint.setColor(color);
614 DrawText(canvas, &paint, "GPU raster: ", SkPaint::kLeft_Align, kFontHeight,
615 left + kPadding, top + kFontHeight + kPadding);
616 DrawText(canvas, &paint, status, SkPaint::kRight_Align, kFontHeight,
617 gpu_status_pos);
619 return area;
622 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
623 SkCanvas* canvas,
624 const PaintTimeCounter* paint_time_counter,
625 int right,
626 int top) const {
627 const int kPadding = 4;
628 const int kFontHeight = 14;
630 const int kGraphWidth =
631 base::saturated_cast<int>(paint_time_counter->HistorySize());
632 const int kGraphHeight = 40;
634 SkPaint paint = CreatePaint();
636 const std::string title = "Compositor frame time (ms)";
637 int title_text_width = MeasureText(&paint, title, kFontHeight);
638 int contents_width = std::max(title_text_width, kGraphWidth);
640 const int width = contents_width + 2 * kPadding;
641 const int height =
642 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
643 const int left = bounds().width() - width - right;
645 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
647 DrawGraphBackground(canvas, &paint, area);
649 SkRect text_bounds = SkRect::MakeXYWH(left + kPadding, top + kPadding,
650 contents_width, kFontHeight);
651 SkRect text_bounds2 =
652 SkRect::MakeXYWH(left + kPadding, text_bounds.bottom() + kPadding,
653 contents_width, kFontHeight);
654 SkRect graph_bounds = SkRect::MakeXYWH(left + (width - kGraphWidth) / 2,
655 text_bounds2.bottom() + 2 * kPadding,
656 kGraphWidth, kGraphHeight);
658 const std::string value_text =
659 base::StringPrintf("%.1f", paint_time_graph_.value);
660 const std::string min_max_text = base::StringPrintf(
661 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
663 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
664 DrawText(canvas, &paint, title, SkPaint::kLeft_Align, kFontHeight,
665 text_bounds.left(), text_bounds.bottom());
666 DrawText(canvas,
667 &paint,
668 value_text,
669 SkPaint::kLeft_Align,
670 kFontHeight,
671 text_bounds2.left(),
672 text_bounds2.bottom());
673 DrawText(canvas,
674 &paint,
675 min_max_text,
676 SkPaint::kRight_Align,
677 kFontHeight,
678 text_bounds2.right(),
679 text_bounds2.bottom());
681 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
682 for (PaintTimeCounter::RingBufferType::Iterator it =
683 paint_time_counter->End();
685 --it) {
686 double pt = it->InMillisecondsF();
688 if (pt == 0.0)
689 continue;
691 double p = pt / paint_time_graph_.current_upper_bound;
692 if (p > 1.0)
693 p = 1.0;
695 canvas->drawRect(
696 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
697 graph_bounds.bottom() - p * graph_bounds.height(),
699 p * graph_bounds.height()),
700 paint);
703 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
705 return area;
708 void HeadsUpDisplayLayerImpl::DrawDebugRect(
709 SkCanvas* canvas,
710 SkPaint* paint,
711 const DebugRect& rect,
712 SkColor stroke_color,
713 SkColor fill_color,
714 float stroke_width,
715 const std::string& label_text) const {
716 gfx::Rect debug_layer_rect =
717 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
718 1.0 / internal_contents_scale_);
719 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
720 paint->setColor(fill_color);
721 paint->setStyle(SkPaint::kFill_Style);
722 canvas->drawIRect(sk_rect, *paint);
724 paint->setColor(stroke_color);
725 paint->setStyle(SkPaint::kStroke_Style);
726 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
727 canvas->drawIRect(sk_rect, *paint);
729 if (label_text.length()) {
730 const int kFontHeight = 12;
731 const int kPadding = 3;
733 // The debug_layer_rect may be huge, and converting to a floating point may
734 // be lossy, so intersect with the HUD layer bounds first to prevent that.
735 gfx::Rect clip_rect = debug_layer_rect;
736 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
737 SkRect sk_clip_rect = RectToSkRect(clip_rect);
739 canvas->save();
740 canvas->clipRect(sk_clip_rect);
741 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
743 SkPaint label_paint = CreatePaint();
744 label_paint.setTextSize(kFontHeight);
745 label_paint.setTypeface(typeface_.get());
746 label_paint.setColor(stroke_color);
748 const SkScalar label_text_width =
749 label_paint.measureText(label_text.c_str(), label_text.length());
750 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
751 kFontHeight + 2 * kPadding),
752 label_paint);
754 label_paint.setAntiAlias(true);
755 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
756 canvas->drawText(label_text.c_str(),
757 label_text.length(),
758 kPadding,
759 kFontHeight * 0.8f + kPadding,
760 label_paint);
762 canvas->restore();
766 void HeadsUpDisplayLayerImpl::DrawDebugRects(
767 SkCanvas* canvas,
768 DebugRectHistory* debug_rect_history) {
769 SkPaint paint = CreatePaint();
771 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
772 std::vector<DebugRect> new_paint_rects;
774 for (size_t i = 0; i < debug_rects.size(); ++i) {
775 SkColor stroke_color = 0;
776 SkColor fill_color = 0;
777 float stroke_width = 0.f;
778 std::string label_text;
780 switch (debug_rects[i].type) {
781 case PAINT_RECT_TYPE:
782 new_paint_rects.push_back(debug_rects[i]);
783 continue;
784 case PROPERTY_CHANGED_RECT_TYPE:
785 stroke_color = DebugColors::PropertyChangedRectBorderColor();
786 fill_color = DebugColors::PropertyChangedRectFillColor();
787 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
788 break;
789 case SURFACE_DAMAGE_RECT_TYPE:
790 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
791 fill_color = DebugColors::SurfaceDamageRectFillColor();
792 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
793 break;
794 case REPLICA_SCREEN_SPACE_RECT_TYPE:
795 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
796 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
797 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
798 break;
799 case SCREEN_SPACE_RECT_TYPE:
800 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
801 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
802 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
803 break;
804 case TOUCH_EVENT_HANDLER_RECT_TYPE:
805 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
806 fill_color = DebugColors::TouchEventHandlerRectFillColor();
807 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
808 label_text = "touch event listener";
809 break;
810 case WHEEL_EVENT_HANDLER_RECT_TYPE:
811 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
812 fill_color = DebugColors::WheelEventHandlerRectFillColor();
813 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
814 label_text = "mousewheel event listener";
815 break;
816 case SCROLL_EVENT_HANDLER_RECT_TYPE:
817 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
818 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
819 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
820 label_text = "scroll event listener";
821 break;
822 case NON_FAST_SCROLLABLE_RECT_TYPE:
823 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
824 fill_color = DebugColors::NonFastScrollableRectFillColor();
825 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
826 label_text = "repaints on scroll";
827 break;
828 case ANIMATION_BOUNDS_RECT_TYPE:
829 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
830 fill_color = DebugColors::LayerAnimationBoundsFillColor();
831 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
832 label_text = "animation bounds";
833 break;
836 DrawDebugRect(canvas,
837 &paint,
838 debug_rects[i],
839 stroke_color,
840 fill_color,
841 stroke_width,
842 label_text);
845 if (new_paint_rects.size()) {
846 paint_rects_.swap(new_paint_rects);
847 fade_step_ = DebugColors::kFadeSteps;
849 if (fade_step_ > 0) {
850 fade_step_--;
851 for (size_t i = 0; i < paint_rects_.size(); ++i) {
852 DrawDebugRect(canvas,
853 &paint,
854 paint_rects_[i],
855 DebugColors::PaintRectBorderColor(fade_step_),
856 DebugColors::PaintRectFillColor(fade_step_),
857 DebugColors::PaintRectBorderWidth(),
858 "");
863 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
864 return "cc::HeadsUpDisplayLayerImpl";
867 void HeadsUpDisplayLayerImpl::AsValueInto(
868 base::trace_event::TracedValue* dict) const {
869 LayerImpl::AsValueInto(dict);
870 dict->SetString("layer_name", "Heads Up Display Layer");
873 } // namespace cc