blob: 6db730bba7cae4a97dc32b2b2117788a48703410 [file] [log] [blame]
Robert Phillips3500b772017-01-27 10:11:42 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/TestUtils.h"
Robert Phillips3500b772017-01-27 10:11:42 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/encode/SkPngEncoder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/utils/SkBase64.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040012#include "src/core/SkUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrContextPriv.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040014#include "src/gpu/GrDrawingManager.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040015#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040016#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/SkGr.h"
Robert Phillips3500b772017-01-27 10:11:42 -050021
Brian Salomon28a8f282019-10-24 20:07:39 -040022void TestReadPixels(skiatest::Reporter* reporter,
23 GrSurfaceContext* srcContext,
24 uint32_t expectedPixelValues[],
25 const char* testName) {
Robert Phillips3500b772017-01-27 10:11:42 -050026 int pixelCnt = srcContext->width() * srcContext->height();
27 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
28 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
29
30 SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
31 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040032 bool read = srcContext->readPixels(ii, pixels.get(), 0, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050033 if (!read) {
34 ERRORF(reporter, "%s: Error reading from texture.", testName);
35 }
36
37 for (int i = 0; i < pixelCnt; ++i) {
38 if (pixels.get()[i] != expectedPixelValues[i]) {
39 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
40 testName, i, expectedPixelValues[i], pixels.get()[i]);
41 break;
42 }
43 }
44}
45
Brian Salomon28a8f282019-10-24 20:07:39 -040046void TestWritePixels(skiatest::Reporter* reporter,
47 GrSurfaceContext* dstContext,
48 bool expectedToWork,
49 const char* testName) {
Robert Phillips3500b772017-01-27 10:11:42 -050050 int pixelCnt = dstContext->width() * dstContext->height();
51 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
52 for (int y = 0; y < dstContext->width(); ++y) {
53 for (int x = 0; x < dstContext->height(); ++x) {
54 pixels.get()[y * dstContext->width() + x] =
Brian Osman4408b1c2018-10-29 14:11:04 -040055 SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y));
Robert Phillips3500b772017-01-27 10:11:42 -050056 }
57 }
58
59 SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
60 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040061 bool write = dstContext->writePixels(ii, pixels.get(), 0, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050062 if (!write) {
63 if (expectedToWork) {
64 ERRORF(reporter, "%s: Error writing to texture.", testName);
65 }
66 return;
67 }
68
69 if (write && !expectedToWork) {
70 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
71 return;
72 }
73
Brian Salomon28a8f282019-10-24 20:07:39 -040074 TestReadPixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050075}
76
Brian Salomon28a8f282019-10-24 20:07:39 -040077void TestCopyFromSurface(skiatest::Reporter* reporter,
78 GrContext* context,
79 GrSurfaceProxy* proxy,
Greg Daniel40903af2020-01-30 14:55:05 -050080 GrSurfaceOrigin origin,
Brian Salomon28a8f282019-10-24 20:07:39 -040081 GrColorType colorType,
82 uint32_t expectedPixelValues[],
83 const char* testName) {
Brian Salomonc5243782020-04-02 12:50:34 -040084 auto copy = GrSurfaceProxy::Copy(context, proxy, origin, GrMipMapped::kNo, SkBackingFit::kExact,
85 SkBudgeted::kYes);
86 SkASSERT(copy && copy->asTextureProxy());
87 auto swizzle = context->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType);
88 GrSurfaceProxyView view(std::move(copy), origin, swizzle);
Greg Daniel3912a4b2020-01-14 09:56:04 -050089 auto dstContext = GrSurfaceContext::Make(context, std::move(view), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050090 kPremul_SkAlphaType, nullptr);
Brian Salomonbf6b9792019-08-21 09:38:10 -040091 SkASSERT(dstContext);
Robert Phillips3500b772017-01-27 10:11:42 -050092
Brian Salomon28a8f282019-10-24 20:07:39 -040093 TestReadPixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050094}
Timothy Liang760dbc42018-07-17 13:28:20 -040095
Brian Salomon28a8f282019-10-24 20:07:39 -040096void FillPixelData(int width, int height, GrColor* data) {
Timothy Liang760dbc42018-07-17 13:28:20 -040097 for (int j = 0; j < height; ++j) {
98 for (int i = 0; i < width; ++i) {
99 unsigned int red = (unsigned int)(256.f * (i / (float)width));
100 unsigned int green = (unsigned int)(256.f * (j / (float)height));
101 data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8),
102 0xff, 0xff);
103 }
104 }
105}
106
Brian Salomon28a8f282019-10-24 20:07:39 -0400107bool CreateBackendTexture(GrContext* context,
108 GrBackendTexture* backendTex,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400109 int width, int height,
110 SkColorType colorType,
111 const SkColor4f& color,
112 GrMipMapped mipMapped,
113 GrRenderable renderable,
114 GrProtected isProtected) {
115 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
116 return CreateBackendTexture(context, backendTex, info, color, mipMapped, renderable,
117 isProtected);
118}
119
120bool CreateBackendTexture(GrContext* context,
121 GrBackendTexture* backendTex,
Brian Salomon28a8f282019-10-24 20:07:39 -0400122 const SkImageInfo& ii,
123 const SkColor4f& color,
124 GrMipMapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400125 GrRenderable renderable,
126 GrProtected isProtected) {
127 bool finishedBECreate = false;
128 auto markFinished = [](void* context) {
129 *(bool*)context = true;
130 };
131
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400132 *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
Greg Danielc1ad77c2020-05-06 11:40:03 -0400133 color, mipMapped, renderable, isProtected,
134 markFinished, &finishedBECreate);
135 if (backendTex->isValid()) {
Greg Danielb5776552020-06-19 20:21:08 -0400136 context->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400137 while (!finishedBECreate) {
138 context->checkAsyncWorkCompletion();
139 }
140 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400141 return backendTex->isValid();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400142}
143
Brian Salomon28a8f282019-10-24 20:07:39 -0400144void DeleteBackendTexture(GrContext* context, const GrBackendTexture& backendTex) {
Greg Danielce9f0162020-06-30 13:42:46 -0400145 context->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400146 context->submit(true);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400147 context->deleteBackendTexture(backendTex);
Robert Phillipsee5fd132019-05-07 13:29:22 -0400148}
149
Brian Salomon28a8f282019-10-24 20:07:39 -0400150bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer,
151 const GrColor* dstBuffer,
152 int width, int height) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400153 const GrColor* srcPtr = srcBuffer;
154 const GrColor* dstPtr = dstBuffer;
Timothy Liang760dbc42018-07-17 13:28:20 -0400155 for (int j = 0; j < height; ++j) {
156 for (int i = 0; i < width; ++i) {
157 if (srcPtr[i] != dstPtr[i]) {
158 return false;
159 }
160 }
161 srcPtr += width;
162 dstPtr += width;
163 }
164 return true;
165}
Michael Ludwige8e10752018-10-01 12:42:53 -0400166
Brian Salomon28a8f282019-10-24 20:07:39 -0400167bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
Michael Ludwige8e10752018-10-01 12:42:53 -0400168 SkPixmap pm;
169 if (!bitmap.peekPixels(&pm)) {
170 dst->set("peekPixels failed");
171 return false;
172 }
173
174 // We're going to embed this PNG in a data URI, so make it as small as possible
175 SkPngEncoder::Options options;
176 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
177 options.fZLibLevel = 9;
178
179 SkDynamicMemoryWStream wStream;
180 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
181 dst->set("SkPngEncoder::Encode failed");
182 return false;
183 }
184
185 sk_sp<SkData> pngData = wStream.detachAsData();
186 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
187
188 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
189 // Infra says these can be pretty big, as long as we're only outputting them on failure.
190 static const size_t kMaxBase64Length = 1024 * 1024;
191 if (len > kMaxBase64Length) {
192 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
193 return false;
194 }
195
196 dst->resize(len);
197 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
198 dst->prepend("data:image/png;base64,");
199 return true;
200}
Mike Reed0c607082019-04-11 17:10:17 -0400201
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400202using AccessPixelFn = const float*(const char* floatBuffer, int x, int y);
203
204bool compare_pixels(int width, int height,
205 const char* floatA, std::function<AccessPixelFn>& atA,
206 const char* floatB, std::function<AccessPixelFn>& atB,
207 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
208
209 for (int y = 0; y < height; ++y) {
210 for (int x = 0; x < width; ++x) {
211 const float* rgbaA = atA(floatA, x, y);
212 const float* rgbaB = atB(floatB, x, y);
213 float diffs[4];
214 bool bad = false;
215 for (int i = 0; i < 4; ++i) {
216 diffs[i] = rgbaB[i] - rgbaA[i];
217 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
218 bad = true;
219 }
220 }
221 if (bad) {
222 error(x, y, diffs);
223 return false;
224 }
225 }
226 }
227 return true;
228}
229
Brian Salomon28a8f282019-10-24 20:07:39 -0400230bool ComparePixels(const GrImageInfo& infoA, const char* a, size_t rowBytesA,
231 const GrImageInfo& infoB, const char* b, size_t rowBytesB,
232 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400233 if (infoA.width() != infoB.width() || infoA.height() != infoB.height()) {
234 static constexpr float kDummyDiffs[4] = {};
235 error(-1, -1, kDummyDiffs);
236 return false;
237 }
238
239 SkAlphaType floatAlphaType = infoA.alphaType();
240 // If one is premul and the other is unpremul we do the comparison in premul space.
241 if ((infoA.alphaType() == kPremul_SkAlphaType ||
242 infoB.alphaType() == kPremul_SkAlphaType) &&
243 (infoA.alphaType() == kUnpremul_SkAlphaType ||
244 infoB.alphaType() == kUnpremul_SkAlphaType)) {
245 floatAlphaType = kPremul_SkAlphaType;
246 }
247 sk_sp<SkColorSpace> floatCS;
248 if (SkColorSpace::Equals(infoA.colorSpace(), infoB.colorSpace())) {
249 floatCS = infoA.refColorSpace();
250 } else {
251 floatCS = SkColorSpace::MakeSRGBLinear();
252 }
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400253 GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS),
Brian Salomon85aeccf2019-07-15 12:30:44 -0400254 infoA.width(), infoA.height());
255
256 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
257 size_t floatRowBytes = floatBpp * infoA.width();
258 std::unique_ptr<char[]> floatA(new char[floatRowBytes * infoA.height()]);
259 std::unique_ptr<char[]> floatB(new char[floatRowBytes * infoA.height()]);
260 SkAssertResult(GrConvertPixels(floatInfo, floatA.get(), floatRowBytes, infoA, a, rowBytesA));
261 SkAssertResult(GrConvertPixels(floatInfo, floatB.get(), floatRowBytes, infoB, b, rowBytesB));
262
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400263 auto at = std::function<AccessPixelFn>(
264 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
265 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
266 });
267
268 return compare_pixels(infoA.width(), infoA.height(),
269 floatA.get(), at, floatB.get(), at,
270 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400271}
272
Brian Salomon28a8f282019-10-24 20:07:39 -0400273bool ComparePixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4],
274 std::function<ComparePixmapsErrorReporter>& error) {
275 return ComparePixels(a.info(), static_cast<const char*>(a.addr()), a.rowBytes(),
276 b.info(), static_cast<const char*>(b.addr()), b.rowBytes(),
277 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400278}
279
Brian Salomon28a8f282019-10-24 20:07:39 -0400280bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap,
281 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400282
283 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
284
285 std::unique_ptr<char[]> floatA(new char[floatBpp]);
286 // First convert 'col' to be compatible with 'pixmap'
287 {
288 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400289 GrImageInfo srcInfo(GrColorType::kRGBA_F32, kUnpremul_SkAlphaType, std::move(srcCS), 1, 1);
290 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), 1, 1);
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400291
292 SkAssertResult(GrConvertPixels(dstInfo, floatA.get(), floatBpp, srcInfo,
293 col.vec(), floatBpp));
294 }
295
296 size_t floatRowBytes = floatBpp * pixmap.width();
297 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
298 // Then convert 'pixmap' to RGBA_F32
299 {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400300 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(),
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400301 pixmap.width(), pixmap.height());
302
303 SkAssertResult(GrConvertPixels(dstInfo, floatB.get(), floatRowBytes, pixmap.info(),
304 pixmap.addr(), pixmap.rowBytes()));
305 }
306
307 auto atA = std::function<AccessPixelFn>(
308 [](const char* floatBuffer, int /* x */, int /* y */) {
309 return reinterpret_cast<const float*>(floatBuffer);
310 });
311
312 auto atB = std::function<AccessPixelFn>(
313 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
314 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
315 });
316
317 return compare_pixels(pixmap.width(), pixmap.height(), floatA.get(), atA, floatB.get(), atB,
318 tolRGBA, error);
319}
320
Brian Salomon28a8f282019-10-24 20:07:39 -0400321void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -0500322 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -0400323 int32_t expectedProxyRefs,
324 int32_t expectedBackingRefs) {
Brian Salomon557e8122019-10-24 10:37:08 -0400325 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
326
327 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
328 !proxy->refCntGreaterThan(expectedProxyRefs));
329 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
330}
331
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500332#include "src/utils/SkCharToGlyphCache.h"
Mike Reed0c607082019-04-11 17:10:17 -0400333
334static SkGlyphID hash_to_glyph(uint32_t value) {
335 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
336}
337
338namespace {
339class UnicharGen {
340 SkUnichar fU;
341 const int fStep;
342public:
343 UnicharGen(int step) : fU(0), fStep(step) {}
344
345 SkUnichar next() {
346 fU += fStep;
347 return fU;
348 }
349};
350}
351
352DEF_TEST(chartoglyph_cache, reporter) {
353 SkCharToGlyphCache cache;
354 const int step = 3;
355
356 UnicharGen gen(step);
357 for (int i = 0; i < 500; ++i) {
358 SkUnichar c = gen.next();
359 SkGlyphID glyph = hash_to_glyph(c);
360
361 int index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400362 if (index >= 0) {
363 index = cache.findGlyphIndex(c);
364 }
Mike Reed0c607082019-04-11 17:10:17 -0400365 REPORTER_ASSERT(reporter, index < 0);
366 cache.insertCharAndGlyph(~index, c, glyph);
367
368 UnicharGen gen2(step);
369 for (int j = 0; j <= i; ++j) {
370 c = gen2.next();
371 glyph = hash_to_glyph(c);
372 index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400373 if ((unsigned)index != glyph) {
374 index = cache.findGlyphIndex(c);
375 }
Mike Reed0c607082019-04-11 17:10:17 -0400376 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
377 }
378 }
379}