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 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 32 | 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.com | bf3ef28 | 2012-09-17 11:52:48 +0000 | [diff] [blame] | 38 | device->unref(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 39 | |
| 40 | initDeferredCanvas(deferredCanvas); |
| 41 | |
| 42 | for (int i = 0; i < N; i++) { |
| 43 | drawInDeferredCanvas(deferredCanvas); |
| 44 | } |
| 45 | |
| 46 | finalizeDeferredCanvas(deferredCanvas); |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 47 | deferredCanvas->flush(); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 50 | virtual void initDeferredCanvas(SkDeferredCanvas* canvas) = 0; |
| 51 | virtual void drawInDeferredCanvas(SkDeferredCanvas* canvas) = 0; |
| 52 | virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) = 0; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 53 | |
| 54 | SkString fName; |
| 55 | |
| 56 | private: |
| 57 | typedef SkBenchmark INHERITED; |
| 58 | }; |
| 59 | |
| 60 | class SimpleNotificationClient : public SkDeferredCanvas::NotificationClient { |
| 61 | public: |
| 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;} |
| 68 | private: |
| 69 | bool fDummy; |
robertphillips@google.com | 5990397 | 2013-02-07 21:02:23 +0000 | [diff] [blame] | 70 | |
| 71 | typedef SkDeferredCanvas::NotificationClient INHERITED; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // Test that records very simple draw operations. |
| 75 | // This benchmark aims to capture performance fluctuations in the recording |
| 76 | // overhead of SkDeferredCanvas |
| 77 | class DeferredRecordBench : public DeferredCanvasBench { |
| 78 | public: |
| 79 | DeferredRecordBench(void* param) |
| 80 | : INHERITED(param, "record") { |
| 81 | } |
| 82 | |
| 83 | enum { |
| 84 | M = SkBENCHLOOP(700), // number of individual draws in each loop |
| 85 | }; |
| 86 | protected: |
| 87 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 88 | virtual void initDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE { |
| 89 | canvas->setNotificationClient(&fNotificationClient); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 90 | } |
| 91 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 92 | virtual void drawInDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE { |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 93 | SkRect rect; |
| 94 | rect.setXYWH(0, 0, 10, 10); |
| 95 | SkPaint paint; |
| 96 | for (int i = 0; i < M; i++) { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 97 | 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.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 104 | virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE { |
| 105 | canvas->clear(0x0); |
| 106 | canvas->setNotificationClient(NULL); |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 107 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 109 | private: |
| 110 | typedef DeferredCanvasBench INHERITED; |
junov@chromium.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 111 | SimpleNotificationClient fNotificationClient; |
junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | |
| 117 | static SkBenchmark* Fact0(void* p) { return new DeferredRecordBench(p); } |
| 118 | |
| 119 | static BenchRegistry gReg0(Fact0); |