aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/DeferredSurfaceCopyBench.cpp
blob: f9f8c8e95b8a5f7a59215af43fcdfcd2b72fb3b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#if SK_SUPPORT_GPU
#include "GrRenderTarget.h"
#endif
#include "SkBenchmark.h"
#include "SkDeferredCanvas.h"
#include "SkDevice.h"
#include "SkImage.h"
#include "SkSurface.h"

class DeferredSurfaceCopyBench : public SkBenchmark {
    enum {
        kSurfaceWidth = 1000,
        kSurfaceHeight = 1000,
    };
public:
    DeferredSurfaceCopyBench(bool discardableContents) {
        fDiscardableContents = discardableContents;
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE {
        return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
            "DeferredSurfaceCopy_nonDiscardable";
    }

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        // The canvas is not actually used for this test except to provide
        // configuration information: gpu, multisampling, size, etc?
        SkImage::Info info;
        info.fWidth = kSurfaceWidth;
        info.fHeight = kSurfaceHeight;
        info.fColorType = SkImage::kPMColor_ColorType;
        info.fAlphaType = kPremul_SkAlphaType;
        const SkRect fullCanvasRect = SkRect::MakeWH(
            SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight));
        SkSurface* surface;
#if SK_SUPPORT_GPU
        GrRenderTarget* rt = reinterpret_cast<GrRenderTarget*>(
            canvas->getDevice()->accessRenderTarget());
        if (NULL != rt) {
            surface = SkSurface::NewRenderTarget(rt->getContext(), info, rt->numSamples());
        } else
#endif
        {
            surface = SkSurface::NewRaster(info);
        }
        SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
        surface->unref();

        for (int iteration = 0; iteration < this->getLoops(); iteration++) {
            drawingCanvas->clear(0);
            SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
            SkPaint paint;
            if (!fDiscardableContents) {
                // If paint is not opaque, prior canvas contents are
                // not discardable because they are needed for compositing.
                paint.setAlpha(127);
            }
            drawingCanvas->drawRect(fullCanvasRect, paint);
            // Trigger copy on write, which should be faster in the discardable case.
            drawingCanvas->flush();
        }
    }

private:
    bool fDiscardableContents;

    typedef SkBenchmark INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

DEF_BENCH( return new DeferredSurfaceCopyBench(false); )
DEF_BENCH( return new DeferredSurfaceCopyBench(true); )