1
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 */
8#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        this->setBGColor(0xff000000);
23    }
24
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
34    virtual void onDraw(SkCanvas* canvas) {
35        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                // use RT flag bit because in GL it makes the texture be bottom-up
79                desc.fFlags     = i ? kRenderTarget_GrTextureFlagBit :
80                                      kNone_GrTextureFlags;
81                desc.fConfig    = kSkia8888_PM_GrPixelConfig;
82                desc.fWidth     = 2 * S;
83                desc.fHeight    = 2 * S;
84                desc.fSampleCnt = 0;
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) {
103                    vm.setRotate(90 * SK_Scalar1,
104                                 S * SK_Scalar1,
105                                 S * SK_Scalar1);
106                } else {
107                    vm.reset();
108                }
109                ctx->setMatrix(vm);
110                GrMatrix tm;
111                tm = vm;
112                GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
113                *sampleMat = vm;
114                sampleMat->postIDiv(2*S, 2*S);
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                texture->writePixels(S, (i ? 0 : S), S, S,
129                                     texture->config(), gTextureData,
130                                     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
147