1 /*
2 * Copyright 2017 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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkStream.h"
17 #include "include/core/SkString.h"
18 #include "include/encode/SkPngEncoder.h"
19 #include "include/gpu/GrBackendSurface.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/gpu/GrRecordingContext.h"
22 #include "include/private/base/SkTemplates.h"
23 #include "include/private/base/SkTo.h"
24 #include "include/private/gpu/ganesh/GrTypesPriv.h"
25 #include "include/utils/SkBase64.h"
26 #include "src/core/SkAutoPixmapStorage.h"
27 #include "src/gpu/ganesh/GrCaps.h"
28 #include "src/gpu/ganesh/GrDataUtils.h"
29 #include "src/gpu/ganesh/GrDirectContextPriv.h"
30 #include "src/gpu/ganesh/GrImageInfo.h"
31 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
32 #include "src/gpu/ganesh/GrSurfaceProxy.h"
33 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
34 #include "src/gpu/ganesh/SkGr.h"
35 #include "src/gpu/ganesh/SurfaceContext.h"
36 #include "src/utils/SkCharToGlyphCache.h"
37 #include "tests/Test.h"
38 #include "tests/TestUtils.h"
39
40 #include <cmath>
41 #include <cstdlib>
42 #include <utility>
43
TestReadPixels(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::v1::SurfaceContext * srcContext,uint32_t expectedPixelValues[],const char * testName)44 void TestReadPixels(skiatest::Reporter* reporter,
45 GrDirectContext* dContext,
46 skgpu::v1::SurfaceContext* srcContext,
47 uint32_t expectedPixelValues[],
48 const char* testName) {
49 int pixelCnt = srcContext->width() * srcContext->height();
50 SkImageInfo ii = SkImageInfo::Make(srcContext->dimensions(),
51 kRGBA_8888_SkColorType,
52 kPremul_SkAlphaType);
53 SkAutoPixmapStorage pm;
54 pm.alloc(ii);
55 pm.erase(SK_ColorTRANSPARENT);
56
57 bool read = srcContext->readPixels(dContext, pm, {0, 0});
58 if (!read) {
59 ERRORF(reporter, "%s: Error reading from texture.", testName);
60 }
61
62 for (int i = 0; i < pixelCnt; ++i) {
63 if (pm.addr32()[i] != expectedPixelValues[i]) {
64 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
65 testName, i, expectedPixelValues[i], pm.addr32()[i]);
66 break;
67 }
68 }
69 }
70
TestWritePixels(skiatest::Reporter * reporter,GrDirectContext * dContext,skgpu::v1::SurfaceContext * dstContext,bool expectedToWork,const char * testName)71 void TestWritePixels(skiatest::Reporter* reporter,
72 GrDirectContext* dContext,
73 skgpu::v1::SurfaceContext* dstContext,
74 bool expectedToWork,
75 const char* testName) {
76 SkImageInfo ii = SkImageInfo::Make(dstContext->dimensions(),
77 kRGBA_8888_SkColorType,
78 kPremul_SkAlphaType);
79 SkAutoPixmapStorage pm;
80 pm.alloc(ii);
81 for (int y = 0; y < dstContext->height(); ++y) {
82 for (int x = 0; x < dstContext->width(); ++x) {
83 *pm.writable_addr32(x, y) = SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y));
84 }
85 }
86
87 bool write = dstContext->writePixels(dContext, pm, {0, 0});
88 if (!write) {
89 if (expectedToWork) {
90 ERRORF(reporter, "%s: Error writing to texture.", testName);
91 }
92 return;
93 }
94
95 if (write && !expectedToWork) {
96 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
97 return;
98 }
99
100 TestReadPixels(reporter, dContext, dstContext, pm.writable_addr32(0, 0), testName);
101 }
102
TestCopyFromSurface(skiatest::Reporter * reporter,GrDirectContext * dContext,sk_sp<GrSurfaceProxy> proxy,GrSurfaceOrigin origin,GrColorType colorType,uint32_t expectedPixelValues[],const char * testName)103 void TestCopyFromSurface(skiatest::Reporter* reporter,
104 GrDirectContext* dContext,
105 sk_sp<GrSurfaceProxy> proxy,
106 GrSurfaceOrigin origin,
107 GrColorType colorType,
108 uint32_t expectedPixelValues[],
109 const char* testName) {
110 auto copy = GrSurfaceProxy::Copy(dContext,
111 std::move(proxy),
112 origin,
113 GrMipmapped::kNo,
114 SkBackingFit::kExact,
115 skgpu::Budgeted::kYes,
116 /*label=*/"CopyFromSurface_Test");
117 SkASSERT(copy && copy->asTextureProxy());
118 auto swizzle = dContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType);
119 GrSurfaceProxyView view(std::move(copy), origin, swizzle);
120 auto dstContext = dContext->priv().makeSC(std::move(view),
121 {colorType, kPremul_SkAlphaType, nullptr});
122 SkASSERT(dstContext);
123
124 TestReadPixels(reporter, dContext, dstContext.get(), expectedPixelValues, testName);
125 }
126
BipmapToBase64DataURI(const SkBitmap & bitmap,SkString * dst)127 bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
128 SkPixmap pm;
129 if (!bitmap.peekPixels(&pm)) {
130 dst->set("peekPixels failed");
131 return false;
132 }
133
134 // We're going to embed this PNG in a data URI, so make it as small as possible
135 SkPngEncoder::Options options;
136 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
137 options.fZLibLevel = 9;
138
139 SkDynamicMemoryWStream wStream;
140 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
141 dst->set("SkPngEncoder::Encode failed");
142 return false;
143 }
144
145 sk_sp<SkData> pngData = wStream.detachAsData();
146 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
147
148 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
149 // Infra says these can be pretty big, as long as we're only outputting them on failure.
150 static const size_t kMaxBase64Length = 1024 * 1024;
151 if (len > kMaxBase64Length) {
152 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
153 return false;
154 }
155
156 dst->resize(len);
157 SkBase64::Encode(pngData->data(), pngData->size(), dst->data());
158 dst->prepend("data:image/png;base64,");
159 return true;
160 }
161
compare_colors(int x,int y,const float rgbaA[],const float rgbaB[],const float tolRGBA[4],std::function<ComparePixmapsErrorReporter> & error)162 static bool compare_colors(int x, int y,
163 const float rgbaA[],
164 const float rgbaB[],
165 const float tolRGBA[4],
166 std::function<ComparePixmapsErrorReporter>& error) {
167 float diffs[4];
168 bool bad = false;
169 for (int i = 0; i < 4; ++i) {
170 diffs[i] = rgbaB[i] - rgbaA[i];
171 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
172 bad = true;
173 }
174 }
175 if (bad) {
176 error(x, y, diffs);
177 return false;
178 }
179 return true;
180 }
181
ComparePixels(const GrCPixmap & a,const GrCPixmap & b,const float tolRGBA[4],std::function<ComparePixmapsErrorReporter> & error)182 bool ComparePixels(const GrCPixmap& a,
183 const GrCPixmap& b,
184 const float tolRGBA[4],
185 std::function<ComparePixmapsErrorReporter>& error) {
186 if (a.dimensions() != b.dimensions()) {
187 static constexpr float kEmptyDiffs[4] = {};
188 error(-1, -1, kEmptyDiffs);
189 return false;
190 }
191
192 SkAlphaType floatAlphaType = a.alphaType();
193 // If one is premul and the other is unpremul we do the comparison in premul space.
194 if ((a.alphaType() == kPremul_SkAlphaType || b.alphaType() == kPremul_SkAlphaType) &&
195 (a.alphaType() == kUnpremul_SkAlphaType || b.alphaType() == kUnpremul_SkAlphaType)) {
196 floatAlphaType = kPremul_SkAlphaType;
197 }
198 sk_sp<SkColorSpace> floatCS;
199 if (SkColorSpace::Equals(a.colorSpace(), b.colorSpace())) {
200 floatCS = a.refColorSpace();
201 } else {
202 floatCS = SkColorSpace::MakeSRGBLinear();
203 }
204 GrImageInfo floatInfo(GrColorType::kRGBA_F32,
205 floatAlphaType,
206 std::move(floatCS),
207 a.dimensions());
208
209 GrPixmap floatA = GrPixmap::Allocate(floatInfo);
210 GrPixmap floatB = GrPixmap::Allocate(floatInfo);
211 SkAssertResult(GrConvertPixels(floatA, a));
212 SkAssertResult(GrConvertPixels(floatB, b));
213
214 SkASSERT(floatA.rowBytes() == floatB.rowBytes());
215 auto at = [rb = floatA.rowBytes()](const void* base, int x, int y) {
216 return SkTAddOffset<const float>(base, y*rb + x*sizeof(float)*4);
217 };
218
219 for (int y = 0; y < floatA.height(); ++y) {
220 for (int x = 0; x < floatA.width(); ++x) {
221 const float* rgbaA = at(floatA.addr(), x, y);
222 const float* rgbaB = at(floatB.addr(), x, y);
223 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
224 return false;
225 }
226 }
227 }
228 return true;
229 }
230
CheckSolidPixels(const SkColor4f & col,const SkPixmap & pixmap,const float tolRGBA[4],std::function<ComparePixmapsErrorReporter> & error)231 bool CheckSolidPixels(const SkColor4f& col,
232 const SkPixmap& pixmap,
233 const float tolRGBA[4],
234 std::function<ComparePixmapsErrorReporter>& error) {
235 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
236
237 // First convert 'col' to be compatible with 'pixmap'
238 GrPixmap colorPixmap;
239 {
240 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
241 GrImageInfo srcInfo(GrColorType::kRGBA_F32,
242 kUnpremul_SkAlphaType,
243 std::move(srcCS),
244 {1, 1});
245 GrCPixmap srcPixmap(srcInfo, col.vec(), floatBpp);
246 GrImageInfo dstInfo =
247 srcInfo.makeAlphaType(pixmap.alphaType()).makeColorSpace(pixmap.refColorSpace());
248 colorPixmap = GrPixmap::Allocate(dstInfo);
249 SkAssertResult(GrConvertPixels(colorPixmap, srcPixmap));
250 }
251
252 size_t floatRowBytes = floatBpp * pixmap.width();
253 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
254 // Then convert 'pixmap' to RGBA_F32
255 GrPixmap f32Pixmap = GrPixmap::Allocate(pixmap.info().makeColorType(kRGBA_F32_SkColorType));
256 SkAssertResult(GrConvertPixels(f32Pixmap, pixmap));
257
258 for (int y = 0; y < f32Pixmap.height(); ++y) {
259 for (int x = 0; x < f32Pixmap.width(); ++x) {
260 auto rgbaA = SkTAddOffset<const float>(f32Pixmap.addr(),
261 f32Pixmap.rowBytes()*y + floatBpp*x);
262 auto rgbaB = static_cast<const float*>(colorPixmap.addr());
263 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
264 return false;
265 }
266 }
267 }
268 return true;
269 }
270
CheckSingleThreadedProxyRefs(skiatest::Reporter * reporter,GrSurfaceProxy * proxy,int32_t expectedProxyRefs,int32_t expectedBackingRefs)271 void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
272 GrSurfaceProxy* proxy,
273 int32_t expectedProxyRefs,
274 int32_t expectedBackingRefs) {
275 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
276
277 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
278 !proxy->refCntGreaterThan(expectedProxyRefs));
279 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
280 }
281
CreateSurfaceContext(GrRecordingContext * rContext,const GrImageInfo & info,SkBackingFit fit,GrSurfaceOrigin origin,GrRenderable renderable,int sampleCount,GrMipmapped mipmapped,GrProtected isProtected,skgpu::Budgeted budgeted)282 std::unique_ptr<skgpu::v1::SurfaceContext> CreateSurfaceContext(GrRecordingContext* rContext,
283 const GrImageInfo& info,
284 SkBackingFit fit,
285 GrSurfaceOrigin origin,
286 GrRenderable renderable,
287 int sampleCount,
288 GrMipmapped mipmapped,
289 GrProtected isProtected,
290 skgpu::Budgeted budgeted) {
291 GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(info.colorType(),
292 renderable);
293 return rContext->priv().makeSC(info,
294 format,
295 /*label=*/{},
296 fit,
297 origin,
298 renderable,
299 sampleCount,
300 mipmapped,
301 isProtected,
302 budgeted);
303 }
304
hash_to_glyph(uint32_t value)305 static SkGlyphID hash_to_glyph(uint32_t value) {
306 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
307 }
308
309 namespace {
310 class UnicharGen {
311 SkUnichar fU;
312 const int fStep;
313 public:
UnicharGen(int step)314 UnicharGen(int step) : fU(0), fStep(step) {}
315
next()316 SkUnichar next() {
317 fU += fStep;
318 return fU;
319 }
320 };
321 } // namespace
322
DEF_TEST(chartoglyph_cache,reporter)323 DEF_TEST(chartoglyph_cache, reporter) {
324 SkCharToGlyphCache cache;
325 const int step = 3;
326
327 UnicharGen gen(step);
328 for (int i = 0; i < 500; ++i) {
329 SkUnichar c = gen.next();
330 SkGlyphID glyph = hash_to_glyph(c);
331
332 int index = cache.findGlyphIndex(c);
333 if (index >= 0) {
334 index = cache.findGlyphIndex(c);
335 }
336 REPORTER_ASSERT(reporter, index < 0);
337 cache.insertCharAndGlyph(~index, c, glyph);
338
339 UnicharGen gen2(step);
340 for (int j = 0; j <= i; ++j) {
341 c = gen2.next();
342 glyph = hash_to_glyph(c);
343 index = cache.findGlyphIndex(c);
344 if ((unsigned)index != glyph) {
345 index = cache.findGlyphIndex(c);
346 }
347 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
348 }
349 }
350 }
351