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 { |
| 20 | N = SkBENCHLOOP(5), |
| 21 | kSurfaceWidth = 1000, |
| 22 | kSurfaceHeight = 1000, |
| 23 | }; |
| 24 | public: |
| 25 | DeferredSurfaceCopyBench(void* param, bool discardableContents) : SkBenchmark(param) { |
| 26 | fDiscardableContents = discardableContents; |
| 27 | } |
| 28 | |
| 29 | protected: |
| 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.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 38 | 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.org | a213899 | 2013-04-17 13:55:45 +0000 | [diff] [blame] | 47 | GrRenderTarget* rt = reinterpret_cast<GrRenderTarget*>( |
| 48 | canvas->getDevice()->accessRenderTarget()); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 49 | 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.org | cb62224 | 2013-08-09 14:24:59 +0000 | [diff] [blame] | 56 | SkAutoTUnref<SkDeferredCanvas> drawingCanvas(SkDeferredCanvas::Create(surface)); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 57 | surface->unref(); |
| 58 | |
| 59 | for (int iteration = 0; iteration < N; iteration++) { |
junov@chromium.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 60 | drawingCanvas->clear(0); |
| 61 | SkAutoTUnref<SkImage> image(drawingCanvas->newImageSnapshot()); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 62 | 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.org | 66070a5 | 2013-05-28 17:39:08 +0000 | [diff] [blame] | 68 | drawingCanvas->drawRect(fullCanvasRect, paint); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 69 | // 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] | 70 | drawingCanvas->flush(); |
junov@chromium.org | d61ba6e | 2013-04-17 13:43:04 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | bool fDiscardableContents; |
| 76 | |
| 77 | typedef SkBenchmark INHERITED; |
| 78 | }; |
| 79 | |
| 80 | ////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | static SkBenchmark* Fact0(void* p) { return new DeferredSurfaceCopyBench(p, false); } |
| 83 | static SkBenchmark* Fact1(void* p) { return new DeferredSurfaceCopyBench(p, true); } |
| 84 | |
| 85 | static BenchRegistry gReg0(Fact0); |
| 86 | static BenchRegistry gReg1(Fact1); |