blob: 927ee170c315666cc58f2ca584812710a142f1ae [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.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000011#include "gm.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000012
13#if SK_SUPPORT_GPU
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000014#include "GrContext.h"
bsalomon@google.com1c31f632012-07-26 19:39:06 +000015#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000016#include "SkColorPriv.h"
17#include "SkDevice.h"
18
19namespace skiagm {
20
21extern GrContext* GetGr();
22
23static const int S = 200;
24
25class TexDataGM : public GM {
26public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000027 TexDataGM() {
28 this->setBGColor(0xff000000);
29 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000030
31protected:
32 virtual SkString onShortName() {
33 return SkString("texdata");
34 }
35
36 virtual SkISize onISize() {
37 return make_isize(2*S, 2*S);
38 }
39
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000040 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000041 SkDevice* device = canvas->getDevice();
42 GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
43 GrContext* ctx = GetGr();
44 if (ctx && target) {
45 SkPMColor gTextureData[(2 * S) * (2 * S)];
46 static const int stride = 2 * S;
47 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
48 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
49 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
50 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
51 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
52 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
53 for (int i = 0; i < 2; ++i) {
54 int offset = 0;
55 // fill upper-left
56 for (int y = 0; y < S; ++y) {
57 for (int x = 0; x < S; ++x) {
58 gTextureData[offset + y * stride + x] = gray;
59 }
60 }
61 // fill upper-right
62 offset = S;
63 for (int y = 0; y < S; ++y) {
64 for (int x = 0; x < S; ++x) {
65 gTextureData[offset + y * stride + x] = white;
66 }
67 }
68 // fill lower left
69 offset = S * stride;
70 for (int y = 0; y < S; ++y) {
71 for (int x = 0; x < S; ++x) {
72 gTextureData[offset + y * stride + x] = black;
73 }
74 }
75 // fill lower right
76 offset = S * stride + S;
77 for (int y = 0; y < S; ++y) {
78 for (int x = 0; x < S; ++x) {
79 gTextureData[offset + y * stride + x] = gray;
80 }
81 }
82
83 GrTextureDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000084 // use RT flag bit because in GL it makes the texture be bottom-up
85 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
86 kNone_GrTextureFlags;
bsalomon@google.com5bc34f02011-12-06 14:46:34 +000087 desc.fConfig = kSkia8888_PM_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000088 desc.fWidth = 2 * S;
89 desc.fHeight = 2 * S;
90 GrTexture* texture =
91 ctx->createUncachedTexture(desc, gTextureData, 0);
92
93 if (!texture) {
94 return;
95 }
96 GrAutoUnref au(texture);
97
robertphillips@google.com56c79b12012-07-11 20:57:46 +000098 GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000099
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000100 ctx->setRenderTarget(target);
101
102 GrPaint paint;
103 paint.reset();
104 paint.fColor = 0xffffffff;
bsalomon@google.com47059542012-06-06 20:51:20 +0000105 paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
106 paint.fDstBlendCoeff = kISA_GrBlendCoeff;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000107 GrMatrix vm;
108 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000109 vm.setRotate(90 * SK_Scalar1,
110 S * SK_Scalar1,
111 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000112 } else {
113 vm.reset();
114 }
115 ctx->setMatrix(vm);
116 GrMatrix tm;
117 tm = vm;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000118 GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
119 *sampleMat = vm;
120 sampleMat->postIDiv(2*S, 2*S);
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000121 paint.textureSampler(0)->setCustomStage(
122 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
123
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000124
125 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
126
127 // now update the lower right of the texture in first pass
128 // or upper right in second pass
129 offset = 0;
130 for (int y = 0; y < S; ++y) {
131 for (int x = 0; x < S; ++x) {
132 gTextureData[offset + y * stride + x] =
133 ((x + y) % 2) ? (i ? green : red) : blue;
134 }
135 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000136 texture->writePixels(S, (i ? 0 : S), S, S,
137 texture->config(), gTextureData,
138 4 * stride);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000139 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
140 }
141 }
142 }
143
144private:
145 typedef GM INHERITED;
146};
147
148//////////////////////////////////////////////////////////////////////////////
149
150static GM* MyFactory(void*) { return new TexDataGM; }
151static GMRegistry reg(MyFactory);
152
153}
154
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000155#endif