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) |
Brian Osman | 0ce3d43 | 2017-06-08 11:07:18 -0400 | [diff] [blame] | 33 | SkImageInfo info = canvas->imageInfo().makeWH(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 |
Brian Osman | 0ce3d43 | 2017-06-08 11:07:18 -0400 | [diff] [blame] | 39 | SkImageInfo n32Info = SkImageInfo::MakeN32Premul(100, 100); |
| 40 | fRasterSurface = SkSurface::MakeRaster(n32Info); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 41 | } |
| 42 | |
hendrikw | 4870e6d | 2015-11-24 05:50:23 -0800 | [diff] [blame] | 43 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 44 | // Release the image and raster surface here to prevent out of order destruction |
| 45 | // between these and the gpu interface. |
| 46 | fRasterSurface.reset(nullptr); |
| 47 | fImage.reset(nullptr); |
| 48 | } |
| 49 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 50 | void onDraw(int loops, SkCanvas*) override { |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 51 | for (int i = 0; i < loops; i++) { |
| 52 | for (int inner = 0; inner < 10; ++inner) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 53 | fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0); |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | SkString fName; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 60 | sk_sp<SkImage> fImage; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 61 | sk_sp<SkSurface> fRasterSurface; |
reed | 799a364 | 2015-08-03 14:18:04 -0700 | [diff] [blame] | 62 | |
| 63 | typedef Benchmark INHERITED; |
| 64 | }; |
reed | dea6e1c | 2015-08-04 08:42:40 -0700 | [diff] [blame] | 65 | DEF_BENCH( return new Image2RasterBench; ) |