texdata.cpp revision 296b1ccf9b8e9c8b945645efcbaa9c71c7135f58
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 "GrRenderTargetContext.h"
16#include "GrTextureContext.h"
17#include "GrFixedClip.h"
18#include "SkColorPriv.h"
19#include "SkGr.h"
20#include "effects/GrPorterDuffXferProcessor.h"
21#include "effects/GrSimpleTextureEffect.h"
22
23constexpr int S = 200;
24constexpr int kStride = 2 * S;
25
26// Fill in the pixels:
27//   gray  | white
28//   -------------
29//   black | gray
30static void fill_in_pixels(SkPMColor* pixels) {
31    const SkPMColor gray  = SkPackARGB32(0x40, 0x40, 0x40, 0x40);
32    const SkPMColor white = SkPackARGB32(0xff, 0xff, 0xff, 0xff);
33    const SkPMColor black = SkPackARGB32(0x00, 0x00, 0x00, 0x00);
34
35    int offset = 0;
36
37    // fill upper-left
38    for (int y = 0; y < S; ++y) {
39        for (int x = 0; x < S; ++x) {
40            pixels[offset + y * kStride + x] = gray;
41        }
42    }
43    // fill upper-right
44    offset = S;
45    for (int y = 0; y < S; ++y) {
46        for (int x = 0; x < S; ++x) {
47            pixels[offset + y * kStride + x] = white;
48        }
49    }
50    // fill lower left
51    offset = S * kStride;
52    for (int y = 0; y < S; ++y) {
53        for (int x = 0; x < S; ++x) {
54            pixels[offset + y * kStride + x] = black;
55        }
56    }
57    // fill lower right
58    offset = S * kStride + S;
59    for (int y = 0; y < S; ++y) {
60        for (int x = 0; x < S; ++x) {
61            pixels[offset + y * kStride + x] = gray;
62        }
63    }
64}
65
66DEF_SIMPLE_GM_BG(texdata, canvas, 2 * S, 2 * S, SK_ColorBLACK) {
67    GrRenderTargetContext* renderTargetContext =
68        canvas->internal_private_accessTopLayerRenderTargetContext();
69    if (!renderTargetContext) {
70        skiagm::GM::DrawGpuOnlyMessage(canvas);
71        return;
72    }
73
74    GrContext* context = canvas->getGrContext();
75    if (!context) {
76        return;
77    }
78
79    const SkImageInfo ii = SkImageInfo::Make(S, S, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
80
81    SkAutoTArray<SkPMColor> gTextureData((2 * S) * (2 * S));
82    const SkPMColor red   = SkPackARGB32(0x80, 0x80, 0x00, 0x00);
83    const SkPMColor blue  = SkPackARGB32(0x80, 0x00, 0x00, 0x80);
84    const SkPMColor green = SkPackARGB32(0x80, 0x00, 0x80, 0x00);
85    for (int i = 0; i < 2; ++i) {
86        fill_in_pixels(gTextureData.get());
87
88        GrSurfaceDesc desc;
89        desc.fOrigin    = i ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
90        desc.fWidth     = 2 * S;
91        desc.fHeight    = 2 * S;
92        desc.fConfig    = SkImageInfo2GrPixelConfig(ii, *context->caps());
93
94        sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
95                                                                   desc, SkBudgeted::kNo,
96                                                                   gTextureData.get(), 0);
97        if (!proxy) {
98            return;
99        }
100
101        sk_sp<GrSurfaceContext> tContext = context->contextPriv().makeWrappedSurfaceContext(
102                std::move(proxy), nullptr);
103
104        if (!tContext) {
105            return;
106        }
107
108        // setup new clip
109        GrFixedClip clip(SkIRect::MakeWH(2*S, 2*S));
110
111        GrPaint paint;
112        paint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
113
114        SkMatrix vm;
115        if (i) {
116            vm.setRotate(90 * SK_Scalar1, S * SK_Scalar1, S * SK_Scalar1);
117        } else {
118            vm.reset();
119        }
120        paint.addColorTextureProcessor(context->resourceProvider(), tContext->asTextureProxyRef(),
121                                       nullptr, vm);
122
123        renderTargetContext->drawRect(clip, GrPaint(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