Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 11 | #include "src/core/SkAutoPixmapStorage.h" |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 12 | #include "src/core/SkCompressedDataUtils.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 13 | #include "src/core/SkMipmap.h" |
Greg Daniel | 01f278c | 2020-06-12 16:58:17 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrBackendUtils.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 16 | #include "src/image/SkImage_Base.h" |
| 17 | #include "tests/Test.h" |
| 18 | #include "tests/TestUtils.h" |
| 19 | #include "tools/ToolUtils.h" |
| 20 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 21 | // Just verify that 'actual' is entirely 'expected' |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 22 | static void check_solid_pixmap(skiatest::Reporter* reporter, |
| 23 | const SkColor4f& expected, const SkPixmap& actual, |
| 24 | const char* label0, const char* label1, const char* label2) { |
| 25 | const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f }; |
| 26 | |
| 27 | auto error = std::function<ComparePixmapsErrorReporter>( |
| 28 | [reporter, label0, label1, label2](int x, int y, const float diffs[4]) { |
| 29 | SkASSERT(x >= 0 && y >= 0); |
| 30 | ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)", |
| 31 | label0, label1, label2, x, y, |
| 32 | diffs[0], diffs[1], diffs[2], diffs[3]); |
| 33 | }); |
| 34 | |
| 35 | CheckSolidPixels(expected, actual, tols, error); |
| 36 | } |
| 37 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 38 | // Create an SkImage to wrap 'backendTex' |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 39 | sk_sp<SkImage> create_image(GrDirectContext* dContext, const GrBackendTexture& backendTex) { |
Greg Daniel | 01f278c | 2020-06-12 16:58:17 -0400 | [diff] [blame] | 40 | SkImage::CompressionType compression = |
| 41 | GrBackendFormatToCompressionType(backendTex.getBackendFormat()); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 42 | |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 43 | SkAlphaType at = SkCompressionTypeIsOpaque(compression) ? kOpaque_SkAlphaType |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 44 | : kPremul_SkAlphaType; |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 45 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 46 | return SkImage::MakeFromCompressedTexture(dContext, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 47 | backendTex, |
| 48 | kTopLeft_GrSurfaceOrigin, |
| 49 | at, |
| 50 | nullptr); |
| 51 | } |
| 52 | |
| 53 | // Draw the compressed backend texture (wrapped in an SkImage) into an RGBA surface, attempting |
| 54 | // to access all the mipMap levels. |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 55 | static void check_compressed_mipmaps(GrRecordingContext* rContext, sk_sp<SkImage> img, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 56 | SkImage::CompressionType compressionType, |
| 57 | const SkColor4f expectedColors[6], |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 58 | GrMipmapped mipMapped, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 59 | skiatest::Reporter* reporter, const char* label) { |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 60 | |
| 61 | SkImageInfo readbackSurfaceII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, |
| 62 | kPremul_SkAlphaType); |
| 63 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 64 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 65 | SkBudgeted::kNo, |
| 66 | readbackSurfaceII, 1, |
| 67 | kTopLeft_GrSurfaceOrigin, |
| 68 | nullptr); |
| 69 | if (!surf) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | SkCanvas* canvas = surf->getCanvas(); |
| 74 | |
| 75 | SkPaint p; |
Mike Reed | 8dacba3 | 2020-11-19 09:50:28 -0500 | [diff] [blame] | 76 | p.setFilterQuality(kMedium_SkFilterQuality); // to force mipMapping |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 77 | p.setBlendMode(SkBlendMode::kSrc); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 78 | |
| 79 | int numMipLevels = 1; |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 80 | if (mipMapped == GrMipmapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 81 | numMipLevels = SkMipmap::ComputeLevelCount(32, 32)+1; |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) { |
| 85 | SkASSERT(rectSize >= 1); |
| 86 | |
| 87 | canvas->clear(SK_ColorTRANSPARENT); |
| 88 | |
| 89 | SkRect r = SkRect::MakeWH(rectSize, rectSize); |
| 90 | canvas->drawImageRect(img, r, &p); |
| 91 | |
| 92 | SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize, |
| 93 | kRGBA_8888_SkColorType, |
| 94 | kUnpremul_SkAlphaType); |
| 95 | SkAutoPixmapStorage actual2; |
| 96 | SkAssertResult(actual2.tryAlloc(readbackII)); |
| 97 | actual2.erase(SkColors::kTransparent); |
| 98 | |
| 99 | bool result = surf->readPixels(actual2, 0, 0); |
| 100 | REPORTER_ASSERT(reporter, result); |
| 101 | |
| 102 | SkString str; |
| 103 | str.appendf("mip-level %d", i); |
| 104 | |
| 105 | check_solid_pixmap(reporter, expectedColors[i], actual2, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 106 | GrCompressionTypeToStr(compressionType), label, str.c_str()); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 110 | // Verify that we can readback from a compressed texture |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 111 | static void check_readback(GrDirectContext* dContext, sk_sp<SkImage> img, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 112 | SkImage::CompressionType compressionType, |
| 113 | const SkColor4f& expectedColor, |
| 114 | skiatest::Reporter* reporter, const char* label) { |
Robert Phillips | 314524e | 2020-01-30 08:38:40 -0500 | [diff] [blame] | 115 | #ifdef SK_BUILD_FOR_IOS |
| 116 | // reading back ETC2 is broken on Metal/iOS (skbug.com/9839) |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 117 | if (dContext->backend() == GrBackendApi::kMetal) { |
Robert Phillips | 314524e | 2020-01-30 08:38:40 -0500 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | #endif |
| 121 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 122 | SkAutoPixmapStorage actual; |
| 123 | |
| 124 | SkImageInfo readBackII = SkImageInfo::Make(img->width(), img->height(), |
| 125 | kRGBA_8888_SkColorType, |
| 126 | kUnpremul_SkAlphaType); |
| 127 | |
| 128 | SkAssertResult(actual.tryAlloc(readBackII)); |
| 129 | actual.erase(SkColors::kTransparent); |
| 130 | |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 131 | bool result = img->readPixels(dContext, actual, 0, 0); |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, result); |
| 133 | |
| 134 | check_solid_pixmap(reporter, expectedColor, actual, |
| 135 | GrCompressionTypeToStr(compressionType), label, ""); |
| 136 | } |
| 137 | |
| 138 | // Test initialization of compressed GrBackendTextures to a specific color |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 139 | static void test_compressed_color_init(GrDirectContext* dContext, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 140 | skiatest::Reporter* reporter, |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 141 | std::function<GrBackendTexture (GrDirectContext*, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 142 | const SkColor4f&, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 143 | GrMipmapped)> create, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 144 | const SkColor4f& color, |
| 145 | SkImage::CompressionType compression, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 146 | GrMipmapped mipMapped) { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 147 | GrBackendTexture backendTex = create(dContext, color, mipMapped); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 148 | if (!backendTex.isValid()) { |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 152 | sk_sp<SkImage> img = create_image(dContext, backendTex); |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 153 | if (!img) { |
| 154 | return; |
| 155 | } |
| 156 | |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 157 | SkColor4f expectedColors[6] = { color, color, color, color, color, color }; |
| 158 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 159 | check_compressed_mipmaps(dContext, img, compression, expectedColors, mipMapped, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 160 | reporter, "colorinit"); |
Greg Daniel | 95afafb | 2020-07-22 12:09:26 -0400 | [diff] [blame] | 161 | check_readback(dContext, img, compression, color, reporter, "solid readback"); |
| 162 | |
| 163 | SkColor4f newColor; |
| 164 | newColor.fR = color.fB; |
| 165 | newColor.fG = color.fR; |
| 166 | newColor.fB = color.fG; |
| 167 | newColor.fA = color.fA; |
| 168 | |
| 169 | bool result = dContext->updateCompressedBackendTexture(backendTex, newColor, nullptr, nullptr); |
| 170 | // Since we were able to create the compressed texture we should be able to update it. |
| 171 | REPORTER_ASSERT(reporter, result); |
| 172 | |
| 173 | SkColor4f expectedNewColors[6] = {newColor, newColor, newColor, newColor, newColor, newColor}; |
| 174 | |
| 175 | check_compressed_mipmaps(dContext, img, compression, expectedNewColors, mipMapped, reporter, |
| 176 | "colorinit"); |
| 177 | check_readback(dContext, std::move(img), compression, newColor, reporter, "solid readback"); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 178 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 179 | dContext->deleteBackendTexture(backendTex); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 182 | // Create compressed data pulling the color for each mipmap level from 'levelColors'. |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 183 | static std::unique_ptr<const char[]> make_compressed_data(SkImage::CompressionType compression, |
| 184 | SkColor4f levelColors[6], |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 185 | GrMipmapped mipMapped) { |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 186 | SkISize dimensions { 32, 32 }; |
| 187 | |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 188 | int numMipLevels = 1; |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 189 | if (mipMapped == GrMipmapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 190 | numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 193 | SkTArray<size_t> mipMapOffsets(numMipLevels); |
| 194 | |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 195 | size_t dataSize = SkCompressedDataSize(compression, dimensions, &mipMapOffsets, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 196 | mipMapped == GrMipmapped::kYes); |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 197 | char* data = new char[dataSize]; |
| 198 | |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 199 | for (int level = 0; level < numMipLevels; ++level) { |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 200 | // We have to do this a level at a time bc we might have a different color for |
| 201 | // each level |
| 202 | GrFillInCompressedData(compression, dimensions, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 203 | GrMipmapped::kNo, &data[mipMapOffsets[level]], levelColors[level]); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 204 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 205 | dimensions = {std::max(1, dimensions.width() /2), std::max(1, dimensions.height()/2)}; |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return std::unique_ptr<const char[]>(data); |
| 209 | } |
| 210 | |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 211 | // Verify that we can initialize a compressed backend texture with data (esp. |
| 212 | // the mipmap levels). |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 213 | static void test_compressed_data_init(GrDirectContext* dContext, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 214 | skiatest::Reporter* reporter, |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 215 | std::function<GrBackendTexture (GrDirectContext*, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 216 | const char* data, |
| 217 | size_t dataSize, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 218 | GrMipmapped)> create, |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 219 | SkImage::CompressionType compression, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 220 | GrMipmapped mipMapped) { |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 221 | |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 222 | SkColor4f expectedColors[6] = { |
| 223 | { 1.0f, 0.0f, 0.0f, 1.0f }, // R |
| 224 | { 0.0f, 1.0f, 0.0f, 1.0f }, // G |
| 225 | { 0.0f, 0.0f, 1.0f, 1.0f }, // B |
| 226 | { 0.0f, 1.0f, 1.0f, 1.0f }, // C |
| 227 | { 1.0f, 0.0f, 1.0f, 1.0f }, // M |
| 228 | { 1.0f, 1.0f, 0.0f, 1.0f }, // Y |
| 229 | }; |
| 230 | |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 231 | std::unique_ptr<const char[]> data(make_compressed_data(compression, expectedColors, |
| 232 | mipMapped)); |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 233 | size_t dataSize = SkCompressedDataSize(compression, { 32, 32 }, nullptr, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 234 | mipMapped == GrMipmapped::kYes); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 235 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 236 | GrBackendTexture backendTex = create(dContext, data.get(), dataSize, mipMapped); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 237 | if (!backendTex.isValid()) { |
| 238 | return; |
| 239 | } |
| 240 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 241 | sk_sp<SkImage> img = create_image(dContext, backendTex); |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 242 | if (!img) { |
| 243 | return; |
| 244 | } |
| 245 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 246 | check_compressed_mipmaps(dContext, img, compression, expectedColors, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 247 | mipMapped, reporter, "pixmap"); |
Greg Daniel | 95afafb | 2020-07-22 12:09:26 -0400 | [diff] [blame] | 248 | check_readback(dContext, img, compression, expectedColors[0], reporter, "data readback"); |
| 249 | |
| 250 | SkColor4f expectedColorsNew[6] = { |
| 251 | {1.0f, 1.0f, 0.0f, 1.0f}, // Y |
| 252 | {1.0f, 0.0f, 0.0f, 1.0f}, // R |
| 253 | {0.0f, 1.0f, 0.0f, 1.0f}, // G |
| 254 | {0.0f, 0.0f, 1.0f, 1.0f}, // B |
| 255 | {0.0f, 1.0f, 1.0f, 1.0f}, // C |
| 256 | {1.0f, 0.0f, 1.0f, 1.0f}, // M |
| 257 | }; |
| 258 | |
| 259 | std::unique_ptr<const char[]> dataNew( |
| 260 | make_compressed_data(compression, expectedColorsNew, mipMapped)); |
| 261 | size_t dataNewSize = |
| 262 | SkCompressedDataSize(compression, {32, 32}, nullptr, mipMapped == GrMipMapped::kYes); |
| 263 | |
| 264 | bool result = dContext->updateCompressedBackendTexture(backendTex, dataNew.get(), dataNewSize, |
| 265 | nullptr, nullptr); |
| 266 | // Since we were able to create the compressed texture we should be able to update it. |
| 267 | REPORTER_ASSERT(reporter, result); |
| 268 | |
| 269 | check_compressed_mipmaps(dContext, img, compression, expectedColorsNew, mipMapped, reporter, |
| 270 | "pixmap"); |
| 271 | check_readback(dContext, std::move(img), compression, expectedColorsNew[0], reporter, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 272 | "data readback"); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 273 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 274 | dContext->deleteBackendTexture(backendTex); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CompressedBackendAllocationTest, reporter, ctxInfo) { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 278 | auto dContext = ctxInfo.directContext(); |
| 279 | const GrCaps* caps = dContext->priv().caps(); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 280 | |
| 281 | struct { |
| 282 | SkImage::CompressionType fCompression; |
| 283 | SkColor4f fColor; |
| 284 | } combinations[] = { |
Robert Phillips | c558f72 | 2020-01-13 13:02:26 -0500 | [diff] [blame] | 285 | { SkImage::CompressionType::kETC2_RGB8_UNORM, SkColors::kRed }, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 286 | { SkImage::CompressionType::kBC1_RGB8_UNORM, SkColors::kBlue }, |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 287 | { SkImage::CompressionType::kBC1_RGBA8_UNORM, SkColors::kTransparent }, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | for (auto combo : combinations) { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 291 | GrBackendFormat format = dContext->compressedBackendFormat(combo.fCompression); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 292 | if (!format.isValid()) { |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | if (!caps->isFormatTexturable(format)) { |
| 297 | continue; |
| 298 | } |
| 299 | |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 300 | for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) { |
Brian Salomon | 69100f0 | 2020-07-21 10:49:25 -0400 | [diff] [blame] | 301 | if (GrMipmapped::kYes == mipMapped && !caps->mipmapSupport()) { |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 302 | continue; |
| 303 | } |
| 304 | |
| 305 | // color initialized |
| 306 | { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 307 | auto createWithColorMtd = [format](GrDirectContext* dContext, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 308 | const SkColor4f& color, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 309 | GrMipmapped mipMapped) { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 310 | return dContext->createCompressedBackendTexture(32, 32, format, color, |
| 311 | mipMapped, GrProtected::kNo); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 312 | }; |
| 313 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 314 | test_compressed_color_init(dContext, reporter, createWithColorMtd, |
Robert Phillips | 07f0e41 | 2020-01-17 15:20:00 -0500 | [diff] [blame] | 315 | combo.fColor, combo.fCompression, mipMapped); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // data initialized |
| 319 | { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 320 | auto createWithDataMtd = [format](GrDirectContext* dContext, |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 321 | const char* data, size_t dataSize, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 322 | GrMipmapped mipMapped) { |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 323 | return dContext->createCompressedBackendTexture(32, 32, format, data, dataSize, |
| 324 | mipMapped, GrProtected::kNo); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 325 | }; |
| 326 | |
Robert Phillips | c8ae494 | 2020-07-20 10:56:01 -0400 | [diff] [blame] | 327 | test_compressed_data_init(dContext, reporter, createWithDataMtd, |
Robert Phillips | ca0f837 | 2019-12-20 09:57:41 -0500 | [diff] [blame] | 328 | combo.fCompression, mipMapped); |
Robert Phillips | 3e5e2f2 | 2019-12-19 11:19:16 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | } |
| 332 | } |
| 333 | } |