blob: 86c908ac2be14759ba7c69c1baf7d54a4cbca7d9 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrSurfaceContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrSurfaceProxy.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/SkGr.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,
Brian Salomon28a8f282019-10-24 20:07:39 -040026 GrSurfaceContext* srcContext,
27 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,
Brian Salomon28a8f282019-10-24 20:07:39 -040053 GrSurfaceContext* dstContext,
54 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);
Brian Salomon14f99fc2020-12-07 12:19:47 -050095 auto dstContext = GrSurfaceContext::Make(dContext,
96 std::move(view),
97 {colorType, kPremul_SkAlphaType, nullptr});
Brian Salomonbf6b9792019-08-21 09:38:10 -040098 SkASSERT(dstContext);
Robert Phillips3500b772017-01-27 10:11:42 -050099
Adlai Hollerc95b5892020-08-11 12:02:22 -0400100 TestReadPixels(reporter, dContext, dstContext.get(), expectedPixelValues, testName);
Robert Phillips3500b772017-01-27 10:11:42 -0500101}
Timothy Liang760dbc42018-07-17 13:28:20 -0400102
Brian Salomon28a8f282019-10-24 20:07:39 -0400103bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) {
Michael Ludwige8e10752018-10-01 12:42:53 -0400104 SkPixmap pm;
105 if (!bitmap.peekPixels(&pm)) {
106 dst->set("peekPixels failed");
107 return false;
108 }
109
110 // We're going to embed this PNG in a data URI, so make it as small as possible
111 SkPngEncoder::Options options;
112 options.fFilterFlags = SkPngEncoder::FilterFlag::kAll;
113 options.fZLibLevel = 9;
114
115 SkDynamicMemoryWStream wStream;
116 if (!SkPngEncoder::Encode(&wStream, pm, options)) {
117 dst->set("SkPngEncoder::Encode failed");
118 return false;
119 }
120
121 sk_sp<SkData> pngData = wStream.detachAsData();
122 size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr);
123
124 // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs.
125 // Infra says these can be pretty big, as long as we're only outputting them on failure.
126 static const size_t kMaxBase64Length = 1024 * 1024;
127 if (len > kMaxBase64Length) {
128 dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len));
129 return false;
130 }
131
132 dst->resize(len);
133 SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str());
134 dst->prepend("data:image/png;base64,");
135 return true;
136}
Mike Reed0c607082019-04-11 17:10:17 -0400137
Brian Salomon5392c942021-03-30 16:14:37 -0400138static bool compare_colors(int x, int y,
139 const float rgbaA[],
140 const float rgbaB[],
141 const float tolRGBA[4],
142 std::function<ComparePixmapsErrorReporter>& error) {
143 float diffs[4];
144 bool bad = false;
145 for (int i = 0; i < 4; ++i) {
146 diffs[i] = rgbaB[i] - rgbaA[i];
147 if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) {
148 bad = true;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400149 }
150 }
Brian Salomon5392c942021-03-30 16:14:37 -0400151 if (bad) {
152 error(x, y, diffs);
153 return false;
154 }
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400155 return true;
156}
157
Brian Salomond8db5882021-03-31 14:19:31 -0400158bool ComparePixels(const GrCPixmap& a,
159 const GrCPixmap& b,
160 const float tolRGBA[4],
161 std::function<ComparePixmapsErrorReporter>& error) {
162 if (a.dimensions() != b.dimensions()) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400163 static constexpr float kDummyDiffs[4] = {};
164 error(-1, -1, kDummyDiffs);
165 return false;
166 }
167
Brian Salomond8db5882021-03-31 14:19:31 -0400168 SkAlphaType floatAlphaType = a.alphaType();
Brian Salomon85aeccf2019-07-15 12:30:44 -0400169 // If one is premul and the other is unpremul we do the comparison in premul space.
Brian Salomond8db5882021-03-31 14:19:31 -0400170 if ((a.alphaType() == kPremul_SkAlphaType || b.alphaType() == kPremul_SkAlphaType) &&
171 (a.alphaType() == kUnpremul_SkAlphaType || b.alphaType() == kUnpremul_SkAlphaType)) {
Brian Salomon85aeccf2019-07-15 12:30:44 -0400172 floatAlphaType = kPremul_SkAlphaType;
173 }
174 sk_sp<SkColorSpace> floatCS;
Brian Salomond8db5882021-03-31 14:19:31 -0400175 if (SkColorSpace::Equals(a.colorSpace(), b.colorSpace())) {
176 floatCS = a.refColorSpace();
Brian Salomon85aeccf2019-07-15 12:30:44 -0400177 } else {
178 floatCS = SkColorSpace::MakeSRGBLinear();
179 }
Brian Salomon5392c942021-03-30 16:14:37 -0400180 GrImageInfo floatInfo(GrColorType::kRGBA_F32,
181 floatAlphaType,
182 std::move(floatCS),
Brian Salomond8db5882021-03-31 14:19:31 -0400183 a.dimensions());
Brian Salomon85aeccf2019-07-15 12:30:44 -0400184
Brian Salomon5392c942021-03-30 16:14:37 -0400185 GrPixmap floatA = GrPixmap::Allocate(floatInfo);
186 GrPixmap floatB = GrPixmap::Allocate(floatInfo);
Brian Salomond8db5882021-03-31 14:19:31 -0400187 SkAssertResult(GrConvertPixels(floatA, a));
188 SkAssertResult(GrConvertPixels(floatB, b));
Brian Salomon85aeccf2019-07-15 12:30:44 -0400189
Brian Salomon5392c942021-03-30 16:14:37 -0400190 SkASSERT(floatA.rowBytes() == floatB.rowBytes());
191 auto at = [rb = floatA.rowBytes()](const void* base, int x, int y) {
192 return SkTAddOffset<const float>(base, y*rb + x*sizeof(float)*4);
193 };
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400194
Brian Salomon5392c942021-03-30 16:14:37 -0400195 for (int y = 0; y < floatA.height(); ++y) {
196 for (int x = 0; x < floatA.width(); ++x) {
197 const float* rgbaA = at(floatA.addr(), x, y);
198 const float* rgbaB = at(floatB.addr(), x, y);
199 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
200 return false;
201 }
202 }
203 }
204 return true;
Brian Salomon85aeccf2019-07-15 12:30:44 -0400205}
206
Brian Salomon5392c942021-03-30 16:14:37 -0400207bool CheckSolidPixels(const SkColor4f& col,
208 const SkPixmap& pixmap,
209 const float tolRGBA[4],
210 std::function<ComparePixmapsErrorReporter>& error) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400211 size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32);
212
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400213 // First convert 'col' to be compatible with 'pixmap'
Brian Salomon5392c942021-03-30 16:14:37 -0400214 GrPixmap colorPixmap;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400215 {
216 sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear();
Brian Salomon5392c942021-03-30 16:14:37 -0400217 GrImageInfo srcInfo(GrColorType::kRGBA_F32,
218 kUnpremul_SkAlphaType,
219 std::move(srcCS),
220 {1, 1});
221 GrCPixmap srcPixmap(srcInfo, col.vec(), floatBpp);
222 GrImageInfo dstInfo =
223 srcInfo.makeAlphaType(pixmap.alphaType()).makeColorSpace(pixmap.refColorSpace());
224 colorPixmap = GrPixmap::Allocate(dstInfo);
225 SkAssertResult(GrConvertPixels(colorPixmap, srcPixmap));
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400226 }
227
228 size_t floatRowBytes = floatBpp * pixmap.width();
229 std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]);
230 // Then convert 'pixmap' to RGBA_F32
Brian Salomon5392c942021-03-30 16:14:37 -0400231 GrPixmap f32Pixmap = GrPixmap::Allocate(pixmap.info().makeColorType(kRGBA_F32_SkColorType));
232 SkAssertResult(GrConvertPixels(f32Pixmap, pixmap));
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400233
Brian Salomon5392c942021-03-30 16:14:37 -0400234 for (int y = 0; y < f32Pixmap.height(); ++y) {
235 for (int x = 0; x < f32Pixmap.width(); ++x) {
236 auto rgbaA = SkTAddOffset<const float>(f32Pixmap.addr(),
237 f32Pixmap.rowBytes()*y + floatBpp*x);
238 auto rgbaB = static_cast<const float*>(colorPixmap.addr());
239 if (!compare_colors(x, y, rgbaA, rgbaB, tolRGBA, error)) {
240 return false;
241 }
242 }
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400243 }
Brian Salomon5392c942021-03-30 16:14:37 -0400244 return true;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400245}
246
Brian Salomon28a8f282019-10-24 20:07:39 -0400247void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter,
Greg Danield11ae2e2020-02-10 16:36:07 -0500248 GrSurfaceProxy* proxy,
Brian Salomon28a8f282019-10-24 20:07:39 -0400249 int32_t expectedProxyRefs,
250 int32_t expectedBackingRefs) {
Brian Salomon557e8122019-10-24 10:37:08 -0400251 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
252
253 REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) &&
254 !proxy->refCntGreaterThan(expectedProxyRefs));
255 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
256}
257
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500258#include "src/utils/SkCharToGlyphCache.h"
Mike Reed0c607082019-04-11 17:10:17 -0400259
260static SkGlyphID hash_to_glyph(uint32_t value) {
261 return SkToU16(((value >> 16) ^ value) & 0xFFFF);
262}
263
264namespace {
265class UnicharGen {
266 SkUnichar fU;
267 const int fStep;
268public:
269 UnicharGen(int step) : fU(0), fStep(step) {}
270
271 SkUnichar next() {
272 fU += fStep;
273 return fU;
274 }
275};
John Stilesa6841be2020-08-06 14:11:56 -0400276} // namespace
Mike Reed0c607082019-04-11 17:10:17 -0400277
278DEF_TEST(chartoglyph_cache, reporter) {
279 SkCharToGlyphCache cache;
280 const int step = 3;
281
282 UnicharGen gen(step);
283 for (int i = 0; i < 500; ++i) {
284 SkUnichar c = gen.next();
285 SkGlyphID glyph = hash_to_glyph(c);
286
287 int index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400288 if (index >= 0) {
289 index = cache.findGlyphIndex(c);
290 }
Mike Reed0c607082019-04-11 17:10:17 -0400291 REPORTER_ASSERT(reporter, index < 0);
292 cache.insertCharAndGlyph(~index, c, glyph);
293
294 UnicharGen gen2(step);
295 for (int j = 0; j <= i; ++j) {
296 c = gen2.next();
297 glyph = hash_to_glyph(c);
298 index = cache.findGlyphIndex(c);
Mike Reed194cab02019-04-15 12:07:19 -0400299 if ((unsigned)index != glyph) {
300 index = cache.findGlyphIndex(c);
301 }
Mike Reed0c607082019-04-11 17:10:17 -0400302 REPORTER_ASSERT(reporter, (unsigned)index == glyph);
303 }
304 }
305}