texdata.cpp revision a68937c950847cea2eb7064ef5b63feb82ed3a71
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
9cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com// This test only works with the GPU backend.
10cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
11d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com#include "gm.h"
12a68937c950847cea2eb7064ef5b63feb82ed3a71bsalomon@google.com
13a68937c950847cea2eb7064ef5b63feb82ed3a71bsalomon@google.com#if SK_SUPPORT_GPU
14d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com#include "GrContext.h"
151c31f633232df572f89a3bc1b0fee3e46d22cb5bbsalomon@google.com#include "effects/GrSingleTextureEffect.h"
16d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com#include "SkColorPriv.h"
17d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com#include "SkDevice.h"
18d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
19d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comnamespace skiagm {
20d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
21d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comextern GrContext* GetGr();
22d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
23d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comstatic const int S = 200;
24d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
25d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comclass TexDataGM : public GM {
26d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.compublic:
2748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    TexDataGM() {
2848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        this->setBGColor(0xff000000);
2948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    }
30d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
31d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comprotected:
32d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    virtual SkString onShortName() {
33d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        return SkString("texdata");
34d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    }
35d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
36d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    virtual SkISize onISize() {
37d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        return make_isize(2*S, 2*S);
38d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    }
39d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
40d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    virtual void onDraw(SkCanvas* canvas) {
41d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        SkDevice* device = canvas->getDevice();
42d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        GrRenderTarget* target = (GrRenderTarget*) device->accessRenderTarget();
43d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        GrContext* ctx = GetGr();
44d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        if (ctx && target) {
45d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            SkPMColor gTextureData[(2 * S) * (2 * S)];
46d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const int stride = 2 * S;
47d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor gray  = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
48d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
49d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor red   = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
50d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor blue  = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
51d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
52d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
53d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            for (int i = 0; i < 2; ++i) {
54d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                int offset = 0;
55d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // fill upper-left
56d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                for (int y = 0; y < S; ++y) {
57d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    for (int x = 0; x < S; ++x) {
58d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                        gTextureData[offset + y * stride + x] = gray;
59d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    }
60d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
61d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // fill upper-right
62d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                offset = S;
63d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                for (int y = 0; y < S; ++y) {
64d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    for (int x = 0; x < S; ++x) {
65d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                        gTextureData[offset + y * stride + x] = white;
66d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    }
67d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
68d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // fill lower left
69d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                offset = S * stride;
70d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                for (int y = 0; y < S; ++y) {
71d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    for (int x = 0; x < S; ++x) {
72d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                        gTextureData[offset + y * stride + x] = black;
73d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    }
74d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
75d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // fill lower right
76d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                offset = S * stride + S;
77d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                for (int y = 0; y < S; ++y) {
78d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    for (int x = 0; x < S; ++x) {
79d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                        gTextureData[offset + y * stride + x] = gray;
80d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    }
81d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
82d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
83d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrTextureDesc desc;
84d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // use RT flag bit because in GL it makes the texture be bottom-up
85d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                desc.fFlags     = i ? kRenderTarget_GrTextureFlagBit :
86d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                                      kNone_GrTextureFlags;
875bc34f04fe70cdde702ac3bff1fea0ccb275d4a5bsalomon@google.com                desc.fConfig    = kSkia8888_PM_GrPixelConfig;
88d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                desc.fWidth     = 2 * S;
89d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                desc.fHeight    = 2 * S;
90d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrTexture* texture =
91d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    ctx->createUncachedTexture(desc, gTextureData, 0);
92d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
93d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                if (!texture) {
94d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    return;
95d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
96d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrAutoUnref au(texture);
97d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
9856c79b10377e358b8092d9c6ab3e1aacf2cd60e5robertphillips@google.com                GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
993e11c0bd92fbd12f59080c3f9450201d6105db83robertphillips@google.com
100d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                ctx->setRenderTarget(target);
101d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
102d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrPaint paint;
103d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                paint.reset();
104d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                paint.fColor = 0xffffffff;
10547059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com                paint.fSrcBlendCoeff = kOne_GrBlendCoeff;
10647059542e7aa153926377456a6c611e55c8e428cbsalomon@google.com                paint.fDstBlendCoeff = kISA_GrBlendCoeff;
107d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrMatrix vm;
108d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                if (i) {
1099d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com                    vm.setRotate(90 * SK_Scalar1,
1109d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com                                 S * SK_Scalar1,
1119d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com                                 S * SK_Scalar1);
112d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                } else {
113d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    vm.reset();
114d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
115d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                ctx->setMatrix(vm);
116d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                GrMatrix tm;
117d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                tm = vm;
118aa814fe4a148b05f9160a5f77cb4e630406ea62dbsalomon@google.com                GrMatrix* sampleMat = paint.textureSampler(0)->matrix();
119aa814fe4a148b05f9160a5f77cb4e630406ea62dbsalomon@google.com                *sampleMat = vm;
120aa814fe4a148b05f9160a5f77cb4e630406ea62dbsalomon@google.com                sampleMat->postIDiv(2*S, 2*S);
1211c31f633232df572f89a3bc1b0fee3e46d22cb5bbsalomon@google.com                paint.textureSampler(0)->setCustomStage(
1221c31f633232df572f89a3bc1b0fee3e46d22cb5bbsalomon@google.com                    SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
1231c31f633232df572f89a3bc1b0fee3e46d22cb5bbsalomon@google.com
124d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
125d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
126d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
127d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // now update the lower right of the texture in first pass
128d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                // or upper right in second pass
129d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                offset = 0;
130d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                for (int y = 0; y < S; ++y) {
131d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    for (int x = 0; x < S; ++x) {
132d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                        gTextureData[offset + y * stride + x] =
133d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                            ((x + y) % 2) ? (i ? green : red) : blue;
134d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                    }
135d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                }
1366f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                texture->writePixels(S, (i ? 0 : S), S, S,
1376f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                     texture->config(), gTextureData,
1386f3795105b2b458079e53a721c1735c9518f6bb5bsalomon@google.com                                     4 * stride);
139d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com                ctx->drawRect(paint, GrRect::MakeWH(2*S, 2*S));
140d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com            }
141d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com        }
142d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    }
143d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
144d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comprivate:
145d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com    typedef GM INHERITED;
146d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com};
147d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
148d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com//////////////////////////////////////////////////////////////////////////////
149d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
150d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comstatic GM* MyFactory(void*) { return new TexDataGM; }
151d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.comstatic GMRegistry reg(MyFactory);
152d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
153d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com}
154d9f826c0dbc38f3608797a02948adf9bc99d5d50bsalomon@google.com
155cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
156