junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 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: |
| 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: |
| 24 | virtual const char* onGetName() { |
| 25 | return fName.c_str(); |
| 26 | } |
| 27 | |
| 28 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | bf3ef28 | 2012-09-17 11:52:48 +0000 | [diff] [blame] | 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(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 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: |
| 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;} |
| 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: |
| 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 | |
| 82 | virtual void initDeferredCanvas(SkDeferredCanvas& canvas) SK_OVERRIDE { |
junov@chromium.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 83 | canvas.setNotificationClient(&fNotificationClient); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 84 | } |
| 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.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 100 | canvas.setNotificationClient(NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 101 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 102 | |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 103 | private: |
| 104 | typedef DeferredCanvasBench INHERITED; |
junov@chromium.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 105 | SimpleNotificationClient fNotificationClient; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | |
| 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | static SkBenchmark* Fact0(void* p) { return new DeferredRecordBench(p); } |
| 112 | |
| 113 | static BenchRegistry gReg0(Fact0); |