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 | |
| 8 | #include "TestUtils.h" |
| 9 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 10 | #include "GrProxyProvider.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 11 | #include "GrSurfaceContext.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 12 | #include "GrSurfaceContextPriv.h" |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 13 | #include "GrSurfaceProxy.h" |
Mike Reed | 84dd857 | 2017-03-08 22:21:00 -0500 | [diff] [blame] | 14 | #include "GrTextureProxy.h" |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 15 | #include "ProxyUtils.h" |
Brian Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 16 | #include "SkGr.h" |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 17 | #include "SkBase64.h" |
| 18 | #include "SkPngEncoder.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 | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 97 | void test_copy_to_surface(skiatest::Reporter* reporter, GrProxyProvider* proxyProvider, |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 98 | GrSurfaceContext* dstContext, const char* testName) { |
| 99 | |
| 100 | int pixelCnt = dstContext->width() * dstContext->height(); |
| 101 | SkAutoTMalloc<uint32_t> pixels(pixelCnt); |
| 102 | for (int y = 0; y < dstContext->width(); ++y) { |
| 103 | for (int x = 0; x < dstContext->height(); ++x) { |
| 104 | pixels.get()[y * dstContext->width() + x] = |
Brian Osman | 4408b1c | 2018-10-29 14:11:04 -0400 | [diff] [blame] | 105 | SkColorToPremulGrColor(SkColorSetARGB(2*y, y, x, x * y)); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 109 | for (auto isRT : {false, true}) { |
| 110 | for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) { |
| 111 | auto src = sk_gpu_test::MakeTextureProxyFromData( |
| 112 | dstContext->surfPriv().getContext(), isRT, dstContext->width(), |
| 113 | dstContext->height(), GrColorType::kRGBA_8888, origin, pixels.get(), 0); |
| 114 | dstContext->copy(src.get()); |
| 115 | test_read_pixels(reporter, dstContext, pixels.get(), testName); |
| 116 | } |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 119 | |
| 120 | void fill_pixel_data(int width, int height, GrColor* data) { |
| 121 | for (int j = 0; j < height; ++j) { |
| 122 | for (int i = 0; i < width; ++i) { |
| 123 | unsigned int red = (unsigned int)(256.f * (i / (float)width)); |
| 124 | unsigned int green = (unsigned int)(256.f * (j / (float)height)); |
| 125 | data[i + j * width] = GrColorPackRGBA(red - (red >> 8), green - (green >> 8), |
| 126 | 0xff, 0xff); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | bool does_full_buffer_contain_correct_color(GrColor* srcBuffer, |
| 132 | GrColor* dstBuffer, |
| 133 | int width, |
| 134 | int height) { |
| 135 | GrColor* srcPtr = srcBuffer; |
| 136 | GrColor* dstPtr = dstBuffer; |
| 137 | for (int j = 0; j < height; ++j) { |
| 138 | for (int i = 0; i < width; ++i) { |
| 139 | if (srcPtr[i] != dstPtr[i]) { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | srcPtr += width; |
| 144 | dstPtr += width; |
| 145 | } |
| 146 | return true; |
| 147 | } |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 148 | |
| 149 | bool bitmap_to_base64_data_uri(const SkBitmap& bitmap, SkString* dst) { |
| 150 | SkPixmap pm; |
| 151 | if (!bitmap.peekPixels(&pm)) { |
| 152 | dst->set("peekPixels failed"); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | // We're going to embed this PNG in a data URI, so make it as small as possible |
| 157 | SkPngEncoder::Options options; |
| 158 | options.fFilterFlags = SkPngEncoder::FilterFlag::kAll; |
| 159 | options.fZLibLevel = 9; |
| 160 | |
| 161 | SkDynamicMemoryWStream wStream; |
| 162 | if (!SkPngEncoder::Encode(&wStream, pm, options)) { |
| 163 | dst->set("SkPngEncoder::Encode failed"); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | sk_sp<SkData> pngData = wStream.detachAsData(); |
| 168 | size_t len = SkBase64::Encode(pngData->data(), pngData->size(), nullptr); |
| 169 | |
| 170 | // The PNG can be almost arbitrarily large. We don't want to fill our logs with enormous URLs. |
| 171 | // Infra says these can be pretty big, as long as we're only outputting them on failure. |
| 172 | static const size_t kMaxBase64Length = 1024 * 1024; |
| 173 | if (len > kMaxBase64Length) { |
| 174 | dst->printf("Encoded image too large (%u bytes)", static_cast<uint32_t>(len)); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | dst->resize(len); |
| 179 | SkBase64::Encode(pngData->data(), pngData->size(), dst->writable_str()); |
| 180 | dst->prepend("data:image/png;base64,"); |
| 181 | return true; |
| 182 | } |