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    SkString onShortName() override {
31        return SkString("texdata");
32    }
33
34    SkISize onISize() override {
35        return SkISize::Make(2*S, 2*S);
36    }
37
38    void onDraw(SkCanvas* canvas) override {
39        GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
40        GrContext* ctx = canvas->getGrContext();
41        if (ctx && target) {
42            SkAutoTArray<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                GrSurfaceDesc desc;
81                // use RT flag bit because in GL it makes the texture be bottom-up
82                desc.fFlags     = i ? kRenderTarget_GrSurfaceFlag :
83                                      kNone_GrSurfaceFlags;
84                desc.fConfig    = kSkia8888_GrPixelConfig;
85                desc.fWidth     = 2 * S;
86                desc.fHeight    = 2 * S;
87                GrTexture* texture = ctx->textureProvider()->createTexture(
88                    desc, false, gTextureData.get(), 0);
89
90                if (!texture) {
91                    return;
92                }
93                SkAutoTUnref<GrTexture> au(texture);
94
95                // setup new clip
96                GrClip clip(SkRect::MakeWH(2*S, 2*S));
97
98                GrPaint paint;
99                paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
100
101                SkMatrix 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                SkMatrix tm;
110                tm = vm;
111                tm.postIDiv(2*S, 2*S);
112                paint.addColorTextureProcessor(texture, tm);
113
114                ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
115
116                // now update the lower right of the texture in first pass
117                // or upper right in second pass
118                offset = 0;
119                for (int y = 0; y < S; ++y) {
120                    for (int x = 0; x < S; ++x) {
121                        gTextureData[offset + y * stride + x] =
122                            ((x + y) % 2) ? (i ? green : red) : blue;
123                    }
124                }
125                texture->writePixels(S, (i ? 0 : S), S, S,
126                                     texture->config(), gTextureData.get(),
127                                     4 * stride);
128                ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
129            }
130        } else {
131            this->drawGpuOnlyMessage(canvas);
132        }
133    }
134
135private:
136    typedef GM INHERITED;
137};
138
139//////////////////////////////////////////////////////////////////////////////
140
141static GM* MyFactory(void*) { return new TexDataGM; }
142static GMRegistry reg(MyFactory);
143
144}
145
146#endif
147