2015-08-03 21:18:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImage.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
2015-08-04 15:42:40 +00:00
|
|
|
class Image2RasterBench : public Benchmark {
|
2015-08-03 21:18:04 +00:00
|
|
|
public:
|
2015-08-04 15:42:40 +00:00
|
|
|
Image2RasterBench() {
|
|
|
|
fName.set("native_image_to_raster_surface");
|
2015-08-03 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
2015-08-04 15:42:40 +00:00
|
|
|
return kGPU_Backend == backend || kRaster_Backend == backend;
|
2015-08-03 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2015-08-04 15:42:40 +00:00
|
|
|
// We explicitly want to bench drawing a Image [cpu or gpu backed] into a raster target,
|
|
|
|
// to ensure that we can cache the read-back in the case of gpu -> raster
|
|
|
|
//
|
2015-08-03 21:18:04 +00:00
|
|
|
void onPerCanvasPreDraw(SkCanvas* canvas) override {
|
2015-08-04 15:42:40 +00:00
|
|
|
// create an Image reflecting the canvas (gpu or cpu)
|
2017-06-08 15:07:18 +00:00
|
|
|
SkImageInfo info = canvas->imageInfo().makeWH(100, 100);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(canvas->makeSurface(info));
|
2015-08-03 21:18:04 +00:00
|
|
|
canvas->drawColor(SK_ColorRED);
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = surface->makeImageSnapshot();
|
2015-08-03 21:18:04 +00:00
|
|
|
|
|
|
|
// create a cpu-backed Surface
|
2017-06-08 15:07:18 +00:00
|
|
|
SkImageInfo n32Info = SkImageInfo::MakeN32Premul(100, 100);
|
|
|
|
fRasterSurface = SkSurface::MakeRaster(n32Info);
|
2015-08-03 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 13:50:23 +00:00
|
|
|
void onPerCanvasPostDraw(SkCanvas*) override {
|
|
|
|
// Release the image and raster surface here to prevent out of order destruction
|
|
|
|
// between these and the gpu interface.
|
|
|
|
fRasterSurface.reset(nullptr);
|
|
|
|
fImage.reset(nullptr);
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2015-08-03 21:18:04 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
|
|
|
for (int inner = 0; inner < 10; ++inner) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fRasterSurface->getCanvas()->drawImage(fImage.get(), 0, 0);
|
2015-08-03 21:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkString fName;
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> fRasterSurface;
|
2015-08-03 21:18:04 +00:00
|
|
|
|
|
|
|
typedef Benchmark INHERITED;
|
|
|
|
};
|
2015-12-03 20:56:59 +00:00
|
|
|
DEF_BENCH( return new Image2RasterBench; )
|