e0bf8e7a78e07042aa3c75ba00e2d4e896c43ebc
[platform/upstream/libSkiaSharp.git] / bench / DeferredSurfaceCopyBench.cpp
1
2 /*
3  * Copyright 2013 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8
9 #include "Benchmark.h"
10 #include "SkDeferredCanvas.h"
11 #include "SkDevice.h"
12 #include "SkImage.h"
13 #include "SkSurface.h"
14 #if SK_SUPPORT_GPU
15 #include "GrRenderTarget.h"
16 #endif
17
18 class DeferredSurfaceCopyBench : public Benchmark {
19     enum {
20         kSurfaceWidth = 1000,
21         kSurfaceHeight = 1000,
22     };
23 public:
24     DeferredSurfaceCopyBench(bool discardableContents) {
25         fDiscardableContents = discardableContents;
26     }
27
28 protected:
29     const char* onGetName() SK_OVERRIDE {
30         return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
31             "DeferredSurfaceCopy_nonDiscardable";
32     }
33
34     void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
35         // The canvas is not actually used for this test except to provide
36         // configuration information: gpu, multisampling, size, etc?
37         SkImageInfo info = SkImageInfo::MakeN32Premul(kSurfaceWidth, kSurfaceHeight);
38         const SkRect fullCanvasRect = SkRect::MakeWH(
39             SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight));
40         SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
41
42         // newSurface() can return NULL for several reasons, so we need to check
43         if (NULL == surface.get()) {
44             SkDebugf("DeferredSurfaceCopyBench newSurface failed, bench results are meaningless\n");
45             return; // should we signal the caller that we hit an error?
46         }
47
48         SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
49
50         for (int iteration = 0; iteration < loops; iteration++) {
51             drawingCanvas->clear(0);
52             SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
53             SkPaint paint;
54             if (!fDiscardableContents) {
55                 // If paint is not opaque, prior canvas contents are
56                 // not discardable because they are needed for compositing.
57                 paint.setAlpha(127);
58             }
59             drawingCanvas->drawRect(fullCanvasRect, paint);
60             // Trigger copy on write, which should be faster in the discardable case.
61             drawingCanvas->flush();
62         }
63     }
64
65 private:
66     bool fDiscardableContents;
67
68     typedef Benchmark INHERITED;
69 };
70
71 //////////////////////////////////////////////////////////////////////////////
72
73 DEF_BENCH( return new DeferredSurfaceCopyBench(false); )
74 DEF_BENCH( return new DeferredSurfaceCopyBench(true); )