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
24constexpr int S = 200;
25constexpr int kStride = 2 * S;
26
27// Fill in the pixels:
28//   gray  | white
29//   -------------
30//   black | gray
31static 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
67DEF_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        SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
96
97        sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kNo,
98                                                                        gTextureData.get(), 0);
99        if (!proxy) {
100            return;
101        }
102
103        sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext(
104                                                                                  std::move(proxy));
105
106        if (!tContext) {
107            return;
108        }
109
110        // setup new clip
111        GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
112
113        GrPaint paint;
114        paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
115
116        SkMatrix vm;
117        if (i) {
118            vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1);
119        } else {
120            vm.reset();
121        }
122        paint.addColorTextureProcessor(tContext->asTextureProxyRef(), vm);
123
124        renderTargetContext->drawRect(clip, GrPaint::Clone(paint), GrAA::kNo, vm,
125                                      SkRect::MakeWH(2 * S, 2 * S));
126
127        // now update the lower right of the texture in first pass
128        // or upper right in second pass
129        for (int y = 0; y < S; ++y) {
130            for (int x = 0; x < S; ++x) {
131                gTextureData[y * kStride + x] = ((x + y) % 2) ? (i ? green : red) : blue;
132            }
133        }
134
135        if (!tContext->writePixels(ii, gTextureData.get(), 4 * kStride, S, i ? 0 : S)) {
136            continue;
137        }
138
139        renderTargetContext->drawRect(clip, std::move(paint), GrAA::kNo, vm,
140                                      SkRect::MakeWH(2 * S, 2 * S));
141    }
142}
143#endif
144
145