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