3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkDeferredCanvas.h"
13 #include "SkSurface.h"
15 #include "GrRenderTarget.h"
18 class DeferredSurfaceCopyBench : public Benchmark {
21 kSurfaceHeight = 1000,
24 DeferredSurfaceCopyBench(bool discardableContents) {
25 fDiscardableContents = discardableContents;
29 const char* onGetName() SK_OVERRIDE {
30 return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
31 "DeferredSurfaceCopy_nonDiscardable";
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));
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?
48 SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
50 for (int iteration = 0; iteration < loops; iteration++) {
51 drawingCanvas->clear(0);
52 SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
54 if (!fDiscardableContents) {
55 // If paint is not opaque, prior canvas contents are
56 // not discardable because they are needed for compositing.
59 drawingCanvas->drawRect(fullCanvasRect, paint);
60 // Trigger copy on write, which should be faster in the discardable case.
61 drawingCanvas->flush();
66 bool fDiscardableContents;
68 typedef Benchmark INHERITED;
71 //////////////////////////////////////////////////////////////////////////////
73 DEF_BENCH( return new DeferredSurfaceCopyBench(false); )
74 DEF_BENCH( return new DeferredSurfaceCopyBench(true); )