blob: 7a1e9d9f4420d6e1c05ccc21024eb3ff30fa5c6e [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
15namespace skiagm {
16
17/*
18 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
19 * discarding it, drawing to it, and then drawing it to the main canvas.
20 */
21class DiscardGM : public GM {
22
23public:
24 DiscardGM() {
25 }
26
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000027protected:
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000029 return SkString("discard");
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000033 return SkISize::Make(100, 100);
34 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000037 GrContext* context = canvas->getGrContext();
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (nullptr == context) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000039 return;
40 }
41
42 SkISize size = this->getISize();
43 size.fWidth /= 10;
44 size.fHeight /= 10;
45 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reede8f30622016-03-23 18:59:25 -070046 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070047 if (nullptr == surface) {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000048 return;
49 }
50
51 canvas->clear(SK_ColorBLACK);
52
53 SkRandom rand;
54 for (int x = 0; x < 10; ++x) {
55 for (int y = 0; y < 10; ++y) {
56 surface->getCanvas()->discard();
57 // Make something that isn't too close to the background color, black.
caryclark12596012015-07-29 05:27:47 -070058 SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000059 switch (rand.nextULessThan(3)) {
60 case 0:
61 surface->getCanvas()->drawColor(color);
62 break;
63 case 1:
64 surface->getCanvas()->clear(color);
65 break;
66 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000067 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070068 paint.setShader(SkShader::MakeColorShader(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000069 surface->getCanvas()->drawPaint(paint);
70 break;
71 }
halcanary96fcdcc2015-08-27 07:41:13 -070072 surface->draw(canvas, 10.f*x, 10.f*y, nullptr);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000073 }
74 }
75
76 surface->getCanvas()->discard();
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000077 }
78
79private:
80 typedef GM INHERITED;
81};
82
83//////////////////////////////////////////////////////////////////////////////
84
halcanary385fe4d2015-08-26 13:07:48 -070085DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000086
87} // end namespace