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: TextureDomainEffect()28 TextureDomainEffect() { 29 this->setBGColor(0xFFFFFFFF); 30 } 31 32 protected: onShortName()33 virtual SkString onShortName() SK_OVERRIDE { 34 return SkString("texture_domain_effect"); 35 } 36 onISize()37 virtual SkISize onISize() SK_OVERRIDE { 38 return SkISize::Make(400, 800); 39 } 40 onGetFlags() const41 virtual uint32_t onGetFlags() const SK_OVERRIDE { 42 // This is a GPU-specific GM. 43 return kGPUOnly_Flag; 44 } 45 onOnceBeforeDraw()46 virtual void onOnceBeforeDraw() SK_OVERRIDE { 47 fBmp.allocN32Pixels(100, 100); 48 SkCanvas canvas(fBmp); 49 canvas.clear(0x00000000); 50 SkPaint paint; 51 52 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY }; 53 paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1, 54 NULL, SK_ARRAY_COUNT(colors1)))->unref(); 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 }; 59 paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, NULL, 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 }; 66 paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, NULL, 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 onDraw(SkCanvas * canvas)73 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 74 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); 75 if (NULL == rt) { 76 return; 77 } 78 GrContext* context = rt->getContext(); 79 if (NULL == context) { 80 return; 81 } 82 83 GrTestTarget tt; 84 context->getTestTarget(&tt); 85 if (NULL == tt.target()) { 86 SkDEBUGFAIL("Couldn't get Gr test target."); 87 return; 88 } 89 90 GrDrawState* drawState = tt.target()->drawState(); 91 92 GrTexture* texture = GrLockAndRefCachedBitmapTexture(context, fBmp, NULL); 93 if (NULL == texture) { 94 return; 95 } 96 97 static const SkScalar kDrawPad = 10.f; 98 static const SkScalar kTestPad = 10.f; 99 100 SkTArray<SkMatrix> textureMatrices; 101 textureMatrices.push_back().setIDiv(texture->width(), texture->height()); 102 textureMatrices.push_back() = textureMatrices[0]; 103 textureMatrices.back().postScale(1.5f, 0.85f); 104 textureMatrices.push_back() = textureMatrices[0]; 105 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f); 106 107 const SkIRect texelDomains[] = { 108 SkIRect::MakeWH(fBmp.width(), fBmp.height()), 109 SkIRect::MakeXYWH(fBmp.width() / 4, 110 fBmp.height() / 4, 111 fBmp.width() / 2, 112 fBmp.height() / 2), 113 }; 114 115 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp.width()), 116 SkIntToScalar(fBmp.height())); 117 renderRect.outset(kDrawPad, kDrawPad); 118 119 SkScalar y = kDrawPad + kTestPad; 120 for (int tm = 0; tm < textureMatrices.count(); ++tm) { 121 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { 122 SkScalar x = kDrawPad + kTestPad; 123 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { 124 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; 125 SkAutoTUnref<GrEffectRef> effect( 126 GrTextureDomainEffect::Create(texture, textureMatrices[tm], 127 GrTextureDomain::MakeTexelDomain(texture, 128 texelDomains[d]), 129 mode, GrTextureParams::kNone_FilterMode)); 130 131 if (!effect) { 132 continue; 133 } 134 SkMatrix viewMatrix; 135 viewMatrix.setTranslate(x, y); 136 drawState->reset(viewMatrix); 137 drawState->setRenderTarget(rt); 138 drawState->setColor(0xffffffff); 139 drawState->addColorEffect(effect, 1); 140 141 tt.target()->drawSimpleRect(renderRect); 142 x += renderRect.width() + kTestPad; 143 } 144 y += renderRect.height() + kTestPad; 145 } 146 } 147 GrUnlockAndUnrefCachedBitmapTexture(texture); 148 } 149 150 private: 151 SkBitmap fBmp; 152 153 typedef GM INHERITED; 154 }; 155 156 DEF_GM( return SkNEW(TextureDomainEffect); ) 157 } 158 159 #endif 160