Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob496452250f61599ac25fbc66a464aca7c7c74fc4
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.
5 #include "cc/layers/heads_up_display_layer_impl.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/numerics/safe_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/trace_event.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "cc/debug/debug_colors.h"
15 #include "cc/debug/frame_rate_counter.h"
16 #include "cc/output/begin_frame_args.h"
17 #include "cc/output/renderer.h"
18 #include "cc/quads/texture_draw_quad.h"
19 #include "cc/resources/memory_history.h"
20 #include "cc/trees/layer_tree_host_impl.h"
21 #include "cc/trees/layer_tree_impl.h"
22 #include "skia/ext/platform_canvas.h"
23 #include "third_party/skia/include/core/SkPaint.h"
24 #include "third_party/skia/include/core/SkPath.h"
25 #include "third_party/skia/include/core/SkTypeface.h"
26 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/size.h"
29 #include "ui/gfx/geometry/size_conversions.h"
30 #include "ui/gfx/hud_font.h"
32 namespace cc {
34 static inline SkPaint CreatePaint() {
35 SkPaint paint;
36 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
37 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
38 // swizzle our colors when drawing to the SkCanvas.
39 SkColorMatrix swizzle_matrix;
40 for (int i = 0; i < 20; ++i)
41 swizzle_matrix.fMat[i] = 0;
42 swizzle_matrix.fMat[0 + 5 * 2] = 1;
43 swizzle_matrix.fMat[1 + 5 * 1] = 1;
44 swizzle_matrix.fMat[2 + 5 * 0] = 1;
45 swizzle_matrix.fMat[3 + 5 * 3] = 1;
47 skia::RefPtr<SkColorMatrixFilter> filter =
48 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
49 paint.setColorFilter(filter.get());
50 #endif
51 return paint;
54 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
55 double start_upper_bound)
56 : value(0.0),
57 min(0.0),
58 max(0.0),
59 current_upper_bound(start_upper_bound),
60 default_upper_bound(start_upper_bound),
61 indicator(indicator_value) {}
63 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
64 double target_upper_bound = std::max(max, default_upper_bound);
65 current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
66 return current_upper_bound;
69 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
70 int id)
71 : LayerImpl(tree_impl, id),
72 typeface_(gfx::GetHudTypeface()),
73 internal_contents_scale_(1.f),
74 fps_graph_(60.0, 80.0),
75 paint_time_graph_(16.0, 48.0),
76 fade_step_(0) {
77 if (!typeface_) {
78 typeface_ = skia::AdoptRef(
79 SkTypeface::CreateFromName("monospace", SkTypeface::kBold));
83 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
85 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
86 LayerTreeImpl* tree_impl) {
87 return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
90 void HeadsUpDisplayLayerImpl::AcquireResource(
91 ResourceProvider* resource_provider) {
92 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
93 it != resources_.end();
94 ++it) {
95 if (!resource_provider->InUseByConsumer((*it)->id())) {
96 resources_.swap(it, resources_.end() - 1);
97 return;
101 scoped_ptr<ScopedResource> resource =
102 ScopedResource::Create(resource_provider);
103 resource->Allocate(internal_content_bounds_,
104 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
105 resources_.push_back(resource.Pass());
108 class ResourceSizeIsEqualTo {
109 public:
110 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
111 : compare_size_(size_) {}
113 bool operator()(const ScopedResource* resource) {
114 return resource->size() == compare_size_;
117 private:
118 const gfx::Size compare_size_;
121 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
122 ResourceProvider* resource_provider) {
123 ScopedPtrVector<ScopedResource>::iterator it_erase =
124 resources_.partition(ResourceSizeIsEqualTo(internal_content_bounds_));
125 resources_.erase(it_erase, resources_.end());
128 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
129 ResourceProvider* resource_provider) {
130 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
131 return false;
133 internal_contents_scale_ = GetIdealContentsScale();
134 internal_content_bounds_ =
135 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_));
137 ReleaseUnmatchedSizeResources(resource_provider);
138 AcquireResource(resource_provider);
139 return LayerImpl::WillDraw(draw_mode, resource_provider);
142 void HeadsUpDisplayLayerImpl::AppendQuads(
143 RenderPass* render_pass,
144 AppendQuadsData* append_quads_data) {
145 if (!resources_.back()->id())
146 return;
148 SharedQuadState* shared_quad_state =
149 render_pass->CreateAndAppendSharedQuadState();
150 PopulateScaledSharedQuadState(shared_quad_state, internal_contents_scale_);
152 gfx::Rect quad_rect(internal_content_bounds_);
153 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
154 gfx::Rect visible_quad_rect(quad_rect);
155 bool premultiplied_alpha = true;
156 gfx::PointF uv_top_left(0.f, 0.f);
157 gfx::PointF uv_bottom_right(1.f, 1.f);
158 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
159 bool flipped = false;
160 bool nearest_neighbor = false;
161 TextureDrawQuad* quad =
162 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
163 quad->SetNew(shared_quad_state,
164 quad_rect,
165 opaque_rect,
166 visible_quad_rect,
167 resources_.back()->id(),
168 premultiplied_alpha,
169 uv_top_left,
170 uv_bottom_right,
171 SK_ColorTRANSPARENT,
172 vertex_opacity,
173 flipped,
174 nearest_neighbor);
175 ValidateQuadResources(quad);
178 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
179 DrawMode draw_mode,
180 ResourceProvider* resource_provider) {
181 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
182 return;
184 SkISize canvas_size;
185 if (hud_surface_)
186 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
187 else
188 canvas_size.set(0, 0);
190 if (canvas_size.width() != internal_content_bounds_.width() ||
191 canvas_size.height() != internal_content_bounds_.height() ||
192 !hud_surface_) {
193 TRACE_EVENT0("cc", "ResizeHudCanvas");
195 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
196 internal_content_bounds_.width(), internal_content_bounds_.height()));
199 UpdateHudContents();
202 TRACE_EVENT0("cc", "DrawHudContents");
203 hud_surface_->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
204 hud_surface_->getCanvas()->save();
205 hud_surface_->getCanvas()->scale(internal_contents_scale_,
206 internal_contents_scale_);
208 DrawHudContents(hud_surface_->getCanvas());
210 hud_surface_->getCanvas()->restore();
213 TRACE_EVENT0("cc", "UploadHudTexture");
214 SkImageInfo info;
215 size_t row_bytes = 0;
216 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
217 DCHECK(pixels);
218 DCHECK(info.colorType() == kN32_SkColorType);
219 resource_provider->CopyToResource(resources_.back()->id(),
220 static_cast<const uint8_t*>(pixels),
221 internal_content_bounds_);
224 void HeadsUpDisplayLayerImpl::ReleaseResources() {
225 resources_.clear();
228 gfx::Rect HeadsUpDisplayLayerImpl::GetEnclosingRectInTargetSpace() const {
229 DCHECK_GT(internal_contents_scale_, 0.f);
230 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_);
233 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
234 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
236 // Don't update numbers every frame so text is readable.
237 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
238 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
239 time_of_last_graph_update_ = now;
241 if (debug_state.show_fps_counter) {
242 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
243 fps_graph_.value = fps_counter->GetAverageFPS();
244 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
247 if (debug_state.ShowMemoryStats()) {
248 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
249 if (memory_history->End())
250 memory_entry_ = **memory_history->End();
251 else
252 memory_entry_ = MemoryHistory::Entry();
256 fps_graph_.UpdateUpperBound();
257 paint_time_graph_.UpdateUpperBound();
260 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
261 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
263 if (debug_state.ShowHudRects()) {
264 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
265 if (IsAnimatingHUDContents()) {
266 layer_tree_impl()->SetNeedsRedraw();
270 if (!debug_state.show_fps_counter)
271 return;
273 SkRect area =
274 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
275 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
276 SkMaxScalar(area.width(), 150));
278 if (debug_state.ShowMemoryStats())
279 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
281 int HeadsUpDisplayLayerImpl::MeasureText(SkPaint* paint,
282 const std::string& text,
283 int size) const {
284 const bool anti_alias = paint->isAntiAlias();
285 paint->setAntiAlias(true);
286 paint->setTextSize(size);
287 paint->setTypeface(typeface_.get());
288 SkScalar text_width = paint->measureText(text.c_str(), text.length());
290 paint->setAntiAlias(anti_alias);
291 return SkScalarCeilToInt(text_width);
293 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
294 SkPaint* paint,
295 const std::string& text,
296 SkPaint::Align align,
297 int size,
298 int x,
299 int y) const {
300 const bool anti_alias = paint->isAntiAlias();
301 paint->setAntiAlias(true);
303 paint->setTextSize(size);
304 paint->setTextAlign(align);
305 paint->setTypeface(typeface_.get());
306 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
308 paint->setAntiAlias(anti_alias);
311 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
312 SkPaint* paint,
313 const std::string& text,
314 SkPaint::Align align,
315 int size,
316 const SkPoint& pos) const {
317 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
320 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
321 SkPaint* paint,
322 const SkRect& bounds) const {
323 paint->setColor(DebugColors::HUDBackgroundColor());
324 canvas->drawRect(bounds, *paint);
327 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
328 SkPaint* paint,
329 const SkRect& bounds,
330 const Graph& graph) const {
331 // Draw top and bottom line.
332 paint->setColor(DebugColors::HUDSeparatorLineColor());
333 canvas->drawLine(bounds.left(),
334 bounds.top() - 1,
335 bounds.right(),
336 bounds.top() - 1,
337 *paint);
338 canvas->drawLine(
339 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
341 // Draw indicator line (additive blend mode to increase contrast when drawn on
342 // top of graph).
343 paint->setColor(DebugColors::HUDIndicatorLineColor());
344 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
345 const double indicator_top =
346 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
347 1.0;
348 canvas->drawLine(bounds.left(),
349 bounds.top() + indicator_top,
350 bounds.right(),
351 bounds.top() + indicator_top,
352 *paint);
353 paint->setXfermode(nullptr);
356 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
357 SkCanvas* canvas,
358 const FrameRateCounter* fps_counter,
359 int right,
360 int top) const {
361 const int kPadding = 4;
362 const int kGap = 6;
364 const int kFontHeight = 15;
366 const int kGraphWidth =
367 base::saturated_cast<int>(fps_counter->time_stamp_history_size()) - 2;
368 const int kGraphHeight = 40;
370 const int kHistogramWidth = 37;
372 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
373 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
374 int left = bounds().width() - width - right;
375 SkRect area = SkRect::MakeXYWH(left, top, width, height);
377 SkPaint paint = CreatePaint();
378 DrawGraphBackground(canvas, &paint, area);
380 SkRect text_bounds =
381 SkRect::MakeXYWH(left + kPadding,
382 top + kPadding,
383 kGraphWidth + kHistogramWidth + kGap + 2,
384 kFontHeight);
385 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
386 text_bounds.bottom() + 2 * kPadding,
387 kGraphWidth,
388 kGraphHeight);
389 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
390 graph_bounds.top(),
391 kHistogramWidth,
392 kGraphHeight);
394 const std::string value_text =
395 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
396 const std::string min_max_text =
397 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
399 VLOG(1) << value_text;
401 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
402 DrawText(canvas,
403 &paint,
404 value_text,
405 SkPaint::kLeft_Align,
406 kFontHeight,
407 text_bounds.left(),
408 text_bounds.bottom());
409 DrawText(canvas,
410 &paint,
411 min_max_text,
412 SkPaint::kRight_Align,
413 kFontHeight,
414 text_bounds.right(),
415 text_bounds.bottom());
417 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
419 // Collect graph and histogram data.
420 SkPath path;
422 const int kHistogramSize = 20;
423 double histogram[kHistogramSize] = { 1.0 };
424 double max_bucket_value = 1.0;
426 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
427 --it) {
428 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
430 // Skip this particular instantaneous frame rate if it is not likely to have
431 // been valid.
432 if (!fps_counter->IsBadFrameInterval(delta)) {
433 double fps = 1.0 / delta.InSecondsF();
435 // Clamp the FPS to the range we want to plot visually.
436 double p = fps / fps_graph_.current_upper_bound;
437 if (p > 1.0)
438 p = 1.0;
440 // Plot this data point.
441 SkPoint cur =
442 SkPoint::Make(graph_bounds.left() + it.index(),
443 graph_bounds.bottom() - p * graph_bounds.height());
444 if (path.isEmpty())
445 path.moveTo(cur);
446 else
447 path.lineTo(cur);
449 // Use the fps value to find the right bucket in the histogram.
450 int bucket_index = floor(p * (kHistogramSize - 1));
452 // Add the delta time to take the time spent at that fps rate into
453 // account.
454 histogram[bucket_index] += delta.InSecondsF();
455 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
459 // Draw FPS histogram.
460 paint.setColor(DebugColors::HUDSeparatorLineColor());
461 canvas->drawLine(histogram_bounds.left() - 1,
462 histogram_bounds.top() - 1,
463 histogram_bounds.left() - 1,
464 histogram_bounds.bottom() + 1,
465 paint);
466 canvas->drawLine(histogram_bounds.right() + 1,
467 histogram_bounds.top() - 1,
468 histogram_bounds.right() + 1,
469 histogram_bounds.bottom() + 1,
470 paint);
472 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
473 const double bar_height = histogram_bounds.height() / kHistogramSize;
475 for (int i = kHistogramSize - 1; i >= 0; --i) {
476 if (histogram[i] > 0) {
477 double bar_width =
478 histogram[i] / max_bucket_value * histogram_bounds.width();
479 canvas->drawRect(
480 SkRect::MakeXYWH(histogram_bounds.left(),
481 histogram_bounds.bottom() - (i + 1) * bar_height,
482 bar_width,
484 paint);
488 // Draw FPS graph.
489 paint.setAntiAlias(true);
490 paint.setStyle(SkPaint::kStroke_Style);
491 paint.setStrokeWidth(1);
492 canvas->drawPath(path, paint);
494 return area;
497 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
498 int right,
499 int top,
500 int width) const {
501 if (!memory_entry_.total_bytes_used)
502 return SkRect::MakeEmpty();
504 const int kPadding = 4;
505 const int kFontHeight = 13;
507 const int height = 3 * kFontHeight + 4 * kPadding;
508 const int left = bounds().width() - width - right;
509 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
511 const double kMegabyte = 1024.0 * 1024.0;
513 SkPaint paint = CreatePaint();
514 DrawGraphBackground(canvas, &paint, area);
516 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
517 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
518 top + kPadding + 2 * kFontHeight);
519 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
520 top + 2 * kPadding + 3 * kFontHeight);
522 paint.setColor(DebugColors::MemoryDisplayTextColor());
523 DrawText(canvas,
524 &paint,
525 "GPU memory",
526 SkPaint::kLeft_Align,
527 kFontHeight,
528 title_pos);
530 std::string text = base::StringPrintf(
531 "%6.1f MB used", memory_entry_.total_bytes_used / kMegabyte);
532 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
534 if (!memory_entry_.had_enough_memory)
535 paint.setColor(SK_ColorRED);
536 text = base::StringPrintf("%6.1f MB max ",
537 memory_entry_.total_budget_in_bytes / kMegabyte);
538 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
540 return area;
543 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
544 int right,
545 int top,
546 int width) const {
547 std::string status;
548 SkColor color = SK_ColorRED;
549 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
550 case GpuRasterizationStatus::ON:
551 status = "on";
552 color = SK_ColorGREEN;
553 break;
554 case GpuRasterizationStatus::ON_FORCED:
555 status = "on (forced)";
556 color = SK_ColorGREEN;
557 break;
558 case GpuRasterizationStatus::OFF_DEVICE:
559 status = "off (device)";
560 color = SK_ColorRED;
561 break;
562 case GpuRasterizationStatus::OFF_VIEWPORT:
563 status = "off (viewport)";
564 color = SK_ColorYELLOW;
565 break;
566 case GpuRasterizationStatus::MSAA_CONTENT:
567 status = "MSAA (content)";
568 color = SK_ColorCYAN;
569 break;
570 case GpuRasterizationStatus::OFF_CONTENT:
571 status = "off (content)";
572 color = SK_ColorYELLOW;
573 break;
576 if (status.empty())
577 return SkRect::MakeEmpty();
579 const int kPadding = 4;
580 const int kFontHeight = 13;
582 const int height = 2 * kFontHeight + 3 * kPadding;
583 const int left = bounds().width() - width - right;
584 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
586 SkPaint paint = CreatePaint();
587 DrawGraphBackground(canvas, &paint, area);
589 SkPoint gpu_status_pos = SkPoint::Make(left + width - kPadding,
590 top + 2 * kFontHeight + 2 * kPadding);
592 paint.setColor(color);
593 DrawText(canvas, &paint, "GPU raster: ", SkPaint::kLeft_Align, kFontHeight,
594 left + kPadding, top + kFontHeight + kPadding);
595 DrawText(canvas, &paint, status, SkPaint::kRight_Align, kFontHeight,
596 gpu_status_pos);
598 return area;
601 void HeadsUpDisplayLayerImpl::DrawDebugRect(
602 SkCanvas* canvas,
603 SkPaint* paint,
604 const DebugRect& rect,
605 SkColor stroke_color,
606 SkColor fill_color,
607 float stroke_width,
608 const std::string& label_text) const {
609 gfx::Rect debug_layer_rect =
610 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
611 1.0 / internal_contents_scale_);
612 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
613 paint->setColor(fill_color);
614 paint->setStyle(SkPaint::kFill_Style);
615 canvas->drawIRect(sk_rect, *paint);
617 paint->setColor(stroke_color);
618 paint->setStyle(SkPaint::kStroke_Style);
619 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
620 canvas->drawIRect(sk_rect, *paint);
622 if (label_text.length()) {
623 const int kFontHeight = 12;
624 const int kPadding = 3;
626 // The debug_layer_rect may be huge, and converting to a floating point may
627 // be lossy, so intersect with the HUD layer bounds first to prevent that.
628 gfx::Rect clip_rect = debug_layer_rect;
629 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
630 SkRect sk_clip_rect = RectToSkRect(clip_rect);
632 canvas->save();
633 canvas->clipRect(sk_clip_rect);
634 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
636 SkPaint label_paint = CreatePaint();
637 label_paint.setTextSize(kFontHeight);
638 label_paint.setTypeface(typeface_.get());
639 label_paint.setColor(stroke_color);
641 const SkScalar label_text_width =
642 label_paint.measureText(label_text.c_str(), label_text.length());
643 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
644 kFontHeight + 2 * kPadding),
645 label_paint);
647 label_paint.setAntiAlias(true);
648 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
649 canvas->drawText(label_text.c_str(),
650 label_text.length(),
651 kPadding,
652 kFontHeight * 0.8f + kPadding,
653 label_paint);
655 canvas->restore();
659 void HeadsUpDisplayLayerImpl::DrawDebugRects(
660 SkCanvas* canvas,
661 DebugRectHistory* debug_rect_history) {
662 SkPaint paint = CreatePaint();
664 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
665 std::vector<DebugRect> new_paint_rects;
667 for (size_t i = 0; i < debug_rects.size(); ++i) {
668 SkColor stroke_color = 0;
669 SkColor fill_color = 0;
670 float stroke_width = 0.f;
671 std::string label_text;
673 switch (debug_rects[i].type) {
674 case PAINT_RECT_TYPE:
675 new_paint_rects.push_back(debug_rects[i]);
676 continue;
677 case PROPERTY_CHANGED_RECT_TYPE:
678 stroke_color = DebugColors::PropertyChangedRectBorderColor();
679 fill_color = DebugColors::PropertyChangedRectFillColor();
680 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
681 break;
682 case SURFACE_DAMAGE_RECT_TYPE:
683 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
684 fill_color = DebugColors::SurfaceDamageRectFillColor();
685 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
686 break;
687 case REPLICA_SCREEN_SPACE_RECT_TYPE:
688 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
689 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
690 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
691 break;
692 case SCREEN_SPACE_RECT_TYPE:
693 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
694 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
695 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
696 break;
697 case TOUCH_EVENT_HANDLER_RECT_TYPE:
698 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
699 fill_color = DebugColors::TouchEventHandlerRectFillColor();
700 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
701 label_text = "touch event listener";
702 break;
703 case WHEEL_EVENT_HANDLER_RECT_TYPE:
704 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
705 fill_color = DebugColors::WheelEventHandlerRectFillColor();
706 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
707 label_text = "mousewheel event listener";
708 break;
709 case SCROLL_EVENT_HANDLER_RECT_TYPE:
710 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
711 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
712 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
713 label_text = "scroll event listener";
714 break;
715 case NON_FAST_SCROLLABLE_RECT_TYPE:
716 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
717 fill_color = DebugColors::NonFastScrollableRectFillColor();
718 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
719 label_text = "repaints on scroll";
720 break;
721 case ANIMATION_BOUNDS_RECT_TYPE:
722 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
723 fill_color = DebugColors::LayerAnimationBoundsFillColor();
724 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
725 label_text = "animation bounds";
726 break;
729 DrawDebugRect(canvas,
730 &paint,
731 debug_rects[i],
732 stroke_color,
733 fill_color,
734 stroke_width,
735 label_text);
738 if (new_paint_rects.size()) {
739 paint_rects_.swap(new_paint_rects);
740 fade_step_ = DebugColors::kFadeSteps;
742 if (fade_step_ > 0) {
743 fade_step_--;
744 for (size_t i = 0; i < paint_rects_.size(); ++i) {
745 DrawDebugRect(canvas,
746 &paint,
747 paint_rects_[i],
748 DebugColors::PaintRectBorderColor(fade_step_),
749 DebugColors::PaintRectFillColor(fade_step_),
750 DebugColors::PaintRectBorderWidth(),
751 "");
756 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
757 return "cc::HeadsUpDisplayLayerImpl";
760 void HeadsUpDisplayLayerImpl::AsValueInto(
761 base::trace_event::TracedValue* dict) const {
762 LayerImpl::AsValueInto(dict);
763 dict->SetString("layer_name", "Heads Up Display Layer");
766 } // namespace cc