blob: 9dda6fb12ea8086a6f3625577db2dea4227bad0c [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) {
Greg Daniel40903af2020-01-30 14:55:05 -050084 GrSurfaceProxyView view = GrSurfaceProxy::Copy(context, proxy, origin, colorType,
85 GrMipMapped::kNo, SkBackingFit::kExact,
86 SkBudgeted::kYes);
87 SkASSERT(view.asTextureProxy());
Robert Phillips3500b772017-01-27 10:11:42 -050088
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,
109 const SkImageInfo& ii,
110 const SkColor4f& color,
111 GrMipMapped mipMapped,
112 GrRenderable renderable) {
Robert Phillips4d87b2b2019-07-23 13:44:16 -0400113 *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(),
114 color, mipMapped, renderable);
Robert Phillipscb1adb42019-06-10 15:09:34 -0400115 return backendTex->isValid();
Robert Phillipsee5fd132019-05-07 13:29:22 -0400116}
117
Brian Salomon28a8f282019-10-24 20:07:39 -0400118void DeleteBackendTexture(GrContext* context, const GrBackendTexture& backendTex) {
Greg Danielb3f82dd2019-05-29 14:24:32 -0400119 GrFlushInfo flushInfo;
120 flushInfo.fFlags = kSyncCpu_GrFlushFlag;
121 context->flush(flushInfo);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400122 context->deleteBackendTexture(backendTex);
Robert Phillipsee5fd132019-05-07 13:29:22 -0400123}
124
Brian Salomon28a8f282019-10-24 20:07:39 -0400125bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer,
126 const GrColor* dstBuffer,
127 int width, int height) {
Robert Phillipscb1adb42019-06-10 15:09:34 -0400128 const GrColor* srcPtr = srcBuffer;
129 const GrColor* dstPtr = dstBuffer;
Timothy Liang760dbc42018-07-17 13:28:20 -0400130 for (int j = 0; j < height; ++j) {
131 for (int i = 0; i < width; ++i) {
132 if (srcPtr[i] != dstPtr[i]) {
133 return false;
134 }
135 }
136 srcPtr += width;
137 dstPtr += width;
138 }
139 return true;
140}
Michael Ludwige8e10752018-10-01 12:42:53 -0400141
Brian Salomon28a8f282019-10-24 20:07:39 -0400142bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
Michael Ludwige8e10752018-10-01 12:42:53 -0400143 SkPixmap pm;
144 if (!bitmap.peekPixels(&pm)) {
145 dst->set("peekPixels failed");
146 return false;
147 }
148
149 // We're going to embed this PNG in a data URI, so make it as small as possible
150 SkPngEncoder::Options options;
151 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
152 options.fZLibLevel = 9;
153
154 SkDynamicMemoryWStream wStream;
155 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
156 dst->set("SkPngEncoder::Encode failed");
157 return false;
158 }
159
160 sk_sp<SkData> pngData = wStream.detachAsData();
161 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
162
163 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
164 // Infra says these can be pretty big, as long as we're only outputting them on failure.
165 static const size_t kMaxBase64Length = 1024 * 1024;
166 if (len > kMaxBase64Length) {
167 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
168 return false;
169 }
170
171 dst->resize(len);
172 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
173 dst->prepend("data:image/png;base64,");
174 return true;
175}
Mike Reed0c607082019-04-11 17:10:17 -0400176
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400177using AccessPixelFn = const float*(const char* floatBuffer, int x, int y);
178
179bool compare_pixels(int width, int height,
180 const char* floatA, std::function<AccessPixelFn>& atA,
181 const char* floatB, std::function<AccessPixelFn>& atB,
182 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
183
184 for (int y = 0; y < height; ++y) {
185 for (int x = 0; x < width; ++x) {
186 const float* rgbaA = atA(floatA, x, y);
187 const float* rgbaB = atB(floatB, x, y);
188 float diffs[4];
189 bool bad = false;
190 for (int i = 0; i < 4; ++i) {
191 diffs[i] = rgbaB[i] - rgbaA[i];
192 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
193 bad = true;
194 }
195 }
196 if (bad) {
197 error(x, y, diffs);
198 return false;
199 }
200 }
201 }
202 return true;
203}
204
Brian Salomon28a8f282019-10-24 20:07:39 -0400205bool ComparePixels(const GrImageInfo& infoA, const char* a, size_t rowBytesA,
206 const GrImageInfo& infoB, const char* b, size_t rowBytesB,
207 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400208 if (infoA.width() != infoB.width() || infoA.height() != infoB.height()) {
209 static constexpr float kDummyDiffs[4] = {};
210 error(-1, -1, kDummyDiffs);
211 return false;
212 }
213
214 SkAlphaType floatAlphaType = infoA.alphaType();
215 // If one is premul and the other is unpremul we do the comparison in premul space.
216 if ((infoA.alphaType() == kPremul_SkAlphaType ||
217 infoB.alphaType() == kPremul_SkAlphaType) &&
218 (infoA.alphaType() == kUnpremul_SkAlphaType ||
219 infoB.alphaType() == kUnpremul_SkAlphaType)) {
220 floatAlphaType = kPremul_SkAlphaType;
221 }
222 sk_sp<SkColorSpace> floatCS;
223 if (SkColorSpace::Equals(infoA.colorSpace(), infoB.colorSpace())) {
224 floatCS = infoA.refColorSpace();
225 } else {
226 floatCS = SkColorSpace::MakeSRGBLinear();
227 }
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400228 GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS),
Brian Salomon85aeccf2019-07-15 12:30:44 -0400229 infoA.width(), infoA.height());
230
231 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
232 size_t floatRowBytes = floatBpp * infoA.width();
233 std::unique_ptr<char[]> floatA(new char[floatRowBytes * infoA.height()]);
234 std::unique_ptr<char[]> floatB(new char[floatRowBytes * infoA.height()]);
235 SkAssertResult(GrConvertPixels(floatInfo, floatA.get(), floatRowBytes, infoA, a, rowBytesA));
236 SkAssertResult(GrConvertPixels(floatInfo, floatB.get(), floatRowBytes, infoB, b, rowBytesB));
237
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400238 auto at = std::function<AccessPixelFn>(
239 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
240 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
241 });
242
243 return compare_pixels(infoA.width(), infoA.height(),
244 floatA.get(), at, floatB.get(), at,
245 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400246}
247
Brian Salomon28a8f282019-10-24 20:07:39 -0400248bool ComparePixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4],
249 std::function<ComparePixmapsErrorReporter>& error) {
250 return ComparePixels(a.info(), static_cast<const char*>(a.addr()), a.rowBytes(),
251 b.info(), static_cast<const char*>(b.addr()), b.rowBytes(),
252 tolRGBA, error);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400253}
254
Brian Salomon28a8f282019-10-24 20:07:39 -0400255bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap,
256 const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400257
258 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
259
260 std::unique_ptr<char[]> floatA(new char[floatBpp]);
261 // First convert 'col' to be compatible with 'pixmap'
262 {
263 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400264 GrImageInfo srcInfo(GrColorType::kRGBA_F32, kUnpremul_SkAlphaType, std::move(srcCS), 1, 1);
265 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), 1, 1);
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400266
267 SkAssertResult(GrConvertPixels(dstInfo, floatA.get(), floatBpp, srcInfo,
268 col.vec(), floatBpp));
269 }
270
271 size_t floatRowBytes = floatBpp * pixmap.width();
272 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
273 // Then convert 'pixmap' to RGBA_F32
274 {
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400275 GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(),
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400276 pixmap.width(), pixmap.height());
277
278 SkAssertResult(GrConvertPixels(dstInfo, floatB.get(), floatRowBytes, pixmap.info(),
279 pixmap.addr(), pixmap.rowBytes()));
280 }
281
282 auto atA = std::function<AccessPixelFn>(
283 [](const char* floatBuffer, int /* x */, int /* y */) {
284 return reinterpret_cast<const float*>(floatBuffer);
285 });
286
287 auto atB = std::function<AccessPixelFn>(
288 [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) {
289 return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp);
290 });
291
292 return compare_pixels(pixmap.width(), pixmap.height(), floatA.get(), atA, floatB.get(), atB,
293 tolRGBA, error);
294}
295
Brian Salomon28a8f282019-10-24 20:07:39 -0400296void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -0500297 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -0400298 int32_t expectedProxyRefs,
299 int32_t expectedBackingRefs) {
Brian Salomon557e8122019-10-24 10:37:08 -0400300 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
301
302 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
303 !proxy->refCntGreaterThan(expectedProxyRefs));
304 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
305}
306
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500307#include "src/utils/SkCharToGlyphCache.h"
Mike Reed0c607082019-04-11 17:10:17 -0400308
309static SkGlyphID hash_to_glyph(uint32_t value) {
310 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
311}
312
313namespace {
314class UnicharGen {
315 SkUnichar fU;
316 const int fStep;
317public:
318 UnicharGen(int step) : fU(0), fStep(step) {}
319
320 SkUnichar next() {
321 fU += fStep;
322 return fU;
323 }
324};
325}
326
327DEF_TEST(chartoglyph_cache, reporter) {
328 SkCharToGlyphCache cache;
329 const int step = 3;
330
331 UnicharGen gen(step);
332 for (int i = 0; i < 500; ++i) {
333 SkUnichar c = gen.next();
334 SkGlyphID glyph = hash_to_glyph(c);
335
336 int index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400337 if (index >= 0) {
338 index = cache.findGlyphIndex(c);
339 }
Mike Reed0c607082019-04-11 17:10:17 -0400340 REPORTER_ASSERT(reporter, index < 0);
341 cache.insertCharAndGlyph(~index, c, glyph);
342
343 UnicharGen gen2(step);
344 for (int j = 0; j <= i; ++j) {
345 c = gen2.next();
346 glyph = hash_to_glyph(c);
347 index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400348 if ((unsigned)index != glyph) {
349 index = cache.findGlyphIndex(c);
350 }
Mike Reed0c607082019-04-11 17:10:17 -0400351 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
352 }
353 }
354}