blob: dff2944bdbeae900417f89a7c599c6bcf8987d7b [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:
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000031 return SkString("texdata");
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() 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
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
reed@google.com9c135db2014-03-12 18:28:35 +000039 GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040 GrContext* ctx = canvas->getGrContext();
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000041 if (ctx && target) {
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000042 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000043 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
bsalomonf2703d82014-10-28 14:33:06 -070080 GrSurfaceDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000081 // use RT flag bit because in GL it makes the texture be bottom-up
bsalomonf2703d82014-10-28 14:33:06 -070082 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag :
83 kNone_GrSurfaceFlags;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000084 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000085 desc.fWidth = 2 * S;
86 desc.fHeight = 2 * S;
bsalomond309e7a2015-04-30 14:18:54 -070087 GrTexture* texture = ctx->textureProvider()->createTexture(
88 desc, false, gTextureData.get(), 0);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000089
90 if (!texture) {
91 return;
92 }
bsalomondcabb052014-07-21 14:24:01 -070093 SkAutoTUnref<GrTexture> au(texture);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000094
joshualitt570d2f82015-02-25 13:19:48 -080095 // setup new clip
96 GrClip clip(SkRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000097
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000098 GrPaint paint;
egdaniel95131432014-12-09 11:15:43 -080099 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
100
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000101 SkMatrix vm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000102 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000103 vm.setRotate(90 * SK_Scalar1,
104 S * SK_Scalar1,
105 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000106 } else {
107 vm.reset();
108 }
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000109 SkMatrix tm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000110 tm = vm;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000111 tm.postIDiv(2*S, 2*S);
joshualittb0a8a372014-09-23 09:50:21 -0700112 paint.addColorTextureProcessor(texture, tm);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000113
joshualitt570d2f82015-02-25 13:19:48 -0800114 ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000115
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) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121 gTextureData[offset + y * stride + x] =
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000122 ((x + y) % 2) ? (i ? green : red) : blue;
123 }
124 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000125 texture->writePixels(S, (i ? 0 : S), S, S,
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +0000126 texture->config(), gTextureData.get(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000127 4 * stride);
joshualitt570d2f82015-02-25 13:19:48 -0800128 ctx->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000129 }
bsalomonb62da802015-01-31 07:51:14 -0800130 } else {
131 this->drawGpuOnlyMessage(canvas);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000132 }
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