blob: 0b843bbf32f03e8cfad80c65bf43021951cf1690 [file] [log] [blame]
Robert Phillips0c6daf02019-05-16 12:43:11 -04001/*
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 Kleinfe0aeb32019-05-20 10:55:11 -05008#include "include/core/SkSurface.h"
Robert Phillips02dc0302019-07-02 17:58:27 -04009#include "include/core/SkSurfaceCharacterization.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050010#include "include/gpu/GrContext.h"
Robert Phillips459b2952019-05-23 09:38:27 -040011#include "src/core/SkAutoPixmapStorage.h"
Mike Klein4b432fa2019-06-06 11:44:05 -050012#include "src/gpu/GrContextPriv.h"
Robert Phillipsefb9f142019-05-17 14:19:44 -040013#include "src/image/SkImage_Base.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040014#include "tests/Test.h"
Robert Phillipse3b6fe42019-09-11 11:26:46 -040015#include "tests/TestUtils.h"
16#include "tools/ToolUtils.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040017
Robert Phillips27eb5252019-06-03 12:59:40 -040018#ifdef SK_GL
19#include "src/gpu/gl/GrGLGpu.h"
20#include "src/gpu/gl/GrGLUtil.h"
21#endif
22
Robert Phillips0c6daf02019-05-16 12:43:11 -040023// Test wrapping of GrBackendObjects in SkSurfaces and SkImages
24void test_wrapping(GrContext* context, skiatest::Reporter* reporter,
Robert Phillipsb04b6942019-05-21 17:24:31 -040025 std::function<GrBackendTexture (GrContext*,
26 GrMipMapped,
27 GrRenderable)> create,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040028 GrColorType grColorType, GrMipMapped mipMapped, GrRenderable renderable) {
Robert Phillips0c6daf02019-05-16 12:43:11 -040029 GrResourceCache* cache = context->priv().getResourceCache();
30
31 const int initialCount = cache->getResourceCount();
32
Robert Phillipsefb9f142019-05-17 14:19:44 -040033 GrBackendTexture backendTex = create(context, mipMapped, renderable);
34 if (!backendTex.isValid()) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040035 ERRORF(reporter, "Couldn't create backendTexture for grColorType %d renderable %s\n",
36 grColorType,
Robert Phillips0c6daf02019-05-16 12:43:11 -040037 GrRenderable::kYes == renderable ? "yes" : "no");
38 return;
39 }
Robert Phillipsb04b6942019-05-21 17:24:31 -040040
Robert Phillips0c6daf02019-05-16 12:43:11 -040041 // Skia proper should know nothing about the new backend object
42 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
43
Robert Phillipsb7f95d12019-07-26 11:13:19 -040044 SkColorType skColorType = GrColorTypeToSkColorType(grColorType);
45
46 // Wrapping a backendTexture in an image requires an SkColorType
47 if (kUnknown_SkColorType == skColorType) {
Robert Phillipsb04b6942019-05-21 17:24:31 -040048 context->deleteBackendTexture(backendTex);
49 return;
50 }
51
Robert Phillipsd470e1b2019-09-04 15:05:35 -040052 if (GrRenderable::kYes == renderable && context->colorTypeSupportedAsSurface(skColorType)) {
Robert Phillipsb04b6942019-05-21 17:24:31 -040053 sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context,
Robert Phillips459b2952019-05-23 09:38:27 -040054 backendTex,
55 kTopLeft_GrSurfaceOrigin,
56 0,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040057 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -040058 nullptr, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -040059 if (!surf) {
60 ERRORF(reporter, "Couldn't make surface from backendTexture for colorType %d\n",
Robert Phillipsb7f95d12019-07-26 11:13:19 -040061 skColorType);
Robert Phillipsb04b6942019-05-21 17:24:31 -040062 } else {
63 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -040064 }
Robert Phillipsb04b6942019-05-21 17:24:31 -040065 }
Robert Phillips0c6daf02019-05-16 12:43:11 -040066
Robert Phillipsb04b6942019-05-21 17:24:31 -040067 {
68 sk_sp<SkImage> img = SkImage::MakeFromTexture(context,
Robert Phillips459b2952019-05-23 09:38:27 -040069 backendTex,
70 kTopLeft_GrSurfaceOrigin,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040071 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -040072 kPremul_SkAlphaType,
73 nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -040074 if (!img) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040075 ERRORF(reporter, "Couldn't make image from backendTexture for skColorType %d\n",
76 skColorType);
Robert Phillipsb04b6942019-05-21 17:24:31 -040077 } else {
78 SkImage_Base* ib = as_IB(img);
Robert Phillipsefb9f142019-05-17 14:19:44 -040079
Robert Phillipsb04b6942019-05-21 17:24:31 -040080 GrTextureProxy* proxy = ib->peekProxy();
81 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsefb9f142019-05-17 14:19:44 -040082
Robert Phillipsb04b6942019-05-21 17:24:31 -040083 REPORTER_ASSERT(reporter, mipMapped == proxy->proxyMipMapped());
84 REPORTER_ASSERT(reporter, proxy->isInstantiated());
85 REPORTER_ASSERT(reporter, mipMapped == proxy->mipMapped());
Robert Phillipsefb9f142019-05-17 14:19:44 -040086
Robert Phillipsb04b6942019-05-21 17:24:31 -040087 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -040088 }
89 }
90
91 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
92
Robert Phillips5c7a25b2019-05-20 08:38:07 -040093 context->deleteBackendTexture(backendTex);
Robert Phillips0c6daf02019-05-16 12:43:11 -040094}
95
Robert Phillipse3b6fe42019-09-11 11:26:46 -040096static 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 Phillips27eb5252019-06-03 12:59:40 -0400102
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400103 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 Phillips27eb5252019-06-03 12:59:40 -0400112}
113
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400114static SkColor4f get_expected_color(SkColor4f orig, SkColorType ct) {
Robert Phillips459b2952019-05-23 09:38:27 -0400115
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400116 uint32_t components = SkColorTypeComponentFlags(ct);
Robert Phillips459b2952019-05-23 09:38:27 -0400117
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400118 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 Phillips459b2952019-05-23 09:38:27 -0400121 }
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400122
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 Phillips459b2952019-05-23 09:38:27 -0400141}
142
143// Test initialization of GrBackendObjects to a specific color
144void test_color_init(GrContext* context, skiatest::Reporter* reporter,
145 std::function<GrBackendTexture (GrContext*,
146 const SkColor4f&,
147 GrMipMapped,
148 GrRenderable)> create,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400149 GrColorType grColorType, const SkColor4f& color,
Robert Phillips459b2952019-05-23 09:38:27 -0400150 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 Phillipsb7f95d12019-07-26 11:13:19 -0400157 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 Phillips459b2952019-05-23 09:38:27 -0400161 // TODO: burrow in and scrappily check that data was uploaded!
162 context->deleteBackendTexture(backendTex);
163 return;
164 }
165
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400166 SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType
167 : kPremul_SkAlphaType;
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400168
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400169 SkImageInfo ii = SkImageInfo::Make(32, 32, skColorType, at);
Robert Phillips459b2952019-05-23 09:38:27 -0400170
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400171 SkColor4f expectedColor = get_expected_color(color, skColorType);
Robert Phillips459b2952019-05-23 09:38:27 -0400172
173 SkAutoPixmapStorage actual;
174 SkAssertResult(actual.tryAlloc(ii));
175 actual.erase(SkColors::kTransparent);
176
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400177 if (GrRenderable::kYes == renderable && context->colorTypeSupportedAsSurface(skColorType)) {
Robert Phillips459b2952019-05-23 09:38:27 -0400178 sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(context,
179 backendTex,
180 kTopLeft_GrSurfaceOrigin,
181 0,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400182 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -0400183 nullptr, nullptr);
184 if (surf) {
185 bool result = surf->readPixels(actual, 0, 0);
186 REPORTER_ASSERT(reporter, result);
187
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400188 check_solid_pixmap(reporter, expectedColor, actual, skColorType,
189 "SkSurface::readPixels");
Robert Phillips459b2952019-05-23 09:38:27 -0400190
191 actual.erase(SkColors::kTransparent);
192 }
193 }
194
195 {
196 sk_sp<SkImage> img = SkImage::MakeFromTexture(context,
197 backendTex,
198 kTopLeft_GrSurfaceOrigin,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400199 skColorType,
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400200 at,
Robert Phillips459b2952019-05-23 09:38:27 -0400201 nullptr);
202 if (img) {
Robert Phillips27eb5252019-06-03 12:59:40 -0400203 // 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 Phillips459b2952019-05-23 09:38:27 -0400209#if 0
Robert Phillips27eb5252019-06-03 12:59:40 -0400210 ERRORF(reporter, "Couldn't readback from SkImage for colorType: %d\n",
211 colorType);
Robert Phillips459b2952019-05-23 09:38:27 -0400212#endif
Robert Phillips27eb5252019-06-03 12:59:40 -0400213 } else {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400214 check_solid_pixmap(reporter, expectedColor, actual, skColorType,
215 "SkImage::readPixels");
Robert Phillips27eb5252019-06-03 12:59:40 -0400216 }
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 Phillipsb7f95d12019-07-26 11:13:19 -0400227 if (kRGBA_F32_SkColorType == skColorType && GrMipMapped::kYes == mipMapped &&
Robert Phillipsef032cd2019-06-03 15:12:50 -0400228 kGLES_GrGLStandard == glGPU->ctxInfo().standard()) {
Robert Phillips27eb5252019-06-03 12:59:40 -0400229 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 Phillips27eb5252019-06-03 12:59:40 -0400238 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 Phillipse3b6fe42019-09-11 11:26:46 -0400262 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 Phillips27eb5252019-06-03 12:59:40 -0400269 bool result = surf->readPixels(actual2, 0, 0);
270 REPORTER_ASSERT(reporter, result);
271
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400272 check_solid_pixmap(reporter, expectedColor, actual2, skColorType,
273 "mip-level failure");
Robert Phillips27eb5252019-06-03 12:59:40 -0400274 }
Robert Phillips459b2952019-05-23 09:38:27 -0400275 }
276 }
277 }
278
279 context->deleteBackendTexture(backendTex);
280}
281
Robert Phillips02dc0302019-07-02 17:58:27 -0400282enum class VkLayout {
283 kUndefined,
284 kReadOnlyOptimal,
285 kColorAttachmentOptimal
286};
287
288void 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.
319DEF_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 Phillipsd5e80ca2019-07-29 14:11:35 -0400355 {
356 GrBackendFormat format = context->defaultBackendFormat(
357 c.imageInfo().colorType(),
358 GrRenderable::kYes);
359 REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat());
360 }
361
Robert Phillips02dc0302019-07-02 17:58:27 -0400362 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 Phillipsd5e80ca2019-07-29 14:11:35 -0400379 {
380 GrBackendFormat format = context->defaultBackendFormat(
381 c.imageInfo().colorType(),
382 GrRenderable::kYes);
383 REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat());
384 }
385
Robert Phillips02dc0302019-07-02 17:58:27 -0400386 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 Phillipsefb9f142019-05-17 14:19:44 -0400400///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400401DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ColorTypeBackendAllocationTest, reporter, ctxInfo) {
402 GrContext* context = ctxInfo.grContext();
403 const GrCaps* caps = context->priv().caps();
404
Robert Phillips459b2952019-05-23 09:38:27 -0400405 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400406 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400407
Robert Phillips0c6daf02019-05-16 12:43:11 -0400408 struct {
409 SkColorType fColorType;
Robert Phillips459b2952019-05-23 09:38:27 -0400410 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400411 } combinations[] = {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400412 { 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 Phillips459b2952019-05-23 09:38:27 -0400417 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400418 { kBGRA_8888_SkColorType, { 1, 0, 0, 1.0f } },
Robert Phillips459b2952019-05-23 09:38:27 -0400419 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400420 { kRGBA_1010102_SkColorType, { .25f, .5f, .75f, 1.0f }},
Robert Phillipsb04b6942019-05-21 17:24:31 -0400421 // The kRGB_101010x_SkColorType has no Ganesh correlate
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400422 { 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 Phillips0c6daf02019-05-16 12:43:11 -0400433 };
434
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400435 GR_STATIC_ASSERT(kLastEnum_SkColorType == SK_ARRAY_COUNT(combinations));
Robert Phillips0c6daf02019-05-16 12:43:11 -0400436
437 for (auto combo : combinations) {
438 SkColorType colorType = combo.fColorType;
439
Robert Phillips0c6daf02019-05-16 12:43:11 -0400440 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 Phillipsefb9f142019-05-17 14:19:44 -0400447 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
448 if (GrMipMapped::kYes == mipMapped && !caps->mipMapSupport()) {
449 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400450 }
451
Robert Phillipsefb9f142019-05-17 14:19:44 -0400452 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400453 if (!caps->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
454 renderable).isValid()) {
455 continue;
456 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400457 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 Phillipsefb9f142019-05-17 14:19:44 -0400462 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400463
Robert Phillipsb04b6942019-05-21 17:24:31 -0400464 {
465 auto uninitCreateMtd = [colorType](GrContext* context,
466 GrMipMapped mipMapped,
467 GrRenderable renderable) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400468 auto result = context->createBackendTexture(32, 32, colorType,
469 mipMapped, renderable,
470 GrProtected::kNo);
471 check_vk_layout(result, VkLayout::kUndefined);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400472
473#ifdef SK_DEBUG
474 {
475 GrBackendFormat format = context->defaultBackendFormat(colorType,
476 renderable);
477 SkASSERT(format == result.getBackendFormat());
478 }
479#endif
480
Robert Phillips02dc0302019-07-02 17:58:27 -0400481 return result;
Robert Phillipsb04b6942019-05-21 17:24:31 -0400482 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400483
Robert Phillipsb04b6942019-05-21 17:24:31 -0400484 test_wrapping(context, reporter, uninitCreateMtd,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400485 SkColorTypeToGrColorType(colorType), mipMapped, renderable);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400486 }
Robert Phillips459b2952019-05-23 09:38:27 -0400487
488 {
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400489 // GL has difficulties reading back from these combinations. In particular,
490 // reading back kGray_8 is a mess.
Robert Phillips459b2952019-05-23 09:38:27 -0400491 if (GrBackendApi::kOpenGL == context->backend()) {
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400492 if (kAlpha_8_SkColorType == combo.fColorType ||
493 kGray_8_SkColorType == combo.fColorType) {
Robert Phillips459b2952019-05-23 09:38:27 -0400494 continue;
495 }
Robert Phillips459b2952019-05-23 09:38:27 -0400496 }
497
498 auto createWithColorMtd = [colorType](GrContext* context,
499 const SkColor4f& color,
500 GrMipMapped mipMapped,
501 GrRenderable renderable) {
Robert Phillips02dc0302019-07-02 17:58:27 -0400502 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 Phillipsd5e80ca2019-07-29 14:11:35 -0400508
509#ifdef SK_DEBUG
510 {
511 GrBackendFormat format = context->defaultBackendFormat(colorType,
512 renderable);
513 SkASSERT(format == result.getBackendFormat());
514 }
515#endif
516
Robert Phillips02dc0302019-07-02 17:58:27 -0400517 return result;
Robert Phillips459b2952019-05-23 09:38:27 -0400518 };
519
520 test_color_init(context, reporter, createWithColorMtd,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400521 SkColorTypeToGrColorType(colorType),
522 combo.fColor, mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400523 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400524 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400525 }
526 }
527
528}
529
Robert Phillipsefb9f142019-05-17 14:19:44 -0400530///////////////////////////////////////////////////////////////////////////////
531#ifdef SK_GL
532
533#include "src/gpu/gl/GrGLCaps.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -0400534#include "src/gpu/gl/GrGLDefines.h"
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400535#include "src/gpu/gl/GrGLUtil.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -0400536
537DEF_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 Phillipsefb9f142019-05-17 14:19:44 -0400541 const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(context->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400542
Robert Phillips459b2952019-05-23 09:38:27 -0400543 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400544 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400545
Robert Phillips0c6daf02019-05-16 12:43:11 -0400546 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400547 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400548 GrGLenum fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -0400549 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400550 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400551 { GrColorType::kRGBA_8888, GR_GL_RGBA8, SkColors::kRed },
552 { GrColorType::kRGBA_8888_SRGB, GR_GL_SRGB8_ALPHA8, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400553
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400554 { GrColorType::kRGB_888x, GR_GL_RGBA8, SkColors::kYellow },
555 { GrColorType::kRGB_888x, GR_GL_RGB8, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400556
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400557 { 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 Phillips0c6daf02019-05-16 12:43:11 -0400563
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400564 { GrColorType::kAlpha_8, GR_GL_ALPHA8, kTransCol },
565 { GrColorType::kAlpha_8, GR_GL_R8, kTransCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400566
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400567 { GrColorType::kGray_8, GR_GL_LUMINANCE8, kGrayCol },
568 { GrColorType::kGray_8, GR_GL_R8, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400569
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400570 { GrColorType::kRGBA_F32, GR_GL_RGBA32F, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400571
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400572 { GrColorType::kRGBA_F16_Clamped, GR_GL_RGBA16F, SkColors::kLtGray },
573 { GrColorType::kRGBA_F16, GR_GL_RGBA16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400574
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400575 { GrColorType::kRG_88, GR_GL_RG8, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400576 { GrColorType::kAlpha_F16, GR_GL_R16F, { 1.0f, 0, 0, 0.5f } },
577 { GrColorType::kAlpha_F16, GR_GL_LUMINANCE16F, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400578
Robert Phillips429f0d32019-09-11 17:03:28 -0400579 { GrColorType::kAlpha_16, GR_GL_R16, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400580 { GrColorType::kRG_1616, GR_GL_RG16, SkColors::kYellow },
Robert Phillips66a46032019-06-18 08:00:42 -0400581
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400582 { GrColorType::kRGBA_16161616, GR_GL_RGBA16, SkColors::kLtGray },
583 { GrColorType::kRG_F16, GR_GL_RG16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400584 };
585
586 for (auto combo : combinations) {
587 GrBackendFormat format = GrBackendFormat::MakeGL(combo.fFormat, GR_GL_TEXTURE_2D);
588
Greg Daniel7bfc9132019-08-14 14:23:53 -0400589 if (!glCaps->isFormatTexturable(format)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400590 continue;
591 }
Greg Daniele877dce2019-07-11 10:52:43 -0400592
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400593 if (GrColorType::kBGRA_8888 == combo.fColorType) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400594 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 Phillipsefb9f142019-05-17 14:19:44 -0400602 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
603 if (GrMipMapped::kYes == mipMapped && !glCaps->mipMapSupport()) {
604 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400605 }
606
Robert Phillipsefb9f142019-05-17 14:19:44 -0400607 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400608
Robert Phillipsefb9f142019-05-17 14:19:44 -0400609 if (GrRenderable::kYes == renderable) {
Greg Daniel900583a2019-08-06 12:05:31 -0400610 if (!glCaps->isFormatAsColorTypeRenderable(combo.fColorType, format)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400611 continue;
612 }
613 }
614
Robert Phillipsd34691b2019-09-24 13:38:43 -0400615 {
Robert Phillipsb04b6942019-05-21 17:24:31 -0400616 auto uninitCreateMtd = [format](GrContext* context,
617 GrMipMapped mipMapped,
618 GrRenderable renderable) {
619 return context->createBackendTexture(32, 32, format,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400620 mipMapped, renderable,
621 GrProtected::kNo);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400622 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400623
Robert Phillipsb04b6942019-05-21 17:24:31 -0400624 test_wrapping(context, reporter, uninitCreateMtd,
625 combo.fColorType, mipMapped, renderable);
626 }
Robert Phillips459b2952019-05-23 09:38:27 -0400627
628 {
629 // GL has difficulties reading back from these combinations
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400630 if (GrColorType::kAlpha_8 == combo.fColorType) {
Robert Phillips459b2952019-05-23 09:38:27 -0400631 continue;
632 }
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400633 if (GrRenderable::kYes != renderable) {
Robert Phillips459b2952019-05-23 09:38:27 -0400634 continue;
635 }
636
637 auto createWithColorMtd = [format](GrContext* context,
638 const SkColor4f& color,
639 GrMipMapped mipMapped,
640 GrRenderable renderable) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400641 return context->createBackendTexture(32, 32, format, color,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400642 mipMapped, renderable,
643 GrProtected::kNo);
Robert Phillips459b2952019-05-23 09:38:27 -0400644 };
645
646 test_color_init(context, reporter, createWithColorMtd,
647 combo.fColorType, combo.fColor, mipMapped, renderable);
648 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400649 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400650 }
651 }
652}
653
Robert Phillipsefb9f142019-05-17 14:19:44 -0400654#endif
655
656///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400657
658#ifdef SK_VULKAN
659
660#include "src/gpu/vk/GrVkCaps.h"
661
662DEF_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 Phillips459b2952019-05-23 09:38:27 -0400666 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400667 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400668
Robert Phillips0c6daf02019-05-16 12:43:11 -0400669 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400670 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400671 VkFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -0400672 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400673 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400674 { GrColorType::kRGBA_8888, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kRed },
675 { GrColorType::kRGBA_8888_SRGB, VK_FORMAT_R8G8B8A8_SRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400676
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400677 // 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 Phillipsb7f95d12019-07-26 11:13:19 -0400680 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kYellow },
681 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8_UNORM, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400682
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400683 { GrColorType::kBGRA_8888, VK_FORMAT_B8G8R8A8_UNORM, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400684
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400685 { 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 Phillipsefb9f142019-05-17 14:19:44 -0400687
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400688 { GrColorType::kABGR_4444, VK_FORMAT_R4G4B4A4_UNORM_PACK16, SkColors::kCyan },
689 { GrColorType::kABGR_4444, VK_FORMAT_B4G4R4A4_UNORM_PACK16, SkColors::kYellow },
Robert Phillipsefb9f142019-05-17 14:19:44 -0400690
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400691 { GrColorType::kAlpha_8, VK_FORMAT_R8_UNORM, kTransCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400692 // 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 Phillipsb7f95d12019-07-26 11:13:19 -0400696 { GrColorType::kGray_8, VK_FORMAT_R8_UNORM, kGrayCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400697
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400698 { GrColorType::kRGBA_F32, VK_FORMAT_R32G32B32A32_SFLOAT, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400699
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400700 { GrColorType::kRGBA_F16_Clamped, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kLtGray },
701 { GrColorType::kRGBA_F16, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400702
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400703 { GrColorType::kRG_88, VK_FORMAT_R8G8_UNORM, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400704 { GrColorType::kAlpha_F16, VK_FORMAT_R16_SFLOAT, { 1.0f, 0, 0, 0.5f }},
705
Robert Phillips429f0d32019-09-11 17:03:28 -0400706 { GrColorType::kAlpha_16, VK_FORMAT_R16_UNORM, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400707 { GrColorType::kRG_1616, VK_FORMAT_R16G16_UNORM, SkColors::kYellow },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400708 { GrColorType::kRGBA_16161616, VK_FORMAT_R16G16B16A16_UNORM, SkColors::kLtGray },
709 { GrColorType::kRG_F16, VK_FORMAT_R16G16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400710 };
711
712 for (auto combo : combinations) {
Greg Daniel2f2caea2019-07-08 14:24:47 -0400713 if (!vkCaps->isVkFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400714 continue;
715 }
716
717 GrBackendFormat format = GrBackendFormat::MakeVk(combo.fFormat);
718
Robert Phillipsefb9f142019-05-17 14:19:44 -0400719 for (auto mipMapped : { GrMipMapped::kNo, GrMipMapped::kYes }) {
720 if (GrMipMapped::kYes == mipMapped && !vkCaps->mipMapSupport()) {
721 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400722 }
723
Robert Phillipsefb9f142019-05-17 14:19:44 -0400724 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400725
Robert Phillipsefb9f142019-05-17 14:19:44 -0400726 if (GrRenderable::kYes == renderable) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400727 // We must also check whether we allow rendering to the format using the
728 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -0400729 if (!vkCaps->isFormatAsColorTypeRenderable(
730 combo.fColorType, GrBackendFormat::MakeVk(combo.fFormat), 1)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400731 continue;
732 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400733 }
734
Robert Phillipsd34691b2019-09-24 13:38:43 -0400735 {
Robert Phillipsb04b6942019-05-21 17:24:31 -0400736 auto uninitCreateMtd = [format](GrContext* context,
Robert Phillips459b2952019-05-23 09:38:27 -0400737 GrMipMapped mipMapped,
738 GrRenderable renderable) {
Robert Phillipsd1d869d2019-06-07 14:21:31 -0400739 GrBackendTexture beTex = context->createBackendTexture(32, 32, format,
740 mipMapped,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400741 renderable,
742 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -0400743 check_vk_layout(beTex, VkLayout::kUndefined);
Robert Phillipsd1d869d2019-06-07 14:21:31 -0400744 return beTex;
Robert Phillipsb04b6942019-05-21 17:24:31 -0400745 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400746
Robert Phillipsb04b6942019-05-21 17:24:31 -0400747 test_wrapping(context, reporter, uninitCreateMtd,
748 combo.fColorType, mipMapped, renderable);
749 }
Robert Phillips459b2952019-05-23 09:38:27 -0400750
Robert Phillips459b2952019-05-23 09:38:27 -0400751 {
Brian Salomonb450f3b2019-07-09 09:36:51 -0400752 // 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 Phillipsb7f95d12019-07-26 11:13:19 -0400762 case GrColorType::kAlpha_8:
Brian Salomonb450f3b2019-07-09 09:36:51 -0400763 SkASSERT(combo.fFormat == VK_FORMAT_R8_UNORM);
764 swizzle = GrSwizzle("aaaa");
765 break;
Robert Phillips429f0d32019-09-11 17:03:28 -0400766 case GrColorType::kAlpha_16:
767 SkASSERT(combo.fFormat == VK_FORMAT_R16_UNORM);
768 swizzle = GrSwizzle("aaaa");
769 break;
Robert Phillips17a3a0b2019-09-18 13:56:54 -0400770 case GrColorType::kAlpha_F16:
771 SkASSERT(combo.fFormat == VK_FORMAT_R16_SFLOAT);
772 swizzle = GrSwizzle("aaaa");
773 break;
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400774 case GrColorType::kABGR_4444:
Brian Salomonb450f3b2019-07-09 09:36:51 -0400775 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 Phillipsd1d869d2019-06-07 14:21:31 -0400788 GrBackendTexture beTex = context->createBackendTexture(32, 32, format,
Brian Salomonb450f3b2019-07-09 09:36:51 -0400789 swizzledColor,
790 mipMapped,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400791 renderable,
792 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -0400793 check_vk_layout(beTex, GrRenderable::kYes == renderable
794 ? VkLayout::kColorAttachmentOptimal
795 : VkLayout::kReadOnlyOptimal);
Robert Phillipsd1d869d2019-06-07 14:21:31 -0400796 return beTex;
Robert Phillips459b2952019-05-23 09:38:27 -0400797 };
Robert Phillips459b2952019-05-23 09:38:27 -0400798 test_color_init(context, reporter, createWithColorMtd,
799 combo.fColorType, combo.fColor, mipMapped, renderable);
800 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400801 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400802 }
803 }
804}
805
806#endif