Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/TestUtils.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/encode/SkPngEncoder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/utils/SkBase64.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 12 | #include "src/core/SkUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrContextPriv.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrDrawingManager.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrGpu.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrSurfaceContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceProxy.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/SkGr.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 21 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 22 | void TestReadPixels(skiatest::Reporter* reporter, |
| 23 | GrSurfaceContext* srcContext, |
| 24 | uint32_t expectedPixelValues[], |
| 25 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 26 | 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 Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 32 | bool read = srcContext->readPixels(ii, pixels.get(), 0, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 33 | 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 46 | void TestWritePixels(skiatest::Reporter* reporter, |
| 47 | GrSurfaceContext* dstContext, |
| 48 | bool expectedToWork, |
| 49 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 50 | 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 Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 55 | SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(), |
| 60 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 61 | bool write = dstContext->writePixels(ii, pixels.get(), 0, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 62 | 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 74 | TestReadPixels(reporter, dstContext, pixels.get(), testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 77 | void TestCopyFromSurface(skiatest::Reporter* reporter, |
| 78 | GrContext* context, |
| 79 | GrSurfaceProxy* proxy, |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 80 | GrSurfaceOrigin origin, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 81 | GrColorType colorType, |
| 82 | uint32_t expectedPixelValues[], |
| 83 | const char* testName) { |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 84 | 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 Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 89 | auto dstContext = GrSurfaceContext::Make(context, std::move(view), colorType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 90 | kPremul_SkAlphaType, nullptr); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 91 | SkASSERT(dstContext); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 92 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 93 | TestReadPixels(reporter, dstContext.get(), expectedPixelValues, testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 94 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 95 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 96 | void FillPixelData(int width, int height, GrColor* data) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 97 | 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 107 | bool CreateBackendTexture(GrContext* context, |
| 108 | GrBackendTexture* backendTex, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 109 | 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 | |
| 120 | bool CreateBackendTexture(GrContext* context, |
| 121 | GrBackendTexture* backendTex, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 122 | const SkImageInfo& ii, |
| 123 | const SkColor4f& color, |
| 124 | GrMipMapped mipMapped, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 125 | GrRenderable renderable, |
| 126 | GrProtected isProtected) { |
| 127 | bool finishedBECreate = false; |
| 128 | auto markFinished = [](void* context) { |
| 129 | *(bool*)context = true; |
| 130 | }; |
| 131 | |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 132 | *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(), |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 133 | color, mipMapped, renderable, isProtected, |
| 134 | markFinished, &finishedBECreate); |
| 135 | if (backendTex->isValid()) { |
Greg Daniel | b577655 | 2020-06-19 20:21:08 -0400 | [diff] [blame] | 136 | context->submit(); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 137 | while (!finishedBECreate) { |
| 138 | context->checkAsyncWorkCompletion(); |
| 139 | } |
| 140 | } |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 141 | return backendTex->isValid(); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 144 | void DeleteBackendTexture(GrContext* context, const GrBackendTexture& backendTex) { |
Greg Daniel | ce9f016 | 2020-06-30 13:42:46 -0400 | [diff] [blame] | 145 | context->flush(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 146 | context->submit(true); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 147 | context->deleteBackendTexture(backendTex); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 150 | bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer, |
| 151 | const GrColor* dstBuffer, |
| 152 | int width, int height) { |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 153 | const GrColor* srcPtr = srcBuffer; |
| 154 | const GrColor* dstPtr = dstBuffer; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 155 | 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 Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 166 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 167 | bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) { |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 168 | 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 Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 201 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 202 | using AccessPixelFn = const float*(const char* floatBuffer, int x, int y); |
| 203 | |
| 204 | bool 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 230 | bool 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 233 | 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 253 | GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS), |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 254 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 263 | 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 273 | bool 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 280 | bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap, |
| 281 | const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 282 | |
| 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 289 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 291 | |
| 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 300 | GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 301 | 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 321 | void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter, |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 322 | GrSurfaceProxy* proxy, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 323 | int32_t expectedProxyRefs, |
| 324 | int32_t expectedBackingRefs) { |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 325 | 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 332 | #include "src/utils/SkCharToGlyphCache.h" |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 333 | |
| 334 | static SkGlyphID hash_to_glyph(uint32_t value) { |
| 335 | return SkToU16(((value >> 16) ^ value) & 0xFFFF); |
| 336 | } |
| 337 | |
| 338 | namespace { |
| 339 | class UnicharGen { |
| 340 | SkUnichar fU; |
| 341 | const int fStep; |
| 342 | public: |
| 343 | UnicharGen(int step) : fU(0), fStep(step) {} |
| 344 | |
| 345 | SkUnichar next() { |
| 346 | fU += fStep; |
| 347 | return fU; |
| 348 | } |
| 349 | }; |
| 350 | } |
| 351 | |
| 352 | DEF_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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 362 | if (index >= 0) { |
| 363 | index = cache.findGlyphIndex(c); |
| 364 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 365 | 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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 373 | if ((unsigned)index != glyph) { |
| 374 | index = cache.findGlyphIndex(c); |
| 375 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 376 | REPORTER_ASSERT(reporter, (unsigned)index == glyph); |
| 377 | } |
| 378 | } |
| 379 | } |