rasterize_and_record_benchmark_impl.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 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/debug/rasterize_and_record_benchmark_impl.h"
6
7#include <algorithm>
8#include <limits>
9
10#include "base/basictypes.h"
11#include "base/values.h"
12#include "cc/layers/layer_impl.h"
13#include "cc/layers/picture_layer_impl.h"
14#include "cc/trees/layer_tree_host_common.h"
15#include "cc/trees/layer_tree_host_impl.h"
16#include "ui/gfx/rect.h"
17
18namespace cc {
19
20namespace {
21
22const int kDefaultRasterizeRepeatCount = 100;
23
24base::TimeTicks Now() {
25  return base::TimeTicks::IsThreadNowSupported()
26             ? base::TimeTicks::ThreadNow()
27             : base::TimeTicks::HighResNow();
28}
29
30}  // namespace
31
32RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
33    scoped_refptr<base::MessageLoopProxy> origin_loop,
34    base::Value* value,
35    const MicroBenchmarkImpl::DoneCallback& callback)
36    : MicroBenchmarkImpl(callback, origin_loop),
37      rasterize_repeat_count_(kDefaultRasterizeRepeatCount) {
38  base::DictionaryValue* settings = NULL;
39  value->GetAsDictionary(&settings);
40  if (!settings)
41    return;
42
43  if (settings->HasKey("rasterize_repeat_count"))
44    settings->GetInteger("rasterize_repeat_count", &rasterize_repeat_count_);
45}
46
47RasterizeAndRecordBenchmarkImpl::~RasterizeAndRecordBenchmarkImpl() {}
48
49void RasterizeAndRecordBenchmarkImpl::DidCompleteCommit(
50    LayerTreeHostImpl* host) {
51  LayerTreeHostCommon::CallFunctionForSubtree(
52      host->RootLayer(),
53      base::Bind(&RasterizeAndRecordBenchmarkImpl::Run,
54                 base::Unretained(this)));
55
56  scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
57  result->SetInteger("pixels_rasterized", rasterize_results_.pixels_rasterized);
58  result->SetDouble("rasterize_time_ms",
59                    rasterize_results_.total_best_time.InMillisecondsF());
60
61  NotifyDone(result.PassAs<base::Value>());
62}
63
64void RasterizeAndRecordBenchmarkImpl::Run(LayerImpl* layer) {
65  layer->RunMicroBenchmark(this);
66}
67
68void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
69  if (layer->visible_content_rect().IsEmpty())
70    return;
71
72  PictureLayerTilingSet tiling_set(layer, layer->content_bounds());
73
74  PictureLayerTiling* tiling = tiling_set.AddTiling(layer->contents_scale_x());
75  tiling->CreateAllTilesForTesting();
76  for (PictureLayerTiling::CoverageIterator it(
77           tiling, layer->contents_scale_x(), layer->visible_content_rect());
78       it;
79       ++it) {
80    DCHECK(*it);
81
82    PicturePileImpl* picture_pile = (*it)->picture_pile();
83    gfx::Rect content_rect = (*it)->content_rect();
84    float contents_scale = (*it)->contents_scale();
85
86    base::TimeDelta min_time =
87        base::TimeDelta::FromInternalValue(std::numeric_limits<int64>::max());
88    for (int i = 0; i < rasterize_repeat_count_; ++i) {
89      SkBitmap bitmap;
90      bitmap.setConfig(SkBitmap::kARGB_8888_Config,
91                       content_rect.width(),
92                       content_rect.height());
93      bitmap.allocPixels();
94
95      SkBitmapDevice device(bitmap);
96      SkCanvas canvas(&device);
97      PicturePileImpl::Analysis analysis;
98
99      base::TimeTicks start = Now();
100      picture_pile->AnalyzeInRect(
101          content_rect, contents_scale, &analysis, NULL);
102      picture_pile->RasterToBitmap(&canvas, content_rect, contents_scale, NULL);
103      base::TimeTicks end = Now();
104      base::TimeDelta duration = end - start;
105      if (duration < min_time)
106        min_time = duration;
107    }
108
109    rasterize_results_.pixels_rasterized +=
110        content_rect.width() * content_rect.height();
111    rasterize_results_.total_best_time += min_time;
112  }
113}
114
115RasterizeAndRecordBenchmarkImpl::RasterizeResults::RasterizeResults()
116    : pixels_rasterized(0) {}
117
118RasterizeAndRecordBenchmarkImpl::RasterizeResults::~RasterizeResults() {}
119
120}  // namespace cc
121