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 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 11 | #include "SkPaint.h" |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 12 | #include "SkRandom.h" |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 13 | #include "SkSurface.h" |
| 14 | |
| 15 | namespace 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 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 21 | class DiscardGM : public GpuGM { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 22 | |
| 23 | public: |
| 24 | DiscardGM() { |
| 25 | } |
| 26 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 27 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | SkString onShortName() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 29 | return SkString("discard"); |
| 30 | } |
| 31 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 32 | SkISize onISize() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 33 | return SkISize::Make(100, 100); |
| 34 | } |
| 35 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 36 | DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas, |
| 37 | SkString* errorMsg) override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 38 | SkISize size = this->getISize(); |
| 39 | size.fWidth /= 10; |
| 40 | size.fHeight /= 10; |
| 41 | SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 42 | auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | if (nullptr == surface) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 44 | *errorMsg = "Could not create render target."; |
| 45 | return DrawResult::kFail; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | canvas->clear(SK_ColorBLACK); |
| 49 | |
| 50 | SkRandom rand; |
| 51 | for (int x = 0; x < 10; ++x) { |
| 52 | for (int y = 0; y < 10; ++y) { |
| 53 | surface->getCanvas()->discard(); |
| 54 | // Make something that isn't too close to the background color, black. |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 55 | SkColor color = sk_tool_utils::color_to_565(rand.nextU() | 0xFF404040); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 56 | switch (rand.nextULessThan(3)) { |
| 57 | case 0: |
| 58 | surface->getCanvas()->drawColor(color); |
| 59 | break; |
| 60 | case 1: |
| 61 | surface->getCanvas()->clear(color); |
| 62 | break; |
| 63 | case 2: |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 64 | SkPaint paint; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 65 | paint.setShader(SkShader::MakeColorShader(color)); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 66 | surface->getCanvas()->drawPaint(paint); |
| 67 | break; |
| 68 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | surface->draw(canvas, 10.f*x, 10.f*y, nullptr); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | surface->getCanvas()->discard(); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 74 | return DrawResult::kOk; |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef GM INHERITED; |
| 79 | }; |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 83 | DEF_GM(return new DiscardGM;) |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 84 | |
| 85 | } // end namespace |