blob: 2563faddcf46872be4590631a6887ffa30ae470a [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:
21 TexDataGM() {}
22
23protected:
24 virtual SkString onShortName() {
25 return SkString("texdata");
26 }
27
28 virtual SkISize onISize() {
29 return make_isize(2*S, 2*S);
30 }
31
32 void drawBG(SkCanvas* canvas) {
33 canvas->drawColor(0xff000000);
34 }
35
36 virtual void onDraw(SkCanvas* canvas) {
37 drawBG(canvas);
38 SkDevice* device = canvas->getDevice();
39 GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
40 GrContext* ctx = GetGr();
41 if (ctx && target) {
42 SkPMColor gTextureData[(2 * S) * (2 * S)];
43 static const int stride = 2 * S;
44 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
45 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
46 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
47 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
48 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
49 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
50 for (int i = 0; i < 2; ++i) {
51 int offset = 0;
52 // fill upper-left
53 for (int y = 0; y < S; ++y) {
54 for (int x = 0; x < S; ++x) {
55 gTextureData[offset + y * stride + x] = gray;
56 }
57 }
58 // fill upper-right
59 offset = S;
60 for (int y = 0; y < S; ++y) {
61 for (int x = 0; x < S; ++x) {
62 gTextureData[offset + y * stride + x] = white;
63 }
64 }
65 // fill lower left
66 offset = S * stride;
67 for (int y = 0; y < S; ++y) {
68 for (int x = 0; x < S; ++x) {
69 gTextureData[offset + y * stride + x] = black;
70 }
71 }
72 // fill lower right
73 offset = S * stride + S;
74 for (int y = 0; y < S; ++y) {
75 for (int x = 0; x < S; ++x) {
76 gTextureData[offset + y * stride + x] = gray;
77 }
78 }
79
80 GrTextureDesc desc;
81 desc.fAALevel = kNone_GrAALevel;
82 // use RT flag bit because in GL it makes the texture be bottom-up
83 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
84 kNone_GrTextureFlags;
85 desc.fFormat = kRGBA_8888_GrPixelConfig;
86 desc.fWidth = 2 * S;
87 desc.fHeight = 2 * S;
88 GrTexture* texture =
89 ctx->createUncachedTexture(desc, gTextureData, 0);
90
91 if (!texture) {
92 return;
93 }
94 GrAutoUnref au(texture);
95
96 ctx->setClip(GrRect::MakeWH(2*S, 2*S));
97 ctx->setRenderTarget(target);
98
99 GrPaint paint;
100 paint.reset();
101 paint.fColor = 0xffffffff;
102 paint.fSrcBlendCoeff = kOne_BlendCoeff;
103 paint.fDstBlendCoeff = kISA_BlendCoeff;
104 GrMatrix vm;
105 if (i) {
106 vm.setRotate(90, S , S);
107 } else {
108 vm.reset();
109 }
110 ctx->setMatrix(vm);
111 GrMatrix tm;
112 tm = vm;
113 tm.postIDiv(2*S, 2*S);
114 paint.getTextureSampler(0)->setMatrix(tm);
115 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 }
128 // BUG: uploadTextureData doesn't force a flush
129 ctx->flush();
130 texture->uploadTextureData(S, i ? 0 : S, S, S, gTextureData, 4 * stride);
131 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