Home | History | Annotate | Download | only in layers
      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/debug/trace_event.h"
     11 #include "base/debug/trace_event_argument.h"
     12 #include "base/strings/stringprintf.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_impl.h"
     21 #include "skia/ext/platform_canvas.h"
     22 #include "third_party/khronos/GLES2/gl2.h"
     23 #include "third_party/khronos/GLES2/gl2ext.h"
     24 #include "third_party/skia/include/core/SkBitmap.h"
     25 #include "third_party/skia/include/core/SkPaint.h"
     26 #include "third_party/skia/include/core/SkTypeface.h"
     27 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
     28 #include "ui/gfx/point.h"
     29 #include "ui/gfx/size.h"
     30 
     31 namespace cc {
     32 
     33 static inline SkPaint CreatePaint() {
     34   SkPaint paint;
     35 #if (SK_R32_SHIFT || SK_B32_SHIFT != 16)
     36   // The SkCanvas is in RGBA but the shader is expecting BGRA, so we need to
     37   // swizzle our colors when drawing to the SkCanvas.
     38   SkColorMatrix swizzle_matrix;
     39   for (int i = 0; i < 20; ++i)
     40     swizzle_matrix.fMat[i] = 0;
     41   swizzle_matrix.fMat[0 + 5 * 2] = 1;
     42   swizzle_matrix.fMat[1 + 5 * 1] = 1;
     43   swizzle_matrix.fMat[2 + 5 * 0] = 1;
     44   swizzle_matrix.fMat[3 + 5 * 3] = 1;
     45 
     46   skia::RefPtr<SkColorMatrixFilter> filter =
     47       skia::AdoptRef(SkColorMatrixFilter::Create(swizzle_matrix));
     48   paint.setColorFilter(filter.get());
     49 #endif
     50   return paint;
     51 }
     52 
     53 HeadsUpDisplayLayerImpl::Graph::Graph(double indicator_value,
     54                                       double start_upper_bound)
     55     : value(0.0),
     56       min(0.0),
     57       max(0.0),
     58       current_upper_bound(start_upper_bound),
     59       default_upper_bound(start_upper_bound),
     60       indicator(indicator_value) {}
     61 
     62 double HeadsUpDisplayLayerImpl::Graph::UpdateUpperBound() {
     63   double target_upper_bound = std::max(max, default_upper_bound);
     64   current_upper_bound += (target_upper_bound - current_upper_bound) * 0.5;
     65   return current_upper_bound;
     66 }
     67 
     68 HeadsUpDisplayLayerImpl::HeadsUpDisplayLayerImpl(LayerTreeImpl* tree_impl,
     69                                                  int id)
     70     : LayerImpl(tree_impl, id),
     71       typeface_(skia::AdoptRef(
     72           SkTypeface::CreateFromName("monospace", SkTypeface::kBold))),
     73       fps_graph_(60.0, 80.0),
     74       paint_time_graph_(16.0, 48.0),
     75       fade_step_(0) {}
     76 
     77 HeadsUpDisplayLayerImpl::~HeadsUpDisplayLayerImpl() {}
     78 
     79 scoped_ptr<LayerImpl> HeadsUpDisplayLayerImpl::CreateLayerImpl(
     80     LayerTreeImpl* tree_impl) {
     81   return HeadsUpDisplayLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
     82 }
     83 
     84 void HeadsUpDisplayLayerImpl::AcquireResource(
     85     ResourceProvider* resource_provider) {
     86   for (ScopedPtrVector<ScopedResource>::iterator it = resources_.begin();
     87        it != resources_.end();
     88        ++it) {
     89     if (!resource_provider->InUseByConsumer((*it)->id())) {
     90       resources_.swap(it, resources_.end() - 1);
     91       return;
     92     }
     93   }
     94 
     95   scoped_ptr<ScopedResource> resource =
     96       ScopedResource::Create(resource_provider);
     97   resource->Allocate(
     98       content_bounds(), ResourceProvider::TextureHintImmutable, RGBA_8888);
     99   resources_.push_back(resource.Pass());
    100 }
    101 
    102 class ResourceSizeIsEqualTo {
    103  public:
    104   explicit ResourceSizeIsEqualTo(const gfx::Size& size_)
    105       : compare_size_(size_) {}
    106 
    107   bool operator()(const ScopedResource* resource) {
    108     return resource->size() == compare_size_;
    109   }
    110 
    111  private:
    112   const gfx::Size compare_size_;
    113 };
    114 
    115 void HeadsUpDisplayLayerImpl::ReleaseUnmatchedSizeResources(
    116     ResourceProvider* resource_provider) {
    117   ScopedPtrVector<ScopedResource>::iterator it_erase =
    118       resources_.partition(ResourceSizeIsEqualTo(content_bounds()));
    119   resources_.erase(it_erase, resources_.end());
    120 }
    121 
    122 bool HeadsUpDisplayLayerImpl::WillDraw(DrawMode draw_mode,
    123                                        ResourceProvider* resource_provider) {
    124   if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
    125     return false;
    126 
    127   ReleaseUnmatchedSizeResources(resource_provider);
    128   AcquireResource(resource_provider);
    129   return LayerImpl::WillDraw(draw_mode, resource_provider);
    130 }
    131 
    132 void HeadsUpDisplayLayerImpl::AppendQuads(
    133     RenderPass* render_pass,
    134     const OcclusionTracker<LayerImpl>& occlusion_tracker,
    135     AppendQuadsData* append_quads_data) {
    136   if (!resources_.back()->id())
    137     return;
    138 
    139   SharedQuadState* shared_quad_state =
    140       render_pass->CreateAndAppendSharedQuadState();
    141   PopulateSharedQuadState(shared_quad_state);
    142 
    143   gfx::Rect quad_rect(content_bounds());
    144   gfx::Rect opaque_rect(contents_opaque() ? quad_rect : gfx::Rect());
    145   gfx::Rect visible_quad_rect(quad_rect);
    146   bool premultiplied_alpha = true;
    147   gfx::PointF uv_top_left(0.f, 0.f);
    148   gfx::PointF uv_bottom_right(1.f, 1.f);
    149   const float vertex_opacity[] = { 1.f, 1.f, 1.f, 1.f };
    150   bool flipped = false;
    151   TextureDrawQuad* quad =
    152       render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
    153   quad->SetNew(shared_quad_state,
    154                quad_rect,
    155                opaque_rect,
    156                visible_quad_rect,
    157                resources_.back()->id(),
    158                premultiplied_alpha,
    159                uv_top_left,
    160                uv_bottom_right,
    161                SK_ColorTRANSPARENT,
    162                vertex_opacity,
    163                flipped);
    164 }
    165 
    166 void HeadsUpDisplayLayerImpl::UpdateHudTexture(
    167     DrawMode draw_mode,
    168     ResourceProvider* resource_provider) {
    169   if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE || !resources_.back()->id())
    170     return;
    171 
    172   SkISize canvas_size;
    173   if (hud_canvas_)
    174     canvas_size = hud_canvas_->getDeviceSize();
    175   else
    176     canvas_size.set(0, 0);
    177 
    178   if (canvas_size.width() != content_bounds().width() ||
    179       canvas_size.height() != content_bounds().height() || !hud_canvas_) {
    180     TRACE_EVENT0("cc", "ResizeHudCanvas");
    181     bool opaque = false;
    182     hud_canvas_ = make_scoped_ptr(skia::CreateBitmapCanvas(
    183         content_bounds().width(), content_bounds().height(), opaque));
    184   }
    185 
    186   UpdateHudContents();
    187 
    188   {
    189     TRACE_EVENT0("cc", "DrawHudContents");
    190     hud_canvas_->clear(SkColorSetARGB(0, 0, 0, 0));
    191     hud_canvas_->save();
    192     hud_canvas_->scale(contents_scale_x(), contents_scale_y());
    193 
    194     DrawHudContents(hud_canvas_.get());
    195 
    196     hud_canvas_->restore();
    197   }
    198 
    199   TRACE_EVENT0("cc", "UploadHudTexture");
    200   SkImageInfo info;
    201   size_t row_bytes = 0;
    202   const void* pixels = hud_canvas_->peekPixels(&info, &row_bytes);
    203   DCHECK(pixels);
    204   gfx::Rect content_rect(content_bounds());
    205   DCHECK(info.colorType() == kN32_SkColorType);
    206   resource_provider->SetPixels(resources_.back()->id(),
    207                                static_cast<const uint8_t*>(pixels),
    208                                content_rect,
    209                                content_rect,
    210                                gfx::Vector2d());
    211 }
    212 
    213 void HeadsUpDisplayLayerImpl::ReleaseResources() {
    214   resources_.clear();
    215 }
    216 
    217 void HeadsUpDisplayLayerImpl::UpdateHudContents() {
    218   const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
    219 
    220   // Don't update numbers every frame so text is readable.
    221   base::TimeTicks now = layer_tree_impl()->CurrentBeginFrameArgs().frame_time;
    222   if (base::TimeDelta(now - time_of_last_graph_update_).InSecondsF() > 0.25f) {
    223     time_of_last_graph_update_ = now;
    224 
    225     if (debug_state.show_fps_counter) {
    226       FrameRateCounter* fps_counter = layer_tree_impl()->frame_rate_counter();
    227       fps_graph_.value = fps_counter->GetAverageFPS();
    228       fps_counter->GetMinAndMaxFPS(&fps_graph_.min, &fps_graph_.max);
    229     }
    230 
    231     if (debug_state.continuous_painting) {
    232       PaintTimeCounter* paint_time_counter =
    233           layer_tree_impl()->paint_time_counter();
    234       base::TimeDelta latest, min, max;
    235 
    236       if (paint_time_counter->End())
    237         latest = **paint_time_counter->End();
    238       paint_time_counter->GetMinAndMaxPaintTime(&min, &max);
    239 
    240       paint_time_graph_.value = latest.InMillisecondsF();
    241       paint_time_graph_.min = min.InMillisecondsF();
    242       paint_time_graph_.max = max.InMillisecondsF();
    243     }
    244 
    245     if (debug_state.ShowMemoryStats()) {
    246       MemoryHistory* memory_history = layer_tree_impl()->memory_history();
    247       if (memory_history->End())
    248         memory_entry_ = **memory_history->End();
    249       else
    250         memory_entry_ = MemoryHistory::Entry();
    251     }
    252   }
    253 
    254   fps_graph_.UpdateUpperBound();
    255   paint_time_graph_.UpdateUpperBound();
    256 }
    257 
    258 void HeadsUpDisplayLayerImpl::DrawHudContents(SkCanvas* canvas) {
    259   const LayerTreeDebugState& debug_state = layer_tree_impl()->debug_state();
    260 
    261   if (debug_state.ShowHudRects()) {
    262     DrawDebugRects(canvas, layer_tree_impl()->debug_rect_history());
    263     if (IsAnimatingHUDContents()) {
    264       layer_tree_impl()->SetNeedsRedraw();
    265     }
    266   }
    267 
    268   SkRect area = SkRect::MakeEmpty();
    269   if (debug_state.continuous_painting) {
    270     area = DrawPaintTimeDisplay(
    271         canvas, layer_tree_impl()->paint_time_counter(), 0, 0);
    272   } else if (debug_state.show_fps_counter) {
    273     // Don't show the FPS display when continuous painting is enabled, because
    274     // it would show misleading numbers.
    275     area =
    276         DrawFPSDisplay(canvas, layer_tree_impl()->frame_rate_counter(), 0, 0);
    277   }
    278 
    279   if (debug_state.ShowMemoryStats())
    280     DrawMemoryDisplay(canvas, 0, area.bottom(), SkMaxScalar(area.width(), 150));
    281 }
    282 
    283 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
    284                                        SkPaint* paint,
    285                                        const std::string& text,
    286                                        SkPaint::Align align,
    287                                        int size,
    288                                        int x,
    289                                        int y) const {
    290   const bool anti_alias = paint->isAntiAlias();
    291   paint->setAntiAlias(true);
    292 
    293   paint->setTextSize(size);
    294   paint->setTextAlign(align);
    295   paint->setTypeface(typeface_.get());
    296   canvas->drawText(text.c_str(), text.length(), x, y, *paint);
    297 
    298   paint->setAntiAlias(anti_alias);
    299 }
    300 
    301 void HeadsUpDisplayLayerImpl::DrawText(SkCanvas* canvas,
    302                                        SkPaint* paint,
    303                                        const std::string& text,
    304                                        SkPaint::Align align,
    305                                        int size,
    306                                        const SkPoint& pos) const {
    307   DrawText(canvas, paint, text, align, size, pos.x(), pos.y());
    308 }
    309 
    310 void HeadsUpDisplayLayerImpl::DrawGraphBackground(SkCanvas* canvas,
    311                                                   SkPaint* paint,
    312                                                   const SkRect& bounds) const {
    313   paint->setColor(DebugColors::HUDBackgroundColor());
    314   canvas->drawRect(bounds, *paint);
    315 }
    316 
    317 void HeadsUpDisplayLayerImpl::DrawGraphLines(SkCanvas* canvas,
    318                                              SkPaint* paint,
    319                                              const SkRect& bounds,
    320                                              const Graph& graph) const {
    321   // Draw top and bottom line.
    322   paint->setColor(DebugColors::HUDSeparatorLineColor());
    323   canvas->drawLine(bounds.left(),
    324                    bounds.top() - 1,
    325                    bounds.right(),
    326                    bounds.top() - 1,
    327                    *paint);
    328   canvas->drawLine(
    329       bounds.left(), bounds.bottom(), bounds.right(), bounds.bottom(), *paint);
    330 
    331   // Draw indicator line (additive blend mode to increase contrast when drawn on
    332   // top of graph).
    333   paint->setColor(DebugColors::HUDIndicatorLineColor());
    334   paint->setXfermodeMode(SkXfermode::kPlus_Mode);
    335   const double indicator_top =
    336       bounds.height() * (1.0 - graph.indicator / graph.current_upper_bound) -
    337       1.0;
    338   canvas->drawLine(bounds.left(),
    339                    bounds.top() + indicator_top,
    340                    bounds.right(),
    341                    bounds.top() + indicator_top,
    342                    *paint);
    343   paint->setXfermode(NULL);
    344 }
    345 
    346 SkRect HeadsUpDisplayLayerImpl::DrawFPSDisplay(
    347     SkCanvas* canvas,
    348     const FrameRateCounter* fps_counter,
    349     int right,
    350     int top) const {
    351   const int kPadding = 4;
    352   const int kGap = 6;
    353 
    354   const int kFontHeight = 15;
    355 
    356   const int kGraphWidth = fps_counter->time_stamp_history_size() - 2;
    357   const int kGraphHeight = 40;
    358 
    359   const int kHistogramWidth = 37;
    360 
    361   int width = kGraphWidth + kHistogramWidth + 4 * kPadding;
    362   int height = kFontHeight + kGraphHeight + 4 * kPadding + 2;
    363   int left = bounds().width() - width - right;
    364   SkRect area = SkRect::MakeXYWH(left, top, width, height);
    365 
    366   SkPaint paint = CreatePaint();
    367   DrawGraphBackground(canvas, &paint, area);
    368 
    369   SkRect text_bounds =
    370       SkRect::MakeXYWH(left + kPadding,
    371                        top + kPadding,
    372                        kGraphWidth + kHistogramWidth + kGap + 2,
    373                        kFontHeight);
    374   SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
    375                                          text_bounds.bottom() + 2 * kPadding,
    376                                          kGraphWidth,
    377                                          kGraphHeight);
    378   SkRect histogram_bounds = SkRect::MakeXYWH(graph_bounds.right() + kGap,
    379                                              graph_bounds.top(),
    380                                              kHistogramWidth,
    381                                              kGraphHeight);
    382 
    383   const std::string value_text =
    384       base::StringPrintf("FPS:%5.1f", fps_graph_.value);
    385   const std::string min_max_text =
    386       base::StringPrintf("%.0f-%.0f", fps_graph_.min, fps_graph_.max);
    387 
    388   paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
    389   DrawText(canvas,
    390            &paint,
    391            value_text,
    392            SkPaint::kLeft_Align,
    393            kFontHeight,
    394            text_bounds.left(),
    395            text_bounds.bottom());
    396   DrawText(canvas,
    397            &paint,
    398            min_max_text,
    399            SkPaint::kRight_Align,
    400            kFontHeight,
    401            text_bounds.right(),
    402            text_bounds.bottom());
    403 
    404   DrawGraphLines(canvas, &paint, graph_bounds, fps_graph_);
    405 
    406   // Collect graph and histogram data.
    407   SkPath path;
    408 
    409   const int kHistogramSize = 20;
    410   double histogram[kHistogramSize] = { 1.0 };
    411   double max_bucket_value = 1.0;
    412 
    413   for (FrameRateCounter::RingBufferType::Iterator it = --fps_counter->end(); it;
    414        --it) {
    415     base::TimeDelta delta = fps_counter->RecentFrameInterval(it.index() + 1);
    416 
    417     // Skip this particular instantaneous frame rate if it is not likely to have
    418     // been valid.
    419     if (!fps_counter->IsBadFrameInterval(delta)) {
    420       double fps = 1.0 / delta.InSecondsF();
    421 
    422       // Clamp the FPS to the range we want to plot visually.
    423       double p = fps / fps_graph_.current_upper_bound;
    424       if (p > 1.0)
    425         p = 1.0;
    426 
    427       // Plot this data point.
    428       SkPoint cur =
    429           SkPoint::Make(graph_bounds.left() + it.index(),
    430                         graph_bounds.bottom() - p * graph_bounds.height());
    431       if (path.isEmpty())
    432         path.moveTo(cur);
    433       else
    434         path.lineTo(cur);
    435 
    436       // Use the fps value to find the right bucket in the histogram.
    437       int bucket_index = floor(p * (kHistogramSize - 1));
    438 
    439       // Add the delta time to take the time spent at that fps rate into
    440       // account.
    441       histogram[bucket_index] += delta.InSecondsF();
    442       max_bucket_value = std::max(histogram[bucket_index], max_bucket_value);
    443     }
    444   }
    445 
    446   // Draw FPS histogram.
    447   paint.setColor(DebugColors::HUDSeparatorLineColor());
    448   canvas->drawLine(histogram_bounds.left() - 1,
    449                    histogram_bounds.top() - 1,
    450                    histogram_bounds.left() - 1,
    451                    histogram_bounds.bottom() + 1,
    452                    paint);
    453   canvas->drawLine(histogram_bounds.right() + 1,
    454                    histogram_bounds.top() - 1,
    455                    histogram_bounds.right() + 1,
    456                    histogram_bounds.bottom() + 1,
    457                    paint);
    458 
    459   paint.setColor(DebugColors::FPSDisplayTextAndGraphColor());
    460   const double bar_height = histogram_bounds.height() / kHistogramSize;
    461 
    462   for (int i = kHistogramSize - 1; i >= 0; --i) {
    463     if (histogram[i] > 0) {
    464       double bar_width =
    465           histogram[i] / max_bucket_value * histogram_bounds.width();
    466       canvas->drawRect(
    467           SkRect::MakeXYWH(histogram_bounds.left(),
    468                            histogram_bounds.bottom() - (i + 1) * bar_height,
    469                            bar_width,
    470                            1),
    471           paint);
    472     }
    473   }
    474 
    475   // Draw FPS graph.
    476   paint.setAntiAlias(true);
    477   paint.setStyle(SkPaint::kStroke_Style);
    478   paint.setStrokeWidth(1);
    479   canvas->drawPath(path, paint);
    480 
    481   return area;
    482 }
    483 
    484 SkRect HeadsUpDisplayLayerImpl::DrawMemoryDisplay(SkCanvas* canvas,
    485                                                   int right,
    486                                                   int top,
    487                                                   int width) const {
    488   if (!memory_entry_.bytes_total())
    489     return SkRect::MakeEmpty();
    490 
    491   const int kPadding = 4;
    492   const int kFontHeight = 13;
    493 
    494   const int height = 3 * kFontHeight + 4 * kPadding;
    495   const int left = bounds().width() - width - right;
    496   const SkRect area = SkRect::MakeXYWH(left, top, width, height);
    497 
    498   const double megabyte = 1024.0 * 1024.0;
    499 
    500   SkPaint paint = CreatePaint();
    501   DrawGraphBackground(canvas, &paint, area);
    502 
    503   SkPoint title_pos = SkPoint::Make(left + kPadding, top + kFontHeight);
    504   SkPoint stat1_pos = SkPoint::Make(left + width - kPadding - 1,
    505                                     top + kPadding + 2 * kFontHeight);
    506   SkPoint stat2_pos = SkPoint::Make(left + width - kPadding - 1,
    507                                     top + 2 * kPadding + 3 * kFontHeight);
    508 
    509   paint.setColor(DebugColors::MemoryDisplayTextColor());
    510   DrawText(canvas,
    511            &paint,
    512            "GPU memory",
    513            SkPaint::kLeft_Align,
    514            kFontHeight,
    515            title_pos);
    516 
    517   std::string text =
    518       base::StringPrintf("%6.1f MB used",
    519                          (memory_entry_.bytes_unreleasable +
    520                           memory_entry_.bytes_allocated) / megabyte);
    521   DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat1_pos);
    522 
    523   if (memory_entry_.bytes_over) {
    524     paint.setColor(SK_ColorRED);
    525     text = base::StringPrintf("%6.1f MB over",
    526                               memory_entry_.bytes_over / megabyte);
    527   } else {
    528     text = base::StringPrintf("%6.1f MB max ",
    529                               memory_entry_.total_budget_in_bytes / megabyte);
    530   }
    531   DrawText(canvas, &paint, text, SkPaint::kRight_Align, kFontHeight, stat2_pos);
    532 
    533   return area;
    534 }
    535 
    536 SkRect HeadsUpDisplayLayerImpl::DrawPaintTimeDisplay(
    537     SkCanvas* canvas,
    538     const PaintTimeCounter* paint_time_counter,
    539     int right,
    540     int top) const {
    541   const int kPadding = 4;
    542   const int kFontHeight = 15;
    543 
    544   const int kGraphWidth = paint_time_counter->HistorySize();
    545   const int kGraphHeight = 40;
    546 
    547   const int width = kGraphWidth + 2 * kPadding;
    548   const int height =
    549       kFontHeight + kGraphHeight + 4 * kPadding + 2 + kFontHeight + kPadding;
    550   const int left = bounds().width() - width - right;
    551 
    552   const SkRect area = SkRect::MakeXYWH(left, top, width, height);
    553 
    554   SkPaint paint = CreatePaint();
    555   DrawGraphBackground(canvas, &paint, area);
    556 
    557   SkRect text_bounds = SkRect::MakeXYWH(
    558       left + kPadding, top + kPadding, kGraphWidth, kFontHeight);
    559   SkRect text_bounds2 = SkRect::MakeXYWH(left + kPadding,
    560                                          text_bounds.bottom() + kPadding,
    561                                          kGraphWidth,
    562                                          kFontHeight);
    563   SkRect graph_bounds = SkRect::MakeXYWH(left + kPadding,
    564                                          text_bounds2.bottom() + 2 * kPadding,
    565                                          kGraphWidth,
    566                                          kGraphHeight);
    567 
    568   const std::string value_text =
    569       base::StringPrintf("%.1f", paint_time_graph_.value);
    570   const std::string min_max_text = base::StringPrintf(
    571       "%.1f-%.1f", paint_time_graph_.min, paint_time_graph_.max);
    572 
    573   paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
    574   DrawText(canvas,
    575            &paint,
    576            "Page paint time (ms)",
    577            SkPaint::kLeft_Align,
    578            kFontHeight,
    579            text_bounds.left(),
    580            text_bounds.bottom());
    581   DrawText(canvas,
    582            &paint,
    583            value_text,
    584            SkPaint::kLeft_Align,
    585            kFontHeight,
    586            text_bounds2.left(),
    587            text_bounds2.bottom());
    588   DrawText(canvas,
    589            &paint,
    590            min_max_text,
    591            SkPaint::kRight_Align,
    592            kFontHeight,
    593            text_bounds2.right(),
    594            text_bounds2.bottom());
    595 
    596   paint.setColor(DebugColors::PaintTimeDisplayTextAndGraphColor());
    597   for (PaintTimeCounter::RingBufferType::Iterator it =
    598            paint_time_counter->End();
    599        it;
    600        --it) {
    601     double pt = it->InMillisecondsF();
    602 
    603     if (pt == 0.0)
    604       continue;
    605 
    606     double p = pt / paint_time_graph_.current_upper_bound;
    607     if (p > 1.0)
    608       p = 1.0;
    609 
    610     canvas->drawRect(
    611         SkRect::MakeXYWH(graph_bounds.left() + it.index(),
    612                          graph_bounds.bottom() - p * graph_bounds.height(),
    613                          1,
    614                          p * graph_bounds.height()),
    615         paint);
    616   }
    617 
    618   DrawGraphLines(canvas, &paint, graph_bounds, paint_time_graph_);
    619 
    620   return area;
    621 }
    622 
    623 void HeadsUpDisplayLayerImpl::DrawDebugRect(
    624     SkCanvas* canvas,
    625     SkPaint* paint,
    626     const DebugRect& rect,
    627     SkColor stroke_color,
    628     SkColor fill_color,
    629     float stroke_width,
    630     const std::string& label_text) const {
    631   gfx::Rect debug_layer_rect = gfx::ScaleToEnclosingRect(
    632       rect.rect, 1.0 / contents_scale_x(), 1.0 / contents_scale_y());
    633   SkIRect sk_rect = RectToSkIRect(debug_layer_rect);
    634   paint->setColor(fill_color);
    635   paint->setStyle(SkPaint::kFill_Style);
    636   canvas->drawIRect(sk_rect, *paint);
    637 
    638   paint->setColor(stroke_color);
    639   paint->setStyle(SkPaint::kStroke_Style);
    640   paint->setStrokeWidth(SkFloatToScalar(stroke_width));
    641   canvas->drawIRect(sk_rect, *paint);
    642 
    643   if (label_text.length()) {
    644     const int kFontHeight = 12;
    645     const int kPadding = 3;
    646 
    647     // The debug_layer_rect may be huge, and converting to a floating point may
    648     // be lossy, so intersect with the HUD layer bounds first to prevent that.
    649     gfx::Rect clip_rect = debug_layer_rect;
    650     clip_rect.Intersect(gfx::Rect(content_bounds()));
    651     SkRect sk_clip_rect = RectToSkRect(clip_rect);
    652 
    653     canvas->save();
    654     canvas->clipRect(sk_clip_rect);
    655     canvas->translate(sk_clip_rect.x(), sk_clip_rect.y());
    656 
    657     SkPaint label_paint = CreatePaint();
    658     label_paint.setTextSize(kFontHeight);
    659     label_paint.setTypeface(typeface_.get());
    660     label_paint.setColor(stroke_color);
    661 
    662     const SkScalar label_text_width =
    663         label_paint.measureText(label_text.c_str(), label_text.length());
    664     canvas->drawRect(SkRect::MakeWH(label_text_width + 2 * kPadding,
    665                                     kFontHeight + 2 * kPadding),
    666                      label_paint);
    667 
    668     label_paint.setAntiAlias(true);
    669     label_paint.setColor(SkColorSetARGB(255, 50, 50, 50));
    670     canvas->drawText(label_text.c_str(),
    671                      label_text.length(),
    672                      kPadding,
    673                      kFontHeight * 0.8f + kPadding,
    674                      label_paint);
    675 
    676     canvas->restore();
    677   }
    678 }
    679 
    680 void HeadsUpDisplayLayerImpl::DrawDebugRects(
    681     SkCanvas* canvas,
    682     DebugRectHistory* debug_rect_history) {
    683   SkPaint paint = CreatePaint();
    684 
    685   const std::vector<DebugRect>& debug_rects = debug_rect_history->debug_rects();
    686   std::vector<DebugRect> new_paint_rects;
    687 
    688   for (size_t i = 0; i < debug_rects.size(); ++i) {
    689     SkColor stroke_color = 0;
    690     SkColor fill_color = 0;
    691     float stroke_width = 0.f;
    692     std::string label_text;
    693 
    694     switch (debug_rects[i].type) {
    695       case PAINT_RECT_TYPE:
    696         new_paint_rects.push_back(debug_rects[i]);
    697         continue;
    698       case PROPERTY_CHANGED_RECT_TYPE:
    699         stroke_color = DebugColors::PropertyChangedRectBorderColor();
    700         fill_color = DebugColors::PropertyChangedRectFillColor();
    701         stroke_width = DebugColors::PropertyChangedRectBorderWidth();
    702         break;
    703       case SURFACE_DAMAGE_RECT_TYPE:
    704         stroke_color = DebugColors::SurfaceDamageRectBorderColor();
    705         fill_color = DebugColors::SurfaceDamageRectFillColor();
    706         stroke_width = DebugColors::SurfaceDamageRectBorderWidth();
    707         break;
    708       case REPLICA_SCREEN_SPACE_RECT_TYPE:
    709         stroke_color = DebugColors::ScreenSpaceSurfaceReplicaRectBorderColor();
    710         fill_color = DebugColors::ScreenSpaceSurfaceReplicaRectFillColor();
    711         stroke_width = DebugColors::ScreenSpaceSurfaceReplicaRectBorderWidth();
    712         break;
    713       case SCREEN_SPACE_RECT_TYPE:
    714         stroke_color = DebugColors::ScreenSpaceLayerRectBorderColor();
    715         fill_color = DebugColors::ScreenSpaceLayerRectFillColor();
    716         stroke_width = DebugColors::ScreenSpaceLayerRectBorderWidth();
    717         break;
    718       case OCCLUDING_RECT_TYPE:
    719         stroke_color = DebugColors::OccludingRectBorderColor();
    720         fill_color = DebugColors::OccludingRectFillColor();
    721         stroke_width = DebugColors::OccludingRectBorderWidth();
    722         break;
    723       case NONOCCLUDING_RECT_TYPE:
    724         stroke_color = DebugColors::NonOccludingRectBorderColor();
    725         fill_color = DebugColors::NonOccludingRectFillColor();
    726         stroke_width = DebugColors::NonOccludingRectBorderWidth();
    727         break;
    728       case TOUCH_EVENT_HANDLER_RECT_TYPE:
    729         stroke_color = DebugColors::TouchEventHandlerRectBorderColor();
    730         fill_color = DebugColors::TouchEventHandlerRectFillColor();
    731         stroke_width = DebugColors::TouchEventHandlerRectBorderWidth();
    732         label_text = "touch event listener";
    733         break;
    734       case WHEEL_EVENT_HANDLER_RECT_TYPE:
    735         stroke_color = DebugColors::WheelEventHandlerRectBorderColor();
    736         fill_color = DebugColors::WheelEventHandlerRectFillColor();
    737         stroke_width = DebugColors::WheelEventHandlerRectBorderWidth();
    738         label_text = "mousewheel event listener";
    739         break;
    740       case SCROLL_EVENT_HANDLER_RECT_TYPE:
    741         stroke_color = DebugColors::ScrollEventHandlerRectBorderColor();
    742         fill_color = DebugColors::ScrollEventHandlerRectFillColor();
    743         stroke_width = DebugColors::ScrollEventHandlerRectBorderWidth();
    744         label_text = "scroll event listener";
    745         break;
    746       case NON_FAST_SCROLLABLE_RECT_TYPE:
    747         stroke_color = DebugColors::NonFastScrollableRectBorderColor();
    748         fill_color = DebugColors::NonFastScrollableRectFillColor();
    749         stroke_width = DebugColors::NonFastScrollableRectBorderWidth();
    750         label_text = "repaints on scroll";
    751         break;
    752       case ANIMATION_BOUNDS_RECT_TYPE:
    753         stroke_color = DebugColors::LayerAnimationBoundsBorderColor();
    754         fill_color = DebugColors::LayerAnimationBoundsFillColor();
    755         stroke_width = DebugColors::LayerAnimationBoundsBorderWidth();
    756         label_text = "animation bounds";
    757         break;
    758     }
    759 
    760     DrawDebugRect(canvas,
    761                   &paint,
    762                   debug_rects[i],
    763                   stroke_color,
    764                   fill_color,
    765                   stroke_width,
    766                   label_text);
    767   }
    768 
    769   if (new_paint_rects.size()) {
    770     paint_rects_.swap(new_paint_rects);
    771     fade_step_ = DebugColors::kFadeSteps;
    772   }
    773   if (fade_step_ > 0) {
    774     fade_step_--;
    775     for (size_t i = 0; i < paint_rects_.size(); ++i) {
    776       DrawDebugRect(canvas,
    777                     &paint,
    778                     paint_rects_[i],
    779                     DebugColors::PaintRectBorderColor(fade_step_),
    780                     DebugColors::PaintRectFillColor(fade_step_),
    781                     DebugColors::PaintRectBorderWidth(),
    782                     "");
    783     }
    784   }
    785 }
    786 
    787 const char* HeadsUpDisplayLayerImpl::LayerTypeAsString() const {
    788   return "cc::HeadsUpDisplayLayerImpl";
    789 }
    790 
    791 void HeadsUpDisplayLayerImpl::AsValueInto(
    792     base::debug::TracedValue* dict) const {
    793   LayerImpl::AsValueInto(dict);
    794   dict->SetString("layer_name", "Heads Up Display Layer");
    795 }
    796 
    797 }  // namespace cc
    798