blob: 6449b611bd6973f001a404f37c40558e0cb6985b [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
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +00009#include "SkBenchmark.h"
10#include "SkDeferredCanvas.h"
11#include "SkDevice.h"
12#include "SkImage.h"
13#include "SkSurface.h"
commit-bot@chromium.org61744ec2014-05-16 13:15:41 +000014#if SK_SUPPORT_GPU
15#include "GrRenderTarget.h"
16#endif
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000017
18class DeferredSurfaceCopyBench : public SkBenchmark {
19 enum {
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000020 kSurfaceWidth = 1000,
21 kSurfaceHeight = 1000,
22 };
23public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000024 DeferredSurfaceCopyBench(bool discardableContents) {
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000025 fDiscardableContents = discardableContents;
26 }
27
28protected:
29 virtual const char* onGetName() SK_OVERRIDE {
30 return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
31 "DeferredSurfaceCopy_nonDiscardable";
32 }
33
commit-bot@chromium.org33614712013-12-03 18:17:16 +000034 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000035 // The canvas is not actually used for this test except to provide
36 // configuration information: gpu, multisampling, size, etc?
reed@google.com2bd8b812013-11-01 13:46:54 +000037 SkImageInfo info;
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000038 info.fWidth = kSurfaceWidth;
39 info.fHeight = kSurfaceHeight;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000040 info.fColorType = kN32_SkColorType;
reed@google.comd28ba802013-09-20 19:33:52 +000041 info.fAlphaType = kPremul_SkAlphaType;
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000042 const SkRect fullCanvasRect = SkRect::MakeWH(
43 SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight));
44 SkSurface* surface;
45#if SK_SUPPORT_GPU
junov@chromium.orga2138992013-04-17 13:55:45 +000046 GrRenderTarget* rt = reinterpret_cast<GrRenderTarget*>(
47 canvas->getDevice()->accessRenderTarget());
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000048 if (NULL != rt) {
49 surface = SkSurface::NewRenderTarget(rt->getContext(), info, rt->numSamples());
50 } else
51#endif
52 {
53 surface = SkSurface::NewRaster(info);
54 }
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000055 SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000056 surface->unref();
57
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int iteration = 0; iteration < loops; iteration++) {
junov@chromium.org66070a52013-05-28 17:39:08 +000059 drawingCanvas->clear(0);
60 SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000061 SkPaint paint;
62 if (!fDiscardableContents) {
63 // If paint is not opaque, prior canvas contents are
64 // not discardable because they are needed for compositing.
65 paint.setAlpha(127);
66 }
junov@chromium.org66070a52013-05-28 17:39:08 +000067 drawingCanvas->drawRect(fullCanvasRect, paint);
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000068 // Trigger copy on write, which should be faster in the discardable case.
junov@chromium.org66070a52013-05-28 17:39:08 +000069 drawingCanvas->flush();
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000070 }
71 }
72
73private:
74 bool fDiscardableContents;
75
76 typedef SkBenchmark INHERITED;
77};
78
79//////////////////////////////////////////////////////////////////////////////
80
mtklein@google.com410e6e82013-09-13 19:52:27 +000081DEF_BENCH( return new DeferredSurfaceCopyBench(false); )
82DEF_BENCH( return new DeferredSurfaceCopyBench(true); )