blob: e7c15b24982f01499e712f332d9f584ba0cdf3d6 [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"
robertphillipsea461502015-05-26 11:38:03 -070015#include "GrDrawContext.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080016#include "SkColorPriv.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "effects/GrPorterDuffXferProcessor.h"
18#include "effects/GrSimpleTextureEffect.h"
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000019
20namespace skiagm {
21
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000022static const int S = 200;
23
24class TexDataGM : public GM {
25public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000026 TexDataGM() {
27 this->setBGColor(0xff000000);
28 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000029
30protected:
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000032 return SkString("texdata");
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070036 return SkISize::Make(2*S, 2*S);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000037 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 void onDraw(SkCanvas* canvas) override {
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();
robertphillipsea461502015-05-26 11:38:03 -070042 GrDrawContext* drawContext = ctx ? ctx->drawContext() : NULL;
43 if (drawContext && 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;
bsalomond309e7a2015-04-30 14:18:54 -070089 GrTexture* texture = ctx->textureProvider()->createTexture(
90 desc, false, 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
joshualitt570d2f82015-02-25 13:19:48 -080097 // setup new clip
98 GrClip clip(SkRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000099
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000100 GrPaint paint;
egdaniel95131432014-12-09 11:15:43 -0800101 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
102
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000103 SkMatrix vm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000104 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +0000105 vm.setRotate(90 * SK_Scalar1,
106 S * SK_Scalar1,
107 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000108 } else {
109 vm.reset();
110 }
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);
joshualittb0a8a372014-09-23 09:50:21 -0700114 paint.addColorTextureProcessor(texture, tm);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000115
robertphillipsea461502015-05-26 11:38:03 -0700116 drawContext->drawRect(target, clip, paint, vm, 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);
robertphillipsea461502015-05-26 11:38:03 -0700130 drawContext->drawRect(target, clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000131 }
bsalomonb62da802015-01-31 07:51:14 -0800132 } else {
133 this->drawGpuOnlyMessage(canvas);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000134 }
135 }
136
137private:
138 typedef GM INHERITED;
139};
140
141//////////////////////////////////////////////////////////////////////////////
142
143static GM* MyFactory(void*) { return new TexDataGM; }
144static GMRegistry reg(MyFactory);
145
146}
147
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000148#endif