Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / cc / layers / heads_up_display_layer_impl.cc
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
30 namespace cc {
31
32 static 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
52 HeadsUpDisplayLayerImpl::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
61 double 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
67 HeadsUpDisplayLayerImpl::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
76 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
77
78 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
79     LayerTreeImpl* tree_impl) {
80   return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
81 }
82
83 bool 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
108 void 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
139 void 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
186 void HeadsUpDisplayLayerImpl::ReleaseResources() { hud_resource_.reset(); }
187
188 void 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
229 void 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
254 void 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
272 void 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
281 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
282                                                   SkPaint* paint,
283                                                   const SkRect& bounds) const {
284   paint->setColor(DebugColors::HUDBackgroundColor());
285   canvas->drawRect(bounds, *paint);
286 }
287
288 void 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
317 SkRect 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
455 SkRect 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
507 SkRect 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
594 void 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::RectF debug_layer_rect = gfx::ScaleRect(
603       rect.rect, 1.0 / contents_scale_x(), 1.0 / contents_scale_y());
604   SkRect sk_rect = RectFToSkRect(debug_layer_rect);
605   paint.setColor(fill_color);
606   paint.setStyle(SkPaint::kFill_Style);
607   canvas->drawRect(sk_rect, paint);
608
609   paint.setColor(stroke_color);
610   paint.setStyle(SkPaint::kStroke_Style);
611   paint.setStrokeWidth(SkFloatToScalar(stroke_width));
612   canvas->drawRect(sk_rect, paint);
613
614   if (label_text.length()) {
615     const int kFontHeight = 12;
616     const int kPadding = 3;
617
618     canvas->save();
619     canvas->clipRect(sk_rect);
620     canvas->translate(sk_rect.x(), sk_rect.y());
621
622     SkPaint label_paint = CreatePaint();
623     label_paint.setTextSize(kFontHeight);
624     label_paint.setTypeface(typeface_.get());
625     label_paint.setColor(stroke_color);
626
627     const SkScalar label_text_width =
628         label_paint.measureText(label_text.c_str(), label_text.length());
629     canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
630                                     kFontHeight + 2 * kPadding),
631                      label_paint);
632
633     label_paint.setAntiAlias(true);
634     label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
635     canvas->drawText(label_text.c_str(),
636                      label_text.length(),
637                      kPadding,
638                      kFontHeight * 0.8f + kPadding,
639                      label_paint);
640
641     canvas->restore();
642   }
643 }
644
645 void HeadsUpDisplayLayerImpl::DrawDebugRects(
646     SkCanvas* canvas,
647     DebugRectHistory* debug_rect_history) {
648   SkPaint paint = CreatePaint();
649
650   const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
651   std::vector<DebugRect> new_paint_rects;
652
653   for (size_t i = 0; i < debug_rects.size(); ++i) {
654     SkColor stroke_color = 0;
655     SkColor fill_color = 0;
656     float stroke_width = 0.f;
657     std::string label_text;
658
659     switch (debug_rects[i].type) {
660       case PAINT_RECT_TYPE:
661         new_paint_rects.push_back(debug_rects[i]);
662         continue;
663       case PROPERTY_CHANGED_RECT_TYPE:
664         stroke_color = DebugColors::PropertyChangedRectBorderColor();
665         fill_color = DebugColors::PropertyChangedRectFillColor();
666         stroke_width = DebugColors::PropertyChangedRectBorderWidth();
667         break;
668       case SURFACE_DAMAGE_RECT_TYPE:
669         stroke_color = DebugColors::SurfaceDamageRectBorderColor();
670         fill_color = DebugColors::SurfaceDamageRectFillColor();
671         stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
672         break;
673       case REPLICA_SCREEN_SPACE_RECT_TYPE:
674         stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
675         fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
676         stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
677         break;
678       case SCREEN_SPACE_RECT_TYPE:
679         stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
680         fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
681         stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
682         break;
683       case OCCLUDING_RECT_TYPE:
684         stroke_color = DebugColors::OccludingRectBorderColor();
685         fill_color = DebugColors::OccludingRectFillColor();
686         stroke_width = DebugColors::OccludingRectBorderWidth();
687         break;
688       case NONOCCLUDING_RECT_TYPE:
689         stroke_color = DebugColors::NonOccludingRectBorderColor();
690         fill_color = DebugColors::NonOccludingRectFillColor();
691         stroke_width = DebugColors::NonOccludingRectBorderWidth();
692         break;
693       case TOUCH_EVENT_HANDLER_RECT_TYPE:
694         stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
695         fill_color = DebugColors::TouchEventHandlerRectFillColor();
696         stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
697         label_text = "touch event listener";
698         break;
699       case WHEEL_EVENT_HANDLER_RECT_TYPE:
700         stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
701         fill_color = DebugColors::WheelEventHandlerRectFillColor();
702         stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
703         label_text = "mousewheel event listener";
704         break;
705       case NON_FAST_SCROLLABLE_RECT_TYPE:
706         stroke_color = DebugColors::NonFastScrollableRectBorderColor();
707         fill_color = DebugColors::NonFastScrollableRectFillColor();
708         stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
709         label_text = "repaints on scroll";
710         break;
711       case ANIMATION_BOUNDS_RECT_TYPE:
712         stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
713         fill_color = DebugColors::LayerAnimationBoundsFillColor();
714         stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
715         label_text = "animation bounds";
716         break;
717     }
718
719     DrawDebugRect(canvas,
720                   paint,
721                   debug_rects[i],
722                   stroke_color,
723                   fill_color,
724                   stroke_width,
725                   label_text);
726   }
727
728   if (new_paint_rects.size()) {
729     paint_rects_.swap(new_paint_rects);
730     fade_step_ = DebugColors::kFadeSteps;
731   }
732   if (fade_step_ > 0) {
733     fade_step_--;
734     for (size_t i = 0; i < paint_rects_.size(); ++i) {
735       DrawDebugRect(canvas,
736                     paint,
737                     paint_rects_[i],
738                     DebugColors::PaintRectBorderColor(fade_step_),
739                     DebugColors::PaintRectFillColor(fade_step_),
740                     DebugColors::PaintRectBorderWidth(),
741                     "");
742     }
743   }
744 }
745
746 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
747   return "cc::HeadsUpDisplayLayerImpl";
748 }
749
750 void HeadsUpDisplayLayerImpl::AsValueInto(base::DictionaryValue* dict) const {
751   LayerImpl::AsValueInto(dict);
752   dict->SetString("layer_name", "Heads Up Display Layer");
753 }
754
755 }  // namespace cc