blob: 977947b0c67e979fd70f9265876c6feaaef669ff [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00007
8// This test only works with the GPU backend.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000010#include "gm.h"
bsalomon@google.coma68937c2012-08-03 15:00:52 +000011
12#if SK_SUPPORT_GPU
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000013#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "GrDrawContext.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
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000019static const int S = 200;
20
halcanary2a243382015-09-09 08:16:41 -070021DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
robertphillips175dd9b2016-04-28 14:32:04 -070022 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
23 if (!drawContext) {
24 skiagm::GM::DrawGpuOnlyMessage(canvas);
25 return;
26 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000027
robertphillips175dd9b2016-04-28 14:32:04 -070028 GrContext* context = canvas->getGrContext();
29 if (!context) {
30 return;
31 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000032
robertphillips175dd9b2016-04-28 14:32:04 -070033 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
34 static const int stride = 2 * S;
35 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
36 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
37 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
38 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
39 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
40 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
41 for (int i = 0; i < 2; ++i) {
42 int offset = 0;
43 // fill upper-left
44 for (int y = 0; y < S; ++y) {
45 for (int x = 0; x < S; ++x) {
46 gTextureData[offset + y * stride + x] = gray;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000047 }
48 }
robertphillips175dd9b2016-04-28 14:32:04 -070049 // fill upper-right
50 offset = S;
51 for (int y = 0; y < S; ++y) {
52 for (int x = 0; x < S; ++x) {
53 gTextureData[offset + y * stride + x] = white;
54 }
55 }
56 // fill lower left
57 offset = S * stride;
58 for (int y = 0; y < S; ++y) {
59 for (int x = 0; x < S; ++x) {
60 gTextureData[offset + y * stride + x] = black;
61 }
62 }
63 // fill lower right
64 offset = S * stride + S;
65 for (int y = 0; y < S; ++y) {
66 for (int x = 0; x < S; ++x) {
67 gTextureData[offset + y * stride + x] = gray;
68 }
69 }
70
71 GrSurfaceDesc desc;
72 // use RT flag bit because in GL it makes the texture be bottom-up
73 desc.fFlags = i ? kRenderTarget_GrSurfaceFlag :
74 kNone_GrSurfaceFlags;
75 desc.fConfig = kSkia8888_GrPixelConfig;
76 desc.fWidth = 2 * S;
77 desc.fHeight = 2 * S;
78 GrTexture* texture = context->textureProvider()->createTexture(
79 desc, SkBudgeted::kNo, gTextureData.get(), 0);
80
81 if (!texture) {
82 return;
83 }
84 SkAutoTUnref<GrTexture> au(texture);
85
86 // setup new clip
87 GrClip clip(SkRect::MakeWH(2*S, 2*S));
88
89 GrPaint paint;
90 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
91
92 SkMatrix vm;
93 if (i) {
94 vm.setRotate(90 * SK_Scalar1,
95 S * SK_Scalar1,
96 S * SK_Scalar1);
97 } else {
98 vm.reset();
99 }
100 SkMatrix tm;
101 tm = vm;
102 tm.postIDiv(2*S, 2*S);
103 paint.addColorTextureProcessor(texture, tm);
104
105 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
106
107 // now update the lower right of the texture in first pass
108 // or upper right in second pass
109 offset = 0;
110 for (int y = 0; y < S; ++y) {
111 for (int x = 0; x < S; ++x) {
112 gTextureData[offset + y * stride + x] =
113 ((x + y) % 2) ? (i ? green : red) : blue;
114 }
115 }
116 texture->writePixels(S, (i ? 0 : S), S, S,
117 texture->config(), gTextureData.get(),
118 4 * stride);
119 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
120 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000121}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000122#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700123