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) { |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 84 | GrSurfaceProxyView view = GrSurfaceProxy::Copy(context, proxy, origin, colorType, |
| 85 | GrMipMapped::kNo, SkBackingFit::kExact, |
| 86 | SkBudgeted::kYes); |
| 87 | SkASSERT(view.asTextureProxy()); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 88 | |
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, |
| 109 | const SkImageInfo& ii, |
| 110 | const SkColor4f& color, |
| 111 | GrMipMapped mipMapped, |
| 112 | GrRenderable renderable) { |
Robert Phillips | 4d87b2b | 2019-07-23 13:44:16 -0400 | [diff] [blame] | 113 | *backendTex = context->createBackendTexture(ii.width(), ii.height(), ii.colorType(), |
| 114 | color, mipMapped, renderable); |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 115 | return backendTex->isValid(); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 118 | void DeleteBackendTexture(GrContext* context, const GrBackendTexture& backendTex) { |
Greg Daniel | b3f82dd | 2019-05-29 14:24:32 -0400 | [diff] [blame] | 119 | GrFlushInfo flushInfo; |
| 120 | flushInfo.fFlags = kSyncCpu_GrFlushFlag; |
| 121 | context->flush(flushInfo); |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 122 | context->deleteBackendTexture(backendTex); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 125 | bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer, |
| 126 | const GrColor* dstBuffer, |
| 127 | int width, int height) { |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 128 | const GrColor* srcPtr = srcBuffer; |
| 129 | const GrColor* dstPtr = dstBuffer; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 130 | 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 Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 141 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 142 | bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) { |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 143 | 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 Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 176 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 177 | using AccessPixelFn = const float*(const char* floatBuffer, int x, int y); |
| 178 | |
| 179 | bool 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 205 | bool 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 208 | 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 228 | GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS), |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 229 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 238 | 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 248 | bool 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 255 | bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap, |
| 256 | const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 257 | |
| 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 264 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 266 | |
| 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 Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 275 | GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 276 | 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 Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 296 | void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter, |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 297 | GrSurfaceProxy* proxy, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 298 | int32_t expectedProxyRefs, |
| 299 | int32_t expectedBackingRefs) { |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 300 | 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 307 | #include "src/utils/SkCharToGlyphCache.h" |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 308 | |
| 309 | static SkGlyphID hash_to_glyph(uint32_t value) { |
| 310 | return SkToU16(((value >> 16) ^ value) & 0xFFFF); |
| 311 | } |
| 312 | |
| 313 | namespace { |
| 314 | class UnicharGen { |
| 315 | SkUnichar fU; |
| 316 | const int fStep; |
| 317 | public: |
| 318 | UnicharGen(int step) : fU(0), fStep(step) {} |
| 319 | |
| 320 | SkUnichar next() { |
| 321 | fU += fStep; |
| 322 | return fU; |
| 323 | } |
| 324 | }; |
| 325 | } |
| 326 | |
| 327 | DEF_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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 337 | if (index >= 0) { |
| 338 | index = cache.findGlyphIndex(c); |
| 339 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 340 | 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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 348 | if ((unsigned)index != glyph) { |
| 349 | index = cache.findGlyphIndex(c); |
| 350 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 351 | REPORTER_ASSERT(reporter, (unsigned)index == glyph); |
| 352 | } |
| 353 | } |
| 354 | } |