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