blob: 0c23749e02ed9a3306dc852525bbd66a63ef3116 [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"
csmartdalton02fa32c2016-08-19 13:29:27 -070015#include "GrFixedClip.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) {
robertphillips175dd9b2016-04-28 14:32:04 -070023 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
24 if (!drawContext) {
25 skiagm::GM::DrawGpuOnlyMessage(canvas);
26 return;
27 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000028
robertphillips175dd9b2016-04-28 14:32:04 -070029 GrContext* context = canvas->getGrContext();
30 if (!context) {
31 return;
32 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000033
robertphillips175dd9b2016-04-28 14:32:04 -070034 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
35 static const int stride = 2 * S;
36 static const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
37 static const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
38 static const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
39 static const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
40 static const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
41 static const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
42 for (int i = 0; i < 2; ++i) {
43 int offset = 0;
44 // fill upper-left
45 for (int y = 0; y < S; ++y) {
46 for (int x = 0; x < S; ++x) {
47 gTextureData[offset + y * stride + x] = gray;
bsalomon@google.comd9f826c2011-07-18 15:25:04 +000048 }
49 }
robertphillips175dd9b2016-04-28 14:32:04 -070050 // fill upper-right
51 offset = S;
52 for (int y = 0; y < S; ++y) {
53 for (int x = 0; x < S; ++x) {
54 gTextureData[offset + y * stride + x] = white;
55 }
56 }
57 // fill lower left
58 offset = S * stride;
59 for (int y = 0; y < S; ++y) {
60 for (int x = 0; x < S; ++x) {
61 gTextureData[offset + y * stride + x] = black;
62 }
63 }
64 // fill lower right
65 offset = S * stride + S;
66 for (int y = 0; y < S; ++y) {
67 for (int x = 0; x < S; ++x) {
68 gTextureData[offset + y * stride + x] = gray;
69 }
70 }
71
72 GrSurfaceDesc desc;
robertphillips677da9d2016-05-11 05:15:55 -070073 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
robertphillips175dd9b2016-04-28 14:32:04 -070074 desc.fConfig = kSkia8888_GrPixelConfig;
75 desc.fWidth = 2 * S;
76 desc.fHeight = 2 * S;
77 GrTexture* texture = context->textureProvider()->createTexture(
78 desc, SkBudgeted::kNo, gTextureData.get(), 0);
79
80 if (!texture) {
81 return;
82 }
83 SkAutoTUnref<GrTexture> au(texture);
84
85 // setup new clip
cdalton846c0512016-05-13 10:25:00 -070086 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
robertphillips175dd9b2016-04-28 14:32:04 -070087
88 GrPaint paint;
89 paint.setPorterDuffXPFactory(SkXfermode::kSrcOver_Mode);
90
91 SkMatrix vm;
92 if (i) {
93 vm.setRotate(90 * SK_Scalar1,
94 S * SK_Scalar1,
95 S * SK_Scalar1);
96 } else {
97 vm.reset();
98 }
99 SkMatrix tm;
100 tm = vm;
101 tm.postIDiv(2*S, 2*S);
brianosman54f30c12016-07-18 10:53:52 -0700102 paint.addColorTextureProcessor(texture, nullptr, tm);
robertphillips175dd9b2016-04-28 14:32:04 -0700103
104 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
105
106 // now update the lower right of the texture in first pass
107 // or upper right in second pass
108 offset = 0;
109 for (int y = 0; y < S; ++y) {
110 for (int x = 0; x < S; ++x) {
111 gTextureData[offset + y * stride + x] =
112 ((x + y) % 2) ? (i ? green : red) : blue;
113 }
114 }
115 texture->writePixels(S, (i ? 0 : S), S, S,
116 texture->config(), gTextureData.get(),
117 4 * stride);
118 drawContext->drawRect(clip, paint, vm, SkRect::MakeWH(2*S, 2*S));
119 }
bsalomon@google.comd9f826c2011-07-18 15:25:04 +0000120}
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000121#endif
robertphillips175dd9b2016-04-28 14:32:04 -0700122