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