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