blob: e0bf8e7a78e07042aa3c75ba00e2d4e896c43ebc [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
tfarinaf168b862014-06-19 12:32:29 -07009#include "Benchmark.h"
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000010#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
tfarinaf168b862014-06-19 12:32:29 -070018class DeferredSurfaceCopyBench : public Benchmark {
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000019 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:
mtklein72c9faa2015-01-09 10:06:39 -080029 const char* onGetName() SK_OVERRIDE {
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000030 return fDiscardableContents ? "DeferredSurfaceCopy_discardable" :
31 "DeferredSurfaceCopy_nonDiscardable";
32 }
33
mtklein72c9faa2015-01-09 10:06:39 -080034 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?
reed52d9ac62014-06-30 09:05:34 -070037 SkImageInfo info = SkImageInfo::MakeN32Premul(kSurfaceWidth, kSurfaceHeight);
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000038 const SkRect fullCanvasRect = SkRect::MakeWH(
39 SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight));
reed52d9ac62014-06-30 09:05:34 -070040 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
reed337c2dd2014-10-02 12:58:48 -070041
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
commit-bot@chromium.orgcb622242013-08-09 14:24:59 +000048 SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface));
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000049
commit-bot@chromium.org33614712013-12-03 18:17:16 +000050 for (int iteration = 0; iteration < loops; iteration++) {
junov@chromium.org66070a52013-05-28 17:39:08 +000051 drawingCanvas->clear(0);
52 SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot());
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000053 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 }
junov@chromium.org66070a52013-05-28 17:39:08 +000059 drawingCanvas->drawRect(fullCanvasRect, paint);
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000060 // Trigger copy on write, which should be faster in the discardable case.
junov@chromium.org66070a52013-05-28 17:39:08 +000061 drawingCanvas->flush();
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000062 }
63 }
64
65private:
66 bool fDiscardableContents;
67
tfarinaf168b862014-06-19 12:32:29 -070068 typedef Benchmark INHERITED;
junov@chromium.orgd61ba6e2013-04-17 13:43:04 +000069};
70
71//////////////////////////////////////////////////////////////////////////////
72
mtklein@google.com410e6e82013-09-13 19:52:27 +000073DEF_BENCH( return new DeferredSurfaceCopyBench(false); )
74DEF_BENCH( return new DeferredSurfaceCopyBench(true); )