blob: e7dcf53b9d216d271c02f20dc2e3e45e492af863 [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;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000078 // use RT flag bit because in GL it makes the texture be bottom-up
79 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
80 kNone_GrTextureFlags;
bsalomon@google.com5bc34f02011-12-06 14:46:34 +000081 desc.fConfig = kSkia8888_PM_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000082 desc.fWidth = 2 * S;
83 desc.fHeight = 2 * S;
84 GrTexture* texture =
85 ctx->createUncachedTexture(desc, gTextureData, 0);
86
87 if (!texture) {
88 return;
89 }
90 GrAutoUnref au(texture);
91
92 ctx->setClip(GrRect::MakeWH(2*S, 2*S));
93 ctx->setRenderTarget(target);
94
95 GrPaint paint;
96 paint.reset();
97 paint.fColor = 0xffffffff;
bsalomon@google.com47059542012-06-06 20:51:20 +000098 paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
99 paint.fDstBlendCoeff = kISA_GrBlendCoeff;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000100 GrMatrix vm;
101 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000102 vm.setRotate(90 * SK_Scalar1,
103 S * SK_Scalar1,
104 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000105 } else {
106 vm.reset();
107 }
108 ctx->setMatrix(vm);
109 GrMatrix tm;
110 tm = vm;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000111 GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
112 *sampleMat = vm;
113 sampleMat->postIDiv(2*S, 2*S);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000114 paint.setTexture(0, texture);
115
116 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
117
118 // now update the lower right of the texture in first pass
119 // or upper right in second pass
120 offset = 0;
121 for (int y = 0; y < S; ++y) {
122 for (int x = 0; x < S; ++x) {
123 gTextureData[offset + y * stride + x] =
124 ((x + y) % 2) ? (i ? green : red) : blue;
125 }
126 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000127 texture->writePixels(S, (i ? 0 : S), S, S,
128 texture->config(), gTextureData,
129 4 * stride);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000130 ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
131 }
132 }
133 }
134
135private:
136 typedef GM INHERITED;
137};
138
139//////////////////////////////////////////////////////////////////////////////
140
141static GM* MyFactory(void*) { return new TexDataGM; }
142static GMRegistry reg(MyFactory);
143
144}
145