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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
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.
|
|
|
|
*/
|
2021-06-03 14:14:16 +00:00
|
|
|
class DiscardGM : public GM {
|
2014-04-24 21:03:00 +00:00
|
|
|
|
|
|
|
public:
|
2020-06-30 17:28:00 +00:00
|
|
|
DiscardGM() {}
|
2014-04-24 21:03:00 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
|
|
|
auto dContext = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (!dContext || dContext->abandoned()) {
|
2020-06-29 19:36:12 +00:00
|
|
|
*errorMsg = "GM relies on having access to a live direct context.";
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2014-04-24 21:03:00 +00:00
|
|
|
SkISize size = this->getISize();
|
|
|
|
size.fWidth /= 10;
|
|
|
|
size.fHeight /= 10;
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(size);
|
2021-06-03 14:14:16 +00:00
|
|
|
sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, 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.
|
2019-03-20 16:12:10 +00:00
|
|
|
SkColor color = ToolUtils::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;
|
2019-04-09 17:55:36 +00:00
|
|
|
paint.setShader(SkShaders::Color(color));
|
2014-04-24 21:03:00 +00:00
|
|
|
surface->getCanvas()->drawPaint(paint);
|
|
|
|
break;
|
|
|
|
}
|
2021-01-06 13:43:51 +00:00
|
|
|
surface->draw(canvas, 10.f*x, 10.f*y);
|
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:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2014-04-24 21:03:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new DiscardGM;)
|
2014-04-24 21:03:00 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|