commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | // This test only works with the GPU backend. |
| 10 | |
| 11 | #include "gm.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame^] | 15 | #include "GrDrawContext.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 16 | #include "GrContext.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 17 | #include "SkBitmap.h" |
| 18 | #include "SkGr.h" |
| 19 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame^] | 20 | #include "batches/GrDrawBatch.h" |
| 21 | #include "batches/GrRectBatchFactory.h" |
| 22 | #include "effects/GrTextureDomain.h" |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | /** |
| 26 | * This GM directly exercises GrTextureDomainEffect. |
| 27 | */ |
| 28 | class TextureDomainEffect : public GM { |
| 29 | public: |
| 30 | TextureDomainEffect() { |
| 31 | this->setBGColor(0xFFFFFFFF); |
| 32 | } |
| 33 | |
| 34 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkString onShortName() override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 36 | return SkString("texture_domain_effect"); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkISize onISize() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 40 | const SkScalar canvasWidth = kDrawPad + |
| 41 | (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 42 | kTestPad * GrTextureDomain::kModeCount; |
| 43 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 44 | } |
| 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void onOnceBeforeDraw() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 47 | fBmp.allocN32Pixels(kTargetWidth, kTargetHeight); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 48 | SkCanvas canvas(fBmp); |
commit-bot@chromium.org | c5713e4 | 2014-04-07 21:18:46 +0000 | [diff] [blame] | 49 | canvas.clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 50 | SkPaint paint; |
| 51 | |
| 52 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
| 53 | paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | nullptr, SK_ARRAY_COUNT(colors1)))->unref(); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 55 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 56 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 57 | |
| 58 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, nullptr, |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 60 | SK_ARRAY_COUNT(colors2)))->unref(); |
| 61 | paint.setXfermodeMode(SkXfermode::kDarken_Mode); |
| 62 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 63 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 64 | |
| 65 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, nullptr, |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 67 | SK_ARRAY_COUNT(colors3)))->unref(); |
| 68 | paint.setXfermodeMode(SkXfermode::kLighten_Mode); |
| 69 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 70 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 71 | } |
| 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 74 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | if (nullptr == rt) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | GrContext* context = rt->getContext(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | if (nullptr == context) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 80 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame^] | 84 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt)); |
| 85 | if (!drawContext) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 89 | SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp, |
| 90 | GrTextureParams::ClampNoFilter())); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 91 | if (!texture) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 95 | SkTArray<SkMatrix> textureMatrices; |
| 96 | textureMatrices.push_back().setIDiv(texture->width(), texture->height()); |
| 97 | textureMatrices.push_back() = textureMatrices[0]; |
| 98 | textureMatrices.back().postScale(1.5f, 0.85f); |
| 99 | textureMatrices.push_back() = textureMatrices[0]; |
| 100 | textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f); |
| 101 | |
| 102 | const SkIRect texelDomains[] = { |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 103 | fBmp.bounds(), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 104 | SkIRect::MakeXYWH(fBmp.width() / 4, |
| 105 | fBmp.height() / 4, |
| 106 | fBmp.width() / 2, |
| 107 | fBmp.height() / 2), |
| 108 | }; |
| 109 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 110 | SkRect renderRect = SkRect::Make(fBmp.bounds()); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 111 | renderRect.outset(kDrawPad, kDrawPad); |
| 112 | |
| 113 | SkScalar y = kDrawPad + kTestPad; |
| 114 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
| 115 | for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { |
| 116 | SkScalar x = kDrawPad + kTestPad; |
| 117 | for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { |
| 118 | GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 119 | GrPipelineBuilder pipelineBuilder; |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 120 | pipelineBuilder.setXPFactory( |
| 121 | GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref(); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 122 | SkAutoTUnref<const GrFragmentProcessor> fp( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 123 | GrTextureDomainEffect::Create(texture, textureMatrices[tm], |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 124 | GrTextureDomain::MakeTexelDomain(texture, |
| 125 | texelDomains[d]), |
| 126 | mode, GrTextureParams::kNone_FilterMode)); |
| 127 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | if (!fp) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 129 | continue; |
| 130 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 131 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 132 | pipelineBuilder.setRenderTarget(rt); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 133 | pipelineBuilder.addColorFragmentProcessor(fp); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 134 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame^] | 135 | SkAutoTUnref<GrDrawBatch> batch( |
| 136 | GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix, |
| 137 | renderRect, nullptr, nullptr)); |
| 138 | drawContext->internal_drawBatch(pipelineBuilder, batch); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 139 | x += renderRect.width() + kTestPad; |
| 140 | } |
| 141 | y += renderRect.height() + kTestPad; |
| 142 | } |
| 143 | } |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 147 | static const SkScalar kDrawPad; |
| 148 | static const SkScalar kTestPad; |
| 149 | static const int kTargetWidth = 100; |
| 150 | static const int kTargetHeight = 100; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 151 | SkBitmap fBmp; |
| 152 | |
| 153 | typedef GM INHERITED; |
| 154 | }; |
| 155 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 156 | // Windows builds did not like SkScalar initialization in class :( |
| 157 | const SkScalar TextureDomainEffect::kDrawPad = 10.f; |
| 158 | const SkScalar TextureDomainEffect::kTestPad = 10.f; |
| 159 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 160 | DEF_GM(return new TextureDomainEffect;) |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | #endif |