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" |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 12 | #include "src/core/SkAutoPixmapStorage.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 13 | #include "src/core/SkUtils.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrDirectContextPriv.h" |
Greg Daniel | 46cfbc6 | 2019-06-07 11:43:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDrawingManager.h" |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrGpu.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrRecordingContextPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrSurfaceProxy.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/SkGr.h" |
Robert Phillips | 53eaa64 | 2021-08-10 13:49:51 -0400 | [diff] [blame] | 22 | #include "src/gpu/SurfaceContext.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 23 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 24 | void TestReadPixels(skiatest::Reporter* reporter, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 25 | GrDirectContext* dContext, |
Robert Phillips | 53eaa64 | 2021-08-10 13:49:51 -0400 | [diff] [blame] | 26 | skgpu::SurfaceContext* srcContext, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 27 | uint32_t expectedPixelValues[], |
| 28 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 29 | int pixelCnt = srcContext->width() * srcContext->height(); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 30 | 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 Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 36 | |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 37 | bool read = srcContext->readPixels(dContext, pm, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 38 | if (!read) { |
| 39 | ERRORF(reporter, "%s: Error reading from texture.", testName); |
| 40 | } |
| 41 | |
| 42 | for (int i = 0; i < pixelCnt; ++i) { |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 43 | if (pm.addr32()[i] != expectedPixelValues[i]) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 44 | ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.", |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 45 | testName, i, expectedPixelValues[i], pm.addr32()[i]); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 51 | void TestWritePixels(skiatest::Reporter* reporter, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 52 | GrDirectContext* dContext, |
Robert Phillips | 53eaa64 | 2021-08-10 13:49:51 -0400 | [diff] [blame] | 53 | skgpu::SurfaceContext* dstContext, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 54 | bool expectedToWork, |
| 55 | const char* testName) { |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 56 | 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 Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 67 | bool write = dstContext->writePixels(dContext, pm, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 68 | 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 Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 80 | TestReadPixels(reporter, dContext, dstContext, pm.writable_addr32(0, 0), testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 83 | void TestCopyFromSurface(skiatest::Reporter* reporter, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 84 | GrDirectContext* dContext, |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 85 | sk_sp<GrSurfaceProxy> proxy, |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 86 | GrSurfaceOrigin origin, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 87 | GrColorType colorType, |
| 88 | uint32_t expectedPixelValues[], |
| 89 | const char* testName) { |
Brian Salomon | 982127b | 2021-01-21 10:43:35 -0500 | [diff] [blame] | 90 | auto copy = GrSurfaceProxy::Copy(dContext, std::move(proxy), origin, GrMipmapped::kNo, |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame] | 91 | SkBackingFit::kExact, SkBudgeted::kYes); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 92 | SkASSERT(copy && copy->asTextureProxy()); |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 93 | auto swizzle = dContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 94 | GrSurfaceProxyView view(std::move(copy), origin, swizzle); |
Robert Phillips | 33bf2b5 | 2021-08-02 11:14:38 -0400 | [diff] [blame] | 95 | auto dstContext = dContext->priv().makeSC(std::move(view), |
| 96 | {colorType, kPremul_SkAlphaType, nullptr}); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 97 | SkASSERT(dstContext); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 98 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 99 | TestReadPixels(reporter, dContext, dstContext.get(), expectedPixelValues, testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 100 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 101 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 102 | bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) { |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 103 | 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 Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 136 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 137 | static 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 150 | if (bad) { |
| 151 | error(x, y, diffs); |
| 152 | return false; |
| 153 | } |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 157 | bool 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 Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 162 | static constexpr float kEmptyDiffs[4] = {}; |
| 163 | error(-1, -1, kEmptyDiffs); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 167 | SkAlphaType floatAlphaType = a.alphaType(); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 168 | // If one is premul and the other is unpremul we do the comparison in premul space. |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 169 | if ((a.alphaType() == kPremul_SkAlphaType || b.alphaType() == kPremul_SkAlphaType) && |
| 170 | (a.alphaType() == kUnpremul_SkAlphaType || b.alphaType() == kUnpremul_SkAlphaType)) { |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 171 | floatAlphaType = kPremul_SkAlphaType; |
| 172 | } |
| 173 | sk_sp<SkColorSpace> floatCS; |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 174 | if (SkColorSpace::Equals(a.colorSpace(), b.colorSpace())) { |
| 175 | floatCS = a.refColorSpace(); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 176 | } else { |
| 177 | floatCS = SkColorSpace::MakeSRGBLinear(); |
| 178 | } |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 179 | GrImageInfo floatInfo(GrColorType::kRGBA_F32, |
| 180 | floatAlphaType, |
| 181 | std::move(floatCS), |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 182 | a.dimensions()); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 183 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 184 | GrPixmap floatA = GrPixmap::Allocate(floatInfo); |
| 185 | GrPixmap floatB = GrPixmap::Allocate(floatInfo); |
Brian Salomon | d8db588 | 2021-03-31 14:19:31 -0400 | [diff] [blame] | 186 | SkAssertResult(GrConvertPixels(floatA, a)); |
| 187 | SkAssertResult(GrConvertPixels(floatB, b)); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 188 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 189 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 193 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 194 | 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 Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 206 | bool CheckSolidPixels(const SkColor4f& col, |
| 207 | const SkPixmap& pixmap, |
| 208 | const float tolRGBA[4], |
| 209 | std::function<ComparePixmapsErrorReporter>& error) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 210 | size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32); |
| 211 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 212 | // First convert 'col' to be compatible with 'pixmap' |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 213 | GrPixmap colorPixmap; |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 214 | { |
| 215 | sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear(); |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 216 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 225 | } |
| 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 Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 230 | GrPixmap f32Pixmap = GrPixmap::Allocate(pixmap.info().makeColorType(kRGBA_F32_SkColorType)); |
| 231 | SkAssertResult(GrConvertPixels(f32Pixmap, pixmap)); |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 232 | |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 233 | 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 Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 242 | } |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 243 | return true; |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 246 | void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter, |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 247 | GrSurfaceProxy* proxy, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 248 | int32_t expectedProxyRefs, |
| 249 | int32_t expectedBackingRefs) { |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 250 | 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 Phillips | 168296b | 2021-08-16 11:41:27 -0400 | [diff] [blame] | 257 | std::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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 279 | #include "src/utils/SkCharToGlyphCache.h" |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 280 | |
| 281 | static SkGlyphID hash_to_glyph(uint32_t value) { |
| 282 | return SkToU16(((value >> 16) ^ value) & 0xFFFF); |
| 283 | } |
| 284 | |
| 285 | namespace { |
| 286 | class UnicharGen { |
| 287 | SkUnichar fU; |
| 288 | const int fStep; |
| 289 | public: |
| 290 | UnicharGen(int step) : fU(0), fStep(step) {} |
| 291 | |
| 292 | SkUnichar next() { |
| 293 | fU += fStep; |
| 294 | return fU; |
| 295 | } |
| 296 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 297 | } // namespace |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 298 | |
| 299 | DEF_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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 309 | if (index >= 0) { |
| 310 | index = cache.findGlyphIndex(c); |
| 311 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 312 | 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 Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 320 | if ((unsigned)index != glyph) { |
| 321 | index = cache.findGlyphIndex(c); |
| 322 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, (unsigned)index == glyph); |
| 324 | } |
| 325 | } |
| 326 | } |