Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [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 | |
Mike Klein | fe0aeb3 | 2019-05-20 10:55:11 -0500 | [diff] [blame] | 8 | #include "include/core/SkSurface.h" |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 9 | #include "include/core/SkSurfaceCharacterization.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContext.h" |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 11 | #include "src/core/SkAutoPixmapStorage.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 13 | #include "src/image/SkImage_Base.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 14 | #include "tests/Test.h" |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 15 | #include "tests/TestUtils.h" |
| 16 | #include "tools/ToolUtils.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 17 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 18 | #ifdef SK_GL |
| 19 | #include "src/gpu/gl/GrGLGpu.h" |
| 20 | #include "src/gpu/gl/GrGLUtil.h" |
| 21 | #endif |
| 22 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 23 | #ifdef SK_METAL |
| 24 | #include "include/gpu/mtl/GrMtlTypes.h" |
| 25 | #include "src/gpu/mtl/GrMtlCppUtil.h" |
| 26 | #endif |
| 27 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 28 | // Test wrapping of GrBackendObjects in SkSurfaces and SkImages (non-static since used in Mtl test) |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 29 | void test_wrapping(GrContext* context, skiatest::Reporter* reporter, |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 30 | std::function<GrBackendTexture (GrContext*, |
| 31 | GrMipMapped, |
| 32 | GrRenderable)> create, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 33 | GrColorType grColorType, GrMipMapped mipMapped, GrRenderable renderable) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 34 | GrResourceCache* cache = context->priv().getResourceCache(); |
| 35 | |
| 36 | const int initialCount = cache->getResourceCount(); |
| 37 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 38 | GrBackendTexture backendTex = create(context, mipMapped, renderable); |
| 39 | if (!backendTex.isValid()) { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 40 | ERRORF(reporter, "Couldn't create backendTexture for grColorType %d renderable %s\n", |
| 41 | grColorType, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 42 | GrRenderable::kYes == renderable ? "yes" : "no"); |
| 43 | return; |
| 44 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 45 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 46 | // Skia proper should know nothing about the new backend object |
| 47 | REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount()); |
| 48 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 49 | SkColorType skColorType = GrColorTypeToSkColorType(grColorType); |
| 50 | |
| 51 | // Wrapping a backendTexture in an image requires an SkColorType |
| 52 | if (kUnknown_SkColorType == skColorType) { |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 53 | context->deleteBackendTexture(backendTex); |
| 54 | return; |
| 55 | } |
| 56 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 57 | if (GrRenderable::kYes == renderable && context->colorTypeSupportedAsSurface(skColorType)) { |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 58 | sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 59 | backendTex, |
| 60 | kTopLeft_GrSurfaceOrigin, |
| 61 | 0, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 62 | skColorType, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 63 | nullptr, nullptr); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 64 | if (!surf) { |
| 65 | ERRORF(reporter, "Couldn't make surface from backendTexture for colorType %d\n", |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 66 | skColorType); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 67 | } else { |
| 68 | REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 69 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 70 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 71 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 72 | { |
| 73 | sk_sp<SkImage> img = SkImage::MakeFromTexture(context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 74 | backendTex, |
| 75 | kTopLeft_GrSurfaceOrigin, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 76 | skColorType, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 77 | kPremul_SkAlphaType, |
| 78 | nullptr); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 79 | if (!img) { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 80 | ERRORF(reporter, "Couldn't make image from backendTexture for skColorType %d\n", |
| 81 | skColorType); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 82 | } else { |
| 83 | SkImage_Base* ib = as_IB(img); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 84 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 85 | GrTextureProxy* proxy = ib->peekProxy(); |
| 86 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 87 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, mipMapped == proxy->proxyMipMapped()); |
| 89 | REPORTER_ASSERT(reporter, proxy->isInstantiated()); |
| 90 | REPORTER_ASSERT(reporter, mipMapped == proxy->mipMapped()); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 91 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount()); |
| 97 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 98 | context->deleteBackendTexture(backendTex); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 101 | static bool isBGRA(const GrBackendFormat& format) { |
| 102 | switch (format.backend()) { |
| 103 | case GrBackendApi::kMetal: |
| 104 | #ifdef SK_METAL |
| 105 | return GrMtlFormatIsBGRA(format.asMtlFormat()); |
| 106 | #else |
| 107 | return false; |
| 108 | #endif |
| 109 | case GrBackendApi::kDawn: |
| 110 | return false; |
| 111 | case GrBackendApi::kOpenGL: |
| 112 | #ifdef SK_GL |
| 113 | return format.asGLFormat() == GrGLFormat::kBGRA8; |
| 114 | #else |
| 115 | return false; |
| 116 | #endif |
| 117 | case GrBackendApi::kVulkan: { |
| 118 | #ifdef SK_VULKAN |
| 119 | VkFormat vkFormat; |
| 120 | format.asVkFormat(&vkFormat); |
| 121 | return vkFormat == VK_FORMAT_B8G8R8A8_UNORM; |
| 122 | #else |
| 123 | return false; |
| 124 | #endif |
| 125 | } |
| 126 | case GrBackendApi::kMock: |
| 127 | return format.asMockColorType() == GrColorType::kBGRA_8888; |
| 128 | } |
| 129 | SkUNREACHABLE; |
| 130 | } |
| 131 | |
| 132 | static bool isRGB(const GrBackendFormat& format) { |
| 133 | switch (format.backend()) { |
| 134 | case GrBackendApi::kMetal: |
| 135 | return false; // Metal doesn't even pretend to support this |
| 136 | case GrBackendApi::kDawn: |
| 137 | return false; |
| 138 | case GrBackendApi::kOpenGL: |
| 139 | #ifdef SK_GL |
| 140 | return format.asGLFormat() == GrGLFormat::kRGB8; |
| 141 | #else |
| 142 | return false; |
| 143 | #endif |
| 144 | case GrBackendApi::kVulkan: { |
| 145 | #ifdef SK_VULKAN |
| 146 | VkFormat vkFormat; |
| 147 | format.asVkFormat(&vkFormat); |
| 148 | return vkFormat == VK_FORMAT_R8G8B8_UNORM; |
| 149 | #else |
| 150 | return false; |
| 151 | #endif |
| 152 | } |
| 153 | case GrBackendApi::kMock: |
| 154 | return false; // No GrColorType::kRGB_888 |
| 155 | } |
| 156 | SkUNREACHABLE; |
| 157 | } |
| 158 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 159 | static void check_solid_pixmap(skiatest::Reporter* reporter, |
| 160 | const SkColor4f& expected, const SkPixmap& actual, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 161 | SkColorType ct, const char* label1, const char* label2) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 162 | // we need 0.001f across the board just for noise |
| 163 | // we need 0.01f across the board for 1010102 |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 164 | const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f }; |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 165 | |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 166 | auto error = std::function<ComparePixmapsErrorReporter>( |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 167 | [reporter, ct, label1, label2](int x, int y, const float diffs[4]) { |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 168 | SkASSERT(x >= 0 && y >= 0); |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 169 | ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)", |
| 170 | ToolUtils::colortype_name(ct), label1, label2, x, y, |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 171 | diffs[0], diffs[1], diffs[2], diffs[3]); |
| 172 | }); |
| 173 | |
| 174 | check_solid_pixels(expected, actual, tols, error); |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 177 | // What would raster do? |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 178 | static SkColor4f get_expected_color(SkColor4f orig, SkColorType ct) { |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 179 | if (ct == kGray_8_SkColorType) { |
| 180 | // Prevent the premul raster graciously applies in this case |
| 181 | orig.fA = 1.0; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 182 | } |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 183 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 184 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType |
| 185 | : kPremul_SkAlphaType; |
Robert Phillips | e3b6fe4 | 2019-09-11 11:26:46 -0400 | [diff] [blame] | 186 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 187 | SkImageInfo ii = SkImageInfo::Make(2, 2, ct, at); |
| 188 | SkAutoPixmapStorage pm; |
| 189 | pm.alloc(ii); |
| 190 | pm.erase(orig); |
| 191 | SkColor tmp = pm.getColor(0, 0); |
| 192 | return SkColor4f::FromColor(tmp); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 195 | static void check_mipmaps(GrContext* context, const GrBackendTexture& backendTex, |
| 196 | SkColorType skColorType, const SkColor4f expectedColors[6], |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 197 | skiatest::Reporter* reporter, const char* label); |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 198 | |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 199 | static void check_base_readbacks(GrContext* context, const GrBackendTexture& backendTex, |
| 200 | SkColorType skColorType, GrRenderable renderable, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 201 | const SkColor4f& color, skiatest::Reporter* reporter, |
| 202 | const char* label) { |
| 203 | if (isRGB(backendTex.getBackendFormat())) { |
| 204 | // readPixels is busted for the RGB backend format (skbug.com/8862) |
| 205 | // TODO: add a GrColorType::kRGB_888 to fix the situation |
| 206 | return; |
| 207 | } |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 208 | |
| 209 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType |
| 210 | : kPremul_SkAlphaType; |
| 211 | |
| 212 | SkColor4f expectedColor = get_expected_color(color, skColorType); |
| 213 | |
| 214 | SkAutoPixmapStorage actual; |
| 215 | |
| 216 | { |
| 217 | SkImageInfo readBackII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 218 | kUnpremul_SkAlphaType); |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 219 | |
| 220 | SkAssertResult(actual.tryAlloc(readBackII)); |
| 221 | } |
| 222 | |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 223 | { |
| 224 | sk_sp<SkImage> img = SkImage::MakeFromTexture(context, |
| 225 | backendTex, |
| 226 | kTopLeft_GrSurfaceOrigin, |
| 227 | skColorType, |
| 228 | at, |
| 229 | nullptr); |
| 230 | if (img) { |
| 231 | actual.erase(SkColors::kTransparent); |
| 232 | bool result = img->readPixels(actual, 0, 0); |
| 233 | if (!result) { |
| 234 | // TODO: we need a better way to tell a priori if readPixels will work for an |
| 235 | // arbitrary colorType |
| 236 | #if 0 |
| 237 | ERRORF(reporter, "Couldn't readback from SkImage for colorType: %d\n", colorType); |
| 238 | #endif |
| 239 | } else { |
| 240 | check_solid_pixmap(reporter, expectedColor, actual, skColorType, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 241 | label, "SkImage::readPixels"); |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 245 | |
| 246 | // This will mark any mipmaps as dirty (bc that is what we do when we wrap a renderable |
| 247 | // backend texture) so it must be done last! |
| 248 | if (GrRenderable::kYes == renderable && context->colorTypeSupportedAsSurface(skColorType)) { |
| 249 | sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context, |
| 250 | backendTex, |
| 251 | kTopLeft_GrSurfaceOrigin, |
| 252 | 0, |
| 253 | skColorType, |
| 254 | nullptr, nullptr); |
| 255 | if (surf) { |
| 256 | actual.erase(SkColors::kTransparent); |
| 257 | bool result = surf->readPixels(actual, 0, 0); |
| 258 | REPORTER_ASSERT(reporter, result); |
| 259 | |
| 260 | check_solid_pixmap(reporter, expectedColor, actual, skColorType, |
| 261 | label, "SkSurface::readPixels"); |
| 262 | } |
| 263 | } |
Robert Phillips | 0ee1034 | 2019-09-25 09:55:16 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 266 | // Test initialization of GrBackendObjects to a specific color (non-static since used in Mtl test) |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 267 | void test_color_init(GrContext* context, skiatest::Reporter* reporter, |
| 268 | std::function<GrBackendTexture (GrContext*, |
| 269 | const SkColor4f&, |
| 270 | GrMipMapped, |
| 271 | GrRenderable)> create, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 272 | GrColorType grColorType, const SkColor4f& color, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 273 | GrMipMapped mipMapped, GrRenderable renderable) { |
| 274 | GrBackendTexture backendTex = create(context, color, mipMapped, renderable); |
| 275 | if (!backendTex.isValid()) { |
| 276 | // errors here should be reported by the test_wrapping test |
| 277 | return; |
| 278 | } |
| 279 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 280 | SkColorType skColorType = GrColorTypeToSkColorType(grColorType); |
| 281 | |
| 282 | // Can't wrap backend textures in images and surfaces w/o an SkColorType |
| 283 | if (kUnknown_SkColorType == skColorType) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 284 | // TODO: burrow in and scrappily check that data was uploaded! |
| 285 | context->deleteBackendTexture(backendTex); |
| 286 | return; |
| 287 | } |
| 288 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 289 | if (mipMapped == GrMipMapped::kYes) { |
| 290 | SkColor4f expectedColor = get_expected_color(color, skColorType); |
| 291 | SkColor4f expectedColors[6] = { expectedColor, expectedColor, expectedColor, |
| 292 | expectedColor, expectedColor, expectedColor }; |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 293 | check_mipmaps(context, backendTex, skColorType, expectedColors, reporter, "colorinit"); |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 296 | // The last step in this test will dirty the mipmaps so do it last |
| 297 | check_base_readbacks(context, backendTex, skColorType, renderable, color, |
| 298 | reporter, "colorinit"); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 299 | context->deleteBackendTexture(backendTex); |
| 300 | } |
| 301 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 302 | // Draw the backend texture (wrapped in an SkImage) into an RGBA surface, attempting to access |
| 303 | // all the mipMap levels. |
| 304 | static void check_mipmaps(GrContext* context, const GrBackendTexture& backendTex, |
| 305 | SkColorType skColorType, const SkColor4f expectedColors[6], |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 306 | skiatest::Reporter* reporter, const char* label) { |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 307 | |
| 308 | #ifdef SK_GL |
| 309 | // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices) |
| 310 | if (GrBackendApi::kOpenGL == context->backend()) { |
| 311 | GrGLGpu* glGPU = static_cast<GrGLGpu*>(context->priv().getGpu()); |
| 312 | |
| 313 | if (kRGBA_F32_SkColorType == skColorType && |
| 314 | kGLES_GrGLStandard == glGPU->ctxInfo().standard()) { |
| 315 | return; |
| 316 | } |
| 317 | } |
| 318 | #endif |
| 319 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 320 | if (isRGB(backendTex.getBackendFormat())) { |
| 321 | // readPixels is busted for the RGB backend format (skbug.com/8862) |
| 322 | // TODO: add a GrColorType::kRGB_888 to fix the situation |
| 323 | return; |
| 324 | } |
| 325 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 326 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType |
| 327 | : kPremul_SkAlphaType; |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 328 | |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 329 | sk_sp<SkImage> img = SkImage::MakeFromTexture(context, |
| 330 | backendTex, |
| 331 | kTopLeft_GrSurfaceOrigin, |
| 332 | skColorType, |
| 333 | at, |
| 334 | nullptr); |
| 335 | if (!img) { |
| 336 | return; |
| 337 | } |
| 338 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 339 | SkImageInfo readbackSurfaceII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, |
| 340 | kPremul_SkAlphaType); |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 341 | |
| 342 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, |
| 343 | SkBudgeted::kNo, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 344 | readbackSurfaceII, 1, |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 345 | kTopLeft_GrSurfaceOrigin, |
| 346 | nullptr); |
| 347 | if (!surf) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | SkCanvas* canvas = surf->getCanvas(); |
| 352 | |
| 353 | SkPaint p; |
| 354 | p.setFilterQuality(kHigh_SkFilterQuality); |
| 355 | |
| 356 | int numMipLevels = 6; |
| 357 | |
| 358 | for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) { |
| 359 | SkASSERT(rectSize >= 1); |
| 360 | |
| 361 | SkRect r = SkRect::MakeWH(rectSize, rectSize); |
| 362 | canvas->clear(SK_ColorTRANSPARENT); |
| 363 | canvas->drawImageRect(img, r, &p); |
| 364 | |
| 365 | SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize, |
| 366 | kRGBA_8888_SkColorType, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 367 | kUnpremul_SkAlphaType); |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 368 | SkAutoPixmapStorage actual2; |
| 369 | SkAssertResult(actual2.tryAlloc(readbackII)); |
| 370 | actual2.erase(SkColors::kTransparent); |
| 371 | |
| 372 | bool result = surf->readPixels(actual2, 0, 0); |
| 373 | REPORTER_ASSERT(reporter, result); |
| 374 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 375 | check_solid_pixmap(reporter, expectedColors[i], actual2, skColorType, |
| 376 | label, "mip-level failure"); |
Robert Phillips | c1dbb4b | 2019-09-24 16:32:35 -0400 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 380 | static int make_pixmaps(SkColorType skColorType, GrMipMapped mipMapped, |
| 381 | const SkColor4f colors[6], SkAutoPixmapStorage pixmaps[6]) { |
| 382 | int levelSize = 32; |
| 383 | int numMipLevels = mipMapped == GrMipMapped::kYes ? 6 : 1; |
| 384 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType |
| 385 | : kPremul_SkAlphaType; |
| 386 | for (int level = 0; level < numMipLevels; ++level) { |
| 387 | SkImageInfo ii = SkImageInfo::Make(levelSize, levelSize, skColorType, at); |
| 388 | pixmaps[level].alloc(ii); |
| 389 | pixmaps[level].erase(colors[level]); |
| 390 | levelSize /= 2; |
| 391 | } |
| 392 | return numMipLevels; |
| 393 | } |
| 394 | |
| 395 | // Test initialization of GrBackendObjects using SkPixmaps |
| 396 | static void test_pixmap_init(GrContext* context, skiatest::Reporter* reporter, |
| 397 | std::function<GrBackendTexture (GrContext*, |
| 398 | const SkPixmap srcData[], |
| 399 | int numLevels, |
| 400 | GrRenderable)> create, |
| 401 | SkColorType skColorType, GrMipMapped mipMapped, |
| 402 | GrRenderable renderable) { |
| 403 | SkAutoPixmapStorage pixmapMem[6]; |
| 404 | SkColor4f colors[6] = { |
| 405 | { 1.0f, 0.0f, 0.0f, 1.0f }, // R |
| 406 | { 0.0f, 1.0f, 0.0f, 0.9f }, // G |
| 407 | { 0.0f, 0.0f, 1.0f, 0.7f }, // B |
| 408 | { 0.0f, 1.0f, 1.0f, 0.5f }, // C |
| 409 | { 1.0f, 0.0f, 1.0f, 0.3f }, // M |
| 410 | { 1.0f, 1.0f, 0.0f, 0.2f }, // Y |
| 411 | }; |
| 412 | |
| 413 | int numMipLevels = make_pixmaps(skColorType, mipMapped, colors, pixmapMem); |
| 414 | SkASSERT(numMipLevels); |
| 415 | |
| 416 | // TODO: this is tedious. Should we pass in an array of SkBitmaps instead? |
| 417 | SkPixmap pixmaps[6]; |
| 418 | for (int i = 0; i < numMipLevels; ++i) { |
| 419 | pixmaps[i].reset(pixmapMem[i].info(), pixmapMem[i].addr(), pixmapMem[i].rowBytes()); |
| 420 | } |
| 421 | |
| 422 | GrBackendTexture backendTex = create(context, pixmaps, numMipLevels, renderable); |
| 423 | if (!backendTex.isValid()) { |
| 424 | // errors here should be reported by the test_wrapping test |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | if (skColorType == kBGRA_8888_SkColorType && !isBGRA(backendTex.getBackendFormat())) { |
| 429 | // When kBGRA is backed by an RGBA something goes wrong in the swizzling |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | if (mipMapped == GrMipMapped::kYes) { |
| 434 | SkColor4f expectedColors[6] = { |
| 435 | get_expected_color(colors[0], skColorType), |
| 436 | get_expected_color(colors[1], skColorType), |
| 437 | get_expected_color(colors[2], skColorType), |
| 438 | get_expected_color(colors[3], skColorType), |
| 439 | get_expected_color(colors[4], skColorType), |
| 440 | get_expected_color(colors[5], skColorType), |
| 441 | }; |
| 442 | |
| 443 | check_mipmaps(context, backendTex, skColorType, expectedColors, reporter, "pixmap"); |
| 444 | } |
| 445 | |
| 446 | // The last step in this test will dirty the mipmaps so do it last |
| 447 | check_base_readbacks(context, backendTex, skColorType, renderable, colors[0], |
| 448 | reporter, "pixmap"); |
| 449 | context->deleteBackendTexture(backendTex); |
| 450 | } |
| 451 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 452 | enum class VkLayout { |
| 453 | kUndefined, |
| 454 | kReadOnlyOptimal, |
| 455 | kColorAttachmentOptimal |
| 456 | }; |
| 457 | |
| 458 | void check_vk_layout(const GrBackendTexture& backendTex, VkLayout layout) { |
| 459 | #if defined(SK_VULKAN) && defined(SK_DEBUG) |
| 460 | VkImageLayout expected; |
| 461 | |
| 462 | switch (layout) { |
| 463 | case VkLayout::kUndefined: |
| 464 | expected = VK_IMAGE_LAYOUT_UNDEFINED; |
| 465 | break; |
| 466 | case VkLayout::kReadOnlyOptimal: |
| 467 | expected = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 468 | break; |
| 469 | case VkLayout::kColorAttachmentOptimal: |
| 470 | expected = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 471 | break; |
| 472 | default: |
| 473 | SkUNREACHABLE; |
| 474 | } |
| 475 | |
| 476 | GrVkImageInfo vkII; |
| 477 | |
| 478 | if (backendTex.getVkImageInfo(&vkII)) { |
| 479 | SkASSERT(expected == vkII.fImageLayout); |
| 480 | SkASSERT(VK_IMAGE_TILING_OPTIMAL == vkII.fImageTiling); |
| 481 | } |
| 482 | #endif |
| 483 | } |
| 484 | |
| 485 | /////////////////////////////////////////////////////////////////////////////// |
| 486 | // This test is a bit different from the others in this file. It is mainly checking that, for any |
| 487 | // SkSurface we can create in Ganesh, we can also create a backend texture that is compatible with |
| 488 | // its characterization and then create a new surface that wraps that backend texture. |
| 489 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CharacterizationBackendAllocationTest, reporter, ctxInfo) { |
| 490 | GrContext* context = ctxInfo.grContext(); |
| 491 | |
| 492 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 493 | SkColorType colorType = static_cast<SkColorType>(ct); |
| 494 | |
| 495 | SkImageInfo ii = SkImageInfo::Make(32, 32, colorType, kPremul_SkAlphaType); |
| 496 | |
| 497 | for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) { |
| 498 | for (bool mipMaps : { true, false } ) { |
| 499 | for (int sampleCount : {1, 2}) { |
| 500 | SkSurfaceCharacterization c; |
| 501 | |
| 502 | // Get a characterization, if possible |
| 503 | { |
| 504 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, |
| 505 | ii, sampleCount, |
| 506 | origin, nullptr, mipMaps); |
| 507 | if (!s) { |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | if (!s->characterize(&c)) { |
| 512 | continue; |
| 513 | } |
| 514 | |
| 515 | REPORTER_ASSERT(reporter, s->isCompatible(c)); |
| 516 | } |
| 517 | |
| 518 | // Test out uninitialized path |
| 519 | { |
| 520 | GrBackendTexture backendTex = context->createBackendTexture(c); |
| 521 | check_vk_layout(backendTex, VkLayout::kUndefined); |
| 522 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 523 | REPORTER_ASSERT(reporter, c.isCompatible(backendTex)); |
| 524 | |
Robert Phillips | d5e80ca | 2019-07-29 14:11:35 -0400 | [diff] [blame] | 525 | { |
| 526 | GrBackendFormat format = context->defaultBackendFormat( |
| 527 | c.imageInfo().colorType(), |
| 528 | GrRenderable::kYes); |
| 529 | REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat()); |
| 530 | } |
| 531 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 532 | sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c, |
| 533 | backendTex); |
| 534 | REPORTER_ASSERT(reporter, s2); |
| 535 | REPORTER_ASSERT(reporter, s2->isCompatible(c)); |
| 536 | |
| 537 | s2 = nullptr; |
| 538 | context->deleteBackendTexture(backendTex); |
| 539 | } |
| 540 | |
| 541 | // Test out color-initialized path |
| 542 | { |
| 543 | GrBackendTexture backendTex = context->createBackendTexture(c, |
| 544 | SkColors::kRed); |
| 545 | check_vk_layout(backendTex, VkLayout::kColorAttachmentOptimal); |
| 546 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 547 | REPORTER_ASSERT(reporter, c.isCompatible(backendTex)); |
| 548 | |
Robert Phillips | d5e80ca | 2019-07-29 14:11:35 -0400 | [diff] [blame] | 549 | { |
| 550 | GrBackendFormat format = context->defaultBackendFormat( |
| 551 | c.imageInfo().colorType(), |
| 552 | GrRenderable::kYes); |
| 553 | REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat()); |
| 554 | } |
| 555 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 556 | sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c, |
| 557 | backendTex); |
| 558 | REPORTER_ASSERT(reporter, s2); |
| 559 | REPORTER_ASSERT(reporter, s2->isCompatible(c)); |
| 560 | |
| 561 | s2 = nullptr; |
| 562 | context->deleteBackendTexture(backendTex); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 570 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 571 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ColorTypeBackendAllocationTest, reporter, ctxInfo) { |
| 572 | GrContext* context = ctxInfo.grContext(); |
| 573 | const GrCaps* caps = context->priv().caps(); |
| 574 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 575 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 576 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 577 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 578 | struct { |
| 579 | SkColorType fColorType; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 580 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 581 | } combinations[] = { |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 582 | { kAlpha_8_SkColorType, kTransCol }, |
| 583 | { kRGB_565_SkColorType, SkColors::kRed }, |
| 584 | { kARGB_4444_SkColorType, SkColors::kGreen }, |
| 585 | { kRGBA_8888_SkColorType, SkColors::kBlue }, |
| 586 | { kRGB_888x_SkColorType, SkColors::kCyan }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 587 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 588 | { kBGRA_8888_SkColorType, { 1, 0, 0, 1.0f } }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 589 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 590 | { kRGBA_1010102_SkColorType, { .25f, .5f, .75f, 1.0f }}, |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 591 | // The kRGB_101010x_SkColorType has no Ganesh correlate |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 592 | { kRGB_101010x_SkColorType, { 0, 0.5f, 0, 0.5f } }, |
| 593 | { kGray_8_SkColorType, kGrayCol }, |
| 594 | { kRGBA_F16Norm_SkColorType, SkColors::kLtGray }, |
| 595 | { kRGBA_F16_SkColorType, SkColors::kYellow }, |
| 596 | { kRGBA_F32_SkColorType, SkColors::kGray }, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 597 | { kR8G8_unorm_SkColorType, { .25f, .75f, 0, 1 } }, |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 598 | { kR16G16_unorm_SkColorType, SkColors::kGreen }, |
| 599 | { kA16_unorm_SkColorType, kTransCol }, |
| 600 | { kA16_float_SkColorType, kTransCol }, |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 601 | { kR16G16_float_SkColorType, { .25f, .75f, 0, 1 } }, |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 602 | { kR16G16B16A16_unorm_SkColorType,{ .25f, .5f, .75f, 1 } }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 603 | }; |
| 604 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 605 | GR_STATIC_ASSERT(kLastEnum_SkColorType == SK_ARRAY_COUNT(combinations)); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 606 | |
| 607 | for (auto combo : combinations) { |
| 608 | SkColorType colorType = combo.fColorType; |
| 609 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 610 | if (GrBackendApi::kMetal == context->backend()) { |
| 611 | // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly) |
| 612 | if (kRGBA_F32_SkColorType == combo.fColorType) { |
| 613 | continue; |
| 614 | } |
| 615 | } |
| 616 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 617 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 618 | if (GrMipMapped::kYes == mipMapped && !caps->mipMapSupport()) { |
| 619 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 622 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 623 | if (!caps->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType), |
| 624 | renderable).isValid()) { |
| 625 | continue; |
| 626 | } |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 627 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 628 | if (GrRenderable::kYes == renderable) { |
| 629 | if (kRGB_888x_SkColorType == combo.fColorType) { |
| 630 | // Ganesh can't perform the blends correctly when rendering this format |
| 631 | continue; |
| 632 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 633 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 634 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 635 | { |
| 636 | auto uninitCreateMtd = [colorType](GrContext* context, |
| 637 | GrMipMapped mipMapped, |
| 638 | GrRenderable renderable) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 639 | auto result = context->createBackendTexture(32, 32, colorType, |
| 640 | mipMapped, renderable, |
| 641 | GrProtected::kNo); |
| 642 | check_vk_layout(result, VkLayout::kUndefined); |
Robert Phillips | d5e80ca | 2019-07-29 14:11:35 -0400 | [diff] [blame] | 643 | |
| 644 | #ifdef SK_DEBUG |
| 645 | { |
| 646 | GrBackendFormat format = context->defaultBackendFormat(colorType, |
| 647 | renderable); |
| 648 | SkASSERT(format == result.getBackendFormat()); |
| 649 | } |
| 650 | #endif |
| 651 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 652 | return result; |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 653 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 654 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 655 | test_wrapping(context, reporter, uninitCreateMtd, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 656 | SkColorTypeToGrColorType(colorType), mipMapped, renderable); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 657 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 658 | |
| 659 | { |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 660 | // GL has difficulties reading back from these combinations. In particular, |
| 661 | // reading back kGray_8 is a mess. |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 662 | if (GrBackendApi::kOpenGL == context->backend()) { |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 663 | if (kAlpha_8_SkColorType == combo.fColorType || |
| 664 | kGray_8_SkColorType == combo.fColorType) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 665 | continue; |
| 666 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | auto createWithColorMtd = [colorType](GrContext* context, |
| 670 | const SkColor4f& color, |
| 671 | GrMipMapped mipMapped, |
| 672 | GrRenderable renderable) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 673 | auto result = context->createBackendTexture(32, 32, colorType, color, |
| 674 | mipMapped, renderable, |
| 675 | GrProtected::kNo); |
| 676 | check_vk_layout(result, GrRenderable::kYes == renderable |
| 677 | ? VkLayout::kColorAttachmentOptimal |
| 678 | : VkLayout::kReadOnlyOptimal); |
Robert Phillips | d5e80ca | 2019-07-29 14:11:35 -0400 | [diff] [blame] | 679 | |
| 680 | #ifdef SK_DEBUG |
| 681 | { |
| 682 | GrBackendFormat format = context->defaultBackendFormat(colorType, |
| 683 | renderable); |
| 684 | SkASSERT(format == result.getBackendFormat()); |
| 685 | } |
| 686 | #endif |
| 687 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 688 | return result; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 689 | }; |
| 690 | |
| 691 | test_color_init(context, reporter, createWithColorMtd, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 692 | SkColorTypeToGrColorType(colorType), |
| 693 | combo.fColor, mipMapped, renderable); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 694 | } |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 695 | |
| 696 | // Gray_8 is problematic. In the colorInit tests there is ambiguity when |
| 697 | // mapping from format to colorType (since R8 or A8 could be either Alpha_8 |
| 698 | // or Gray_8). To compensate for this ambiguity we feed in colors with |
| 699 | // R==G==B==A. If we actually do know the colorType (as is the case |
| 700 | // in the SkPixmap case, there is no ambiguity but the two test expectations |
| 701 | // now collide. |
| 702 | // For now, skip the SkPixmap tests. The real answer is to plumb the |
| 703 | // SkColorType down further in the color-init case. |
| 704 | if (colorType != kGray_8_SkColorType) { |
| 705 | auto createWithSrcDataMtd = [](GrContext* context, |
| 706 | const SkPixmap srcData[], |
| 707 | int numLevels, |
| 708 | GrRenderable renderable) { |
| 709 | SkASSERT(srcData && numLevels); |
| 710 | auto result = context->priv().createBackendTexture(srcData, numLevels, |
| 711 | renderable, |
| 712 | GrProtected::kNo); |
| 713 | check_vk_layout(result, VkLayout::kReadOnlyOptimal); |
| 714 | #ifdef SK_DEBUG |
| 715 | { |
| 716 | auto format = context->defaultBackendFormat(srcData[0].colorType(), |
| 717 | renderable); |
| 718 | SkASSERT(format == result.getBackendFormat()); |
| 719 | } |
| 720 | #endif |
| 721 | return result; |
| 722 | }; |
| 723 | |
| 724 | test_pixmap_init(context, reporter, createWithSrcDataMtd, colorType, |
| 725 | mipMapped, renderable); |
| 726 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 727 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 728 | } |
| 729 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 732 | /////////////////////////////////////////////////////////////////////////////// |
| 733 | #ifdef SK_GL |
| 734 | |
| 735 | #include "src/gpu/gl/GrGLCaps.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 736 | #include "src/gpu/gl/GrGLDefines.h" |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 737 | #include "src/gpu/gl/GrGLUtil.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 738 | |
| 739 | DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLBackendAllocationTest, reporter, ctxInfo) { |
| 740 | sk_gpu_test::GLTestContext* glCtx = ctxInfo.glContext(); |
| 741 | GrGLStandard standard = glCtx->gl()->fStandard; |
| 742 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 743 | const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(context->priv().caps()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 744 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 745 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 746 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 747 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 748 | struct { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 749 | GrColorType fColorType; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 750 | GrGLenum fFormat; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 751 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 752 | } combinations[] = { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 753 | { GrColorType::kRGBA_8888, GR_GL_RGBA8, SkColors::kRed }, |
| 754 | { GrColorType::kRGBA_8888_SRGB, GR_GL_SRGB8_ALPHA8, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 755 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 756 | { GrColorType::kRGB_888x, GR_GL_RGBA8, SkColors::kYellow }, |
| 757 | { GrColorType::kRGB_888x, GR_GL_RGB8, SkColors::kCyan }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 758 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 759 | { GrColorType::kBGRA_8888, GR_GL_RGBA8, SkColors::kBlue }, |
| 760 | { GrColorType::kBGRA_8888, GR_GL_BGRA8, SkColors::kBlue }, |
| 761 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
| 762 | { GrColorType::kRGBA_1010102, GR_GL_RGB10_A2, { 0.5f, 0, 0, 1.0f } }, |
| 763 | { GrColorType::kBGR_565, GR_GL_RGB565, SkColors::kRed }, |
| 764 | { GrColorType::kABGR_4444, GR_GL_RGBA4, SkColors::kGreen }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 765 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 766 | { GrColorType::kAlpha_8, GR_GL_ALPHA8, kTransCol }, |
| 767 | { GrColorType::kAlpha_8, GR_GL_R8, kTransCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 768 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 769 | { GrColorType::kGray_8, GR_GL_LUMINANCE8, kGrayCol }, |
| 770 | { GrColorType::kGray_8, GR_GL_R8, kGrayCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 771 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 772 | { GrColorType::kRGBA_F32, GR_GL_RGBA32F, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 773 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 774 | { GrColorType::kRGBA_F16_Clamped, GR_GL_RGBA16F, SkColors::kLtGray }, |
| 775 | { GrColorType::kRGBA_F16, GR_GL_RGBA16F, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 776 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 777 | { GrColorType::kRG_88, GR_GL_RG8, { 1, 0.5f, 0, 1 } }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 778 | { GrColorType::kAlpha_F16, GR_GL_R16F, { 1.0f, 0, 0, 0.5f } }, |
| 779 | { GrColorType::kAlpha_F16, GR_GL_LUMINANCE16F, kGrayCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 780 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 781 | { GrColorType::kAlpha_16, GR_GL_R16, kTransCol }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 782 | { GrColorType::kRG_1616, GR_GL_RG16, SkColors::kYellow }, |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 783 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 784 | { GrColorType::kRGBA_16161616, GR_GL_RGBA16, SkColors::kLtGray }, |
| 785 | { GrColorType::kRG_F16, GR_GL_RG16F, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 786 | }; |
| 787 | |
| 788 | for (auto combo : combinations) { |
| 789 | GrBackendFormat format = GrBackendFormat::MakeGL(combo.fFormat, GR_GL_TEXTURE_2D); |
| 790 | |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 791 | if (!glCaps->isFormatTexturable(format)) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 792 | continue; |
| 793 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 794 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 795 | if (GrColorType::kBGRA_8888 == combo.fColorType) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 796 | if (GR_GL_RGBA8 == combo.fFormat && kGL_GrGLStandard != standard) { |
| 797 | continue; |
| 798 | } |
| 799 | if (GR_GL_BGRA8 == combo.fFormat && kGL_GrGLStandard == standard) { |
| 800 | continue; |
| 801 | } |
| 802 | } |
| 803 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 804 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 805 | if (GrMipMapped::kYes == mipMapped && !glCaps->mipMapSupport()) { |
| 806 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 807 | } |
| 808 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 809 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 810 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 811 | if (GrRenderable::kYes == renderable) { |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 812 | if (!glCaps->isFormatAsColorTypeRenderable(combo.fColorType, format)) { |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 813 | continue; |
| 814 | } |
| 815 | } |
| 816 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 817 | { |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 818 | auto uninitCreateMtd = [format](GrContext* context, |
| 819 | GrMipMapped mipMapped, |
| 820 | GrRenderable renderable) { |
| 821 | return context->createBackendTexture(32, 32, format, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 822 | mipMapped, renderable, |
| 823 | GrProtected::kNo); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 824 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 825 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 826 | test_wrapping(context, reporter, uninitCreateMtd, |
| 827 | combo.fColorType, mipMapped, renderable); |
| 828 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 829 | |
| 830 | { |
| 831 | // GL has difficulties reading back from these combinations |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 832 | if (GrColorType::kAlpha_8 == combo.fColorType) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 833 | continue; |
| 834 | } |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 835 | if (GrRenderable::kYes != renderable) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 836 | continue; |
| 837 | } |
| 838 | |
| 839 | auto createWithColorMtd = [format](GrContext* context, |
| 840 | const SkColor4f& color, |
| 841 | GrMipMapped mipMapped, |
| 842 | GrRenderable renderable) { |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 843 | return context->createBackendTexture(32, 32, format, color, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 844 | mipMapped, renderable, |
| 845 | GrProtected::kNo); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 846 | }; |
| 847 | |
| 848 | test_color_init(context, reporter, createWithColorMtd, |
| 849 | combo.fColorType, combo.fColor, mipMapped, renderable); |
| 850 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 851 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 856 | #endif |
| 857 | |
| 858 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 859 | |
| 860 | #ifdef SK_VULKAN |
| 861 | |
| 862 | #include "src/gpu/vk/GrVkCaps.h" |
| 863 | |
| 864 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendAllocationTest, reporter, ctxInfo) { |
| 865 | GrContext* context = ctxInfo.grContext(); |
| 866 | const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(context->priv().caps()); |
| 867 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 868 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 869 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 1 }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 870 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 871 | struct { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 872 | GrColorType fColorType; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 873 | VkFormat fFormat; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 874 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 875 | } combinations[] = { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 876 | { GrColorType::kRGBA_8888, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kRed }, |
| 877 | { GrColorType::kRGBA_8888_SRGB, VK_FORMAT_R8G8B8A8_SRGB, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 878 | |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 879 | // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format), |
| 880 | // there is nothing to tell Skia to make the provided color opaque. Clients will need |
| 881 | // to provide an opaque initialization color in this case. |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 882 | { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kYellow }, |
| 883 | { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8_UNORM, SkColors::kCyan }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 884 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 885 | { GrColorType::kBGRA_8888, VK_FORMAT_B8G8R8A8_UNORM, SkColors::kBlue }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 886 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 887 | { GrColorType::kRGBA_1010102, VK_FORMAT_A2B10G10R10_UNORM_PACK32, { 0.5f, 0, 0, 1.0f }}, |
| 888 | { GrColorType::kBGR_565, VK_FORMAT_R5G6B5_UNORM_PACK16, SkColors::kRed }, |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 889 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 890 | { GrColorType::kABGR_4444, VK_FORMAT_R4G4B4A4_UNORM_PACK16, SkColors::kCyan }, |
| 891 | { GrColorType::kABGR_4444, VK_FORMAT_B4G4R4A4_UNORM_PACK16, SkColors::kYellow }, |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 892 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 893 | { GrColorType::kAlpha_8, VK_FORMAT_R8_UNORM, kTransCol }, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 894 | // In this config (i.e., a Gray8 color type with an R8 backing format), there is nothing |
| 895 | // to tell Skia this isn't an Alpha8 color type (so it will initialize the texture with |
| 896 | // the alpha channel of the color). Clients should, in general, fill all the channels |
| 897 | // of the provided color with the same value in such cases. |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 898 | { GrColorType::kGray_8, VK_FORMAT_R8_UNORM, kGrayCol }, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 899 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 900 | { GrColorType::kRGBA_F32, VK_FORMAT_R32G32B32A32_SFLOAT, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 901 | |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 902 | { GrColorType::kRGBA_F16_Clamped, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kLtGray }, |
| 903 | { GrColorType::kRGBA_F16, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 904 | |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 905 | { GrColorType::kRG_88, VK_FORMAT_R8G8_UNORM, { 1, 0.5f, 0, 1 } }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 906 | { GrColorType::kAlpha_F16, VK_FORMAT_R16_SFLOAT, { 1.0f, 0, 0, 0.5f }}, |
| 907 | |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 908 | { GrColorType::kAlpha_16, VK_FORMAT_R16_UNORM, kTransCol }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 909 | { GrColorType::kRG_1616, VK_FORMAT_R16G16_UNORM, SkColors::kYellow }, |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 910 | { GrColorType::kRGBA_16161616, VK_FORMAT_R16G16B16A16_UNORM, SkColors::kLtGray }, |
| 911 | { GrColorType::kRG_F16, VK_FORMAT_R16G16_SFLOAT, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 912 | }; |
| 913 | |
| 914 | for (auto combo : combinations) { |
Greg Daniel | 2f2caea | 2019-07-08 14:24:47 -0400 | [diff] [blame] | 915 | if (!vkCaps->isVkFormatTexturable(combo.fFormat)) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 916 | continue; |
| 917 | } |
| 918 | |
| 919 | GrBackendFormat format = GrBackendFormat::MakeVk(combo.fFormat); |
| 920 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 921 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 922 | if (GrMipMapped::kYes == mipMapped && !vkCaps->mipMapSupport()) { |
| 923 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 926 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 927 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 928 | if (GrRenderable::kYes == renderable) { |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 929 | // We must also check whether we allow rendering to the format using the |
| 930 | // color type. |
Greg Daniel | 900583a | 2019-08-06 12:05:31 -0400 | [diff] [blame] | 931 | if (!vkCaps->isFormatAsColorTypeRenderable( |
| 932 | combo.fColorType, GrBackendFormat::MakeVk(combo.fFormat), 1)) { |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 933 | continue; |
| 934 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 935 | } |
| 936 | |
Robert Phillips | d34691b | 2019-09-24 13:38:43 -0400 | [diff] [blame] | 937 | { |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 938 | auto uninitCreateMtd = [format](GrContext* context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 939 | GrMipMapped mipMapped, |
| 940 | GrRenderable renderable) { |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 941 | GrBackendTexture beTex = context->createBackendTexture(32, 32, format, |
| 942 | mipMapped, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 943 | renderable, |
| 944 | GrProtected::kNo); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 945 | check_vk_layout(beTex, VkLayout::kUndefined); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 946 | return beTex; |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 947 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 948 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 949 | test_wrapping(context, reporter, uninitCreateMtd, |
| 950 | combo.fColorType, mipMapped, renderable); |
| 951 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 952 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 953 | { |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 954 | // We're creating backend textures without specifying a color type "view" of |
| 955 | // them at the public API level. Therefore, Ganesh will not apply any swizzles |
| 956 | // before writing the color to the texture. However, our validation code does |
| 957 | // rely on interpreting the texture contents via a SkColorType and therefore |
| 958 | // swizzles may be applied during the read step. |
| 959 | // Ideally we'd update our validation code to use a "raw" read that doesn't |
| 960 | // impose a color type but for now we just munge the data we upload to match the |
| 961 | // expectation. |
| 962 | GrSwizzle swizzle; |
| 963 | switch (combo.fColorType) { |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 964 | case GrColorType::kAlpha_8: |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 965 | SkASSERT(combo.fFormat == VK_FORMAT_R8_UNORM); |
| 966 | swizzle = GrSwizzle("aaaa"); |
| 967 | break; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 968 | case GrColorType::kAlpha_16: |
| 969 | SkASSERT(combo.fFormat == VK_FORMAT_R16_UNORM); |
| 970 | swizzle = GrSwizzle("aaaa"); |
| 971 | break; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 972 | case GrColorType::kAlpha_F16: |
| 973 | SkASSERT(combo.fFormat == VK_FORMAT_R16_SFLOAT); |
| 974 | swizzle = GrSwizzle("aaaa"); |
| 975 | break; |
Robert Phillips | b7f95d1 | 2019-07-26 11:13:19 -0400 | [diff] [blame] | 976 | case GrColorType::kABGR_4444: |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 977 | if (combo.fFormat == VK_FORMAT_B4G4R4A4_UNORM_PACK16) { |
| 978 | swizzle = GrSwizzle("bgra"); |
| 979 | } |
| 980 | break; |
| 981 | default: |
| 982 | swizzle = GrSwizzle("rgba"); |
| 983 | break; |
| 984 | } |
| 985 | auto createWithColorMtd = [format, swizzle](GrContext* context, |
| 986 | const SkColor4f& color, |
| 987 | GrMipMapped mipMapped, |
| 988 | GrRenderable renderable) { |
| 989 | auto swizzledColor = swizzle.applyTo(color); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 990 | GrBackendTexture beTex = context->createBackendTexture(32, 32, format, |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 991 | swizzledColor, |
| 992 | mipMapped, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 993 | renderable, |
| 994 | GrProtected::kNo); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 995 | check_vk_layout(beTex, GrRenderable::kYes == renderable |
| 996 | ? VkLayout::kColorAttachmentOptimal |
| 997 | : VkLayout::kReadOnlyOptimal); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 998 | return beTex; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 999 | }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 1000 | test_color_init(context, reporter, createWithColorMtd, |
| 1001 | combo.fColorType, combo.fColor, mipMapped, renderable); |
| 1002 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 1003 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | #endif |