Separate projection mode from rest of touch HUD
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blobf31ef7b3da9db1ba596a4999d76793216db19c69
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/string_split.h"
11 #include "base/strings/stringprintf.h"
12 #include "cc/debug/debug_colors.h"
13 #include "cc/debug/debug_rect_history.h"
14 #include "cc/debug/frame_rate_counter.h"
15 #include "cc/debug/paint_time_counter.h"
16 #include "cc/layers/quad_sink.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/resources/tile_manager.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/point.h"
30 #include "ui/gfx/size.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(new SkColorMatrixFilter(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_(skia::AdoptRef(
73 SkTypeface::CreateFromName("monospace", SkTypeface::kBold))),
74 fps_graph_(60.0, 80.0),
75 paint_time_graph_(16.0, 48.0) {}
77 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
79 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
80 LayerTreeImpl* tree_impl) {
81 return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
84 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
85 ResourceProvider* resource_provider) {
86 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
87 return false;
89 if (!hud_resource_)
90 hud_resource_ = ScopedResource::create(resource_provider);
92 // TODO(danakj): Scale the HUD by device scale to make it more friendly under
93 // high DPI.
95 // TODO(danakj): The HUD could swap between two textures instead of creating a
96 // texture every frame in ubercompositor.
97 if (hud_resource_->size() != bounds() ||
98 resource_provider->InUseByConsumer(hud_resource_->id()))
99 hud_resource_->Free();
101 if (!hud_resource_->id()) {
102 hud_resource_->Allocate(
103 bounds(), GL_RGBA, ResourceProvider::TextureUsageAny);
106 return LayerImpl::WillDraw(draw_mode, resource_provider);
109 void HeadsUpDisplayLayerImpl::AppendQuads(QuadSink* quad_sink,
110 AppendQuadsData* append_quads_data) {
111 if (!hud_resource_->id())
112 return;
114 SharedQuadState* shared_quad_state =
115 quad_sink->UseSharedQuadState(CreateSharedQuadState());
117 gfx::Rect quad_rect(bounds());
118 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
119 bool premultiplied_alpha = true;
120 gfx::PointF uv_top_left(0.f, 0.f);
121 gfx::PointF uv_bottom_right(1.f, 1.f);
122 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
123 bool flipped = false;
124 scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
125 quad->SetNew(shared_quad_state,
126 quad_rect,
127 opaque_rect,
128 hud_resource_->id(),
129 premultiplied_alpha,
130 uv_top_left,
131 uv_bottom_right,
132 vertex_opacity,
133 flipped);
134 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
137 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
138 ResourceProvider* resource_provider) {
139 if (!hud_resource_->id())
140 return;
142 SkISize canvas_size;
143 if (hud_canvas_)
144 canvas_size = hud_canvas_->getDeviceSize();
145 else
146 canvas_size.set(0, 0);
148 if (canvas_size.fWidth != bounds().width() ||
149 canvas_size.fHeight != bounds().height() || !hud_canvas_) {
150 bool opaque = false;
151 hud_canvas_ = make_scoped_ptr(
152 skia::CreateBitmapCanvas(bounds().width(), bounds().height(), opaque));
155 UpdateHudContents();
157 hud_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
158 DrawHudContents(hud_canvas_.get());
160 const SkBitmap* bitmap = &hud_canvas_->getDevice()->accessBitmap(false);
161 SkAutoLockPixels locker(*bitmap);
163 gfx::Rect layer_rect(bounds());
164 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
165 resource_provider->SetPixels(hud_resource_->id(),
166 static_cast<const uint8_t*>(bitmap->getPixels()),
167 layer_rect,
168 layer_rect,
169 gfx::Vector2d());
172 void HeadsUpDisplayLayerImpl::DidLoseOutputSurface() { hud_resource_.reset(); }
174 bool HeadsUpDisplayLayerImpl::LayerIsAlwaysDamaged() const { return true; }
176 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
177 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
179 // Don't update numbers every frame so text is readable.
180 base::TimeTicks now = layer_tree_impl()->CurrentFrameTimeTicks();
181 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
182 time_of_last_graph_update_ = now;
184 if (debug_state.show_fps_counter) {
185 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
186 fps_graph_.value = fps_counter->GetAverageFPS();
187 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
190 if (debug_state.continuous_painting) {
191 PaintTimeCounter* paint_time_counter =
192 layer_tree_impl()->paint_time_counter();
193 base::TimeDelta latest, min, max;
195 if (paint_time_counter->End())
196 latest = **paint_time_counter->End();
197 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
199 paint_time_graph_.value = latest.InMillisecondsF();
200 paint_time_graph_.min = min.InMillisecondsF();
201 paint_time_graph_.max = max.InMillisecondsF();
204 if (debug_state.ShowMemoryStats()) {
205 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
206 if (memory_history->End())
207 memory_entry_ = **memory_history->End();
208 else
209 memory_entry_ = MemoryHistory::Entry();
213 fps_graph_.UpdateUpperBound();
214 paint_time_graph_.UpdateUpperBound();
217 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) const {
218 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
220 if (debug_state.ShowHudRects())
221 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
223 SkRect area = SkRect::MakeEmpty();
224 if (debug_state.continuous_painting) {
225 // Don't show the FPS display when continuous painting is enabled, because
226 // it would show misleading numbers.
227 area = DrawPaintTimeDisplay(
228 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
229 } else if (debug_state.show_fps_counter) {
230 area =
231 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
234 if (debug_state.ShowMemoryStats())
235 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
238 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
239 SkPaint* paint,
240 const std::string& text,
241 SkPaint::Align align,
242 int size,
243 int x,
244 int y) const {
245 const bool anti_alias = paint->isAntiAlias();
246 paint->setAntiAlias(true);
248 paint->setTextSize(size);
249 paint->setTextAlign(align);
250 paint->setTypeface(typeface_.get());
251 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
253 paint->setAntiAlias(anti_alias);
256 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
257 SkPaint* paint,
258 const std::string& text,
259 SkPaint::Align align,
260 int size,
261 const SkPoint& pos) const {
262 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
265 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
266 SkPaint* paint,
267 const SkRect& bounds) const {
268 paint->setColor(DebugColors::HUDBackgroundColor());
269 canvas->drawRect(bounds, *paint);
272 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
273 SkPaint* paint,
274 const SkRect& bounds,
275 const Graph& graph) const {
276 // Draw top and bottom line.
277 paint->setColor(DebugColors::HUDSeparatorLineColor());
278 canvas->drawLine(bounds.left(),
279 bounds.top() - 1,
280 bounds.right(),
281 bounds.top() - 1,
282 *paint);
283 canvas->drawLine(
284 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
286 // Draw indicator line (additive blend mode to increase contrast when drawn on
287 // top of graph).
288 paint->setColor(DebugColors::HUDIndicatorLineColor());
289 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
290 const double indicator_top =
291 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
292 1.0;
293 canvas->drawLine(bounds.left(),
294 bounds.top() + indicator_top,
295 bounds.right(),
296 bounds.top() + indicator_top,
297 *paint);
298 paint->setXfermode(NULL);
301 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
302 SkCanvas* canvas,
303 const FrameRateCounter* fps_counter,
304 int right,
305 int top) const {
306 const int kPadding = 4;
307 const int kGap = 6;
309 const int kFontHeight = 15;
311 const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
312 const int kGraphHeight = 40;
314 const int kHistogramWidth = 37;
316 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
317 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
318 int left = bounds().width() - width - right;
319 SkRect area = SkRect::MakeXYWH(left, top, width, height);
321 SkPaint paint = CreatePaint();
322 DrawGraphBackground(canvas, &paint, area);
324 SkRect text_bounds =
325 SkRect::MakeXYWH(left + kPadding,
326 top + kPadding,
327 kGraphWidth + kHistogramWidth + kGap + 2,
328 kFontHeight);
329 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
330 text_bounds.bottom() + 2 * kPadding,
331 kGraphWidth,
332 kGraphHeight);
333 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
334 graph_bounds.top(),
335 kHistogramWidth,
336 kGraphHeight);
338 const std::string value_text =
339 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
340 const std::string min_max_text =
341 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
343 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
344 DrawText(canvas,
345 &paint,
346 value_text,
347 SkPaint::kLeft_Align,
348 kFontHeight,
349 text_bounds.left(),
350 text_bounds.bottom());
351 DrawText(canvas,
352 &paint,
353 min_max_text,
354 SkPaint::kRight_Align,
355 kFontHeight,
356 text_bounds.right(),
357 text_bounds.bottom());
359 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
361 // Collect graph and histogram data.
362 SkPath path;
364 const int kHistogramSize = 20;
365 double histogram[kHistogramSize] = { 1.0 };
366 double max_bucket_value = 1.0;
368 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
369 --it) {
370 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
372 // Skip this particular instantaneous frame rate if it is not likely to have
373 // been valid.
374 if (!fps_counter->IsBadFrameInterval(delta)) {
375 double fps = 1.0 / delta.InSecondsF();
377 // Clamp the FPS to the range we want to plot visually.
378 double p = fps / fps_graph_.current_upper_bound;
379 if (p > 1.0)
380 p = 1.0;
382 // Plot this data point.
383 SkPoint cur =
384 SkPoint::Make(graph_bounds.left() + it.index(),
385 graph_bounds.bottom() - p * graph_bounds.height());
386 if (path.isEmpty())
387 path.moveTo(cur);
388 else
389 path.lineTo(cur);
391 // Use the fps value to find the right bucket in the histogram.
392 int bucket_index = floor(p * (kHistogramSize - 1));
394 // Add the delta time to take the time spent at that fps rate into
395 // account.
396 histogram[bucket_index] += delta.InSecondsF();
397 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
401 // Draw FPS histogram.
402 paint.setColor(DebugColors::HUDSeparatorLineColor());
403 canvas->drawLine(histogram_bounds.left() - 1,
404 histogram_bounds.top() - 1,
405 histogram_bounds.left() - 1,
406 histogram_bounds.bottom() + 1,
407 paint);
408 canvas->drawLine(histogram_bounds.right() + 1,
409 histogram_bounds.top() - 1,
410 histogram_bounds.right() + 1,
411 histogram_bounds.bottom() + 1,
412 paint);
414 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
415 const double bar_height = histogram_bounds.height() / kHistogramSize;
417 for (int i = kHistogramSize - 1; i >= 0; --i) {
418 if (histogram[i] > 0) {
419 double bar_width =
420 histogram[i] / max_bucket_value * histogram_bounds.width();
421 canvas->drawRect(
422 SkRect::MakeXYWH(histogram_bounds.left(),
423 histogram_bounds.bottom() - (i + 1) * bar_height,
424 bar_width,
426 paint);
430 // Draw FPS graph.
431 paint.setAntiAlias(true);
432 paint.setStyle(SkPaint::kStroke_Style);
433 paint.setStrokeWidth(1);
434 canvas->drawPath(path, paint);
436 return area;
439 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
440 int right,
441 int top,
442 int width) const {
443 if (!memory_entry_.bytes_total())
444 return SkRect::MakeEmpty();
446 const int kPadding = 4;
447 const int kFontHeight = 13;
449 const int height = 3 * kFontHeight + 4 * kPadding;
450 const int left = bounds().width() - width - right;
451 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
453 const double megabyte = 1024.0 * 1024.0;
455 SkPaint paint = CreatePaint();
456 DrawGraphBackground(canvas, &paint, area);
458 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
459 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
460 top + kPadding + 2 * kFontHeight);
461 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
462 top + 2 * kPadding + 3 * kFontHeight);
464 paint.setColor(DebugColors::MemoryDisplayTextColor());
465 DrawText(canvas,
466 &paint,
467 "GPU memory",
468 SkPaint::kLeft_Align,
469 kFontHeight,
470 title_pos);
472 std::string text =
473 base::StringPrintf("%6.1f MB used",
474 (memory_entry_.bytes_unreleasable +
475 memory_entry_.bytes_allocated) / megabyte);
476 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
478 if (memory_entry_.bytes_over) {
479 paint.setColor(SK_ColorRED);
480 text = base::StringPrintf("%6.1f MB over",
481 memory_entry_.bytes_over / megabyte);
482 } else {
483 text = base::StringPrintf("%6.1f MB max ",
484 memory_entry_.total_budget_in_bytes / megabyte);
486 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
488 return area;
491 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
492 SkCanvas* canvas,
493 const PaintTimeCounter* paint_time_counter,
494 int right,
495 int top) const {
496 const int kPadding = 4;
497 const int kFontHeight = 15;
499 const int kGraphWidth = paint_time_counter->HistorySize();
500 const int kGraphHeight = 40;
502 const int width = kGraphWidth + 2 * kPadding;
503 const int height =
504 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
505 const int left = bounds().width() - width - right;
507 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
509 SkPaint paint = CreatePaint();
510 DrawGraphBackground(canvas, &paint, area);
512 SkRect text_bounds = SkRect::MakeXYWH(
513 left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
514 SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
515 text_bounds.bottom() + kPadding,
516 kGraphWidth,
517 kFontHeight);
518 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
519 text_bounds2.bottom() + 2 * kPadding,
520 kGraphWidth,
521 kGraphHeight);
523 const std::string value_text =
524 base::StringPrintf("%.1f", paint_time_graph_.value);
525 const std::string min_max_text = base::StringPrintf(
526 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
528 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
529 DrawText(canvas,
530 &paint,
531 "Page paint time (ms)",
532 SkPaint::kLeft_Align,
533 kFontHeight,
534 text_bounds.left(),
535 text_bounds.bottom());
536 DrawText(canvas,
537 &paint,
538 value_text,
539 SkPaint::kLeft_Align,
540 kFontHeight,
541 text_bounds2.left(),
542 text_bounds2.bottom());
543 DrawText(canvas,
544 &paint,
545 min_max_text,
546 SkPaint::kRight_Align,
547 kFontHeight,
548 text_bounds2.right(),
549 text_bounds2.bottom());
551 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
552 for (PaintTimeCounter::RingBufferType::Iterator it =
553 paint_time_counter->End();
555 --it) {
556 double pt = it->InMillisecondsF();
558 if (pt == 0.0)
559 continue;
561 double p = pt / paint_time_graph_.current_upper_bound;
562 if (p > 1.0)
563 p = 1.0;
565 canvas->drawRect(
566 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
567 graph_bounds.bottom() - p * graph_bounds.height(),
569 p * graph_bounds.height()),
570 paint);
573 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
575 return area;
578 void HeadsUpDisplayLayerImpl::DrawDebugRects(
579 SkCanvas* canvas,
580 DebugRectHistory* debug_rect_history) const {
581 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
582 float rect_scale = 1.f / layer_tree_impl()->device_scale_factor();
583 SkPaint paint = CreatePaint();
585 canvas->save();
586 canvas->scale(rect_scale, rect_scale);
588 for (size_t i = 0; i < debug_rects.size(); ++i) {
589 SkColor stroke_color = 0;
590 SkColor fill_color = 0;
591 float stroke_width = 0.f;
592 std::string label_text;
594 switch (debug_rects[i].type) {
595 case PAINT_RECT_TYPE:
596 stroke_color = DebugColors::PaintRectBorderColor();
597 fill_color = DebugColors::PaintRectFillColor();
598 stroke_width = DebugColors::PaintRectBorderWidth(layer_tree_impl());
599 break;
600 case PROPERTY_CHANGED_RECT_TYPE:
601 stroke_color = DebugColors::PropertyChangedRectBorderColor();
602 fill_color = DebugColors::PropertyChangedRectFillColor();
603 stroke_width =
604 DebugColors::PropertyChangedRectBorderWidth(layer_tree_impl());
605 break;
606 case SURFACE_DAMAGE_RECT_TYPE:
607 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
608 fill_color = DebugColors::SurfaceDamageRectFillColor();
609 stroke_width =
610 DebugColors::SurfaceDamageRectBorderWidth(layer_tree_impl());
611 break;
612 case REPLICA_SCREEN_SPACE_RECT_TYPE:
613 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
614 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
615 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth(
616 layer_tree_impl());
617 break;
618 case SCREEN_SPACE_RECT_TYPE:
619 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
620 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
621 stroke_width =
622 DebugColors::ScreenSpaceLayerRectBorderWidth(layer_tree_impl());
623 break;
624 case OCCLUDING_RECT_TYPE:
625 stroke_color = DebugColors::OccludingRectBorderColor();
626 fill_color = DebugColors::OccludingRectFillColor();
627 stroke_width = DebugColors::OccludingRectBorderWidth(layer_tree_impl());
628 break;
629 case NONOCCLUDING_RECT_TYPE:
630 stroke_color = DebugColors::NonOccludingRectBorderColor();
631 fill_color = DebugColors::NonOccludingRectFillColor();
632 stroke_width =
633 DebugColors::NonOccludingRectBorderWidth(layer_tree_impl());
634 break;
635 case TOUCH_EVENT_HANDLER_RECT_TYPE:
636 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
637 fill_color = DebugColors::TouchEventHandlerRectFillColor();
638 stroke_width =
639 DebugColors::TouchEventHandlerRectBorderWidth(layer_tree_impl());
640 label_text = "touch event listener";
641 break;
642 case WHEEL_EVENT_HANDLER_RECT_TYPE:
643 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
644 fill_color = DebugColors::WheelEventHandlerRectFillColor();
645 stroke_width =
646 DebugColors::WheelEventHandlerRectBorderWidth(layer_tree_impl());
647 label_text = "mousewheel event listener";
648 break;
649 case NON_FAST_SCROLLABLE_RECT_TYPE:
650 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
651 fill_color = DebugColors::NonFastScrollableRectFillColor();
652 stroke_width =
653 DebugColors::NonFastScrollableRectBorderWidth(layer_tree_impl());
654 label_text = "repaints on scroll";
655 break;
658 const gfx::RectF& rect = debug_rects[i].rect;
659 SkRect sk_rect =
660 SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
661 paint.setColor(fill_color);
662 paint.setStyle(SkPaint::kFill_Style);
663 canvas->drawRect(sk_rect, paint);
665 paint.setColor(stroke_color);
666 paint.setStyle(SkPaint::kStroke_Style);
667 paint.setStrokeWidth(SkFloatToScalar(stroke_width));
668 canvas->drawRect(sk_rect, paint);
670 if (label_text.length()) {
671 const int kFontHeight = 12;
672 const int kPadding = 3;
673 const float device_scale_factor =
674 layer_tree_impl()->device_scale_factor();
676 canvas->save();
677 canvas->clipRect(sk_rect);
678 canvas->translate(sk_rect.x(), sk_rect.y());
679 canvas->scale(device_scale_factor, device_scale_factor);
681 SkPaint label_paint = CreatePaint();
682 label_paint.setTextSize(kFontHeight);
683 label_paint.setTypeface(typeface_.get());
684 label_paint.setColor(stroke_color);
686 const SkScalar label_text_width =
687 label_paint.measureText(label_text.c_str(), label_text.length());
688 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
689 kFontHeight + 2 * kPadding),
690 label_paint);
692 label_paint.setAntiAlias(true);
693 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
694 canvas->drawText(label_text.c_str(),
695 label_text.length(),
696 kPadding,
697 kFontHeight * 0.8f + kPadding,
698 label_paint);
700 canvas->restore();
704 canvas->restore();
707 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
708 return "cc::HeadsUpDisplayLayerImpl";
711 } // namespace cc