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