blob: cbf96d0f919fbd968be97c33171be6959c7f10bd [file] [log] [blame]
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +00001
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#if SK_SUPPORT_GPU
10#include "GrRenderTarget.h"
11#endif
12#include "SkBenchmark.h"
13#include "SkDeferredCanvas.h"
14#include "SkDevice.h"
15#include "SkImage.h"
16#include "SkSurface.h"
17
18class DeferredSurfaceCopyBench : public SkBenchmark {
19 enum {
20 N = SkBENCHLOOP(5),
21 kSurfaceWidth = 1000,
22 kSurfaceHeight = 1000,
23 };
24public:
25 DeferredSurfaceCopyBench(void* param, bool discardableContents) : SkBenchmark(param) {
26 fDiscardableContents = discardableContents;
27 }
28
29protected:
30 virtual const char* onGetName() SK_OVERRIDE {
31 return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
32 "DeferredSurfaceCopy_nonDiscardable";
33 }
34
35 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
36 // The canvas is not actually used for this test except to provide
37 // configuration information: gpu, multisampling, size, etc?
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000038 SkImage::Info info;
39 info.fWidth = kSurfaceWidth;
40 info.fHeight = kSurfaceHeight;
41 info.fColorType = SkImage::kPMColor_ColorType;
42 info.fAlphaType = SkImage::kPremul_AlphaType;
43 const SkRect fullCanvasRect = SkRect::MakeWH(
44 SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight));
45 SkSurface* surface;
46#if SK_SUPPORT_GPU
junov@chromium.orga2138992013-04-17 13:55:45 +000047 GrRenderTarget* rt = reinterpret_cast<GrRenderTarget*>(
48 canvas->getDevice()->accessRenderTarget());
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000049 if (NULL != rt) {
50 surface = SkSurface::NewRenderTarget(rt->getContext(), info, rt->numSamples());
51 } else
52#endif
53 {
54 surface = SkSurface::NewRaster(info);
55 }
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000056 SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000057 surface->unref();
58
59 for (int iteration = 0; iteration < N; iteration++) {
junov@chromium.org66070a52013-05-28 17:39:08 +000060 drawingCanvas->clear(0);
61 SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000062 SkPaint paint;
63 if (!fDiscardableContents) {
64 // If paint is not opaque, prior canvas contents are
65 // not discardable because they are needed for compositing.
66 paint.setAlpha(127);
67 }
junov@chromium.org66070a52013-05-28 17:39:08 +000068 drawingCanvas->drawRect(fullCanvasRect, paint);
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000069 // Trigger copy on write, which should be faster in the discardable case.
junov@chromium.org66070a52013-05-28 17:39:08 +000070 drawingCanvas->flush();
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000071 }
72 }
73
74private:
75 bool fDiscardableContents;
76
77 typedef SkBenchmark INHERITED;
78};
79
80//////////////////////////////////////////////////////////////////////////////
81
82static SkBenchmark* Fact0(void* p) { return new DeferredSurfaceCopyBench(p, false); }
83static SkBenchmark* Fact1(void* p) { return new DeferredSurfaceCopyBench(p, true); }
84
85static BenchRegistry gReg0(Fact0);
86static BenchRegistry gReg1(Fact1);