blob: 3b19e18adca7dbcf876d62acc6d23131d03f99a4 [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"
Mike Klein4b432fa2019-06-06 11:44:05 -050013#include "src/gpu/GrContextPriv.h"
Robert Phillipsefb9f142019-05-17 14:19:44 -040014#include "src/image/SkImage_Base.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040015#include "tests/Test.h"
Robert Phillipse3b6fe42019-09-11 11:26:46 -040016#include "tests/TestUtils.h"
17#include "tools/ToolUtils.h"
Robert Phillips0c6daf02019-05-16 12:43:11 -040018
Robert Phillips27eb5252019-06-03 12:59:40 -040019#ifdef SK_GL
Robert Phillipsee946932019-12-18 11:16:17 -050020#include "src/gpu/gl/GrGLCaps.h"
21#include "src/gpu/gl/GrGLDefines.h"
Robert Phillips27eb5252019-06-03 12:59:40 -040022#include "src/gpu/gl/GrGLGpu.h"
23#include "src/gpu/gl/GrGLUtil.h"
24#endif
25
Robert Phillips7f367982019-09-26 14:01:36 -040026#ifdef SK_METAL
27#include "include/gpu/mtl/GrMtlTypes.h"
28#include "src/gpu/mtl/GrMtlCppUtil.h"
29#endif
30
Robert Phillipsfe4b4812020-07-17 14:15:51 -040031static void wait_on_backend_work_to_finish(GrDirectContext* dContext, bool* finishedCreate) {
32 dContext->submit();
Greg Danielc1ad77c2020-05-06 11:40:03 -040033 while (finishedCreate && !(*finishedCreate)) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040034 dContext->checkAsyncWorkCompletion();
Greg Danielc1ad77c2020-05-06 11:40:03 -040035 }
36 if (finishedCreate) {
37 // The same boolean (pointed to by finishedCreate) is often used multiply and sequentially
38 // throughout our tests to create different backend textures.
39 // Reset it here so that it can be use to signal a future backend texture's creation
40 *finishedCreate = false;
41 }
Greg Danielb2365d82020-05-13 15:32:04 -040042}
43
Robert Phillipsfe4b4812020-07-17 14:15:51 -040044static void delete_backend_texture(GrDirectContext* dContext,
45 const GrBackendTexture& backendTexture,
Greg Danielb2365d82020-05-13 15:32:04 -040046 bool* finishedCreate) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040047 wait_on_backend_work_to_finish(dContext, finishedCreate);
48 dContext->deleteBackendTexture(backendTexture);
Greg Danielc1ad77c2020-05-06 11:40:03 -040049}
50
Greg Danielb2365d82020-05-13 15:32:04 -040051static void mark_signaled(void* context) {
52 *(bool*)context = true;
53}
54
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -040055// Test wrapping of GrBackendObjects in SkSurfaces and SkImages (non-static since used in Mtl test)
Robert Phillipsfe4b4812020-07-17 14:15:51 -040056void test_wrapping(GrDirectContext* dContext,
57 skiatest::Reporter* reporter,
58 std::function<GrBackendTexture (GrDirectContext*,
Brian Salomon7e67dca2020-07-21 09:27:25 -040059 GrMipmapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -040060 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040061 GrColorType grColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -040062 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -040063 GrRenderable renderable,
Greg Danielc1ad77c2020-05-06 11:40:03 -040064 bool* finishedBECreate) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040065 GrResourceCache* cache = dContext->priv().getResourceCache();
Robert Phillips0c6daf02019-05-16 12:43:11 -040066
67 const int initialCount = cache->getResourceCount();
68
Robert Phillipsfe4b4812020-07-17 14:15:51 -040069 GrBackendTexture backendTex = create(dContext, mipMapped, renderable);
Robert Phillipsefb9f142019-05-17 14:19:44 -040070 if (!backendTex.isValid()) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -040071 ERRORF(reporter, "Couldn't create backendTexture for grColorType %d renderable %s\n",
72 grColorType,
Robert Phillips0c6daf02019-05-16 12:43:11 -040073 GrRenderable::kYes == renderable ? "yes" : "no");
74 return;
75 }
Robert Phillipsb04b6942019-05-21 17:24:31 -040076
Robert Phillips0c6daf02019-05-16 12:43:11 -040077 // Skia proper should know nothing about the new backend object
78 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
79
Robert Phillipsb7f95d12019-07-26 11:13:19 -040080 SkColorType skColorType = GrColorTypeToSkColorType(grColorType);
81
82 // Wrapping a backendTexture in an image requires an SkColorType
83 if (kUnknown_SkColorType == skColorType) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -040084 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillipsb04b6942019-05-21 17:24:31 -040085 return;
86 }
87
Robert Phillipsfe4b4812020-07-17 14:15:51 -040088 if (GrRenderable::kYes == renderable && dContext->colorTypeSupportedAsSurface(skColorType)) {
89 sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(dContext,
Robert Phillips459b2952019-05-23 09:38:27 -040090 backendTex,
91 kTopLeft_GrSurfaceOrigin,
92 0,
Robert Phillipsb7f95d12019-07-26 11:13:19 -040093 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -040094 nullptr, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -040095 if (!surf) {
Robert Phillips9a30ee02020-04-29 08:58:39 -040096 ERRORF(reporter, "Couldn't make surface from backendTexture for %s\n",
97 ToolUtils::colortype_name(skColorType));
Robert Phillipsb04b6942019-05-21 17:24:31 -040098 } else {
99 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400100 }
Robert Phillipsb04b6942019-05-21 17:24:31 -0400101 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400102
Robert Phillipsb04b6942019-05-21 17:24:31 -0400103 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400104 sk_sp<SkImage> img = SkImage::MakeFromTexture(dContext,
Robert Phillips459b2952019-05-23 09:38:27 -0400105 backendTex,
106 kTopLeft_GrSurfaceOrigin,
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400107 skColorType,
Robert Phillips459b2952019-05-23 09:38:27 -0400108 kPremul_SkAlphaType,
109 nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400110 if (!img) {
Robert Phillips9a30ee02020-04-29 08:58:39 -0400111 ERRORF(reporter, "Couldn't make image from backendTexture for %s\n",
112 ToolUtils::colortype_name(skColorType));
Robert Phillipsb04b6942019-05-21 17:24:31 -0400113 } else {
114 SkImage_Base* ib = as_IB(img);
Robert Phillipsefb9f142019-05-17 14:19:44 -0400115
Robert Phillipsb04b6942019-05-21 17:24:31 -0400116 GrTextureProxy* proxy = ib->peekProxy();
117 REPORTER_ASSERT(reporter, proxy);
Robert Phillipsefb9f142019-05-17 14:19:44 -0400118
Robert Phillipsb04b6942019-05-21 17:24:31 -0400119 REPORTER_ASSERT(reporter, mipMapped == proxy->proxyMipMapped());
120 REPORTER_ASSERT(reporter, proxy->isInstantiated());
121 REPORTER_ASSERT(reporter, mipMapped == proxy->mipMapped());
Robert Phillipsefb9f142019-05-17 14:19:44 -0400122
Robert Phillipsb04b6942019-05-21 17:24:31 -0400123 REPORTER_ASSERT(reporter, initialCount+1 == cache->getResourceCount());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400124 }
125 }
126
127 REPORTER_ASSERT(reporter, initialCount == cache->getResourceCount());
128
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400129 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips0c6daf02019-05-16 12:43:11 -0400130}
131
Robert Phillips9a30ee02020-04-29 08:58:39 -0400132static bool isBGRA8(const GrBackendFormat& format) {
Robert Phillips7f367982019-09-26 14:01:36 -0400133 switch (format.backend()) {
Robert Phillips7f367982019-09-26 14:01:36 -0400134 case GrBackendApi::kOpenGL:
135#ifdef SK_GL
136 return format.asGLFormat() == GrGLFormat::kBGRA8;
137#else
138 return false;
139#endif
140 case GrBackendApi::kVulkan: {
141#ifdef SK_VULKAN
142 VkFormat vkFormat;
143 format.asVkFormat(&vkFormat);
144 return vkFormat == VK_FORMAT_B8G8R8A8_UNORM;
145#else
146 return false;
147#endif
148 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500149 case GrBackendApi::kMetal:
150#ifdef SK_METAL
Robert Phillips9a30ee02020-04-29 08:58:39 -0400151 return GrMtlFormatIsBGRA8(format.asMtlFormat());
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500152#else
153 return false;
154#endif
155 case GrBackendApi::kDirect3D:
156#ifdef SK_DIRECT3D
157 return false; // TODO
158#else
159 return false;
160#endif
161 case GrBackendApi::kDawn:
Stephen White36248742020-06-10 22:24:57 -0400162#ifdef SK_DAWN
163 wgpu::TextureFormat dawnFormat;
164 format.asDawnFormat(&dawnFormat);
165 return dawnFormat == wgpu::TextureFormat::BGRA8Unorm;
166#else
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500167 return false;
Stephen White36248742020-06-10 22:24:57 -0400168#endif
Robert Phillipsa27d6252019-12-10 14:48:36 -0500169 case GrBackendApi::kMock: {
170 SkImage::CompressionType compression = format.asMockCompressionType();
171 if (compression != SkImage::CompressionType::kNone) {
172 return false; // No compressed formats are BGRA
173 }
174
Robert Phillips7f367982019-09-26 14:01:36 -0400175 return format.asMockColorType() == GrColorType::kBGRA_8888;
Robert Phillipsa27d6252019-12-10 14:48:36 -0500176 }
Robert Phillips7f367982019-09-26 14:01:36 -0400177 }
178 SkUNREACHABLE;
179}
180
181static bool isRGB(const GrBackendFormat& format) {
182 switch (format.backend()) {
Robert Phillips7f367982019-09-26 14:01:36 -0400183 case GrBackendApi::kOpenGL:
184#ifdef SK_GL
185 return format.asGLFormat() == GrGLFormat::kRGB8;
186#else
187 return false;
188#endif
189 case GrBackendApi::kVulkan: {
190#ifdef SK_VULKAN
191 VkFormat vkFormat;
192 format.asVkFormat(&vkFormat);
193 return vkFormat == VK_FORMAT_R8G8B8_UNORM;
194#else
195 return false;
196#endif
197 }
Jim Van Verthb01e12b2020-02-18 14:34:38 -0500198 case GrBackendApi::kMetal:
199 return false; // Metal doesn't even pretend to support this
200 case GrBackendApi::kDirect3D:
201 return false; // Not supported in Direct3D 12
202 case GrBackendApi::kDawn:
203 return false;
Robert Phillips7f367982019-09-26 14:01:36 -0400204 case GrBackendApi::kMock:
205 return false; // No GrColorType::kRGB_888
206 }
207 SkUNREACHABLE;
208}
209
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400210static void check_solid_pixmap(skiatest::Reporter* reporter,
211 const SkColor4f& expected, const SkPixmap& actual,
Robert Phillips7f367982019-09-26 14:01:36 -0400212 SkColorType ct, const char* label1, const char* label2) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400213 // we need 0.001f across the board just for noise
214 // we need 0.01f across the board for 1010102
Robert Phillips7f367982019-09-26 14:01:36 -0400215 const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f };
Robert Phillips27eb5252019-06-03 12:59:40 -0400216
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400217 auto error = std::function<ComparePixmapsErrorReporter>(
Robert Phillips7f367982019-09-26 14:01:36 -0400218 [reporter, ct, label1, label2](int x, int y, const float diffs[4]) {
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400219 SkASSERT(x >= 0 && y >= 0);
Robert Phillips7f367982019-09-26 14:01:36 -0400220 ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)",
221 ToolUtils::colortype_name(ct), label1, label2, x, y,
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400222 diffs[0], diffs[1], diffs[2], diffs[3]);
223 });
224
Brian Salomon28a8f282019-10-24 20:07:39 -0400225 CheckSolidPixels(expected, actual, tols, error);
Robert Phillips27eb5252019-06-03 12:59:40 -0400226}
227
Robert Phillips7f367982019-09-26 14:01:36 -0400228// What would raster do?
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400229static SkColor4f get_expected_color(SkColor4f orig, SkColorType ct) {
Robert Phillips7f367982019-09-26 14:01:36 -0400230 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType
231 : kPremul_SkAlphaType;
Robert Phillipse3b6fe42019-09-11 11:26:46 -0400232
Robert Phillips7f367982019-09-26 14:01:36 -0400233 SkImageInfo ii = SkImageInfo::Make(2, 2, ct, at);
234 SkAutoPixmapStorage pm;
235 pm.alloc(ii);
236 pm.erase(orig);
237 SkColor tmp = pm.getColor(0, 0);
238 return SkColor4f::FromColor(tmp);
Robert Phillips459b2952019-05-23 09:38:27 -0400239}
240
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400241static void check_mipmaps(GrDirectContext*, const GrBackendTexture&,
242 SkColorType, const SkColor4f expectedColors[6],
243 skiatest::Reporter*, const char* label);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400244
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400245static void check_base_readbacks(GrDirectContext* dContext, const GrBackendTexture& backendTex,
Robert Phillips0ee10342019-09-25 09:55:16 -0400246 SkColorType skColorType, GrRenderable renderable,
Robert Phillips7f367982019-09-26 14:01:36 -0400247 const SkColor4f& color, skiatest::Reporter* reporter,
248 const char* label) {
249 if (isRGB(backendTex.getBackendFormat())) {
250 // readPixels is busted for the RGB backend format (skbug.com/8862)
251 // TODO: add a GrColorType::kRGB_888 to fix the situation
252 return;
253 }
Robert Phillips0ee10342019-09-25 09:55:16 -0400254
255 SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType
256 : kPremul_SkAlphaType;
257
258 SkColor4f expectedColor = get_expected_color(color, skColorType);
259
260 SkAutoPixmapStorage actual;
261
262 {
263 SkImageInfo readBackII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
Robert Phillips7f367982019-09-26 14:01:36 -0400264 kUnpremul_SkAlphaType);
Robert Phillips0ee10342019-09-25 09:55:16 -0400265
266 SkAssertResult(actual.tryAlloc(readBackII));
267 }
268
Robert Phillips0ee10342019-09-25 09:55:16 -0400269 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400270 sk_sp<SkImage> img = SkImage::MakeFromTexture(dContext,
Robert Phillips0ee10342019-09-25 09:55:16 -0400271 backendTex,
272 kTopLeft_GrSurfaceOrigin,
273 skColorType,
274 at,
275 nullptr);
276 if (img) {
277 actual.erase(SkColors::kTransparent);
278 bool result = img->readPixels(actual, 0, 0);
279 if (!result) {
280 // TODO: we need a better way to tell a priori if readPixels will work for an
281 // arbitrary colorType
282#if 0
283 ERRORF(reporter, "Couldn't readback from SkImage for colorType: %d\n", colorType);
284#endif
285 } else {
286 check_solid_pixmap(reporter, expectedColor, actual, skColorType,
Robert Phillips7f367982019-09-26 14:01:36 -0400287 label, "SkImage::readPixels");
Robert Phillips0ee10342019-09-25 09:55:16 -0400288 }
289 }
290 }
Robert Phillips7f367982019-09-26 14:01:36 -0400291
292 // This will mark any mipmaps as dirty (bc that is what we do when we wrap a renderable
293 // backend texture) so it must be done last!
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400294 if (GrRenderable::kYes == renderable && dContext->colorTypeSupportedAsSurface(skColorType)) {
295 sk_sp<SkSurface> surf = SkSurface::MakeFromBackendTexture(dContext,
Robert Phillips7f367982019-09-26 14:01:36 -0400296 backendTex,
297 kTopLeft_GrSurfaceOrigin,
298 0,
299 skColorType,
300 nullptr, nullptr);
301 if (surf) {
302 actual.erase(SkColors::kTransparent);
303 bool result = surf->readPixels(actual, 0, 0);
304 REPORTER_ASSERT(reporter, result);
305
306 check_solid_pixmap(reporter, expectedColor, actual, skColorType,
307 label, "SkSurface::readPixels");
308 }
309 }
Robert Phillips0ee10342019-09-25 09:55:16 -0400310}
311
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400312// Test initialization of GrBackendObjects to a specific color (non-static since used in Mtl test)
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400313void test_color_init(GrDirectContext* dContext,
314 skiatest::Reporter* reporter,
315 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips459b2952019-05-23 09:38:27 -0400316 const SkColor4f&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400317 GrMipmapped,
Robert Phillips459b2952019-05-23 09:38:27 -0400318 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400319 GrColorType grColorType,
320 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400321 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400322 GrRenderable renderable,
323 bool* finishedBECreate) {
324 GrBackendTexture backendTex = create(dContext, color, mipMapped, renderable);
Robert Phillips459b2952019-05-23 09:38:27 -0400325 if (!backendTex.isValid()) {
326 // errors here should be reported by the test_wrapping test
327 return;
328 }
329
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400330 SkColorType skColorType = GrColorTypeToSkColorType(grColorType);
331
332 // Can't wrap backend textures in images and surfaces w/o an SkColorType
333 if (kUnknown_SkColorType == skColorType) {
Robert Phillips459b2952019-05-23 09:38:27 -0400334 // TODO: burrow in and scrappily check that data was uploaded!
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400335 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips459b2952019-05-23 09:38:27 -0400336 return;
337 }
338
Greg Danielb2365d82020-05-13 15:32:04 -0400339 auto checkBackendTexture = [&](const SkColor4f& testColor) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400340 if (mipMapped == GrMipmapped::kYes) {
Greg Danielb2365d82020-05-13 15:32:04 -0400341 SkColor4f expectedColor = get_expected_color(testColor, skColorType);
342 SkColor4f expectedColors[6] = {expectedColor, expectedColor, expectedColor,
343 expectedColor, expectedColor, expectedColor};
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400344 check_mipmaps(dContext, backendTex, skColorType, expectedColors,
345 reporter, "colorinit");
Greg Danielb2365d82020-05-13 15:32:04 -0400346 }
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400347
Greg Danielb2365d82020-05-13 15:32:04 -0400348 // The last step in this test will dirty the mipmaps so do it last
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400349 check_base_readbacks(dContext, backendTex, skColorType, renderable, testColor,
350 reporter, "colorinit");
Greg Danielb2365d82020-05-13 15:32:04 -0400351 };
352
353 checkBackendTexture(color);
354
355 // Make sure the initial create work has finished so we can test the update independently.
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400356 wait_on_backend_work_to_finish(dContext, finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400357
358 SkColor4f newColor = {color.fB , color.fR, color.fG, color.fA };
359
Greg Daniel373d7dd2020-07-21 10:41:50 -0400360 dContext->updateBackendTexture(backendTex, skColorType, newColor, mark_signaled,
361 finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400362
363 checkBackendTexture(newColor);
364
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400365 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips459b2952019-05-23 09:38:27 -0400366}
367
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400368// Draw the backend texture (wrapped in an SkImage) into an RGBA surface, attempting to access
369// all the mipMap levels.
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400370static void check_mipmaps(GrDirectContext* dContext, const GrBackendTexture& backendTex,
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400371 SkColorType skColorType, const SkColor4f expectedColors[6],
Robert Phillips7f367982019-09-26 14:01:36 -0400372 skiatest::Reporter* reporter, const char* label) {
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400373
374#ifdef SK_GL
375 // skbug.com/9141 (RGBA_F32 mipmaps appear to be broken on some Mali devices)
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400376 if (GrBackendApi::kOpenGL == dContext->backend()) {
377 GrGLGpu* glGPU = static_cast<GrGLGpu*>(dContext->priv().getGpu());
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400378
379 if (kRGBA_F32_SkColorType == skColorType &&
380 kGLES_GrGLStandard == glGPU->ctxInfo().standard()) {
381 return;
382 }
383 }
384#endif
385
Robert Phillips7f367982019-09-26 14:01:36 -0400386 if (isRGB(backendTex.getBackendFormat())) {
387 // readPixels is busted for the RGB backend format (skbug.com/8862)
388 // TODO: add a GrColorType::kRGB_888 to fix the situation
389 return;
390 }
391
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400392 SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType
393 : kPremul_SkAlphaType;
Robert Phillips7f367982019-09-26 14:01:36 -0400394
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400395 sk_sp<SkImage> img = SkImage::MakeFromTexture(dContext,
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400396 backendTex,
397 kTopLeft_GrSurfaceOrigin,
398 skColorType,
399 at,
400 nullptr);
401 if (!img) {
402 return;
403 }
404
Robert Phillips7f367982019-09-26 14:01:36 -0400405 SkImageInfo readbackSurfaceII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
406 kPremul_SkAlphaType);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400407
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400408 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(dContext,
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400409 SkBudgeted::kNo,
Robert Phillips7f367982019-09-26 14:01:36 -0400410 readbackSurfaceII, 1,
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400411 kTopLeft_GrSurfaceOrigin,
412 nullptr);
413 if (!surf) {
414 return;
415 }
416
417 SkCanvas* canvas = surf->getCanvas();
418
419 SkPaint p;
420 p.setFilterQuality(kHigh_SkFilterQuality);
421
422 int numMipLevels = 6;
423
424 for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
425 SkASSERT(rectSize >= 1);
426
427 SkRect r = SkRect::MakeWH(rectSize, rectSize);
428 canvas->clear(SK_ColorTRANSPARENT);
429 canvas->drawImageRect(img, r, &p);
430
431 SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
432 kRGBA_8888_SkColorType,
Robert Phillips7f367982019-09-26 14:01:36 -0400433 kUnpremul_SkAlphaType);
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400434 SkAutoPixmapStorage actual2;
435 SkAssertResult(actual2.tryAlloc(readbackII));
436 actual2.erase(SkColors::kTransparent);
437
438 bool result = surf->readPixels(actual2, 0, 0);
439 REPORTER_ASSERT(reporter, result);
440
Robert Phillipsee946932019-12-18 11:16:17 -0500441 SkString str;
442 str.appendf("mip-level %d", i);
443
Robert Phillips7f367982019-09-26 14:01:36 -0400444 check_solid_pixmap(reporter, expectedColors[i], actual2, skColorType,
Robert Phillipsee946932019-12-18 11:16:17 -0500445 label, str.c_str());
Robert Phillipsc1dbb4b2019-09-24 16:32:35 -0400446 }
447}
448
Brian Salomon7e67dca2020-07-21 09:27:25 -0400449static int make_pixmaps(SkColorType skColorType, GrMipmapped mipMapped,
Robert Phillips7f367982019-09-26 14:01:36 -0400450 const SkColor4f colors[6], SkAutoPixmapStorage pixmaps[6]) {
451 int levelSize = 32;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400452 int numMipLevels = mipMapped == GrMipmapped::kYes ? 6 : 1;
Robert Phillips7f367982019-09-26 14:01:36 -0400453 SkAlphaType at = SkColorTypeIsAlwaysOpaque(skColorType) ? kOpaque_SkAlphaType
454 : kPremul_SkAlphaType;
455 for (int level = 0; level < numMipLevels; ++level) {
456 SkImageInfo ii = SkImageInfo::Make(levelSize, levelSize, skColorType, at);
457 pixmaps[level].alloc(ii);
458 pixmaps[level].erase(colors[level]);
459 levelSize /= 2;
460 }
461 return numMipLevels;
462}
463
464// Test initialization of GrBackendObjects using SkPixmaps
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400465static void test_pixmap_init(GrDirectContext* dContext,
466 skiatest::Reporter* reporter,
467 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips7f367982019-09-26 14:01:36 -0400468 const SkPixmap srcData[],
469 int numLevels,
470 GrRenderable)> create,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400471 SkColorType skColorType,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400472 GrMipmapped mipMapped,
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400473 GrRenderable renderable,
474 bool* finishedBECreate) {
Robert Phillips7f367982019-09-26 14:01:36 -0400475 SkAutoPixmapStorage pixmapMem[6];
476 SkColor4f colors[6] = {
477 { 1.0f, 0.0f, 0.0f, 1.0f }, // R
478 { 0.0f, 1.0f, 0.0f, 0.9f }, // G
479 { 0.0f, 0.0f, 1.0f, 0.7f }, // B
480 { 0.0f, 1.0f, 1.0f, 0.5f }, // C
481 { 1.0f, 0.0f, 1.0f, 0.3f }, // M
482 { 1.0f, 1.0f, 0.0f, 0.2f }, // Y
483 };
484
485 int numMipLevels = make_pixmaps(skColorType, mipMapped, colors, pixmapMem);
486 SkASSERT(numMipLevels);
487
488 // TODO: this is tedious. Should we pass in an array of SkBitmaps instead?
489 SkPixmap pixmaps[6];
490 for (int i = 0; i < numMipLevels; ++i) {
491 pixmaps[i].reset(pixmapMem[i].info(), pixmapMem[i].addr(), pixmapMem[i].rowBytes());
492 }
493
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400494 GrBackendTexture backendTex = create(dContext, pixmaps, numMipLevels, renderable);
Robert Phillips7f367982019-09-26 14:01:36 -0400495 if (!backendTex.isValid()) {
496 // errors here should be reported by the test_wrapping test
497 return;
498 }
499
Robert Phillips9a30ee02020-04-29 08:58:39 -0400500 if (skColorType == kBGRA_8888_SkColorType && !isBGRA8(backendTex.getBackendFormat())) {
Robert Phillips7f367982019-09-26 14:01:36 -0400501 // When kBGRA is backed by an RGBA something goes wrong in the swizzling
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400502 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips7f367982019-09-26 14:01:36 -0400503 return;
504 }
505
Greg Danielb2365d82020-05-13 15:32:04 -0400506 auto checkBackendTexture = [&](SkColor4f colors[6]) {
Brian Salomon7e67dca2020-07-21 09:27:25 -0400507 if (mipMapped == GrMipmapped::kYes) {
Greg Danielb2365d82020-05-13 15:32:04 -0400508 SkColor4f expectedColors[6] = {
509 get_expected_color(colors[0], skColorType),
510 get_expected_color(colors[1], skColorType),
511 get_expected_color(colors[2], skColorType),
512 get_expected_color(colors[3], skColorType),
513 get_expected_color(colors[4], skColorType),
514 get_expected_color(colors[5], skColorType),
515 };
Robert Phillips7f367982019-09-26 14:01:36 -0400516
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400517 check_mipmaps(dContext, backendTex, skColorType, expectedColors, reporter, "pixmap");
Greg Danielb2365d82020-05-13 15:32:04 -0400518 }
519
520 // The last step in this test will dirty the mipmaps so do it last
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400521 check_base_readbacks(dContext, backendTex, skColorType, renderable, colors[0], reporter,
Greg Danielb2365d82020-05-13 15:32:04 -0400522 "pixmap");
523 };
524
525 checkBackendTexture(colors);
526
527 // Make sure the initial create work has finished so we can test the update independently.
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400528 wait_on_backend_work_to_finish(dContext, finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400529
530 SkColor4f colorsNew[6] = {
531 {1.0f, 1.0f, 0.0f, 0.2f}, // Y
532 {1.0f, 0.0f, 0.0f, 1.0f}, // R
533 {0.0f, 1.0f, 0.0f, 0.9f}, // G
534 {0.0f, 0.0f, 1.0f, 0.7f}, // B
535 {0.0f, 1.0f, 1.0f, 0.5f}, // C
536 {1.0f, 0.0f, 1.0f, 0.3f}, // M
537 };
538 make_pixmaps(skColorType, mipMapped, colorsNew, pixmapMem);
539 for (int i = 0; i < numMipLevels; ++i) {
540 pixmaps[i].reset(pixmapMem[i].info(), pixmapMem[i].addr(), pixmapMem[i].rowBytes());
Robert Phillips7f367982019-09-26 14:01:36 -0400541 }
542
Greg Danielb2365d82020-05-13 15:32:04 -0400543 // Upload new data and make sure everything still works
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400544 dContext->updateBackendTexture(backendTex, pixmaps, numMipLevels, mark_signaled,
545 finishedBECreate);
Greg Danielb2365d82020-05-13 15:32:04 -0400546
547 checkBackendTexture(colorsNew);
548
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400549 delete_backend_texture(dContext, backendTex, finishedBECreate);
Robert Phillips7f367982019-09-26 14:01:36 -0400550}
551
Robert Phillips02dc0302019-07-02 17:58:27 -0400552enum class VkLayout {
553 kUndefined,
554 kReadOnlyOptimal,
Robert Phillips02dc0302019-07-02 17:58:27 -0400555};
556
557void check_vk_layout(const GrBackendTexture& backendTex, VkLayout layout) {
558#if defined(SK_VULKAN) && defined(SK_DEBUG)
559 VkImageLayout expected;
560
561 switch (layout) {
562 case VkLayout::kUndefined:
563 expected = VK_IMAGE_LAYOUT_UNDEFINED;
564 break;
565 case VkLayout::kReadOnlyOptimal:
566 expected = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
567 break;
Robert Phillips02dc0302019-07-02 17:58:27 -0400568 default:
569 SkUNREACHABLE;
570 }
571
572 GrVkImageInfo vkII;
573
574 if (backendTex.getVkImageInfo(&vkII)) {
575 SkASSERT(expected == vkII.fImageLayout);
576 SkASSERT(VK_IMAGE_TILING_OPTIMAL == vkII.fImageTiling);
577 }
578#endif
579}
580
581///////////////////////////////////////////////////////////////////////////////
582// This test is a bit different from the others in this file. It is mainly checking that, for any
583// SkSurface we can create in Ganesh, we can also create a backend texture that is compatible with
584// its characterization and then create a new surface that wraps that backend texture.
585DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CharacterizationBackendAllocationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400586 auto context = ctxInfo.directContext();
Robert Phillips02dc0302019-07-02 17:58:27 -0400587
588 for (int ct = 0; ct <= kLastEnum_SkColorType; ++ct) {
589 SkColorType colorType = static_cast<SkColorType>(ct);
590
591 SkImageInfo ii = SkImageInfo::Make(32, 32, colorType, kPremul_SkAlphaType);
592
593 for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
594 for (bool mipMaps : { true, false } ) {
595 for (int sampleCount : {1, 2}) {
596 SkSurfaceCharacterization c;
597
598 // Get a characterization, if possible
599 {
600 sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
601 ii, sampleCount,
602 origin, nullptr, mipMaps);
603 if (!s) {
604 continue;
605 }
606
607 if (!s->characterize(&c)) {
608 continue;
609 }
610
611 REPORTER_ASSERT(reporter, s->isCompatible(c));
612 }
613
614 // Test out uninitialized path
615 {
616 GrBackendTexture backendTex = context->createBackendTexture(c);
617 check_vk_layout(backendTex, VkLayout::kUndefined);
618 REPORTER_ASSERT(reporter, backendTex.isValid());
619 REPORTER_ASSERT(reporter, c.isCompatible(backendTex));
620
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400621 {
622 GrBackendFormat format = context->defaultBackendFormat(
623 c.imageInfo().colorType(),
624 GrRenderable::kYes);
625 REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat());
626 }
627
Robert Phillips02dc0302019-07-02 17:58:27 -0400628 sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c,
629 backendTex);
630 REPORTER_ASSERT(reporter, s2);
631 REPORTER_ASSERT(reporter, s2->isCompatible(c));
632
633 s2 = nullptr;
634 context->deleteBackendTexture(backendTex);
635 }
636
637 // Test out color-initialized path
Greg Danielc1ad77c2020-05-06 11:40:03 -0400638
Robert Phillips02dc0302019-07-02 17:58:27 -0400639 {
Greg Danielc1ad77c2020-05-06 11:40:03 -0400640
641 bool finished = false;
Robert Phillips02dc0302019-07-02 17:58:27 -0400642 GrBackendTexture backendTex = context->createBackendTexture(c,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400643 SkColors::kRed,
644 mark_signaled,
645 &finished);
Greg Danieldddf7092020-05-06 11:52:37 -0400646 check_vk_layout(backendTex, VkLayout::kReadOnlyOptimal);
Robert Phillips02dc0302019-07-02 17:58:27 -0400647 REPORTER_ASSERT(reporter, backendTex.isValid());
648 REPORTER_ASSERT(reporter, c.isCompatible(backendTex));
649
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400650 {
651 GrBackendFormat format = context->defaultBackendFormat(
652 c.imageInfo().colorType(),
653 GrRenderable::kYes);
654 REPORTER_ASSERT(reporter, format == backendTex.getBackendFormat());
655 }
656
Robert Phillips02dc0302019-07-02 17:58:27 -0400657 sk_sp<SkSurface> s2 = SkSurface::MakeFromBackendTexture(context, c,
658 backendTex);
659 REPORTER_ASSERT(reporter, s2);
660 REPORTER_ASSERT(reporter, s2->isCompatible(c));
661
662 s2 = nullptr;
Greg Danielc1ad77c2020-05-06 11:40:03 -0400663 delete_backend_texture(context, backendTex, &finished);
Robert Phillips02dc0302019-07-02 17:58:27 -0400664 }
665 }
666 }
667 }
668 }
669}
670
Robert Phillipsefb9f142019-05-17 14:19:44 -0400671///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400672DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ColorTypeBackendAllocationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400673 auto context = ctxInfo.directContext();
Robert Phillips0c6daf02019-05-16 12:43:11 -0400674 const GrCaps* caps = context->priv().caps();
675
Robert Phillips459b2952019-05-23 09:38:27 -0400676 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400677 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400678
Robert Phillips0c6daf02019-05-16 12:43:11 -0400679 struct {
680 SkColorType fColorType;
Robert Phillips459b2952019-05-23 09:38:27 -0400681 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400682 } combinations[] = {
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400683 { kAlpha_8_SkColorType, kTransCol },
684 { kRGB_565_SkColorType, SkColors::kRed },
685 { kARGB_4444_SkColorType, SkColors::kGreen },
686 { kRGBA_8888_SkColorType, SkColors::kBlue },
687 { kRGB_888x_SkColorType, SkColors::kCyan },
Robert Phillips459b2952019-05-23 09:38:27 -0400688 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400689 { kBGRA_8888_SkColorType, { 1, 0, 0, 1.0f } },
Robert Phillips9a30ee02020-04-29 08:58:39 -0400690 // TODO: readback is busted for *10A2 when alpha = 0.5f (perhaps premul vs. unpremul)
691 { kRGBA_1010102_SkColorType, { 0.25f, 0.5f, 0.75f, 1.0f }},
692 { kBGRA_1010102_SkColorType, { 0.25f, 0.5f, 0.75f, 1.0f }},
693 // RGB/BGR 101010x have no Ganesh correlate
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400694 { kRGB_101010x_SkColorType, { 0, 0.5f, 0, 0.5f } },
Mike Kleinf7eb0542020-02-11 12:19:08 -0600695 { kBGR_101010x_SkColorType, { 0, 0.5f, 0, 0.5f } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400696 { kGray_8_SkColorType, kGrayCol },
697 { kRGBA_F16Norm_SkColorType, SkColors::kLtGray },
698 { kRGBA_F16_SkColorType, SkColors::kYellow },
699 { kRGBA_F32_SkColorType, SkColors::kGray },
Robert Phillips7f367982019-09-26 14:01:36 -0400700 { kR8G8_unorm_SkColorType, { .25f, .75f, 0, 1 } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400701 { kR16G16_unorm_SkColorType, SkColors::kGreen },
702 { kA16_unorm_SkColorType, kTransCol },
703 { kA16_float_SkColorType, kTransCol },
Robert Phillips7f367982019-09-26 14:01:36 -0400704 { kR16G16_float_SkColorType, { .25f, .75f, 0, 1 } },
Robert Phillipsea1b30b2019-09-19 16:05:48 -0400705 { kR16G16B16A16_unorm_SkColorType,{ .25f, .5f, .75f, 1 } },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400706 };
707
Brian Salomon4dea72a2019-12-18 10:43:10 -0500708 static_assert(kLastEnum_SkColorType == SK_ARRAY_COUNT(combinations));
Robert Phillips0c6daf02019-05-16 12:43:11 -0400709
710 for (auto combo : combinations) {
711 SkColorType colorType = combo.fColorType;
712
Robert Phillips0c6daf02019-05-16 12:43:11 -0400713 if (GrBackendApi::kMetal == context->backend()) {
714 // skbug.com/9086 (Metal caps may not be handling RGBA32 correctly)
715 if (kRGBA_F32_SkColorType == combo.fColorType) {
716 continue;
717 }
718 }
719
Brian Salomon7e67dca2020-07-21 09:27:25 -0400720 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -0400721 if (GrMipmapped::kYes == mipMapped && !caps->mipmapSupport()) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400722 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400723 }
724
Robert Phillipsefb9f142019-05-17 14:19:44 -0400725 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Greg Daniel7bfc9132019-08-14 14:23:53 -0400726 if (!caps->getDefaultBackendFormat(SkColorTypeToGrColorType(colorType),
727 renderable).isValid()) {
728 continue;
729 }
Robert Phillips7f367982019-09-26 14:01:36 -0400730
Robert Phillipsefb9f142019-05-17 14:19:44 -0400731 if (GrRenderable::kYes == renderable) {
732 if (kRGB_888x_SkColorType == combo.fColorType) {
733 // Ganesh can't perform the blends correctly when rendering this format
734 continue;
735 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400736 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400737
Robert Phillipsb04b6942019-05-21 17:24:31 -0400738 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400739 auto uninitCreateMtd = [colorType](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400740 GrMipmapped mipMapped,
Robert Phillipsb04b6942019-05-21 17:24:31 -0400741 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400742 auto result = dContext->createBackendTexture(32, 32, colorType,
743 mipMapped, renderable,
744 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -0400745 check_vk_layout(result, VkLayout::kUndefined);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400746
747#ifdef SK_DEBUG
748 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400749 GrBackendFormat format = dContext->defaultBackendFormat(colorType,
750 renderable);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400751 SkASSERT(format == result.getBackendFormat());
752 }
753#endif
754
Robert Phillips02dc0302019-07-02 17:58:27 -0400755 return result;
Robert Phillipsb04b6942019-05-21 17:24:31 -0400756 };
Robert Phillipsefb9f142019-05-17 14:19:44 -0400757
Robert Phillipsb04b6942019-05-21 17:24:31 -0400758 test_wrapping(context, reporter, uninitCreateMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400759 SkColorTypeToGrColorType(colorType), mipMapped, renderable,
760 nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -0400761 }
Robert Phillips459b2952019-05-23 09:38:27 -0400762
Greg Danielc1ad77c2020-05-06 11:40:03 -0400763 bool finishedBackendCreation = false;
764 bool* finishedPtr = &finishedBackendCreation;
765
Robert Phillips459b2952019-05-23 09:38:27 -0400766 {
Robert Phillips459b2952019-05-23 09:38:27 -0400767
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400768 auto createWithColorMtd = [colorType, finishedPtr](GrDirectContext* dContext,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400769 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400770 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400771 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400772 auto result = dContext->createBackendTexture(32, 32, colorType, color,
773 mipMapped, renderable,
774 GrProtected::kNo,
775 mark_signaled, finishedPtr);
Greg Danieldddf7092020-05-06 11:52:37 -0400776 check_vk_layout(result, VkLayout::kReadOnlyOptimal);
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400777
778#ifdef SK_DEBUG
779 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400780 GrBackendFormat format = dContext->defaultBackendFormat(colorType,
Robert Phillipsd5e80ca2019-07-29 14:11:35 -0400781 renderable);
782 SkASSERT(format == result.getBackendFormat());
783 }
784#endif
785
Robert Phillips02dc0302019-07-02 17:58:27 -0400786 return result;
Robert Phillips459b2952019-05-23 09:38:27 -0400787 };
Brian Salomon85c3d682019-11-04 15:04:54 -0500788 // We make our comparison color using SkPixmap::erase(color) on a pixmap of
789 // combo.fColorType and then calling SkPixmap::readPixels(). erase() will premul
790 // the color passed to it. However, createBackendTexture() that takes a
791 // SkColor4f is color type / alpha type unaware and will simply compute
792 // luminance from the r, g, b, channels.
793 SkColor4f color = combo.fColor;
794 if (colorType == kGray_8_SkColorType) {
795 color = {color.fR * color.fA,
796 color.fG * color.fA,
797 color.fB * color.fA,
798 1.f};
799 }
Robert Phillips459b2952019-05-23 09:38:27 -0400800 test_color_init(context, reporter, createWithColorMtd,
Brian Salomon85c3d682019-11-04 15:04:54 -0500801 SkColorTypeToGrColorType(colorType), color, mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400802 renderable, finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -0400803 }
Robert Phillips7f367982019-09-26 14:01:36 -0400804
Robert Phillips9a30ee02020-04-29 08:58:39 -0400805 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400806 auto createWithSrcDataMtd = [finishedPtr](GrDirectContext* dContext,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400807 const SkPixmap srcData[],
808 int numLevels,
809 GrRenderable renderable) {
Robert Phillips9a30ee02020-04-29 08:58:39 -0400810 SkASSERT(srcData && numLevels);
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400811 auto result = dContext->createBackendTexture(srcData, numLevels, renderable,
812 GrProtected::kNo, mark_signaled,
813 finishedPtr);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400814 check_vk_layout(result, VkLayout::kReadOnlyOptimal);
Robert Phillips7f367982019-09-26 14:01:36 -0400815#ifdef SK_DEBUG
Robert Phillips9a30ee02020-04-29 08:58:39 -0400816 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400817 auto format = dContext->defaultBackendFormat(srcData[0].colorType(),
818 renderable);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400819 SkASSERT(format == result.getBackendFormat());
820 }
Robert Phillips7f367982019-09-26 14:01:36 -0400821#endif
Robert Phillips9a30ee02020-04-29 08:58:39 -0400822 return result;
823 };
Robert Phillips7f367982019-09-26 14:01:36 -0400824
Robert Phillips9a30ee02020-04-29 08:58:39 -0400825 test_pixmap_init(context, reporter, createWithSrcDataMtd, colorType, mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -0400826 renderable, finishedPtr);
Robert Phillips9a30ee02020-04-29 08:58:39 -0400827 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400828 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400829 }
830 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400831}
832
Robert Phillipsefb9f142019-05-17 14:19:44 -0400833///////////////////////////////////////////////////////////////////////////////
834#ifdef SK_GL
835
Robert Phillips0c6daf02019-05-16 12:43:11 -0400836DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLBackendAllocationTest, reporter, ctxInfo) {
837 sk_gpu_test::GLTestContext* glCtx = ctxInfo.glContext();
838 GrGLStandard standard = glCtx->gl()->fStandard;
Robert Phillips6d344c32020-07-06 10:56:46 -0400839 auto context = ctxInfo.directContext();
Robert Phillipsefb9f142019-05-17 14:19:44 -0400840 const GrGLCaps* glCaps = static_cast<const GrGLCaps*>(context->priv().caps());
Robert Phillips0c6daf02019-05-16 12:43:11 -0400841
Robert Phillips459b2952019-05-23 09:38:27 -0400842 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillipsbd1ef682019-05-31 12:48:49 -0400843 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 0.75f };
Robert Phillips459b2952019-05-23 09:38:27 -0400844
Robert Phillips0c6daf02019-05-16 12:43:11 -0400845 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400846 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400847 GrGLenum fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -0400848 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400849 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400850 { GrColorType::kRGBA_8888, GR_GL_RGBA8, SkColors::kRed },
851 { GrColorType::kRGBA_8888_SRGB, GR_GL_SRGB8_ALPHA8, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400852
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400853 { GrColorType::kRGB_888x, GR_GL_RGBA8, SkColors::kYellow },
854 { GrColorType::kRGB_888x, GR_GL_RGB8, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400855
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400856 { GrColorType::kBGRA_8888, GR_GL_RGBA8, SkColors::kBlue },
857 { GrColorType::kBGRA_8888, GR_GL_BGRA8, SkColors::kBlue },
858 // TODO: readback is busted when alpha = 0.5f (perhaps premul vs. unpremul)
Robert Phillips9a30ee02020-04-29 08:58:39 -0400859 { GrColorType::kRGBA_1010102, GR_GL_RGB10_A2, { 0.25f, 0.5f, 0.75f, 1.f }},
860 { GrColorType::kBGRA_1010102, GR_GL_RGB10_A2, { 0.25f, 0.5f, 0.75f, 1.f }},
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400861 { GrColorType::kBGR_565, GR_GL_RGB565, SkColors::kRed },
862 { GrColorType::kABGR_4444, GR_GL_RGBA4, SkColors::kGreen },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400863
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400864 { GrColorType::kAlpha_8, GR_GL_ALPHA8, kTransCol },
865 { GrColorType::kAlpha_8, GR_GL_R8, kTransCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400866
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400867 { GrColorType::kGray_8, GR_GL_LUMINANCE8, kGrayCol },
868 { GrColorType::kGray_8, GR_GL_R8, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400869
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400870 { GrColorType::kRGBA_F32, GR_GL_RGBA32F, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400871
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400872 { GrColorType::kRGBA_F16_Clamped, GR_GL_RGBA16F, SkColors::kLtGray },
873 { GrColorType::kRGBA_F16, GR_GL_RGBA16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400874
Robert Phillipsd470e1b2019-09-04 15:05:35 -0400875 { GrColorType::kRG_88, GR_GL_RG8, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400876 { GrColorType::kAlpha_F16, GR_GL_R16F, { 1.0f, 0, 0, 0.5f } },
877 { GrColorType::kAlpha_F16, GR_GL_LUMINANCE16F, kGrayCol },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400878
Robert Phillips429f0d32019-09-11 17:03:28 -0400879 { GrColorType::kAlpha_16, GR_GL_R16, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400880 { GrColorType::kRG_1616, GR_GL_RG16, SkColors::kYellow },
Robert Phillips66a46032019-06-18 08:00:42 -0400881
Robert Phillipsb7f95d12019-07-26 11:13:19 -0400882 { GrColorType::kRGBA_16161616, GR_GL_RGBA16, SkColors::kLtGray },
883 { GrColorType::kRG_F16, GR_GL_RG16F, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -0400884 };
885
886 for (auto combo : combinations) {
Brian Salomon0f396992020-06-19 19:51:21 -0400887 for (GrGLenum target : {GR_GL_TEXTURE_2D, GR_GL_TEXTURE_RECTANGLE}) {
888 GrBackendFormat format = GrBackendFormat::MakeGL(combo.fFormat, target);
Robert Phillips0c6daf02019-05-16 12:43:11 -0400889
Brian Salomon0f396992020-06-19 19:51:21 -0400890 if (!glCaps->isFormatTexturable(format)) {
Robert Phillipsefb9f142019-05-17 14:19:44 -0400891 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -0400892 }
893
Brian Salomon0f396992020-06-19 19:51:21 -0400894 if (GrColorType::kBGRA_8888 == combo.fColorType ||
895 GrColorType::kBGRA_1010102 == combo.fColorType) {
896 // We allow using a GL_RGBA8 or GR_GL_RGB10_A2 texture as BGRA on desktop GL but not
897 // ES
898 if (kGL_GrGLStandard != standard &&
899 (GR_GL_RGBA8 == combo.fFormat || GR_GL_RGB10_A2 == combo.fFormat)) {
900 continue;
901 }
902 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400903
Brian Salomon7e67dca2020-07-21 09:27:25 -0400904 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
905 if (GrMipmapped::kYes == mipMapped &&
Brian Salomon69100f02020-07-21 10:49:25 -0400906 (!glCaps->mipmapSupport() || target == GR_GL_TEXTURE_RECTANGLE)) {
Brian Salomon0f396992020-06-19 19:51:21 -0400907 continue;
Robert Phillipsefb9f142019-05-17 14:19:44 -0400908 }
909
Brian Salomon0f396992020-06-19 19:51:21 -0400910 for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
911 if (GrRenderable::kYes == renderable) {
912 if (!glCaps->isFormatAsColorTypeRenderable(combo.fColorType, format)) {
913 continue;
914 }
Robert Phillips459b2952019-05-23 09:38:27 -0400915 }
916
Brian Salomon0f396992020-06-19 19:51:21 -0400917 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400918 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400919 GrMipmapped mipMapped,
Brian Salomon0f396992020-06-19 19:51:21 -0400920 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400921 return dContext->createBackendTexture(32, 32, format, mipMapped,
922 renderable, GrProtected::kNo);
Brian Salomon0f396992020-06-19 19:51:21 -0400923 };
Greg Danielc1ad77c2020-05-06 11:40:03 -0400924
Brian Salomon0f396992020-06-19 19:51:21 -0400925 test_wrapping(context, reporter, uninitCreateMtd, combo.fColorType,
926 mipMapped, renderable, nullptr);
Brian Salomon85c3d682019-11-04 15:04:54 -0500927 }
Robert Phillips459b2952019-05-23 09:38:27 -0400928
Brian Salomon0f396992020-06-19 19:51:21 -0400929 {
930 // We're creating backend textures without specifying a color type "view" of
931 // them at the public API level. Therefore, Ganesh will not apply any
932 // swizzles before writing the color to the texture. However, our validation
933 // code does rely on interpreting the texture contents via a SkColorType and
934 // therefore swizzles may be applied during the read step. Ideally we'd
935 // update our validation code to use a "raw" read that doesn't impose a
936 // color type but for now we just munge the data we upload to match the
937 // expectation.
938 GrSwizzle swizzle;
939 switch (combo.fColorType) {
940 case GrColorType::kAlpha_8:
941 swizzle = GrSwizzle("aaaa");
942 break;
943 case GrColorType::kAlpha_16:
944 swizzle = GrSwizzle("aaaa");
945 break;
946 case GrColorType::kAlpha_F16:
947 swizzle = GrSwizzle("aaaa");
948 break;
949 default:
950 break;
951 }
952
953 bool finishedBackendCreation = false;
954 bool* finishedPtr = &finishedBackendCreation;
955
956 auto createWithColorMtd = [format, swizzle, finishedPtr](
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400957 GrDirectContext* dContext,
Brian Salomon0f396992020-06-19 19:51:21 -0400958 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400959 GrMipmapped mipMapped,
Brian Salomon0f396992020-06-19 19:51:21 -0400960 GrRenderable renderable) {
961 auto swizzledColor = swizzle.applyTo(color);
Robert Phillipsfe4b4812020-07-17 14:15:51 -0400962 return dContext->createBackendTexture(
Brian Salomon0f396992020-06-19 19:51:21 -0400963 32, 32, format, swizzledColor, mipMapped, renderable,
964 GrProtected::kNo, mark_signaled, finishedPtr);
965 };
966 // We make our comparison color using SkPixmap::erase(color) on a pixmap of
967 // combo.fColorType and then calling SkPixmap::readPixels(). erase() will
968 // premul the color passed to it. However, createBackendTexture() that takes
969 // a SkColor4f is color type/alpha type unaware and will simply compute
970 // luminance from the r, g, b, channels.
971 SkColor4f color = combo.fColor;
972 if (combo.fColorType == GrColorType::kGray_8) {
973 color = {color.fR * color.fA,
974 color.fG * color.fA,
975 color.fB * color.fA,
976 1.f};
977 }
978
979 test_color_init(context, reporter, createWithColorMtd, combo.fColorType,
980 color, mipMapped, renderable, finishedPtr);
981 }
Robert Phillips459b2952019-05-23 09:38:27 -0400982 }
Robert Phillipsefb9f142019-05-17 14:19:44 -0400983 }
Robert Phillips0c6daf02019-05-16 12:43:11 -0400984 }
985 }
986}
987
Robert Phillipsefb9f142019-05-17 14:19:44 -0400988#endif
989
990///////////////////////////////////////////////////////////////////////////////
Robert Phillips0c6daf02019-05-16 12:43:11 -0400991
992#ifdef SK_VULKAN
993
994#include "src/gpu/vk/GrVkCaps.h"
995
996DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkBackendAllocationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400997 auto context = ctxInfo.directContext();
Robert Phillips0c6daf02019-05-16 12:43:11 -0400998 const GrVkCaps* vkCaps = static_cast<const GrVkCaps*>(context->priv().caps());
999
Robert Phillips459b2952019-05-23 09:38:27 -04001000 constexpr SkColor4f kTransCol { 0, 0.25f, 0.75f, 0.5f };
Robert Phillips7f367982019-09-26 14:01:36 -04001001 constexpr SkColor4f kGrayCol { 0.75f, 0.75f, 0.75f, 1 };
Robert Phillips459b2952019-05-23 09:38:27 -04001002
Robert Phillips0c6daf02019-05-16 12:43:11 -04001003 struct {
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001004 GrColorType fColorType;
Robert Phillips0c6daf02019-05-16 12:43:11 -04001005 VkFormat fFormat;
Robert Phillips459b2952019-05-23 09:38:27 -04001006 SkColor4f fColor;
Robert Phillips0c6daf02019-05-16 12:43:11 -04001007 } combinations[] = {
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001008 { GrColorType::kRGBA_8888, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kRed },
1009 { GrColorType::kRGBA_8888_SRGB, VK_FORMAT_R8G8B8A8_SRGB, SkColors::kRed },
Robert Phillips0c6daf02019-05-16 12:43:11 -04001010
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001011 // In this configuration (i.e., an RGB_888x colortype with an RGBA8 backing format),
1012 // there is nothing to tell Skia to make the provided color opaque. Clients will need
1013 // to provide an opaque initialization color in this case.
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001014 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8A8_UNORM, SkColors::kYellow },
1015 { GrColorType::kRGB_888x, VK_FORMAT_R8G8B8_UNORM, SkColors::kCyan },
Robert Phillips0c6daf02019-05-16 12:43:11 -04001016
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001017 { GrColorType::kBGRA_8888, VK_FORMAT_B8G8R8A8_UNORM, SkColors::kBlue },
Robert Phillips0c6daf02019-05-16 12:43:11 -04001018
Robert Phillips9a30ee02020-04-29 08:58:39 -04001019 { GrColorType::kRGBA_1010102, VK_FORMAT_A2B10G10R10_UNORM_PACK32,
1020 { 0.25f, 0.5f, 0.75f, 1.0f }},
1021 { GrColorType::kBGRA_1010102, VK_FORMAT_A2R10G10B10_UNORM_PACK32,
1022 { 0.25f, 0.5f, 0.75f, 1.0f }},
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001023 { GrColorType::kBGR_565, VK_FORMAT_R5G6B5_UNORM_PACK16, SkColors::kRed },
Robert Phillipsefb9f142019-05-17 14:19:44 -04001024
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001025 { GrColorType::kABGR_4444, VK_FORMAT_R4G4B4A4_UNORM_PACK16, SkColors::kCyan },
1026 { GrColorType::kABGR_4444, VK_FORMAT_B4G4R4A4_UNORM_PACK16, SkColors::kYellow },
Robert Phillipsefb9f142019-05-17 14:19:44 -04001027
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001028 { GrColorType::kAlpha_8, VK_FORMAT_R8_UNORM, kTransCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001029 // In this config (i.e., a Gray8 color type with an R8 backing format), there is nothing
1030 // to tell Skia this isn't an Alpha8 color type (so it will initialize the texture with
1031 // the alpha channel of the color). Clients should, in general, fill all the channels
1032 // of the provided color with the same value in such cases.
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001033 { GrColorType::kGray_8, VK_FORMAT_R8_UNORM, kGrayCol },
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001034
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001035 { GrColorType::kRGBA_F16_Clamped, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kLtGray },
1036 { GrColorType::kRGBA_F16, VK_FORMAT_R16G16B16A16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -04001037
Robert Phillipsd470e1b2019-09-04 15:05:35 -04001038 { GrColorType::kRG_88, VK_FORMAT_R8G8_UNORM, { 1, 0.5f, 0, 1 } },
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001039 { GrColorType::kAlpha_F16, VK_FORMAT_R16_SFLOAT, { 1.0f, 0, 0, 0.5f }},
1040
Robert Phillips429f0d32019-09-11 17:03:28 -04001041 { GrColorType::kAlpha_16, VK_FORMAT_R16_UNORM, kTransCol },
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001042 { GrColorType::kRG_1616, VK_FORMAT_R16G16_UNORM, SkColors::kYellow },
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001043 { GrColorType::kRGBA_16161616, VK_FORMAT_R16G16B16A16_UNORM, SkColors::kLtGray },
1044 { GrColorType::kRG_F16, VK_FORMAT_R16G16_SFLOAT, SkColors::kYellow },
Robert Phillips0c6daf02019-05-16 12:43:11 -04001045 };
1046
1047 for (auto combo : combinations) {
Greg Daniel2f2caea2019-07-08 14:24:47 -04001048 if (!vkCaps->isVkFormatTexturable(combo.fFormat)) {
Robert Phillips0c6daf02019-05-16 12:43:11 -04001049 continue;
1050 }
1051
1052 GrBackendFormat format = GrBackendFormat::MakeVk(combo.fFormat);
1053
Brian Salomon7e67dca2020-07-21 09:27:25 -04001054 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -04001055 if (GrMipmapped::kYes == mipMapped && !vkCaps->mipmapSupport()) {
Robert Phillipsefb9f142019-05-17 14:19:44 -04001056 continue;
Robert Phillips0c6daf02019-05-16 12:43:11 -04001057 }
1058
Robert Phillipsefb9f142019-05-17 14:19:44 -04001059 for (auto renderable : { GrRenderable::kNo, GrRenderable::kYes }) {
Robert Phillips0c6daf02019-05-16 12:43:11 -04001060
Robert Phillipsefb9f142019-05-17 14:19:44 -04001061 if (GrRenderable::kYes == renderable) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001062 // We must also check whether we allow rendering to the format using the
1063 // color type.
Greg Daniel900583a2019-08-06 12:05:31 -04001064 if (!vkCaps->isFormatAsColorTypeRenderable(
1065 combo.fColorType, GrBackendFormat::MakeVk(combo.fFormat), 1)) {
Brian Salomon4eb38b72019-08-05 12:58:39 -04001066 continue;
1067 }
Robert Phillipsefb9f142019-05-17 14:19:44 -04001068 }
1069
Robert Phillipsd34691b2019-09-24 13:38:43 -04001070 {
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001071 auto uninitCreateMtd = [format](GrDirectContext* dContext,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001072 GrMipmapped mipMapped,
Robert Phillips459b2952019-05-23 09:38:27 -04001073 GrRenderable renderable) {
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001074 GrBackendTexture beTex = dContext->createBackendTexture(32, 32, format,
1075 mipMapped,
1076 renderable,
1077 GrProtected::kNo);
Robert Phillips02dc0302019-07-02 17:58:27 -04001078 check_vk_layout(beTex, VkLayout::kUndefined);
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001079 return beTex;
Robert Phillipsb04b6942019-05-21 17:24:31 -04001080 };
Robert Phillipsefb9f142019-05-17 14:19:44 -04001081
Robert Phillipsb04b6942019-05-21 17:24:31 -04001082 test_wrapping(context, reporter, uninitCreateMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001083 combo.fColorType, mipMapped, renderable, nullptr);
Robert Phillipsb04b6942019-05-21 17:24:31 -04001084 }
Robert Phillips459b2952019-05-23 09:38:27 -04001085
Robert Phillips459b2952019-05-23 09:38:27 -04001086 {
Brian Salomonb450f3b2019-07-09 09:36:51 -04001087 // We're creating backend textures without specifying a color type "view" of
1088 // them at the public API level. Therefore, Ganesh will not apply any swizzles
1089 // before writing the color to the texture. However, our validation code does
1090 // rely on interpreting the texture contents via a SkColorType and therefore
1091 // swizzles may be applied during the read step.
1092 // Ideally we'd update our validation code to use a "raw" read that doesn't
1093 // impose a color type but for now we just munge the data we upload to match the
1094 // expectation.
1095 GrSwizzle swizzle;
1096 switch (combo.fColorType) {
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001097 case GrColorType::kAlpha_8:
Brian Salomonb450f3b2019-07-09 09:36:51 -04001098 SkASSERT(combo.fFormat == VK_FORMAT_R8_UNORM);
1099 swizzle = GrSwizzle("aaaa");
1100 break;
Robert Phillips429f0d32019-09-11 17:03:28 -04001101 case GrColorType::kAlpha_16:
1102 SkASSERT(combo.fFormat == VK_FORMAT_R16_UNORM);
1103 swizzle = GrSwizzle("aaaa");
1104 break;
Robert Phillips17a3a0b2019-09-18 13:56:54 -04001105 case GrColorType::kAlpha_F16:
1106 SkASSERT(combo.fFormat == VK_FORMAT_R16_SFLOAT);
1107 swizzle = GrSwizzle("aaaa");
1108 break;
Robert Phillipsb7f95d12019-07-26 11:13:19 -04001109 case GrColorType::kABGR_4444:
Brian Salomonb450f3b2019-07-09 09:36:51 -04001110 if (combo.fFormat == VK_FORMAT_B4G4R4A4_UNORM_PACK16) {
1111 swizzle = GrSwizzle("bgra");
1112 }
1113 break;
1114 default:
1115 swizzle = GrSwizzle("rgba");
1116 break;
1117 }
Greg Danielc1ad77c2020-05-06 11:40:03 -04001118
1119 bool finishedBackendCreation = false;
1120 bool* finishedPtr = &finishedBackendCreation;
1121
1122 auto createWithColorMtd = [format, swizzle, finishedPtr](
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001123 GrDirectContext* dContext,
1124 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -04001125 GrMipmapped mipMapped,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001126 GrRenderable renderable) {
Brian Salomonb450f3b2019-07-09 09:36:51 -04001127 auto swizzledColor = swizzle.applyTo(color);
Robert Phillipsfe4b4812020-07-17 14:15:51 -04001128 GrBackendTexture beTex = dContext->createBackendTexture(32, 32, format,
1129 swizzledColor,
1130 mipMapped,
1131 renderable,
1132 GrProtected::kNo,
1133 mark_signaled,
1134 finishedPtr);
Greg Danieldddf7092020-05-06 11:52:37 -04001135 check_vk_layout(beTex, VkLayout::kReadOnlyOptimal);
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001136 return beTex;
Robert Phillips459b2952019-05-23 09:38:27 -04001137 };
Robert Phillips459b2952019-05-23 09:38:27 -04001138 test_color_init(context, reporter, createWithColorMtd,
Greg Danielc1ad77c2020-05-06 11:40:03 -04001139 combo.fColorType, combo.fColor, mipMapped, renderable,
1140 finishedPtr);
Robert Phillips459b2952019-05-23 09:38:27 -04001141 }
Robert Phillipsefb9f142019-05-17 14:19:44 -04001142 }
Robert Phillips0c6daf02019-05-16 12:43:11 -04001143 }
1144 }
1145}
1146
1147#endif