2015-10-15 16:49:31 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef WrappedBenchmark_DEFINED
|
|
|
|
#define WrappedBenchmark_DEFINED
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
|
|
|
#include "SkSurface.h"
|
2015-11-24 16:54:29 +00:00
|
|
|
#include "GrContext.h"
|
2016-04-28 21:32:04 +00:00
|
|
|
#include "GrDrawContext.h"
|
2015-11-24 16:54:29 +00:00
|
|
|
#include "GrRenderTarget.h"
|
2015-10-15 16:49:31 +00:00
|
|
|
|
|
|
|
// Wrap some other benchmark to allow specialization to either
|
|
|
|
// cpu or gpu backends. The derived class will override 'setupOffScreen'
|
|
|
|
// to create an offscreen surface in which the actual rendering will occur.
|
|
|
|
class WrappedBenchmark : public Benchmark {
|
|
|
|
public:
|
|
|
|
// Takes ownership of caller's ref on `bench`.
|
2015-10-26 20:14:36 +00:00
|
|
|
explicit WrappedBenchmark(const SkSurfaceProps& surfaceProps, Benchmark* bench)
|
|
|
|
: fSurfaceProps(surfaceProps)
|
|
|
|
, fBench(bench) {}
|
|
|
|
|
|
|
|
const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
|
2015-10-15 16:49:31 +00:00
|
|
|
|
|
|
|
const char* onGetName() override { return fBench->getName(); }
|
|
|
|
const char* onGetUniqueName() override { return fBench->getUniqueName(); }
|
|
|
|
|
|
|
|
void onDelayedSetup() override { fBench->delayedSetup(); }
|
|
|
|
void onPerCanvasPreDraw(SkCanvas* canvas) override {
|
2015-11-24 16:54:29 +00:00
|
|
|
this->setupOffScreen(canvas);
|
|
|
|
fOffScreen->getCanvas()->clear(SK_ColorWHITE);
|
2015-10-15 16:49:31 +00:00
|
|
|
fBench->perCanvasPreDraw(fOffScreen->getCanvas());
|
|
|
|
}
|
|
|
|
void onPreDraw(SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
SkASSERT(fOffScreen);
|
2015-10-15 16:49:31 +00:00
|
|
|
fBench->preDraw(fOffScreen->getCanvas());
|
|
|
|
}
|
|
|
|
void onPostDraw(SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
SkASSERT(fOffScreen);
|
2015-10-15 16:49:31 +00:00
|
|
|
fBench->postDraw(fOffScreen->getCanvas());
|
|
|
|
}
|
|
|
|
void onPerCanvasPostDraw(SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
SkASSERT(fOffScreen);
|
2015-10-15 16:49:31 +00:00
|
|
|
fBench->perCanvasPostDraw(fOffScreen->getCanvas());
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(int loops, SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
SkASSERT(fOffScreen);
|
2015-10-15 16:49:31 +00:00
|
|
|
fBench->draw(loops, fOffScreen->getCanvas());
|
2015-11-24 16:54:29 +00:00
|
|
|
this->blitToScreen(canvas);
|
2015-10-15 16:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkIPoint onGetSize() override { return fBench->getSize(); }
|
|
|
|
|
2015-11-24 16:54:29 +00:00
|
|
|
protected:
|
|
|
|
virtual void setupOffScreen(SkCanvas*)=0;
|
|
|
|
|
|
|
|
void blitToScreen(SkCanvas* canvas) {
|
|
|
|
int w = SkTMin(fBench->getSize().fX, fOffScreen->width());
|
|
|
|
int h = SkTMin(fBench->getSize().fY, fOffScreen->width());
|
|
|
|
this->onBlitToScreen(canvas, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onBlitToScreen(SkCanvas* canvas, int w, int h) = 0;
|
2015-10-15 16:49:31 +00:00
|
|
|
|
2015-10-26 20:14:36 +00:00
|
|
|
SkSurfaceProps fSurfaceProps;
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> fOffScreen;
|
2015-10-15 16:49:31 +00:00
|
|
|
SkAutoTUnref<Benchmark> fBench;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a raster surface for off screen rendering
|
|
|
|
class CpuWrappedBenchmark : public WrappedBenchmark {
|
|
|
|
public:
|
2015-10-26 20:14:36 +00:00
|
|
|
explicit CpuWrappedBenchmark(const SkSurfaceProps& surfaceProps, Benchmark* bench)
|
|
|
|
: INHERITED(surfaceProps, bench) {}
|
2015-10-15 16:49:31 +00:00
|
|
|
|
|
|
|
private:
|
2015-11-24 16:54:29 +00:00
|
|
|
void setupOffScreen(SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
fOffScreen = SkSurface::MakeRaster(canvas->imageInfo(), &this->surfaceProps());
|
2015-11-24 16:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onBlitToScreen(SkCanvas* canvas, int w, int h) override {
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> image(fOffScreen->makeImageSnapshot());
|
2015-11-24 16:54:29 +00:00
|
|
|
SkPaint blitPaint;
|
|
|
|
blitPaint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImageRect(image.get(), SkIRect::MakeWH(w, h),
|
2015-11-24 16:54:29 +00:00
|
|
|
SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)), &blitPaint);
|
2015-10-15 16:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef WrappedBenchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create an MSAA & NVPR-enabled GPU backend
|
2015-11-24 17:20:24 +00:00
|
|
|
class GpuWrappedBenchmark : public WrappedBenchmark {
|
2015-10-15 16:49:31 +00:00
|
|
|
public:
|
2015-11-24 17:20:24 +00:00
|
|
|
explicit GpuWrappedBenchmark(const SkSurfaceProps& surfaceProps, Benchmark* bench,
|
|
|
|
int numSamples)
|
2015-10-26 20:14:36 +00:00
|
|
|
: INHERITED(surfaceProps, bench)
|
2015-10-15 16:49:31 +00:00
|
|
|
, fNumSamples(numSamples) {}
|
|
|
|
|
|
|
|
private:
|
2015-11-24 16:54:29 +00:00
|
|
|
void setupOffScreen(SkCanvas* canvas) override {
|
2016-03-24 01:59:25 +00:00
|
|
|
fOffScreen = SkSurface::MakeRenderTarget(canvas->getGrContext(),
|
2016-02-25 16:33:02 +00:00
|
|
|
SkBudgeted::kNo,
|
2015-11-24 16:54:29 +00:00
|
|
|
canvas->imageInfo(),
|
|
|
|
fNumSamples,
|
2016-03-24 01:59:25 +00:00
|
|
|
&this->surfaceProps());
|
2015-11-24 16:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onBlitToScreen(SkCanvas* canvas, int w, int h) override {
|
|
|
|
// We call copySurface directly on the underlying GPU surfaces for a more efficient blit.
|
2016-04-28 21:32:04 +00:00
|
|
|
GrDrawContext* dstDC = canvas->internal_private_accessTopLayerDrawContext();
|
|
|
|
SkASSERT(dstDC);
|
2015-11-24 16:54:29 +00:00
|
|
|
|
2016-04-28 21:32:04 +00:00
|
|
|
GrDrawContext* srcDC =
|
|
|
|
fOffScreen->getCanvas()->internal_private_accessTopLayerDrawContext();
|
|
|
|
SkASSERT(srcDC);
|
|
|
|
GrRenderTarget* src = srcDC->accessRenderTarget();
|
2016-04-21 18:05:32 +00:00
|
|
|
SkASSERT(src);
|
2015-11-24 16:54:29 +00:00
|
|
|
|
2016-04-28 21:32:04 +00:00
|
|
|
SkASSERT(canvas->getGrContext() == fOffScreen->getCanvas()->getGrContext());
|
2015-11-24 16:54:29 +00:00
|
|
|
|
2016-04-28 21:32:04 +00:00
|
|
|
dstDC->copySurface(src, SkIRect::MakeWH(w, h), SkIPoint::Make(0, 0));
|
2015-10-15 16:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fNumSamples;
|
|
|
|
typedef WrappedBenchmark INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //WrappedBenchmark_DEFINED
|