texdata.cpp revision ce49e16d3b4a5b554d75a609e4ac8973c23c17e2
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
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU
14#include "GrContext.h"
15#include "effects/GrSingleTextureEffect.h"
16#include "SkColorPriv.h"
17#include "SkDevice.h"
18
19namespace skiagm {
20
21extern GrContext* GetGr();
22
23static const int S = 200;
24
25class TexDataGM : public GM {
26public:
27    TexDataGM() {
28        this->setBGColor(0xff000000);
29    }
30
31protected:
32    virtual SkString onShortName() {
33        return SkString("texdata");
34    }
35
36    virtual SkISize onISize() {
37        return make_isize(2*S, 2*S);
38    }
39
40    virtual void onDraw(SkCanvas* canvas) {
41        SkDevice* device = canvas->getDevice();
42        GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
43        GrContext* ctx = GetGr();
44        if (ctx && target) {
45            SkPMColor gTextureData[(2 * S) * (2 * S)];
46            static const int stride = 2 * S;
47            static const SkPMColor gray  = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
48            static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
49            static const SkPMColor red   = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
50            static const SkPMColor blue  = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
51            static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
52            static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
53            for (int i = 0; i < 2; ++i) {
54                int offset = 0;
55                // fill upper-left
56                for (int y = 0; y < S; ++y) {
57                    for (int x = 0; x < S; ++x) {
58                        gTextureData[offset + y * stride + x] = gray;
59                    }
60                }
61                // fill upper-right
62                offset = S;
63                for (int y = 0; y < S; ++y) {
64                    for (int x = 0; x < S; ++x) {
65                        gTextureData[offset + y * stride + x] = white;
66                    }
67                }
68                // fill lower left
69                offset = S * stride;
70                for (int y = 0; y < S; ++y) {
71                    for (int x = 0; x < S; ++x) {
72                        gTextureData[offset + y * stride + x] = black;
73                    }
74                }
75                // fill lower right
76                offset = S * stride + S;
77                for (int y = 0; y < S; ++y) {
78                    for (int x = 0; x < S; ++x) {
79                        gTextureData[offset + y * stride + x] = gray;
80                    }
81                }
82
83                GrTextureDesc desc;
84                // use RT flag bit because in GL it makes the texture be bottom-up
85                desc.fFlags     = i ? kRenderTarget_GrTextureFlagBit :
86                                      kNone_GrTextureFlags;
87                desc.fConfig    = kSkia8888_PM_GrPixelConfig;
88                desc.fWidth     = 2 * S;
89                desc.fHeight    = 2 * S;
90                GrTexture* texture =
91                    ctx->createUncachedTexture(desc, gTextureData, 0);
92
93                if (!texture) {
94                    return;
95                }
96                GrAutoUnref au(texture);
97
98                GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
99
100                ctx->setRenderTarget(target);
101
102                GrPaint paint;
103                paint.setBlendFunc(kOne_GrBlendCoeff, kISA_GrBlendCoeff);
104                GrMatrix vm;
105                if (i) {
106                    vm.setRotate(90 * SK_Scalar1,
107                                 S * SK_Scalar1,
108                                 S * SK_Scalar1);
109                } else {
110                    vm.reset();
111                }
112                ctx->setMatrix(vm);
113                GrMatrix tm;
114                tm = vm;
115                tm.postIDiv(2*S, 2*S);
116                paint.colorStage(0)->setEffect(SkNEW_ARGS(GrSingleTextureEffect,
117                                                          (texture, tm)))->unref();
118
119                ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
120
121                // now update the lower right of the texture in first pass
122                // or upper right in second pass
123                offset = 0;
124                for (int y = 0; y < S; ++y) {
125                    for (int x = 0; x < S; ++x) {
126                        gTextureData[offset + y * stride + x] =
127                            ((x + y) % 2) ? (i ? green : red) : blue;
128                    }
129                }
130                texture->writePixels(S, (i ? 0 : S), S, S,
131                                     texture->config(), gTextureData,
132                                     4 * stride);
133                ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
134            }
135        }
136    }
137
138private:
139    typedef GM INHERITED;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
144static GM* MyFactory(void*) { return new TexDataGM; }
145static GMRegistry reg(MyFactory);
146
147}
148
149#endif
150