blob: a1a390163209fc24307cf3f4a05998994df8a667 [file] [log] [blame]
junov@chromium.org9ed02b92012-08-14 13:36:26 +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 "SkDeferredCanvas.h"
9#include "SkDevice.h"
10#include "SkString.h"
11
12class DeferredCanvasBench : public SkBenchmark {
13public:
14 DeferredCanvasBench(void* param, const char name[]) : INHERITED(param) {
15 fName.printf("deferred_canvas_%s", name);
16 }
17
18 enum {
19 N = SkBENCHLOOP(25), // number of times to create the picture
20 CANVAS_WIDTH = 200,
21 CANVAS_HEIGHT = 200,
22 };
23protected:
24 virtual const char* onGetName() {
25 return fName.c_str();
26 }
27
28 virtual void onDraw(SkCanvas* canvas) {
robertphillips@google.combf3ef282012-09-17 11:52:48 +000029 SkDevice *device = canvas->getDevice()->createCompatibleDevice(
30 SkBitmap::kARGB_8888_Config, CANVAS_WIDTH, CANVAS_HEIGHT, false);
31
32 SkDeferredCanvas deferredCanvas(device);
33
34 device->unref();
junov@chromium.org9ed02b92012-08-14 13:36:26 +000035
36 initDeferredCanvas(deferredCanvas);
37
38 for (int i = 0; i < N; i++) {
39 drawInDeferredCanvas(deferredCanvas);
40 }
41
42 finalizeDeferredCanvas(deferredCanvas);
43 deferredCanvas.flush();
44 }
45
46 virtual void initDeferredCanvas(SkDeferredCanvas& canvas) = 0;
47 virtual void drawInDeferredCanvas(SkDeferredCanvas& canvas) = 0;
48 virtual void finalizeDeferredCanvas(SkDeferredCanvas& canvas) = 0;
49
50 SkString fName;
51
52private:
53 typedef SkBenchmark INHERITED;
54};
55
56class SimpleNotificationClient : public SkDeferredCanvas::NotificationClient {
57public:
58 SimpleNotificationClient() : fDummy(false) {}
59
60 //bogus virtual implementations that just do something small
61 virtual void prepareForDraw() SK_OVERRIDE {fDummy = true;}
62 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {fDummy = false;}
63 virtual void flushedDrawCommands() SK_OVERRIDE {fDummy = !fDummy;}
64private:
65 bool fDummy;
66};
67
68// Test that records very simple draw operations.
69// This benchmark aims to capture performance fluctuations in the recording
70// overhead of SkDeferredCanvas
71class DeferredRecordBench : public DeferredCanvasBench {
72public:
73 DeferredRecordBench(void* param)
74 : INHERITED(param, "record") {
75 }
76
77 enum {
78 M = SkBENCHLOOP(700), // number of individual draws in each loop
79 };
80protected:
81
82 virtual void initDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE {
junov@chromium.org52805482012-08-20 14:25:04 +000083 canvas.setNotificationClient(&fNotificationClient);
junov@chromium.org9ed02b92012-08-14 13:36:26 +000084 }
85
86 virtual void drawInDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE {
87 SkRect rect;
88 rect.setXYWH(0, 0, 10, 10);
89 SkPaint paint;
90 for (int i = 0; i < M; i++) {
91 canvas.save(SkCanvas::kMatrixClip_SaveFlag);
92 canvas.translate(SkIntToScalar(i * 27 % CANVAS_WIDTH), SkIntToScalar(i * 13 % CANVAS_HEIGHT));
93 canvas.drawRect(rect, paint);
94 canvas.restore();
95 }
96 }
97
98 virtual void finalizeDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE {
99 canvas.clear(0x0);
junov@chromium.org52805482012-08-20 14:25:04 +0000100 canvas.setNotificationClient(NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000101 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000102
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000103private:
104 typedef DeferredCanvasBench INHERITED;
junov@chromium.org52805482012-08-20 14:25:04 +0000105 SimpleNotificationClient fNotificationClient;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000106};
107
108
109///////////////////////////////////////////////////////////////////////////////
110
111static SkBenchmark* Fact0(void* p) { return new DeferredRecordBench(p); }
112
113static BenchRegistry gReg0(Fact0);