blob: 88b81e026a86a9e4392d37b32e0d89014c4e2a32 [file] [log] [blame]
Robert Phillips3e5e2f22019-12-19 11:19:16 -05001/*
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"
Robert Phillips6d344c32020-07-06 10:56:46 -04009#include "include/gpu/GrDirectContext.h"
Robert Phillipsc8ae4942020-07-20 10:56:01 -040010#include "include/gpu/GrRecordingContext.h"
Robert Phillips3e5e2f22019-12-19 11:19:16 -050011#include "src/core/SkAutoPixmapStorage.h"
Robert Phillips99dead92020-01-27 16:11:57 -050012#include "src/core/SkCompressedDataUtils.h"
Mike Reed13711eb2020-07-14 17:16:32 -040013#include "src/core/SkMipmap.h"
Mike Reed839eef32020-12-23 11:18:24 -050014#include "src/core/SkPaintPriv.h"
Greg Daniel01f278c2020-06-12 16:58:17 -040015#include "src/gpu/GrBackendUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.h"
Robert Phillips3e5e2f22019-12-19 11:19:16 -050017#include "src/image/SkImage_Base.h"
18#include "tests/Test.h"
19#include "tests/TestUtils.h"
20#include "tools/ToolUtils.h"
21
Robert Phillips07f0e412020-01-17 15:20:00 -050022// Just verify that 'actual' is entirely 'expected'
Robert Phillips3e5e2f22019-12-19 11:19:16 -050023static void check_solid_pixmap(skiatest::Reporter* reporter,
24 const SkColor4f& expected, const SkPixmap& actual,
25 const char* label0, const char* label1, const char* label2) {
26 const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f };
27
28 auto error = std::function<ComparePixmapsErrorReporter>(
29 [reporter, label0, label1, label2](int x, int y, const float diffs[4]) {
30 SkASSERT(x >= 0 && y >= 0);
31 ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)",
32 label0, label1, label2, x, y,
33 diffs[0], diffs[1], diffs[2], diffs[3]);
34 });
35
36 CheckSolidPixels(expected, actual, tols, error);
37}
38
Robert Phillips07f0e412020-01-17 15:20:00 -050039// Create an SkImage to wrap 'backendTex'
Adlai Holler14dc7912020-08-11 15:48:49 +000040sk_sp<SkImage> create_image(GrDirectContext* dContext, const GrBackendTexture& backendTex) {
Greg Daniel01f278c2020-06-12 16:58:17 -040041 SkImage::CompressionType compression =
42 GrBackendFormatToCompressionType(backendTex.getBackendFormat());
Robert Phillips3e5e2f22019-12-19 11:19:16 -050043
Robert Phillips99dead92020-01-27 16:11:57 -050044 SkAlphaType at = SkCompressionTypeIsOpaque(compression) ? kOpaque_SkAlphaType
Robert Phillipsb0855272020-01-15 12:56:52 -050045 : kPremul_SkAlphaType;
Robert Phillips3e5e2f22019-12-19 11:19:16 -050046
Adlai Holler14dc7912020-08-11 15:48:49 +000047 return SkImage::MakeFromCompressedTexture(dContext,
Robert Phillips07f0e412020-01-17 15:20:00 -050048 backendTex,
49 kTopLeft_GrSurfaceOrigin,
50 at,
51 nullptr);
52}
53
54// Draw the compressed backend texture (wrapped in an SkImage) into an RGBA surface, attempting
55// to access all the mipMap levels.
Robert Phillipsc8ae4942020-07-20 10:56:01 -040056static void check_compressed_mipmaps(GrRecordingContext* rContext, sk_sp<SkImage> img,
Robert Phillips07f0e412020-01-17 15:20:00 -050057 SkImage::CompressionType compressionType,
58 const SkColor4f expectedColors[6],
Brian Salomon7e67dca2020-07-21 09:27:25 -040059 GrMipmapped mipMapped,
Robert Phillips07f0e412020-01-17 15:20:00 -050060 skiatest::Reporter* reporter, const char* label) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -050061
62 SkImageInfo readbackSurfaceII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
63 kPremul_SkAlphaType);
64
Robert Phillipsc8ae4942020-07-20 10:56:01 -040065 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -050066 SkBudgeted::kNo,
67 readbackSurfaceII, 1,
68 kTopLeft_GrSurfaceOrigin,
69 nullptr);
70 if (!surf) {
71 return;
72 }
73
74 SkCanvas* canvas = surf->getCanvas();
75
76 SkPaint p;
Mike Reed839eef32020-12-23 11:18:24 -050077 SkPaintPriv::SetFQ(&p, kMedium_SkFilterQuality); // to force mipMapping
Robert Phillipsb0855272020-01-15 12:56:52 -050078 p.setBlendMode(SkBlendMode::kSrc);
Robert Phillips3e5e2f22019-12-19 11:19:16 -050079
80 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -040081 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -040082 numMipLevels = SkMipmap::ComputeLevelCount(32, 32)+1;
Robert Phillips3e5e2f22019-12-19 11:19:16 -050083 }
84
85 for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
86 SkASSERT(rectSize >= 1);
87
88 canvas->clear(SK_ColorTRANSPARENT);
89
90 SkRect r = SkRect::MakeWH(rectSize, rectSize);
91 canvas->drawImageRect(img, r, &p);
92
93 SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
94 kRGBA_8888_SkColorType,
95 kUnpremul_SkAlphaType);
96 SkAutoPixmapStorage actual2;
97 SkAssertResult(actual2.tryAlloc(readbackII));
98 actual2.erase(SkColors::kTransparent);
99
100 bool result = surf->readPixels(actual2, 0, 0);
101 REPORTER_ASSERT(reporter, result);
102
103 SkString str;
104 str.appendf("mip-level %d", i);
105
106 check_solid_pixmap(reporter, expectedColors[i], actual2,
Robert Phillips07f0e412020-01-17 15:20:00 -0500107 GrCompressionTypeToStr(compressionType), label, str.c_str());
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500108 }
109}
110
Robert Phillips07f0e412020-01-17 15:20:00 -0500111// Verify that we can readback from a compressed texture
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400112static void check_readback(GrDirectContext* dContext, sk_sp<SkImage> img,
Robert Phillips07f0e412020-01-17 15:20:00 -0500113 SkImage::CompressionType compressionType,
114 const SkColor4f& expectedColor,
115 skiatest::Reporter* reporter, const char* label) {
Robert Phillips314524e2020-01-30 08:38:40 -0500116#ifdef SK_BUILD_FOR_IOS
117 // reading back ETC2 is broken on Metal/iOS (skbug.com/9839)
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400118 if (dContext->backend() == GrBackendApi::kMetal) {
Robert Phillips314524e2020-01-30 08:38:40 -0500119 return;
120 }
121#endif
122
Robert Phillips07f0e412020-01-17 15:20:00 -0500123 SkAutoPixmapStorage actual;
124
125 SkImageInfo readBackII = SkImageInfo::Make(img->width(), img->height(),
126 kRGBA_8888_SkColorType,
127 kUnpremul_SkAlphaType);
128
129 SkAssertResult(actual.tryAlloc(readBackII));
130 actual.erase(SkColors::kTransparent);
131
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400132 bool result = img->readPixels(dContext, actual, 0, 0);
Robert Phillips07f0e412020-01-17 15:20:00 -0500133 REPORTER_ASSERT(reporter, result);
134
135 check_solid_pixmap(reporter, expectedColor, actual,
136 GrCompressionTypeToStr(compressionType), label, "");
137}
138
139// Test initialization of compressed GrBackendTextures to a specific color
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400140static void test_compressed_color_init(GrDirectContext* dContext,
Robert Phillips07f0e412020-01-17 15:20:00 -0500141 skiatest::Reporter* reporter,
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400142 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500143 const SkColor4f&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400144 GrMipmapped)> create,
Robert Phillips07f0e412020-01-17 15:20:00 -0500145 const SkColor4f& color,
146 SkImage::CompressionType compression,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400147 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400148 GrBackendTexture backendTex = create(dContext, color, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500149 if (!backendTex.isValid()) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500150 return;
151 }
152
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400153 sk_sp<SkImage> img = create_image(dContext, backendTex);
Robert Phillips07f0e412020-01-17 15:20:00 -0500154 if (!img) {
155 return;
156 }
157
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500158 SkColor4f expectedColors[6] = { color, color, color, color, color, color };
159
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400160 check_compressed_mipmaps(dContext, img, compression, expectedColors, mipMapped,
Robert Phillips07f0e412020-01-17 15:20:00 -0500161 reporter, "colorinit");
Greg Daniel95afafb2020-07-22 12:09:26 -0400162 check_readback(dContext, img, compression, color, reporter, "solid readback");
163
164 SkColor4f newColor;
165 newColor.fR = color.fB;
166 newColor.fG = color.fR;
167 newColor.fB = color.fG;
168 newColor.fA = color.fA;
169
170 bool result = dContext->updateCompressedBackendTexture(backendTex, newColor, nullptr, nullptr);
171 // Since we were able to create the compressed texture we should be able to update it.
172 REPORTER_ASSERT(reporter, result);
173
174 SkColor4f expectedNewColors[6] = {newColor, newColor, newColor, newColor, newColor, newColor};
175
176 check_compressed_mipmaps(dContext, img, compression, expectedNewColors, mipMapped, reporter,
177 "colorinit");
178 check_readback(dContext, std::move(img), compression, newColor, reporter, "solid readback");
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500179
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400180 dContext->deleteBackendTexture(backendTex);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500181}
182
Robert Phillips07f0e412020-01-17 15:20:00 -0500183// Create compressed data pulling the color for each mipmap level from 'levelColors'.
Robert Phillipsca0f8372019-12-20 09:57:41 -0500184static std::unique_ptr<const char[]> make_compressed_data(SkImage::CompressionType compression,
185 SkColor4f levelColors[6],
Brian Salomon7e67dca2020-07-21 09:27:25 -0400186 GrMipmapped mipMapped) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500187 SkISize dimensions { 32, 32 };
188
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500189 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400190 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400191 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500192 }
193
Robert Phillipsca0f8372019-12-20 09:57:41 -0500194 SkTArray<size_t> mipMapOffsets(numMipLevels);
195
Robert Phillips99dead92020-01-27 16:11:57 -0500196 size_t dataSize = SkCompressedDataSize(compression, dimensions, &mipMapOffsets,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400197 mipMapped == GrMipmapped::kYes);
Robert Phillipsca0f8372019-12-20 09:57:41 -0500198 char* data = new char[dataSize];
199
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500200 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillipsca0f8372019-12-20 09:57:41 -0500201 // We have to do this a level at a time bc we might have a different color for
202 // each level
203 GrFillInCompressedData(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400204 GrMipmapped::kNo, &data[mipMapOffsets[level]], levelColors[level]);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500205
Brian Osman788b9162020-02-07 10:36:46 -0500206 dimensions = {std::max(1, dimensions.width() /2), std::max(1, dimensions.height()/2)};
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500207 }
208
209 return std::unique_ptr<const char[]>(data);
210}
211
Robert Phillips07f0e412020-01-17 15:20:00 -0500212// Verify that we can initialize a compressed backend texture with data (esp.
213// the mipmap levels).
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400214static void test_compressed_data_init(GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500215 skiatest::Reporter* reporter,
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400216 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500217 const char* data,
218 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400219 GrMipmapped)> create,
Robert Phillipsca0f8372019-12-20 09:57:41 -0500220 SkImage::CompressionType compression,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400221 GrMipmapped mipMapped) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500222
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500223 SkColor4f expectedColors[6] = {
224 { 1.0f, 0.0f, 0.0f, 1.0f }, // R
225 { 0.0f, 1.0f, 0.0f, 1.0f }, // G
226 { 0.0f, 0.0f, 1.0f, 1.0f }, // B
227 { 0.0f, 1.0f, 1.0f, 1.0f }, // C
228 { 1.0f, 0.0f, 1.0f, 1.0f }, // M
229 { 1.0f, 1.0f, 0.0f, 1.0f }, // Y
230 };
231
Robert Phillipsca0f8372019-12-20 09:57:41 -0500232 std::unique_ptr<const char[]> data(make_compressed_data(compression, expectedColors,
233 mipMapped));
Robert Phillips99dead92020-01-27 16:11:57 -0500234 size_t dataSize = SkCompressedDataSize(compression, { 32, 32 }, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400235 mipMapped == GrMipmapped::kYes);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500236
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400237 GrBackendTexture backendTex = create(dContext, data.get(), dataSize, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500238 if (!backendTex.isValid()) {
239 return;
240 }
241
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400242 sk_sp<SkImage> img = create_image(dContext, backendTex);
Robert Phillips07f0e412020-01-17 15:20:00 -0500243 if (!img) {
244 return;
245 }
246
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400247 check_compressed_mipmaps(dContext, img, compression, expectedColors,
Robert Phillips07f0e412020-01-17 15:20:00 -0500248 mipMapped, reporter, "pixmap");
Greg Daniel95afafb2020-07-22 12:09:26 -0400249 check_readback(dContext, img, compression, expectedColors[0], reporter, "data readback");
250
251 SkColor4f expectedColorsNew[6] = {
252 {1.0f, 1.0f, 0.0f, 1.0f}, // Y
253 {1.0f, 0.0f, 0.0f, 1.0f}, // R
254 {0.0f, 1.0f, 0.0f, 1.0f}, // G
255 {0.0f, 0.0f, 1.0f, 1.0f}, // B
256 {0.0f, 1.0f, 1.0f, 1.0f}, // C
257 {1.0f, 0.0f, 1.0f, 1.0f}, // M
258 };
259
260 std::unique_ptr<const char[]> dataNew(
261 make_compressed_data(compression, expectedColorsNew, mipMapped));
262 size_t dataNewSize =
263 SkCompressedDataSize(compression, {32, 32}, nullptr, mipMapped == GrMipMapped::kYes);
264
265 bool result = dContext->updateCompressedBackendTexture(backendTex, dataNew.get(), dataNewSize,
266 nullptr, nullptr);
267 // Since we were able to create the compressed texture we should be able to update it.
268 REPORTER_ASSERT(reporter, result);
269
270 check_compressed_mipmaps(dContext, img, compression, expectedColorsNew, mipMapped, reporter,
271 "pixmap");
272 check_readback(dContext, std::move(img), compression, expectedColorsNew[0], reporter,
Robert Phillips07f0e412020-01-17 15:20:00 -0500273 "data readback");
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500274
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400275 dContext->deleteBackendTexture(backendTex);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500276}
277
278DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CompressedBackendAllocationTest, reporter, ctxInfo) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400279 auto dContext = ctxInfo.directContext();
280 const GrCaps* caps = dContext->priv().caps();
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500281
282 struct {
283 SkImage::CompressionType fCompression;
284 SkColor4f fColor;
285 } combinations[] = {
Robert Phillipsc558f722020-01-13 13:02:26 -0500286 { SkImage::CompressionType::kETC2_RGB8_UNORM, SkColors::kRed },
Robert Phillips07f0e412020-01-17 15:20:00 -0500287 { SkImage::CompressionType::kBC1_RGB8_UNORM, SkColors::kBlue },
Robert Phillipsb0855272020-01-15 12:56:52 -0500288 { SkImage::CompressionType::kBC1_RGBA8_UNORM, SkColors::kTransparent },
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500289 };
290
291 for (auto combo : combinations) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400292 GrBackendFormat format = dContext->compressedBackendFormat(combo.fCompression);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500293 if (!format.isValid()) {
294 continue;
295 }
296
297 if (!caps->isFormatTexturable(format)) {
298 continue;
299 }
300
Brian Salomon7e67dca2020-07-21 09:27:25 -0400301 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -0400302 if (GrMipmapped::kYes == mipMapped && !caps->mipmapSupport()) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500303 continue;
304 }
305
306 // color initialized
307 {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400308 auto createWithColorMtd = [format](GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500309 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400310 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400311 return dContext->createCompressedBackendTexture(32, 32, format, color,
312 mipMapped, GrProtected::kNo);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500313 };
314
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400315 test_compressed_color_init(dContext, reporter, createWithColorMtd,
Robert Phillips07f0e412020-01-17 15:20:00 -0500316 combo.fColor, combo.fCompression, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500317 }
318
319 // data initialized
320 {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400321 auto createWithDataMtd = [format](GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500322 const char* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400323 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400324 return dContext->createCompressedBackendTexture(32, 32, format, data, dataSize,
325 mipMapped, GrProtected::kNo);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500326 };
327
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400328 test_compressed_data_init(dContext, reporter, createWithDataMtd,
Robert Phillipsca0f8372019-12-20 09:57:41 -0500329 combo.fCompression, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500330 }
331
332 }
333 }
334}