blob: 0666cc421166b302c652adbad2f782129e1e6b00 [file] [log] [blame]
djsollen@google.comdde718c2012-05-30 16:50:11 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkBenchmark.h"
8#include "SkCanvas.h"
9#include "SkColor.h"
10#include "SkPaint.h"
11#include "SkPicture.h"
robertphillips@google.com770963f2014-04-18 18:04:41 +000012#include "SkPictureRecorder.h"
djsollen@google.comdde718c2012-05-30 16:50:11 +000013#include "SkPoint.h"
reed@google.com7fe64092012-07-10 13:17:45 +000014#include "SkRandom.h"
djsollen@google.comdde718c2012-05-30 16:50:11 +000015#include "SkRect.h"
16#include "SkString.h"
17
18class PictureRecordBench : public SkBenchmark {
19public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000020 PictureRecordBench(const char name[]) {
djsollen@google.comdde718c2012-05-30 16:50:11 +000021 fName.printf("picture_record_%s", name);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022 }
23
24 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
25 return backend == kNonRendering_Backend;
djsollen@google.comdde718c2012-05-30 16:50:11 +000026 }
27
28 enum {
djsollen@google.comdde718c2012-05-30 16:50:11 +000029 PICTURE_WIDTH = 1000,
30 PICTURE_HEIGHT = 4000,
31 };
32protected:
mtklein@google.com93d2f552013-09-17 18:58:53 +000033 virtual const char* onGetName() SK_OVERRIDE {
djsollen@google.comdde718c2012-05-30 16:50:11 +000034 return fName.c_str();
35 }
djsollen@google.comdde718c2012-05-30 16:50:11 +000036private:
mtklein@google.com93d2f552013-09-17 18:58:53 +000037 SkString fName;
djsollen@google.comdde718c2012-05-30 16:50:11 +000038 typedef SkBenchmark INHERITED;
39};
40
mtklein@google.com93d2f552013-09-17 18:58:53 +000041
42static const int kMaxLoopsPerCanvas = 10000;
43
djsollen@google.comdde718c2012-05-30 16:50:11 +000044/*
45 * An SkPicture has internal dictionaries to store bitmaps, matrices, paints,
46 * and regions. This bench populates those dictionaries to test the speed of
47 * reading and writing to those particular dictionary data structures.
48 */
49class DictionaryRecordBench : public PictureRecordBench {
50public:
mtklein@google.com93d2f552013-09-17 18:58:53 +000051 DictionaryRecordBench() : INHERITED("dictionaries") {}
djsollen@google.comdde718c2012-05-30 16:50:11 +000052
djsollen@google.comdde718c2012-05-30 16:50:11 +000053protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000055 SkPictureRecorder recorder;
mtklein@google.com93d2f552013-09-17 18:58:53 +000056 SkCanvas* canvas = NULL;
djsollen@google.comdde718c2012-05-30 16:50:11 +000057
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 const SkPoint translateDelta = getTranslateDelta(loops);
djsollen@google.comdde718c2012-05-30 16:50:11 +000059
commit-bot@chromium.org33614712013-12-03 18:17:16 +000060 for (int i = 0; i < loops; i++) {
mtklein@google.com93d2f552013-09-17 18:58:53 +000061 if (0 == i % kMaxLoopsPerCanvas) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000062 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000063 canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
mtklein@google.com93d2f552013-09-17 18:58:53 +000064 }
djsollen@google.comdde718c2012-05-30 16:50:11 +000065
66 SkColor color = SK_ColorYELLOW + (i % 255);
mtklein@google.comc2897432013-09-10 19:23:38 +000067 SkIRect rect = SkIRect::MakeWH(i % PICTURE_WIDTH, i % PICTURE_HEIGHT);
djsollen@google.comdde718c2012-05-30 16:50:11 +000068
69 canvas->save();
70
71 // set the clip to the given region
72 SkRegion region;
73 region.setRect(rect);
74 canvas->clipRegion(region);
75
76 // fill the clip with a color
77 SkPaint paint;
78 paint.setColor(color);
79 canvas->drawPaint(paint);
80
81 // set a matrix on the canvas
82 SkMatrix matrix;
83 matrix.setRotate(SkIntToScalar(i % 360));
84 canvas->setMatrix(matrix);
85
86 // create a simple bitmap
87 SkBitmap bitmap;
88 bitmap.setConfig(SkBitmap::kRGB_565_Config, 10, 10);
89 bitmap.allocPixels();
90
91 // draw a single color into the bitmap
92 SkCanvas bitmapCanvas(bitmap);
93 bitmapCanvas.drawColor(SkColorSetA(color, i % 255));
94
95 // draw the bitmap onto the canvas
96 canvas->drawBitmapMatrix(bitmap, matrix);
97
98 canvas->restore();
99 canvas->translate(translateDelta.fX, translateDelta.fY);
100 }
101 }
102
mtklein@google.comc2897432013-09-10 19:23:38 +0000103 SkPoint getTranslateDelta(int M) {
djsollen@google.comdde718c2012-05-30 16:50:11 +0000104 SkIPoint canvasSize = onGetSize();
105 return SkPoint::Make(SkIntToScalar((PICTURE_WIDTH - canvasSize.fX)/M),
106 SkIntToScalar((PICTURE_HEIGHT- canvasSize.fY)/M));
107 }
108private:
109 typedef PictureRecordBench INHERITED;
110};
111
junov@chromium.orgef760602012-06-27 20:03:16 +0000112/*
113 * Populates the SkPaint dictionary with a large number of unique paint
114 * objects that differ only by color
115 */
116class UniquePaintDictionaryRecordBench : public PictureRecordBench {
117public:
mtklein@google.com93d2f552013-09-17 18:58:53 +0000118 UniquePaintDictionaryRecordBench() : INHERITED("unique_paint_dictionary") { }
junov@chromium.orgef760602012-06-27 20:03:16 +0000119
junov@chromium.orgef760602012-06-27 20:03:16 +0000120protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000121 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000122 SkRandom rand;
mtklein@google.com5e5239e2013-09-17 17:14:25 +0000123 SkPaint paint;
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000124 SkPictureRecorder recorder;
mtklein@google.com5e5239e2013-09-17 17:14:25 +0000125 SkCanvas* canvas = NULL;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000126 for (int i = 0; i < loops; i++) {
mtklein@google.com93d2f552013-09-17 18:58:53 +0000127 if (0 == i % kMaxLoopsPerCanvas) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000128 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000129 canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
mtklein@google.com5e5239e2013-09-17 17:14:25 +0000130 }
reed@google.com7fe64092012-07-10 13:17:45 +0000131 paint.setColor(rand.nextU());
junov@chromium.orgef760602012-06-27 20:03:16 +0000132 canvas->drawPaint(paint);
133 }
134 }
135
136private:
137 typedef PictureRecordBench INHERITED;
138};
139
140/*
141 * Populates the SkPaint dictionary with a number of unique paint
commit-bot@chromium.org0c4e21d2013-07-11 14:26:09 +0000142 * objects that get reused repeatedly.
143 *
144 * Re-creating the paint objects in the inner loop slows the benchmark down 10%.
145 * Using setColor(i % objCount) instead of a random color creates a very high rate
146 * of hash conflicts, slowing us down 12%.
junov@chromium.orgef760602012-06-27 20:03:16 +0000147 */
148class RecurringPaintDictionaryRecordBench : public PictureRecordBench {
149public:
mtklein@google.com93d2f552013-09-17 18:58:53 +0000150 RecurringPaintDictionaryRecordBench() : INHERITED("recurring_paint_dictionary") {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000151 SkRandom rand;
commit-bot@chromium.org0c4e21d2013-07-11 14:26:09 +0000152 for (int i = 0; i < ObjCount; i++) {
153 fPaint[i].setColor(rand.nextU());
154 }
155 }
junov@chromium.orgef760602012-06-27 20:03:16 +0000156
157 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000158 ObjCount = 100, // number of unique paint objects
junov@chromium.orgef760602012-06-27 20:03:16 +0000159 };
160protected:
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000161 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000162 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000163 SkCanvas* canvas = recorder.beginRecording(PICTURE_WIDTH, PICTURE_HEIGHT, NULL, 0);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000164 for (int i = 0; i < loops; i++) {
commit-bot@chromium.org0c4e21d2013-07-11 14:26:09 +0000165 canvas->drawPaint(fPaint[i % ObjCount]);
junov@chromium.orgef760602012-06-27 20:03:16 +0000166 }
167 }
168
169private:
commit-bot@chromium.org0c4e21d2013-07-11 14:26:09 +0000170 SkPaint fPaint [ObjCount];
junov@chromium.orgef760602012-06-27 20:03:16 +0000171 typedef PictureRecordBench INHERITED;
172};
173
djsollen@google.comdde718c2012-05-30 16:50:11 +0000174///////////////////////////////////////////////////////////////////////////////
175
mtklein@google.com410e6e82013-09-13 19:52:27 +0000176DEF_BENCH( return new DictionaryRecordBench(); )
177DEF_BENCH( return new UniquePaintDictionaryRecordBench(); )
178DEF_BENCH( return new RecurringPaintDictionaryRecordBench(); )