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 | |
| 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 | |
| 18 | class DeferredSurfaceCopyBench : public SkBenchmark { |
| 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: |
| 29 | virtual const char* onGetName() SK_OVERRIDE { |
| 30 | return fDiscardableContents ? "DeferredSurfaceCopy_discardable" : |
| 31 | "DeferredSurfaceCopy_nonDiscardable"; |
| 32 | } |
| 33 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 34 | virtual 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@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 37 | SkImageInfo info; |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 38 | info.fWidth = kSurfaceWidth; |
| 39 | info.fHeight = kSurfaceHeight; |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 40 | info.fColorType = kPMColor_SkColorType; |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 41 | info.fAlphaType = kPremul_SkAlphaType; |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 42 | const SkRect fullCanvasRect = SkRect::MakeWH( |
| 43 | SkIntToScalar(kSurfaceWidth), SkIntToScalar(kSurfaceHeight)); |
| 44 | SkSurface* surface; |
| 45 | #if SK_SUPPORT_GPU |
junov@chromium.org | a213899 | 2013-04-17 13:55:45 +0000 | [diff] [blame] | 46 | GrRenderTarget* rt = reinterpret_cast<GrRenderTarget*>( |
| 47 | canvas->getDevice()->accessRenderTarget()); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 48 | 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.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 55 | SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface)); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 56 | surface->unref(); |
| 57 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 58 | for (int iteration = 0; iteration < loops; iteration++) { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 59 | drawingCanvas->clear(0); |
| 60 | SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot()); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 61 | 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.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 67 | drawingCanvas->drawRect(fullCanvasRect, paint); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 68 | // 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] | 69 | drawingCanvas->flush(); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | bool fDiscardableContents; |
| 75 | |
| 76 | typedef SkBenchmark INHERITED; |
| 77 | }; |
| 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 81 | DEF_BENCH( return new DeferredSurfaceCopyBench(false); ) |
| 82 | DEF_BENCH( return new DeferredSurfaceCopyBench(true); ) |