blob: 99087b56adddb020c006d73be1f2f9ede4f139b7 [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"
egdaniel8d95ffa2014-12-08 13:26:43 -080015#include "SkColorPriv.h"
egdaniel95131432014-12-09 11:15:43 -080016#include "effects/GrPorterDuffXferProcessor.h"
17#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000018
19namespace skiagm {
20
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000021static const int S = 200;
22
23class TexDataGM : public GM {
24public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000025 TexDataGM() {
26 this->setBGColor(0xff000000);
27 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000028
29protected:
mtklein72c9faa2015-01-09 10:06:39 -080030 SkString onShortName() SK_OVERRIDE {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000031 return SkString("texdata");
32 }
33
mtklein72c9faa2015-01-09 10:06:39 -080034 SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070035 return SkISize::Make(2*S, 2*S);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000036 }
37
mtklein72c9faa2015-01-09 10:06:39 -080038 uint32_t onGetFlags() const SK_OVERRIDE { return kGPUOnly_Flag; }
commit-bot@chromium.org1d5bbb22013-10-14 14:15:28 +000039
mtklein72c9faa2015-01-09 10:06:39 -080040 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
reed@google.com9c135db2014-03-12 18:28:35 +000041 GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000042 GrContext* ctx = canvas->getGrContext();
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000043 if (ctx && target) {
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000044 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000045 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
bsalomonf2703d82014-10-28 14:33:06 -070082 GrSurfaceDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000083 // use RT flag bit because in GL it makes the texture be bottom-up
bsalomonf2703d82014-10-28 14:33:06 -070084 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag :
85 kNone_GrSurfaceFlags;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000086 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000087 desc.fWidth = 2 * S;
88 desc.fHeight = 2 * S;
rmistry@google.comd6176b02012-08-23 18:14:13 +000089 GrTexture* texture =
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000090 ctx->createUncachedTexture(desc, gTextureData.get(), 0);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000091
92 if (!texture) {
93 return;
94 }
bsalomondcabb052014-07-21 14:24:01 -070095 SkAutoTUnref<GrTexture> au(texture);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000096
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000097 GrContext::AutoClip acs(ctx, SkRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000098
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000099 ctx->setRenderTarget(target);
100
101 GrPaint paint;
egdaniel95131432014-12-09 11:15:43 -0800102 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
103
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000104 SkMatrix vm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000105 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000106 vm.setRotate(90 * SK_Scalar1,
107 S * SK_Scalar1,
108 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000109 } else {
110 vm.reset();
111 }
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000112 SkMatrix tm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000113 tm = vm;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000114 tm.postIDiv(2*S, 2*S);
joshualittb0a8a372014-09-23 09:50:21 -0700115 paint.addColorTextureProcessor(texture, tm);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000116
joshualitt5531d512014-12-17 15:50:11 -0800117 ctx->drawRect(paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000118
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) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000124 gTextureData[offset + y * stride + x] =
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000125 ((x + y) % 2) ? (i ? green : red) : blue;
126 }
127 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000128 texture->writePixels(S, (i ? 0 : S), S, S,
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +0000129 texture->config(), gTextureData.get(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000130 4 * stride);
joshualitt5531d512014-12-17 15:50:11 -0800131 ctx->drawRect(paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000132 }
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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000147#endif