texdata.cpp revision 752e7eb157f8a18c26b88b7b85eecdbd5549d52e
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 "SkColorPriv.h"
16#include "effects/GrPorterDuffXferProcessor.h"
17#include "effects/GrSimpleTextureEffect.h"
18
19namespace skiagm {
20
21static const int S = 200;
22
23class TexDataGM : public GM {
24public:
25    TexDataGM() {
26        this->setBGColor(0xff000000);
27    }
28
29protected:
30    virtual SkString onShortName() SK_OVERRIDE {
31        return SkString("texdata");
32    }
33
34    virtual SkISize onISize() SK_OVERRIDE {
35        return SkISize::Make(2*S, 2*S);
36    }
37
38    virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
39
40    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
41        GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
42        GrContext* ctx = canvas->getGrContext();
43        if (ctx && target) {
44            SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
45            static const int stride = 2 * S;
46            static const SkPMColor gray  = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
47            static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
48            static const SkPMColor red   = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
49            static const SkPMColor blue  = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
50            static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
51            static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
52            for (int i = 0; i < 2; ++i) {
53                int offset = 0;
54                // fill upper-left
55                for (int y = 0; y < S; ++y) {
56                    for (int x = 0; x < S; ++x) {
57                        gTextureData[offset + y * stride + x] = gray;
58                    }
59                }
60                // fill upper-right
61                offset = S;
62                for (int y = 0; y < S; ++y) {
63                    for (int x = 0; x < S; ++x) {
64                        gTextureData[offset + y * stride + x] = white;
65                    }
66                }
67                // fill lower left
68                offset = S * stride;
69                for (int y = 0; y < S; ++y) {
70                    for (int x = 0; x < S; ++x) {
71                        gTextureData[offset + y * stride + x] = black;
72                    }
73                }
74                // fill lower right
75                offset = S * stride + S;
76                for (int y = 0; y < S; ++y) {
77                    for (int x = 0; x < S; ++x) {
78                        gTextureData[offset + y * stride + x] = gray;
79                    }
80                }
81
82                GrSurfaceDesc desc;
83                // use RT flag bit because in GL it makes the texture be bottom-up
84                desc.fFlags     = i ? kRenderTarget_GrSurfaceFlag :
85                                      kNone_GrSurfaceFlags;
86                desc.fConfig    = kSkia8888_GrPixelConfig;
87                desc.fWidth     = 2 * S;
88                desc.fHeight    = 2 * S;
89                GrTexture* texture =
90                    ctx->createUncachedTexture(desc, gTextureData.get(), 0);
91
92                if (!texture) {
93                    return;
94                }
95                SkAutoTUnref<GrTexture> au(texture);
96
97                GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S));
98
99                ctx->setRenderTarget(target);
100
101                GrPaint paint;
102                paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
103
104                SkMatrix 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                SkMatrix tm;
113                tm = vm;
114                tm.postIDiv(2*S, 2*S);
115                paint.addColorTextureProcessor(texture, tm);
116
117                ctx->drawRect(paint, vm, SkRect::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.get(),
130                                     4 * stride);
131                ctx->drawRect(paint, vm, SkRect::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#endif
148