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 | |
| 15 | #include "GrContext.h" |
| 16 | #include "GrTest.h" |
| 17 | #include "effects/GrTextureDomain.h" |
| 18 | #include "SkBitmap.h" |
| 19 | #include "SkGr.h" |
| 20 | #include "SkGradientShader.h" |
| 21 | |
| 22 | namespace skiagm { |
| 23 | /** |
| 24 | * This GM directly exercises GrTextureDomainEffect. |
| 25 | */ |
| 26 | class TextureDomainEffect : public GM { |
| 27 | public: |
| 28 | TextureDomainEffect() { |
| 29 | this->setBGColor(0xFFFFFFFF); |
| 30 | } |
| 31 | |
| 32 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkString onShortName() override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 34 | return SkString("texture_domain_effect"); |
| 35 | } |
| 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | SkISize onISize() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 38 | const SkScalar canvasWidth = kDrawPad + |
| 39 | (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount + |
| 40 | kTestPad * GrTextureDomain::kModeCount; |
| 41 | return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onOnceBeforeDraw() override { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 45 | fBmp.allocN32Pixels(kTargetWidth, kTargetHeight); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 46 | SkCanvas canvas(fBmp); |
commit-bot@chromium.org | c5713e4 | 2014-04-07 21:18:46 +0000 | [diff] [blame] | 47 | canvas.clear(0x00000000); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 48 | SkPaint paint; |
| 49 | |
| 50 | SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; |
| 51 | paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1, |
| 52 | NULL, SK_ARRAY_COUNT(colors1)))->unref(); |
| 53 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 54 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 55 | |
| 56 | SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW }; |
| 57 | paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, NULL, |
| 58 | SK_ARRAY_COUNT(colors2)))->unref(); |
| 59 | paint.setXfermodeMode(SkXfermode::kDarken_Mode); |
| 60 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 61 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 62 | |
| 63 | SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN }; |
| 64 | paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, NULL, |
| 65 | SK_ARRAY_COUNT(colors3)))->unref(); |
| 66 | paint.setXfermodeMode(SkXfermode::kLighten_Mode); |
| 67 | canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, |
| 68 | fBmp.width() + 10.f, fBmp.height() + 10.f), paint); |
| 69 | } |
| 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 72 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 73 | if (NULL == rt) { |
| 74 | return; |
| 75 | } |
| 76 | GrContext* context = rt->getContext(); |
| 77 | if (NULL == context) { |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 78 | this->drawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | GrTestTarget tt; |
| 83 | context->getTestTarget(&tt); |
| 84 | if (NULL == tt.target()) { |
| 85 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 86 | return; |
| 87 | } |
| 88 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 89 | SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp, NULL)); |
| 90 | if (!texture) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 94 | SkTArray<SkMatrix> textureMatrices; |
| 95 | textureMatrices.push_back().setIDiv(texture->width(), texture->height()); |
| 96 | textureMatrices.push_back() = textureMatrices[0]; |
| 97 | textureMatrices.back().postScale(1.5f, 0.85f); |
| 98 | textureMatrices.push_back() = textureMatrices[0]; |
| 99 | textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f); |
| 100 | |
| 101 | const SkIRect texelDomains[] = { |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 102 | fBmp.bounds(), |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 103 | SkIRect::MakeXYWH(fBmp.width() / 4, |
| 104 | fBmp.height() / 4, |
| 105 | fBmp.width() / 2, |
| 106 | fBmp.height() / 2), |
| 107 | }; |
| 108 | |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 109 | SkRect renderRect = SkRect::Make(fBmp.bounds()); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 110 | renderRect.outset(kDrawPad, kDrawPad); |
| 111 | |
| 112 | SkScalar y = kDrawPad + kTestPad; |
| 113 | for (int tm = 0; tm < textureMatrices.count(); ++tm) { |
| 114 | for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { |
| 115 | SkScalar x = kDrawPad + kTestPad; |
| 116 | for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { |
| 117 | GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 118 | GrPipelineBuilder pipelineBuilder; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 119 | SkAutoTUnref<GrFragmentProcessor> fp( |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 120 | GrTextureDomainEffect::Create(pipelineBuilder.getProcessorDataManager(), |
| 121 | texture, textureMatrices[tm], |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 122 | GrTextureDomain::MakeTexelDomain(texture, |
| 123 | texelDomains[d]), |
| 124 | mode, GrTextureParams::kNone_FilterMode)); |
| 125 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 126 | if (!fp) { |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 127 | continue; |
| 128 | } |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 129 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 130 | pipelineBuilder.setRenderTarget(rt); |
| 131 | pipelineBuilder.addColorProcessor(fp); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 132 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 133 | tt.target()->drawSimpleRect(pipelineBuilder, |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 134 | GrColor_WHITE, |
| 135 | viewMatrix, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 136 | renderRect); |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 137 | x += renderRect.width() + kTestPad; |
| 138 | } |
| 139 | y += renderRect.height() + kTestPad; |
| 140 | } |
| 141 | } |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 145 | static const SkScalar kDrawPad; |
| 146 | static const SkScalar kTestPad; |
| 147 | static const int kTargetWidth = 100; |
| 148 | static const int kTargetHeight = 100; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 149 | SkBitmap fBmp; |
| 150 | |
| 151 | typedef GM INHERITED; |
| 152 | }; |
| 153 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 154 | // Windows builds did not like SkScalar initialization in class :( |
| 155 | const SkScalar TextureDomainEffect::kDrawPad = 10.f; |
| 156 | const SkScalar TextureDomainEffect::kTestPad = 10.f; |
| 157 | |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 158 | DEF_GM( return SkNEW(TextureDomainEffect); ) |
| 159 | } |
| 160 | |
| 161 | #endif |