blob: a4529031ddd1c7841e8c981b8e4563e3ef1e093b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkImage.h"
11#include "include/core/SkSurface.h"
reed799a3642015-08-03 14:18:04 -070012
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)
Brian Osman0ce3d432017-06-08 11:07:18 -040033 SkImageInfo info = canvas->imageInfo().makeWH(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
Brian Osman0ce3d432017-06-08 11:07:18 -040039 SkImageInfo n32Info = SkImageInfo::MakeN32Premul(100, 100);
40 fRasterSurface = SkSurface::MakeRaster(n32Info);
reed799a3642015-08-03 14:18:04 -070041 }
42
hendrikw4870e6d2015-11-24 05:50:23 -080043 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
mtkleina1ebeb22015-10-01 09:43:39 -070050 void onDraw(int loops, SkCanvas*) override {
reed799a3642015-08-03 14:18:04 -070051 for (int i = 0; i < loops; i++) {
52 for (int inner = 0; inner < 10; ++inner) {
reed9ce9d672016-03-17 10:51:11 -070053 fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0);
reed799a3642015-08-03 14:18:04 -070054 }
55 }
56 }
57
58private:
59 SkString fName;
reed9ce9d672016-03-17 10:51:11 -070060 sk_sp<SkImage> fImage;
reede8f30622016-03-23 18:59:25 -070061 sk_sp<SkSurface> fRasterSurface;
reed799a3642015-08-03 14:18:04 -070062
63 typedef Benchmark INHERITED;
64};
reeddea6e1c2015-08-04 08:42:40 -070065DEF_BENCH( return new Image2RasterBench; )