blob: 8db3a68ab07d8e448ae0c07582dae0daed2270cf [file] [log] [blame]
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000010#include "SkCanvas.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000011#include "SkPaint.h"
bsalomon4ee6bd82015-05-27 13:23:23 -070012#include "SkRandom.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000013#include "SkSurface.h"
14
reedf037e0b2014-10-30 11:34:15 -070015#if SK_SUPPORT_GPU
16
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000017namespace skiagm {
18
19/*
20 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
21 * discarding it, drawing to it, and then drawing it to the main canvas.
22 */
23class DiscardGM : public GM {
24
25public:
26 DiscardGM() {
27 }
28
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000029protected:
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000031 return SkString("discard");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000035 return SkISize::Make(100, 100);
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000039 GrContext* context = canvas->getGrContext();
halcanary96fcdcc2015-08-27 07:41:13 -070040 if (nullptr == context) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000041 return;
42 }
43
44 SkISize size = this->getISize();
45 size.fWidth /= 10;
46 size.fHeight /= 10;
47 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reede8f30622016-03-23 18:59:25 -070048 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070049 if (nullptr == surface) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000050 return;
51 }
52
53 canvas->clear(SK_ColorBLACK);
54
55 SkRandom rand;
56 for (int x = 0; x < 10; ++x) {
57 for (int y = 0; y < 10; ++y) {
58 surface->getCanvas()->discard();
59 // Make something that isn't too close to the background color, black.
caryclark12596012015-07-29 05:27:47 -070060 SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000061 switch (rand.nextULessThan(3)) {
62 case 0:
63 surface->getCanvas()->drawColor(color);
64 break;
65 case 1:
66 surface->getCanvas()->clear(color);
67 break;
68 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000069 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070070 paint.setShader(SkShader::MakeColorShader(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000071 surface->getCanvas()->drawPaint(paint);
72 break;
73 }
halcanary96fcdcc2015-08-27 07:41:13 -070074 surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000075 }
76 }
77
78 surface->getCanvas()->discard();
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000079 }
80
81private:
82 typedef GM INHERITED;
83};
84
85//////////////////////////////////////////////////////////////////////////////
86
halcanary385fe4d2015-08-26 13:07:48 -070087DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000088
89} // end namespace
90
91#endif