2014-04-24 21:03:00 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2015-05-27 20:23:23 +00:00
|
|
|
#include "SkRandom.h"
|
2014-04-24 21:03:00 +00:00
|
|
|
#include "SkSurface.h"
|
|
|
|
|
2014-10-30 18:34:15 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2014-04-24 21:03:00 +00:00
|
|
|
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() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-04-24 21:03:00 +00:00
|
|
|
return SkString("discard");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-04-24 21:03:00 +00:00
|
|
|
return SkISize::Make(100, 100);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-04-24 21:03:00 +00:00
|
|
|
GrContext* context = canvas->getGrContext();
|
|
|
|
if (NULL == context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize size = this->getISize();
|
|
|
|
size.fWidth /= 10;
|
|
|
|
size.fHeight /= 10;
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(size);
|
2015-01-16 15:32:33 +00:00
|
|
|
SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info);
|
2014-04-24 21:03:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-04-24 21:23:37 +00:00
|
|
|
surface->draw(canvas, 10.f*x, 10.f*y, NULL);
|
2014-04-24 21:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
surface->getCanvas()->discard();
|
|
|
|
surface->unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
DEF_GM( return SkNEW(DiscardGM); )
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
#endif
|