skia2/gm/discard.cpp
bsalomon afe3005be3 Require budget decision when creating a RenderTarget SkSurface.
Restructure SkGpuDevice creation:
*SkSurfaceProps are optional.
*Use SkSurfaceProps to communicate DF text rather than a flag.
*Tell SkGpuDevice::Create whether RT comes from cache or not.

Review URL: https://codereview.chromium.org/848903004
2015-01-16 07:32:33 -08:00

96 lines
2.4 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkCanvas.h"
#include "SkColorShader.h"
#include "SkPaint.h"
#include "SkSurface.h"
#if SK_SUPPORT_GPU
namespace skiagm {
/*
* This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
* discarding it, drawing to it, and then drawing it to the main canvas.
*/
class DiscardGM : public GM {
public:
DiscardGM() {
}
uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
protected:
SkString onShortName() SK_OVERRIDE {
return SkString("discard");
}
SkISize onISize() SK_OVERRIDE {
return SkISize::Make(100, 100);
}
void onDraw(SkCanvas* canvas) SK_OVERRIDE {
GrContext* context = canvas->getGrContext();
if (NULL == context) {
return;
}
SkISize size = this->getISize();
size.fWidth /= 10;
size.fHeight /= 10;
SkImageInfo info = SkImageInfo::MakeN32Premul(size);
SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info);
if (NULL == surface) {
return;
}
canvas->clear(SK_ColorBLACK);
SkRandom rand;
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
surface->getCanvas()->discard();
// Make something that isn't too close to the background color, black.
SkColor color = rand.nextU() | 0xFF404040;
switch (rand.nextULessThan(3)) {
case 0:
surface->getCanvas()->drawColor(color);
break;
case 1:
surface->getCanvas()->clear(color);
break;
case 2:
SkColorShader shader(color);
SkPaint paint;
paint.setShader(&shader);
surface->getCanvas()->drawPaint(paint);
break;
}
surface->draw(canvas, 10.f*x, 10.f*y, NULL);
}
}
surface->getCanvas()->discard();
surface->unref();
}
private:
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM( return SkNEW(DiscardGM); )
} // end namespace
#endif