1 /*
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
12 class DeferredCanvasBench : public SkBenchmark {
13 public:
DeferredCanvasBench(void * param,const char name[])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 };
23 protected:
onGetName()24 virtual const char* onGetName() {
25 return fName.c_str();
26 }
27
onDraw(SkCanvas * canvas)28 virtual void onDraw(SkCanvas* canvas) {
29 SkDevice *device = canvas->getDevice()->createCompatibleDevice(
30 SkBitmap::kARGB_8888_Config, CANVAS_WIDTH, CANVAS_HEIGHT, false);
31
32 SkDeferredCanvas deferredCanvas(device);
33
34 device->unref();
35
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
52 private:
53 typedef SkBenchmark INHERITED;
54 };
55
56 class SimpleNotificationClient : public SkDeferredCanvas::NotificationClient {
57 public:
SimpleNotificationClient()58 SimpleNotificationClient() : fDummy(false) {}
59
60 //bogus virtual implementations that just do something small
prepareForDraw()61 virtual void prepareForDraw() SK_OVERRIDE {fDummy = true;}
storageAllocatedForRecordingChanged(size_t)62 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {fDummy = false;}
flushedDrawCommands()63 virtual void flushedDrawCommands() SK_OVERRIDE {fDummy = !fDummy;}
64 private:
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
71 class DeferredRecordBench : public DeferredCanvasBench {
72 public:
DeferredRecordBench(void * param)73 DeferredRecordBench(void* param)
74 : INHERITED(param, "record") {
75 }
76
77 enum {
78 M = SkBENCHLOOP(700), // number of individual draws in each loop
79 };
80 protected:
81
initDeferredCanvas(SkDeferredCanvas & canvas)82 virtual void initDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE {
83 canvas.setNotificationClient(&fNotificationClient);
84 }
85
drawInDeferredCanvas(SkDeferredCanvas & canvas)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
finalizeDeferredCanvas(SkDeferredCanvas & canvas)98 virtual void finalizeDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE {
99 canvas.clear(0x0);
100 canvas.setNotificationClient(NULL);
101 }
102
103 private:
104 typedef DeferredCanvasBench INHERITED;
105 SimpleNotificationClient fNotificationClient;
106 };
107
108
109 ///////////////////////////////////////////////////////////////////////////////
110
Fact0(void * p)111 static SkBenchmark* Fact0(void* p) { return new DeferredRecordBench(p); }
112
113 static BenchRegistry gReg0(Fact0);
114