reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Benchmark.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkImage.h" |
| 11 | #include "SkSurface.h" |
| 12 | |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 13 | class Image2RasterBench : public Benchmark { |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 14 | public: |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 15 | Image2RasterBench() { |
| 16 | fName.set("native_image_to_raster_surface"); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | bool isSuitableFor(Backend backend) override { |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 20 | return kGPU_Backend == backend || kRaster_Backend == backend; |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | protected: |
| 24 | const char* onGetName() override { |
| 25 | return fName.c_str(); |
| 26 | } |
| 27 | |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 28 | // We explicitly want to bench drawing a Image [cpu or gpu backed] into a raster target, |
| 29 | // to ensure that we can cache the read-back in the case of gpu -> raster |
| 30 | // |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 31 | void onPerCanvasPreDraw(SkCanvas* canvas) override { |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 32 | // create an Image reflecting the canvas (gpu or cpu) |
| 33 | SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 34 | auto surface(canvas->makeSurface(info)); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 35 | canvas->drawColor(SK_ColorRED); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 36 | fImage = surface->makeImageSnapshot(); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 37 | |
| 38 | // create a cpu-backed Surface |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 39 | fRasterSurface = SkSurface::MakeRaster(info); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 40 | } |
| 41 | |
hendrikw | 4870e6d | 2015-11-24 05:50:23 -0800 | [diff] [blame] | 42 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 43 | // Release the image and raster surface here to prevent out of order destruction |
| 44 | // between these and the gpu interface. |
| 45 | fRasterSurface.reset(nullptr); |
| 46 | fImage.reset(nullptr); |
| 47 | } |
| 48 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 49 | void onDraw(int loops, SkCanvas*) override { |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 50 | for (int i = 0; i < loops; i++) { |
| 51 | for (int inner = 0; inner < 10; ++inner) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 52 | fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | SkString fName; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 59 | sk_sp<SkImage> fImage; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 60 | sk_sp<SkSurface> fRasterSurface; |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 61 | |
| 62 | typedef Benchmark INHERITED; |
| 63 | }; |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 64 | DEF_BENCH( return new Image2RasterBench; ) |