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