junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 1 | |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 9 | #include "Benchmark.h" |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 10 | #include "SkDeferredCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkImage.h" |
| 13 | #include "SkSurface.h" |
commit-bot@chromium.org | 61744ec | 2014-05-16 13:15:41 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
| 15 | #include "GrRenderTarget.h" |
| 16 | #endif |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 17 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class DeferredSurfaceCopyBench : public Benchmark { |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 19 | enum { |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 20 | kSurfaceWidth = 1000, |
| 21 | kSurfaceHeight = 1000, |
| 22 | }; |
| 23 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 24 | DeferredSurfaceCopyBench(bool discardableContents) { |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 25 | fDiscardableContents = discardableContents; |
| 26 | } |
| 27 | |
| 28 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 29 | const char* onGetName() SK_OVERRIDE { |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 30 | return fDiscardableContents ? "DeferredSurfaceCopy_discardable" : |
| 31 | "DeferredSurfaceCopy_nonDiscardable"; |
| 32 | } |
| 33 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 34 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 35 | // The canvas is not actually used for this test except to provide |
| 36 | // configuration information: gpu, multisampling, size, etc? |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 37 | SkImageInfo info = SkImageInfo::MakeN32Premul(kSurfaceWidth, kSurfaceHeight); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 38 | const SkRect fullCanvasRect = SkRect::MakeWH( |
| 39 | SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight)); |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 40 | SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); |
reed | 337c2dd | 2014-10-02 12:58:48 -0700 | [diff] [blame] | 41 | |
| 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.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 48 | SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface)); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 50 | for (int iteration = 0; iteration < loops; iteration++) { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 51 | drawingCanvas->clear(0); |
| 52 | SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot()); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 53 | 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.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 59 | drawingCanvas->drawRect(fullCanvasRect, paint); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 60 | // Trigger copy on write, which should be faster in the discardable case. |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 61 | drawingCanvas->flush(); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | bool fDiscardableContents; |
| 67 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 68 | typedef Benchmark INHERITED; |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 73 | DEF_BENCH( return new DeferredSurfaceCopyBench(false); ) |
| 74 | DEF_BENCH( return new DeferredSurfaceCopyBench(true); ) |