blob: 72735a160fa816d7485843ce5caee8d983c86af7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00008
9// This test only works with the GPU backend.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000011#include "gm.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000012
13#if SK_SUPPORT_GPU
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000014#include "GrContext.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000015#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000016#include "SkColorPriv.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000017
18namespace skiagm {
19
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000020static const int S = 200;
21
22class TexDataGM : public GM {
23public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000024 TexDataGM() {
25 this->setBGColor(0xff000000);
26 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000027
28protected:
29 virtual SkString onShortName() {
30 return SkString("texdata");
31 }
32
33 virtual SkISize onISize() {
34 return make_isize(2*S, 2*S);
35 }
36
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000037 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
38
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000039 virtual void onDraw(SkCanvas* canvas) {
reed@google.com9c135db2014-03-12 18:28:35 +000040 GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000041 GrContext* ctx = canvas->getGrContext();
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000042 if (ctx && target) {
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000043 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000044 static const int stride = 2 * S;
45 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
46 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
47 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
48 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
49 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
50 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
51 for (int i = 0; i < 2; ++i) {
52 int offset = 0;
53 // fill upper-left
54 for (int y = 0; y < S; ++y) {
55 for (int x = 0; x < S; ++x) {
56 gTextureData[offset + y * stride + x] = gray;
57 }
58 }
59 // fill upper-right
60 offset = S;
61 for (int y = 0; y < S; ++y) {
62 for (int x = 0; x < S; ++x) {
63 gTextureData[offset + y * stride + x] = white;
64 }
65 }
66 // fill lower left
67 offset = S * stride;
68 for (int y = 0; y < S; ++y) {
69 for (int x = 0; x < S; ++x) {
70 gTextureData[offset + y * stride + x] = black;
71 }
72 }
73 // fill lower right
74 offset = S * stride + S;
75 for (int y = 0; y < S; ++y) {
76 for (int x = 0; x < S; ++x) {
77 gTextureData[offset + y * stride + x] = gray;
78 }
79 }
80
81 GrTextureDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000082 // use RT flag bit because in GL it makes the texture be bottom-up
83 desc.fFlags = i ? kRenderTarget_GrTextureFlagBit :
84 kNone_GrTextureFlags;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000085 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000086 desc.fWidth = 2 * S;
87 desc.fHeight = 2 * S;
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 GrTexture* texture =
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000089 ctx->createUncachedTexture(desc, gTextureData.get(), 0);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000090
91 if (!texture) {
92 return;
93 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000094 SkAutoUnref au(texture);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000095
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000096 GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000097
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000098 ctx->setRenderTarget(target);
99
100 GrPaint paint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000101 paint.setBlendFunc(kOne_GrBlendCoeff, kISA_GrBlendCoeff);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000102 SkMatrix vm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000103 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000104 vm.setRotate(90 * SK_Scalar1,
105 S * SK_Scalar1,
106 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000107 } else {
108 vm.reset();
109 }
110 ctx->setMatrix(vm);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000111 SkMatrix tm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000112 tm = vm;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000113 tm.postIDiv(2*S, 2*S);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000114 paint.addColorTextureEffect(texture, tm);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000115
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000116 ctx->drawRect(paint, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000117
118 // now update the lower right of the texture in first pass
119 // or upper right in second pass
120 offset = 0;
121 for (int y = 0; y < S; ++y) {
122 for (int x = 0; x < S; ++x) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123 gTextureData[offset + y * stride + x] =
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000124 ((x + y) % 2) ? (i ? green : red) : blue;
125 }
126 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000127 texture->writePixels(S, (i ? 0 : S), S, S,
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +0000128 texture->config(), gTextureData.get(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000129 4 * stride);
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000130 ctx->drawRect(paint, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000131 }
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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000146#endif