blob: f7f090eb19497a0a1f31d79b8bd7765bd2f71089 [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
Robert Phillipse19babf2020-04-06 13:57:30 -04008#include "include/core/SkCanvas.h"
Mike Kleinfe0aeb32019-05-20 10:55:11 -05009#include "include/core/SkSurface.h"
Robert Phillips02dc0302019-07-02 17:58:27 -040010#include "include/core/SkSurfaceCharacterization.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040011#include "include/gpu/GrDirectContext.h"
Robert Phillips459b2952019-05-23 09:38:27 -040012#include "src/core/SkAutoPixmapStorage.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon04e3e502020-12-16 15:55:25 -050014#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrSurfaceFillContext.h"
16#include "src/gpu/effects/GrBlendFragmentProcessor.h"
17#include "src/gpu/effects/generated/GrConstColorProcessor.h"
Robert Phillipsefb9f142019-05-17 14:19:44 -040018#include "src/image/SkImage_Base.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040019#include "tests/Test.h"
Robert Phillipse3b6fe42019-09-11 11:26:46 -040020#include "tests/TestUtils.h"
21#include "tools/ToolUtils.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040022
Robert Phillips27eb5252019-06-03 12:59:40 -040023#ifdef SK_GL
Robert Phillipsee946932019-12-18 11:16:17 -050024#include "src/gpu/gl/GrGLCaps.h"
25#include "src/gpu/gl/GrGLDefines.h"
Robert Phillips27eb5252019-06-03 12:59:40 -040026#include "src/gpu/gl/GrGLGpu.h"
27#include "src/gpu/gl/GrGLUtil.h"
28#endif
29
Robert Phillips7f367982019-09-26 14:01:36 -040030#ifdef SK_METAL
31#include "include/gpu/mtl/GrMtlTypes.h"
32#include "src/gpu/mtl/GrMtlCppUtil.h"
33#endif
34
Robert Phillipsfe4b4812020-07-17 14:15:51 -040035static void wait_on_backend_work_to_finish(GrDirectContext* dContext, bool* finishedCreate) {
36 dContext->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -040037 while (finishedCreate && !(*finishedCreate)) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040038 dContext->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -040039 }
40 if (finishedCreate) {
41 // The same boolean (pointed to by finishedCreate) is often used multiply and sequentially
42 // throughout our tests to create different backend textures.
43 // Reset it here so that it can be use to signal a future backend texture's creation
44 *finishedCreate = false;
45 }
Greg Danielb2365d82020-05-13 15:32:04 -040046}
47
Robert Phillipsfe4b4812020-07-17 14:15:51 -040048static void delete_backend_texture(GrDirectContext* dContext,
49 const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -040050 bool* finishedCreate) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040051 wait_on_backend_work_to_finish(dContext, finishedCreate);
52 dContext->deleteBackendTexture(backendTexture);
Greg Danielc1ad77c2020-05-06 11:40:03 -040053}
54
Greg Danielb2365d82020-05-13 15:32:04 -040055static void mark_signaled(void* context) {
56 *(bool*)context = true;
57}
58
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -040059// Test wrapping of GrBackendObjects in SkSurfaces and SkImages (non-static since used in Mtl test)
Robert Phillipsfe4b4812020-07-17 14:15:51 -040060void test_wrapping(GrDirectContext* dContext,
61 skiatest::Reporter* reporter,
62 std::function<GrBackendTexture (GrDirectContext*,
Brian Salomon7e67dca2020-07-21 09:27:25 -040063 GrMipmapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -040064 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040065 GrColorType grColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040066 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040067 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -040068 bool* finishedBECreate) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040069 GrResourceCache* cache = dContext->priv().getResourceCache();
Robert Phillips0c6daf02019-05-16 12:43:11 -040070
71 const int initialCount = cache->getResourceCount();
72
Robert Phillipsfe4b4812020-07-17 14:15:51 -040073 GrBackendTexture backendTex = create(dContext, mipMapped, renderable);
Robert Phillipsefb9f142019-05-17 14:19:44 -040074 if (!backendTex.isValid()) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040075 ERRORF(reporter, "Couldn't create backendTexture for grColorType %d renderable %s\n",
76 grColorType,
Robert Phillips0c6daf02019-05-16 12:43:11 -040077 GrRenderable::kYes == renderable ? "yes" : "no");
78 return;
79 }
Robert Phillipsb04b6942019-05-21 17:24:31 -040080
Robert Phillips0c6daf02019-05-16 12:43:11 -040081 // Skia proper should know nothing about the new backend object
82 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
83
Robert Phillipsb7f95d12019-07-26 11:13:19 -040084 SkColorType skColorType = GrColorTypeToSkColorType(grColorType);
85
Brian Salomon04e3e502020-12-16 15:55:25 -050086 // Wrapping a backendTexture in an SkImage/SkSurface requires an SkColorType
87 if (skColorType == kUnknown_SkColorType) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040088 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillipsb04b6942019-05-21 17:24:31 -040089 return;
90 }
91
Robert Phillipsfe4b4812020-07-17 14:15:51 -040092 if (GrRenderable::kYes == renderable && dContext->colorTypeSupportedAsSurface(skColorType)) {
93 sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(dContext,
Robert Phillips459b2952019-05-23 09:38:27 -040094 backendTex,
95 kTopLeft_GrSurfaceOrigin,
96 0,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040097 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -040098 nullptr, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -040099 if (!surf) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500100 ERRORF(reporter, "Couldn't make SkSurface from backendTexture for %s\n",
Robert Phillips9a30ee02020-04-29 08:58:39 -0400101 ToolUtils::colortype_name(skColorType));
Robert Phillipsb04b6942019-05-21 17:24:31 -0400102 } else {
103 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400104 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400105 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400106
Robert Phillipsb04b6942019-05-21 17:24:31 -0400107 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400108 sk_sp<SkImage> img = SkImage::MakeFromTexture(dContext,
Robert Phillips459b2952019-05-23 09:38:27 -0400109 backendTex,
110 kTopLeft_GrSurfaceOrigin,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400111 skColorType,
Brian Salomon04e3e502020-12-16 15:55:25 -0500112 kUnpremul_SkAlphaType,
Robert Phillips459b2952019-05-23 09:38:27 -0400113 nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400114 if (!img) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500115 ERRORF(reporter, "Couldn't make SkImage from backendTexture for %s\n",
Robert Phillips9a30ee02020-04-29 08:58:39 -0400116 ToolUtils::colortype_name(skColorType));
Robert Phillipsb04b6942019-05-21 17:24:31 -0400117 } else {
118 SkImage_Base* ib = as_IB(img);
Robert Phillipsefb9f142019-05-17 14:19:44 -0400119
Robert Phillipsb04b6942019-05-21 17:24:31 -0400120 GrTextureProxy* proxy = ib->peekProxy();
121 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsefb9f142019-05-17 14:19:44 -0400122
Brian Salomon8c82a872020-07-21 12:09:58 -0400123 REPORTER_ASSERT(reporter, mipMapped == proxy->proxyMipmapped());
Robert Phillipsb04b6942019-05-21 17:24:31 -0400124 REPORTER_ASSERT(reporter, proxy->isInstantiated());
Brian Salomon8c82a872020-07-21 12:09:58 -0400125 REPORTER_ASSERT(reporter, mipMapped == proxy->mipmapped());
Robert Phillipsefb9f142019-05-17 14:19:44 -0400126
Robert Phillipsb04b6942019-05-21 17:24:31 -0400127 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400128 }
129 }
130
131 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
132
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400133 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips0c6daf02019-05-16 12:43:11 -0400134}
135
Robert Phillips9a30ee02020-04-29 08:58:39 -0400136static bool isBGRA8(const GrBackendFormat& format) {
Robert Phillips7f367982019-09-26 14:01:36 -0400137 switch (format.backend()) {
Robert Phillips7f367982019-09-26 14:01:36 -0400138 case GrBackendApi::kOpenGL:
139#ifdef SK_GL
140 return format.asGLFormat() == GrGLFormat::kBGRA8;
141#else
142 return false;
143#endif
144 case GrBackendApi::kVulkan: {
145#ifdef SK_VULKAN
146 VkFormat vkFormat;
147 format.asVkFormat(&vkFormat);
148 return vkFormat == VK_FORMAT_B8G8R8A8_UNORM;
149#else
150 return false;
151#endif
152 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500153 case GrBackendApi::kMetal:
154#ifdef SK_METAL
Robert Phillips9a30ee02020-04-29 08:58:39 -0400155 return GrMtlFormatIsBGRA8(format.asMtlFormat());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500156#else
157 return false;
158#endif
159 case GrBackendApi::kDirect3D:
160#ifdef SK_DIRECT3D
161 return false; // TODO
162#else
163 return false;
164#endif
165 case GrBackendApi::kDawn:
Stephen White36248742020-06-10 22:24:57 -0400166#ifdef SK_DAWN
167 wgpu::TextureFormat dawnFormat;
168 format.asDawnFormat(&dawnFormat);
169 return dawnFormat == wgpu::TextureFormat::BGRA8Unorm;
170#else
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500171 return false;
Stephen White36248742020-06-10 22:24:57 -0400172#endif
Robert Phillipsa27d6252019-12-10 14:48:36 -0500173 case GrBackendApi::kMock: {
174 SkImage::CompressionType compression = format.asMockCompressionType();
175 if (compression != SkImage::CompressionType::kNone) {
176 return false; // No compressed formats are BGRA
177 }
178
Robert Phillips7f367982019-09-26 14:01:36 -0400179 return format.asMockColorType() == GrColorType::kBGRA_8888;
Robert Phillipsa27d6252019-12-10 14:48:36 -0500180 }
Robert Phillips7f367982019-09-26 14:01:36 -0400181 }
182 SkUNREACHABLE;
183}
184
185static bool isRGB(const GrBackendFormat& format) {
186 switch (format.backend()) {
Robert Phillips7f367982019-09-26 14:01:36 -0400187 case GrBackendApi::kOpenGL:
188#ifdef SK_GL
189 return format.asGLFormat() == GrGLFormat::kRGB8;
190#else
191 return false;
192#endif
193 case GrBackendApi::kVulkan: {
194#ifdef SK_VULKAN
195 VkFormat vkFormat;
196 format.asVkFormat(&vkFormat);
197 return vkFormat == VK_FORMAT_R8G8B8_UNORM;
198#else
199 return false;
200#endif
201 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500202 case GrBackendApi::kMetal:
203 return false; // Metal doesn't even pretend to support this
204 case GrBackendApi::kDirect3D:
205 return false; // Not supported in Direct3D 12
206 case GrBackendApi::kDawn:
207 return false;
Robert Phillips7f367982019-09-26 14:01:36 -0400208 case GrBackendApi::kMock:
209 return false; // No GrColorType::kRGB_888
210 }
211 SkUNREACHABLE;
212}
213
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400214static void check_solid_pixmap(skiatest::Reporter* reporter,
Brian Salomon04e3e502020-12-16 15:55:25 -0500215 const SkColor4f& expected,
216 const SkPixmap& actual,
217 GrColorType ct,
218 const char* label1,
219 const char* label2) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400220 // we need 0.001f across the board just for noise
221 // we need 0.01f across the board for 1010102
Robert Phillips7f367982019-09-26 14:01:36 -0400222 const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f };
Robert Phillips27eb5252019-06-03 12:59:40 -0400223
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400224 auto error = std::function<ComparePixmapsErrorReporter>(
Robert Phillips7f367982019-09-26 14:01:36 -0400225 [reporter, ct, label1, label2](int x, int y, const float diffs[4]) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400226 SkASSERT(x >= 0 && y >= 0);
Brian Salomon04e3e502020-12-16 15:55:25 -0500227 ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)", GrColorTypeToStr(ct),
228 label1, label2, x, y, diffs[0], diffs[1], diffs[2], diffs[3]);
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400229 });
230
Brian Salomon28a8f282019-10-24 20:07:39 -0400231 CheckSolidPixels(expected, actual, tols, error);
Robert Phillips27eb5252019-06-03 12:59:40 -0400232}
233
Brian Salomon04e3e502020-12-16 15:55:25 -0500234// Determine what color we expect if we store 'orig' in 'ct' converted back to SkColor4f.
235static SkColor4f get_expected_color(SkColor4f orig, GrColorType ct) {
236 GrImageInfo ii(ct, kUnpremul_SkAlphaType, nullptr, {1, 1});
237 std::unique_ptr<char[]> data(new char[ii.minRowBytes()]);
238 GrClearImage(ii, data.get(), ii.minRowBytes(), orig);
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400239
Brian Salomon04e3e502020-12-16 15:55:25 -0500240 // Read back to SkColor4f.
241 SkColor4f result;
242 GrImageInfo resultII(GrColorType::kRGBA_F32, kUnpremul_SkAlphaType, nullptr, {1, 1});
243 GrConvertPixels(resultII, &result.fR, sizeof(result), ii, data.get(), ii.minRowBytes());
244 return result;
Robert Phillips459b2952019-05-23 09:38:27 -0400245}
246
Brian Salomon04e3e502020-12-16 15:55:25 -0500247static void check_mipmaps(GrDirectContext*,
248 const GrBackendTexture&,
249 GrColorType,
250 const SkColor4f expectedColors[6],
251 skiatest::Reporter*,
252 const char* label);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400253
Brian Salomon04e3e502020-12-16 15:55:25 -0500254static void check_base_readbacks(GrDirectContext* dContext,
255 const GrBackendTexture& backendTex,
256 GrColorType colorType,
257 GrRenderable renderableTexture,
258 const SkColor4f& color,
259 skiatest::Reporter* reporter,
Robert Phillips7f367982019-09-26 14:01:36 -0400260 const char* label) {
261 if (isRGB(backendTex.getBackendFormat())) {
262 // readPixels is busted for the RGB backend format (skbug.com/8862)
263 // TODO: add a GrColorType::kRGB_888 to fix the situation
264 return;
265 }
Robert Phillips0ee10342019-09-25 09:55:16 -0400266
Brian Salomon04e3e502020-12-16 15:55:25 -0500267 SkColor4f expectedColor = get_expected_color(color, colorType);
Robert Phillips0ee10342019-09-25 09:55:16 -0400268
269 SkAutoPixmapStorage actual;
270
271 {
Brian Salomon04e3e502020-12-16 15:55:25 -0500272 SkImageInfo readBackII = SkImageInfo::Make(32, 32,
273 kRGBA_8888_SkColorType,
Robert Phillips7f367982019-09-26 14:01:36 -0400274 kUnpremul_SkAlphaType);
Robert Phillips0ee10342019-09-25 09:55:16 -0400275
276 SkAssertResult(actual.tryAlloc(readBackII));
277 }
Brian Salomon04e3e502020-12-16 15:55:25 -0500278 for (GrRenderable renderableCtx : {GrRenderable::kNo, GrRenderable::kYes}) {
279 if (renderableCtx == GrRenderable::kYes && renderableTexture == GrRenderable::kNo) {
280 continue;
Brian Salomon0263bff2020-12-16 08:41:39 -0500281 }
Brian Salomon04e3e502020-12-16 15:55:25 -0500282 sk_sp<GrSurfaceProxy> proxy;
283 if (renderableCtx == GrRenderable::kYes) {
284 proxy = dContext->priv().proxyProvider()->wrapRenderableBackendTexture(
285 backendTex, 1, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, nullptr);
286 } else {
287 proxy = dContext->priv().proxyProvider()->wrapBackendTexture(
288 backendTex, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
289 }
290 if (!proxy) {
291 ERRORF(reporter, "Could not make proxy from backend texture");
292 return;
293 }
294 auto swizzle = dContext->priv().caps()->getReadSwizzle(backendTex.getBackendFormat(),
295 colorType);
296 GrSurfaceProxyView readView(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
297 GrColorInfo info(colorType, kUnpremul_SkAlphaType, nullptr);
298 auto surfaceContext = GrSurfaceContext::Make(dContext, readView, info);
299 if (!surfaceContext) {
300 ERRORF(reporter, "Could not create surface context for colorType: %d\n", colorType);
301 }
Brian Salomon0263bff2020-12-16 08:41:39 -0500302
Brian Salomon04e3e502020-12-16 15:55:25 -0500303 if (!surfaceContext->readPixels(dContext, actual.info(), actual.writable_addr(),
304 actual.rowBytes(), {0, 0})) {
305 // TODO: we need a better way to tell a priori if readPixels will work for an
306 // arbitrary colorType
307#if 0
308 ERRORF(reporter, "Couldn't readback from GrSurfaceContext for colorType: %d\n",
309 colorType);
310#endif
311 } else {
312 auto name = SkStringPrintf("%s::readPixels",
313 (renderableCtx == GrRenderable::kYes ? "GrSurfaceFillContext"
314 : "GrSurfaceContext"));
315 check_solid_pixmap(reporter, expectedColor, actual, colorType, label, name.c_str());
Robert Phillips7f367982019-09-26 14:01:36 -0400316 }
317 }
Robert Phillips0ee10342019-09-25 09:55:16 -0400318}
319
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400320// Test initialization of GrBackendObjects to a specific color (non-static since used in Mtl test)
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400321void test_color_init(GrDirectContext* dContext,
322 skiatest::Reporter* reporter,
Brian Salomon04e3e502020-12-16 15:55:25 -0500323 std::function<GrBackendTexture(GrDirectContext*,
324 const SkColor4f&,
325 GrMipmapped,
326 GrRenderable)> create,
327 GrColorType colorType,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400328 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400329 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400330 GrRenderable renderable,
331 bool* finishedBECreate) {
332 GrBackendTexture backendTex = create(dContext, color, mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400333 if (!backendTex.isValid()) {
334 // errors here should be reported by the test_wrapping test
335 return;
336 }
337
Greg Danielb2365d82020-05-13 15:32:04 -0400338 auto checkBackendTexture = [&](const SkColor4f& testColor) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400339 if (mipMapped == GrMipmapped::kYes) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500340 SkColor4f expectedColor = get_expected_color(testColor, colorType);
Greg Danielb2365d82020-05-13 15:32:04 -0400341 SkColor4f expectedColors[6] = {expectedColor, expectedColor, expectedColor,
342 expectedColor, expectedColor, expectedColor};
Brian Salomon04e3e502020-12-16 15:55:25 -0500343 check_mipmaps(dContext, backendTex, colorType, expectedColors, reporter, "colorinit");
Greg Danielb2365d82020-05-13 15:32:04 -0400344 }
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400345
Greg Danielb2365d82020-05-13 15:32:04 -0400346 // The last step in this test will dirty the mipmaps so do it last
Brian Salomon04e3e502020-12-16 15:55:25 -0500347 check_base_readbacks(dContext, backendTex, colorType, renderable, testColor, reporter,
348 "colorinit");
Greg Danielb2365d82020-05-13 15:32:04 -0400349 };
350
351 checkBackendTexture(color);
352
353 // Make sure the initial create work has finished so we can test the update independently.
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400354 wait_on_backend_work_to_finish(dContext, finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400355
356 SkColor4f newColor = {color.fB , color.fR, color.fG, color.fA };
357
Brian Salomon04e3e502020-12-16 15:55:25 -0500358 SkColorType skColorType = GrColorTypeToSkColorType(colorType);
Greg Daniel373d7dd2020-07-21 10:41:50 -0400359 dContext->updateBackendTexture(backendTex, skColorType, newColor, mark_signaled,
360 finishedBECreate);
Brian Salomon04e3e502020-12-16 15:55:25 -0500361 // Our update method only works with SkColorTypes.
362 if (skColorType != kUnknown_SkColorType) {
363 checkBackendTexture(newColor);
364 }
Greg Danielb2365d82020-05-13 15:32:04 -0400365
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400366 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips459b2952019-05-23 09:38:27 -0400367}
368
Brian Salomon04e3e502020-12-16 15:55:25 -0500369// Draw the backend texture into an RGBA surface fill context, attempting to access all the mipMap
370// levels.
371static void check_mipmaps(GrDirectContext* dContext,
372 const GrBackendTexture& backendTex,
373 GrColorType colorType,
374 const SkColor4f expectedColors[6],
375 skiatest::Reporter* reporter,
376 const char* label) {
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400377#ifdef SK_GL
378 // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices)
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400379 if (GrBackendApi::kOpenGL == dContext->backend()) {
380 GrGLGpu* glGPU = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400381
Brian Salomon04e3e502020-12-16 15:55:25 -0500382 if (colorType == GrColorType::kRGBA_F32 &&
383 glGPU->ctxInfo().standard() == kGLES_GrGLStandard) {
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400384 return;
385 }
386 }
387#endif
388
Robert Phillips7f367982019-09-26 14:01:36 -0400389 if (isRGB(backendTex.getBackendFormat())) {
390 // readPixels is busted for the RGB backend format (skbug.com/8862)
391 // TODO: add a GrColorType::kRGB_888 to fix the situation
392 return;
393 }
394
Brian Salomon04e3e502020-12-16 15:55:25 -0500395 GrImageInfo info(GrColorType::kRGBA_8888, kUnpremul_SkAlphaType, nullptr, {32, 32});
396 auto dstFillContext = GrSurfaceFillContext::Make(dContext, info);
397 if (!dstFillContext) {
398 ERRORF(reporter, "Could not make dst fill context.");
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400399 return;
400 }
401
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400402 int numMipLevels = 6;
403
Brian Salomon04e3e502020-12-16 15:55:25 -0500404 auto proxy = dContext->priv().proxyProvider()->wrapBackendTexture(backendTex,
405 kBorrow_GrWrapOwnership,
406 GrWrapCacheable::kNo,
407 kRW_GrIOType);
408 if (!proxy) {
409 ERRORF(reporter, "Could not make proxy from backend texture");
410 return;
411 }
412 auto swizzle = dContext->priv().caps()->getReadSwizzle(backendTex.getBackendFormat(),
413 colorType);
414 GrSurfaceProxyView readView(proxy, kTopLeft_GrSurfaceOrigin, swizzle);
415
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400416 for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
417 SkASSERT(rectSize >= 1);
Brian Salomon04e3e502020-12-16 15:55:25 -0500418 dstFillContext->clear(SK_PMColor4fTRANSPARENT);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400419
Brian Salomon04e3e502020-12-16 15:55:25 -0500420 SkMatrix texMatrix;
421 texMatrix.setScale(1 << i, 1 << i);
422 static constexpr GrSamplerState kNearestNearest(GrSamplerState::Filter::kNearest,
423 GrSamplerState::MipmapMode::kNearest);
424 auto fp = GrTextureEffect::Make(readView,
425 kUnpremul_SkAlphaType,
426 texMatrix,
427 kNearestNearest,
428 *dstFillContext->caps());
429 // Our swizzles for alpha color types currently produce (a, a, a, a) in the shader. Remove
430 // this once they are correctly (0, 0, 0, a).
431 if (GrColorTypeIsAlphaOnly(colorType)) {
432 auto black = GrConstColorProcessor::Make(SK_PMColor4fBLACK);
433 fp = GrBlendFragmentProcessor::Make(std::move(fp),
434 std::move(black),
435 SkBlendMode::kModulate);
436 }
437 dstFillContext->fillRectWithFP(SkIRect::MakeWH(rectSize, rectSize), std::move(fp));
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400438
439 SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
440 kRGBA_8888_SkColorType,
Robert Phillips7f367982019-09-26 14:01:36 -0400441 kUnpremul_SkAlphaType);
Brian Salomon04e3e502020-12-16 15:55:25 -0500442 SkAutoPixmapStorage actual;
443 SkAssertResult(actual.tryAlloc(readbackII));
444 actual.erase(SkColors::kTransparent);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400445
Brian Salomon04e3e502020-12-16 15:55:25 -0500446 bool result = dstFillContext->readPixels(dContext,
447 actual.info(),
448 actual.writable_addr(),
449 actual.rowBytes(),
450 {0, 0});
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400451 REPORTER_ASSERT(reporter, result);
452
Robert Phillipsee946932019-12-18 11:16:17 -0500453 SkString str;
454 str.appendf("mip-level %d", i);
455
Brian Salomon04e3e502020-12-16 15:55:25 -0500456 check_solid_pixmap(reporter, expectedColors[i], actual, colorType, label, str.c_str());
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400457 }
458}
459
Brian Salomon7e67dca2020-07-21 09:27:25 -0400460static int make_pixmaps(SkColorType skColorType, GrMipmapped mipMapped,
Robert Phillips7f367982019-09-26 14:01:36 -0400461 const SkColor4f colors[6], SkAutoPixmapStorage pixmaps[6]) {
462 int levelSize = 32;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400463 int numMipLevels = mipMapped == GrMipmapped::kYes ? 6 : 1;
Robert Phillips7f367982019-09-26 14:01:36 -0400464 for (int level = 0; level < numMipLevels; ++level) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500465 SkImageInfo ii = SkImageInfo::Make(levelSize,
466 levelSize,
467 skColorType,
468 kUnpremul_SkAlphaType);
Robert Phillips7f367982019-09-26 14:01:36 -0400469 pixmaps[level].alloc(ii);
470 pixmaps[level].erase(colors[level]);
471 levelSize /= 2;
472 }
473 return numMipLevels;
474}
475
476// Test initialization of GrBackendObjects using SkPixmaps
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400477static void test_pixmap_init(GrDirectContext* dContext,
478 skiatest::Reporter* reporter,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500479 std::function<GrBackendTexture(GrDirectContext*,
480 const SkPixmap srcData[],
481 int numLevels,
482 GrSurfaceOrigin,
483 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400484 SkColorType skColorType,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500485 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400486 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400487 GrRenderable renderable,
488 bool* finishedBECreate) {
Robert Phillips7f367982019-09-26 14:01:36 -0400489 SkAutoPixmapStorage pixmapMem[6];
490 SkColor4f colors[6] = {
491 { 1.0f, 0.0f, 0.0f, 1.0f }, // R
492 { 0.0f, 1.0f, 0.0f, 0.9f }, // G
493 { 0.0f, 0.0f, 1.0f, 0.7f }, // B
494 { 0.0f, 1.0f, 1.0f, 0.5f }, // C
495 { 1.0f, 0.0f, 1.0f, 0.3f }, // M
496 { 1.0f, 1.0f, 0.0f, 0.2f }, // Y
497 };
498
499 int numMipLevels = make_pixmaps(skColorType, mipMapped, colors, pixmapMem);
500 SkASSERT(numMipLevels);
501
502 // TODO: this is tedious. Should we pass in an array of SkBitmaps instead?
503 SkPixmap pixmaps[6];
504 for (int i = 0; i < numMipLevels; ++i) {
505 pixmaps[i].reset(pixmapMem[i].info(), pixmapMem[i].addr(), pixmapMem[i].rowBytes());
506 }
507
Brian Salomonb5f880a2020-12-07 11:30:16 -0500508 GrBackendTexture backendTex = create(dContext, pixmaps, numMipLevels, origin, renderable);
Robert Phillips7f367982019-09-26 14:01:36 -0400509 if (!backendTex.isValid()) {
510 // errors here should be reported by the test_wrapping test
511 return;
512 }
513
Robert Phillips9a30ee02020-04-29 08:58:39 -0400514 if (skColorType == kBGRA_8888_SkColorType && !isBGRA8(backendTex.getBackendFormat())) {
Robert Phillips7f367982019-09-26 14:01:36 -0400515 // When kBGRA is backed by an RGBA something goes wrong in the swizzling
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400516 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips7f367982019-09-26 14:01:36 -0400517 return;
518 }
519
Greg Danielb2365d82020-05-13 15:32:04 -0400520 auto checkBackendTexture = [&](SkColor4f colors[6]) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500521 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400522 if (mipMapped == GrMipmapped::kYes) {
Greg Danielb2365d82020-05-13 15:32:04 -0400523 SkColor4f expectedColors[6] = {
Brian Salomon04e3e502020-12-16 15:55:25 -0500524 get_expected_color(colors[0], grColorType),
525 get_expected_color(colors[1], grColorType),
526 get_expected_color(colors[2], grColorType),
527 get_expected_color(colors[3], grColorType),
528 get_expected_color(colors[4], grColorType),
529 get_expected_color(colors[5], grColorType),
Greg Danielb2365d82020-05-13 15:32:04 -0400530 };
Robert Phillips7f367982019-09-26 14:01:36 -0400531
Brian Salomon04e3e502020-12-16 15:55:25 -0500532 check_mipmaps(dContext, backendTex, grColorType, expectedColors, reporter, "pixmap");
Greg Danielb2365d82020-05-13 15:32:04 -0400533 }
534
535 // The last step in this test will dirty the mipmaps so do it last
Brian Salomon04e3e502020-12-16 15:55:25 -0500536 check_base_readbacks(dContext, backendTex, grColorType, renderable, colors[0], reporter,
Greg Danielb2365d82020-05-13 15:32:04 -0400537 "pixmap");
538 };
539
540 checkBackendTexture(colors);
541
542 // Make sure the initial create work has finished so we can test the update independently.
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400543 wait_on_backend_work_to_finish(dContext, finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400544
545 SkColor4f colorsNew[6] = {
546 {1.0f, 1.0f, 0.0f, 0.2f}, // Y
547 {1.0f, 0.0f, 0.0f, 1.0f}, // R
548 {0.0f, 1.0f, 0.0f, 0.9f}, // G
549 {0.0f, 0.0f, 1.0f, 0.7f}, // B
550 {0.0f, 1.0f, 1.0f, 0.5f}, // C
551 {1.0f, 0.0f, 1.0f, 0.3f}, // M
552 };
553 make_pixmaps(skColorType, mipMapped, colorsNew, pixmapMem);
554 for (int i = 0; i < numMipLevels; ++i) {
555 pixmaps[i].reset(pixmapMem[i].info(), pixmapMem[i].addr(), pixmapMem[i].rowBytes());
Robert Phillips7f367982019-09-26 14:01:36 -0400556 }
557
Greg Danielb2365d82020-05-13 15:32:04 -0400558 // Upload new data and make sure everything still works
Brian Salomonb5f880a2020-12-07 11:30:16 -0500559 dContext->updateBackendTexture(backendTex, pixmaps, numMipLevels, origin, mark_signaled,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400560 finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400561
562 checkBackendTexture(colorsNew);
563
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400564 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips7f367982019-09-26 14:01:36 -0400565}
566
Robert Phillips02dc0302019-07-02 17:58:27 -0400567enum class VkLayout {
568 kUndefined,
569 kReadOnlyOptimal,
Robert Phillips02dc0302019-07-02 17:58:27 -0400570};
571
572void check_vk_layout(const GrBackendTexture& backendTex, VkLayout layout) {
573#if defined(SK_VULKAN) && defined(SK_DEBUG)
574 VkImageLayout expected;
575
576 switch (layout) {
577 case VkLayout::kUndefined:
578 expected = VK_IMAGE_LAYOUT_UNDEFINED;
579 break;
580 case VkLayout::kReadOnlyOptimal:
581 expected = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
582 break;
Robert Phillips02dc0302019-07-02 17:58:27 -0400583 default:
584 SkUNREACHABLE;
585 }
586
587 GrVkImageInfo vkII;
588
589 if (backendTex.getVkImageInfo(&vkII)) {
590 SkASSERT(expected == vkII.fImageLayout);
591 SkASSERT(VK_IMAGE_TILING_OPTIMAL == vkII.fImageTiling);
592 }
593#endif
594}
595
596///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400597DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ColorTypeBackendAllocationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400598 auto context = ctxInfo.directContext();
Robert Phillips0c6daf02019-05-16 12:43:11 -0400599 const GrCaps* caps = context->priv().caps();
600
Robert Phillips459b2952019-05-23 09:38:27 -0400601 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400602 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400603
Robert Phillips0c6daf02019-05-16 12:43:11 -0400604 struct {
605 SkColorType fColorType;
Robert Phillips459b2952019-05-23 09:38:27 -0400606 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400607 } combinations[] = {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400608 { kAlpha_8_SkColorType, kTransCol },
609 { kRGB_565_SkColorType, SkColors::kRed },
610 { kARGB_4444_SkColorType, SkColors::kGreen },
611 { kRGBA_8888_SkColorType, SkColors::kBlue },
612 { kRGB_888x_SkColorType, SkColors::kCyan },
Robert Phillips459b2952019-05-23 09:38:27 -0400613 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400614 { kBGRA_8888_SkColorType, { 1, 0, 0, 1.0f } },
Robert Phillips9a30ee02020-04-29 08:58:39 -0400615 // TODO: readback is busted for *10A2 when alpha = 0.5f (perhaps premul vs. unpremul)
616 { kRGBA_1010102_SkColorType, { 0.25f, 0.5f, 0.75f, 1.0f }},
617 { kBGRA_1010102_SkColorType, { 0.25f, 0.5f, 0.75f, 1.0f }},
618 // RGB/BGR 101010x have no Ganesh correlate
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400619 { kRGB_101010x_SkColorType, { 0, 0.5f, 0, 0.5f } },
Mike Kleinf7eb0542020-02-11 12:19:08 -0600620 { kBGR_101010x_SkColorType, { 0, 0.5f, 0, 0.5f } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400621 { kGray_8_SkColorType, kGrayCol },
622 { kRGBA_F16Norm_SkColorType, SkColors::kLtGray },
623 { kRGBA_F16_SkColorType, SkColors::kYellow },
624 { kRGBA_F32_SkColorType, SkColors::kGray },
Robert Phillips7f367982019-09-26 14:01:36 -0400625 { kR8G8_unorm_SkColorType, { .25f, .75f, 0, 1 } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400626 { kR16G16_unorm_SkColorType, SkColors::kGreen },
627 { kA16_unorm_SkColorType, kTransCol },
628 { kA16_float_SkColorType, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -0400629 { kR16G16_float_SkColorType, { .25f, .75f, 0, 1 } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400630 { kR16G16B16A16_unorm_SkColorType,{ .25f, .5f, .75f, 1 } },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400631 };
632
Brian Salomon4dea72a2019-12-18 10:43:10 -0500633 static_assert(kLastEnum_SkColorType == SK_ARRAY_COUNT(combinations));
Robert Phillips0c6daf02019-05-16 12:43:11 -0400634
635 for (auto combo : combinations) {
636 SkColorType colorType = combo.fColorType;
637
Robert Phillips0c6daf02019-05-16 12:43:11 -0400638 if (GrBackendApi::kMetal == context->backend()) {
639 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
640 if (kRGBA_F32_SkColorType == combo.fColorType) {
641 continue;
642 }
643 }
644
Brian Salomon04e3e502020-12-16 15:55:25 -0500645 for (auto mipmapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
646 if (GrMipmapped::kYes == mipmapped && !caps->mipmapSupport()) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400647 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400648 }
649
Robert Phillipsefb9f142019-05-17 14:19:44 -0400650 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400651 if (!caps->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
652 renderable).isValid()) {
653 continue;
654 }
Robert Phillips7f367982019-09-26 14:01:36 -0400655
Robert Phillipsefb9f142019-05-17 14:19:44 -0400656 if (GrRenderable::kYes == renderable) {
657 if (kRGB_888x_SkColorType == combo.fColorType) {
658 // Ganesh can't perform the blends correctly when rendering this format
659 continue;
660 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400661 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400662
Robert Phillipsb04b6942019-05-21 17:24:31 -0400663 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400664 auto uninitCreateMtd = [colorType](GrDirectContext* dContext,
Brian Salomon04e3e502020-12-16 15:55:25 -0500665 GrMipmapped mipmapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -0400666 GrRenderable renderable) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500667 auto result = dContext->createBackendTexture(32, 32,
668 colorType,
669 mipmapped,
670 renderable,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400671 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -0400672 check_vk_layout(result, VkLayout::kUndefined);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400673
674#ifdef SK_DEBUG
675 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400676 GrBackendFormat format = dContext->defaultBackendFormat(colorType,
677 renderable);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400678 SkASSERT(format == result.getBackendFormat());
679 }
680#endif
681
Robert Phillips02dc0302019-07-02 17:58:27 -0400682 return result;
Robert Phillipsb04b6942019-05-21 17:24:31 -0400683 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400684
Robert Phillipsb04b6942019-05-21 17:24:31 -0400685 test_wrapping(context, reporter, uninitCreateMtd,
Brian Salomon04e3e502020-12-16 15:55:25 -0500686 SkColorTypeToGrColorType(colorType), mipmapped, renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400687 nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400688 }
Robert Phillips459b2952019-05-23 09:38:27 -0400689
Greg Danielc1ad77c2020-05-06 11:40:03 -0400690 bool finishedBackendCreation = false;
691 bool* finishedPtr = &finishedBackendCreation;
692
Robert Phillips459b2952019-05-23 09:38:27 -0400693 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400694 auto createWithColorMtd = [colorType, finishedPtr](GrDirectContext* dContext,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400695 const SkColor4f& color,
Brian Salomon04e3e502020-12-16 15:55:25 -0500696 GrMipmapped mipmapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400697 GrRenderable renderable) {
Brian Salomon04e3e502020-12-16 15:55:25 -0500698 auto result = dContext->createBackendTexture(32, 32,
699 colorType,
700 color,
701 mipmapped,
702 renderable,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400703 GrProtected::kNo,
Brian Salomon04e3e502020-12-16 15:55:25 -0500704 mark_signaled,
705 finishedPtr);
Greg Danieldddf7092020-05-06 11:52:37 -0400706 check_vk_layout(result, VkLayout::kReadOnlyOptimal);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400707
708#ifdef SK_DEBUG
709 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400710 GrBackendFormat format = dContext->defaultBackendFormat(colorType,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400711 renderable);
712 SkASSERT(format == result.getBackendFormat());
713 }
714#endif
715
Robert Phillips02dc0302019-07-02 17:58:27 -0400716 return result;
Robert Phillips459b2952019-05-23 09:38:27 -0400717 };
Robert Phillips459b2952019-05-23 09:38:27 -0400718 test_color_init(context, reporter, createWithColorMtd,
Brian Salomon04e3e502020-12-16 15:55:25 -0500719 SkColorTypeToGrColorType(colorType), combo.fColor, mipmapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400720 renderable, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400721 }
Robert Phillips7f367982019-09-26 14:01:36 -0400722
Brian Salomonb5f880a2020-12-07 11:30:16 -0500723 for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400724 auto createWithSrcDataMtd = [finishedPtr](GrDirectContext* dContext,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400725 const SkPixmap srcData[],
726 int numLevels,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500727 GrSurfaceOrigin origin,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400728 GrRenderable renderable) {
Robert Phillips9a30ee02020-04-29 08:58:39 -0400729 SkASSERT(srcData && numLevels);
Brian Salomonb5f880a2020-12-07 11:30:16 -0500730 auto result = dContext->createBackendTexture(srcData,
731 numLevels,
732 origin,
733 renderable,
734 GrProtected::kNo,
735 mark_signaled,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400736 finishedPtr);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400737 check_vk_layout(result, VkLayout::kReadOnlyOptimal);
Robert Phillips7f367982019-09-26 14:01:36 -0400738#ifdef SK_DEBUG
Robert Phillips9a30ee02020-04-29 08:58:39 -0400739 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400740 auto format = dContext->defaultBackendFormat(srcData[0].colorType(),
741 renderable);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400742 SkASSERT(format == result.getBackendFormat());
743 }
Robert Phillips7f367982019-09-26 14:01:36 -0400744#endif
Robert Phillips9a30ee02020-04-29 08:58:39 -0400745 return result;
746 };
Robert Phillips7f367982019-09-26 14:01:36 -0400747
Brian Salomonb5f880a2020-12-07 11:30:16 -0500748 test_pixmap_init(context,
749 reporter,
750 createWithSrcDataMtd,
751 colorType,
752 origin,
Brian Salomon04e3e502020-12-16 15:55:25 -0500753 mipmapped,
Brian Salomonb5f880a2020-12-07 11:30:16 -0500754 renderable,
755 finishedPtr);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400756 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400757 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400758 }
759 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400760}
761
Robert Phillipsefb9f142019-05-17 14:19:44 -0400762///////////////////////////////////////////////////////////////////////////////
763#ifdef SK_GL
764
Robert Phillips0c6daf02019-05-16 12:43:11 -0400765DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLBackendAllocationTest, reporter, ctxInfo) {
766 sk_gpu_test::GLTestContext* glCtx = ctxInfo.glContext();
767 GrGLStandard standard = glCtx->gl()->fStandard;
Robert Phillips6d344c32020-07-06 10:56:46 -0400768 auto context = ctxInfo.directContext();
Robert Phillipsefb9f142019-05-17 14:19:44 -0400769 const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(context->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400770
Robert Phillips459b2952019-05-23 09:38:27 -0400771 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400772 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400773
Robert Phillips0c6daf02019-05-16 12:43:11 -0400774 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400775 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400776 GrGLenum fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -0400777 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400778 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400779 { GrColorType::kRGBA_8888, GR_GL_RGBA8, SkColors::kRed },
780 { GrColorType::kRGBA_8888_SRGB, GR_GL_SRGB8_ALPHA8, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400781
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400782 { GrColorType::kRGB_888x, GR_GL_RGBA8, SkColors::kYellow },
783 { GrColorType::kRGB_888x, GR_GL_RGB8, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400784
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400785 { GrColorType::kBGRA_8888, GR_GL_RGBA8, SkColors::kBlue },
786 { GrColorType::kBGRA_8888, GR_GL_BGRA8, SkColors::kBlue },
787 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillips9a30ee02020-04-29 08:58:39 -0400788 { GrColorType::kRGBA_1010102, GR_GL_RGB10_A2, { 0.25f, 0.5f, 0.75f, 1.f }},
789 { GrColorType::kBGRA_1010102, GR_GL_RGB10_A2, { 0.25f, 0.5f, 0.75f, 1.f }},
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400790 { GrColorType::kBGR_565, GR_GL_RGB565, SkColors::kRed },
791 { GrColorType::kABGR_4444, GR_GL_RGBA4, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400792
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400793 { GrColorType::kAlpha_8, GR_GL_ALPHA8, kTransCol },
794 { GrColorType::kAlpha_8, GR_GL_R8, kTransCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400795
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400796 { GrColorType::kGray_8, GR_GL_LUMINANCE8, kGrayCol },
797 { GrColorType::kGray_8, GR_GL_R8, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400798
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400799 { GrColorType::kRGBA_F32, GR_GL_RGBA32F, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400800
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400801 { GrColorType::kRGBA_F16_Clamped, GR_GL_RGBA16F, SkColors::kLtGray },
802 { GrColorType::kRGBA_F16, GR_GL_RGBA16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400803
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400804 { GrColorType::kRG_88, GR_GL_RG8, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400805 { GrColorType::kAlpha_F16, GR_GL_R16F, { 1.0f, 0, 0, 0.5f } },
806 { GrColorType::kAlpha_F16, GR_GL_LUMINANCE16F, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400807
Robert Phillips429f0d32019-09-11 17:03:28 -0400808 { GrColorType::kAlpha_16, GR_GL_R16, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400809 { GrColorType::kRG_1616, GR_GL_RG16, SkColors::kYellow },
Robert Phillips66a46032019-06-18 08:00:42 -0400810
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400811 { GrColorType::kRGBA_16161616, GR_GL_RGBA16, SkColors::kLtGray },
812 { GrColorType::kRG_F16, GR_GL_RG16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400813 };
814
815 for (auto combo : combinations) {
Brian Salomon0f396992020-06-19 19:51:21 -0400816 for (GrGLenum target : {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE}) {
817 GrBackendFormat format = GrBackendFormat::MakeGL(combo.fFormat, target);
Robert Phillips0c6daf02019-05-16 12:43:11 -0400818
Brian Salomon0f396992020-06-19 19:51:21 -0400819 if (!glCaps->isFormatTexturable(format)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400820 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400821 }
822
Brian Salomon0f396992020-06-19 19:51:21 -0400823 if (GrColorType::kBGRA_8888 == combo.fColorType ||
824 GrColorType::kBGRA_1010102 == combo.fColorType) {
825 // We allow using a GL_RGBA8 or GR_GL_RGB10_A2 texture as BGRA on desktop GL but not
826 // ES
827 if (kGL_GrGLStandard != standard &&
828 (GR_GL_RGBA8 == combo.fFormat || GR_GL_RGB10_A2 == combo.fFormat)) {
829 continue;
830 }
831 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400832
Brian Salomon7e67dca2020-07-21 09:27:25 -0400833 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
834 if (GrMipmapped::kYes == mipMapped &&
Brian Salomon69100f02020-07-21 10:49:25 -0400835 (!glCaps->mipmapSupport() || target == GR_GL_TEXTURE_RECTANGLE)) {
Brian Salomon0f396992020-06-19 19:51:21 -0400836 continue;
Robert Phillipsefb9f142019-05-17 14:19:44 -0400837 }
838
Brian Salomon0f396992020-06-19 19:51:21 -0400839 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
840 if (GrRenderable::kYes == renderable) {
841 if (!glCaps->isFormatAsColorTypeRenderable(combo.fColorType, format)) {
842 continue;
843 }
Robert Phillips459b2952019-05-23 09:38:27 -0400844 }
845
Brian Salomon0f396992020-06-19 19:51:21 -0400846 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400847 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400848 GrMipmapped mipMapped,
Brian Salomon0f396992020-06-19 19:51:21 -0400849 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400850 return dContext->createBackendTexture(32, 32, format, mipMapped,
851 renderable, GrProtected::kNo);
Brian Salomon0f396992020-06-19 19:51:21 -0400852 };
Greg Danielc1ad77c2020-05-06 11:40:03 -0400853
Brian Salomon0f396992020-06-19 19:51:21 -0400854 test_wrapping(context, reporter, uninitCreateMtd, combo.fColorType,
855 mipMapped, renderable, nullptr);
Brian Salomon85c3d682019-11-04 15:04:54 -0500856 }
Robert Phillips459b2952019-05-23 09:38:27 -0400857
Brian Salomon0f396992020-06-19 19:51:21 -0400858 {
859 // We're creating backend textures without specifying a color type "view" of
860 // them at the public API level. Therefore, Ganesh will not apply any
861 // swizzles before writing the color to the texture. However, our validation
862 // code does rely on interpreting the texture contents via a SkColorType and
863 // therefore swizzles may be applied during the read step. Ideally we'd
864 // update our validation code to use a "raw" read that doesn't impose a
865 // color type but for now we just munge the data we upload to match the
866 // expectation.
867 GrSwizzle swizzle;
868 switch (combo.fColorType) {
869 case GrColorType::kAlpha_8:
870 swizzle = GrSwizzle("aaaa");
871 break;
872 case GrColorType::kAlpha_16:
873 swizzle = GrSwizzle("aaaa");
874 break;
875 case GrColorType::kAlpha_F16:
876 swizzle = GrSwizzle("aaaa");
877 break;
878 default:
879 break;
880 }
881
882 bool finishedBackendCreation = false;
883 bool* finishedPtr = &finishedBackendCreation;
884
885 auto createWithColorMtd = [format, swizzle, finishedPtr](
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400886 GrDirectContext* dContext,
Brian Salomon0f396992020-06-19 19:51:21 -0400887 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400888 GrMipmapped mipMapped,
Brian Salomon0f396992020-06-19 19:51:21 -0400889 GrRenderable renderable) {
890 auto swizzledColor = swizzle.applyTo(color);
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400891 return dContext->createBackendTexture(
Brian Salomon0f396992020-06-19 19:51:21 -0400892 32, 32, format, swizzledColor, mipMapped, renderable,
893 GrProtected::kNo, mark_signaled, finishedPtr);
894 };
Brian Salomon0f396992020-06-19 19:51:21 -0400895 test_color_init(context, reporter, createWithColorMtd, combo.fColorType,
Brian Salomon04e3e502020-12-16 15:55:25 -0500896 combo.fColor, mipMapped, renderable, finishedPtr);
Brian Salomon0f396992020-06-19 19:51:21 -0400897 }
Robert Phillips459b2952019-05-23 09:38:27 -0400898 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400899 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400900 }
901 }
902}
903
Robert Phillipsefb9f142019-05-17 14:19:44 -0400904#endif
905
906///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400907
908#ifdef SK_VULKAN
909
910#include "src/gpu/vk/GrVkCaps.h"
911
912DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendAllocationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400913 auto context = ctxInfo.directContext();
Robert Phillips0c6daf02019-05-16 12:43:11 -0400914 const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(context->priv().caps());
915
Robert Phillips459b2952019-05-23 09:38:27 -0400916 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -0400917 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 1 };
Robert Phillips459b2952019-05-23 09:38:27 -0400918
Robert Phillips0c6daf02019-05-16 12:43:11 -0400919 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400920 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400921 VkFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -0400922 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400923 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400924 { GrColorType::kRGBA_8888, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kRed },
925 { GrColorType::kRGBA_8888_SRGB, VK_FORMAT_R8G8B8A8_SRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400926
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400927 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format),
928 // there is nothing to tell Skia to make the provided color opaque. Clients will need
929 // to provide an opaque initialization color in this case.
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400930 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kYellow },
931 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8_UNORM, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400932
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400933 { GrColorType::kBGRA_8888, VK_FORMAT_B8G8R8A8_UNORM, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400934
Robert Phillips9a30ee02020-04-29 08:58:39 -0400935 { GrColorType::kRGBA_1010102, VK_FORMAT_A2B10G10R10_UNORM_PACK32,
936 { 0.25f, 0.5f, 0.75f, 1.0f }},
937 { GrColorType::kBGRA_1010102, VK_FORMAT_A2R10G10B10_UNORM_PACK32,
938 { 0.25f, 0.5f, 0.75f, 1.0f }},
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400939 { GrColorType::kBGR_565, VK_FORMAT_R5G6B5_UNORM_PACK16, SkColors::kRed },
Robert Phillipsefb9f142019-05-17 14:19:44 -0400940
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400941 { GrColorType::kABGR_4444, VK_FORMAT_R4G4B4A4_UNORM_PACK16, SkColors::kCyan },
942 { GrColorType::kABGR_4444, VK_FORMAT_B4G4R4A4_UNORM_PACK16, SkColors::kYellow },
Robert Phillipsefb9f142019-05-17 14:19:44 -0400943
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400944 { GrColorType::kAlpha_8, VK_FORMAT_R8_UNORM, kTransCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400945 // In this config (i.e., a Gray8 color type with an R8 backing format), there is nothing
946 // to tell Skia this isn't an Alpha8 color type (so it will initialize the texture with
947 // the alpha channel of the color). Clients should, in general, fill all the channels
948 // of the provided color with the same value in such cases.
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400949 { GrColorType::kGray_8, VK_FORMAT_R8_UNORM, kGrayCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400950
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400951 { GrColorType::kRGBA_F16_Clamped, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kLtGray },
952 { GrColorType::kRGBA_F16, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400953
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400954 { GrColorType::kRG_88, VK_FORMAT_R8G8_UNORM, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400955 { GrColorType::kAlpha_F16, VK_FORMAT_R16_SFLOAT, { 1.0f, 0, 0, 0.5f }},
956
Robert Phillips429f0d32019-09-11 17:03:28 -0400957 { GrColorType::kAlpha_16, VK_FORMAT_R16_UNORM, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400958 { GrColorType::kRG_1616, VK_FORMAT_R16G16_UNORM, SkColors::kYellow },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400959 { GrColorType::kRGBA_16161616, VK_FORMAT_R16G16B16A16_UNORM, SkColors::kLtGray },
960 { GrColorType::kRG_F16, VK_FORMAT_R16G16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400961 };
962
963 for (auto combo : combinations) {
Greg Daniel2f2caea2019-07-08 14:24:47 -0400964 if (!vkCaps->isVkFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400965 continue;
966 }
967
968 GrBackendFormat format = GrBackendFormat::MakeVk(combo.fFormat);
969
Brian Salomon7e67dca2020-07-21 09:27:25 -0400970 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -0400971 if (GrMipmapped::kYes == mipMapped && !vkCaps->mipmapSupport()) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400972 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400973 }
974
Robert Phillipsefb9f142019-05-17 14:19:44 -0400975 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -0400976
Robert Phillipsefb9f142019-05-17 14:19:44 -0400977 if (GrRenderable::kYes == renderable) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400978 // We must also check whether we allow rendering to the format using the
979 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -0400980 if (!vkCaps->isFormatAsColorTypeRenderable(
981 combo.fColorType, GrBackendFormat::MakeVk(combo.fFormat), 1)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -0400982 continue;
983 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400984 }
985
Robert Phillipsd34691b2019-09-24 13:38:43 -0400986 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400987 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400988 GrMipmapped mipMapped,
Robert Phillips459b2952019-05-23 09:38:27 -0400989 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400990 GrBackendTexture beTex = dContext->createBackendTexture(32, 32, format,
991 mipMapped,
992 renderable,
993 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -0400994 check_vk_layout(beTex, VkLayout::kUndefined);
Robert Phillipsd1d869d2019-06-07 14:21:31 -0400995 return beTex;
Robert Phillipsb04b6942019-05-21 17:24:31 -0400996 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400997
Robert Phillipsb04b6942019-05-21 17:24:31 -0400998 test_wrapping(context, reporter, uninitCreateMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400999 combo.fColorType, mipMapped, renderable, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -04001000 }
Robert Phillips459b2952019-05-23 09:38:27 -04001001
Robert Phillips459b2952019-05-23 09:38:27 -04001002 {
Brian Salomonb450f3b2019-07-09 09:36:51 -04001003 // We're creating backend textures without specifying a color type "view" of
1004 // them at the public API level. Therefore, Ganesh will not apply any swizzles
1005 // before writing the color to the texture. However, our validation code does
1006 // rely on interpreting the texture contents via a SkColorType and therefore
1007 // swizzles may be applied during the read step.
1008 // Ideally we'd update our validation code to use a "raw" read that doesn't
1009 // impose a color type but for now we just munge the data we upload to match the
1010 // expectation.
1011 GrSwizzle swizzle;
1012 switch (combo.fColorType) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001013 case GrColorType::kAlpha_8:
Brian Salomonb450f3b2019-07-09 09:36:51 -04001014 SkASSERT(combo.fFormat == VK_FORMAT_R8_UNORM);
1015 swizzle = GrSwizzle("aaaa");
1016 break;
Robert Phillips429f0d32019-09-11 17:03:28 -04001017 case GrColorType::kAlpha_16:
1018 SkASSERT(combo.fFormat == VK_FORMAT_R16_UNORM);
1019 swizzle = GrSwizzle("aaaa");
1020 break;
Robert Phillips17a3a0b2019-09-18 13:56:54 -04001021 case GrColorType::kAlpha_F16:
1022 SkASSERT(combo.fFormat == VK_FORMAT_R16_SFLOAT);
1023 swizzle = GrSwizzle("aaaa");
1024 break;
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001025 case GrColorType::kABGR_4444:
Brian Salomonb450f3b2019-07-09 09:36:51 -04001026 if (combo.fFormat == VK_FORMAT_B4G4R4A4_UNORM_PACK16) {
1027 swizzle = GrSwizzle("bgra");
1028 }
1029 break;
1030 default:
1031 swizzle = GrSwizzle("rgba");
1032 break;
1033 }
Greg Danielc1ad77c2020-05-06 11:40:03 -04001034
1035 bool finishedBackendCreation = false;
1036 bool* finishedPtr = &finishedBackendCreation;
1037
1038 auto createWithColorMtd = [format, swizzle, finishedPtr](
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001039 GrDirectContext* dContext,
1040 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001041 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001042 GrRenderable renderable) {
Brian Salomonb450f3b2019-07-09 09:36:51 -04001043 auto swizzledColor = swizzle.applyTo(color);
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001044 GrBackendTexture beTex = dContext->createBackendTexture(32, 32, format,
1045 swizzledColor,
1046 mipMapped,
1047 renderable,
1048 GrProtected::kNo,
1049 mark_signaled,
1050 finishedPtr);
Greg Danieldddf7092020-05-06 11:52:37 -04001051 check_vk_layout(beTex, VkLayout::kReadOnlyOptimal);
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001052 return beTex;
Robert Phillips459b2952019-05-23 09:38:27 -04001053 };
Robert Phillips459b2952019-05-23 09:38:27 -04001054 test_color_init(context, reporter, createWithColorMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001055 combo.fColorType, combo.fColor, mipMapped, renderable,
1056 finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -04001057 }
Robert Phillipsefb9f142019-05-17 14:19:44 -04001058 }
Robert Phillips0c6daf02019-05-16 12:43:11 -04001059 }
1060 }
1061}
1062
1063#endif