blob: 45765368ba52c49b7e0b5cdb21c47ba69299c3b7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRefCnt.h"
14#include "include/core/SkShader.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkTypes.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040019#include "include/gpu/GrDirectContext.h"
20#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/utils/SkRandom.h"
22#include "tools/ToolUtils.h"
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000023
24namespace skiagm {
25
26/*
27 * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly
28 * discarding it, drawing to it, and then drawing it to the main canvas.
29 */
Robert Phillipsedcd4312021-06-03 10:14:16 -040030class DiscardGM : public GM {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000031
32public:
Robert Phillips44333c52020-06-30 13:28:00 -040033 DiscardGM() {}
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000034
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000035protected:
mtklein36352bf2015-03-25 18:17:31 -070036 SkString onShortName() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000037 return SkString("discard");
38 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkISize onISize() override {
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000041 return SkISize::Make(100, 100);
42 }
43
Robert Phillipsedcd4312021-06-03 10:14:16 -040044 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
45 auto dContext = GrAsDirectContext(canvas->recordingContext());
46 if (!dContext || dContext->abandoned()) {
Robert Phillips95c250c2020-06-29 15:36:12 -040047 *errorMsg = "GM relies on having access to a live direct context.";
48 return DrawResult::kSkip;
49 }
50
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000051 SkISize size = this->getISize();
52 size.fWidth /= 10;
53 size.fHeight /= 10;
54 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
Robert Phillipsedcd4312021-06-03 10:14:16 -040055 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
halcanary96fcdcc2015-08-27 07:41:13 -070056 if (nullptr == surface) {
Chris Dalton50e24d72019-02-07 16:20:09 -070057 *errorMsg = "Could not create render target.";
58 return DrawResult::kFail;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000059 }
60
61 canvas->clear(SK_ColorBLACK);
62
63 SkRandom rand;
64 for (int x = 0; x < 10; ++x) {
65 for (int y = 0; y < 10; ++y) {
66 surface->getCanvas()->discard();
67 // Make something that isn't too close to the background color, black.
Mike Kleinea3f0142019-03-20 11:12:10 -050068 SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000069 switch (rand.nextULessThan(3)) {
70 case 0:
71 surface->getCanvas()->drawColor(color);
72 break;
73 case 1:
74 surface->getCanvas()->clear(color);
75 break;
76 case 2:
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000077 SkPaint paint;
Mike Reedc8bea7d2019-04-09 13:55:36 -040078 paint.setShader(SkShaders::Color(color));
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000079 surface->getCanvas()->drawPaint(paint);
80 break;
81 }
Mike Reedb746b1f2021-01-06 08:43:51 -050082 surface->draw(canvas, 10.f*x, 10.f*y);
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000083 }
84 }
85
86 surface->getCanvas()->discard();
Chris Dalton50e24d72019-02-07 16:20:09 -070087 return DrawResult::kOk;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000088 }
89
90private:
John Stiles7571f9e2020-09-02 22:42:33 -040091 using INHERITED = GM;
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000092};
93
94//////////////////////////////////////////////////////////////////////////////
95
halcanary385fe4d2015-08-26 13:07:48 -070096DEF_GM(return new DiscardGM;)
commit-bot@chromium.org04f03d12014-04-24 21:03:00 +000097
John Stilesa6841be2020-08-06 14:11:56 -040098} // namespace skiagm