Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob2ed00a67f1291826a774384d6f414c8a1b321d76
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/khronos/GLES2/gl2.h"
24 #include "third_party/khronos/GLES2/gl2ext.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkTypeface.h"
28 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
29 #include "ui/gfx/geometry/point.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/geometry/size_conversions.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_(skia::AdoptRef(
74 SkTypeface::CreateFromName("monospace", SkTypeface::kBold))),
75 internal_contents_scale_(1.f),
76 fps_graph_(60.0, 80.0),
77 paint_time_graph_(16.0, 48.0),
78 fade_step_(0) {
81 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
83 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
84 LayerTreeImpl* tree_impl) {
85 return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
88 void HeadsUpDisplayLayerImpl::AcquireResource(
89 ResourceProvider* resource_provider) {
90 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
91 it != resources_.end();
92 ++it) {
93 if (!resource_provider->InUseByConsumer((*it)->id())) {
94 resources_.swap(it, resources_.end() - 1);
95 return;
99 scoped_ptr<ScopedResource> resource =
100 ScopedResource::Create(resource_provider);
101 resource->Allocate(internal_content_bounds_,
102 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
103 resources_.push_back(resource.Pass());
106 class ResourceSizeIsEqualTo {
107 public:
108 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
109 : compare_size_(size_) {}
111 bool operator()(const ScopedResource* resource) {
112 return resource->size() == compare_size_;
115 private:
116 const gfx::Size compare_size_;
119 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
120 ResourceProvider* resource_provider) {
121 ScopedPtrVector<ScopedResource>::iterator it_erase =
122 resources_.partition(ResourceSizeIsEqualTo(internal_content_bounds_));
123 resources_.erase(it_erase, resources_.end());
126 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
127 ResourceProvider* resource_provider) {
128 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
129 return false;
131 internal_contents_scale_ = draw_properties().ideal_contents_scale;
132 internal_content_bounds_ =
133 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_));
135 ReleaseUnmatchedSizeResources(resource_provider);
136 AcquireResource(resource_provider);
137 return LayerImpl::WillDraw(draw_mode, resource_provider);
140 void HeadsUpDisplayLayerImpl::AppendQuads(
141 RenderPass* render_pass,
142 AppendQuadsData* append_quads_data) {
143 if (!resources_.back()->id())
144 return;
146 SharedQuadState* shared_quad_state =
147 render_pass->CreateAndAppendSharedQuadState();
148 PopulateScaledSharedQuadState(shared_quad_state, internal_contents_scale_);
150 gfx::Rect quad_rect(internal_content_bounds_);
151 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
152 gfx::Rect visible_quad_rect(quad_rect);
153 bool premultiplied_alpha = true;
154 gfx::PointF uv_top_left(0.f, 0.f);
155 gfx::PointF uv_bottom_right(1.f, 1.f);
156 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
157 bool flipped = false;
158 bool nearest_neighbor = false;
159 TextureDrawQuad* quad =
160 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
161 quad->SetNew(shared_quad_state,
162 quad_rect,
163 opaque_rect,
164 visible_quad_rect,
165 resources_.back()->id(),
166 premultiplied_alpha,
167 uv_top_left,
168 uv_bottom_right,
169 SK_ColorTRANSPARENT,
170 vertex_opacity,
171 flipped,
172 nearest_neighbor);
175 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
176 DrawMode draw_mode,
177 ResourceProvider* resource_provider) {
178 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
179 return;
181 SkISize canvas_size;
182 if (hud_surface_)
183 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
184 else
185 canvas_size.set(0, 0);
187 if (canvas_size.width() != internal_content_bounds_.width() ||
188 canvas_size.height() != internal_content_bounds_.height() ||
189 !hud_surface_) {
190 TRACE_EVENT0("cc", "ResizeHudCanvas");
192 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
193 internal_content_bounds_.width(), internal_content_bounds_.height()));
196 UpdateHudContents();
199 TRACE_EVENT0("cc", "DrawHudContents");
200 hud_surface_->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
201 hud_surface_->getCanvas()->save();
202 hud_surface_->getCanvas()->scale(internal_contents_scale_,
203 internal_contents_scale_);
205 DrawHudContents(hud_surface_->getCanvas());
207 hud_surface_->getCanvas()->restore();
210 TRACE_EVENT0("cc", "UploadHudTexture");
211 SkImageInfo info;
212 size_t row_bytes = 0;
213 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
214 DCHECK(pixels);
215 DCHECK(info.colorType() == kN32_SkColorType);
216 resource_provider->CopyToResource(resources_.back()->id(),
217 static_cast<const uint8_t*>(pixels),
218 internal_content_bounds_);
221 void HeadsUpDisplayLayerImpl::ReleaseResources() {
222 resources_.clear();
225 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
226 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
228 // Don't update numbers every frame so text is readable.
229 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
230 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
231 time_of_last_graph_update_ = now;
233 if (debug_state.show_fps_counter) {
234 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
235 fps_graph_.value = fps_counter->GetAverageFPS();
236 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
239 if (debug_state.continuous_painting) {
240 PaintTimeCounter* paint_time_counter =
241 layer_tree_impl()->paint_time_counter();
242 base::TimeDelta latest, min, max;
244 if (paint_time_counter->End())
245 latest = **paint_time_counter->End();
246 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
248 paint_time_graph_.value = latest.InMillisecondsF();
249 paint_time_graph_.min = min.InMillisecondsF();
250 paint_time_graph_.max = max.InMillisecondsF();
253 if (debug_state.ShowMemoryStats()) {
254 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
255 if (memory_history->End())
256 memory_entry_ = **memory_history->End();
257 else
258 memory_entry_ = MemoryHistory::Entry();
262 fps_graph_.UpdateUpperBound();
263 paint_time_graph_.UpdateUpperBound();
266 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
267 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
269 if (debug_state.ShowHudRects()) {
270 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
271 if (IsAnimatingHUDContents()) {
272 layer_tree_impl()->SetNeedsRedraw();
276 SkRect area = SkRect::MakeEmpty();
277 if (debug_state.continuous_painting) {
278 area = DrawPaintTimeDisplay(
279 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
280 } else if (debug_state.show_fps_counter) {
281 // Don't show the FPS display when continuous painting is enabled, because
282 // it would show misleading numbers.
283 area =
284 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
287 if (debug_state.show_fps_counter || debug_state.continuous_painting) {
288 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
289 SkMaxScalar(area.width(), 150));
292 if (debug_state.ShowMemoryStats())
293 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
296 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
297 SkPaint* paint,
298 const std::string& text,
299 SkPaint::Align align,
300 int size,
301 int x,
302 int y) const {
303 const bool anti_alias = paint->isAntiAlias();
304 paint->setAntiAlias(true);
306 paint->setTextSize(size);
307 paint->setTextAlign(align);
308 paint->setTypeface(typeface_.get());
309 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
311 paint->setAntiAlias(anti_alias);
314 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
315 SkPaint* paint,
316 const std::string& text,
317 SkPaint::Align align,
318 int size,
319 const SkPoint& pos) const {
320 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
323 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
324 SkPaint* paint,
325 const SkRect& bounds) const {
326 paint->setColor(DebugColors::HUDBackgroundColor());
327 canvas->drawRect(bounds, *paint);
330 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
331 SkPaint* paint,
332 const SkRect& bounds,
333 const Graph& graph) const {
334 // Draw top and bottom line.
335 paint->setColor(DebugColors::HUDSeparatorLineColor());
336 canvas->drawLine(bounds.left(),
337 bounds.top() - 1,
338 bounds.right(),
339 bounds.top() - 1,
340 *paint);
341 canvas->drawLine(
342 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
344 // Draw indicator line (additive blend mode to increase contrast when drawn on
345 // top of graph).
346 paint->setColor(DebugColors::HUDIndicatorLineColor());
347 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
348 const double indicator_top =
349 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
350 1.0;
351 canvas->drawLine(bounds.left(),
352 bounds.top() + indicator_top,
353 bounds.right(),
354 bounds.top() + indicator_top,
355 *paint);
356 paint->setXfermode(nullptr);
359 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
360 SkCanvas* canvas,
361 const FrameRateCounter* fps_counter,
362 int right,
363 int top) const {
364 const int kPadding = 4;
365 const int kGap = 6;
367 const int kFontHeight = 15;
369 const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
370 const int kGraphHeight = 40;
372 const int kHistogramWidth = 37;
374 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
375 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
376 int left = bounds().width() - width - right;
377 SkRect area = SkRect::MakeXYWH(left, top, width, height);
379 SkPaint paint = CreatePaint();
380 DrawGraphBackground(canvas, &paint, area);
382 SkRect text_bounds =
383 SkRect::MakeXYWH(left + kPadding,
384 top + kPadding,
385 kGraphWidth + kHistogramWidth + kGap + 2,
386 kFontHeight);
387 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
388 text_bounds.bottom() + 2 * kPadding,
389 kGraphWidth,
390 kGraphHeight);
391 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
392 graph_bounds.top(),
393 kHistogramWidth,
394 kGraphHeight);
396 const std::string value_text =
397 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
398 const std::string min_max_text =
399 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
401 VLOG(1) << value_text;
403 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
404 DrawText(canvas,
405 &paint,
406 value_text,
407 SkPaint::kLeft_Align,
408 kFontHeight,
409 text_bounds.left(),
410 text_bounds.bottom());
411 DrawText(canvas,
412 &paint,
413 min_max_text,
414 SkPaint::kRight_Align,
415 kFontHeight,
416 text_bounds.right(),
417 text_bounds.bottom());
419 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
421 // Collect graph and histogram data.
422 SkPath path;
424 const int kHistogramSize = 20;
425 double histogram[kHistogramSize] = { 1.0 };
426 double max_bucket_value = 1.0;
428 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
429 --it) {
430 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
432 // Skip this particular instantaneous frame rate if it is not likely to have
433 // been valid.
434 if (!fps_counter->IsBadFrameInterval(delta)) {
435 double fps = 1.0 / delta.InSecondsF();
437 // Clamp the FPS to the range we want to plot visually.
438 double p = fps / fps_graph_.current_upper_bound;
439 if (p > 1.0)
440 p = 1.0;
442 // Plot this data point.
443 SkPoint cur =
444 SkPoint::Make(graph_bounds.left() + it.index(),
445 graph_bounds.bottom() - p * graph_bounds.height());
446 if (path.isEmpty())
447 path.moveTo(cur);
448 else
449 path.lineTo(cur);
451 // Use the fps value to find the right bucket in the histogram.
452 int bucket_index = floor(p * (kHistogramSize - 1));
454 // Add the delta time to take the time spent at that fps rate into
455 // account.
456 histogram[bucket_index] += delta.InSecondsF();
457 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
461 // Draw FPS histogram.
462 paint.setColor(DebugColors::HUDSeparatorLineColor());
463 canvas->drawLine(histogram_bounds.left() - 1,
464 histogram_bounds.top() - 1,
465 histogram_bounds.left() - 1,
466 histogram_bounds.bottom() + 1,
467 paint);
468 canvas->drawLine(histogram_bounds.right() + 1,
469 histogram_bounds.top() - 1,
470 histogram_bounds.right() + 1,
471 histogram_bounds.bottom() + 1,
472 paint);
474 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
475 const double bar_height = histogram_bounds.height() / kHistogramSize;
477 for (int i = kHistogramSize - 1; i >= 0; --i) {
478 if (histogram[i] > 0) {
479 double bar_width =
480 histogram[i] / max_bucket_value * histogram_bounds.width();
481 canvas->drawRect(
482 SkRect::MakeXYWH(histogram_bounds.left(),
483 histogram_bounds.bottom() - (i + 1) * bar_height,
484 bar_width,
486 paint);
490 // Draw FPS graph.
491 paint.setAntiAlias(true);
492 paint.setStyle(SkPaint::kStroke_Style);
493 paint.setStrokeWidth(1);
494 canvas->drawPath(path, paint);
496 return area;
499 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
500 int right,
501 int top,
502 int width) const {
503 if (!memory_entry_.total_bytes_used)
504 return SkRect::MakeEmpty();
506 const int kPadding = 4;
507 const int kFontHeight = 13;
509 const int height = 3 * kFontHeight + 4 * kPadding;
510 const int left = bounds().width() - width - right;
511 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
513 const double kMegabyte = 1024.0 * 1024.0;
515 SkPaint paint = CreatePaint();
516 DrawGraphBackground(canvas, &paint, area);
518 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
519 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
520 top + kPadding + 2 * kFontHeight);
521 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
522 top + 2 * kPadding + 3 * kFontHeight);
524 paint.setColor(DebugColors::MemoryDisplayTextColor());
525 DrawText(canvas,
526 &paint,
527 "GPU memory",
528 SkPaint::kLeft_Align,
529 kFontHeight,
530 title_pos);
532 std::string text = base::StringPrintf(
533 "%6.1f MB used", memory_entry_.total_bytes_used / kMegabyte);
534 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
536 if (!memory_entry_.had_enough_memory)
537 paint.setColor(SK_ColorRED);
538 text = base::StringPrintf("%6.1f MB max ",
539 memory_entry_.total_budget_in_bytes / kMegabyte);
540 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
542 return area;
545 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
546 int right,
547 int top,
548 int width) const {
549 std::string status;
550 SkColor color = SK_ColorRED;
551 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
552 case GpuRasterizationStatus::ON:
553 status = "GPU raster: on";
554 color = SK_ColorGREEN;
555 break;
556 case GpuRasterizationStatus::ON_FORCED:
557 status = "GPU raster: on (forced)";
558 color = SK_ColorGREEN;
559 break;
560 case GpuRasterizationStatus::OFF_DEVICE:
561 status = "GPU raster: off (device)";
562 color = SK_ColorRED;
563 break;
564 case GpuRasterizationStatus::OFF_VIEWPORT:
565 status = "GPU raster: off (viewport)";
566 color = SK_ColorYELLOW;
567 break;
568 case GpuRasterizationStatus::OFF_CONTENT:
569 status = "GPU raster: off (content)";
570 color = SK_ColorYELLOW;
571 break;
574 if (status.empty())
575 return SkRect::MakeEmpty();
577 const int kPadding = 4;
578 const int kFontHeight = 13;
580 const int height = kFontHeight + 2 * kPadding;
581 const int left = bounds().width() - width - right;
582 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
584 SkPaint paint = CreatePaint();
585 DrawGraphBackground(canvas, &paint, area);
587 SkPoint gpu_status_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
589 paint.setColor(color);
590 DrawText(canvas, &paint, status, SkPaint::kLeft_Align, kFontHeight,
591 gpu_status_pos);
593 return area;
596 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
597 SkCanvas* canvas,
598 const PaintTimeCounter* paint_time_counter,
599 int right,
600 int top) const {
601 const int kPadding = 4;
602 const int kFontHeight = 15;
604 const int kGraphWidth = paint_time_counter->HistorySize();
605 const int kGraphHeight = 40;
607 const int width = kGraphWidth + 2 * kPadding;
608 const int height =
609 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
610 const int left = bounds().width() - width - right;
612 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
614 SkPaint paint = CreatePaint();
615 DrawGraphBackground(canvas, &paint, area);
617 SkRect text_bounds = SkRect::MakeXYWH(
618 left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
619 SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
620 text_bounds.bottom() + kPadding,
621 kGraphWidth,
622 kFontHeight);
623 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
624 text_bounds2.bottom() + 2 * kPadding,
625 kGraphWidth,
626 kGraphHeight);
628 const std::string value_text =
629 base::StringPrintf("%.1f", paint_time_graph_.value);
630 const std::string min_max_text = base::StringPrintf(
631 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
633 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
634 DrawText(canvas, &paint, "Compositor frame time (ms)", SkPaint::kLeft_Align,
635 kFontHeight, text_bounds.left(), text_bounds.bottom());
636 DrawText(canvas,
637 &paint,
638 value_text,
639 SkPaint::kLeft_Align,
640 kFontHeight,
641 text_bounds2.left(),
642 text_bounds2.bottom());
643 DrawText(canvas,
644 &paint,
645 min_max_text,
646 SkPaint::kRight_Align,
647 kFontHeight,
648 text_bounds2.right(),
649 text_bounds2.bottom());
651 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
652 for (PaintTimeCounter::RingBufferType::Iterator it =
653 paint_time_counter->End();
655 --it) {
656 double pt = it->InMillisecondsF();
658 if (pt == 0.0)
659 continue;
661 double p = pt / paint_time_graph_.current_upper_bound;
662 if (p > 1.0)
663 p = 1.0;
665 canvas->drawRect(
666 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
667 graph_bounds.bottom() - p * graph_bounds.height(),
669 p * graph_bounds.height()),
670 paint);
673 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
675 return area;
678 void HeadsUpDisplayLayerImpl::DrawDebugRect(
679 SkCanvas* canvas,
680 SkPaint* paint,
681 const DebugRect& rect,
682 SkColor stroke_color,
683 SkColor fill_color,
684 float stroke_width,
685 const std::string& label_text) const {
686 gfx::Rect debug_layer_rect =
687 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
688 1.0 / internal_contents_scale_);
689 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
690 paint->setColor(fill_color);
691 paint->setStyle(SkPaint::kFill_Style);
692 canvas->drawIRect(sk_rect, *paint);
694 paint->setColor(stroke_color);
695 paint->setStyle(SkPaint::kStroke_Style);
696 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
697 canvas->drawIRect(sk_rect, *paint);
699 if (label_text.length()) {
700 const int kFontHeight = 12;
701 const int kPadding = 3;
703 // The debug_layer_rect may be huge, and converting to a floating point may
704 // be lossy, so intersect with the HUD layer bounds first to prevent that.
705 gfx::Rect clip_rect = debug_layer_rect;
706 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
707 SkRect sk_clip_rect = RectToSkRect(clip_rect);
709 canvas->save();
710 canvas->clipRect(sk_clip_rect);
711 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
713 SkPaint label_paint = CreatePaint();
714 label_paint.setTextSize(kFontHeight);
715 label_paint.setTypeface(typeface_.get());
716 label_paint.setColor(stroke_color);
718 const SkScalar label_text_width =
719 label_paint.measureText(label_text.c_str(), label_text.length());
720 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
721 kFontHeight + 2 * kPadding),
722 label_paint);
724 label_paint.setAntiAlias(true);
725 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
726 canvas->drawText(label_text.c_str(),
727 label_text.length(),
728 kPadding,
729 kFontHeight * 0.8f + kPadding,
730 label_paint);
732 canvas->restore();
736 void HeadsUpDisplayLayerImpl::DrawDebugRects(
737 SkCanvas* canvas,
738 DebugRectHistory* debug_rect_history) {
739 SkPaint paint = CreatePaint();
741 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
742 std::vector<DebugRect> new_paint_rects;
744 for (size_t i = 0; i < debug_rects.size(); ++i) {
745 SkColor stroke_color = 0;
746 SkColor fill_color = 0;
747 float stroke_width = 0.f;
748 std::string label_text;
750 switch (debug_rects[i].type) {
751 case PAINT_RECT_TYPE:
752 new_paint_rects.push_back(debug_rects[i]);
753 continue;
754 case PROPERTY_CHANGED_RECT_TYPE:
755 stroke_color = DebugColors::PropertyChangedRectBorderColor();
756 fill_color = DebugColors::PropertyChangedRectFillColor();
757 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
758 break;
759 case SURFACE_DAMAGE_RECT_TYPE:
760 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
761 fill_color = DebugColors::SurfaceDamageRectFillColor();
762 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
763 break;
764 case REPLICA_SCREEN_SPACE_RECT_TYPE:
765 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
766 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
767 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
768 break;
769 case SCREEN_SPACE_RECT_TYPE:
770 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
771 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
772 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
773 break;
774 case TOUCH_EVENT_HANDLER_RECT_TYPE:
775 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
776 fill_color = DebugColors::TouchEventHandlerRectFillColor();
777 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
778 label_text = "touch event listener";
779 break;
780 case WHEEL_EVENT_HANDLER_RECT_TYPE:
781 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
782 fill_color = DebugColors::WheelEventHandlerRectFillColor();
783 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
784 label_text = "mousewheel event listener";
785 break;
786 case SCROLL_EVENT_HANDLER_RECT_TYPE:
787 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
788 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
789 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
790 label_text = "scroll event listener";
791 break;
792 case NON_FAST_SCROLLABLE_RECT_TYPE:
793 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
794 fill_color = DebugColors::NonFastScrollableRectFillColor();
795 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
796 label_text = "repaints on scroll";
797 break;
798 case ANIMATION_BOUNDS_RECT_TYPE:
799 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
800 fill_color = DebugColors::LayerAnimationBoundsFillColor();
801 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
802 label_text = "animation bounds";
803 break;
806 DrawDebugRect(canvas,
807 &paint,
808 debug_rects[i],
809 stroke_color,
810 fill_color,
811 stroke_width,
812 label_text);
815 if (new_paint_rects.size()) {
816 paint_rects_.swap(new_paint_rects);
817 fade_step_ = DebugColors::kFadeSteps;
819 if (fade_step_ > 0) {
820 fade_step_--;
821 for (size_t i = 0; i < paint_rects_.size(); ++i) {
822 DrawDebugRect(canvas,
823 &paint,
824 paint_rects_[i],
825 DebugColors::PaintRectBorderColor(fade_step_),
826 DebugColors::PaintRectFillColor(fade_step_),
827 DebugColors::PaintRectBorderWidth(),
828 "");
833 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
834 return "cc::HeadsUpDisplayLayerImpl";
837 void HeadsUpDisplayLayerImpl::AsValueInto(
838 base::trace_event::TracedValue* dict) const {
839 LayerImpl::AsValueInto(dict);
840 dict->SetString("layer_name", "Heads Up Display Layer");
843 } // namespace cc