blob: c68a16a000c4bc6b1ffd8ac6edc207c574ccae6f [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"
10#include "SkColorPriv.h"
11#include "SkDevice.h"
12
13namespace skiagm {
14
15extern GrContext* GetGr();
16
17static const int S = 200;
18
19class TexDataGM : public GM {
20public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000021 TexDataGM() {
22 this->setBGColor(0xff000000);
23 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000024
25protected:
26 virtual SkString onShortName() {
27 return SkString("texdata");
28 }
29
30 virtual SkISize onISize() {
31 return make_isize(2*S, 2*S);
32 }
33
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000034 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000035 SkDevice* device = canvas->getDevice();
36 GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
37 GrContext* ctx = GetGr();
38 if (ctx && target) {
39 SkPMColor gTextureData[(2 * S) * (2 * S)];
40 static const int stride = 2 * S;
41 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
42 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
43 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
44 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
45 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
46 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
47 for (int i = 0; i < 2; ++i) {
48 int offset = 0;
49 // fill upper-left
50 for (int y = 0; y < S; ++y) {
51 for (int x = 0; x < S; ++x) {
52 gTextureData[offset + y * stride + x] = gray;
53 }
54 }
55 // fill upper-right
56 offset = S;
57 for (int y = 0; y < S; ++y) {
58 for (int x = 0; x < S; ++x) {
59 gTextureData[offset + y * stride + x] = white;
60 }
61 }
62 // fill lower left
63 offset = S * stride;
64 for (int y = 0; y < S; ++y) {
65 for (int x = 0; x < S; ++x) {
66 gTextureData[offset + y * stride + x] = black;
67 }
68 }
69 // fill lower right
70 offset = S * stride + S;
71 for (int y = 0; y < S; ++y) {
72 for (int x = 0; x < S; ++x) {
73 gTextureData[offset + y * stride + x] = gray;
74 }
75 }
76
77 GrTextureDesc desc;
78 desc.fAALevel = kNone_GrAALevel;
79 // 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
93 ctx->setClip(GrRect::MakeWH(2*S, 2*S));
94 ctx->setRenderTarget(target);
95
96 GrPaint paint;
97 paint.reset();
98 paint.fColor = 0xffffffff;
99 paint.fSrcBlendCoeff = kOne_BlendCoeff;
100 paint.fDstBlendCoeff = kISA_BlendCoeff;
101 GrMatrix vm;
102 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000103 vm.setRotate(90 * SK_Scalar1,
104 S * SK_Scalar1,
105 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000106 } else {
107 vm.reset();
108 }
109 ctx->setMatrix(vm);
110 GrMatrix tm;
111 tm = vm;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000112 GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
113 *sampleMat = vm;
114 sampleMat->postIDiv(2*S, 2*S);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000115 paint.setTexture(0, texture);
116
117 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
118
119 // now update the lower right of the texture in first pass
120 // or upper right in second pass
121 offset = 0;
122 for (int y = 0; y < S; ++y) {
123 for (int x = 0; x < S; ++x) {
124 gTextureData[offset + y * stride + x] =
125 ((x + y) % 2) ? (i ? green : red) : blue;
126 }
127 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000128 texture->writePixels(S, (i ? 0 : S), S, S,
129 texture->config(), gTextureData,
130 4 * stride);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000131 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
132 }
133 }
134 }
135
136private:
137 typedef GM INHERITED;
138};
139
140//////////////////////////////////////////////////////////////////////////////
141
142static GM* MyFactory(void*) { return new TexDataGM; }
143static GMRegistry reg(MyFactory);
144
145}
146