blob: 2c9b6eda53feb4eacb97611f6e99ccd38ab15eba [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 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 */
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00008
9// This test only works with the GPU backend.
10#if SK_SUPPORT_GPU
11
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000012#include "gm.h"
13#include "GrContext.h"
bsalomon@google.com1c31f632012-07-26 19:39:06 +000014#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000015#include "SkColorPriv.h"
16#include "SkDevice.h"
17
18namespace skiagm {
19
20extern GrContext* GetGr();
21
22static const int S = 200;
23
24class TexDataGM : public GM {
25public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000026 TexDataGM() {
27 this->setBGColor(0xff000000);
28 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000029
30protected:
31 virtual SkString onShortName() {
32 return SkString("texdata");
33 }
34
35 virtual SkISize onISize() {
36 return make_isize(2*S, 2*S);
37 }
38
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000039 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000040 SkDevice* device = canvas->getDevice();
41 GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
42 GrContext* ctx = GetGr();
43 if (ctx && target) {
44 SkPMColor gTextureData[(2 * S) * (2 * S)];
45 static const int stride = 2 * S;
46 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
47 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
48 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
49 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
50 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
51 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
52 for (int i = 0; i < 2; ++i) {
53 int offset = 0;
54 // fill upper-left
55 for (int y = 0; y < S; ++y) {
56 for (int x = 0; x < S; ++x) {
57 gTextureData[offset + y * stride + x] = gray;
58 }
59 }
60 // fill upper-right
61 offset = S;
62 for (int y = 0; y < S; ++y) {
63 for (int x = 0; x < S; ++x) {
64 gTextureData[offset + y * stride + x] = white;
65 }
66 }
67 // fill lower left
68 offset = S * stride;
69 for (int y = 0; y < S; ++y) {
70 for (int x = 0; x < S; ++x) {
71 gTextureData[offset + y * stride + x] = black;
72 }
73 }
74 // fill lower right
75 offset = S * stride + S;
76 for (int y = 0; y < S; ++y) {
77 for (int x = 0; x < S; ++x) {
78 gTextureData[offset + y * stride + x] = gray;
79 }
80 }
81
82 GrTextureDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000083 // use RT flag bit because in GL it makes the texture be bottom-up
84 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
85 kNone_GrTextureFlags;
bsalomon@google.com5bc34f02011-12-06 14:46:34 +000086 desc.fConfig = kSkia8888_PM_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000087 desc.fWidth = 2 * S;
88 desc.fHeight = 2 * S;
89 GrTexture* texture =
90 ctx->createUncachedTexture(desc, gTextureData, 0);
91
92 if (!texture) {
93 return;
94 }
95 GrAutoUnref au(texture);
96
robertphillips@google.com56c79b12012-07-11 20:57:46 +000097 GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000098
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000099 ctx->setRenderTarget(target);
100
101 GrPaint paint;
102 paint.reset();
103 paint.fColor = 0xffffffff;
bsalomon@google.com47059542012-06-06 20:51:20 +0000104 paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
105 paint.fDstBlendCoeff = kISA_GrBlendCoeff;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000106 GrMatrix vm;
107 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000108 vm.setRotate(90 * SK_Scalar1,
109 S * SK_Scalar1,
110 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000111 } else {
112 vm.reset();
113 }
114 ctx->setMatrix(vm);
115 GrMatrix tm;
116 tm = vm;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000117 GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
118 *sampleMat = vm;
119 sampleMat->postIDiv(2*S, 2*S);
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000120 paint.textureSampler(0)->setCustomStage(
121 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
122
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000123
124 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
125
126 // now update the lower right of the texture in first pass
127 // or upper right in second pass
128 offset = 0;
129 for (int y = 0; y < S; ++y) {
130 for (int x = 0; x < S; ++x) {
131 gTextureData[offset + y * stride + x] =
132 ((x + y) % 2) ? (i ? green : red) : blue;
133 }
134 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000135 texture->writePixels(S, (i ? 0 : S), S, S,
136 texture->config(), gTextureData,
137 4 * stride);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000138 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
139 }
140 }
141 }
142
143private:
144 typedef GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149static GM* MyFactory(void*) { return new TexDataGM; }
150static GMRegistry reg(MyFactory);
151
152}
153
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000154#endif