blob: 08ac867962bcce1a9292f49f54c2fd30327e6393 [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"
Brian Salomondd4087d2020-12-23 20:36:44 -050012#include "src/core/SkAutoPixmapStorage.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040013#include "src/core/SkUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040015#include "src/gpu/GrDrawingManager.h"
Robert Phillipsee5fd132019-05-07 13:29:22 -040016#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Robert Phillipseffd13f2020-07-20 15:00:36 -040018#include "src/gpu/GrRecordingContextPriv.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 Phillips53eaa642021-08-10 13:49:51 -040022#include "src/gpu/SurfaceContext.h"
Robert Phillips3500b772017-01-27 10:11:42 -050023
Brian Salomon28a8f282019-10-24 20:07:39 -040024void TestReadPixels(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -040025 GrDirectContext* dContext,
Robert Phillips53eaa642021-08-10 13:49:51 -040026 skgpu::SurfaceContext* srcContext,
Brian Salomon28a8f282019-10-24 20:07:39 -040027 uint32_t expectedPixelValues[],
28 const char* testName) {
Robert Phillips3500b772017-01-27 10:11:42 -050029 int pixelCnt = srcContext->width() * srcContext->height();
Brian Salomondd4087d2020-12-23 20:36:44 -050030 SkImageInfo ii = SkImageInfo::Make(srcContext->dimensions(),
31 kRGBA_8888_SkColorType,
32 kPremul_SkAlphaType);
33 SkAutoPixmapStorage pm;
34 pm.alloc(ii);
35 pm.erase(SK_ColorTRANSPARENT);
Robert Phillips3500b772017-01-27 10:11:42 -050036
Brian Salomondd4087d2020-12-23 20:36:44 -050037 bool read = srcContext->readPixels(dContext, pm, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050038 if (!read) {
39 ERRORF(reporter, "%s: Error reading from texture.", testName);
40 }
41
42 for (int i = 0; i < pixelCnt; ++i) {
Brian Salomondd4087d2020-12-23 20:36:44 -050043 if (pm.addr32()[i] != expectedPixelValues[i]) {
Robert Phillips3500b772017-01-27 10:11:42 -050044 ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.",
Brian Salomondd4087d2020-12-23 20:36:44 -050045 testName, i, expectedPixelValues[i], pm.addr32()[i]);
Robert Phillips3500b772017-01-27 10:11:42 -050046 break;
47 }
48 }
49}
50
Brian Salomon28a8f282019-10-24 20:07:39 -040051void TestWritePixels(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -040052 GrDirectContext* dContext,
Robert Phillips53eaa642021-08-10 13:49:51 -040053 skgpu::SurfaceContext* dstContext,
Brian Salomon28a8f282019-10-24 20:07:39 -040054 bool expectedToWork,
55 const char* testName) {
Brian Salomondd4087d2020-12-23 20:36:44 -050056 SkImageInfo ii = SkImageInfo::Make(dstContext->dimensions(),
57 kRGBA_8888_SkColorType,
58 kPremul_SkAlphaType);
59 SkAutoPixmapStorage pm;
60 pm.alloc(ii);
61 for (int y = 0; y < dstContext->height(); ++y) {
62 for (int x = 0; x < dstContext->width(); ++x) {
63 *pm.writable_addr32(x, y) = SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y));
Robert Phillips3500b772017-01-27 10:11:42 -050064 }
65 }
66
Brian Salomondd4087d2020-12-23 20:36:44 -050067 bool write = dstContext->writePixels(dContext, pm, {0, 0});
Robert Phillips3500b772017-01-27 10:11:42 -050068 if (!write) {
69 if (expectedToWork) {
70 ERRORF(reporter, "%s: Error writing to texture.", testName);
71 }
72 return;
73 }
74
75 if (write && !expectedToWork) {
76 ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName);
77 return;
78 }
79
Brian Salomondd4087d2020-12-23 20:36:44 -050080 TestReadPixels(reporter, dContext, dstContext, pm.writable_addr32(0, 0), testName);
Robert Phillips3500b772017-01-27 10:11:42 -050081}
82
Brian Salomon28a8f282019-10-24 20:07:39 -040083void TestCopyFromSurface(skiatest::Reporter* reporter,
Adlai Hollerc95b5892020-08-11 12:02:22 -040084 GrDirectContext* dContext,
Brian Salomon982127b2021-01-21 10:43:35 -050085 sk_sp<GrSurfaceProxy> proxy,
Greg Daniel40903af2020-01-30 14:55:05 -050086 GrSurfaceOrigin origin,
Brian Salomon28a8f282019-10-24 20:07:39 -040087 GrColorType colorType,
88 uint32_t expectedPixelValues[],
89 const char* testName) {
Brian Salomon982127b2021-01-21 10:43:35 -050090 auto copy = GrSurfaceProxy::Copy(dContext, std::move(proxy), origin, GrMipmapped::kNo,
Robert Phillipseffd13f2020-07-20 15:00:36 -040091 SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonc5243782020-04-02 12:50:34 -040092 SkASSERT(copy && copy->asTextureProxy());
Adlai Hollerc95b5892020-08-11 12:02:22 -040093 auto swizzle = dContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType);
Brian Salomonc5243782020-04-02 12:50:34 -040094 GrSurfaceProxyView view(std::move(copy), origin, swizzle);
Robert Phillips33bf2b52021-08-02 11:14:38 -040095 auto dstContext = dContext->priv().makeSC(std::move(view),
96 {colorType, kPremul_SkAlphaType, nullptr});
Brian Salomonbf6b9792019-08-21 09:38:10 -040097 SkASSERT(dstContext);
Robert Phillips3500b772017-01-27 10:11:42 -050098
Adlai Hollerc95b5892020-08-11 12:02:22 -040099 TestReadPixels(reporter, dContext, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -0500100}
Timothy Liang760dbc42018-07-17 13:28:20 -0400101
Brian Salomon28a8f282019-10-24 20:07:39 -0400102bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
Michael Ludwige8e10752018-10-01 12:42:53 -0400103 SkPixmap pm;
104 if (!bitmap.peekPixels(&pm)) {
105 dst->set("peekPixels failed");
106 return false;
107 }
108
109 // We're going to embed this PNG in a data URI, so make it as small as possible
110 SkPngEncoder::Options options;
111 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
112 options.fZLibLevel = 9;
113
114 SkDynamicMemoryWStream wStream;
115 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
116 dst->set("SkPngEncoder::Encode failed");
117 return false;
118 }
119
120 sk_sp<SkData> pngData = wStream.detachAsData();
121 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
122
123 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
124 // Infra says these can be pretty big, as long as we're only outputting them on failure.
125 static const size_t kMaxBase64Length = 1024 * 1024;
126 if (len > kMaxBase64Length) {
127 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
128 return false;
129 }
130
131 dst->resize(len);
132 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
133 dst->prepend("data:image/png;base64,");
134 return true;
135}
Mike Reed0c607082019-04-11 17:10:17 -0400136
Brian Salomon5392c942021-03-30 16:14:37 -0400137static bool compare_colors(int x, int y,
138 const float rgbaA[],
139 const float rgbaB[],
140 const float tolRGBA[4],
141 std::function<ComparePixmapsErrorReporter>& error) {
142 float diffs[4];
143 bool bad = false;
144 for (int i = 0; i < 4; ++i) {
145 diffs[i] = rgbaB[i] - rgbaA[i];
146 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
147 bad = true;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400148 }
149 }
Brian Salomon5392c942021-03-30 16:14:37 -0400150 if (bad) {
151 error(x, y, diffs);
152 return false;
153 }
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400154 return true;
155}
156
Brian Salomond8db5882021-03-31 14:19:31 -0400157bool ComparePixels(const GrCPixmap& a,
158 const GrCPixmap& b,
159 const float tolRGBA[4],
160 std::function<ComparePixmapsErrorReporter>& error) {
161 if (a.dimensions() != b.dimensions()) {
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400162 static constexpr float kEmptyDiffs[4] = {};
163 error(-1, -1, kEmptyDiffs);
Brian Salomon85aeccf2019-07-15 12:30:44 -0400164 return false;
165 }
166
Brian Salomond8db5882021-03-31 14:19:31 -0400167 SkAlphaType floatAlphaType = a.alphaType();
Brian Salomon85aeccf2019-07-15 12:30:44 -0400168 // If one is premul and the other is unpremul we do the comparison in premul space.
Brian Salomond8db5882021-03-31 14:19:31 -0400169 if ((a.alphaType() == kPremul_SkAlphaType || b.alphaType() == kPremul_SkAlphaType) &&
170 (a.alphaType() == kUnpremul_SkAlphaType || b.alphaType() == kUnpremul_SkAlphaType)) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400171 floatAlphaType = kPremul_SkAlphaType;
172 }
173 sk_sp<SkColorSpace> floatCS;
Brian Salomond8db5882021-03-31 14:19:31 -0400174 if (SkColorSpace::Equals(a.colorSpace(), b.colorSpace())) {
175 floatCS = a.refColorSpace();
Brian Salomon85aeccf2019-07-15 12:30:44 -0400176 } else {
177 floatCS = SkColorSpace::MakeSRGBLinear();
178 }
Brian Salomon5392c942021-03-30 16:14:37 -0400179 GrImageInfo floatInfo(GrColorType::kRGBA_F32,
180 floatAlphaType,
181 std::move(floatCS),
Brian Salomond8db5882021-03-31 14:19:31 -0400182 a.dimensions());
Brian Salomon85aeccf2019-07-15 12:30:44 -0400183
Brian Salomon5392c942021-03-30 16:14:37 -0400184 GrPixmap floatA = GrPixmap::Allocate(floatInfo);
185 GrPixmap floatB = GrPixmap::Allocate(floatInfo);
Brian Salomond8db5882021-03-31 14:19:31 -0400186 SkAssertResult(GrConvertPixels(floatA, a));
187 SkAssertResult(GrConvertPixels(floatB, b));
Brian Salomon85aeccf2019-07-15 12:30:44 -0400188
Brian Salomon5392c942021-03-30 16:14:37 -0400189 SkASSERT(floatA.rowBytes() == floatB.rowBytes());
190 auto at = [rb = floatA.rowBytes()](const void* base, int x, int y) {
191 return SkTAddOffset<const float>(base, y*rb + x*sizeof(float)*4);
192 };
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400193
Brian Salomon5392c942021-03-30 16:14:37 -0400194 for (int y = 0; y < floatA.height(); ++y) {
195 for (int x = 0; x < floatA.width(); ++x) {
196 const float* rgbaA = at(floatA.addr(), x, y);
197 const float* rgbaB = at(floatB.addr(), x, y);
198 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
199 return false;
200 }
201 }
202 }
203 return true;
Brian Salomon85aeccf2019-07-15 12:30:44 -0400204}
205
Brian Salomon5392c942021-03-30 16:14:37 -0400206bool CheckSolidPixels(const SkColor4f& col,
207 const SkPixmap& pixmap,
208 const float tolRGBA[4],
209 std::function<ComparePixmapsErrorReporter>& error) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400210 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
211
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400212 // First convert 'col' to be compatible with 'pixmap'
Brian Salomon5392c942021-03-30 16:14:37 -0400213 GrPixmap colorPixmap;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400214 {
215 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
Brian Salomon5392c942021-03-30 16:14:37 -0400216 GrImageInfo srcInfo(GrColorType::kRGBA_F32,
217 kUnpremul_SkAlphaType,
218 std::move(srcCS),
219 {1, 1});
220 GrCPixmap srcPixmap(srcInfo, col.vec(), floatBpp);
221 GrImageInfo dstInfo =
222 srcInfo.makeAlphaType(pixmap.alphaType()).makeColorSpace(pixmap.refColorSpace());
223 colorPixmap = GrPixmap::Allocate(dstInfo);
224 SkAssertResult(GrConvertPixels(colorPixmap, srcPixmap));
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400225 }
226
227 size_t floatRowBytes = floatBpp * pixmap.width();
228 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
229 // Then convert 'pixmap' to RGBA_F32
Brian Salomon5392c942021-03-30 16:14:37 -0400230 GrPixmap f32Pixmap = GrPixmap::Allocate(pixmap.info().makeColorType(kRGBA_F32_SkColorType));
231 SkAssertResult(GrConvertPixels(f32Pixmap, pixmap));
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400232
Brian Salomon5392c942021-03-30 16:14:37 -0400233 for (int y = 0; y < f32Pixmap.height(); ++y) {
234 for (int x = 0; x < f32Pixmap.width(); ++x) {
235 auto rgbaA = SkTAddOffset<const float>(f32Pixmap.addr(),
236 f32Pixmap.rowBytes()*y + floatBpp*x);
237 auto rgbaB = static_cast<const float*>(colorPixmap.addr());
238 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
239 return false;
240 }
241 }
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400242 }
Brian Salomon5392c942021-03-30 16:14:37 -0400243 return true;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400244}
245
Brian Salomon28a8f282019-10-24 20:07:39 -0400246void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -0500247 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -0400248 int32_t expectedProxyRefs,
249 int32_t expectedBackingRefs) {
Brian Salomon557e8122019-10-24 10:37:08 -0400250 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
251
252 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
253 !proxy->refCntGreaterThan(expectedProxyRefs));
254 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
255}
256
Robert Phillips168296b2021-08-16 11:41:27 -0400257std::unique_ptr<skgpu::SurfaceContext> CreateSurfaceContext(GrRecordingContext* rContext,
258 const GrImageInfo& info,
259 SkBackingFit fit,
260 GrSurfaceOrigin origin,
261 GrRenderable renderable,
262 int sampleCount,
263 GrMipmapped mipmapped,
264 GrProtected isProtected,
265 SkBudgeted budgeted) {
266 GrBackendFormat format = rContext->priv().caps()->getDefaultBackendFormat(info.colorType(),
267 renderable);
268 return rContext->priv().makeSC(info,
269 format,
270 fit,
271 origin,
272 renderable,
273 sampleCount,
274 mipmapped,
275 isProtected,
276 budgeted);
277}
278
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500279#include "src/utils/SkCharToGlyphCache.h"
Mike Reed0c607082019-04-11 17:10:17 -0400280
281static SkGlyphID hash_to_glyph(uint32_t value) {
282 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
283}
284
285namespace {
286class UnicharGen {
287 SkUnichar fU;
288 const int fStep;
289public:
290 UnicharGen(int step) : fU(0), fStep(step) {}
291
292 SkUnichar next() {
293 fU += fStep;
294 return fU;
295 }
296};
John Stilesa6841be2020-08-06 14:11:56 -0400297} // namespace
Mike Reed0c607082019-04-11 17:10:17 -0400298
299DEF_TEST(chartoglyph_cache, reporter) {
300 SkCharToGlyphCache cache;
301 const int step = 3;
302
303 UnicharGen gen(step);
304 for (int i = 0; i < 500; ++i) {
305 SkUnichar c = gen.next();
306 SkGlyphID glyph = hash_to_glyph(c);
307
308 int index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400309 if (index >= 0) {
310 index = cache.findGlyphIndex(c);
311 }
Mike Reed0c607082019-04-11 17:10:17 -0400312 REPORTER_ASSERT(reporter, index < 0);
313 cache.insertCharAndGlyph(~index, c, glyph);
314
315 UnicharGen gen2(step);
316 for (int j = 0; j <= i; ++j) {
317 c = gen2.next();
318 glyph = hash_to_glyph(c);
319 index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400320 if ((unsigned)index != glyph) {
321 index = cache.findGlyphIndex(c);
322 }
Mike Reed0c607082019-04-11 17:10:17 -0400323 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
324 }
325 }
326}