| 1 |
/* |
| 2 |
* Copyright 2020 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 |
|
| 8 |
#include "include/core/SkTypes.h" |
| 9 |
|
| 10 |
#include "gm/gm.h" |
| 11 |
#include "include/core/SkBitmap.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkImageInfo.h" |
| 17 |
#include "include/core/SkPath.h" |
| 18 |
#include "include/core/SkPathBuilder.h" |
| 19 |
#include "include/core/SkRect.h" |
| 20 |
#include "include/core/SkRefCnt.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkTextureCompressionType.h" |
| 24 |
#include "src/core/SkCompressedDataUtils.h" |
| 25 |
#include "src/core/SkMipmap.h" |
| 26 |
#include "src/image/SkImage_Base.h" |
| 27 |
#include "third_party/etc1/etc1.h" |
| 28 |
#include "tools/gpu/CompressedTexture.h" |
| 29 |
|
| 30 |
#if defined(SK_GANESH) |
| 31 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 32 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 33 |
#include "include/gpu/ganesh/SkImageGanesh.h" |
| 34 |
#include "src/gpu/ganesh/GrCaps.h" |
| 35 |
#include "src/gpu/ganesh/GrImageContextPriv.h" |
| 36 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 37 |
#include "src/gpu/ganesh/image/SkImage_GaneshBase.h" |
| 38 |
#include "tools/ganesh/ProxyUtils.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined(SK_GRAPHITE) |
| 42 |
#include "include/gpu/graphite/Image.h" |
| 43 |
#include "include/gpu/graphite/Recorder.h" |
| 44 |
#include "src/gpu/GpuTypesPriv.h" |
| 45 |
#include "src/gpu/graphite/Caps.h" |
| 46 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 47 |
#include "tools/gpu/ManagedBackendTexture.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
static SkPoint gen_pt(float angle, const SkVector& scale) { |
| 51 |
SkScalar s = SkScalarSin(angle); |
| 52 |
SkScalar c = SkScalarCos(angle); |
| 53 |
|
| 54 |
return { scale.fX * c, scale.fY * s }; |
| 55 |
} |
| 56 |
|
| 57 |
// The resulting path will be centered at (0,0) and its size will match 'dimensions' |
| 58 |
static SkPath make_gear(SkISize dimensions, int numTeeth) { |
| 59 |
SkVector outerRad{ dimensions.fWidth / 2.0f, dimensions.fHeight / 2.0f }; |
| 60 |
SkVector innerRad{ dimensions.fWidth / 2.5f, dimensions.fHeight / 2.5f }; |
| 61 |
const float kAnglePerTooth = 2.0f * SK_ScalarPI / (3 * numTeeth); |
| 62 |
|
| 63 |
float angle = 0.0f; |
| 64 |
|
| 65 |
SkPathBuilder tmp(SkPathFillType::kWinding); |
| 66 |
|
| 67 |
tmp.moveTo(gen_pt(angle, outerRad)); |
| 68 |
|
| 69 |
for (int i = 0; i < numTeeth; ++i, angle += 3*kAnglePerTooth) { |
| 70 |
tmp.lineTo(gen_pt(angle+kAnglePerTooth, outerRad)); |
| 71 |
tmp.lineTo(gen_pt(angle+(1.5f*kAnglePerTooth), innerRad)); |
| 72 |
tmp.lineTo(gen_pt(angle+(2.5f*kAnglePerTooth), innerRad)); |
| 73 |
tmp.lineTo(gen_pt(angle+(3.0f*kAnglePerTooth), outerRad)); |
| 74 |
} |
| 75 |
|
| 76 |
tmp.close(); |
| 77 |
|
| 78 |
float fInnerRad = 0.1f * std::min(dimensions.fWidth, dimensions.fHeight); |
| 79 |
if (fInnerRad > 0.5f) { |
| 80 |
tmp.addCircle(0.0f, 0.0f, fInnerRad, SkPathDirection::kCCW); |
| 81 |
} |
| 82 |
|
| 83 |
return tmp.detach(); |
| 84 |
} |
| 85 |
|
| 86 |
// Render one level of a mipmap |
| 87 |
SkBitmap render_level(SkISize dimensions, SkColor color, SkColorType colorType, bool opaque) { |
| 88 |
SkPath path = make_gear(dimensions, 9); |
| 89 |
|
| 90 |
SkImageInfo ii = SkImageInfo::Make(dimensions.width(), dimensions.height(), |
| 91 |
colorType, opaque ? kOpaque_SkAlphaType |
| 92 |
: kPremul_SkAlphaType); |
| 93 |
SkBitmap bm; |
| 94 |
bm.allocPixels(ii); |
| 95 |
|
| 96 |
bm.eraseColor(opaque ? SK_ColorBLACK : SK_ColorTRANSPARENT); |
| 97 |
|
| 98 |
SkCanvas c(bm); |
| 99 |
|
| 100 |
SkPaint paint; |
| 101 |
paint.setColor(color | 0xFF000000); |
| 102 |
paint.setAntiAlias(false); |
| 103 |
|
| 104 |
c.translate(dimensions.width() / 2.0f, dimensions.height() / 2.0f); |
| 105 |
c.drawPath(path, paint); |
| 106 |
|
| 107 |
return bm; |
| 108 |
} |
| 109 |
|
| 110 |
struct CompressedImageObjects { |
| 111 |
sk_sp<SkImage> fImage; |
| 112 |
#if defined(SK_GRAPHITE) |
| 113 |
sk_sp<sk_gpu_test::ManagedGraphiteTexture> fGraphiteTexture; |
| 114 |
#else |
| 115 |
void* fGraphiteTexture = nullptr; |
| 116 |
#endif |
| 117 |
}; |
| 118 |
|
| 119 |
// Create the compressed data blob needed to represent a mipmapped 2-color texture of the specified |
| 120 |
// compression format. In this case 2-color means either opaque black or transparent black plus |
| 121 |
// one other color. |
| 122 |
// Note that ETC1/ETC2_RGB8_UNORM only supports 565 opaque textures. |
| 123 |
static CompressedImageObjects make_compressed_image(SkCanvas* canvas, |
| 124 |
const SkISize dimensions, |
| 125 |
SkColorType colorType, |
| 126 |
bool opaque, |
| 127 |
SkTextureCompressionType compression) { |
| 128 |
size_t totalSize = SkCompressedDataSize(compression, dimensions, nullptr, true); |
| 129 |
|
| 130 |
sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize); |
| 131 |
char* pixels = (char*) tmp->writable_data(); |
| 132 |
|
| 133 |
int numMipLevels = SkMipmap::ComputeLevelCount(dimensions) + 1; |
| 134 |
|
| 135 |
size_t offset = 0; |
| 136 |
|
| 137 |
// Use a different color for each mipmap level so we can visually evaluate the draws |
| 138 |
static const SkColor kColors[] = { |
| 139 |
SK_ColorRED, |
| 140 |
SK_ColorGREEN, |
| 141 |
SK_ColorBLUE, |
| 142 |
SK_ColorCYAN, |
| 143 |
SK_ColorMAGENTA, |
| 144 |
SK_ColorYELLOW, |
| 145 |
SK_ColorWHITE, |
| 146 |
}; |
| 147 |
|
| 148 |
SkISize levelDims = dimensions; |
| 149 |
for (int i = 0; i < numMipLevels; ++i) { |
| 150 |
size_t levelSize = SkCompressedDataSize(compression, levelDims, nullptr, false); |
| 151 |
|
| 152 |
SkBitmap bm = render_level(levelDims, kColors[i%7], colorType, opaque); |
| 153 |
if (compression == SkTextureCompressionType::kETC2_RGB8_UNORM) { |
| 154 |
SkASSERT(bm.colorType() == kRGB_565_SkColorType); |
| 155 |
SkASSERT(opaque); |
| 156 |
|
| 157 |
if (etc1_encode_image((unsigned char*)bm.getAddr16(0, 0), |
| 158 |
bm.width(), bm.height(), 2, bm.rowBytes(), |
| 159 |
(unsigned char*) &pixels[offset])) { |
| 160 |
return {nullptr, nullptr}; |
| 161 |
} |
| 162 |
} else { |
| 163 |
sk_gpu_test::TwoColorBC1Compress(bm.pixmap(), kColors[i%7], &pixels[offset]); |
| 164 |
} |
| 165 |
|
| 166 |
offset += levelSize; |
| 167 |
levelDims = {std::max(1, levelDims.width()/2), std::max(1, levelDims.height()/2)}; |
| 168 |
} |
| 169 |
|
| 170 |
sk_sp<SkImage> image; |
| 171 |
#if defined(SK_GRAPHITE) |
| 172 |
skgpu::graphite::Recorder* recorder = canvas->recorder(); |
| 173 |
if (recorder) { |
| 174 |
sk_sp<sk_gpu_test::ManagedGraphiteTexture> texture = |
| 175 |
sk_gpu_test::ManagedGraphiteTexture::MakeFromCompressedData(recorder, |
| 176 |
dimensions, |
| 177 |
compression, |
| 178 |
tmp, |
| 179 |
skgpu::Mipmapped::kYes); |
| 180 |
if (texture) { |
| 181 |
image = SkImages::WrapTexture(recorder, |
| 182 |
texture->texture(), |
| 183 |
skgpu::CompressionTypeToSkColorType(compression), |
| 184 |
kPremul_SkAlphaType, |
| 185 |
/*colorSpace=*/nullptr); |
| 186 |
if (image) { |
| 187 |
return {image, texture}; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
#endif |
| 192 |
#if defined(SK_GANESH) |
| 193 |
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) { |
| 194 |
image = SkImages::TextureFromCompressedTextureData(dContext, |
| 195 |
std::move(tmp), |
| 196 |
dimensions.width(), |
| 197 |
dimensions.height(), |
| 198 |
compression, |
| 199 |
skgpu::Mipmapped::kYes); |
| 200 |
} else |
| 201 |
#endif |
| 202 |
{ |
| 203 |
image = SkImages::RasterFromCompressedTextureData( |
| 204 |
std::move(tmp), dimensions.width(), dimensions.height(), compression); |
| 205 |
} |
| 206 |
return {image, nullptr}; |
| 207 |
} |
| 208 |
|
| 209 |
// Basic test of Ganesh's ETC1 and BC1 support |
| 210 |
// The layout is: |
| 211 |
// ETC2 BC1 |
| 212 |
// -------------------------------------- |
| 213 |
// RGB8 | kETC2_RGB8_UNORM | kBC1_RGB8_UNORM | |
| 214 |
// |--------------------------------------| |
| 215 |
// RGBA8 | | kBC1_RGBA8_UNORM | |
| 216 |
// -------------------------------------- |
| 217 |
// |
| 218 |
// The nonPowerOfTwo and nonMultipleOfFour cases exercise some compression edge cases. |
| 219 |
class CompressedTexturesGM : public skiagm::GM { |
| 220 |
public: |
| 221 |
enum class Type { |
| 222 |
kNormal, |
| 223 |
kNonPowerOfTwo, |
| 224 |
kNonMultipleOfFour |
| 225 |
}; |
| 226 |
|
| 227 |
CompressedTexturesGM(Type type) : fType(type) { |
| 228 |
this->setBGColor(0xFFCCCCCC); |
| 229 |
|
| 230 |
switch (fType) { |
| 231 |
case Type::kNonPowerOfTwo: |
| 232 |
// These dimensions force the top two mip levels to be 1x3 and 1x1 |
| 233 |
fImgDimensions.set(20, 60); |
| 234 |
break; |
| 235 |
case Type::kNonMultipleOfFour: |
| 236 |
// These dimensions force the top three mip levels to be 1x7, 1x3 and 1x1 |
| 237 |
fImgDimensions.set(13, 61); // prime numbers - just bc |
| 238 |
break; |
| 239 |
default: |
| 240 |
fImgDimensions.set(kBaseTexWidth, kBaseTexHeight); |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
protected: |
| 247 |
SkString getName() const override { |
| 248 |
SkString name("compressed_textures"); |
| 249 |
|
| 250 |
if (fType == Type::kNonPowerOfTwo) { |
| 251 |
name.append("_npot"); |
| 252 |
} else if (fType == Type::kNonMultipleOfFour) { |
| 253 |
name.append("_nmof"); |
| 254 |
} |
| 255 |
|
| 256 |
return name; |
| 257 |
} |
| 258 |
|
| 259 |
SkISize getISize() override { |
| 260 |
return SkISize::Make(2*kCellWidth + 3*kPad, 2*kBaseTexHeight + 3*kPad); |
| 261 |
} |
| 262 |
|
| 263 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, |
| 264 |
GraphiteTestContext* graphiteTestContext) override { |
| 265 |
#if defined(SK_GANESH) |
| 266 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 267 |
if (dContext && dContext->abandoned()) { |
| 268 |
// This isn't a GpuGM so a null 'context' is okay but an abandoned context |
| 269 |
// if forbidden. |
| 270 |
return DrawResult::kSkip; |
| 271 |
} |
| 272 |
#endif |
| 273 |
|
| 274 |
if (fType == Type::kNonMultipleOfFour) { |
| 275 |
#if defined(SK_GANESH) |
| 276 |
if (dContext && dContext->backend() == GrBackendApi::kDirect3D) { |
| 277 |
// skbug.com/40041877 - Are non-multiple-of-four BC1 textures supported in D3D? |
| 278 |
return DrawResult::kSkip; |
| 279 |
} |
| 280 |
#endif |
| 281 |
#if defined(SK_GRAPHITE) |
| 282 |
skgpu::graphite::Recorder* recorder = canvas->recorder(); |
| 283 |
if (recorder && recorder->backend() == skgpu::BackendApi::kDawn) { |
| 284 |
// Dawn does not support non-multiple-of-four textures at all. For the same reason |
| 285 |
// we can't support it on older D3D devices above, neither can Dawn. However, Dawn |
| 286 |
// disables support for all devices to keep functionality uniform. |
| 287 |
return DrawResult::kSkip; |
| 288 |
} |
| 289 |
#endif |
| 290 |
} |
| 291 |
|
| 292 |
fOpaqueETC2Image = make_compressed_image(canvas, fImgDimensions, |
| 293 |
kRGB_565_SkColorType, true, |
| 294 |
SkTextureCompressionType::kETC2_RGB8_UNORM); |
| 295 |
|
| 296 |
fOpaqueBC1Image = make_compressed_image(canvas, fImgDimensions, |
| 297 |
kRGBA_8888_SkColorType, true, |
| 298 |
SkTextureCompressionType::kBC1_RGB8_UNORM); |
| 299 |
|
| 300 |
fTransparentBC1Image = make_compressed_image(canvas, fImgDimensions, |
| 301 |
kRGBA_8888_SkColorType, false, |
| 302 |
SkTextureCompressionType::kBC1_RGBA8_UNORM); |
| 303 |
|
| 304 |
if (!fOpaqueETC2Image.fImage || !fOpaqueBC1Image.fImage || !fTransparentBC1Image.fImage) { |
| 305 |
*errorMsg = "Failed to create compressed images."; |
| 306 |
return DrawResult::kFail; |
| 307 |
} |
| 308 |
|
| 309 |
return DrawResult::kOk; |
| 310 |
} |
| 311 |
|
| 312 |
void onGpuTeardown() override { |
| 313 |
fOpaqueETC2Image.fImage = nullptr; |
| 314 |
fOpaqueBC1Image.fImage = nullptr; |
| 315 |
fTransparentBC1Image.fImage = nullptr; |
| 316 |
fOpaqueETC2Image.fGraphiteTexture = nullptr; |
| 317 |
fOpaqueBC1Image.fGraphiteTexture = nullptr; |
| 318 |
fTransparentBC1Image.fGraphiteTexture = nullptr; |
| 319 |
} |
| 320 |
|
| 321 |
void onDraw(SkCanvas* canvas) override { |
| 322 |
this->drawCell(canvas, fOpaqueETC2Image.fImage.get(), { kPad, kPad }); |
| 323 |
|
| 324 |
this->drawCell(canvas, fOpaqueBC1Image.fImage.get(), { 2*kPad + kCellWidth, kPad }); |
| 325 |
|
| 326 |
this->drawCell(canvas, fTransparentBC1Image.fImage.get(), |
| 327 |
{ 2*kPad + kCellWidth, 2*kPad + kBaseTexHeight }); |
| 328 |
} |
| 329 |
|
| 330 |
private: |
| 331 |
void drawCell(SkCanvas* canvas, SkImage* image, SkIVector offset) { |
| 332 |
|
| 333 |
SkISize levelDimensions = fImgDimensions; |
| 334 |
int numMipLevels = SkMipmap::ComputeLevelCount(levelDimensions) + 1; |
| 335 |
|
| 336 |
SkSamplingOptions sampling(SkCubicResampler::Mitchell()); |
| 337 |
|
| 338 |
bool isCompressed = false; |
| 339 |
if (image->isTextureBacked()) { |
| 340 |
#if defined(SK_GANESH) |
| 341 |
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) { |
| 342 |
const GrCaps* caps = as_IB(image)->context()->priv().caps(); |
| 343 |
GrTextureProxy* proxy = sk_gpu_test::GetTextureImageProxy(image, dContext); |
| 344 |
isCompressed = caps->isFormatCompressed(proxy->backendFormat()); |
| 345 |
} else |
| 346 |
#endif |
| 347 |
{ |
| 348 |
// Graphite has no fallback to upload the compressed data to a non-compressed |
| 349 |
// format. So if the image is texture backed and graphite then it will be a |
| 350 |
// compressed format. |
| 351 |
isCompressed = true; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
SkPaint redStrokePaint; |
| 356 |
redStrokePaint.setColor(SK_ColorRED); |
| 357 |
redStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 358 |
|
| 359 |
for (int i = 0; i < numMipLevels; ++i) { |
| 360 |
SkRect r = SkRect::MakeXYWH(offset.fX, offset.fY, |
| 361 |
levelDimensions.width(), levelDimensions.height()); |
| 362 |
|
| 363 |
canvas->drawImageRect(image, r, sampling); |
| 364 |
if (!isCompressed) { |
| 365 |
// Make it obvious which drawImages used decompressed images |
| 366 |
canvas->drawRect(r, redStrokePaint); |
| 367 |
} |
| 368 |
|
| 369 |
if (i == 0) { |
| 370 |
offset.fX += levelDimensions.width()+1; |
| 371 |
} else { |
| 372 |
offset.fY += levelDimensions.height()+1; |
| 373 |
} |
| 374 |
|
| 375 |
levelDimensions = {std::max(1, levelDimensions.width()/2), |
| 376 |
std::max(1, levelDimensions.height()/2)}; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
static const int kPad = 8; |
| 381 |
static const int kBaseTexWidth = 64; |
| 382 |
static const int kCellWidth = 1.5f * kBaseTexWidth; |
| 383 |
static const int kBaseTexHeight = 64; |
| 384 |
|
| 385 |
Type fType; |
| 386 |
SkISize fImgDimensions; |
| 387 |
|
| 388 |
|
| 389 |
CompressedImageObjects fOpaqueETC2Image; |
| 390 |
CompressedImageObjects fOpaqueBC1Image; |
| 391 |
CompressedImageObjects fTransparentBC1Image; |
| 392 |
|
| 393 |
using INHERITED = GM; |
| 394 |
}; |
| 395 |
|
| 396 |
////////////////////////////////////////////////////////////////////////////// |
| 397 |
|
| 398 |
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNormal);) |
| 399 |
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNonPowerOfTwo);) |
| 400 |
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNonMultipleOfFour);) |