heads_up_display_layer_impl.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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.
4
5#include "cc/layers/heads_up_display_layer_impl.h"
6
7#include <algorithm>
8#include <vector>
9
10#include "base/strings/stringprintf.h"
11#include "cc/debug/debug_colors.h"
12#include "cc/debug/debug_rect_history.h"
13#include "cc/debug/frame_rate_counter.h"
14#include "cc/debug/paint_time_counter.h"
15#include "cc/layers/quad_sink.h"
16#include "cc/output/renderer.h"
17#include "cc/quads/texture_draw_quad.h"
18#include "cc/resources/memory_history.h"
19#include "cc/trees/layer_tree_impl.h"
20#include "skia/ext/platform_canvas.h"
21#include "third_party/khronos/GLES2/gl2.h"
22#include "third_party/khronos/GLES2/gl2ext.h"
23#include "third_party/skia/include/core/SkBitmap.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/point.h"
28#include "ui/gfx/size.h"
29
30namespace cc {
31
32static inline SkPaint CreatePaint() {
33  SkPaint paint;
34#if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
35  // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
36  // swizzle our colors when drawing to the SkCanvas.
37  SkColorMatrix swizzle_matrix;
38  for (int i = 0; i < 20; ++i)
39    swizzle_matrix.fMat[i] = 0;
40  swizzle_matrix.fMat[0 + 5 * 2] = 1;
41  swizzle_matrix.fMat[1 + 5 * 1] = 1;
42  swizzle_matrix.fMat[2 + 5 * 0] = 1;
43  swizzle_matrix.fMat[3 + 5 * 3] = 1;
44
45  skia::RefPtr<SkColorMatrixFilter> filter =
46      skia::AdoptRef(new SkColorMatrixFilter(swizzle_matrix));
47  paint.setColorFilter(filter.get());
48#endif
49  return paint;
50}
51
52HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
53                                      double start_upper_bound)
54    : value(0.0),
55      min(0.0),
56      max(0.0),
57      current_upper_bound(start_upper_bound),
58      default_upper_bound(start_upper_bound),
59      indicator(indicator_value) {}
60
61double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
62  double target_upper_bound = std::max(max, default_upper_bound);
63  current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
64  return current_upper_bound;
65}
66
67HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
68                                                 int id)
69    : LayerImpl(tree_impl, id),
70      typeface_(skia::AdoptRef(
71          SkTypeface::CreateFromName("monospace", SkTypeface::kBold))),
72      fps_graph_(60.0, 80.0),
73      paint_time_graph_(16.0, 48.0) {}
74
75HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
76
77scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
78    LayerTreeImpl* tree_impl) {
79  return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
80}
81
82bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
83                                       ResourceProvider* resource_provider) {
84  if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
85    return false;
86
87  if (!hud_resource_)
88    hud_resource_ = ScopedResource::create(resource_provider);
89
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      resource_provider->InUseByConsumer(hud_resource_->id()))
94    hud_resource_->Free();
95
96  if (!hud_resource_->id()) {
97    hud_resource_->Allocate(
98        content_bounds(), GL_RGBA, ResourceProvider::TextureUsageAny);
99  }
100
101  return LayerImpl::WillDraw(draw_mode, resource_provider);
102}
103
104void HeadsUpDisplayLayerImpl::AppendQuads(QuadSink* quad_sink,
105                                          AppendQuadsData* append_quads_data) {
106  if (!hud_resource_->id())
107    return;
108
109  SharedQuadState* shared_quad_state =
110      quad_sink->UseSharedQuadState(CreateSharedQuadState());
111
112  gfx::Rect quad_rect(content_bounds());
113  gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
114  bool premultiplied_alpha = true;
115  gfx::PointF uv_top_left(0.f, 0.f);
116  gfx::PointF uv_bottom_right(1.f, 1.f);
117  const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
118  bool flipped = false;
119  scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
120  quad->SetNew(shared_quad_state,
121               quad_rect,
122               opaque_rect,
123               hud_resource_->id(),
124               premultiplied_alpha,
125               uv_top_left,
126               uv_bottom_right,
127               SK_ColorTRANSPARENT,
128               vertex_opacity,
129               flipped);
130  quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
131}
132
133void HeadsUpDisplayLayerImpl::UpdateHudTexture(
134    ResourceProvider* resource_provider) {
135  if (!hud_resource_->id())
136    return;
137
138  SkISize canvas_size;
139  if (hud_canvas_)
140    canvas_size = hud_canvas_->getDeviceSize();
141  else
142    canvas_size.set(0, 0);
143
144  if (canvas_size.width() != content_bounds().width() ||
145      canvas_size.width() != content_bounds().height() || !hud_canvas_) {
146    bool opaque = false;
147    hud_canvas_ = make_scoped_ptr(skia::CreateBitmapCanvas(
148        content_bounds().width(), content_bounds().height(), opaque));
149  }
150
151  UpdateHudContents();
152
153  hud_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
154  hud_canvas_->save();
155  hud_canvas_->scale(contents_scale_x(), contents_scale_y());
156
157  DrawHudContents(hud_canvas_.get());
158
159  hud_canvas_->restore();
160
161  const SkBitmap* bitmap = &hud_canvas_->getDevice()->accessBitmap(false);
162  SkAutoLockPixels locker(*bitmap);
163
164  gfx::Rect content_rect(content_bounds());
165  DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config);
166  resource_provider->SetPixels(hud_resource_->id(),
167                               static_cast<const uint8_t*>(bitmap->getPixels()),
168                               content_rect,
169                               content_rect,
170                               gfx::Vector2d());
171}
172
173void HeadsUpDisplayLayerImpl::DidLoseOutputSurface() { hud_resource_.reset(); }
174
175bool HeadsUpDisplayLayerImpl::LayerIsAlwaysDamaged() const { return true; }
176
177void HeadsUpDisplayLayerImpl::UpdateHudContents() {
178  const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
179
180  // Don't update numbers every frame so text is readable.
181  base::TimeTicks now = layer_tree_impl()->CurrentFrameTimeTicks();
182  if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
183    time_of_last_graph_update_ = now;
184
185    if (debug_state.show_fps_counter) {
186      FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
187      fps_graph_.value = fps_counter->GetAverageFPS();
188      fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
189    }
190
191    if (debug_state.continuous_painting) {
192      PaintTimeCounter* paint_time_counter =
193          layer_tree_impl()->paint_time_counter();
194      base::TimeDelta latest, min, max;
195
196      if (paint_time_counter->End())
197        latest = **paint_time_counter->End();
198      paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
199
200      paint_time_graph_.value = latest.InMillisecondsF();
201      paint_time_graph_.min = min.InMillisecondsF();
202      paint_time_graph_.max = max.InMillisecondsF();
203    }
204
205    if (debug_state.ShowMemoryStats()) {
206      MemoryHistory* memory_history = layer_tree_impl()->memory_history();
207      if (memory_history->End())
208        memory_entry_ = **memory_history->End();
209      else
210        memory_entry_ = MemoryHistory::Entry();
211    }
212  }
213
214  fps_graph_.UpdateUpperBound();
215  paint_time_graph_.UpdateUpperBound();
216}
217
218void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) const {
219  const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
220
221  if (debug_state.ShowHudRects())
222    DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
223
224  SkRect area = SkRect::MakeEmpty();
225  if (debug_state.continuous_painting) {
226    area = DrawPaintTimeDisplay(
227        canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
228  } else if (debug_state.show_fps_counter) {
229    // Don't show the FPS display when continuous painting is enabled, because
230    // it would show misleading numbers.
231    area =
232        DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
233  }
234
235  if (debug_state.ShowMemoryStats())
236    DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
237}
238
239void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
240                                       SkPaint* paint,
241                                       const std::string& text,
242                                       SkPaint::Align align,
243                                       int size,
244                                       int x,
245                                       int y) const {
246  const bool anti_alias = paint->isAntiAlias();
247  paint->setAntiAlias(true);
248
249  paint->setTextSize(size);
250  paint->setTextAlign(align);
251  paint->setTypeface(typeface_.get());
252  canvas->drawText(text.c_str(), text.length(), x, y, *paint);
253
254  paint->setAntiAlias(anti_alias);
255}
256
257void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
258                                       SkPaint* paint,
259                                       const std::string& text,
260                                       SkPaint::Align align,
261                                       int size,
262                                       const SkPoint& pos) const {
263  DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
264}
265
266void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
267                                                  SkPaint* paint,
268                                                  const SkRect& bounds) const {
269  paint->setColor(DebugColors::HUDBackgroundColor());
270  canvas->drawRect(bounds, *paint);
271}
272
273void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
274                                             SkPaint* paint,
275                                             const SkRect& bounds,
276                                             const Graph& graph) const {
277  // Draw top and bottom line.
278  paint->setColor(DebugColors::HUDSeparatorLineColor());
279  canvas->drawLine(bounds.left(),
280                   bounds.top() - 1,
281                   bounds.right(),
282                   bounds.top() - 1,
283                   *paint);
284  canvas->drawLine(
285      bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
286
287  // Draw indicator line (additive blend mode to increase contrast when drawn on
288  // top of graph).
289  paint->setColor(DebugColors::HUDIndicatorLineColor());
290  paint->setXfermodeMode(SkXfermode::kPlus_Mode);
291  const double indicator_top =
292      bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
293      1.0;
294  canvas->drawLine(bounds.left(),
295                   bounds.top() + indicator_top,
296                   bounds.right(),
297                   bounds.top() + indicator_top,
298                   *paint);
299  paint->setXfermode(NULL);
300}
301
302SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
303    SkCanvas* canvas,
304    const FrameRateCounter* fps_counter,
305    int right,
306    int top) const {
307  const int kPadding = 4;
308  const int kGap = 6;
309
310  const int kFontHeight = 15;
311
312  const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
313  const int kGraphHeight = 40;
314
315  const int kHistogramWidth = 37;
316
317  int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
318  int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
319  int left = bounds().width() - width - right;
320  SkRect area = SkRect::MakeXYWH(left, top, width, height);
321
322  SkPaint paint = CreatePaint();
323  DrawGraphBackground(canvas, &paint, area);
324
325  SkRect text_bounds =
326      SkRect::MakeXYWH(left + kPadding,
327                       top + kPadding,
328                       kGraphWidth + kHistogramWidth + kGap + 2,
329                       kFontHeight);
330  SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
331                                         text_bounds.bottom() + 2 * kPadding,
332                                         kGraphWidth,
333                                         kGraphHeight);
334  SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
335                                             graph_bounds.top(),
336                                             kHistogramWidth,
337                                             kGraphHeight);
338
339  const std::string value_text =
340      base::StringPrintf("FPS:%5.1f", fps_graph_.value);
341  const std::string min_max_text =
342      base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
343
344  paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
345  DrawText(canvas,
346           &paint,
347           value_text,
348           SkPaint::kLeft_Align,
349           kFontHeight,
350           text_bounds.left(),
351           text_bounds.bottom());
352  DrawText(canvas,
353           &paint,
354           min_max_text,
355           SkPaint::kRight_Align,
356           kFontHeight,
357           text_bounds.right(),
358           text_bounds.bottom());
359
360  DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
361
362  // Collect graph and histogram data.
363  SkPath path;
364
365  const int kHistogramSize = 20;
366  double histogram[kHistogramSize] = { 1.0 };
367  double max_bucket_value = 1.0;
368
369  for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
370       --it) {
371    base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
372
373    // Skip this particular instantaneous frame rate if it is not likely to have
374    // been valid.
375    if (!fps_counter->IsBadFrameInterval(delta)) {
376      double fps = 1.0 / delta.InSecondsF();
377
378      // Clamp the FPS to the range we want to plot visually.
379      double p = fps / fps_graph_.current_upper_bound;
380      if (p > 1.0)
381        p = 1.0;
382
383      // Plot this data point.
384      SkPoint cur =
385          SkPoint::Make(graph_bounds.left() + it.index(),
386                        graph_bounds.bottom() - p * graph_bounds.height());
387      if (path.isEmpty())
388        path.moveTo(cur);
389      else
390        path.lineTo(cur);
391
392      // Use the fps value to find the right bucket in the histogram.
393      int bucket_index = floor(p * (kHistogramSize - 1));
394
395      // Add the delta time to take the time spent at that fps rate into
396      // account.
397      histogram[bucket_index] += delta.InSecondsF();
398      max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
399    }
400  }
401
402  // Draw FPS histogram.
403  paint.setColor(DebugColors::HUDSeparatorLineColor());
404  canvas->drawLine(histogram_bounds.left() - 1,
405                   histogram_bounds.top() - 1,
406                   histogram_bounds.left() - 1,
407                   histogram_bounds.bottom() + 1,
408                   paint);
409  canvas->drawLine(histogram_bounds.right() + 1,
410                   histogram_bounds.top() - 1,
411                   histogram_bounds.right() + 1,
412                   histogram_bounds.bottom() + 1,
413                   paint);
414
415  paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
416  const double bar_height = histogram_bounds.height() / kHistogramSize;
417
418  for (int i = kHistogramSize - 1; i >= 0; --i) {
419    if (histogram[i] > 0) {
420      double bar_width =
421          histogram[i] / max_bucket_value * histogram_bounds.width();
422      canvas->drawRect(
423          SkRect::MakeXYWH(histogram_bounds.left(),
424                           histogram_bounds.bottom() - (i + 1) * bar_height,
425                           bar_width,
426                           1),
427          paint);
428    }
429  }
430
431  // Draw FPS graph.
432  paint.setAntiAlias(true);
433  paint.setStyle(SkPaint::kStroke_Style);
434  paint.setStrokeWidth(1);
435  canvas->drawPath(path, paint);
436
437  return area;
438}
439
440SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
441                                                  int right,
442                                                  int top,
443                                                  int width) const {
444  if (!memory_entry_.bytes_total())
445    return SkRect::MakeEmpty();
446
447  const int kPadding = 4;
448  const int kFontHeight = 13;
449
450  const int height = 3 * kFontHeight + 4 * kPadding;
451  const int left = bounds().width() - width - right;
452  const SkRect area = SkRect::MakeXYWH(left, top, width, height);
453
454  const double megabyte = 1024.0 * 1024.0;
455
456  SkPaint paint = CreatePaint();
457  DrawGraphBackground(canvas, &paint, area);
458
459  SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
460  SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
461                                    top + kPadding + 2 * kFontHeight);
462  SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
463                                    top + 2 * kPadding + 3 * kFontHeight);
464
465  paint.setColor(DebugColors::MemoryDisplayTextColor());
466  DrawText(canvas,
467           &paint,
468           "GPU memory",
469           SkPaint::kLeft_Align,
470           kFontHeight,
471           title_pos);
472
473  std::string text =
474      base::StringPrintf("%6.1f MB used",
475                         (memory_entry_.bytes_unreleasable +
476                          memory_entry_.bytes_allocated) / megabyte);
477  DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
478
479  if (memory_entry_.bytes_over) {
480    paint.setColor(SK_ColorRED);
481    text = base::StringPrintf("%6.1f MB over",
482                              memory_entry_.bytes_over / megabyte);
483  } else {
484    text = base::StringPrintf("%6.1f MB max ",
485                              memory_entry_.total_budget_in_bytes / megabyte);
486  }
487  DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
488
489  return area;
490}
491
492SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
493    SkCanvas* canvas,
494    const PaintTimeCounter* paint_time_counter,
495    int right,
496    int top) const {
497  const int kPadding = 4;
498  const int kFontHeight = 15;
499
500  const int kGraphWidth = paint_time_counter->HistorySize();
501  const int kGraphHeight = 40;
502
503  const int width = kGraphWidth + 2 * kPadding;
504  const int height =
505      kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
506  const int left = bounds().width() - width - right;
507
508  const SkRect area = SkRect::MakeXYWH(left, top, width, height);
509
510  SkPaint paint = CreatePaint();
511  DrawGraphBackground(canvas, &paint, area);
512
513  SkRect text_bounds = SkRect::MakeXYWH(
514      left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
515  SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
516                                         text_bounds.bottom() + kPadding,
517                                         kGraphWidth,
518                                         kFontHeight);
519  SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
520                                         text_bounds2.bottom() + 2 * kPadding,
521                                         kGraphWidth,
522                                         kGraphHeight);
523
524  const std::string value_text =
525      base::StringPrintf("%.1f", paint_time_graph_.value);
526  const std::string min_max_text = base::StringPrintf(
527      "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
528
529  paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
530  DrawText(canvas,
531           &paint,
532           "Page paint time (ms)",
533           SkPaint::kLeft_Align,
534           kFontHeight,
535           text_bounds.left(),
536           text_bounds.bottom());
537  DrawText(canvas,
538           &paint,
539           value_text,
540           SkPaint::kLeft_Align,
541           kFontHeight,
542           text_bounds2.left(),
543           text_bounds2.bottom());
544  DrawText(canvas,
545           &paint,
546           min_max_text,
547           SkPaint::kRight_Align,
548           kFontHeight,
549           text_bounds2.right(),
550           text_bounds2.bottom());
551
552  paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
553  for (PaintTimeCounter::RingBufferType::Iterator it =
554           paint_time_counter->End();
555       it;
556       --it) {
557    double pt = it->InMillisecondsF();
558
559    if (pt == 0.0)
560      continue;
561
562    double p = pt / paint_time_graph_.current_upper_bound;
563    if (p > 1.0)
564      p = 1.0;
565
566    canvas->drawRect(
567        SkRect::MakeXYWH(graph_bounds.left() + it.index(),
568                         graph_bounds.bottom() - p * graph_bounds.height(),
569                         1,
570                         p * graph_bounds.height()),
571        paint);
572  }
573
574  DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
575
576  return area;
577}
578
579void HeadsUpDisplayLayerImpl::DrawDebugRects(
580    SkCanvas* canvas,
581    DebugRectHistory* debug_rect_history) const {
582  const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
583  SkPaint paint = CreatePaint();
584
585  for (size_t i = 0; i < debug_rects.size(); ++i) {
586    SkColor stroke_color = 0;
587    SkColor fill_color = 0;
588    float stroke_width = 0.f;
589    std::string label_text;
590
591    switch (debug_rects[i].type) {
592      case PAINT_RECT_TYPE:
593        stroke_color = DebugColors::PaintRectBorderColor();
594        fill_color = DebugColors::PaintRectFillColor();
595        stroke_width = DebugColors::PaintRectBorderWidth();
596        break;
597      case PROPERTY_CHANGED_RECT_TYPE:
598        stroke_color = DebugColors::PropertyChangedRectBorderColor();
599        fill_color = DebugColors::PropertyChangedRectFillColor();
600        stroke_width = DebugColors::PropertyChangedRectBorderWidth();
601        break;
602      case SURFACE_DAMAGE_RECT_TYPE:
603        stroke_color = DebugColors::SurfaceDamageRectBorderColor();
604        fill_color = DebugColors::SurfaceDamageRectFillColor();
605        stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
606        break;
607      case REPLICA_SCREEN_SPACE_RECT_TYPE:
608        stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
609        fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
610        stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
611        break;
612      case SCREEN_SPACE_RECT_TYPE:
613        stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
614        fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
615        stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
616        break;
617      case OCCLUDING_RECT_TYPE:
618        stroke_color = DebugColors::OccludingRectBorderColor();
619        fill_color = DebugColors::OccludingRectFillColor();
620        stroke_width = DebugColors::OccludingRectBorderWidth();
621        break;
622      case NONOCCLUDING_RECT_TYPE:
623        stroke_color = DebugColors::NonOccludingRectBorderColor();
624        fill_color = DebugColors::NonOccludingRectFillColor();
625        stroke_width = DebugColors::NonOccludingRectBorderWidth();
626        break;
627      case TOUCH_EVENT_HANDLER_RECT_TYPE:
628        stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
629        fill_color = DebugColors::TouchEventHandlerRectFillColor();
630        stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
631        label_text = "touch event listener";
632        break;
633      case WHEEL_EVENT_HANDLER_RECT_TYPE:
634        stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
635        fill_color = DebugColors::WheelEventHandlerRectFillColor();
636        stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
637        label_text = "mousewheel event listener";
638        break;
639      case NON_FAST_SCROLLABLE_RECT_TYPE:
640        stroke_color = DebugColors::NonFastScrollableRectBorderColor();
641        fill_color = DebugColors::NonFastScrollableRectFillColor();
642        stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
643        label_text = "repaints on scroll";
644        break;
645    }
646
647    gfx::RectF debug_layer_rect = gfx::ScaleRect(debug_rects[i].rect,
648                                                 1.0 / contents_scale_x(),
649                                                 1.0 / contents_scale_y());
650    SkRect sk_rect = RectFToSkRect(debug_layer_rect);
651    paint.setColor(fill_color);
652    paint.setStyle(SkPaint::kFill_Style);
653    canvas->drawRect(sk_rect, paint);
654
655    paint.setColor(stroke_color);
656    paint.setStyle(SkPaint::kStroke_Style);
657    paint.setStrokeWidth(SkFloatToScalar(stroke_width));
658    canvas->drawRect(sk_rect, paint);
659
660    if (label_text.length()) {
661      const int kFontHeight = 12;
662      const int kPadding = 3;
663
664      canvas->save();
665      canvas->clipRect(sk_rect);
666      canvas->translate(sk_rect.x(), sk_rect.y());
667
668      SkPaint label_paint = CreatePaint();
669      label_paint.setTextSize(kFontHeight);
670      label_paint.setTypeface(typeface_.get());
671      label_paint.setColor(stroke_color);
672
673      const SkScalar label_text_width =
674          label_paint.measureText(label_text.c_str(), label_text.length());
675      canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
676                                      kFontHeight + 2 * kPadding),
677                       label_paint);
678
679      label_paint.setAntiAlias(true);
680      label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
681      canvas->drawText(label_text.c_str(),
682                       label_text.length(),
683                       kPadding,
684                       kFontHeight * 0.8f + kPadding,
685                       label_paint);
686
687      canvas->restore();
688    }
689  }
690}
691
692const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
693  return "cc::HeadsUpDisplayLayerImpl";
694}
695
696}  // namespace cc
697