1 /*
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 */
7
8 // This test only works with the GPU backend.
9
10 #include "gm.h"
11
12 #if SK_SUPPORT_GPU
13 #include "GrContext.h"
14 #include "GrContextPriv.h"
15 #include "GrProxyProvider.h"
16 #include "GrRenderTargetContext.h"
17 #include "GrTextureContext.h"
18 #include "GrFixedClip.h"
19 #include "SkColorPriv.h"
20 #include "SkGr.h"
21 #include "effects/GrPorterDuffXferProcessor.h"
22 #include "effects/GrSimpleTextureEffect.h"
23
24 constexpr int S = 200;
25 constexpr int kStride = 2 * S;
26
27 // Fill in the pixels:
28 // gray | white
29 // -------------
30 // black | gray
fill_in_pixels(SkPMColor * pixels)31 static void fill_in_pixels(SkPMColor* pixels) {
32 const SkPMColor gray = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
33 const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
34 const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
35
36 int offset = 0;
37
38 // fill upper-left
39 for (int y = 0; y < S; ++y) {
40 for (int x = 0; x < S; ++x) {
41 pixels[offset + y * kStride + x] = gray;
42 }
43 }
44 // fill upper-right
45 offset = S;
46 for (int y = 0; y < S; ++y) {
47 for (int x = 0; x < S; ++x) {
48 pixels[offset + y * kStride + x] = white;
49 }
50 }
51 // fill lower left
52 offset = S * kStride;
53 for (int y = 0; y < S; ++y) {
54 for (int x = 0; x < S; ++x) {
55 pixels[offset + y * kStride + x] = black;
56 }
57 }
58 // fill lower right
59 offset = S * kStride + S;
60 for (int y = 0; y < S; ++y) {
61 for (int x = 0; x < S; ++x) {
62 pixels[offset + y * kStride + x] = gray;
63 }
64 }
65 }
66
67 DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
68 GrRenderTargetContext* renderTargetContext =
69 canvas->internal_private_accessTopLayerRenderTargetContext();
70 if (!renderTargetContext) {
71 skiagm::GM::DrawGpuOnlyMessage(canvas);
72 return;
73 }
74
75 GrContext* context = canvas->getGrContext();
76 if (!context) {
77 return;
78 }
79
80 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
81 const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
82
83 SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
84 const SkPMColor red = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
85 const SkPMColor blue = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
86 const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
87 for (int i = 0; i < 2; ++i) {
88 fill_in_pixels(gTextureData.get());
89
90 GrSurfaceDesc desc;
91 desc.fOrigin = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
92 desc.fWidth = 2 * S;
93 desc.fHeight = 2 * S;
94 desc.fConfig = SkImageInfo2GrPixelConfig(ii, *context->caps());
95
96 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo,
97 gTextureData.get(), 0);
98 if (!proxy) {
99 return;
100 }
101
102 sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext(
103 std::move(proxy));
104
105 if (!tContext) {
106 return;
107 }
108
109 // setup new clip
110 GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
111
112 GrPaint paint;
113 paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
114
115 SkMatrix vm;
116 if (i) {
117 vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1);
118 } else {
119 vm.reset();
120 }
121 paint.addColorTextureProcessor(tContext->asTextureProxyRef(), vm);
122
123 renderTargetContext->drawRect(clip, GrPaint::Clone(paint), GrAA::kNo, vm,
124 SkRect::MakeWH(2 * S, 2 * S));
125
126 // now update the lower right of the texture in first pass
127 // or upper right in second pass
128 for (int y = 0; y < S; ++y) {
129 for (int x = 0; x < S; ++x) {
130 gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue;
131 }
132 }
133
134 if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) {
135 continue;
136 }
137
138 renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
139 SkRect::MakeWH(2 * S, 2 * S));
140 }
141 }
142 #endif
143
144