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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorShader.h" |
| 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 | |
reed | f037e0b | 2014-10-30 11:34:15 -0700 | [diff] [blame] | 15 | #if SK_SUPPORT_GPU |
| 16 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 17 | namespace skiagm { |
| 18 | |
| 19 | /* |
| 20 | * This GM exercises SkCanvas::discard() by creating an offscreen SkSurface and repeatedly |
| 21 | * discarding it, drawing to it, and then drawing it to the main canvas. |
| 22 | */ |
| 23 | class DiscardGM : public GM { |
| 24 | |
| 25 | public: |
| 26 | DiscardGM() { |
| 27 | } |
| 28 | |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 29 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 31 | return SkString("discard"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 35 | return SkISize::Make(100, 100); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 39 | GrContext* context = canvas->getGrContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | if (nullptr == context) { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
| 44 | SkISize size = this->getISize(); |
| 45 | size.fWidth /= 10; |
| 46 | size.fHeight /= 10; |
| 47 | SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 48 | SkSurface* surface = SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 49 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | if (nullptr == surface) { |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | canvas->clear(SK_ColorBLACK); |
| 55 | |
| 56 | SkRandom rand; |
| 57 | for (int x = 0; x < 10; ++x) { |
| 58 | for (int y = 0; y < 10; ++y) { |
| 59 | surface->getCanvas()->discard(); |
| 60 | // Make something that isn't too close to the background color, black. |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 61 | 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] | 62 | switch (rand.nextULessThan(3)) { |
| 63 | case 0: |
| 64 | surface->getCanvas()->drawColor(color); |
| 65 | break; |
| 66 | case 1: |
| 67 | surface->getCanvas()->clear(color); |
| 68 | break; |
| 69 | case 2: |
| 70 | SkColorShader shader(color); |
| 71 | SkPaint paint; |
| 72 | paint.setShader(&shader); |
| 73 | surface->getCanvas()->drawPaint(paint); |
| 74 | break; |
| 75 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | surface->draw(canvas, 10.f*x, 10.f*y, nullptr); |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | surface->getCanvas()->discard(); |
| 81 | surface->unref(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | typedef GM INHERITED; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 90 | DEF_GM(return new DiscardGM;) |
commit-bot@chromium.org | 04f03d1 | 2014-04-24 21:03:00 +0000 | [diff] [blame] | 91 | |
| 92 | } // end namespace |
| 93 | |
| 94 | #endif |