Home | History | Annotate | Download | only in bench
      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(const char name[])  {
     15         fName.printf("deferred_canvas_%s", name);
     16     }
     17 
     18     enum {
     19         CANVAS_WIDTH = 200,
     20         CANVAS_HEIGHT = 200,
     21     };
     22 protected:
     23     virtual const char* onGetName() {
     24         return fName.c_str();
     25     }
     26 
     27     virtual void onDraw(const int loops, SkCanvas* canvas) {
     28         SkBaseDevice *device = canvas->getDevice()->createCompatibleDevice(
     29             SkBitmap::kARGB_8888_Config, CANVAS_WIDTH, CANVAS_HEIGHT, false);
     30 
     31         SkAutoTUnref<SkDeferredCanvas> deferredCanvas(SkDeferredCanvas::Create(device));
     32         device->unref();
     33 
     34         initDeferredCanvas(deferredCanvas);
     35         drawInDeferredCanvas(loops, deferredCanvas);
     36         finalizeDeferredCanvas(deferredCanvas);
     37         deferredCanvas->flush();
     38     }
     39 
     40     virtual void initDeferredCanvas(SkDeferredCanvas* canvas) = 0;
     41     virtual void drawInDeferredCanvas(const int loops, SkDeferredCanvas* canvas) = 0;
     42     virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) = 0;
     43 
     44     SkString fName;
     45 
     46 private:
     47     typedef SkBenchmark INHERITED;
     48 };
     49 
     50 class SimpleNotificationClient : public SkDeferredCanvas::NotificationClient {
     51 public:
     52     SimpleNotificationClient() : fDummy(false) {}
     53 
     54     //bogus virtual implementations that just do something small
     55     virtual void prepareForDraw() SK_OVERRIDE {fDummy = true;}
     56     virtual void storageAllocatedForRecordingChanged(size_t) SK_OVERRIDE {fDummy = false;}
     57     virtual void flushedDrawCommands() SK_OVERRIDE {fDummy = !fDummy;}
     58 private:
     59     bool fDummy;
     60 
     61     typedef SkDeferredCanvas::NotificationClient INHERITED;
     62 };
     63 
     64 // Test that records very simple draw operations.
     65 // This benchmark aims to capture performance fluctuations in the recording
     66 // overhead of SkDeferredCanvas
     67 class DeferredRecordBench : public DeferredCanvasBench {
     68 public:
     69     DeferredRecordBench()
     70         : INHERITED("record") {
     71     }
     72 
     73 protected:
     74 
     75     virtual void initDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE {
     76         canvas->setNotificationClient(&fNotificationClient);
     77     }
     78 
     79     virtual void drawInDeferredCanvas(const int loops, SkDeferredCanvas* canvas) SK_OVERRIDE {
     80         SkRect rect;
     81         rect.setXYWH(0, 0, 10, 10);
     82         SkPaint paint;
     83         for (int i = 0; i < loops; i++) {
     84             canvas->save(SkCanvas::kMatrixClip_SaveFlag);
     85             canvas->translate(SkIntToScalar(i * 27 % CANVAS_WIDTH), SkIntToScalar(i * 13 % CANVAS_HEIGHT));
     86             canvas->drawRect(rect, paint);
     87             canvas->restore();
     88         }
     89     }
     90 
     91     virtual void finalizeDeferredCanvas(SkDeferredCanvas* canvas) SK_OVERRIDE {
     92         canvas->clear(0x0);
     93         canvas->setNotificationClient(NULL);
     94     }
     95 
     96 private:
     97     typedef DeferredCanvasBench INHERITED;
     98     SimpleNotificationClient fNotificationClient;
     99 };
    100 
    101 
    102 ///////////////////////////////////////////////////////////////////////////////
    103 
    104 DEF_BENCH( return new DeferredRecordBench(); )
    105