commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkRefCnt.h" |
| 14 | #include "include/core/SkShader.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkTypes.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrDirectContext.h" |
| 20 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/utils/SkRandom.h" |
| 22 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 23 | |
| 24 | namespace 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 Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 30 | class DiscardGM : public GM { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 33 | DiscardGM() {} |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 34 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 35 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 36 | SkString onShortName() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 37 | return SkString("discard"); |
| 38 | } |
| 39 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | SkISize onISize() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 41 | return SkISize::Make(100, 100); |
| 42 | } |
| 43 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 44 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 45 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 46 | if (!dContext || dContext->abandoned()) { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 47 | *errorMsg = "GM relies on having access to a live direct context."; |
| 48 | return DrawResult::kSkip; |
| 49 | } |
| 50 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 51 | SkISize size = this->getISize(); |
| 52 | size.fWidth /= 10; |
| 53 | size.fHeight /= 10; |
| 54 | SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 55 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | if (nullptr == surface) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 57 | *errorMsg = "Could not create render target."; |
| 58 | return DrawResult::kFail; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 59 | } |
| 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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 68 | SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 69 | 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.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 77 | SkPaint paint; |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 78 | paint.setShader(SkShaders::Color(color)); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 79 | surface->getCanvas()->drawPaint(paint); |
| 80 | break; |
| 81 | } |
Mike Reed | b746b1f | 2021-01-06 08:43:51 -0500 | [diff] [blame] | 82 | surface->draw(canvas, 10.f*x, 10.f*y); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | surface->getCanvas()->discard(); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 87 | return DrawResult::kOk; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 91 | using INHERITED = GM; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 96 | DEF_GM(return new DiscardGM;) |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 97 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 98 | } // namespace skiagm |