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" |
| 15 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 16 | #ifdef SK_GL |
| 17 | #include "src/gpu/gl/GrGLGpu.h" |
| 18 | #include "src/gpu/gl/GrGLUtil.h" |
| 19 | #endif |
| 20 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 21 | // Test wrapping of GrBackendObjects in SkSurfaces and SkImages |
| 22 | void test_wrapping(GrContext* context, skiatest::Reporter* reporter, |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 23 | std::function<GrBackendTexture (GrContext*, |
| 24 | GrMipMapped, |
| 25 | GrRenderable)> create, |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 26 | SkColorType colorType, GrMipMapped mipMapped, GrRenderable renderable) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 27 | GrResourceCache* cache = context->priv().getResourceCache(); |
| 28 | |
| 29 | const int initialCount = cache->getResourceCount(); |
| 30 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 31 | GrBackendTexture backendTex = create(context, mipMapped, renderable); |
| 32 | if (!backendTex.isValid()) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 33 | ERRORF(reporter, "Couldn't create backendTexture for colorType %d renderable %s\n", |
| 34 | colorType, |
| 35 | GrRenderable::kYes == renderable ? "yes" : "no"); |
| 36 | return; |
| 37 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 38 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 39 | // Skia proper should know nothing about the new backend object |
| 40 | REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount()); |
| 41 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 42 | if (kUnknown_SkColorType == colorType) { |
| 43 | context->deleteBackendTexture(backendTex); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | if (GrRenderable::kYes == renderable) { |
| 48 | sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 49 | backendTex, |
| 50 | kTopLeft_GrSurfaceOrigin, |
| 51 | 0, |
| 52 | colorType, |
| 53 | nullptr, nullptr); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 54 | if (!surf) { |
| 55 | ERRORF(reporter, "Couldn't make surface from backendTexture for colorType %d\n", |
| 56 | colorType); |
| 57 | } else { |
| 58 | REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 59 | } |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 60 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 61 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 62 | { |
| 63 | sk_sp<SkImage> img = SkImage::MakeFromTexture(context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 64 | backendTex, |
| 65 | kTopLeft_GrSurfaceOrigin, |
| 66 | colorType, |
| 67 | kPremul_SkAlphaType, |
| 68 | nullptr); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 69 | if (!img) { |
| 70 | ERRORF(reporter, "Couldn't make image from backendTexture for colorType %d\n", |
| 71 | colorType); |
| 72 | } else { |
| 73 | SkImage_Base* ib = as_IB(img); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 74 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 75 | GrTextureProxy* proxy = ib->peekProxy(); |
| 76 | REPORTER_ASSERT(reporter, proxy); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 77 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 78 | REPORTER_ASSERT(reporter, mipMapped == proxy->proxyMipMapped()); |
| 79 | REPORTER_ASSERT(reporter, proxy->isInstantiated()); |
| 80 | REPORTER_ASSERT(reporter, mipMapped == proxy->mipMapped()); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 81 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount()); |
| 87 | |
Robert Phillips | 5c7a25b | 2019-05-20 08:38:07 -0400 | [diff] [blame] | 88 | context->deleteBackendTexture(backendTex); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 91 | static bool colors_eq(SkColor colA, SkColor colB, int tol) { |
| 92 | int maxDiff = 0; |
| 93 | for (int i = 0; i < 4; ++i) { |
| 94 | int diff = SkTAbs<int>((0xFF & (colA >> i*8)) - (0xFF & (colB >> i*8))); |
| 95 | if (maxDiff < diff) { |
| 96 | maxDiff = diff; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return maxDiff <= tol; |
| 101 | } |
| 102 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 103 | static void compare_pixmaps(const SkPixmap& expected, const SkPixmap& actual, |
| 104 | SkColorType colorType, skiatest::Reporter* reporter) { |
| 105 | SkASSERT(expected.info() == actual.info()); |
| 106 | for (int y = 0; y < expected.height(); ++y) { |
| 107 | for (int x = 0; x < expected.width(); ++x) { |
| 108 | |
| 109 | SkColor expectedCol = expected.getColor(x, y); |
| 110 | SkColor actualCol = actual.getColor(x, y); |
| 111 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 112 | // GPU and raster differ a bit on kGray_8_SkColorType and kRGBA_1010102_SkColorType |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 113 | if (colors_eq(actualCol, expectedCol, 12)) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | |
| 117 | ERRORF(reporter, |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 118 | "Mismatched pixels at %d %d ct: %d expected: 0x%x actual: 0x%x\n", |
| 119 | x, y, colorType, expectedCol, actualCol); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Test initialization of GrBackendObjects to a specific color |
| 126 | void test_color_init(GrContext* context, skiatest::Reporter* reporter, |
| 127 | std::function<GrBackendTexture (GrContext*, |
| 128 | const SkColor4f&, |
| 129 | GrMipMapped, |
| 130 | GrRenderable)> create, |
| 131 | SkColorType colorType, const SkColor4f& color, |
| 132 | GrMipMapped mipMapped, GrRenderable renderable) { |
| 133 | GrBackendTexture backendTex = create(context, color, mipMapped, renderable); |
| 134 | if (!backendTex.isValid()) { |
| 135 | // errors here should be reported by the test_wrapping test |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if (kUnknown_SkColorType == colorType) { |
| 140 | // TODO: burrow in and scrappily check that data was uploaded! |
| 141 | context->deleteBackendTexture(backendTex); |
| 142 | return; |
| 143 | } |
| 144 | |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 145 | SkAlphaType at = SkColorTypeIsAlwaysOpaque(colorType) ? kOpaque_SkAlphaType |
| 146 | : kPremul_SkAlphaType; |
| 147 | |
| 148 | SkImageInfo ii = SkImageInfo::Make(32, 32, colorType, at); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 149 | |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 150 | SkColor4f rasterColor = color; |
| 151 | if (kGray_8_SkColorType == colorType) { |
| 152 | // For the GPU backends, gray implies a single channel which is opaque. |
| 153 | rasterColor.fR = color.fA; |
| 154 | rasterColor.fG = color.fA; |
| 155 | rasterColor.fB = color.fA; |
| 156 | rasterColor.fA = 1.0f; |
| 157 | } else if (kAlpha_8_SkColorType == colorType) { |
| 158 | // For the GPU backends, alpha implies a single alpha channel. |
| 159 | rasterColor.fR = 0; |
| 160 | rasterColor.fG = 0; |
| 161 | rasterColor.fB = 0; |
| 162 | rasterColor.fA = color.fA; |
| 163 | } |
| 164 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 165 | SkAutoPixmapStorage expected; |
| 166 | SkAssertResult(expected.tryAlloc(ii)); |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 167 | expected.erase(rasterColor); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 168 | |
| 169 | SkAutoPixmapStorage actual; |
| 170 | SkAssertResult(actual.tryAlloc(ii)); |
| 171 | actual.erase(SkColors::kTransparent); |
| 172 | |
| 173 | if (GrRenderable::kYes == renderable) { |
| 174 | sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context, |
| 175 | backendTex, |
| 176 | kTopLeft_GrSurfaceOrigin, |
| 177 | 0, |
| 178 | colorType, |
| 179 | nullptr, nullptr); |
| 180 | if (surf) { |
| 181 | bool result = surf->readPixels(actual, 0, 0); |
| 182 | REPORTER_ASSERT(reporter, result); |
| 183 | |
| 184 | compare_pixmaps(expected, actual, colorType, reporter); |
| 185 | |
| 186 | actual.erase(SkColors::kTransparent); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | { |
| 191 | sk_sp<SkImage> img = SkImage::MakeFromTexture(context, |
| 192 | backendTex, |
| 193 | kTopLeft_GrSurfaceOrigin, |
| 194 | colorType, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 195 | at, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 196 | nullptr); |
| 197 | if (img) { |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 198 | // If possible, read back the pixels and check that they're correct |
| 199 | { |
| 200 | bool result = img->readPixels(actual, 0, 0); |
| 201 | if (!result) { |
| 202 | // TODO: we need a better way to tell a priori if readPixels will work for an |
| 203 | // arbitrary colorType |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 204 | #if 0 |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 205 | ERRORF(reporter, "Couldn't readback from SkImage for colorType: %d\n", |
| 206 | colorType); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 207 | #endif |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 208 | } else { |
| 209 | compare_pixmaps(expected, actual, colorType, reporter); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Draw the wrapped image into an RGBA surface attempting to access all the |
| 214 | // mipMap levels. |
| 215 | { |
| 216 | #ifdef SK_GL |
| 217 | // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices) |
| 218 | if (GrBackendApi::kOpenGL == context->backend()) { |
| 219 | GrGLGpu* glGPU = static_cast<GrGLGpu*>(context->priv().getGpu()); |
| 220 | |
Robert Phillips | ef032cd | 2019-06-03 15:12:50 -0400 | [diff] [blame] | 221 | if (kRGBA_F32_SkColorType == colorType && GrMipMapped::kYes == mipMapped && |
| 222 | kGLES_GrGLStandard == glGPU->ctxInfo().standard()) { |
Robert Phillips | 27eb525 | 2019-06-03 12:59:40 -0400 | [diff] [blame] | 223 | context->deleteBackendTexture(backendTex); |
| 224 | return; |
| 225 | } |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | SkImageInfo newII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, |
| 230 | kPremul_SkAlphaType); |
| 231 | |
| 232 | SkAutoPixmapStorage actual2; |
| 233 | SkAssertResult(actual2.tryAlloc(newII)); |
| 234 | actual2.erase(SkColors::kTransparent); |
| 235 | |
| 236 | sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(context, |
| 237 | SkBudgeted::kNo, |
| 238 | newII, 1, |
| 239 | kTopLeft_GrSurfaceOrigin, |
| 240 | nullptr); |
| 241 | if (!surf) { |
| 242 | context->deleteBackendTexture(backendTex); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | SkCanvas* canvas = surf->getCanvas(); |
| 247 | |
| 248 | SkPaint p; |
| 249 | p.setFilterQuality(kHigh_SkFilterQuality); |
| 250 | |
| 251 | int numMipLevels = (GrMipMapped::kYes == mipMapped) ? 6 : 1; |
| 252 | |
| 253 | for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) { |
| 254 | SkASSERT(rectSize >= 1); |
| 255 | |
| 256 | SkRect r = SkRect::MakeWH(rectSize, rectSize); |
| 257 | canvas->clear(SK_ColorTRANSPARENT); |
| 258 | canvas->drawImageRect(img, r, &p); |
| 259 | |
| 260 | bool result = surf->readPixels(actual2, 0, 0); |
| 261 | REPORTER_ASSERT(reporter, result); |
| 262 | |
| 263 | SkColor actualColor = actual2.getColor(0, 0); |
| 264 | |
| 265 | if (!colors_eq(actualColor, rasterColor.toSkColor(), 1)) { |
| 266 | ERRORF(reporter, "Pixel mismatch colorType %d: level: %d e: 0x%x a: 0x%x\n", |
| 267 | colorType, i, rasterColor.toSkColor(), actualColor); |
| 268 | } |
| 269 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | context->deleteBackendTexture(backendTex); |
| 275 | } |
| 276 | |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 277 | enum class VkLayout { |
| 278 | kUndefined, |
| 279 | kReadOnlyOptimal, |
| 280 | kColorAttachmentOptimal |
| 281 | }; |
| 282 | |
| 283 | void check_vk_layout(const GrBackendTexture& backendTex, VkLayout layout) { |
| 284 | #if defined(SK_VULKAN) && defined(SK_DEBUG) |
| 285 | VkImageLayout expected; |
| 286 | |
| 287 | switch (layout) { |
| 288 | case VkLayout::kUndefined: |
| 289 | expected = VK_IMAGE_LAYOUT_UNDEFINED; |
| 290 | break; |
| 291 | case VkLayout::kReadOnlyOptimal: |
| 292 | expected = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 293 | break; |
| 294 | case VkLayout::kColorAttachmentOptimal: |
| 295 | expected = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 296 | break; |
| 297 | default: |
| 298 | SkUNREACHABLE; |
| 299 | } |
| 300 | |
| 301 | GrVkImageInfo vkII; |
| 302 | |
| 303 | if (backendTex.getVkImageInfo(&vkII)) { |
| 304 | SkASSERT(expected == vkII.fImageLayout); |
| 305 | SkASSERT(VK_IMAGE_TILING_OPTIMAL == vkII.fImageTiling); |
| 306 | } |
| 307 | #endif |
| 308 | } |
| 309 | |
| 310 | /////////////////////////////////////////////////////////////////////////////// |
| 311 | // This test is a bit different from the others in this file. It is mainly checking that, for any |
| 312 | // SkSurface we can create in Ganesh, we can also create a backend texture that is compatible with |
| 313 | // its characterization and then create a new surface that wraps that backend texture. |
| 314 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CharacterizationBackendAllocationTest, reporter, ctxInfo) { |
| 315 | GrContext* context = ctxInfo.grContext(); |
| 316 | |
| 317 | for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) { |
| 318 | SkColorType colorType = static_cast<SkColorType>(ct); |
| 319 | |
| 320 | SkImageInfo ii = SkImageInfo::Make(32, 32, colorType, kPremul_SkAlphaType); |
| 321 | |
| 322 | for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) { |
| 323 | for (bool mipMaps : { true, false } ) { |
| 324 | for (int sampleCount : {1, 2}) { |
| 325 | SkSurfaceCharacterization c; |
| 326 | |
| 327 | // Get a characterization, if possible |
| 328 | { |
| 329 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, |
| 330 | ii, sampleCount, |
| 331 | origin, nullptr, mipMaps); |
| 332 | if (!s) { |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | if (!s->characterize(&c)) { |
| 337 | continue; |
| 338 | } |
| 339 | |
| 340 | REPORTER_ASSERT(reporter, s->isCompatible(c)); |
| 341 | } |
| 342 | |
| 343 | // Test out uninitialized path |
| 344 | { |
| 345 | GrBackendTexture backendTex = context->createBackendTexture(c); |
| 346 | check_vk_layout(backendTex, VkLayout::kUndefined); |
| 347 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 348 | REPORTER_ASSERT(reporter, c.isCompatible(backendTex)); |
| 349 | |
| 350 | sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c, |
| 351 | backendTex); |
| 352 | REPORTER_ASSERT(reporter, s2); |
| 353 | REPORTER_ASSERT(reporter, s2->isCompatible(c)); |
| 354 | |
| 355 | s2 = nullptr; |
| 356 | context->deleteBackendTexture(backendTex); |
| 357 | } |
| 358 | |
| 359 | // Test out color-initialized path |
| 360 | { |
| 361 | GrBackendTexture backendTex = context->createBackendTexture(c, |
| 362 | SkColors::kRed); |
| 363 | check_vk_layout(backendTex, VkLayout::kColorAttachmentOptimal); |
| 364 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 365 | REPORTER_ASSERT(reporter, c.isCompatible(backendTex)); |
| 366 | |
| 367 | sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c, |
| 368 | backendTex); |
| 369 | REPORTER_ASSERT(reporter, s2); |
| 370 | REPORTER_ASSERT(reporter, s2->isCompatible(c)); |
| 371 | |
| 372 | s2 = nullptr; |
| 373 | context->deleteBackendTexture(backendTex); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 381 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 382 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ColorTypeBackendAllocationTest, reporter, ctxInfo) { |
| 383 | GrContext* context = ctxInfo.grContext(); |
| 384 | const GrCaps* caps = context->priv().caps(); |
| 385 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 386 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 387 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 388 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 389 | struct { |
| 390 | SkColorType fColorType; |
| 391 | GrPixelConfig fConfig; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 392 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 393 | } combinations[] = { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 394 | { kAlpha_8_SkColorType, kAlpha_8_GrPixelConfig, kTransCol }, |
| 395 | { kRGB_565_SkColorType, kRGB_565_GrPixelConfig, SkColors::kRed }, |
| 396 | { kARGB_4444_SkColorType, kRGBA_4444_GrPixelConfig, SkColors::kGreen }, |
| 397 | { kRGBA_8888_SkColorType, kRGBA_8888_GrPixelConfig, SkColors::kBlue }, |
| 398 | { kRGB_888x_SkColorType, kRGB_888_GrPixelConfig, SkColors::kCyan }, |
| 399 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
| 400 | { kBGRA_8888_SkColorType, kBGRA_8888_GrPixelConfig, { 1, 0, 0, 1.0f } }, |
| 401 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
| 402 | { kRGBA_1010102_SkColorType, kRGBA_1010102_GrPixelConfig, { 0.5f, 0, 0, 1.0f }}, |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 403 | // The kRGB_101010x_SkColorType has no Ganesh correlate |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 404 | { kRGB_101010x_SkColorType, kUnknown_GrPixelConfig, { 0, 0.5f, 0, 0.5f }}, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 405 | { kGray_8_SkColorType, kGray_8_GrPixelConfig, kGrayCol }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 406 | { kRGBA_F16Norm_SkColorType, kRGBA_half_Clamped_GrPixelConfig, SkColors::kLtGray }, |
| 407 | { kRGBA_F16_SkColorType, kRGBA_half_GrPixelConfig, SkColors::kYellow }, |
| 408 | { kRGBA_F32_SkColorType, kRGBA_float_GrPixelConfig, SkColors::kGray }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | SkASSERT(kLastEnum_SkColorType == SK_ARRAY_COUNT(combinations)); |
| 412 | |
| 413 | for (auto combo : combinations) { |
| 414 | SkColorType colorType = combo.fColorType; |
| 415 | |
| 416 | if (!caps->isConfigTexturable(combo.fConfig)) { |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | if (GrBackendApi::kMetal == context->backend()) { |
| 421 | // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly) |
| 422 | if (kRGBA_F32_SkColorType == combo.fColorType) { |
| 423 | continue; |
| 424 | } |
| 425 | } |
| 426 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 427 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 428 | if (GrMipMapped::kYes == mipMapped && !caps->mipMapSupport()) { |
| 429 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 430 | } |
| 431 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 432 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
| 433 | if (GrRenderable::kYes == renderable) { |
| 434 | if (kRGB_888x_SkColorType == combo.fColorType) { |
| 435 | // Ganesh can't perform the blends correctly when rendering this format |
| 436 | continue; |
| 437 | } |
| 438 | if (!caps->isConfigRenderable(combo.fConfig)) { |
| 439 | continue; |
| 440 | } |
| 441 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 442 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 443 | { |
| 444 | auto uninitCreateMtd = [colorType](GrContext* context, |
| 445 | GrMipMapped mipMapped, |
| 446 | GrRenderable renderable) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 447 | auto result = context->createBackendTexture(32, 32, colorType, |
| 448 | mipMapped, renderable, |
| 449 | GrProtected::kNo); |
| 450 | check_vk_layout(result, VkLayout::kUndefined); |
| 451 | return result; |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 452 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 453 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 454 | test_wrapping(context, reporter, uninitCreateMtd, |
| 455 | colorType, mipMapped, renderable); |
| 456 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 457 | |
| 458 | { |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 459 | // GL has difficulties reading back from these combinations. In particular, |
| 460 | // reading back kGray_8 is a mess. |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 461 | if (GrBackendApi::kOpenGL == context->backend()) { |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 462 | if (kAlpha_8_SkColorType == combo.fColorType || |
| 463 | kGray_8_SkColorType == combo.fColorType) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 464 | continue; |
| 465 | } |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 466 | } else if (GrBackendApi::kMetal == context->backend()) { |
| 467 | // Not yet implemented for Metal |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 468 | continue; |
| 469 | } |
| 470 | |
| 471 | auto createWithColorMtd = [colorType](GrContext* context, |
| 472 | const SkColor4f& color, |
| 473 | GrMipMapped mipMapped, |
| 474 | GrRenderable renderable) { |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 475 | auto result = context->createBackendTexture(32, 32, colorType, color, |
| 476 | mipMapped, renderable, |
| 477 | GrProtected::kNo); |
| 478 | check_vk_layout(result, GrRenderable::kYes == renderable |
| 479 | ? VkLayout::kColorAttachmentOptimal |
| 480 | : VkLayout::kReadOnlyOptimal); |
| 481 | return result; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | test_color_init(context, reporter, createWithColorMtd, |
| 485 | colorType, combo.fColor, mipMapped, renderable); |
| 486 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 487 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | } |
| 492 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 493 | /////////////////////////////////////////////////////////////////////////////// |
| 494 | #ifdef SK_GL |
| 495 | |
| 496 | #include "src/gpu/gl/GrGLCaps.h" |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 497 | #include "src/gpu/gl/GrGLDefines.h" |
| 498 | |
| 499 | DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLBackendAllocationTest, reporter, ctxInfo) { |
| 500 | sk_gpu_test::GLTestContext* glCtx = ctxInfo.glContext(); |
| 501 | GrGLStandard standard = glCtx->gl()->fStandard; |
| 502 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 503 | const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(context->priv().caps()); |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 504 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 505 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 506 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 507 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 508 | struct { |
| 509 | SkColorType fColorType; |
| 510 | GrGLenum fFormat; |
| 511 | // TODO: remove 'fConfig' and directly use 'fFormat' in GrGLCaps::isFormatTexturable |
| 512 | GrPixelConfig fConfig; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 513 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 514 | } combinations[] = { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 515 | { kRGBA_8888_SkColorType, GR_GL_RGBA8, |
| 516 | kRGBA_8888_GrPixelConfig, SkColors::kRed }, |
| 517 | { kRGBA_8888_SkColorType, GR_GL_SRGB8_ALPHA8, |
| 518 | kSRGBA_8888_GrPixelConfig, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 519 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 520 | { kRGB_888x_SkColorType, GR_GL_RGBA8, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 521 | kRGBA_8888_GrPixelConfig, SkColors::kYellow }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 522 | { kRGB_888x_SkColorType, GR_GL_RGB8, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 523 | kRGB_888_GrPixelConfig, SkColors::kCyan }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 524 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 525 | { kBGRA_8888_SkColorType, GR_GL_RGBA8, |
| 526 | kRGBA_8888_GrPixelConfig, SkColors::kBlue }, |
| 527 | { kBGRA_8888_SkColorType, GR_GL_BGRA8, |
| 528 | kBGRA_8888_GrPixelConfig, SkColors::kBlue }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 529 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 530 | { kRGBA_1010102_SkColorType, GR_GL_RGB10_A2, |
| 531 | // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul) |
| 532 | kRGBA_1010102_GrPixelConfig, { 0.5f, 0, 0, 1.0f }}, |
| 533 | { kRGB_565_SkColorType, GR_GL_RGB565, |
| 534 | kRGB_565_GrPixelConfig, SkColors::kRed }, |
| 535 | { kARGB_4444_SkColorType, GR_GL_RGBA4, |
| 536 | kRGBA_4444_GrPixelConfig, SkColors::kGreen }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 537 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 538 | { kAlpha_8_SkColorType, GR_GL_ALPHA8, |
| 539 | kAlpha_8_as_Alpha_GrPixelConfig, kTransCol }, |
| 540 | { kAlpha_8_SkColorType, GR_GL_R8, |
| 541 | kAlpha_8_as_Red_GrPixelConfig, kTransCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 542 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 543 | { kGray_8_SkColorType, GR_GL_LUMINANCE8, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 544 | kGray_8_as_Lum_GrPixelConfig, kGrayCol }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 545 | { kGray_8_SkColorType, GR_GL_R8, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 546 | kGray_8_as_Red_GrPixelConfig, kGrayCol }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 547 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 548 | { kRGBA_F32_SkColorType, GR_GL_RGBA32F, |
| 549 | kRGBA_float_GrPixelConfig, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 550 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 551 | { kRGBA_F16Norm_SkColorType, GR_GL_RGBA16F, |
| 552 | kRGBA_half_Clamped_GrPixelConfig, SkColors::kLtGray }, |
| 553 | { kRGBA_F16_SkColorType, GR_GL_RGBA16F, |
| 554 | kRGBA_half_GrPixelConfig, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 555 | |
| 556 | // These backend formats don't have SkColorType equivalents |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 557 | { kUnknown_SkColorType, GR_GL_RG8, |
| 558 | kRG_88_GrPixelConfig, { 0.5f, 0.5f, 0, 0 }}, |
| 559 | { kUnknown_SkColorType, GR_GL_R16F, |
| 560 | kAlpha_half_as_Red_GrPixelConfig, { 1.0f, 0, 0, 0.5f }}, |
| 561 | { kUnknown_SkColorType, GR_GL_COMPRESSED_RGB8_ETC2, |
| 562 | kRGB_ETC1_GrPixelConfig, SkColors::kRed }, |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 563 | { kUnknown_SkColorType, GR_GL_COMPRESSED_ETC1_RGB8, |
| 564 | kRGB_ETC1_GrPixelConfig, SkColors::kRed }, |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 565 | { kUnknown_SkColorType, GR_GL_R16, |
| 566 | kR_16_GrPixelConfig, SkColors::kRed }, |
| 567 | { kUnknown_SkColorType, GR_GL_RG16, |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 568 | kRG_1616_GrPixelConfig, SkColors::kYellow }, |
| 569 | |
| 570 | // Experimental (for Y416 and mutant P016/P010) |
| 571 | { kUnknown_SkColorType, GR_GL_RGBA16, |
| 572 | kRGBA_16161616_GrPixelConfig, SkColors::kLtGray }, |
| 573 | { kUnknown_SkColorType, GR_GL_RG16F, |
| 574 | kRG_half_GrPixelConfig, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 575 | }; |
| 576 | |
| 577 | for (auto combo : combinations) { |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 578 | if (kRGB_ETC1_GrPixelConfig == combo.fConfig) { |
| 579 | // RGB8_ETC2/ETC1_RGB8 is an either/or situation |
| 580 | GrGLenum supportedETC1Format = glCaps->configSizedInternalFormat(combo.fConfig); |
| 581 | if (supportedETC1Format != combo.fFormat) { |
| 582 | continue; |
| 583 | } |
| 584 | } |
| 585 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 586 | GrBackendFormat format = GrBackendFormat::MakeGL(combo.fFormat, GR_GL_TEXTURE_2D); |
| 587 | |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 588 | if (GR_GL_COMPRESSED_RGB8_ETC2 == combo.fFormat || |
| 589 | GR_GL_COMPRESSED_ETC1_RGB8 == combo.fFormat) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 590 | // We current disallow uninitialized ETC1 textures in the GL backend |
| 591 | continue; |
| 592 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 593 | |
Robert Phillips | 9725638 | 2019-07-17 15:26:36 -0400 | [diff] [blame] | 594 | GrColorType grCT = SkColorTypeAndFormatToGrColorType(glCaps, combo.fColorType, format); |
| 595 | if (GrColorType::kUnknown == grCT) { |
| 596 | continue; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (!glCaps->isFormatTexturable(grCT, format)) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 600 | continue; |
| 601 | } |
| 602 | |
| 603 | if (kBGRA_8888_SkColorType == combo.fColorType) { |
| 604 | if (GR_GL_RGBA8 == combo.fFormat && kGL_GrGLStandard != standard) { |
| 605 | continue; |
| 606 | } |
| 607 | if (GR_GL_BGRA8 == combo.fFormat && kGL_GrGLStandard == standard) { |
| 608 | continue; |
| 609 | } |
| 610 | } |
| 611 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 612 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 613 | if (GrMipMapped::kYes == mipMapped && !glCaps->mipMapSupport()) { |
| 614 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 615 | } |
| 616 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 617 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 618 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 619 | if (GrRenderable::kYes == renderable) { |
| 620 | if (kRGB_888x_SkColorType == combo.fColorType) { |
| 621 | // Ganesh can't perform the blends correctly when rendering this format |
| 622 | continue; |
| 623 | } |
| 624 | if (!glCaps->isConfigRenderable(combo.fConfig)) { |
| 625 | continue; |
| 626 | } |
| 627 | } |
| 628 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 629 | { |
| 630 | auto uninitCreateMtd = [format](GrContext* context, |
| 631 | GrMipMapped mipMapped, |
| 632 | GrRenderable renderable) { |
| 633 | return context->createBackendTexture(32, 32, format, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 634 | mipMapped, renderable, |
| 635 | GrProtected::kNo); |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 636 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 637 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 638 | test_wrapping(context, reporter, uninitCreateMtd, |
| 639 | combo.fColorType, mipMapped, renderable); |
| 640 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 641 | |
| 642 | { |
| 643 | // GL has difficulties reading back from these combinations |
| 644 | if (kAlpha_8_SkColorType == combo.fColorType) { |
| 645 | continue; |
| 646 | } |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 647 | if (GrRenderable::kYes != renderable) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 648 | continue; |
| 649 | } |
| 650 | |
| 651 | auto createWithColorMtd = [format](GrContext* context, |
| 652 | const SkColor4f& color, |
| 653 | GrMipMapped mipMapped, |
| 654 | GrRenderable renderable) { |
Robert Phillips | 4bdd36f | 2019-06-04 11:03:06 -0400 | [diff] [blame] | 655 | return context->createBackendTexture(32, 32, format, color, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 656 | mipMapped, renderable, |
| 657 | GrProtected::kNo); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 658 | }; |
| 659 | |
| 660 | test_color_init(context, reporter, createWithColorMtd, |
| 661 | combo.fColorType, combo.fColor, mipMapped, renderable); |
| 662 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 663 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 668 | #endif |
| 669 | |
| 670 | /////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 671 | |
| 672 | #ifdef SK_VULKAN |
| 673 | |
| 674 | #include "src/gpu/vk/GrVkCaps.h" |
| 675 | |
| 676 | DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendAllocationTest, reporter, ctxInfo) { |
| 677 | GrContext* context = ctxInfo.grContext(); |
| 678 | const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(context->priv().caps()); |
| 679 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 680 | constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f }; |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 681 | constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 682 | |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 683 | struct { |
| 684 | SkColorType fColorType; |
| 685 | VkFormat fFormat; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 686 | SkColor4f fColor; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 687 | } combinations[] = { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 688 | { kRGBA_8888_SkColorType, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kRed }, |
| 689 | { kRGBA_8888_SkColorType, VK_FORMAT_R8G8B8A8_SRGB, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 690 | |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 691 | // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format), |
| 692 | // there is nothing to tell Skia to make the provided color opaque. Clients will need |
| 693 | // to provide an opaque initialization color in this case. |
| 694 | { kRGB_888x_SkColorType, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kYellow }, |
| 695 | { kRGB_888x_SkColorType, VK_FORMAT_R8G8B8_UNORM, SkColors::kCyan }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 696 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 697 | { kBGRA_8888_SkColorType, VK_FORMAT_B8G8R8A8_UNORM, SkColors::kBlue }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 698 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 699 | { kRGBA_1010102_SkColorType, VK_FORMAT_A2B10G10R10_UNORM_PACK32, { 0.5f, 0, 0, 1.0f } }, |
| 700 | { kRGB_565_SkColorType, VK_FORMAT_R5G6B5_UNORM_PACK16, SkColors::kRed }, |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 701 | |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 702 | { kARGB_4444_SkColorType, VK_FORMAT_R4G4B4A4_UNORM_PACK16, SkColors::kCyan }, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 703 | { kARGB_4444_SkColorType, VK_FORMAT_B4G4R4A4_UNORM_PACK16, SkColors::kYellow }, |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 704 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 705 | { kAlpha_8_SkColorType, VK_FORMAT_R8_UNORM, kTransCol }, |
Robert Phillips | bd1ef68 | 2019-05-31 12:48:49 -0400 | [diff] [blame] | 706 | // In this config (i.e., a Gray8 color type with an R8 backing format), there is nothing |
| 707 | // to tell Skia this isn't an Alpha8 color type (so it will initialize the texture with |
| 708 | // the alpha channel of the color). Clients should, in general, fill all the channels |
| 709 | // of the provided color with the same value in such cases. |
| 710 | { kGray_8_SkColorType, VK_FORMAT_R8_UNORM, kGrayCol }, |
| 711 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 712 | { kRGBA_F32_SkColorType, VK_FORMAT_R32G32B32A32_SFLOAT, SkColors::kRed }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 713 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 714 | { kRGBA_F16Norm_SkColorType, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kLtGray }, |
| 715 | { kRGBA_F16_SkColorType, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 716 | |
| 717 | // These backend formats don't have SkColorType equivalents |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 718 | { kUnknown_SkColorType, VK_FORMAT_R8G8_UNORM, { 0.5f, 0.5f, 0, 0 } }, |
| 719 | { kUnknown_SkColorType, VK_FORMAT_R16_SFLOAT, { 1.0f, 0, 0, 0.5f } }, |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 720 | { kUnknown_SkColorType, VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, SkColors::kRed }, |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 721 | { kUnknown_SkColorType, VK_FORMAT_R16_UNORM, SkColors::kRed }, |
| 722 | { kUnknown_SkColorType, VK_FORMAT_R16G16_UNORM, SkColors::kYellow }, |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 723 | |
| 724 | // Experimental (for Y416 and mutant P016/P010) |
| 725 | { kUnknown_SkColorType, VK_FORMAT_R16G16B16A16_UNORM, SkColors::kLtGray }, |
| 726 | { kUnknown_SkColorType, VK_FORMAT_R16G16_SFLOAT, SkColors::kYellow }, |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | for (auto combo : combinations) { |
Greg Daniel | 2f2caea | 2019-07-08 14:24:47 -0400 | [diff] [blame] | 730 | if (!vkCaps->isVkFormatTexturable(combo.fFormat)) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 731 | continue; |
| 732 | } |
| 733 | |
| 734 | GrBackendFormat format = GrBackendFormat::MakeVk(combo.fFormat); |
| 735 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 736 | for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) { |
| 737 | if (GrMipMapped::kYes == mipMapped && !vkCaps->mipMapSupport()) { |
| 738 | continue; |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 741 | for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) { |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 742 | |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 743 | if (GrRenderable::kYes == renderable) { |
| 744 | if (kRGB_888x_SkColorType == combo.fColorType) { |
| 745 | // Ganesh can't perform the blends correctly when rendering this format |
| 746 | continue; |
| 747 | } |
| 748 | if (!vkCaps->isFormatRenderable(combo.fFormat)) { |
| 749 | continue; |
| 750 | } |
| 751 | } |
| 752 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 753 | { |
| 754 | auto uninitCreateMtd = [format](GrContext* context, |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 755 | GrMipMapped mipMapped, |
| 756 | GrRenderable renderable) { |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 757 | GrBackendTexture beTex = context->createBackendTexture(32, 32, format, |
| 758 | mipMapped, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 759 | renderable, |
| 760 | GrProtected::kNo); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 761 | check_vk_layout(beTex, VkLayout::kUndefined); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 762 | return beTex; |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 763 | }; |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 764 | |
Robert Phillips | b04b694 | 2019-05-21 17:24:31 -0400 | [diff] [blame] | 765 | test_wrapping(context, reporter, uninitCreateMtd, |
| 766 | combo.fColorType, mipMapped, renderable); |
| 767 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 768 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 769 | { |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 770 | // We're creating backend textures without specifying a color type "view" of |
| 771 | // them at the public API level. Therefore, Ganesh will not apply any swizzles |
| 772 | // before writing the color to the texture. However, our validation code does |
| 773 | // rely on interpreting the texture contents via a SkColorType and therefore |
| 774 | // swizzles may be applied during the read step. |
| 775 | // Ideally we'd update our validation code to use a "raw" read that doesn't |
| 776 | // impose a color type but for now we just munge the data we upload to match the |
| 777 | // expectation. |
| 778 | GrSwizzle swizzle; |
| 779 | switch (combo.fColorType) { |
| 780 | case kAlpha_8_SkColorType: |
| 781 | SkASSERT(combo.fFormat == VK_FORMAT_R8_UNORM); |
| 782 | swizzle = GrSwizzle("aaaa"); |
| 783 | break; |
| 784 | case kARGB_4444_SkColorType: |
| 785 | if (combo.fFormat == VK_FORMAT_B4G4R4A4_UNORM_PACK16) { |
| 786 | swizzle = GrSwizzle("bgra"); |
| 787 | } |
| 788 | break; |
| 789 | default: |
| 790 | swizzle = GrSwizzle("rgba"); |
| 791 | break; |
| 792 | } |
| 793 | auto createWithColorMtd = [format, swizzle](GrContext* context, |
| 794 | const SkColor4f& color, |
| 795 | GrMipMapped mipMapped, |
| 796 | GrRenderable renderable) { |
| 797 | auto swizzledColor = swizzle.applyTo(color); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 798 | GrBackendTexture beTex = context->createBackendTexture(32, 32, format, |
Brian Salomon | b450f3b | 2019-07-09 09:36:51 -0400 | [diff] [blame] | 799 | swizzledColor, |
| 800 | mipMapped, |
Robert Phillips | da2e67a | 2019-07-01 15:04:06 -0400 | [diff] [blame] | 801 | renderable, |
| 802 | GrProtected::kNo); |
Robert Phillips | 02dc030 | 2019-07-02 17:58:27 -0400 | [diff] [blame] | 803 | check_vk_layout(beTex, GrRenderable::kYes == renderable |
| 804 | ? VkLayout::kColorAttachmentOptimal |
| 805 | : VkLayout::kReadOnlyOptimal); |
Robert Phillips | d1d869d | 2019-06-07 14:21:31 -0400 | [diff] [blame] | 806 | return beTex; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 807 | }; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 808 | test_color_init(context, reporter, createWithColorMtd, |
| 809 | combo.fColorType, combo.fColor, mipMapped, renderable); |
| 810 | } |
Robert Phillips | efb9f14 | 2019-05-17 14:19:44 -0400 | [diff] [blame] | 811 | } |
Robert Phillips | 0c6daf0 | 2019-05-16 12:43:11 -0400 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | #endif |