Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blobc031bd4d4690592f5fbfff6f983b01287d0a96da
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 "cc/debug/debug_colors.h"
12 #include "cc/debug/frame_rate_counter.h"
13 #include "cc/debug/paint_time_counter.h"
14 #include "cc/debug/traced_value.h"
15 #include "cc/output/renderer.h"
16 #include "cc/quads/texture_draw_quad.h"
17 #include "cc/resources/memory_history.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "skia/ext/platform_canvas.h"
20 #include "third_party/khronos/GLES2/gl2.h"
21 #include "third_party/khronos/GLES2/gl2ext.h"
22 #include "third_party/skia/include/core/SkBitmap.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/point.h"
27 #include "ui/gfx/size.h"
29 namespace cc {
31 static inline SkPaint CreatePaint() {
32 SkPaint paint;
33 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
34 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
35 // swizzle our colors when drawing to the SkCanvas.
36 SkColorMatrix swizzle_matrix;
37 for (int i = 0; i < 20; ++i)
38 swizzle_matrix.fMat[i] = 0;
39 swizzle_matrix.fMat[0 + 5 * 2] = 1;
40 swizzle_matrix.fMat[1 + 5 * 1] = 1;
41 swizzle_matrix.fMat[2 + 5 * 0] = 1;
42 swizzle_matrix.fMat[3 + 5 * 3] = 1;
44 skia::RefPtr<SkColorMatrixFilter> filter =
45 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
46 paint.setColorFilter(filter.get());
47 #endif
48 return paint;
51 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
52 double start_upper_bound)
53 : value(0.0),
54 min(0.0),
55 max(0.0),
56 current_upper_bound(start_upper_bound),
57 default_upper_bound(start_upper_bound),
58 indicator(indicator_value) {}
60 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
61 double target_upper_bound = std::max(max, default_upper_bound);
62 current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
63 return current_upper_bound;
66 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
67 int id)
68 : LayerImpl(tree_impl, id),
69 typeface_(skia::AdoptRef(
70 SkTypeface::CreateFromName("monospace", SkTypeface::kBold))),
71 fps_graph_(60.0, 80.0),
72 paint_time_graph_(16.0, 48.0),
73 fade_step_(0) {}
75 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
77 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
78 LayerTreeImpl* tree_impl) {
79 return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
82 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
83 ResourceProvider* resource_provider) {
84 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
85 return false;
87 if (!hud_resource_)
88 hud_resource_ = ScopedResource::Create(resource_provider);
90 // TODO(danakj): The HUD could swap between two textures instead of creating a
91 // texture every frame in ubercompositor.
92 if (hud_resource_->size() != content_bounds() ||
93 (hud_resource_->id() &&
94 resource_provider->InUseByConsumer(hud_resource_->id()))) {
95 hud_resource_->Free();
98 if (!hud_resource_->id()) {
99 hud_resource_->Allocate(content_bounds(),
100 ResourceProvider::TextureUsageAny,
101 RGBA_8888);
104 return LayerImpl::WillDraw(draw_mode, resource_provider);
107 void HeadsUpDisplayLayerImpl::AppendQuads(
108 RenderPass* render_pass,
109 const OcclusionTracker<LayerImpl>& occlusion_tracker,
110 AppendQuadsData* append_quads_data) {
111 if (!hud_resource_->id())
112 return;
114 SharedQuadState* shared_quad_state =
115 render_pass->CreateAndAppendSharedQuadState();
116 PopulateSharedQuadState(shared_quad_state);
118 gfx::Rect quad_rect(content_bounds());
119 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
120 gfx::Rect visible_quad_rect(quad_rect);
121 bool premultiplied_alpha = true;
122 gfx::PointF uv_top_left(0.f, 0.f);
123 gfx::PointF uv_bottom_right(1.f, 1.f);
124 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
125 bool flipped = false;
126 TextureDrawQuad* quad =
127 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
128 quad->SetNew(shared_quad_state,
129 quad_rect,
130 opaque_rect,
131 visible_quad_rect,
132 hud_resource_->id(),
133 premultiplied_alpha,
134 uv_top_left,
135 uv_bottom_right,
136 SK_ColorTRANSPARENT,
137 vertex_opacity,
138 flipped);
141 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
142 DrawMode draw_mode,
143 ResourceProvider* resource_provider) {
144 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !hud_resource_->id())
145 return;
147 SkISize canvas_size;
148 if (hud_canvas_)
149 canvas_size = hud_canvas_->getDeviceSize();
150 else
151 canvas_size.set(0, 0);
153 if (canvas_size.width() != content_bounds().width() ||
154 canvas_size.height() != content_bounds().height() || !hud_canvas_) {
155 TRACE_EVENT0("cc", "ResizeHudCanvas");
156 bool opaque = false;
157 hud_canvas_ = make_scoped_ptr(skia::CreateBitmapCanvas(
158 content_bounds().width(), content_bounds().height(), opaque));
161 UpdateHudContents();
164 TRACE_EVENT0("cc", "DrawHudContents");
165 hud_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
166 hud_canvas_->save();
167 hud_canvas_->scale(contents_scale_x(), contents_scale_y());
169 DrawHudContents(hud_canvas_.get());
171 hud_canvas_->restore();
174 TRACE_EVENT0("cc", "UploadHudTexture");
175 SkImageInfo info;
176 size_t row_bytes = 0;
177 const void* pixels = hud_canvas_->peekPixels(&info, &row_bytes);
178 DCHECK(pixels);
179 gfx::Rect content_rect(content_bounds());
180 DCHECK(info.colorType() == kN32_SkColorType);
181 resource_provider->SetPixels(hud_resource_->id(),
182 static_cast<const uint8_t*>(pixels),
183 content_rect,
184 content_rect,
185 gfx::Vector2d());
188 void HeadsUpDisplayLayerImpl::ReleaseResources() { hud_resource_.reset(); }
190 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
191 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
193 // Don't update numbers every frame so text is readable.
194 base::TimeTicks now = layer_tree_impl()->CurrentFrameTimeTicks();
195 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
196 time_of_last_graph_update_ = now;
198 if (debug_state.show_fps_counter) {
199 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
200 fps_graph_.value = fps_counter->GetAverageFPS();
201 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
204 if (debug_state.continuous_painting) {
205 PaintTimeCounter* paint_time_counter =
206 layer_tree_impl()->paint_time_counter();
207 base::TimeDelta latest, min, max;
209 if (paint_time_counter->End())
210 latest = **paint_time_counter->End();
211 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
213 paint_time_graph_.value = latest.InMillisecondsF();
214 paint_time_graph_.min = min.InMillisecondsF();
215 paint_time_graph_.max = max.InMillisecondsF();
218 if (debug_state.ShowMemoryStats()) {
219 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
220 if (memory_history->End())
221 memory_entry_ = **memory_history->End();
222 else
223 memory_entry_ = MemoryHistory::Entry();
227 fps_graph_.UpdateUpperBound();
228 paint_time_graph_.UpdateUpperBound();
231 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
232 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
234 if (debug_state.ShowHudRects()) {
235 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
236 if (IsAnimatingHUDContents()) {
237 layer_tree_impl()->SetNeedsRedraw();
241 SkRect area = SkRect::MakeEmpty();
242 if (debug_state.continuous_painting) {
243 area = DrawPaintTimeDisplay(
244 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
245 } else if (debug_state.show_fps_counter) {
246 // Don't show the FPS display when continuous painting is enabled, because
247 // it would show misleading numbers.
248 area =
249 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
252 if (debug_state.ShowMemoryStats())
253 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
256 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
257 SkPaint* paint,
258 const std::string& text,
259 SkPaint::Align align,
260 int size,
261 int x,
262 int y) const {
263 const bool anti_alias = paint->isAntiAlias();
264 paint->setAntiAlias(true);
266 paint->setTextSize(size);
267 paint->setTextAlign(align);
268 paint->setTypeface(typeface_.get());
269 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
271 paint->setAntiAlias(anti_alias);
274 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
275 SkPaint* paint,
276 const std::string& text,
277 SkPaint::Align align,
278 int size,
279 const SkPoint& pos) const {
280 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
283 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
284 SkPaint* paint,
285 const SkRect& bounds) const {
286 paint->setColor(DebugColors::HUDBackgroundColor());
287 canvas->drawRect(bounds, *paint);
290 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
291 SkPaint* paint,
292 const SkRect& bounds,
293 const Graph& graph) const {
294 // Draw top and bottom line.
295 paint->setColor(DebugColors::HUDSeparatorLineColor());
296 canvas->drawLine(bounds.left(),
297 bounds.top() - 1,
298 bounds.right(),
299 bounds.top() - 1,
300 *paint);
301 canvas->drawLine(
302 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
304 // Draw indicator line (additive blend mode to increase contrast when drawn on
305 // top of graph).
306 paint->setColor(DebugColors::HUDIndicatorLineColor());
307 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
308 const double indicator_top =
309 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
310 1.0;
311 canvas->drawLine(bounds.left(),
312 bounds.top() + indicator_top,
313 bounds.right(),
314 bounds.top() + indicator_top,
315 *paint);
316 paint->setXfermode(NULL);
319 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
320 SkCanvas* canvas,
321 const FrameRateCounter* fps_counter,
322 int right,
323 int top) const {
324 const int kPadding = 4;
325 const int kGap = 6;
327 const int kFontHeight = 15;
329 const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
330 const int kGraphHeight = 40;
332 const int kHistogramWidth = 37;
334 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
335 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
336 int left = bounds().width() - width - right;
337 SkRect area = SkRect::MakeXYWH(left, top, width, height);
339 SkPaint paint = CreatePaint();
340 DrawGraphBackground(canvas, &paint, area);
342 SkRect text_bounds =
343 SkRect::MakeXYWH(left + kPadding,
344 top + kPadding,
345 kGraphWidth + kHistogramWidth + kGap + 2,
346 kFontHeight);
347 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
348 text_bounds.bottom() + 2 * kPadding,
349 kGraphWidth,
350 kGraphHeight);
351 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
352 graph_bounds.top(),
353 kHistogramWidth,
354 kGraphHeight);
356 const std::string value_text =
357 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
358 const std::string min_max_text =
359 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
361 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
362 DrawText(canvas,
363 &paint,
364 value_text,
365 SkPaint::kLeft_Align,
366 kFontHeight,
367 text_bounds.left(),
368 text_bounds.bottom());
369 DrawText(canvas,
370 &paint,
371 min_max_text,
372 SkPaint::kRight_Align,
373 kFontHeight,
374 text_bounds.right(),
375 text_bounds.bottom());
377 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
379 // Collect graph and histogram data.
380 SkPath path;
382 const int kHistogramSize = 20;
383 double histogram[kHistogramSize] = { 1.0 };
384 double max_bucket_value = 1.0;
386 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
387 --it) {
388 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
390 // Skip this particular instantaneous frame rate if it is not likely to have
391 // been valid.
392 if (!fps_counter->IsBadFrameInterval(delta)) {
393 double fps = 1.0 / delta.InSecondsF();
395 // Clamp the FPS to the range we want to plot visually.
396 double p = fps / fps_graph_.current_upper_bound;
397 if (p > 1.0)
398 p = 1.0;
400 // Plot this data point.
401 SkPoint cur =
402 SkPoint::Make(graph_bounds.left() + it.index(),
403 graph_bounds.bottom() - p * graph_bounds.height());
404 if (path.isEmpty())
405 path.moveTo(cur);
406 else
407 path.lineTo(cur);
409 // Use the fps value to find the right bucket in the histogram.
410 int bucket_index = floor(p * (kHistogramSize - 1));
412 // Add the delta time to take the time spent at that fps rate into
413 // account.
414 histogram[bucket_index] += delta.InSecondsF();
415 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
419 // Draw FPS histogram.
420 paint.setColor(DebugColors::HUDSeparatorLineColor());
421 canvas->drawLine(histogram_bounds.left() - 1,
422 histogram_bounds.top() - 1,
423 histogram_bounds.left() - 1,
424 histogram_bounds.bottom() + 1,
425 paint);
426 canvas->drawLine(histogram_bounds.right() + 1,
427 histogram_bounds.top() - 1,
428 histogram_bounds.right() + 1,
429 histogram_bounds.bottom() + 1,
430 paint);
432 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
433 const double bar_height = histogram_bounds.height() / kHistogramSize;
435 for (int i = kHistogramSize - 1; i >= 0; --i) {
436 if (histogram[i] > 0) {
437 double bar_width =
438 histogram[i] / max_bucket_value * histogram_bounds.width();
439 canvas->drawRect(
440 SkRect::MakeXYWH(histogram_bounds.left(),
441 histogram_bounds.bottom() - (i + 1) * bar_height,
442 bar_width,
444 paint);
448 // Draw FPS graph.
449 paint.setAntiAlias(true);
450 paint.setStyle(SkPaint::kStroke_Style);
451 paint.setStrokeWidth(1);
452 canvas->drawPath(path, paint);
454 return area;
457 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
458 int right,
459 int top,
460 int width) const {
461 if (!memory_entry_.bytes_total())
462 return SkRect::MakeEmpty();
464 const int kPadding = 4;
465 const int kFontHeight = 13;
467 const int height = 3 * kFontHeight + 4 * kPadding;
468 const int left = bounds().width() - width - right;
469 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
471 const double megabyte = 1024.0 * 1024.0;
473 SkPaint paint = CreatePaint();
474 DrawGraphBackground(canvas, &paint, area);
476 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
477 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
478 top + kPadding + 2 * kFontHeight);
479 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
480 top + 2 * kPadding + 3 * kFontHeight);
482 paint.setColor(DebugColors::MemoryDisplayTextColor());
483 DrawText(canvas,
484 &paint,
485 "GPU memory",
486 SkPaint::kLeft_Align,
487 kFontHeight,
488 title_pos);
490 std::string text =
491 base::StringPrintf("%6.1f MB used",
492 (memory_entry_.bytes_unreleasable +
493 memory_entry_.bytes_allocated) / megabyte);
494 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
496 if (memory_entry_.bytes_over) {
497 paint.setColor(SK_ColorRED);
498 text = base::StringPrintf("%6.1f MB over",
499 memory_entry_.bytes_over / megabyte);
500 } else {
501 text = base::StringPrintf("%6.1f MB max ",
502 memory_entry_.total_budget_in_bytes / megabyte);
504 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
506 return area;
509 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
510 SkCanvas* canvas,
511 const PaintTimeCounter* paint_time_counter,
512 int right,
513 int top) const {
514 const int kPadding = 4;
515 const int kFontHeight = 15;
517 const int kGraphWidth = paint_time_counter->HistorySize();
518 const int kGraphHeight = 40;
520 const int width = kGraphWidth + 2 * kPadding;
521 const int height =
522 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
523 const int left = bounds().width() - width - right;
525 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
527 SkPaint paint = CreatePaint();
528 DrawGraphBackground(canvas, &paint, area);
530 SkRect text_bounds = SkRect::MakeXYWH(
531 left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
532 SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
533 text_bounds.bottom() + kPadding,
534 kGraphWidth,
535 kFontHeight);
536 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
537 text_bounds2.bottom() + 2 * kPadding,
538 kGraphWidth,
539 kGraphHeight);
541 const std::string value_text =
542 base::StringPrintf("%.1f", paint_time_graph_.value);
543 const std::string min_max_text = base::StringPrintf(
544 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
546 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
547 DrawText(canvas,
548 &paint,
549 "Page paint time (ms)",
550 SkPaint::kLeft_Align,
551 kFontHeight,
552 text_bounds.left(),
553 text_bounds.bottom());
554 DrawText(canvas,
555 &paint,
556 value_text,
557 SkPaint::kLeft_Align,
558 kFontHeight,
559 text_bounds2.left(),
560 text_bounds2.bottom());
561 DrawText(canvas,
562 &paint,
563 min_max_text,
564 SkPaint::kRight_Align,
565 kFontHeight,
566 text_bounds2.right(),
567 text_bounds2.bottom());
569 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
570 for (PaintTimeCounter::RingBufferType::Iterator it =
571 paint_time_counter->End();
573 --it) {
574 double pt = it->InMillisecondsF();
576 if (pt == 0.0)
577 continue;
579 double p = pt / paint_time_graph_.current_upper_bound;
580 if (p > 1.0)
581 p = 1.0;
583 canvas->drawRect(
584 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
585 graph_bounds.bottom() - p * graph_bounds.height(),
587 p * graph_bounds.height()),
588 paint);
591 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
593 return area;
596 void HeadsUpDisplayLayerImpl::DrawDebugRect(
597 SkCanvas* canvas,
598 SkPaint* paint,
599 const DebugRect& rect,
600 SkColor stroke_color,
601 SkColor fill_color,
602 float stroke_width,
603 const std::string& label_text) const {
604 gfx::Rect debug_layer_rect = gfx::ScaleToEnclosingRect(
605 rect.rect, 1.0 / contents_scale_x(), 1.0 / contents_scale_y());
606 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
607 paint->setColor(fill_color);
608 paint->setStyle(SkPaint::kFill_Style);
609 canvas->drawIRect(sk_rect, *paint);
611 paint->setColor(stroke_color);
612 paint->setStyle(SkPaint::kStroke_Style);
613 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
614 canvas->drawIRect(sk_rect, *paint);
616 if (label_text.length()) {
617 const int kFontHeight = 12;
618 const int kPadding = 3;
620 // The debug_layer_rect may be huge, and converting to a floating point may
621 // be lossy, so intersect with the HUD layer bounds first to prevent that.
622 gfx::Rect clip_rect = debug_layer_rect;
623 clip_rect.Intersect(gfx::Rect(content_bounds()));
624 SkRect sk_clip_rect = RectToSkRect(clip_rect);
626 canvas->save();
627 canvas->clipRect(sk_clip_rect);
628 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
630 SkPaint label_paint = CreatePaint();
631 label_paint.setTextSize(kFontHeight);
632 label_paint.setTypeface(typeface_.get());
633 label_paint.setColor(stroke_color);
635 const SkScalar label_text_width =
636 label_paint.measureText(label_text.c_str(), label_text.length());
637 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
638 kFontHeight + 2 * kPadding),
639 label_paint);
641 label_paint.setAntiAlias(true);
642 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
643 canvas->drawText(label_text.c_str(),
644 label_text.length(),
645 kPadding,
646 kFontHeight * 0.8f + kPadding,
647 label_paint);
649 canvas->restore();
653 void HeadsUpDisplayLayerImpl::DrawDebugRects(
654 SkCanvas* canvas,
655 DebugRectHistory* debug_rect_history) {
656 SkPaint paint = CreatePaint();
658 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
659 std::vector<DebugRect> new_paint_rects;
661 for (size_t i = 0; i < debug_rects.size(); ++i) {
662 SkColor stroke_color = 0;
663 SkColor fill_color = 0;
664 float stroke_width = 0.f;
665 std::string label_text;
667 switch (debug_rects[i].type) {
668 case PAINT_RECT_TYPE:
669 new_paint_rects.push_back(debug_rects[i]);
670 continue;
671 case PROPERTY_CHANGED_RECT_TYPE:
672 stroke_color = DebugColors::PropertyChangedRectBorderColor();
673 fill_color = DebugColors::PropertyChangedRectFillColor();
674 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
675 break;
676 case SURFACE_DAMAGE_RECT_TYPE:
677 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
678 fill_color = DebugColors::SurfaceDamageRectFillColor();
679 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
680 break;
681 case REPLICA_SCREEN_SPACE_RECT_TYPE:
682 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
683 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
684 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
685 break;
686 case SCREEN_SPACE_RECT_TYPE:
687 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
688 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
689 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
690 break;
691 case OCCLUDING_RECT_TYPE:
692 stroke_color = DebugColors::OccludingRectBorderColor();
693 fill_color = DebugColors::OccludingRectFillColor();
694 stroke_width = DebugColors::OccludingRectBorderWidth();
695 break;
696 case NONOCCLUDING_RECT_TYPE:
697 stroke_color = DebugColors::NonOccludingRectBorderColor();
698 fill_color = DebugColors::NonOccludingRectFillColor();
699 stroke_width = DebugColors::NonOccludingRectBorderWidth();
700 break;
701 case TOUCH_EVENT_HANDLER_RECT_TYPE:
702 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
703 fill_color = DebugColors::TouchEventHandlerRectFillColor();
704 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
705 label_text = "touch event listener";
706 break;
707 case WHEEL_EVENT_HANDLER_RECT_TYPE:
708 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
709 fill_color = DebugColors::WheelEventHandlerRectFillColor();
710 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
711 label_text = "mousewheel event listener";
712 break;
713 case SCROLL_EVENT_HANDLER_RECT_TYPE:
714 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
715 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
716 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
717 label_text = "scroll event listener";
718 break;
719 case NON_FAST_SCROLLABLE_RECT_TYPE:
720 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
721 fill_color = DebugColors::NonFastScrollableRectFillColor();
722 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
723 label_text = "repaints on scroll";
724 break;
725 case ANIMATION_BOUNDS_RECT_TYPE:
726 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
727 fill_color = DebugColors::LayerAnimationBoundsFillColor();
728 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
729 label_text = "animation bounds";
730 break;
733 DrawDebugRect(canvas,
734 &paint,
735 debug_rects[i],
736 stroke_color,
737 fill_color,
738 stroke_width,
739 label_text);
742 if (new_paint_rects.size()) {
743 paint_rects_.swap(new_paint_rects);
744 fade_step_ = DebugColors::kFadeSteps;
746 if (fade_step_ > 0) {
747 fade_step_--;
748 for (size_t i = 0; i < paint_rects_.size(); ++i) {
749 DrawDebugRect(canvas,
750 &paint,
751 paint_rects_[i],
752 DebugColors::PaintRectBorderColor(fade_step_),
753 DebugColors::PaintRectFillColor(fade_step_),
754 DebugColors::PaintRectBorderWidth(),
755 "");
760 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
761 return "cc::HeadsUpDisplayLayerImpl";
764 void HeadsUpDisplayLayerImpl::AsValueInto(base::DictionaryValue* dict) const {
765 LayerImpl::AsValueInto(dict);
766 dict->SetString("layer_name", "Heads Up Display Layer");
769 } // namespace cc