1 /*
2  * Copyright 2015 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 #include "tests/Test.h"
9 
10 #ifdef SK_GL
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPixmap.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkSamplingOptions.h"
21 #include "include/core/SkTypes.h"
22 #include "include/gpu/GpuTypes.h"
23 #include "include/gpu/GrBackendSurface.h"
24 #include "include/gpu/GrDirectContext.h"
25 #include "include/gpu/GrTypes.h"
26 #include "include/private/SkColorData.h"
27 #include "include/private/base/SkTemplates.h"
28 #include "include/private/gpu/ganesh/GrTypesPriv.h"
29 #include "src/gpu/Swizzle.h"
30 #include "src/gpu/ganesh/GrCaps.h"
31 #include "src/gpu/ganesh/GrColor.h"
32 #include "src/gpu/ganesh/GrDirectContextPriv.h"
33 #include "src/gpu/ganesh/GrFragmentProcessor.h"
34 #include "src/gpu/ganesh/GrImageInfo.h"
35 #include "src/gpu/ganesh/GrPixmap.h"
36 #include "src/gpu/ganesh/GrProxyProvider.h"
37 #include "src/gpu/ganesh/GrSamplerState.h"
38 #include "src/gpu/ganesh/GrSurfaceProxy.h"
39 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
40 #include "src/gpu/ganesh/GrTexture.h"
41 #include "src/gpu/ganesh/GrTextureProxy.h"
42 #include "src/gpu/ganesh/SkGr.h"
43 #include "src/gpu/ganesh/SurfaceContext.h"
44 #include "src/gpu/ganesh/SurfaceFillContext.h"
45 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
46 #include "src/gpu/ganesh/gl/GrGLDefines_impl.h"
47 #include "tests/CtsEnforcement.h"
48 #include "tests/TestUtils.h"
49 #include "tools/gpu/ProxyUtils.h"
50 
51 #include <cstdint>
52 #include <initializer_list>
53 #include <memory>
54 #include <utility>
55 
56 using namespace skia_private;
57 
58 struct GrContextOptions;
59 
60 // skbug.com/5932
test_basic_draw_as_src(skiatest::Reporter * reporter,GrDirectContext * dContext,GrSurfaceProxyView rectView,GrColorType colorType,SkAlphaType alphaType,uint32_t expectedPixelValues[])61 static void test_basic_draw_as_src(skiatest::Reporter* reporter, GrDirectContext* dContext,
62                                    GrSurfaceProxyView rectView, GrColorType colorType,
63                                    SkAlphaType alphaType, uint32_t expectedPixelValues[]) {
64     auto sfc = dContext->priv().makeSFC(
65             {colorType, kPremul_SkAlphaType, nullptr, rectView.dimensions()}, /*label=*/{});
66     for (auto filter : {GrSamplerState::Filter::kNearest, GrSamplerState::Filter::kLinear}) {
67         for (auto mm : {GrSamplerState::MipmapMode::kNone, GrSamplerState::MipmapMode::kLinear}) {
68             sfc->clear(SkPMColor4f::FromBytes_RGBA(0xDDCCBBAA));
69             auto fp = GrTextureEffect::Make(rectView, alphaType, SkMatrix::I(), filter, mm);
70             sfc->fillWithFP(std::move(fp));
71             TestReadPixels(reporter, dContext, sfc.get(), expectedPixelValues,
72                            "RectangleTexture-basic-draw");
73         }
74     }
75 }
76 
test_clear(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::v1::SurfaceContext * rectContext)77 static void test_clear(skiatest::Reporter* reporter, GrDirectContext* dContext,
78                        skgpu::v1::SurfaceContext* rectContext) {
79     if (auto sfc = rectContext->asFillContext()) {
80         // Clear the whole thing.
81         GrColor color0 = GrColorPackRGBA(0xA, 0xB, 0xC, 0xD);
82         sfc->clear(SkPMColor4f::FromBytes_RGBA(color0));
83 
84         int w = sfc->width();
85         int h = sfc->height();
86         int pixelCnt = w * h;
87         AutoTMalloc<uint32_t> expectedPixels(pixelCnt);
88 
89         // The clear color is a GrColor, our readback is to kRGBA_8888, which may be different.
90         uint32_t expectedColor0 = 0;
91         uint8_t* expectedBytes0 = reinterpret_cast<uint8_t*>(&expectedColor0);
92         expectedBytes0[0] = GrColorUnpackR(color0);
93         expectedBytes0[1] = GrColorUnpackG(color0);
94         expectedBytes0[2] = GrColorUnpackB(color0);
95         expectedBytes0[3] = GrColorUnpackA(color0);
96         for (int i = 0; i < sfc->width() * sfc->height(); ++i) {
97             expectedPixels.get()[i] = expectedColor0;
98         }
99 
100         // Clear the the top to a different color.
101         GrColor color1 = GrColorPackRGBA(0x1, 0x2, 0x3, 0x4);
102         SkIRect rect = SkIRect::MakeWH(w, h/2);
103         sfc->clear(rect, SkPMColor4f::FromBytes_RGBA(color1));
104 
105         uint32_t expectedColor1 = 0;
106         uint8_t* expectedBytes1 = reinterpret_cast<uint8_t*>(&expectedColor1);
107         expectedBytes1[0] = GrColorUnpackR(color1);
108         expectedBytes1[1] = GrColorUnpackG(color1);
109         expectedBytes1[2] = GrColorUnpackB(color1);
110         expectedBytes1[3] = GrColorUnpackA(color1);
111 
112         for (int y = 0; y < h/2; ++y) {
113             for (int x = 0; x < w; ++x) {
114                 expectedPixels.get()[y * h + x] = expectedColor1;
115             }
116         }
117 
118         TestReadPixels(reporter, dContext, sfc, expectedPixels.get(), "RectangleTexture-clear");
119     }
120 }
121 
test_copy_to_surface(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::v1::SurfaceContext * dstContext,const char * testName)122 static void test_copy_to_surface(skiatest::Reporter* reporter,
123                                  GrDirectContext* dContext,
124                                  skgpu::v1::SurfaceContext* dstContext,
125                                  const char* testName) {
126 
127     int pixelCnt = dstContext->width() * dstContext->height();
128     AutoTMalloc<uint32_t> pixels(pixelCnt);
129     for (int y = 0; y < dstContext->width(); ++y) {
130         for (int x = 0; x < dstContext->height(); ++x) {
131             pixels.get()[y * dstContext->width() + x] =
132                 SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y));
133         }
134     }
135 
136     for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
137         auto origin = dstContext->origin();
138         GrImageInfo info(GrColorType::kRGBA_8888,
139                          kPremul_SkAlphaType,
140                          nullptr,
141                          dstContext->dimensions());
142         GrCPixmap pixmap(info, pixels.get(), dstContext->width()*sizeof(uint32_t));
143         auto srcView = sk_gpu_test::MakeTextureProxyViewFromData(dContext,
144                                                                  renderable,
145                                                                  origin,
146                                                                  pixmap);
147         // If this assert ever fails we can add a fallback to do copy as draw, but until then we can
148         // be more restrictive.
149         SkAssertResult(dstContext->testCopy(srcView.refProxy()));
150         TestReadPixels(reporter, dContext, dstContext, pixels.get(), testName);
151     }
152 }
153 
DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)154 DEF_GANESH_TEST_FOR_GL_RENDERING_CONTEXTS(RectangleTexture,
155                                           reporter,
156                                           ctxInfo,
157                                           CtsEnforcement::kApiLevel_T) {
158     auto dContext = ctxInfo.directContext();
159 
160     GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
161     static const int kWidth = 16;
162     static const int kHeight = 16;
163 
164     uint32_t pixels[kWidth * kHeight];
165     for (int y = 0; y < kHeight; ++y) {
166         for (int x = 0; x < kWidth; ++x) {
167             pixels[y * kWidth + x] = y * kWidth + x;
168         }
169     }
170     auto ii = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
171     SkPixmap pm(ii, pixels, sizeof(uint32_t)*kWidth);
172 
173     for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin }) {
174 
175         auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
176         GrBackendTexture rectangleTex = dContext->createBackendTexture(
177                 kWidth, kHeight, format, GrMipmapped::kNo, GrRenderable::kYes);
178         if (!rectangleTex.isValid()) {
179             continue;
180         }
181 
182         if (!dContext->updateBackendTexture(rectangleTex, &pm, 1, origin, nullptr, nullptr)) {
183             continue;
184         }
185 
186         GrColor refPixels[kWidth * kHeight];
187         for (int y = 0; y < kHeight; ++y) {
188             for (int x = 0; x < kWidth; ++x) {
189                 refPixels[y * kWidth + x] = pixels[y * kWidth + x];
190             }
191         }
192 
193         sk_sp<GrTextureProxy> rectProxy = proxyProvider->wrapBackendTexture(
194                 rectangleTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
195 
196         if (!rectProxy) {
197             dContext->deleteBackendTexture(rectangleTex);
198             continue;
199         }
200 
201         SkASSERT(rectProxy->mipmapped() == GrMipmapped::kNo);
202         SkASSERT(rectProxy->peekTexture()->mipmapped() == GrMipmapped::kNo);
203 
204         SkASSERT(rectProxy->textureType() == GrTextureType::kRectangle);
205         SkASSERT(rectProxy->peekTexture()->textureType() == GrTextureType::kRectangle);
206         SkASSERT(rectProxy->hasRestrictedSampling());
207         SkASSERT(rectProxy->peekTexture()->hasRestrictedSampling());
208 
209         GrImageInfo grII = ii;
210         skgpu::Swizzle swizzle = dContext->priv().caps()->getReadSwizzle(
211                 rectangleTex.getBackendFormat(), grII.colorType());
212         GrSurfaceProxyView view(rectProxy, origin, swizzle);
213 
214         test_basic_draw_as_src(reporter, dContext, view, grII.colorType(), kPremul_SkAlphaType,
215                                refPixels);
216 
217         // Test copy to both a texture and RT
218         TestCopyFromSurface(reporter, dContext, rectProxy, origin, grII.colorType(), refPixels,
219                             "RectangleTexture-copy-from");
220 
221         auto rectContext = dContext->priv().makeSC(std::move(view), grII.colorInfo());
222         SkASSERT(rectContext);
223 
224         TestReadPixels(reporter, dContext, rectContext.get(), refPixels, "RectangleTexture-read");
225 
226         test_copy_to_surface(reporter, dContext, rectContext.get(), "RectangleTexture-copy-to");
227 
228         TestWritePixels(reporter, dContext, rectContext.get(), true, "RectangleTexture-write");
229 
230         test_clear(reporter, dContext, rectContext.get());
231 
232         dContext->deleteBackendTexture(rectangleTex);
233     }
234 }
235 #endif
236