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