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" |
| 11 | #include "include/private/GrSurfaceProxy.h" |
| 12 | #include "include/private/GrTextureProxy.h" |
| 13 | #include "include/utils/SkBase64.h" |
| 14 | #include "src/gpu/GrContextPriv.h" |
| 15 | #include "src/gpu/GrSurfaceContext.h" |
| 16 | #include "src/gpu/GrSurfaceContextPriv.h" |
| 17 | #include "src/gpu/SkGr.h" |
| 18 | #include "tools/gpu/ProxyUtils.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 19 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 20 | void test_read_pixels(skiatest::Reporter* reporter, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 21 | GrSurfaceContext* srcContext, uint32_t expectedPixelValues[], |
| 22 | const char* testName) { |
| 23 | int pixelCnt = srcContext->width() * srcContext->height(); |
| 24 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 25 | memset(pixels.get(), 0, sizeof(uint32_t)*pixelCnt); |
| 26 | |
| 27 | SkImageInfo ii = SkImageInfo::Make(srcContext->width(), srcContext->height(), |
| 28 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 29 | bool read = srcContext->readPixels(ii, pixels.get(), 0, 0, 0); |
| 30 | if (!read) { |
| 31 | ERRORF(reporter, "%s: Error reading from texture.", testName); |
| 32 | } |
| 33 | |
| 34 | for (int i = 0; i < pixelCnt; ++i) { |
| 35 | if (pixels.get()[i] != expectedPixelValues[i]) { |
| 36 | ERRORF(reporter, "%s: Error, pixel value %d should be 0x%08x, got 0x%08x.", |
| 37 | testName, i, expectedPixelValues[i], pixels.get()[i]); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 43 | void test_write_pixels(skiatest::Reporter* reporter, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 44 | GrSurfaceContext* dstContext, bool expectedToWork, |
| 45 | const char* testName) { |
| 46 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 47 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 48 | for (int y = 0; y < dstContext->width(); ++y) { |
| 49 | for (int x = 0; x < dstContext->height(); ++x) { |
| 50 | pixels.get()[y * dstContext->width() + x] = |
Brian Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 51 | SkColorToPremulGrColor(SkColorSetARGB(2*y, x, y, x + y)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | SkImageInfo ii = SkImageInfo::Make(dstContext->width(), dstContext->height(), |
| 56 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 57 | bool write = dstContext->writePixels(ii, pixels.get(), 0, 0, 0); |
| 58 | if (!write) { |
| 59 | if (expectedToWork) { |
| 60 | ERRORF(reporter, "%s: Error writing to texture.", testName); |
| 61 | } |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (write && !expectedToWork) { |
| 66 | ERRORF(reporter, "%s: writePixels succeeded when it wasn't supposed to.", testName); |
| 67 | return; |
| 68 | } |
| 69 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 70 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void test_copy_from_surface(skiatest::Reporter* reporter, GrContext* context, |
| 74 | GrSurfaceProxy* proxy, uint32_t expectedPixelValues[], |
| 75 | bool onlyTestRTConfig, const char* testName) { |
| 76 | GrSurfaceDesc copyDstDesc; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 77 | copyDstDesc.fWidth = proxy->width(); |
| 78 | copyDstDesc.fHeight = proxy->height(); |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 79 | copyDstDesc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 80 | |
| 81 | for (auto flags : { kNone_GrSurfaceFlags, kRenderTarget_GrSurfaceFlag }) { |
| 82 | if (kNone_GrSurfaceFlags == flags && onlyTestRTConfig) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | copyDstDesc.fFlags = flags; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 87 | auto origin = (kNone_GrSurfaceFlags == flags) ? kTopLeft_GrSurfaceOrigin |
| 88 | : kBottomLeft_GrSurfaceOrigin; |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 89 | |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 90 | sk_sp<GrSurfaceContext> dstContext( |
| 91 | GrSurfaceProxy::TestCopy(context, copyDstDesc, origin, proxy)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 92 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 93 | test_read_pixels(reporter, dstContext.get(), expectedPixelValues, testName); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 97 | void test_copy_to_surface(skiatest::Reporter* reporter, |
| 98 | GrContext* context, |
| 99 | GrSurfaceContext* dstContext, |
| 100 | const char* testName) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 101 | |
| 102 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 103 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 104 | for (int y = 0; y < dstContext->width(); ++y) { |
| 105 | for (int x = 0; x < dstContext->height(); ++x) { |
| 106 | pixels.get()[y * dstContext->width() + x] = |
Brian Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 107 | SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 111 | for (auto isRT : {false, true}) { |
| 112 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 113 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 114 | context, isRT, dstContext->width(), |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 115 | dstContext->height(), GrColorType::kRGBA_8888, origin, pixels.get(), 0); |
| 116 | dstContext->copy(src.get()); |
| 117 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
| 118 | } |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 121 | |
| 122 | void fill_pixel_data(int width, int height, GrColor* data) { |
| 123 | for (int j = 0; j < height; ++j) { |
| 124 | for (int i = 0; i < width; ++i) { |
| 125 | unsigned int red = (unsigned int)(256.f * (i / (float)width)); |
| 126 | unsigned int green = (unsigned int)(256.f * (j / (float)height)); |
| 127 | data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8), |
| 128 | 0xff, 0xff); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | bool does_full_buffer_contain_correct_color(GrColor* srcBuffer, |
| 134 | GrColor* dstBuffer, |
| 135 | int width, |
| 136 | int height) { |
| 137 | GrColor* srcPtr = srcBuffer; |
| 138 | GrColor* dstPtr = dstBuffer; |
| 139 | for (int j = 0; j < height; ++j) { |
| 140 | for (int i = 0; i < width; ++i) { |
| 141 | if (srcPtr[i] != dstPtr[i]) { |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | srcPtr += width; |
| 146 | dstPtr += width; |
| 147 | } |
| 148 | return true; |
| 149 | } |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 150 | |
| 151 | bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst) { |
| 152 | SkPixmap pm; |
| 153 | if (!bitmap.peekPixels(&pm)) { |
| 154 | dst->set("peekPixels failed"); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // We're going to embed this PNG in a data URI, so make it as small as possible |
| 159 | SkPngEncoder::Options options; |
| 160 | options.fFilterFlags = SkPngEncoder::FilterFlag::kAll; |
| 161 | options.fZLibLevel = 9; |
| 162 | |
| 163 | SkDynamicMemoryWStream wStream; |
| 164 | if (!SkPngEncoder::Encode(&wStream, pm, options)) { |
| 165 | dst->set("SkPngEncoder::Encode failed"); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | sk_sp<SkData> pngData = wStream.detachAsData(); |
| 170 | size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr); |
| 171 | |
| 172 | // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs. |
| 173 | // Infra says these can be pretty big, as long as we're only outputting them on failure. |
| 174 | static const size_t kMaxBase64Length = 1024 * 1024; |
| 175 | if (len > kMaxBase64Length) { |
| 176 | dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len)); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | dst->resize(len); |
| 181 | SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str()); |
| 182 | dst->prepend("data:image/png;base64,"); |
| 183 | return true; |
| 184 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 185 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 186 | #include "src/utils/SkCharToGlyphCache.h" |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 187 | |
| 188 | static SkGlyphID hash_to_glyph(uint32_t value) { |
| 189 | return SkToU16(((value >> 16) ^ value) & 0xFFFF); |
| 190 | } |
| 191 | |
| 192 | namespace { |
| 193 | class UnicharGen { |
| 194 | SkUnichar fU; |
| 195 | const int fStep; |
| 196 | public: |
| 197 | UnicharGen(int step) : fU(0), fStep(step) {} |
| 198 | |
| 199 | SkUnichar next() { |
| 200 | fU += fStep; |
| 201 | return fU; |
| 202 | } |
| 203 | }; |
| 204 | } |
| 205 | |
| 206 | DEF_TEST(chartoglyph_cache, reporter) { |
| 207 | SkCharToGlyphCache cache; |
| 208 | const int step = 3; |
| 209 | |
| 210 | UnicharGen gen(step); |
| 211 | for (int i = 0; i < 500; ++i) { |
| 212 | SkUnichar c = gen.next(); |
| 213 | SkGlyphID glyph = hash_to_glyph(c); |
| 214 | |
| 215 | int index = cache.findGlyphIndex(c); |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 216 | if (index >= 0) { |
| 217 | index = cache.findGlyphIndex(c); |
| 218 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 219 | REPORTER_ASSERT(reporter, index < 0); |
| 220 | cache.insertCharAndGlyph(~index, c, glyph); |
| 221 | |
| 222 | UnicharGen gen2(step); |
| 223 | for (int j = 0; j <= i; ++j) { |
| 224 | c = gen2.next(); |
| 225 | glyph = hash_to_glyph(c); |
| 226 | index = cache.findGlyphIndex(c); |
Mike Reed | 194cab0 | 2019-04-15 12:07:19 -0400 | [diff] [blame] | 227 | if ((unsigned)index != glyph) { |
| 228 | index = cache.findGlyphIndex(c); |
| 229 | } |
Mike Reed | 0c60708 | 2019-04-11 17:10:17 -0400 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, (unsigned)index == glyph); |
| 231 | } |
| 232 | } |
| 233 | } |