| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2011 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 |  */ | 
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 7 |  | 
 | 8 | // This test only works with the GPU backend. | 
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 9 |  | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 10 | #include "gm.h" | 
| bsalomon@google.com | a68937c | 2012-08-03 15:00:52 +0000 | [diff] [blame] | 11 |  | 
 | 12 | #if SK_SUPPORT_GPU | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 13 | #include "GrContext.h" | 
| robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #include "GrDrawContext.h" | 
| egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 15 | #include "SkColorPriv.h" | 
| egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 16 | #include "effects/GrPorterDuffXferProcessor.h" | 
 | 17 | #include "effects/GrSimpleTextureEffect.h" | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 18 |  | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 19 | static const int S = 200; | 
 | 20 |  | 
| halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 21 | DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) { | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 22 |     GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); | 
 | 23 |     if (!drawContext) { | 
 | 24 |         skiagm::GM::DrawGpuOnlyMessage(canvas); | 
 | 25 |         return; | 
 | 26 |     } | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 27 |  | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 28 |     GrContext* context = canvas->getGrContext(); | 
 | 29 |     if (!context) { | 
 | 30 |         return; | 
 | 31 |     } | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 32 |  | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 33 |     SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S)); | 
 | 34 |     static const int stride = 2 * S; | 
 | 35 |     static const SkPMColor gray  = SkPackARGB32(0x40, 0x40, 0x40, 0x40); | 
 | 36 |     static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff); | 
 | 37 |     static const SkPMColor red   = SkPackARGB32(0x80, 0x80, 0x00, 0x00); | 
 | 38 |     static const SkPMColor blue  = SkPackARGB32(0x80, 0x00, 0x00, 0x80); | 
 | 39 |     static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00); | 
 | 40 |     static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00); | 
 | 41 |     for (int i = 0; i < 2; ++i) { | 
 | 42 |         int offset = 0; | 
 | 43 |         // fill upper-left | 
 | 44 |         for (int y = 0; y < S; ++y) { | 
 | 45 |             for (int x = 0; x < S; ++x) { | 
 | 46 |                 gTextureData[offset + y * stride + x] = gray; | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 47 |             } | 
 | 48 |         } | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 49 |         // fill upper-right | 
 | 50 |         offset = S; | 
 | 51 |         for (int y = 0; y < S; ++y) { | 
 | 52 |             for (int x = 0; x < S; ++x) { | 
 | 53 |                 gTextureData[offset + y * stride + x] = white; | 
 | 54 |             } | 
 | 55 |         } | 
 | 56 |         // fill lower left | 
 | 57 |         offset = S * stride; | 
 | 58 |         for (int y = 0; y < S; ++y) { | 
 | 59 |             for (int x = 0; x < S; ++x) { | 
 | 60 |                 gTextureData[offset + y * stride + x] = black; | 
 | 61 |             } | 
 | 62 |         } | 
 | 63 |         // fill lower right | 
 | 64 |         offset = S * stride + S; | 
 | 65 |         for (int y = 0; y < S; ++y) { | 
 | 66 |             for (int x = 0; x < S; ++x) { | 
 | 67 |                 gTextureData[offset + y * stride + x] = gray; | 
 | 68 |             } | 
 | 69 |         } | 
 | 70 |  | 
 | 71 |         GrSurfaceDesc desc; | 
| robertphillips | 677da9d | 2016-05-11 05:15:55 -0700 | [diff] [blame] | 72 |         desc.fOrigin    = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 73 |         desc.fConfig    = kSkia8888_GrPixelConfig; | 
 | 74 |         desc.fWidth     = 2 * S; | 
 | 75 |         desc.fHeight    = 2 * S; | 
 | 76 |         GrTexture* texture = context->textureProvider()->createTexture( | 
 | 77 |             desc, SkBudgeted::kNo, gTextureData.get(), 0); | 
 | 78 |  | 
 | 79 |         if (!texture) { | 
 | 80 |             return; | 
 | 81 |         } | 
 | 82 |         SkAutoTUnref<GrTexture> au(texture); | 
 | 83 |  | 
 | 84 |         // setup new clip | 
| cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 85 |         GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S)); | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 86 |  | 
 | 87 |         GrPaint paint; | 
 | 88 |         paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode); | 
 | 89 |  | 
 | 90 |         SkMatrix vm; | 
 | 91 |         if (i) { | 
 | 92 |             vm.setRotate(90 * SK_Scalar1, | 
 | 93 |                             S * SK_Scalar1, | 
 | 94 |                             S * SK_Scalar1); | 
 | 95 |         } else { | 
 | 96 |             vm.reset(); | 
 | 97 |         } | 
 | 98 |         SkMatrix tm; | 
 | 99 |         tm = vm; | 
 | 100 |         tm.postIDiv(2*S, 2*S); | 
 | 101 |         paint.addColorTextureProcessor(texture, tm); | 
 | 102 |  | 
 | 103 |         drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); | 
 | 104 |  | 
 | 105 |         // now update the lower right of the texture in first pass | 
 | 106 |         // or upper right in second pass | 
 | 107 |         offset = 0; | 
 | 108 |         for (int y = 0; y < S; ++y) { | 
 | 109 |             for (int x = 0; x < S; ++x) { | 
 | 110 |                 gTextureData[offset + y * stride + x] = | 
 | 111 |                     ((x + y) % 2) ? (i ? green : red) : blue; | 
 | 112 |             } | 
 | 113 |         } | 
 | 114 |         texture->writePixels(S, (i ? 0 : S), S, S, | 
 | 115 |                                 texture->config(), gTextureData.get(), | 
 | 116 |                                 4 * stride); | 
 | 117 |         drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S)); | 
 | 118 |     } | 
| bsalomon@google.com | d9f826c | 2011-07-18 15:25:04 +0000 | [diff] [blame] | 119 | } | 
| bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 120 | #endif | 
| robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 121 |  |