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