ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / cc / layers / heads_up_display_layer_impl.cc
blob319dd0a86ed8a98e06d79a6cb300fee8aaf0f7a5
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/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/debug/debug_colors.h"
14 #include "cc/debug/frame_rate_counter.h"
15 #include "cc/debug/paint_time_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/khronos/GLES2/gl2.h"
24 #include "third_party/khronos/GLES2/gl2ext.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkTypeface.h"
28 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
29 #include "ui/gfx/geometry/point.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/geometry/size_conversions.h"
32 #include "ui/gfx/hud_font.h"
34 namespace cc {
36 static inline SkPaint CreatePaint() {
37 SkPaint paint;
38 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
39 // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
40 // swizzle our colors when drawing to the SkCanvas.
41 SkColorMatrix swizzle_matrix;
42 for (int i = 0; i < 20; ++i)
43 swizzle_matrix.fMat[i] = 0;
44 swizzle_matrix.fMat[0 + 5 * 2] = 1;
45 swizzle_matrix.fMat[1 + 5 * 1] = 1;
46 swizzle_matrix.fMat[2 + 5 * 0] = 1;
47 swizzle_matrix.fMat[3 + 5 * 3] = 1;
49 skia::RefPtr<SkColorMatrixFilter> filter =
50 skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
51 paint.setColorFilter(filter.get());
52 #endif
53 return paint;
56 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
57 double start_upper_bound)
58 : value(0.0),
59 min(0.0),
60 max(0.0),
61 current_upper_bound(start_upper_bound),
62 default_upper_bound(start_upper_bound),
63 indicator(indicator_value) {}
65 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
66 double target_upper_bound = std::max(max, default_upper_bound);
67 current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
68 return current_upper_bound;
71 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
72 int id)
73 : LayerImpl(tree_impl, id),
74 typeface_(gfx::GetHudTypeface()),
75 internal_contents_scale_(1.f),
76 fps_graph_(60.0, 80.0),
77 paint_time_graph_(16.0, 48.0),
78 fade_step_(0) {
79 if (!typeface_) {
80 typeface_ = skia::AdoptRef(
81 SkTypeface::CreateFromName("monospace", SkTypeface::kBold));
85 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
87 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
88 LayerTreeImpl* tree_impl) {
89 return HeadsUpDisplayLayerImpl::Create(tree_impl, id());
92 void HeadsUpDisplayLayerImpl::AcquireResource(
93 ResourceProvider* resource_provider) {
94 for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
95 it != resources_.end();
96 ++it) {
97 if (!resource_provider->InUseByConsumer((*it)->id())) {
98 resources_.swap(it, resources_.end() - 1);
99 return;
103 scoped_ptr<ScopedResource> resource =
104 ScopedResource::Create(resource_provider);
105 resource->Allocate(internal_content_bounds_,
106 ResourceProvider::TEXTURE_HINT_IMMUTABLE, RGBA_8888);
107 resources_.push_back(resource.Pass());
110 class ResourceSizeIsEqualTo {
111 public:
112 explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
113 : compare_size_(size_) {}
115 bool operator()(const ScopedResource* resource) {
116 return resource->size() == compare_size_;
119 private:
120 const gfx::Size compare_size_;
123 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
124 ResourceProvider* resource_provider) {
125 ScopedPtrVector<ScopedResource>::iterator it_erase =
126 resources_.partition(ResourceSizeIsEqualTo(internal_content_bounds_));
127 resources_.erase(it_erase, resources_.end());
130 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
131 ResourceProvider* resource_provider) {
132 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
133 return false;
135 internal_contents_scale_ = draw_properties().ideal_contents_scale;
136 internal_content_bounds_ =
137 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), internal_contents_scale_));
139 ReleaseUnmatchedSizeResources(resource_provider);
140 AcquireResource(resource_provider);
141 return LayerImpl::WillDraw(draw_mode, resource_provider);
144 void HeadsUpDisplayLayerImpl::AppendQuads(
145 RenderPass* render_pass,
146 AppendQuadsData* append_quads_data) {
147 if (!resources_.back()->id())
148 return;
150 SharedQuadState* shared_quad_state =
151 render_pass->CreateAndAppendSharedQuadState();
152 PopulateScaledSharedQuadState(shared_quad_state, internal_contents_scale_);
154 gfx::Rect quad_rect(internal_content_bounds_);
155 gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
156 gfx::Rect visible_quad_rect(quad_rect);
157 bool premultiplied_alpha = true;
158 gfx::PointF uv_top_left(0.f, 0.f);
159 gfx::PointF uv_bottom_right(1.f, 1.f);
160 const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
161 bool flipped = false;
162 bool nearest_neighbor = false;
163 TextureDrawQuad* quad =
164 render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
165 quad->SetNew(shared_quad_state,
166 quad_rect,
167 opaque_rect,
168 visible_quad_rect,
169 resources_.back()->id(),
170 premultiplied_alpha,
171 uv_top_left,
172 uv_bottom_right,
173 SK_ColorTRANSPARENT,
174 vertex_opacity,
175 flipped,
176 nearest_neighbor);
179 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
180 DrawMode draw_mode,
181 ResourceProvider* resource_provider) {
182 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
183 return;
185 SkISize canvas_size;
186 if (hud_surface_)
187 canvas_size = hud_surface_->getCanvas()->getDeviceSize();
188 else
189 canvas_size.set(0, 0);
191 if (canvas_size.width() != internal_content_bounds_.width() ||
192 canvas_size.height() != internal_content_bounds_.height() ||
193 !hud_surface_) {
194 TRACE_EVENT0("cc", "ResizeHudCanvas");
196 hud_surface_ = skia::AdoptRef(SkSurface::NewRasterN32Premul(
197 internal_content_bounds_.width(), internal_content_bounds_.height()));
200 UpdateHudContents();
203 TRACE_EVENT0("cc", "DrawHudContents");
204 hud_surface_->getCanvas()->clear(SkColorSetARGB(0, 0, 0, 0));
205 hud_surface_->getCanvas()->save();
206 hud_surface_->getCanvas()->scale(internal_contents_scale_,
207 internal_contents_scale_);
209 DrawHudContents(hud_surface_->getCanvas());
211 hud_surface_->getCanvas()->restore();
214 TRACE_EVENT0("cc", "UploadHudTexture");
215 SkImageInfo info;
216 size_t row_bytes = 0;
217 const void* pixels = hud_surface_->getCanvas()->peekPixels(&info, &row_bytes);
218 DCHECK(pixels);
219 DCHECK(info.colorType() == kN32_SkColorType);
220 resource_provider->CopyToResource(resources_.back()->id(),
221 static_cast<const uint8_t*>(pixels),
222 internal_content_bounds_);
225 void HeadsUpDisplayLayerImpl::ReleaseResources() {
226 resources_.clear();
229 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
230 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
232 // Don't update numbers every frame so text is readable.
233 base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
234 if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
235 time_of_last_graph_update_ = now;
237 if (debug_state.show_fps_counter) {
238 FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
239 fps_graph_.value = fps_counter->GetAverageFPS();
240 fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
243 if (debug_state.continuous_painting) {
244 PaintTimeCounter* paint_time_counter =
245 layer_tree_impl()->paint_time_counter();
246 base::TimeDelta latest, min, max;
248 if (paint_time_counter->End())
249 latest = **paint_time_counter->End();
250 paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
252 paint_time_graph_.value = latest.InMillisecondsF();
253 paint_time_graph_.min = min.InMillisecondsF();
254 paint_time_graph_.max = max.InMillisecondsF();
257 if (debug_state.ShowMemoryStats()) {
258 MemoryHistory* memory_history = layer_tree_impl()->memory_history();
259 if (memory_history->End())
260 memory_entry_ = **memory_history->End();
261 else
262 memory_entry_ = MemoryHistory::Entry();
266 fps_graph_.UpdateUpperBound();
267 paint_time_graph_.UpdateUpperBound();
270 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
271 const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
273 if (debug_state.ShowHudRects()) {
274 DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
275 if (IsAnimatingHUDContents()) {
276 layer_tree_impl()->SetNeedsRedraw();
280 SkRect area = SkRect::MakeEmpty();
281 if (debug_state.continuous_painting) {
282 area = DrawPaintTimeDisplay(
283 canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
284 } else if (debug_state.show_fps_counter) {
285 // Don't show the FPS display when continuous painting is enabled, because
286 // it would show misleading numbers.
287 area =
288 DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
291 if (debug_state.show_fps_counter || debug_state.continuous_painting) {
292 area = DrawGpuRasterizationStatus(canvas, 0, area.bottom(),
293 SkMaxScalar(area.width(), 150));
296 if (debug_state.ShowMemoryStats())
297 DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
300 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
301 SkPaint* paint,
302 const std::string& text,
303 SkPaint::Align align,
304 int size,
305 int x,
306 int y) const {
307 const bool anti_alias = paint->isAntiAlias();
308 paint->setAntiAlias(true);
310 paint->setTextSize(size);
311 paint->setTextAlign(align);
312 paint->setTypeface(typeface_.get());
313 canvas->drawText(text.c_str(), text.length(), x, y, *paint);
315 paint->setAntiAlias(anti_alias);
318 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
319 SkPaint* paint,
320 const std::string& text,
321 SkPaint::Align align,
322 int size,
323 const SkPoint& pos) const {
324 DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
327 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
328 SkPaint* paint,
329 const SkRect& bounds) const {
330 paint->setColor(DebugColors::HUDBackgroundColor());
331 canvas->drawRect(bounds, *paint);
334 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
335 SkPaint* paint,
336 const SkRect& bounds,
337 const Graph& graph) const {
338 // Draw top and bottom line.
339 paint->setColor(DebugColors::HUDSeparatorLineColor());
340 canvas->drawLine(bounds.left(),
341 bounds.top() - 1,
342 bounds.right(),
343 bounds.top() - 1,
344 *paint);
345 canvas->drawLine(
346 bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
348 // Draw indicator line (additive blend mode to increase contrast when drawn on
349 // top of graph).
350 paint->setColor(DebugColors::HUDIndicatorLineColor());
351 paint->setXfermodeMode(SkXfermode::kPlus_Mode);
352 const double indicator_top =
353 bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
354 1.0;
355 canvas->drawLine(bounds.left(),
356 bounds.top() + indicator_top,
357 bounds.right(),
358 bounds.top() + indicator_top,
359 *paint);
360 paint->setXfermode(nullptr);
363 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
364 SkCanvas* canvas,
365 const FrameRateCounter* fps_counter,
366 int right,
367 int top) const {
368 const int kPadding = 4;
369 const int kGap = 6;
371 const int kFontHeight = 15;
373 const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
374 const int kGraphHeight = 40;
376 const int kHistogramWidth = 37;
378 int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
379 int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
380 int left = bounds().width() - width - right;
381 SkRect area = SkRect::MakeXYWH(left, top, width, height);
383 SkPaint paint = CreatePaint();
384 DrawGraphBackground(canvas, &paint, area);
386 SkRect text_bounds =
387 SkRect::MakeXYWH(left + kPadding,
388 top + kPadding,
389 kGraphWidth + kHistogramWidth + kGap + 2,
390 kFontHeight);
391 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
392 text_bounds.bottom() + 2 * kPadding,
393 kGraphWidth,
394 kGraphHeight);
395 SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
396 graph_bounds.top(),
397 kHistogramWidth,
398 kGraphHeight);
400 const std::string value_text =
401 base::StringPrintf("FPS:%5.1f", fps_graph_.value);
402 const std::string min_max_text =
403 base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
405 VLOG(1) << value_text;
407 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
408 DrawText(canvas,
409 &paint,
410 value_text,
411 SkPaint::kLeft_Align,
412 kFontHeight,
413 text_bounds.left(),
414 text_bounds.bottom());
415 DrawText(canvas,
416 &paint,
417 min_max_text,
418 SkPaint::kRight_Align,
419 kFontHeight,
420 text_bounds.right(),
421 text_bounds.bottom());
423 DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
425 // Collect graph and histogram data.
426 SkPath path;
428 const int kHistogramSize = 20;
429 double histogram[kHistogramSize] = { 1.0 };
430 double max_bucket_value = 1.0;
432 for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
433 --it) {
434 base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
436 // Skip this particular instantaneous frame rate if it is not likely to have
437 // been valid.
438 if (!fps_counter->IsBadFrameInterval(delta)) {
439 double fps = 1.0 / delta.InSecondsF();
441 // Clamp the FPS to the range we want to plot visually.
442 double p = fps / fps_graph_.current_upper_bound;
443 if (p > 1.0)
444 p = 1.0;
446 // Plot this data point.
447 SkPoint cur =
448 SkPoint::Make(graph_bounds.left() + it.index(),
449 graph_bounds.bottom() - p * graph_bounds.height());
450 if (path.isEmpty())
451 path.moveTo(cur);
452 else
453 path.lineTo(cur);
455 // Use the fps value to find the right bucket in the histogram.
456 int bucket_index = floor(p * (kHistogramSize - 1));
458 // Add the delta time to take the time spent at that fps rate into
459 // account.
460 histogram[bucket_index] += delta.InSecondsF();
461 max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
465 // Draw FPS histogram.
466 paint.setColor(DebugColors::HUDSeparatorLineColor());
467 canvas->drawLine(histogram_bounds.left() - 1,
468 histogram_bounds.top() - 1,
469 histogram_bounds.left() - 1,
470 histogram_bounds.bottom() + 1,
471 paint);
472 canvas->drawLine(histogram_bounds.right() + 1,
473 histogram_bounds.top() - 1,
474 histogram_bounds.right() + 1,
475 histogram_bounds.bottom() + 1,
476 paint);
478 paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
479 const double bar_height = histogram_bounds.height() / kHistogramSize;
481 for (int i = kHistogramSize - 1; i >= 0; --i) {
482 if (histogram[i] > 0) {
483 double bar_width =
484 histogram[i] / max_bucket_value * histogram_bounds.width();
485 canvas->drawRect(
486 SkRect::MakeXYWH(histogram_bounds.left(),
487 histogram_bounds.bottom() - (i + 1) * bar_height,
488 bar_width,
490 paint);
494 // Draw FPS graph.
495 paint.setAntiAlias(true);
496 paint.setStyle(SkPaint::kStroke_Style);
497 paint.setStrokeWidth(1);
498 canvas->drawPath(path, paint);
500 return area;
503 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
504 int right,
505 int top,
506 int width) const {
507 if (!memory_entry_.total_bytes_used)
508 return SkRect::MakeEmpty();
510 const int kPadding = 4;
511 const int kFontHeight = 13;
513 const int height = 3 * kFontHeight + 4 * kPadding;
514 const int left = bounds().width() - width - right;
515 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
517 const double kMegabyte = 1024.0 * 1024.0;
519 SkPaint paint = CreatePaint();
520 DrawGraphBackground(canvas, &paint, area);
522 SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
523 SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
524 top + kPadding + 2 * kFontHeight);
525 SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
526 top + 2 * kPadding + 3 * kFontHeight);
528 paint.setColor(DebugColors::MemoryDisplayTextColor());
529 DrawText(canvas,
530 &paint,
531 "GPU memory",
532 SkPaint::kLeft_Align,
533 kFontHeight,
534 title_pos);
536 std::string text = base::StringPrintf(
537 "%6.1f MB used", memory_entry_.total_bytes_used / kMegabyte);
538 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
540 if (!memory_entry_.had_enough_memory)
541 paint.setColor(SK_ColorRED);
542 text = base::StringPrintf("%6.1f MB max ",
543 memory_entry_.total_budget_in_bytes / kMegabyte);
544 DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
546 return area;
549 SkRect HeadsUpDisplayLayerImpl::DrawGpuRasterizationStatus(SkCanvas* canvas,
550 int right,
551 int top,
552 int width) const {
553 std::string status;
554 SkColor color = SK_ColorRED;
555 switch (layer_tree_impl()->GetGpuRasterizationStatus()) {
556 case GpuRasterizationStatus::ON:
557 status = "GPU raster: on";
558 color = SK_ColorGREEN;
559 break;
560 case GpuRasterizationStatus::ON_FORCED:
561 status = "GPU raster: on (forced)";
562 color = SK_ColorGREEN;
563 break;
564 case GpuRasterizationStatus::OFF_DEVICE:
565 status = "GPU raster: off (device)";
566 color = SK_ColorRED;
567 break;
568 case GpuRasterizationStatus::OFF_VIEWPORT:
569 status = "GPU raster: off (viewport)";
570 color = SK_ColorYELLOW;
571 break;
572 case GpuRasterizationStatus::OFF_CONTENT:
573 status = "GPU raster: off (content)";
574 color = SK_ColorYELLOW;
575 break;
578 if (status.empty())
579 return SkRect::MakeEmpty();
581 const int kPadding = 4;
582 const int kFontHeight = 13;
584 const int height = kFontHeight + 2 * kPadding;
585 const int left = bounds().width() - width - right;
586 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
588 SkPaint paint = CreatePaint();
589 DrawGraphBackground(canvas, &paint, area);
591 SkPoint gpu_status_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
593 paint.setColor(color);
594 DrawText(canvas, &paint, status, SkPaint::kLeft_Align, kFontHeight,
595 gpu_status_pos);
597 return area;
600 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
601 SkCanvas* canvas,
602 const PaintTimeCounter* paint_time_counter,
603 int right,
604 int top) const {
605 const int kPadding = 4;
606 const int kFontHeight = 15;
608 const int kGraphWidth = paint_time_counter->HistorySize();
609 const int kGraphHeight = 40;
611 const int width = kGraphWidth + 2 * kPadding;
612 const int height =
613 kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
614 const int left = bounds().width() - width - right;
616 const SkRect area = SkRect::MakeXYWH(left, top, width, height);
618 SkPaint paint = CreatePaint();
619 DrawGraphBackground(canvas, &paint, area);
621 SkRect text_bounds = SkRect::MakeXYWH(
622 left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
623 SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
624 text_bounds.bottom() + kPadding,
625 kGraphWidth,
626 kFontHeight);
627 SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
628 text_bounds2.bottom() + 2 * kPadding,
629 kGraphWidth,
630 kGraphHeight);
632 const std::string value_text =
633 base::StringPrintf("%.1f", paint_time_graph_.value);
634 const std::string min_max_text = base::StringPrintf(
635 "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
637 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
638 DrawText(canvas, &paint, "Compositor frame time (ms)", SkPaint::kLeft_Align,
639 kFontHeight, text_bounds.left(), text_bounds.bottom());
640 DrawText(canvas,
641 &paint,
642 value_text,
643 SkPaint::kLeft_Align,
644 kFontHeight,
645 text_bounds2.left(),
646 text_bounds2.bottom());
647 DrawText(canvas,
648 &paint,
649 min_max_text,
650 SkPaint::kRight_Align,
651 kFontHeight,
652 text_bounds2.right(),
653 text_bounds2.bottom());
655 paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
656 for (PaintTimeCounter::RingBufferType::Iterator it =
657 paint_time_counter->End();
659 --it) {
660 double pt = it->InMillisecondsF();
662 if (pt == 0.0)
663 continue;
665 double p = pt / paint_time_graph_.current_upper_bound;
666 if (p > 1.0)
667 p = 1.0;
669 canvas->drawRect(
670 SkRect::MakeXYWH(graph_bounds.left() + it.index(),
671 graph_bounds.bottom() - p * graph_bounds.height(),
673 p * graph_bounds.height()),
674 paint);
677 DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
679 return area;
682 void HeadsUpDisplayLayerImpl::DrawDebugRect(
683 SkCanvas* canvas,
684 SkPaint* paint,
685 const DebugRect& rect,
686 SkColor stroke_color,
687 SkColor fill_color,
688 float stroke_width,
689 const std::string& label_text) const {
690 gfx::Rect debug_layer_rect =
691 gfx::ScaleToEnclosingRect(rect.rect, 1.0 / internal_contents_scale_,
692 1.0 / internal_contents_scale_);
693 SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
694 paint->setColor(fill_color);
695 paint->setStyle(SkPaint::kFill_Style);
696 canvas->drawIRect(sk_rect, *paint);
698 paint->setColor(stroke_color);
699 paint->setStyle(SkPaint::kStroke_Style);
700 paint->setStrokeWidth(SkFloatToScalar(stroke_width));
701 canvas->drawIRect(sk_rect, *paint);
703 if (label_text.length()) {
704 const int kFontHeight = 12;
705 const int kPadding = 3;
707 // The debug_layer_rect may be huge, and converting to a floating point may
708 // be lossy, so intersect with the HUD layer bounds first to prevent that.
709 gfx::Rect clip_rect = debug_layer_rect;
710 clip_rect.Intersect(gfx::Rect(internal_content_bounds_));
711 SkRect sk_clip_rect = RectToSkRect(clip_rect);
713 canvas->save();
714 canvas->clipRect(sk_clip_rect);
715 canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
717 SkPaint label_paint = CreatePaint();
718 label_paint.setTextSize(kFontHeight);
719 label_paint.setTypeface(typeface_.get());
720 label_paint.setColor(stroke_color);
722 const SkScalar label_text_width =
723 label_paint.measureText(label_text.c_str(), label_text.length());
724 canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
725 kFontHeight + 2 * kPadding),
726 label_paint);
728 label_paint.setAntiAlias(true);
729 label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
730 canvas->drawText(label_text.c_str(),
731 label_text.length(),
732 kPadding,
733 kFontHeight * 0.8f + kPadding,
734 label_paint);
736 canvas->restore();
740 void HeadsUpDisplayLayerImpl::DrawDebugRects(
741 SkCanvas* canvas,
742 DebugRectHistory* debug_rect_history) {
743 SkPaint paint = CreatePaint();
745 const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
746 std::vector<DebugRect> new_paint_rects;
748 for (size_t i = 0; i < debug_rects.size(); ++i) {
749 SkColor stroke_color = 0;
750 SkColor fill_color = 0;
751 float stroke_width = 0.f;
752 std::string label_text;
754 switch (debug_rects[i].type) {
755 case PAINT_RECT_TYPE:
756 new_paint_rects.push_back(debug_rects[i]);
757 continue;
758 case PROPERTY_CHANGED_RECT_TYPE:
759 stroke_color = DebugColors::PropertyChangedRectBorderColor();
760 fill_color = DebugColors::PropertyChangedRectFillColor();
761 stroke_width = DebugColors::PropertyChangedRectBorderWidth();
762 break;
763 case SURFACE_DAMAGE_RECT_TYPE:
764 stroke_color = DebugColors::SurfaceDamageRectBorderColor();
765 fill_color = DebugColors::SurfaceDamageRectFillColor();
766 stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
767 break;
768 case REPLICA_SCREEN_SPACE_RECT_TYPE:
769 stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
770 fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
771 stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
772 break;
773 case SCREEN_SPACE_RECT_TYPE:
774 stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
775 fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
776 stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
777 break;
778 case TOUCH_EVENT_HANDLER_RECT_TYPE:
779 stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
780 fill_color = DebugColors::TouchEventHandlerRectFillColor();
781 stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
782 label_text = "touch event listener";
783 break;
784 case WHEEL_EVENT_HANDLER_RECT_TYPE:
785 stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
786 fill_color = DebugColors::WheelEventHandlerRectFillColor();
787 stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
788 label_text = "mousewheel event listener";
789 break;
790 case SCROLL_EVENT_HANDLER_RECT_TYPE:
791 stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
792 fill_color = DebugColors::ScrollEventHandlerRectFillColor();
793 stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
794 label_text = "scroll event listener";
795 break;
796 case NON_FAST_SCROLLABLE_RECT_TYPE:
797 stroke_color = DebugColors::NonFastScrollableRectBorderColor();
798 fill_color = DebugColors::NonFastScrollableRectFillColor();
799 stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
800 label_text = "repaints on scroll";
801 break;
802 case ANIMATION_BOUNDS_RECT_TYPE:
803 stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
804 fill_color = DebugColors::LayerAnimationBoundsFillColor();
805 stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
806 label_text = "animation bounds";
807 break;
810 DrawDebugRect(canvas,
811 &paint,
812 debug_rects[i],
813 stroke_color,
814 fill_color,
815 stroke_width,
816 label_text);
819 if (new_paint_rects.size()) {
820 paint_rects_.swap(new_paint_rects);
821 fade_step_ = DebugColors::kFadeSteps;
823 if (fade_step_ > 0) {
824 fade_step_--;
825 for (size_t i = 0; i < paint_rects_.size(); ++i) {
826 DrawDebugRect(canvas,
827 &paint,
828 paint_rects_[i],
829 DebugColors::PaintRectBorderColor(fade_step_),
830 DebugColors::PaintRectFillColor(fade_step_),
831 DebugColors::PaintRectBorderWidth(),
832 "");
837 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
838 return "cc::HeadsUpDisplayLayerImpl";
841 void HeadsUpDisplayLayerImpl::AsValueInto(
842 base::trace_event::TracedValue* dict) const {
843 LayerImpl::AsValueInto(dict);
844 dict->SetString("layer_name", "Heads Up Display Layer");
847 } // namespace cc