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 13 class Image2RasterBench : public Benchmark { 14 public: 15 Image2RasterBench() { 16 fName.set("native_image_to_raster_surface"); 17 } 18 19 bool isSuitableFor(Backend backend) override { 20 return kGPU_Backend == backend || kRaster_Backend == backend; 21 } 22 23 protected: 24 const char* onGetName() override { 25 return fName.c_str(); 26 } 27 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 // 31 void onPerCanvasPreDraw(SkCanvas* canvas) override { 32 // create an Image reflecting the canvas (gpu or cpu) 33 SkImageInfo info = canvas->imageInfo().makeWH(100, 100); 34 auto surface(canvas->makeSurface(info)); 35 canvas->drawColor(SK_ColorRED); 36 fImage = surface->makeImageSnapshot(); 37 38 // create a cpu-backed Surface 39 SkImageInfo n32Info = SkImageInfo::MakeN32Premul(100, 100); 40 fRasterSurface = SkSurface::MakeRaster(n32Info); 41 } 42 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 50 void onDraw(int loops, SkCanvas*) override { 51 for (int i = 0; i < loops; i++) { 52 for (int inner = 0; inner < 10; ++inner) { 53 fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0); 54 } 55 } 56 } 57 58 private: 59 SkString fName; 60 sk_sp<SkImage> fImage; 61 sk_sp<SkSurface> fRasterSurface; 62 63 typedef Benchmark INHERITED; 64 }; 65 DEF_BENCH( return new Image2RasterBench; ) 66