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" |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceContext.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 | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 22 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 23 | void TestReadPixels(skiatest::Reporter* reporter, |
| 24 | GrSurfaceContext* srcContext, |
| 25 | uint32_t expectedPixelValues[], |
| 26 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 27 | int pixelCnt = srcContext->width() * srcContext->height(); |
| 28 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 29 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 30 | |
| 31 | SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(), |
| 32 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 33 | bool read = srcContext->readPixels(ii, pixels.get(), 0, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 34 | if (!read) { |
| 35 | ERRORF(reporter, "%s: Error reading from texture.", testName); |
| 36 | } |
| 37 | |
| 38 | for (int i = 0; i < pixelCnt; ++i) { |
| 39 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 40 | ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.", |
| 41 | testName, i, expectedPixelValues[i], pixels.get()[i]); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 47 | void TestWritePixels(skiatest::Reporter* reporter, |
| 48 | GrSurfaceContext* dstContext, |
| 49 | bool expectedToWork, |
| 50 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 51 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 52 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 53 | for (int y = 0; y < dstContext->width(); ++y) { |
| 54 | for (int x = 0; x < dstContext->height(); ++x) { |
| 55 | pixels.get()[y * dstContext->width() + x] = |
Brian Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 56 | SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(), |
| 61 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 62 | bool write = dstContext->writePixels(ii, pixels.get(), 0, {0, 0}); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 63 | if (!write) { |
| 64 | if (expectedToWork) { |
| 65 | ERRORF(reporter, "%s: Error writing to texture.", testName); |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (write && !expectedToWork) { |
| 71 | ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName); |
| 72 | return; |
| 73 | } |
| 74 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 75 | TestReadPixels(reporter, dstContext, pixels.get(), testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 78 | void TestCopyFromSurface(skiatest::Reporter* reporter, |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 79 | GrRecordingContext* rContext, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 80 | GrSurfaceProxy* proxy, |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 81 | GrSurfaceOrigin origin, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 82 | GrColorType colorType, |
| 83 | uint32_t expectedPixelValues[], |
| 84 | const char* testName) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 85 | auto copy = GrSurfaceProxy::Copy(rContext, proxy, origin, GrMipMapped::kNo, |
| 86 | SkBackingFit::kExact, SkBudgeted::kYes); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 87 | SkASSERT(copy && copy->asTextureProxy()); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 88 | auto swizzle = rContext->priv().caps()->getReadSwizzle(copy->backendFormat(), colorType); |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 89 | GrSurfaceProxyView view(std::move(copy), origin, swizzle); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 90 | auto dstContext = GrSurfaceContext::Make(rContext, std::move(view), colorType, |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 91 | kPremul_SkAlphaType, nullptr); |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 92 | SkASSERT(dstContext); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 93 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 94 | TestReadPixels(reporter, dstContext.get(), expectedPixelValues, testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 95 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 96 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 97 | void FillPixelData(int width, int height, GrColor* data) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 98 | for (int j = 0; j < height; ++j) { |
| 99 | for (int i = 0; i < width; ++i) { |
| 100 | unsigned int red = (unsigned int)(256.f * (i / (float)width)); |
| 101 | unsigned int green = (unsigned int)(256.f * (j / (float)height)); |
| 102 | data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8), |
| 103 | 0xff, 0xff); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 108 | bool CreateBackendTexture(GrDirectContext* dContext, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 109 | GrBackendTexture* backendTex, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 110 | int width, int height, |
| 111 | SkColorType colorType, |
| 112 | const SkColor4f& color, |
| 113 | GrMipMapped mipMapped, |
| 114 | GrRenderable renderable, |
| 115 | GrProtected isProtected) { |
| 116 | SkImageInfo info = SkImageInfo::Make(width, height, colorType, kPremul_SkAlphaType); |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 117 | return CreateBackendTexture(dContext, backendTex, info, color, mipMapped, renderable, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 118 | isProtected); |
| 119 | } |
| 120 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 121 | bool CreateBackendTexture(GrDirectContext* dContext, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 122 | GrBackendTexture* backendTex, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 123 | const SkImageInfo& ii, |
| 124 | const SkColor4f& color, |
| 125 | GrMipMapped mipMapped, |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 126 | GrRenderable renderable, |
| 127 | GrProtected isProtected) { |
| 128 | bool finishedBECreate = false; |
| 129 | auto markFinished = [](void* context) { |
| 130 | *(bool*)context = true; |
| 131 | }; |
| 132 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 133 | *backendTex = dContext->createBackendTexture(ii.width(), ii.height(), ii.colorType(), |
| 134 | color, mipMapped, renderable, isProtected, |
| 135 | markFinished, &finishedBECreate); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 136 | if (backendTex->isValid()) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 137 | dContext->submit(); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 138 | while (!finishedBECreate) { |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 139 | dContext->checkAsyncWorkCompletion(); |
Greg Daniel | c1ad77c | 2020-05-06 11:40:03 -0400 | [diff] [blame] | 140 | } |
| 141 | } |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 142 | return backendTex->isValid(); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Robert Phillips | effd13f | 2020-07-20 15:00:36 -0400 | [diff] [blame^] | 145 | void DeleteBackendTexture(GrDirectContext* dContext, const GrBackendTexture& backendTex) { |
| 146 | dContext->flush(); |
| 147 | dContext->submit(true); |
| 148 | dContext->deleteBackendTexture(backendTex); |
Robert Phillips | ee5fd13 | 2019-05-07 13:29:22 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 151 | bool DoesFullBufferContainCorrectColor(const GrColor* srcBuffer, |
| 152 | const GrColor* dstBuffer, |
| 153 | int width, int height) { |
Robert Phillips | cb1adb4 | 2019-06-10 15:09:34 -0400 | [diff] [blame] | 154 | const GrColor* srcPtr = srcBuffer; |
| 155 | const GrColor* dstPtr = dstBuffer; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 156 | for (int j = 0; j < height; ++j) { |
| 157 | for (int i = 0; i < width; ++i) { |
| 158 | if (srcPtr[i] != dstPtr[i]) { |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | srcPtr += width; |
| 163 | dstPtr += width; |
| 164 | } |
| 165 | return true; |
| 166 | } |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 167 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 168 | bool BipmapToBase64DataURI(const SkBitmap& bitmap, SkString* dst) { |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 169 | SkPixmap pm; |
| 170 | if (!bitmap.peekPixels(&pm)) { |
| 171 | dst->set("peekPixels failed"); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // We're going to embed this PNG in a data URI, so make it as small as possible |
| 176 | SkPngEncoder::Options options; |
| 177 | options.fFilterFlags = SkPngEncoder::FilterFlag::kAll; |
| 178 | options.fZLibLevel = 9; |
| 179 | |
| 180 | SkDynamicMemoryWStream wStream; |
| 181 | if (!SkPngEncoder::Encode(&wStream, pm, options)) { |
| 182 | dst->set("SkPngEncoder::Encode failed"); |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | sk_sp<SkData> pngData = wStream.detachAsData(); |
| 187 | size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr); |
| 188 | |
| 189 | // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs. |
| 190 | // Infra says these can be pretty big, as long as we're only outputting them on failure. |
| 191 | static const size_t kMaxBase64Length = 1024 * 1024; |
| 192 | if (len > kMaxBase64Length) { |
| 193 | dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len)); |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | dst->resize(len); |
| 198 | SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str()); |
| 199 | dst->prepend("data:image/png;base64,"); |
| 200 | return true; |
| 201 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 202 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 203 | using AccessPixelFn = const float*(const char* floatBuffer, int x, int y); |
| 204 | |
| 205 | bool compare_pixels(int width, int height, |
| 206 | const char* floatA, std::function<AccessPixelFn>& atA, |
| 207 | const char* floatB, std::function<AccessPixelFn>& atB, |
| 208 | const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) { |
| 209 | |
| 210 | for (int y = 0; y < height; ++y) { |
| 211 | for (int x = 0; x < width; ++x) { |
| 212 | const float* rgbaA = atA(floatA, x, y); |
| 213 | const float* rgbaB = atB(floatB, x, y); |
| 214 | float diffs[4]; |
| 215 | bool bad = false; |
| 216 | for (int i = 0; i < 4; ++i) { |
| 217 | diffs[i] = rgbaB[i] - rgbaA[i]; |
| 218 | if (std::abs(diffs[i]) > std::abs(tolRGBA[i])) { |
| 219 | bad = true; |
| 220 | } |
| 221 | } |
| 222 | if (bad) { |
| 223 | error(x, y, diffs); |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 231 | bool ComparePixels(const GrImageInfo& infoA, const char* a, size_t rowBytesA, |
| 232 | const GrImageInfo& infoB, const char* b, size_t rowBytesB, |
| 233 | const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) { |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 234 | if (infoA.width() != infoB.width() || infoA.height() != infoB.height()) { |
| 235 | static constexpr float kDummyDiffs[4] = {}; |
| 236 | error(-1, -1, kDummyDiffs); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | SkAlphaType floatAlphaType = infoA.alphaType(); |
| 241 | // If one is premul and the other is unpremul we do the comparison in premul space. |
| 242 | if ((infoA.alphaType() == kPremul_SkAlphaType || |
| 243 | infoB.alphaType() == kPremul_SkAlphaType) && |
| 244 | (infoA.alphaType() == kUnpremul_SkAlphaType || |
| 245 | infoB.alphaType() == kUnpremul_SkAlphaType)) { |
| 246 | floatAlphaType = kPremul_SkAlphaType; |
| 247 | } |
| 248 | sk_sp<SkColorSpace> floatCS; |
| 249 | if (SkColorSpace::Equals(infoA.colorSpace(), infoB.colorSpace())) { |
| 250 | floatCS = infoA.refColorSpace(); |
| 251 | } else { |
| 252 | floatCS = SkColorSpace::MakeSRGBLinear(); |
| 253 | } |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 254 | GrImageInfo floatInfo(GrColorType::kRGBA_F32, floatAlphaType, std::move(floatCS), |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 255 | infoA.width(), infoA.height()); |
| 256 | |
| 257 | size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32); |
| 258 | size_t floatRowBytes = floatBpp * infoA.width(); |
| 259 | std::unique_ptr<char[]> floatA(new char[floatRowBytes * infoA.height()]); |
| 260 | std::unique_ptr<char[]> floatB(new char[floatRowBytes * infoA.height()]); |
| 261 | SkAssertResult(GrConvertPixels(floatInfo, floatA.get(), floatRowBytes, infoA, a, rowBytesA)); |
| 262 | SkAssertResult(GrConvertPixels(floatInfo, floatB.get(), floatRowBytes, infoB, b, rowBytesB)); |
| 263 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 264 | auto at = std::function<AccessPixelFn>( |
| 265 | [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) { |
| 266 | return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp); |
| 267 | }); |
| 268 | |
| 269 | return compare_pixels(infoA.width(), infoA.height(), |
| 270 | floatA.get(), at, floatB.get(), at, |
| 271 | tolRGBA, error); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 274 | bool ComparePixels(const SkPixmap& a, const SkPixmap& b, const float tolRGBA[4], |
| 275 | std::function<ComparePixmapsErrorReporter>& error) { |
| 276 | return ComparePixels(a.info(), static_cast<const char*>(a.addr()), a.rowBytes(), |
| 277 | b.info(), static_cast<const char*>(b.addr()), b.rowBytes(), |
| 278 | tolRGBA, error); |
Brian Salomon | 85aeccf | 2019-07-15 12:30:44 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 281 | bool CheckSolidPixels(const SkColor4f& col, const SkPixmap& pixmap, |
| 282 | const float tolRGBA[4], std::function<ComparePixmapsErrorReporter>& error) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 283 | |
| 284 | size_t floatBpp = GrColorTypeBytesPerPixel(GrColorType::kRGBA_F32); |
| 285 | |
| 286 | std::unique_ptr<char[]> floatA(new char[floatBpp]); |
| 287 | // First convert 'col' to be compatible with 'pixmap' |
| 288 | { |
| 289 | sk_sp<SkColorSpace> srcCS = SkColorSpace::MakeSRGBLinear(); |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 290 | GrImageInfo srcInfo(GrColorType::kRGBA_F32, kUnpremul_SkAlphaType, std::move(srcCS), 1, 1); |
| 291 | GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), 1, 1); |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 292 | |
| 293 | SkAssertResult(GrConvertPixels(dstInfo, floatA.get(), floatBpp, srcInfo, |
| 294 | col.vec(), floatBpp)); |
| 295 | } |
| 296 | |
| 297 | size_t floatRowBytes = floatBpp * pixmap.width(); |
| 298 | std::unique_ptr<char[]> floatB(new char[floatRowBytes * pixmap.height()]); |
| 299 | // Then convert 'pixmap' to RGBA_F32 |
| 300 | { |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 301 | GrImageInfo dstInfo(GrColorType::kRGBA_F32, pixmap.alphaType(), pixmap.refColorSpace(), |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 302 | pixmap.width(), pixmap.height()); |
| 303 | |
| 304 | SkAssertResult(GrConvertPixels(dstInfo, floatB.get(), floatRowBytes, pixmap.info(), |
| 305 | pixmap.addr(), pixmap.rowBytes())); |
| 306 | } |
| 307 | |
| 308 | auto atA = std::function<AccessPixelFn>( |
| 309 | [](const char* floatBuffer, int /* x */, int /* y */) { |
| 310 | return reinterpret_cast<const float*>(floatBuffer); |
| 311 | }); |
| 312 | |
| 313 | auto atB = std::function<AccessPixelFn>( |
| 314 | [floatBpp, floatRowBytes](const char* floatBuffer, int x, int y) { |
| 315 | return reinterpret_cast<const float*>(floatBuffer + y * floatRowBytes + x * floatBpp); |
| 316 | }); |
| 317 | |
| 318 | return compare_pixels(pixmap.width(), pixmap.height(), floatA.get(), atA, floatB.get(), atB, |
| 319 | tolRGBA, error); |
| 320 | } |
| 321 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 322 | void CheckSingleThreadedProxyRefs(skiatest::Reporter* reporter, |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 323 | GrSurfaceProxy* proxy, |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 324 | int32_t expectedProxyRefs, |
| 325 | int32_t expectedBackingRefs) { |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 326 | int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt(); |
| 327 | |
| 328 | REPORTER_ASSERT(reporter, proxy->refCntGreaterThan(expectedProxyRefs - 1) && |
| 329 | !proxy->refCntGreaterThan(expectedProxyRefs)); |
| 330 | REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs); |
| 331 | } |
| 332 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 333 | #include "src/utils/SkCharToGlyphCache.h" |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 334 | |
| 335 | static SkGlyphID hash_to_glyph(uint32_t value) { |
| 336 | return SkToU16(((value >> 16) ^ value) & 0xFFFF); |
| 337 | } |
| 338 | |
| 339 | namespace { |
| 340 | class UnicharGen { |
| 341 | SkUnichar fU; |
| 342 | const int fStep; |
| 343 | public: |
| 344 | UnicharGen(int step) : fU(0), fStep(step) {} |
| 345 | |
| 346 | SkUnichar next() { |
| 347 | fU += fStep; |
| 348 | return fU; |
| 349 | } |
| 350 | }; |
| 351 | } |
| 352 | |
| 353 | DEF_TEST(chartoglyph_cache, reporter) { |
| 354 | SkCharToGlyphCache cache; |
| 355 | const int step = 3; |
| 356 | |
| 357 | UnicharGen gen(step); |
| 358 | for (int i = 0; i < 500; ++i) { |
| 359 | SkUnichar c = gen.next(); |
| 360 | SkGlyphID glyph = hash_to_glyph(c); |
| 361 | |
| 362 | int index = cache.findGlyphIndex(c); |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 363 | if (index >= 0) { |
| 364 | index = cache.findGlyphIndex(c); |
| 365 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 366 | REPORTER_ASSERT(reporter, index < 0); |
| 367 | cache.insertCharAndGlyph(~index, c, glyph); |
| 368 | |
| 369 | UnicharGen gen2(step); |
| 370 | for (int j = 0; j <= i; ++j) { |
| 371 | c = gen2.next(); |
| 372 | glyph = hash_to_glyph(c); |
| 373 | index = cache.findGlyphIndex(c); |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 374 | if ((unsigned)index != glyph) { |
| 375 | index = cache.findGlyphIndex(c); |
| 376 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 377 | REPORTER_ASSERT(reporter, (unsigned)index == glyph); |
| 378 | } |
| 379 | } |
| 380 | } |