blob: c89ce734f7584e3d12d015310d66768f53b26855 [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
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000020static const int S = 200;
21
halcanary2a243382015-09-09 08:16:41 -070022DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
reed@google.com9c135db2014-03-12 18:28:35 +000023 GrRenderTarget* target = canvas->internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000024 GrContext* ctx = canvas->getGrContext();
robertphillips2e1e51f2015-10-15 08:01:48 -070025 SkAutoTUnref<GrDrawContext> drawContext(ctx ? ctx->drawContext(target) : nullptr);
robertphillipsea461502015-05-26 11:38:03 -070026 if (drawContext && target) {
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +000027 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000028 static const int stride = 2 * S;
29 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
30 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
31 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
32 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
33 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
34 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
35 for (int i = 0; i < 2; ++i) {
36 int offset = 0;
37 // fill upper-left
38 for (int y = 0; y < S; ++y) {
39 for (int x = 0; x < S; ++x) {
40 gTextureData[offset + y * stride + x] = gray;
41 }
42 }
43 // fill upper-right
44 offset = S;
45 for (int y = 0; y < S; ++y) {
46 for (int x = 0; x < S; ++x) {
47 gTextureData[offset + y * stride + x] = white;
48 }
49 }
50 // fill lower left
51 offset = S * stride;
52 for (int y = 0; y < S; ++y) {
53 for (int x = 0; x < S; ++x) {
54 gTextureData[offset + y * stride + x] = black;
55 }
56 }
57 // fill lower right
58 offset = S * stride + S;
59 for (int y = 0; y < S; ++y) {
60 for (int x = 0; x < S; ++x) {
61 gTextureData[offset + y * stride + x] = gray;
62 }
63 }
64
bsalomonf2703d82014-10-28 14:33:06 -070065 GrSurfaceDesc desc;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000066 // use RT flag bit because in GL it makes the texture be bottom-up
bsalomonf2703d82014-10-28 14:33:06 -070067 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag :
68 kNone_GrSurfaceFlags;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000069 desc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000070 desc.fWidth = 2 * S;
71 desc.fHeight = 2 * S;
bsalomond309e7a2015-04-30 14:18:54 -070072 GrTexture* texture = ctx->textureProvider()->createTexture(
73 desc, false, gTextureData.get(), 0);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000074
75 if (!texture) {
76 return;
77 }
bsalomondcabb052014-07-21 14:24:01 -070078 SkAutoTUnref<GrTexture> au(texture);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000079
joshualitt570d2f82015-02-25 13:19:48 -080080 // setup new clip
81 GrClip clip(SkRect::MakeWH(2*S, 2*S));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +000082
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000083 GrPaint paint;
egdaniel95131432014-12-09 11:15:43 -080084 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
85
bsalomon@google.comb9086a02012-11-01 18:02:54 +000086 SkMatrix vm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000087 if (i) {
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +000088 vm.setRotate(90 * SK_Scalar1,
89 S * SK_Scalar1,
90 S * SK_Scalar1);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000091 } else {
92 vm.reset();
93 }
bsalomon@google.comb9086a02012-11-01 18:02:54 +000094 SkMatrix tm;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000095 tm = vm;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +000096 tm.postIDiv(2*S, 2*S);
joshualittb0a8a372014-09-23 09:50:21 -070097 paint.addColorTextureProcessor(texture, tm);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000098
robertphillips2e1e51f2015-10-15 08:01:48 -070099 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000100
101 // now update the lower right of the texture in first pass
102 // or upper right in second pass
103 offset = 0;
104 for (int y = 0; y < S; ++y) {
105 for (int x = 0; x < S; ++x) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106 gTextureData[offset + y * stride + x] =
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000107 ((x + y) % 2) ? (i ? green : red) : blue;
108 }
109 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000110 texture->writePixels(S, (i ? 0 : S), S, S,
commit-bot@chromium.orgbeede902013-10-16 11:50:50 +0000111 texture->config(), gTextureData.get(),
bsalomon@google.com6f379512011-11-16 20:36:03 +0000112 4 * stride);
robertphillips2e1e51f2015-10-15 08:01:48 -0700113 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000114 }
bsalomonb62da802015-01-31 07:51:14 -0800115 } else {
halcanary2a243382015-09-09 08:16:41 -0700116 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000117 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000118}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000119#endif