texdata.cpp revision 1c31f633232df572f89a3bc1b0fee3e46d22cb5b
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 "effects/GrSingleTextureEffect.h"
11#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:
22    TexDataGM() {
23        this->setBGColor(0xff000000);
24    }
25
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
35    virtual void onDraw(SkCanvas* canvas) {
36        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;
79                // use RT flag bit because in GL it makes the texture be bottom-up
80                desc.fFlags     = i ? kRenderTarget_GrTextureFlagBit :
81                                      kNone_GrTextureFlags;
82                desc.fConfig    = kSkia8888_PM_GrPixelConfig;
83                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                GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
94
95                ctx->setRenderTarget(target);
96
97                GrPaint paint;
98                paint.reset();
99                paint.fColor = 0xffffffff;
100                paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
101                paint.fDstBlendCoeff = kISA_GrBlendCoeff;
102                GrMatrix vm;
103                if (i) {
104                    vm.setRotate(90 * SK_Scalar1,
105                                 S * SK_Scalar1,
106                                 S * SK_Scalar1);
107                } else {
108                    vm.reset();
109                }
110                ctx->setMatrix(vm);
111                GrMatrix tm;
112                tm = vm;
113                GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
114                *sampleMat = vm;
115                sampleMat->postIDiv(2*S, 2*S);
116                paint.textureSampler(0)->setCustomStage(
117                    SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
118
119
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                }
131                texture->writePixels(S, (i ? 0 : S), S, S,
132                                     texture->config(), gTextureData,
133                                     4 * stride);
134                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
150