blob: fbfce05a664018582e7e3136fa495a3bdf8e75f4 [file] [log] [blame]
reed799a3642015-08-03 14:18:04 -07001/*
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
reeddea6e1c2015-08-04 08:42:40 -070013class Image2RasterBench : public Benchmark {
reed799a3642015-08-03 14:18:04 -070014public:
reeddea6e1c2015-08-04 08:42:40 -070015 Image2RasterBench() {
16 fName.set("native_image_to_raster_surface");
reed799a3642015-08-03 14:18:04 -070017 }
18
19 bool isSuitableFor(Backend backend) override {
reeddea6e1c2015-08-04 08:42:40 -070020 return kGPU_Backend == backend || kRaster_Backend == backend;
reed799a3642015-08-03 14:18:04 -070021 }
22
23protected:
24 const char* onGetName() override {
25 return fName.c_str();
26 }
27
reeddea6e1c2015-08-04 08:42:40 -070028 // 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 //
reed799a3642015-08-03 14:18:04 -070031 void onPerCanvasPreDraw(SkCanvas* canvas) override {
reeddea6e1c2015-08-04 08:42:40 -070032 // create an Image reflecting the canvas (gpu or cpu)
33 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -070034 auto surface(canvas->makeSurface(info));
reed799a3642015-08-03 14:18:04 -070035 canvas->drawColor(SK_ColorRED);
reed9ce9d672016-03-17 10:51:11 -070036 fImage = surface->makeImageSnapshot();
reed799a3642015-08-03 14:18:04 -070037
38 // create a cpu-backed Surface
reede8f30622016-03-23 18:59:25 -070039 fRasterSurface = SkSurface::MakeRaster(info);
reed799a3642015-08-03 14:18:04 -070040 }
41
hendrikw4870e6d2015-11-24 05:50:23 -080042 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
mtkleina1ebeb22015-10-01 09:43:39 -070049 void onDraw(int loops, SkCanvas*) override {
reed799a3642015-08-03 14:18:04 -070050 for (int i = 0; i < loops; i++) {
51 for (int inner = 0; inner < 10; ++inner) {
reed9ce9d672016-03-17 10:51:11 -070052 fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0);
reed799a3642015-08-03 14:18:04 -070053 }
54 }
55 }
56
57private:
58 SkString fName;
reed9ce9d672016-03-17 10:51:11 -070059 sk_sp<SkImage> fImage;
reede8f30622016-03-23 18:59:25 -070060 sk_sp<SkSurface> fRasterSurface;
reed799a3642015-08-03 14:18:04 -070061
62 typedef Benchmark INHERITED;
63};
reeddea6e1c2015-08-04 08:42:40 -070064DEF_BENCH( return new Image2RasterBench; )