blob: 1e3dc7132c149ed30025ca43a33db127444dd033 [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"
Robert Phillipseffd13f2020-07-20 15:00:36 -040017#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrSurfaceProxy.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/SkGr.h"
Robert Phillips3500b772017-01-27 10:11:42 -050022
Brian Salomon28a8f282019-10-24 20:07:39 -040023void TestReadPixels(skiatest::Reporter* reporter,
24 GrSurfaceContext* srcContext,
25 uint32_t expectedPixelValues[],
26 const char* testName) {
Robert Phillips3500b772017-01-27 10:11:42 -050027 int pixelCnt = srcContext->width() * srcContext->height();
28 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
29 memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt);
30
31 SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(),
32 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040033 bool read = srcContext->readPixels(ii, pixels.get(), 0, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050034 if (!read) {
35 ERRORF(reporter, "%s: Error reading from texture.", testName);
36 }
37
38 for (int i = 0; i < pixelCnt; ++i) {
39 if (pixels.get()[i] != expectedPixelValues[i]) {
40 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
41 testName, i, expectedPixelValues[i], pixels.get()[i]);
42 break;
43 }
44 }
45}
46
Brian Salomon28a8f282019-10-24 20:07:39 -040047void TestWritePixels(skiatest::Reporter* reporter,
48 GrSurfaceContext* dstContext,
49 bool expectedToWork,
50 const char* testName) {
Robert Phillips3500b772017-01-27 10:11:42 -050051 int pixelCnt = dstContext->width() * dstContext->height();
52 SkAutoTMalloc<uint32_t> pixels(pixelCnt);
53 for (int y = 0; y < dstContext->width(); ++y) {
54 for (int x = 0; x < dstContext->height(); ++x) {
55 pixels.get()[y * dstContext->width() + x] =
Brian Osman4408b1c2018-10-29 14:11:04 -040056 SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y));
Robert Phillips3500b772017-01-27 10:11:42 -050057 }
58 }
59
60 SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(),
61 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Salomon1d435302019-07-01 13:05:28 -040062 bool write = dstContext->writePixels(ii, pixels.get(), 0, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050063 if (!write) {
64 if (expectedToWork) {
65 ERRORF(reporter, "%s: Error writing to texture.", testName);
66 }
67 return;
68 }
69
70 if (write && !expectedToWork) {
71 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
72 return;
73 }
74
Brian Salomon28a8f282019-10-24 20:07:39 -040075 TestReadPixels(reporter, dstContext, pixels.get(), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050076}
77
Brian Salomon28a8f282019-10-24 20:07:39 -040078void TestCopyFromSurface(skiatest::Reporter* reporter,
Robert Phillipseffd13f2020-07-20 15:00:36 -040079 GrRecordingContext* rContext,
Brian Salomon28a8f282019-10-24 20:07:39 -040080 GrSurfaceProxy* proxy,
Greg Daniel40903af2020-01-30 14:55:05 -050081 GrSurfaceOrigin origin,
Brian Salomon28a8f282019-10-24 20:07:39 -040082 GrColorType colorType,
83 uint32_t expectedPixelValues[],
84 const char* testName) {
Brian Salomon7e67dca2020-07-21 09:27:25 -040085 auto copy = GrSurfaceProxy::Copy(rContext, proxy, origin, GrMipmapped::kNo,
Robert Phillipseffd13f2020-07-20 15:00:36 -040086 SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonc5243782020-04-02 12:50:34 -040087 SkASSERT(copy && copy->asTextureProxy());
Robert Phillipseffd13f2020-07-20 15:00:36 -040088 auto swizzle = rContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType);
Brian Salomonc5243782020-04-02 12:50:34 -040089 GrSurfaceProxyView view(std::move(copy), origin, swizzle);
Robert Phillipseffd13f2020-07-20 15:00:36 -040090 auto dstContext = GrSurfaceContext::Make(rContext, std::move(view), colorType,
Greg Danielbfa19c42019-12-19 16:41:40 -050091 kPremul_SkAlphaType, nullptr);
Brian Salomonbf6b9792019-08-21 09:38:10 -040092 SkASSERT(dstContext);
Robert Phillips3500b772017-01-27 10:11:42 -050093
Brian Salomon28a8f282019-10-24 20:07:39 -040094 TestReadPixels(reporter, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -050095}
Timothy Liang760dbc42018-07-17 13:28:20 -040096
Brian Salomon28a8f282019-10-24 20:07:39 -040097void FillPixelData(int width, int height, GrColor* data) {
Timothy Liang760dbc42018-07-17 13:28:20 -040098 for (int j = 0; j < height; ++j) {
99 for (int i = 0; i < width; ++i) {
100 unsigned int red = (unsigned int)(256.f * (i / (float)width));
101 unsigned int green = (unsigned int)(256.f * (j / (float)height));
102 data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8),
103 0xff, 0xff);
104 }
105 }
106}
107
Robert Phillipseffd13f2020-07-20 15:00:36 -0400108bool CreateBackendTexture(GrDirectContext* dContext,
Brian Salomon28a8f282019-10-24 20:07:39 -0400109 GrBackendTexture* backendTex,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400110 int width, int height,
111 SkColorType colorType,
112 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400113 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400114 GrRenderable renderable,
115 GrProtected isProtected) {
116 SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType);
Robert Phillipseffd13f2020-07-20 15:00:36 -0400117 return CreateBackendTexture(dContext, backendTex, info, color, mipMapped, renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400118 isProtected);
119}
120
Robert Phillipseffd13f2020-07-20 15:00:36 -0400121bool CreateBackendTexture(GrDirectContext* dContext,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400122 GrBackendTexture* backendTex,
Brian Salomon28a8f282019-10-24 20:07:39 -0400123 const SkImageInfo& ii,
124 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400125 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400126 GrRenderable renderable,
127 GrProtected isProtected) {
128 bool finishedBECreate = false;
129 auto markFinished = [](void* context) {
130 *(bool*)context = true;
131 };
132
Robert Phillipseffd13f2020-07-20 15:00:36 -0400133 *backendTex = dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
134 color, mipMapped, renderable, isProtected,
135 markFinished, &finishedBECreate);
Greg Danielc1ad77c2020-05-06 11:40:03 -0400136 if (backendTex->isValid()) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400137 dContext->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400138 while (!finishedBECreate) {
Robert Phillipseffd13f2020-07-20 15:00:36 -0400139 dContext->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -0400140 }
141 }
Robert Phillipscb1adb42019-06-10 15:09:34 -0400142 return backendTex->isValid();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400143}
144
Robert Phillipseffd13f2020-07-20 15:00:36 -0400145void DeleteBackendTexture(GrDirectContext* dContext, const GrBackendTexture& backendTex) {
146 dContext->flush();
147 dContext->submit(true);
148 dContext->deleteBackendTexture(backendTex);
Robert Phillipsee5fd132019-05-07 13:29:22 -0400149}
150
Brian Salomon28a8f282019-10-24 20:07:39 -0400151bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer,
152 const GrColor* dstBuffer,
153 int width, int height) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400154 const GrColor* srcPtr = srcBuffer;
155 const GrColor* dstPtr = dstBuffer;
Timothy Liang760dbc42018-07-17 13:28:20 -0400156 for (int j = 0; j < height; ++j) {
157 for (int i = 0; i < width; ++i) {
158 if (srcPtr[i] != dstPtr[i]) {
159 return false;
160 }
161 }
162 srcPtr += width;
163 dstPtr += width;
164 }
165 return true;
166}
Michael Ludwige8e10752018-10-01 12:42:53 -0400167
Brian Salomon28a8f282019-10-24 20:07:39 -0400168bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
Michael Ludwige8e10752018-10-01 12:42:53 -0400169 SkPixmap pm;
170 if (!bitmap.peekPixels(&pm)) {
171 dst->set("peekPixels failed");
172 return false;
173 }
174
175 // We're going to embed this PNG in a data URI, so make it as small as possible
176 SkPngEncoder::Options options;
177 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
178 options.fZLibLevel = 9;
179
180 SkDynamicMemoryWStream wStream;
181 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
182 dst->set("SkPngEncoder::Encode failed");
183 return false;
184 }
185
186 sk_sp<SkData> pngData = wStream.detachAsData();
187 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
188
189 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
190 // Infra says these can be pretty big, as long as we're only outputting them on failure.
191 static const size_t kMaxBase64Length = 1024 * 1024;
192 if (len > kMaxBase64Length) {
193 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
194 return false;
195 }
196
197 dst->resize(len);
198 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
199 dst->prepend("data:image/png;base64,");
200 return true;
201}
Mike Reed0c607082019-04-11 17:10:17 -0400202
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400203using AccessPixelFn = const float*(const char* floatBuffer, int x, int y);
204
205bool compare_pixels(int width, int height,
206 const char* floatA, std::function<AccessPixelFn>& atA,
207 const char* floatB, std::function<AccessPixelFn>& atB,
208 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
209
210 for (int y = 0; y < height; ++y) {
211 for (int x = 0; x < width; ++x) {
212 const float* rgbaA = atA(floatA, x, y);
213 const float* rgbaB = atB(floatB, x, y);
214 float diffs[4];
215 bool bad = false;
216 for (int i = 0; i < 4; ++i) {
217 diffs[i] = rgbaB[i] - rgbaA[i];
218 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
219 bad = true;
220 }
221 }
222 if (bad) {
223 error(x, y, diffs);
224 return false;
225 }
226 }
227 }
228 return true;
229}
230
Brian Salomon28a8f282019-10-24 20:07:39 -0400231bool ComparePixels(const GrImageInfo& infoA, const char* a, size_t rowBytesA,
232 const GrImageInfo& infoB, const char* b, size_t rowBytesB,
233 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400234 if (infoA.width() != infoB.width() || infoA.height() != infoB.height()) {
235 static constexpr float kDummyDiffs[4] = {};
236 error(-1, -1, kDummyDiffs);
237 return false;
238 }
239
240 SkAlphaType floatAlphaType = infoA.alphaType();
241 // If one is premul and the other is unpremul we do the comparison in premul space.
242 if ((infoA.alphaType() == kPremul_SkAlphaType ||
243 infoB.alphaType() == kPremul_SkAlphaType) &&
244 (infoA.alphaType() == kUnpremul_SkAlphaType ||
245 infoB.alphaType() == kUnpremul_SkAlphaType)) {
246 floatAlphaType = kPremul_SkAlphaType;
247 }
248 sk_sp<SkColorSpace> floatCS;
249 if (SkColorSpace::Equals(infoA.colorSpace(), infoB.colorSpace())) {
250 floatCS = infoA.refColorSpace();
251 } else {
252 floatCS = SkColorSpace::MakeSRGBLinear();
253 }
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400254 GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS),
Brian Salomon85aeccf2019-07-15 12:30:44 -0400255 infoA.width(), infoA.height());
256
257 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
258 size_t floatRowBytes = floatBpp * infoA.width();
259 std::unique_ptr<char[]> floatA(new char[floatRowBytes * infoA.height()]);
260 std::unique_ptr<char[]> floatB(new char[floatRowBytes * infoA.height()]);
261 SkAssertResult(GrConvertPixels(floatInfo, floatA.get(), floatRowBytes, infoA, a, rowBytesA));
262 SkAssertResult(GrConvertPixels(floatInfo, floatB.get(), floatRowBytes, infoB, b, rowBytesB));
263
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400264 auto at = std::function<AccessPixelFn>(
265 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
266 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
267 });
268
269 return compare_pixels(infoA.width(), infoA.height(),
270 floatA.get(), at, floatB.get(), at,
271 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400272}
273
Brian Salomon28a8f282019-10-24 20:07:39 -0400274bool ComparePixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4],
275 std::function<ComparePixmapsErrorReporter>& error) {
276 return ComparePixels(a.info(), static_cast<const char*>(a.addr()), a.rowBytes(),
277 b.info(), static_cast<const char*>(b.addr()), b.rowBytes(),
278 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400279}
280
Brian Salomon28a8f282019-10-24 20:07:39 -0400281bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap,
282 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400283
284 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
285
286 std::unique_ptr<char[]> floatA(new char[floatBpp]);
287 // First convert 'col' to be compatible with 'pixmap'
288 {
289 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400290 GrImageInfo srcInfo(GrColorType::kRGBA_F32, kUnpremul_SkAlphaType, std::move(srcCS), 1, 1);
291 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), 1, 1);
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400292
293 SkAssertResult(GrConvertPixels(dstInfo, floatA.get(), floatBpp, srcInfo,
294 col.vec(), floatBpp));
295 }
296
297 size_t floatRowBytes = floatBpp * pixmap.width();
298 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
299 // Then convert 'pixmap' to RGBA_F32
300 {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400301 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(),
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400302 pixmap.width(), pixmap.height());
303
304 SkAssertResult(GrConvertPixels(dstInfo, floatB.get(), floatRowBytes, pixmap.info(),
305 pixmap.addr(), pixmap.rowBytes()));
306 }
307
308 auto atA = std::function<AccessPixelFn>(
309 [](const char* floatBuffer, int /* x */, int /* y */) {
310 return reinterpret_cast<const float*>(floatBuffer);
311 });
312
313 auto atB = std::function<AccessPixelFn>(
314 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
315 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
316 });
317
318 return compare_pixels(pixmap.width(), pixmap.height(), floatA.get(), atA, floatB.get(), atB,
319 tolRGBA, error);
320}
321
Brian Salomon28a8f282019-10-24 20:07:39 -0400322void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -0500323 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -0400324 int32_t expectedProxyRefs,
325 int32_t expectedBackingRefs) {
Brian Salomon557e8122019-10-24 10:37:08 -0400326 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
327
328 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
329 !proxy->refCntGreaterThan(expectedProxyRefs));
330 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
331}
332
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500333#include "src/utils/SkCharToGlyphCache.h"
Mike Reed0c607082019-04-11 17:10:17 -0400334
335static SkGlyphID hash_to_glyph(uint32_t value) {
336 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
337}
338
339namespace {
340class UnicharGen {
341 SkUnichar fU;
342 const int fStep;
343public:
344 UnicharGen(int step) : fU(0), fStep(step) {}
345
346 SkUnichar next() {
347 fU += fStep;
348 return fU;
349 }
350};
351}
352
353DEF_TEST(chartoglyph_cache, reporter) {
354 SkCharToGlyphCache cache;
355 const int step = 3;
356
357 UnicharGen gen(step);
358 for (int i = 0; i < 500; ++i) {
359 SkUnichar c = gen.next();
360 SkGlyphID glyph = hash_to_glyph(c);
361
362 int index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400363 if (index >= 0) {
364 index = cache.findGlyphIndex(c);
365 }
Mike Reed0c607082019-04-11 17:10:17 -0400366 REPORTER_ASSERT(reporter, index < 0);
367 cache.insertCharAndGlyph(~index, c, glyph);
368
369 UnicharGen gen2(step);
370 for (int j = 0; j <= i; ++j) {
371 c = gen2.next();
372 glyph = hash_to_glyph(c);
373 index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400374 if ((unsigned)index != glyph) {
375 index = cache.findGlyphIndex(c);
376 }
Mike Reed0c607082019-04-11 17:10:17 -0400377 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
378 }
379 }
380}