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"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-04-24 21:03:00 +00:00
|
|
|
#include "SkCanvas.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"
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class DiscardGM : public GpuGM {
|
2014-04-24 21:03:00 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas,
|
|
|
|
SkString* errorMsg) override {
|
2014-04-24 21:03:00 +00:00
|
|
|
SkISize size = this->getISize();
|
|
|
|
size.fWidth /= 10;
|
|
|
|
size.fHeight /= 10;
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(size);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == surface) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Could not create render target.";
|
|
|
|
return DrawResult::kFail;
|
2014-04-24 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2015-07-29 12:27:47 +00:00
|
|
|
SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
|
2014-04-24 21:03:00 +00:00
|
|
|
switch (rand.nextULessThan(3)) {
|
|
|
|
case 0:
|
|
|
|
surface->getCanvas()->drawColor(color);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
surface->getCanvas()->clear(color);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
SkPaint paint;
|
2016-03-25 16:08:00 +00:00
|
|
|
paint.setShader(SkShader::MakeColorShader(color));
|
2014-04-24 21:03:00 +00:00
|
|
|
surface->getCanvas()->drawPaint(paint);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
|
2014-04-24 21:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
surface->getCanvas()->discard();
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2014-04-24 21:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new DiscardGM;)
|
2014-04-24 21:03:00 +00:00
|
|
|
|
|
|
|
} // end namespace
|