commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +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 | |
| 8 | // This test only works with the GPU backend. |
| 9 | |
| 10 | #include "gm.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContextPriv.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 16 | #include "SkBitmap.h" |
| 17 | #include "SkGr.h" |
| 18 | #include "SkGradientShader.h" |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 19 | #include "batches/GrDrawOp.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 20 | #include "batches/GrRectBatchFactory.h" |
| 21 | #include "effects/GrTextureDomain.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 22 | |
| 23 | namespace skiagm { |
| 24 | /** |
| 25 | * This GM directly exercises GrTextureDomainEffect. |
| 26 | */ |
| 27 | class TextureDomainEffect : public GM { |
| 28 | public: |
| 29 | TextureDomainEffect() { |
| 30 | this->setBGColor(0xFFFFFFFF); |
| 31 | } |
| 32 | |
| 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 35 | return SkString("texture_domain_effect"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 39 | const SkScalar canvasWidth = kDrawPad + |
| 40 | (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 41 | kTestPad * GrTextureDomain::kModeCount; |
| 42 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onOnceBeforeDraw() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 46 | fBmp.allocN32Pixels(kTargetWidth, kTargetHeight); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 47 | SkCanvas canvas(fBmp); |
commit-bot@chromium.org | c5713e4 | 2014-04-07 21:18:46 +0000 | [diff] [blame] | 48 | canvas.clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 49 | SkPaint paint; |
| 50 | |
| 51 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 52 | paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr, |
| 53 | SK_ARRAY_COUNT(colors1))); |
| 54 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 55 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 56 | |
| 57 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 58 | paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr, |
| 59 | SK_ARRAY_COUNT(colors2))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 60 | paint.setBlendMode(SkBlendMode::kDarken); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 61 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 62 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 63 | |
| 64 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 65 | paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr, |
| 66 | SK_ARRAY_COUNT(colors3))); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 67 | paint.setBlendMode(SkBlendMode::kLighten); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 68 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f), |
| 69 | paint); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 70 | } |
| 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 73 | GrRenderTargetContext* renderTargetContext = |
| 74 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 75 | if (!renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 76 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 80 | GrContext* context = canvas->getGrContext(); |
| 81 | if (!context) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 85 | sk_sp<GrTexture> texture( |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 86 | GrRefCachedBitmapTexture(context, fBmp, GrSamplerParams::ClampNoFilter())); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 87 | if (!texture) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 91 | SkTArray<SkMatrix> textureMatrices; |
| 92 | textureMatrices.push_back().setIDiv(texture->width(), texture->height()); |
| 93 | textureMatrices.push_back() = textureMatrices[0]; |
| 94 | textureMatrices.back().postScale(1.5f, 0.85f); |
| 95 | textureMatrices.push_back() = textureMatrices[0]; |
| 96 | textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f); |
| 97 | |
| 98 | const SkIRect texelDomains[] = { |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 99 | fBmp.bounds(), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 100 | SkIRect::MakeXYWH(fBmp.width() / 4, |
| 101 | fBmp.height() / 4, |
| 102 | fBmp.width() / 2, |
| 103 | fBmp.height() / 2), |
| 104 | }; |
| 105 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 106 | SkRect renderRect = SkRect::Make(fBmp.bounds()); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 107 | renderRect.outset(kDrawPad, kDrawPad); |
| 108 | |
| 109 | SkScalar y = kDrawPad + kTestPad; |
| 110 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
| 111 | for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { |
| 112 | SkScalar x = kDrawPad + kTestPad; |
| 113 | for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { |
| 114 | GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 115 | GrPaint grPaint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 116 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 117 | sk_sp<GrFragmentProcessor> fp( |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 118 | GrTextureDomainEffect::Make(texture.get(), nullptr, textureMatrices[tm], |
| 119 | GrTextureDomain::MakeTexelDomain(texture.get(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 120 | texelDomains[d]), |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 121 | mode, GrSamplerParams::kNone_FilterMode)); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 122 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 123 | if (!fp) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 124 | continue; |
| 125 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 126 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 127 | grPaint.addColorFragmentProcessor(std::move(fp)); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 128 | |
Brian Salomon | 1951f3d | 2016-12-09 16:07:12 -0500 | [diff] [blame] | 129 | sk_sp<GrDrawOp> op( |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 130 | GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix, |
| 131 | renderRect, nullptr, nullptr)); |
Brian Salomon | 1951f3d | 2016-12-09 16:07:12 -0500 | [diff] [blame] | 132 | renderTargetContext->priv().testingOnly_addDrawOp(grPaint, GrAAType::kNone, |
| 133 | std::move(op)); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 134 | x += renderRect.width() + kTestPad; |
| 135 | } |
| 136 | y += renderRect.height() + kTestPad; |
| 137 | } |
| 138 | } |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 142 | static constexpr SkScalar kDrawPad = 10.f; |
| 143 | static constexpr SkScalar kTestPad = 10.f;; |
| 144 | static constexpr int kTargetWidth = 100; |
| 145 | static constexpr int kTargetHeight = 100; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 146 | SkBitmap fBmp; |
| 147 | |
| 148 | typedef GM INHERITED; |
| 149 | }; |
| 150 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 151 | DEF_GM(return new TextureDomainEffect;) |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | #endif |