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