blob: 4966eafe74ab2e333c3a6fa39478298084ce98d6 [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"
Greg Daniel01f278c2020-06-12 16:58:17 -040014#include "src/gpu/GrBackendUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040015#include "src/gpu/GrDirectContextPriv.h"
Robert Phillips3e5e2f22019-12-19 11:19:16 -050016#include "src/image/SkImage_Base.h"
17#include "tests/Test.h"
18#include "tests/TestUtils.h"
19#include "tools/ToolUtils.h"
20
Robert Phillips07f0e412020-01-17 15:20:00 -050021// Just verify that 'actual' is entirely 'expected'
Robert Phillips3e5e2f22019-12-19 11:19:16 -050022static void check_solid_pixmap(skiatest::Reporter* reporter,
23 const SkColor4f& expected, const SkPixmap& actual,
24 const char* label0, const char* label1, const char* label2) {
25 const float tols[4] = { 0.01f, 0.01f, 0.01f, 0.01f };
26
27 auto error = std::function<ComparePixmapsErrorReporter>(
28 [reporter, label0, label1, label2](int x, int y, const float diffs[4]) {
29 SkASSERT(x >= 0 && y >= 0);
30 ERRORF(reporter, "%s %s %s - mismatch at %d, %d (%f, %f, %f %f)",
31 label0, label1, label2, x, y,
32 diffs[0], diffs[1], diffs[2], diffs[3]);
33 });
34
35 CheckSolidPixels(expected, actual, tols, error);
36}
37
Robert Phillips07f0e412020-01-17 15:20:00 -050038// Create an SkImage to wrap 'backendTex'
Adlai Holler14dc7912020-08-11 15:48:49 +000039sk_sp<SkImage> create_image(GrDirectContext* dContext, const GrBackendTexture& backendTex) {
Greg Daniel01f278c2020-06-12 16:58:17 -040040 SkImage::CompressionType compression =
41 GrBackendFormatToCompressionType(backendTex.getBackendFormat());
Robert Phillips3e5e2f22019-12-19 11:19:16 -050042
Robert Phillips99dead92020-01-27 16:11:57 -050043 SkAlphaType at = SkCompressionTypeIsOpaque(compression) ? kOpaque_SkAlphaType
Robert Phillipsb0855272020-01-15 12:56:52 -050044 : kPremul_SkAlphaType;
Robert Phillips3e5e2f22019-12-19 11:19:16 -050045
Adlai Holler14dc7912020-08-11 15:48:49 +000046 return SkImage::MakeFromCompressedTexture(dContext,
Robert Phillips07f0e412020-01-17 15:20:00 -050047 backendTex,
48 kTopLeft_GrSurfaceOrigin,
49 at,
50 nullptr);
51}
52
53// Draw the compressed backend texture (wrapped in an SkImage) into an RGBA surface, attempting
54// to access all the mipMap levels.
Robert Phillipsc8ae4942020-07-20 10:56:01 -040055static void check_compressed_mipmaps(GrRecordingContext* rContext, sk_sp<SkImage> img,
Robert Phillips07f0e412020-01-17 15:20:00 -050056 SkImage::CompressionType compressionType,
57 const SkColor4f expectedColors[6],
Brian Salomon7e67dca2020-07-21 09:27:25 -040058 GrMipmapped mipMapped,
Robert Phillips07f0e412020-01-17 15:20:00 -050059 skiatest::Reporter* reporter, const char* label) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -050060
61 SkImageInfo readbackSurfaceII = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
62 kPremul_SkAlphaType);
63
Robert Phillipsc8ae4942020-07-20 10:56:01 -040064 sk_sp<SkSurface> surf = SkSurface::MakeRenderTarget(rContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -050065 SkBudgeted::kNo,
66 readbackSurfaceII, 1,
67 kTopLeft_GrSurfaceOrigin,
68 nullptr);
69 if (!surf) {
70 return;
71 }
72
73 SkCanvas* canvas = surf->getCanvas();
74
75 SkPaint p;
Mike Reed8dacba32020-11-19 09:50:28 -050076 p.setFilterQuality(kMedium_SkFilterQuality); // to force mipMapping
Robert Phillipsb0855272020-01-15 12:56:52 -050077 p.setBlendMode(SkBlendMode::kSrc);
Robert Phillips3e5e2f22019-12-19 11:19:16 -050078
79 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -040080 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -040081 numMipLevels = SkMipmap::ComputeLevelCount(32, 32)+1;
Robert Phillips3e5e2f22019-12-19 11:19:16 -050082 }
83
84 for (int i = 0, rectSize = 32; i < numMipLevels; ++i, rectSize /= 2) {
85 SkASSERT(rectSize >= 1);
86
87 canvas->clear(SK_ColorTRANSPARENT);
88
89 SkRect r = SkRect::MakeWH(rectSize, rectSize);
90 canvas->drawImageRect(img, r, &p);
91
92 SkImageInfo readbackII = SkImageInfo::Make(rectSize, rectSize,
93 kRGBA_8888_SkColorType,
94 kUnpremul_SkAlphaType);
95 SkAutoPixmapStorage actual2;
96 SkAssertResult(actual2.tryAlloc(readbackII));
97 actual2.erase(SkColors::kTransparent);
98
99 bool result = surf->readPixels(actual2, 0, 0);
100 REPORTER_ASSERT(reporter, result);
101
102 SkString str;
103 str.appendf("mip-level %d", i);
104
105 check_solid_pixmap(reporter, expectedColors[i], actual2,
Robert Phillips07f0e412020-01-17 15:20:00 -0500106 GrCompressionTypeToStr(compressionType), label, str.c_str());
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500107 }
108}
109
Robert Phillips07f0e412020-01-17 15:20:00 -0500110// Verify that we can readback from a compressed texture
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400111static void check_readback(GrDirectContext* dContext, sk_sp<SkImage> img,
Robert Phillips07f0e412020-01-17 15:20:00 -0500112 SkImage::CompressionType compressionType,
113 const SkColor4f& expectedColor,
114 skiatest::Reporter* reporter, const char* label) {
Robert Phillips314524e2020-01-30 08:38:40 -0500115#ifdef SK_BUILD_FOR_IOS
116 // reading back ETC2 is broken on Metal/iOS (skbug.com/9839)
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400117 if (dContext->backend() == GrBackendApi::kMetal) {
Robert Phillips314524e2020-01-30 08:38:40 -0500118 return;
119 }
120#endif
121
Robert Phillips07f0e412020-01-17 15:20:00 -0500122 SkAutoPixmapStorage actual;
123
124 SkImageInfo readBackII = SkImageInfo::Make(img->width(), img->height(),
125 kRGBA_8888_SkColorType,
126 kUnpremul_SkAlphaType);
127
128 SkAssertResult(actual.tryAlloc(readBackII));
129 actual.erase(SkColors::kTransparent);
130
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400131 bool result = img->readPixels(dContext, actual, 0, 0);
Robert Phillips07f0e412020-01-17 15:20:00 -0500132 REPORTER_ASSERT(reporter, result);
133
134 check_solid_pixmap(reporter, expectedColor, actual,
135 GrCompressionTypeToStr(compressionType), label, "");
136}
137
138// Test initialization of compressed GrBackendTextures to a specific color
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400139static void test_compressed_color_init(GrDirectContext* dContext,
Robert Phillips07f0e412020-01-17 15:20:00 -0500140 skiatest::Reporter* reporter,
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400141 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500142 const SkColor4f&,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400143 GrMipmapped)> create,
Robert Phillips07f0e412020-01-17 15:20:00 -0500144 const SkColor4f& color,
145 SkImage::CompressionType compression,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400146 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400147 GrBackendTexture backendTex = create(dContext, color, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500148 if (!backendTex.isValid()) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500149 return;
150 }
151
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400152 sk_sp<SkImage> img = create_image(dContext, backendTex);
Robert Phillips07f0e412020-01-17 15:20:00 -0500153 if (!img) {
154 return;
155 }
156
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500157 SkColor4f expectedColors[6] = { color, color, color, color, color, color };
158
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400159 check_compressed_mipmaps(dContext, img, compression, expectedColors, mipMapped,
Robert Phillips07f0e412020-01-17 15:20:00 -0500160 reporter, "colorinit");
Greg Daniel95afafb2020-07-22 12:09:26 -0400161 check_readback(dContext, img, compression, color, reporter, "solid readback");
162
163 SkColor4f newColor;
164 newColor.fR = color.fB;
165 newColor.fG = color.fR;
166 newColor.fB = color.fG;
167 newColor.fA = color.fA;
168
169 bool result = dContext->updateCompressedBackendTexture(backendTex, newColor, nullptr, nullptr);
170 // Since we were able to create the compressed texture we should be able to update it.
171 REPORTER_ASSERT(reporter, result);
172
173 SkColor4f expectedNewColors[6] = {newColor, newColor, newColor, newColor, newColor, newColor};
174
175 check_compressed_mipmaps(dContext, img, compression, expectedNewColors, mipMapped, reporter,
176 "colorinit");
177 check_readback(dContext, std::move(img), compression, newColor, reporter, "solid readback");
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500178
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400179 dContext->deleteBackendTexture(backendTex);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500180}
181
Robert Phillips07f0e412020-01-17 15:20:00 -0500182// Create compressed data pulling the color for each mipmap level from 'levelColors'.
Robert Phillipsca0f8372019-12-20 09:57:41 -0500183static std::unique_ptr<const char[]> make_compressed_data(SkImage::CompressionType compression,
184 SkColor4f levelColors[6],
Brian Salomon7e67dca2020-07-21 09:27:25 -0400185 GrMipmapped mipMapped) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500186 SkISize dimensions { 32, 32 };
187
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500188 int numMipLevels = 1;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400189 if (mipMapped == GrMipmapped::kYes) {
Mike Reed13711eb2020-07-14 17:16:32 -0400190 numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500191 }
192
Robert Phillipsca0f8372019-12-20 09:57:41 -0500193 SkTArray<size_t> mipMapOffsets(numMipLevels);
194
Robert Phillips99dead92020-01-27 16:11:57 -0500195 size_t dataSize = SkCompressedDataSize(compression, dimensions, &mipMapOffsets,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400196 mipMapped == GrMipmapped::kYes);
Robert Phillipsca0f8372019-12-20 09:57:41 -0500197 char* data = new char[dataSize];
198
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500199 for (int level = 0; level < numMipLevels; ++level) {
Robert Phillipsca0f8372019-12-20 09:57:41 -0500200 // We have to do this a level at a time bc we might have a different color for
201 // each level
202 GrFillInCompressedData(compression, dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400203 GrMipmapped::kNo, &data[mipMapOffsets[level]], levelColors[level]);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500204
Brian Osman788b9162020-02-07 10:36:46 -0500205 dimensions = {std::max(1, dimensions.width() /2), std::max(1, dimensions.height()/2)};
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500206 }
207
208 return std::unique_ptr<const char[]>(data);
209}
210
Robert Phillips07f0e412020-01-17 15:20:00 -0500211// Verify that we can initialize a compressed backend texture with data (esp.
212// the mipmap levels).
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400213static void test_compressed_data_init(GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500214 skiatest::Reporter* reporter,
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400215 std::function<GrBackendTexture (GrDirectContext*,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500216 const char* data,
217 size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400218 GrMipmapped)> create,
Robert Phillipsca0f8372019-12-20 09:57:41 -0500219 SkImage::CompressionType compression,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400220 GrMipmapped mipMapped) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500221
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500222 SkColor4f expectedColors[6] = {
223 { 1.0f, 0.0f, 0.0f, 1.0f }, // R
224 { 0.0f, 1.0f, 0.0f, 1.0f }, // G
225 { 0.0f, 0.0f, 1.0f, 1.0f }, // B
226 { 0.0f, 1.0f, 1.0f, 1.0f }, // C
227 { 1.0f, 0.0f, 1.0f, 1.0f }, // M
228 { 1.0f, 1.0f, 0.0f, 1.0f }, // Y
229 };
230
Robert Phillipsca0f8372019-12-20 09:57:41 -0500231 std::unique_ptr<const char[]> data(make_compressed_data(compression, expectedColors,
232 mipMapped));
Robert Phillips99dead92020-01-27 16:11:57 -0500233 size_t dataSize = SkCompressedDataSize(compression, { 32, 32 }, nullptr,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400234 mipMapped == GrMipmapped::kYes);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500235
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400236 GrBackendTexture backendTex = create(dContext, data.get(), dataSize, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500237 if (!backendTex.isValid()) {
238 return;
239 }
240
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400241 sk_sp<SkImage> img = create_image(dContext, backendTex);
Robert Phillips07f0e412020-01-17 15:20:00 -0500242 if (!img) {
243 return;
244 }
245
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400246 check_compressed_mipmaps(dContext, img, compression, expectedColors,
Robert Phillips07f0e412020-01-17 15:20:00 -0500247 mipMapped, reporter, "pixmap");
Greg Daniel95afafb2020-07-22 12:09:26 -0400248 check_readback(dContext, img, compression, expectedColors[0], reporter, "data readback");
249
250 SkColor4f expectedColorsNew[6] = {
251 {1.0f, 1.0f, 0.0f, 1.0f}, // Y
252 {1.0f, 0.0f, 0.0f, 1.0f}, // R
253 {0.0f, 1.0f, 0.0f, 1.0f}, // G
254 {0.0f, 0.0f, 1.0f, 1.0f}, // B
255 {0.0f, 1.0f, 1.0f, 1.0f}, // C
256 {1.0f, 0.0f, 1.0f, 1.0f}, // M
257 };
258
259 std::unique_ptr<const char[]> dataNew(
260 make_compressed_data(compression, expectedColorsNew, mipMapped));
261 size_t dataNewSize =
262 SkCompressedDataSize(compression, {32, 32}, nullptr, mipMapped == GrMipMapped::kYes);
263
264 bool result = dContext->updateCompressedBackendTexture(backendTex, dataNew.get(), dataNewSize,
265 nullptr, nullptr);
266 // Since we were able to create the compressed texture we should be able to update it.
267 REPORTER_ASSERT(reporter, result);
268
269 check_compressed_mipmaps(dContext, img, compression, expectedColorsNew, mipMapped, reporter,
270 "pixmap");
271 check_readback(dContext, std::move(img), compression, expectedColorsNew[0], reporter,
Robert Phillips07f0e412020-01-17 15:20:00 -0500272 "data readback");
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500273
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400274 dContext->deleteBackendTexture(backendTex);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500275}
276
277DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CompressedBackendAllocationTest, reporter, ctxInfo) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400278 auto dContext = ctxInfo.directContext();
279 const GrCaps* caps = dContext->priv().caps();
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500280
281 struct {
282 SkImage::CompressionType fCompression;
283 SkColor4f fColor;
284 } combinations[] = {
Robert Phillipsc558f722020-01-13 13:02:26 -0500285 { SkImage::CompressionType::kETC2_RGB8_UNORM, SkColors::kRed },
Robert Phillips07f0e412020-01-17 15:20:00 -0500286 { SkImage::CompressionType::kBC1_RGB8_UNORM, SkColors::kBlue },
Robert Phillipsb0855272020-01-15 12:56:52 -0500287 { SkImage::CompressionType::kBC1_RGBA8_UNORM, SkColors::kTransparent },
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500288 };
289
290 for (auto combo : combinations) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400291 GrBackendFormat format = dContext->compressedBackendFormat(combo.fCompression);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500292 if (!format.isValid()) {
293 continue;
294 }
295
296 if (!caps->isFormatTexturable(format)) {
297 continue;
298 }
299
Brian Salomon7e67dca2020-07-21 09:27:25 -0400300 for (auto mipMapped : { GrMipmapped::kNo, GrMipmapped::kYes }) {
Brian Salomon69100f02020-07-21 10:49:25 -0400301 if (GrMipmapped::kYes == mipMapped && !caps->mipmapSupport()) {
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500302 continue;
303 }
304
305 // color initialized
306 {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400307 auto createWithColorMtd = [format](GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500308 const SkColor4f& color,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400309 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400310 return dContext->createCompressedBackendTexture(32, 32, format, color,
311 mipMapped, GrProtected::kNo);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500312 };
313
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400314 test_compressed_color_init(dContext, reporter, createWithColorMtd,
Robert Phillips07f0e412020-01-17 15:20:00 -0500315 combo.fColor, combo.fCompression, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500316 }
317
318 // data initialized
319 {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400320 auto createWithDataMtd = [format](GrDirectContext* dContext,
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500321 const char* data, size_t dataSize,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400322 GrMipmapped mipMapped) {
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400323 return dContext->createCompressedBackendTexture(32, 32, format, data, dataSize,
324 mipMapped, GrProtected::kNo);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500325 };
326
Robert Phillipsc8ae4942020-07-20 10:56:01 -0400327 test_compressed_data_init(dContext, reporter, createWithDataMtd,
Robert Phillipsca0f8372019-12-20 09:57:41 -0500328 combo.fCompression, mipMapped);
Robert Phillips3e5e2f22019-12-19 11:19:16 -0500329 }
330
331 }
332 }
333}