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" |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 21 | #include "include/gpu/ganesh/SkSurfaceGanesh.h" |
Kevin Lubick | 0d4d114 | 2023-02-13 09:13:10 -0500 | [diff] [blame] | 22 | #include "src/base/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 24 | |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 25 | #if defined(SK_GRAPHITE) |
| 26 | #include "include/gpu/graphite/Surface.h" |
| 27 | #endif |
| 28 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 29 | namespace skiagm { |
| 30 | |
| 31 | /* |
| 32 | * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly |
| 33 | * discarding it, drawing to it, and then drawing it to the main canvas. |
| 34 | */ |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 35 | class DiscardGM : public GM { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
Robert Phillips | 44333c5 | 2020-06-30 13:28:00 -0400 | [diff] [blame] | 38 | DiscardGM() {} |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 39 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 40 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 41 | SkString getName() const override { return SkString("discard"); } |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 42 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 43 | SkISize getISize() override { return SkISize::Make(100, 100); } |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 44 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 45 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 46 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 47 | SkISize size = this->getISize(); |
| 48 | size.fWidth /= 10; |
| 49 | size.fHeight /= 10; |
| 50 | SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 51 | sk_sp<SkSurface> surface; |
| 52 | |
| 53 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 54 | if (dContext && !dContext->abandoned()) { |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 55 | surface = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, info); |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Kevin Lubick | 0f7b44e | 2023-02-28 09:13:11 -0500 | [diff] [blame] | 58 | #if defined(SK_GRAPHITE) |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 59 | auto recorder = canvas->recorder(); |
| 60 | if (recorder) { |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 61 | surface = SkSurfaces::RenderTarget(recorder, info); |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 62 | } |
| 63 | #endif |
| 64 | |
| 65 | if (!surface) { |
Kevin Lubick | 5c93acf | 2023-05-09 12:11:43 -0400 | [diff] [blame] | 66 | surface = SkSurfaces::Raster(info); |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 67 | } |
| 68 | if (!surface) { |
| 69 | *errorMsg = "Could not create surface."; |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 70 | return DrawResult::kFail; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | canvas->clear(SK_ColorBLACK); |
| 74 | |
| 75 | SkRandom rand; |
| 76 | for (int x = 0; x < 10; ++x) { |
| 77 | for (int y = 0; y < 10; ++y) { |
| 78 | surface->getCanvas()->discard(); |
| 79 | // Make something that isn't too close to the background color, black. |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 80 | SkColor color = ToolUtils::color_to_565(rand.nextU() | 0xFF404040); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 81 | switch (rand.nextULessThan(3)) { |
| 82 | case 0: |
| 83 | surface->getCanvas()->drawColor(color); |
| 84 | break; |
| 85 | case 1: |
| 86 | surface->getCanvas()->clear(color); |
| 87 | break; |
| 88 | case 2: |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 89 | SkPaint paint; |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 90 | paint.setShader(SkShaders::Color(color)); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 91 | surface->getCanvas()->drawPaint(paint); |
| 92 | break; |
| 93 | } |
Mike Reed | b746b1f | 2021-01-06 08:43:51 -0500 | [diff] [blame] | 94 | surface->draw(canvas, 10.f*x, 10.f*y); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | surface->getCanvas()->discard(); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 99 | return DrawResult::kOk; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 100 | } |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 105 | DEF_GM(return new DiscardGM;) |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 106 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 107 | } // namespace skiagm |