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: Image2RasterBench()15 Image2RasterBench() { 16 fName.set("native_image_to_raster_surface"); 17 } 18 isSuitableFor(Backend backend)19 bool isSuitableFor(Backend backend) override { 20 return kGPU_Backend == backend || kRaster_Backend == backend; 21 } 22 23 protected: onGetName()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 // onPerCanvasPreDraw(SkCanvas * canvas)31 void onPerCanvasPreDraw(SkCanvas* canvas) override { 32 // create an Image reflecting the canvas (gpu or cpu) 33 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100); 34 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info)); 35 canvas->drawColor(SK_ColorRED); 36 fImage.reset(surface->newImageSnapshot()); 37 38 // create a cpu-backed Surface 39 fRasterSurface.reset(SkSurface::NewRaster(info)); 40 } 41 onPerCanvasPostDraw(SkCanvas *)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 onDraw(int loops,SkCanvas *)49 void onDraw(int loops, SkCanvas*) override { 50 for (int i = 0; i < loops; i++) { 51 for (int inner = 0; inner < 10; ++inner) { 52 fRasterSurface->getCanvas()->drawImage(fImage, 0, 0); 53 } 54 } 55 } 56 57 private: 58 SkString fName; 59 SkAutoTUnref<SkImage> fImage; 60 SkAutoTUnref<SkSurface> fRasterSurface; 61 62 typedef Benchmark INHERITED; 63 }; 64 DEF_BENCH( return new Image2RasterBench; ) 65