Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "SkPerlinNoiseShader.h" | |
| 21 | |
| 22 namespace skiagm { | |
| 23 /** | |
|
robertphillips
2014/03/25 12:12:40
Fix comment?
bsalomon
2014/03/25 14:36:36
Done.
| |
| 24 * This GM directly exercises a GrEffect that draws convex polygons. | |
| 25 */ | |
| 26 class TextureDomainEffect : public GM { | |
| 27 public: | |
| 28 TextureDomainEffect() { | |
| 29 this->setBGColor(0xFFFFFFFF); | |
| 30 } | |
| 31 | |
| 32 protected: | |
| 33 virtual SkString onShortName() SK_OVERRIDE { | |
| 34 return SkString("texture_domain_effect"); | |
| 35 } | |
| 36 | |
| 37 virtual SkISize onISize() SK_OVERRIDE { | |
| 38 return make_isize(400, 800); | |
| 39 } | |
| 40 | |
| 41 virtual uint32_t onGetFlags() const SK_OVERRIDE { | |
| 42 // This is a GPU-specific GM. | |
| 43 return kGPUOnly_Flag; | |
| 44 } | |
| 45 | |
| 46 virtual void onOnceBeforeDraw() SK_OVERRIDE { | |
| 47 fBmp.allocN32Pixels(100, 100); | |
| 48 SkCanvas canvas(fBmp); | |
| 49 SkPaint paint; | |
| 50 paint.setShader(SkPerlinNoiseShader::CreateFractalNoise(0.2f, 0.3f, 3, 0 .f))->unref(); | |
| 51 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, | |
| 52 fBmp.width() + 10.f, fBmp.height() + 10 .f), paint); | |
| 53 } | |
| 54 | |
| 55 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
| 56 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget (); | |
| 57 if (NULL == rt) { | |
| 58 return; | |
| 59 } | |
| 60 GrContext* context = rt->getContext(); | |
| 61 if (NULL == context) { | |
| 62 return; | |
| 63 } | |
| 64 | |
| 65 GrTestTarget tt; | |
| 66 context->getTestTarget(&tt); | |
| 67 if (NULL == tt.target()) { | |
| 68 SkDEBUGFAIL("Couldn't get Gr test target."); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 GrDrawState* drawState = tt.target()->drawState(); | |
| 73 | |
| 74 GrTexture* texture = GrLockAndRefCachedBitmapTexture(context, fBmp, NULL ); | |
| 75 if (NULL == texture) { | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 static const SkScalar kDrawPad = 10.f; | |
| 80 static const SkScalar kTestPad = 10.f; | |
| 81 | |
| 82 SkTArray<SkMatrix> textureMatrices; | |
| 83 textureMatrices.push_back().setIDiv(texture->width(), texture->height()) ; | |
| 84 textureMatrices.push_back() = textureMatrices[0]; | |
| 85 textureMatrices.back().postScale(1.5f, 0.85f); | |
| 86 textureMatrices.push_back() = textureMatrices[0]; | |
| 87 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture-> height() / 2.f); | |
| 88 | |
| 89 const SkIRect texelDomains[] = { | |
| 90 SkIRect::MakeWH(fBmp.width(), fBmp.height()), | |
| 91 SkIRect::MakeXYWH(fBmp.width() / 4, | |
| 92 fBmp.height() / 4, | |
| 93 fBmp.width() / 2, | |
| 94 fBmp.height() / 2), | |
| 95 }; | |
| 96 | |
| 97 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp.width()), | |
|
robertphillips
2014/03/25 12:12:40
rm extra space?
bsalomon
2014/03/25 14:36:36
Done.
| |
| 98 SkIntToScalar(fBmp.height())); | |
| 99 renderRect.outset(kDrawPad, kDrawPad); | |
| 100 | |
| 101 SkScalar y = kDrawPad + kTestPad; | |
| 102 for (int tm = 0; tm < textureMatrices.count(); ++tm) { | |
| 103 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) { | |
| 104 SkScalar x = kDrawPad + kTestPad; | |
| 105 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) { | |
| 106 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m; | |
| 107 SkAutoTUnref<GrEffectRef> effect( | |
| 108 GrTextureDomainEffect::Create(texture, textureMatrices[t m], | |
| 109 GrTextureDomain::MakeTexelDomain (texture, | |
| 110 texelDomains[d]), | |
| 111 mode, GrTextureParams::kNone_Fil terMode)); | |
| 112 | |
| 113 if (!effect) { | |
| 114 continue; | |
| 115 } | |
| 116 SkMatrix viewMatrix; | |
| 117 viewMatrix.setTranslate(x, y); | |
| 118 drawState->reset(viewMatrix); | |
| 119 drawState->setRenderTarget(rt); | |
| 120 drawState->setColor(0xffffffff); | |
| 121 drawState->addColorEffect(effect, 1); | |
| 122 | |
| 123 tt.target()->drawSimpleRect(renderRect); | |
| 124 x += renderRect.width() + kTestPad; | |
| 125 } | |
| 126 y += renderRect.height() + kTestPad; | |
| 127 } | |
| 128 } | |
| 129 GrUnlockAndUnrefCachedBitmapTexture(texture); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 SkBitmap fBmp; | |
| 134 | |
| 135 typedef GM INHERITED; | |
| 136 }; | |
| 137 | |
| 138 DEF_GM( return SkNEW(TextureDomainEffect); ) | |
| 139 } | |
| 140 | |
| 141 #endif | |
| OLD | NEW |