blob: 01eedec633be3299898bfab5fa3e6b9426b539a9 [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;
robertphillips677da9d2016-05-11 05:15:55 -070072 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
robertphillips175dd9b2016-04-28 14:32:04 -070073 desc.fConfig = kSkia8888_GrPixelConfig;
74 desc.fWidth = 2 * S;
75 desc.fHeight = 2 * S;
76 GrTexture* texture = context->textureProvider()->createTexture(
77 desc, SkBudgeted::kNo, gTextureData.get(), 0);
78
79 if (!texture) {
80 return;
81 }
82 SkAutoTUnref<GrTexture> au(texture);
83
84 // setup new clip
cdalton846c0512016-05-13 10:25:00 -070085 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
robertphillips175dd9b2016-04-28 14:32:04 -070086
87 GrPaint paint;
88 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
89
90 SkMatrix vm;
91 if (i) {
92 vm.setRotate(90 * SK_Scalar1,
93 S * SK_Scalar1,
94 S * SK_Scalar1);
95 } else {
96 vm.reset();
97 }
98 SkMatrix tm;
99 tm = vm;
100 tm.postIDiv(2*S, 2*S);
101 paint.addColorTextureProcessor(texture, tm);
102
103 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
104
105 // now update the lower right of the texture in first pass
106 // or upper right in second pass
107 offset = 0;
108 for (int y = 0; y < S; ++y) {
109 for (int x = 0; x < S; ++x) {
110 gTextureData[offset + y * stride + x] =
111 ((x + y) % 2) ? (i ? green : red) : blue;
112 }
113 }
114 texture->writePixels(S, (i ? 0 : S), S, S,
115 texture->config(), gTextureData.get(),
116 4 * stride);
117 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
118 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000119}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000120#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700121