blob: 5ef64dbac962ffbdd9980ad4ce8afa628c0ae450 [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
junov@chromium.org66070a52013-05-28 17:39:08 +000032 SkAutoTUnref<SkDeferredCanvas> deferredCanvas(
33#if SK_DEFERRED_CANVAS_USES_FACTORIES
34 SkDeferredCanvas::Create(device));
35#else
36 SkNEW_ARGS(SkDeferredCanvas, (device)));
37#endif
robertphillips@google.combf3ef282012-09-17 11:52:48 +000038 device->unref();
junov@chromium.org9ed02b92012-08-14 13:36:26 +000039
40 initDeferredCanvas(deferredCanvas);
41
42 for (int i = 0; i < N; i++) {
43 drawInDeferredCanvas(deferredCanvas);
44 }
45
46 finalizeDeferredCanvas(deferredCanvas);
junov@chromium.org66070a52013-05-28 17:39:08 +000047 deferredCanvas->flush();
junov@chromium.org9ed02b92012-08-14 13:36:26 +000048 }
49
junov@chromium.org66070a52013-05-28 17:39:08 +000050 virtual void initDeferredCanvas(SkDeferredCanvas* canvas) = 0;
51 virtual void drawInDeferredCanvas(SkDeferredCanvas* canvas) = 0;
52 virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) = 0;
junov@chromium.org9ed02b92012-08-14 13:36:26 +000053
54 SkString fName;
55
56private:
57 typedef SkBenchmark INHERITED;
58};
59
60class SimpleNotificationClient : public SkDeferredCanvas::NotificationClient {
61public:
62 SimpleNotificationClient() : fDummy(false) {}
63
64 //bogus virtual implementations that just do something small
65 virtual void prepareForDraw() SK_OVERRIDE {fDummy = true;}
66 virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {fDummy = false;}
67 virtual void flushedDrawCommands() SK_OVERRIDE {fDummy = !fDummy;}
68private:
69 bool fDummy;
robertphillips@google.com59903972013-02-07 21:02:23 +000070
71 typedef SkDeferredCanvas::NotificationClient INHERITED;
junov@chromium.org9ed02b92012-08-14 13:36:26 +000072};
73
74// Test that records very simple draw operations.
75// This benchmark aims to capture performance fluctuations in the recording
76// overhead of SkDeferredCanvas
77class DeferredRecordBench : public DeferredCanvasBench {
78public:
79 DeferredRecordBench(void* param)
80 : INHERITED(param, "record") {
81 }
82
83 enum {
84 M = SkBENCHLOOP(700), // number of individual draws in each loop
85 };
86protected:
87
junov@chromium.org66070a52013-05-28 17:39:08 +000088 virtual void initDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE {
89 canvas->setNotificationClient(&fNotificationClient);
junov@chromium.org9ed02b92012-08-14 13:36:26 +000090 }
91
junov@chromium.org66070a52013-05-28 17:39:08 +000092 virtual void drawInDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE {
junov@chromium.org9ed02b92012-08-14 13:36:26 +000093 SkRect rect;
94 rect.setXYWH(0, 0, 10, 10);
95 SkPaint paint;
96 for (int i = 0; i < M; i++) {
junov@chromium.org66070a52013-05-28 17:39:08 +000097 canvas->save(SkCanvas::kMatrixClip_SaveFlag);
98 canvas->translate(SkIntToScalar(i * 27 % CANVAS_WIDTH), SkIntToScalar(i * 13 % CANVAS_HEIGHT));
99 canvas->drawRect(rect, paint);
100 canvas->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000101 }
102 }
103
junov@chromium.org66070a52013-05-28 17:39:08 +0000104 virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE {
105 canvas->clear(0x0);
106 canvas->setNotificationClient(NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000107 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000108
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000109private:
110 typedef DeferredCanvasBench INHERITED;
junov@chromium.org52805482012-08-20 14:25:04 +0000111 SimpleNotificationClient fNotificationClient;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000112};
113
114
115///////////////////////////////////////////////////////////////////////////////
116
117static SkBenchmark* Fact0(void* p) { return new DeferredRecordBench(p); }
118
119static BenchRegistry gReg0(Fact0);