blob: 1d1317704bcc2fc7ff663578ed913eb5ef462118 [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.comd9f826c2011-07-18 15:25:04 +00008#include "gm.h"
9#include "GrContext.h"
bsalomon@google.com1c31f632012-07-26 19:39:06 +000010#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000011#include "SkColorPriv.h"
12#include "SkDevice.h"
13
14namespace skiagm {
15
16extern GrContext* GetGr();
17
18static const int S = 200;
19
20class TexDataGM : public GM {
21public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000022 TexDataGM() {
23 this->setBGColor(0xff000000);
24 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000025
26protected:
27 virtual SkString onShortName() {
28 return SkString("texdata");
29 }
30
31 virtual SkISize onISize() {
32 return make_isize(2*S, 2*S);
33 }
34
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000035 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000036 SkDevice* device = canvas->getDevice();
37 GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
38 GrContext* ctx = GetGr();
39 if (ctx && target) {
40 SkPMColor gTextureData[(2 * S) * (2 * S)];
41 static const int stride = 2 * S;
42 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
43 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
44 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
45 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
46 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
47 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
48 for (int i = 0; i < 2; ++i) {
49 int offset = 0;
50 // fill upper-left
51 for (int y = 0; y < S; ++y) {
52 for (int x = 0; x < S; ++x) {
53 gTextureData[offset + y * stride + x] = gray;
54 }
55 }
56 // fill upper-right
57 offset = S;
58 for (int y = 0; y < S; ++y) {
59 for (int x = 0; x < S; ++x) {
60 gTextureData[offset + y * stride + x] = white;
61 }
62 }
63 // fill lower left
64 offset = S * stride;
65 for (int y = 0; y < S; ++y) {
66 for (int x = 0; x < S; ++x) {
67 gTextureData[offset + y * stride + x] = black;
68 }
69 }
70 // fill lower right
71 offset = S * stride + S;
72 for (int y = 0; y < S; ++y) {
73 for (int x = 0; x < S; ++x) {
74 gTextureData[offset + y * stride + x] = gray;
75 }
76 }
77
78 GrTextureDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000079 // use RT flag bit because in GL it makes the texture be bottom-up
80 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
81 kNone_GrTextureFlags;
bsalomon@google.com5bc34f02011-12-06 14:46:34 +000082 desc.fConfig = kSkia8888_PM_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000083 desc.fWidth = 2 * S;
84 desc.fHeight = 2 * S;
85 GrTexture* texture =
86 ctx->createUncachedTexture(desc, gTextureData, 0);
87
88 if (!texture) {
89 return;
90 }
91 GrAutoUnref au(texture);
92
robertphillips@google.com56c79b12012-07-11 20:57:46 +000093 GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000094
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000095 ctx->setRenderTarget(target);
96
97 GrPaint paint;
98 paint.reset();
99 paint.fColor = 0xffffffff;
bsalomon@google.com47059542012-06-06 20:51:20 +0000100 paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
101 paint.fDstBlendCoeff = kISA_GrBlendCoeff;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000102 GrMatrix vm;
103 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000104 vm.setRotate(90 * SK_Scalar1,
105 S * SK_Scalar1,
106 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000107 } else {
108 vm.reset();
109 }
110 ctx->setMatrix(vm);
111 GrMatrix tm;
112 tm = vm;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000113 GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
114 *sampleMat = vm;
115 sampleMat->postIDiv(2*S, 2*S);
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000116 paint.textureSampler(0)->setCustomStage(
117 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
118
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000119
120 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
121
122 // now update the lower right of the texture in first pass
123 // or upper right in second pass
124 offset = 0;
125 for (int y = 0; y < S; ++y) {
126 for (int x = 0; x < S; ++x) {
127 gTextureData[offset + y * stride + x] =
128 ((x + y) % 2) ? (i ? green : red) : blue;
129 }
130 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000131 texture->writePixels(S, (i ? 0 : S), S, S,
132 texture->config(), gTextureData,
133 4 * stride);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000134 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
135 }
136 }
137 }
138
139private:
140 typedef GM INHERITED;
141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145static GM* MyFactory(void*) { return new TexDataGM; }
146static GMRegistry reg(MyFactory);
147
148}
149