Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 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 "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkStream.h" |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrRecordingContext.h" |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 14 | #include "src/core/SkCompressedDataUtils.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 15 | #include "src/core/SkMipmap.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 17 | #include "src/gpu/gl/GrGLDefines.h" |
| 18 | #include "src/image/SkImage_Base.h" |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 19 | #include "src/image/SkImage_GpuBase.h" |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 20 | |
| 21 | #include "tools/Resources.h" |
| 22 | |
| 23 | //------------------------------------------------------------------------------------------------- |
| 24 | struct ImageInfo { |
| 25 | SkISize fDim; |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 26 | GrMipmapped fMipmapped; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 27 | SkImage::CompressionType fCompressionType; |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | * Get an int from a buffer |
| 32 | * This method is unsafe, the caller is responsible for performing a check |
| 33 | */ |
| 34 | static inline uint32_t get_uint(uint8_t* buffer, uint32_t i) { |
| 35 | uint32_t result; |
| 36 | memcpy(&result, &(buffer[i]), 4); |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | // This KTX loader is barely sufficient to load the specific files this GM requires. Use |
| 41 | // at your own peril. |
| 42 | static sk_sp<SkData> load_ktx(const char* filename, ImageInfo* imageInfo) { |
| 43 | SkFILEStream input(filename); |
| 44 | if (!input.isValid()) { |
| 45 | return nullptr; |
| 46 | } |
| 47 | |
| 48 | constexpr int kKTXIdentifierSize = 12; |
| 49 | constexpr int kKTXHeaderSize = kKTXIdentifierSize + 13 * sizeof(uint32_t); |
| 50 | uint8_t header[kKTXHeaderSize]; |
| 51 | |
| 52 | if (input.read(header, kKTXHeaderSize) != kKTXHeaderSize) { |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | static const uint8_t kExpectedIdentifier[kKTXIdentifierSize] = { |
| 57 | 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A |
| 58 | }; |
| 59 | |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 60 | if (0 != memcmp(header, kExpectedIdentifier, kKTXIdentifierSize)) { |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | uint32_t endianness = get_uint(header, 12); |
| 65 | if (endianness != 0x04030201) { |
| 66 | // TODO: need to swap rest of header and, if glTypeSize is > 1, all |
| 67 | // the texture data. |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
| 71 | uint32_t glType = get_uint(header, 16); |
| 72 | SkDEBUGCODE(uint32_t glTypeSize = get_uint(header, 20);) |
| 73 | uint32_t glFormat = get_uint(header, 24); |
| 74 | uint32_t glInternalFormat = get_uint(header, 28); |
| 75 | //uint32_t glBaseInternalFormat = get_uint(header, 32); |
| 76 | uint32_t pixelWidth = get_uint(header, 36); |
| 77 | uint32_t pixelHeight = get_uint(header, 40); |
| 78 | uint32_t pixelDepth = get_uint(header, 44); |
| 79 | //uint32_t numberOfArrayElements = get_uint(header, 48); |
| 80 | uint32_t numberOfFaces = get_uint(header, 52); |
| 81 | int numberOfMipmapLevels = get_uint(header, 56); |
| 82 | uint32_t bytesOfKeyValueData = get_uint(header, 60); |
| 83 | |
| 84 | if (glType != 0 || glFormat != 0) { // only care about compressed data for now |
| 85 | return nullptr; |
| 86 | } |
| 87 | SkASSERT(glTypeSize == 1); // required for compressed data |
| 88 | |
| 89 | // We only handle these four formats right now |
| 90 | switch (glInternalFormat) { |
| 91 | case GR_GL_COMPRESSED_ETC1_RGB8: |
| 92 | case GR_GL_COMPRESSED_RGB8_ETC2: |
| 93 | imageInfo->fCompressionType = SkImage::CompressionType::kETC2_RGB8_UNORM; |
| 94 | break; |
| 95 | case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT: |
| 96 | imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGB8_UNORM; |
| 97 | break; |
| 98 | case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: |
| 99 | imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGBA8_UNORM; |
| 100 | break; |
| 101 | default: |
| 102 | return nullptr; |
| 103 | } |
| 104 | |
| 105 | imageInfo->fDim.fWidth = pixelWidth; |
| 106 | imageInfo->fDim.fHeight = pixelHeight; |
| 107 | |
| 108 | if (pixelDepth != 0) { |
| 109 | return nullptr; // pixel depth is always zero for 2D textures |
| 110 | } |
| 111 | |
| 112 | if (numberOfFaces != 1) { |
| 113 | return nullptr; // we don't support cube maps right now |
| 114 | } |
| 115 | |
| 116 | if (numberOfMipmapLevels == 1) { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 117 | imageInfo->fMipmapped = GrMipmapped::kNo; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 118 | } else { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 119 | int numRequiredMipLevels = SkMipmap::ComputeLevelCount(pixelWidth, pixelHeight)+1; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 120 | if (numberOfMipmapLevels != numRequiredMipLevels) { |
| 121 | return nullptr; |
| 122 | } |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 123 | imageInfo->fMipmapped = GrMipmapped::kYes; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (bytesOfKeyValueData != 0) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | SkTArray<size_t> individualMipOffsets(numberOfMipmapLevels); |
| 131 | |
| 132 | size_t dataSize = SkCompressedDataSize(imageInfo->fCompressionType, |
| 133 | { (int) pixelWidth, (int) pixelHeight }, |
| 134 | &individualMipOffsets, |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 135 | imageInfo->fMipmapped == GrMipmapped::kYes); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 136 | SkASSERT(individualMipOffsets.size() == (size_t) numberOfMipmapLevels); |
| 137 | |
| 138 | sk_sp<SkData> data = SkData::MakeUninitialized(dataSize); |
| 139 | |
| 140 | uint8_t* dest = (uint8_t*) data->writable_data(); |
| 141 | |
| 142 | size_t offset = 0; |
| 143 | for (int i = 0; i < numberOfMipmapLevels; ++i) { |
| 144 | uint32_t imageSize; |
| 145 | |
| 146 | if (input.read(&imageSize, 4) != 4) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
| 150 | SkASSERT(offset + imageSize <= dataSize); |
| 151 | SkASSERT(offset == individualMipOffsets[i]); |
| 152 | |
| 153 | if (input.read(&dest[offset], imageSize) != imageSize) { |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | offset += imageSize; |
| 158 | } |
| 159 | |
| 160 | return data; |
| 161 | } |
| 162 | |
| 163 | //------------------------------------------------------------------------------------------------- |
| 164 | typedef uint32_t DWORD; |
| 165 | |
| 166 | // Values for the DDS_PIXELFORMAT 'dwFlags' field |
| 167 | constexpr unsigned int kDDPF_FOURCC = 0x4; |
| 168 | |
| 169 | struct DDS_PIXELFORMAT { |
| 170 | DWORD dwSize; |
| 171 | DWORD dwFlags; |
| 172 | DWORD dwFourCC; |
| 173 | DWORD dwRGBBitCount; |
| 174 | DWORD dwRBitMask; |
| 175 | DWORD dwGBitMask; |
| 176 | DWORD dwBBitMask; |
| 177 | DWORD dwABitMask; |
| 178 | }; |
| 179 | |
| 180 | // Values for the DDS_HEADER 'dwFlags' field |
| 181 | constexpr unsigned int kDDSD_CAPS = 0x1; // required |
| 182 | constexpr unsigned int kDDSD_HEIGHT = 0x2; // required |
| 183 | constexpr unsigned int kDDSD_WIDTH = 0x4; // required |
| 184 | constexpr unsigned int kDDSD_PITCH = 0x8; |
| 185 | constexpr unsigned int kDDSD_PIXELFORMAT = 0x001000; // required |
| 186 | constexpr unsigned int kDDSD_MIPMAPCOUNT = 0x020000; |
| 187 | constexpr unsigned int kDDSD_LINEARSIZE = 0x080000; |
| 188 | constexpr unsigned int kDDSD_DEPTH = 0x800000; |
| 189 | |
| 190 | constexpr unsigned int kDDSD_REQUIRED = kDDSD_CAPS | kDDSD_HEIGHT | kDDSD_WIDTH | kDDSD_PIXELFORMAT; |
| 191 | |
| 192 | typedef struct { |
| 193 | DWORD dwSize; |
| 194 | DWORD dwFlags; |
| 195 | DWORD dwHeight; |
| 196 | DWORD dwWidth; |
| 197 | DWORD dwPitchOrLinearSize; |
| 198 | DWORD dwDepth; |
| 199 | DWORD dwMipMapCount; |
| 200 | DWORD dwReserved1[11]; |
| 201 | DDS_PIXELFORMAT ddspf; |
| 202 | DWORD dwCaps; |
| 203 | DWORD dwCaps2; |
| 204 | DWORD dwCaps3; |
| 205 | DWORD dwCaps4; |
| 206 | DWORD dwReserved2; |
| 207 | } DDS_HEADER; |
| 208 | |
| 209 | // This DDS loader is barely sufficient to load the specific files this GM requires. Use |
| 210 | // at your own peril. |
| 211 | static sk_sp<SkData> load_dds(const char* filename, ImageInfo* imageInfo) { |
| 212 | SkFILEStream input(filename); |
| 213 | if (!input.isValid()) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | constexpr uint32_t kMagic = 0x20534444; |
| 218 | uint32_t magic; |
| 219 | |
| 220 | if (input.read(&magic, 4) != 4) { |
| 221 | return nullptr; |
| 222 | } |
| 223 | |
| 224 | if (magic != kMagic) { |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
| 228 | constexpr size_t kDDSHeaderSize = sizeof(DDS_HEADER); |
| 229 | static_assert(kDDSHeaderSize == 124); |
| 230 | constexpr size_t kDDSPixelFormatSize = sizeof(DDS_PIXELFORMAT); |
| 231 | static_assert(kDDSPixelFormatSize == 32); |
| 232 | |
| 233 | DDS_HEADER header; |
| 234 | |
| 235 | if (input.read(&header, kDDSHeaderSize) != kDDSHeaderSize) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | |
| 239 | if (header.dwSize != kDDSHeaderSize || |
| 240 | header.ddspf.dwSize != kDDSPixelFormatSize) { |
| 241 | return nullptr; |
| 242 | } |
| 243 | |
| 244 | if ((header.dwFlags & kDDSD_REQUIRED) != kDDSD_REQUIRED) { |
| 245 | return nullptr; |
| 246 | } |
| 247 | |
| 248 | if (header.dwFlags & (kDDSD_PITCH | kDDSD_LINEARSIZE | kDDSD_DEPTH)) { |
| 249 | // TODO: support these features |
| 250 | } |
| 251 | |
| 252 | imageInfo->fDim.fWidth = header.dwWidth; |
| 253 | imageInfo->fDim.fHeight = header.dwHeight; |
| 254 | |
| 255 | int numberOfMipmapLevels = 1; |
| 256 | if (header.dwFlags & kDDSD_MIPMAPCOUNT) { |
| 257 | if (header.dwMipMapCount == 1) { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 258 | imageInfo->fMipmapped = GrMipmapped::kNo; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 259 | } else { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 260 | int numRequiredLevels = SkMipmap::ComputeLevelCount(header.dwWidth, header.dwHeight)+1; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 261 | if (header.dwMipMapCount != (unsigned) numRequiredLevels) { |
| 262 | return nullptr; |
| 263 | } |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 264 | imageInfo->fMipmapped = GrMipmapped::kYes; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 265 | numberOfMipmapLevels = numRequiredLevels; |
| 266 | } |
| 267 | } else { |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 268 | imageInfo->fMipmapped = GrMipmapped::kNo; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (!(header.ddspf.dwFlags & kDDPF_FOURCC)) { |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
| 275 | // We only handle these one format right now |
| 276 | switch (header.ddspf.dwFourCC) { |
| 277 | case 0x31545844: // DXT1 |
| 278 | imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGB8_UNORM; |
| 279 | break; |
| 280 | default: |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
| 284 | SkTArray<size_t> individualMipOffsets(numberOfMipmapLevels); |
| 285 | |
| 286 | size_t dataSize = SkCompressedDataSize(imageInfo->fCompressionType, |
| 287 | { (int) header.dwWidth, (int) header.dwHeight }, |
| 288 | &individualMipOffsets, |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 289 | imageInfo->fMipmapped == GrMipmapped::kYes); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 290 | SkASSERT(individualMipOffsets.size() == (size_t) numberOfMipmapLevels); |
| 291 | |
| 292 | sk_sp<SkData> data = SkData::MakeUninitialized(dataSize); |
| 293 | |
| 294 | uint8_t* dest = (uint8_t*) data->writable_data(); |
| 295 | |
| 296 | size_t amountRead = input.read(dest, dataSize); |
| 297 | if (amountRead != dataSize) { |
| 298 | return nullptr; |
| 299 | } |
| 300 | |
| 301 | return data; |
| 302 | } |
| 303 | |
| 304 | //------------------------------------------------------------------------------------------------- |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 305 | static sk_sp<SkImage> data_to_img(GrDirectContext *direct, sk_sp<SkData> data, |
| 306 | const ImageInfo& info) { |
| 307 | if (direct) { |
| 308 | return SkImage::MakeTextureFromCompressed(direct, std::move(data), |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 309 | info.fDim.fWidth, |
| 310 | info.fDim.fHeight, |
| 311 | info.fCompressionType, |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 312 | info.fMipmapped); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 313 | } else { |
| 314 | return SkImage::MakeRasterFromCompressed(std::move(data), |
| 315 | info.fDim.fWidth, |
| 316 | info.fDim.fHeight, |
| 317 | info.fCompressionType); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | namespace skiagm { |
| 322 | |
| 323 | // This GM exercises our handling of some of the more exotic formats using externally |
| 324 | // generated content. Right now it only tests ETC1 and BC1. |
| 325 | class ExoticFormatsGM : public GM { |
| 326 | public: |
| 327 | ExoticFormatsGM() { |
Robert Phillips | eb77a76 | 2020-02-03 11:43:20 -0500 | [diff] [blame] | 328 | this->setBGColor(SK_ColorBLACK); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | protected: |
| 332 | SkString onShortName() override { |
| 333 | return SkString("exoticformats"); |
| 334 | } |
| 335 | |
| 336 | SkISize onISize() override { |
Robert Phillips | eb77a76 | 2020-02-03 11:43:20 -0500 | [diff] [blame] | 337 | return SkISize::Make(2*kImgWidthHeight + 3 * kPad, kImgWidthHeight + 2 * kPad); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 338 | } |
| 339 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 340 | bool loadImages(GrDirectContext *direct) { |
| 341 | SkASSERT(!fETC1Image && !fBC1Image); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 342 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 343 | { |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 344 | ImageInfo info; |
| 345 | sk_sp<SkData> data = load_ktx(GetResourcePath("images/flower-etc1.ktx").c_str(), &info); |
| 346 | if (data) { |
| 347 | SkASSERT(info.fDim.equals(kImgWidthHeight, kImgWidthHeight)); |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 348 | SkASSERT(info.fMipmapped == GrMipmapped::kNo); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 349 | SkASSERT(info.fCompressionType == SkImage::CompressionType::kETC2_RGB8_UNORM); |
| 350 | |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 351 | fETC1Image = data_to_img(direct, std::move(data), info); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 352 | } else { |
| 353 | SkDebugf("failed to load flower-etc1.ktx\n"); |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 354 | return false; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 358 | { |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 359 | ImageInfo info; |
| 360 | sk_sp<SkData> data = load_dds(GetResourcePath("images/flower-bc1.dds").c_str(), &info); |
| 361 | if (data) { |
| 362 | SkASSERT(info.fDim.equals(kImgWidthHeight, kImgWidthHeight)); |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 363 | SkASSERT(info.fMipmapped == GrMipmapped::kNo); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 364 | SkASSERT(info.fCompressionType == SkImage::CompressionType::kBC1_RGB8_UNORM); |
| 365 | |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 366 | fBC1Image = data_to_img(direct, std::move(data), info); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 367 | } else { |
| 368 | SkDebugf("failed to load flower-bc1.dds\n"); |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 369 | return false; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 373 | return true; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 376 | void drawImage(SkCanvas* canvas, SkImage* image, int x, int y) { |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 377 | if (!image) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | bool isCompressed = false; |
| 382 | if (image->isTextureBacked()) { |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 383 | GrRecordingContext* rContext = ((SkImage_GpuBase*) image)->context(); |
| 384 | const GrCaps* caps = rContext->priv().caps(); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 385 | |
| 386 | GrTextureProxy* proxy = as_IB(image)->peekProxy(); |
| 387 | isCompressed = caps->isFormatCompressed(proxy->backendFormat()); |
| 388 | } |
| 389 | |
| 390 | canvas->drawImage(image, x, y); |
| 391 | |
| 392 | if (!isCompressed) { |
| 393 | // Make it obvious which drawImages used decompressed images |
| 394 | SkRect r = SkRect::MakeXYWH(x, y, kImgWidthHeight, kImgWidthHeight); |
| 395 | SkPaint paint; |
| 396 | paint.setColor(SK_ColorRED); |
| 397 | paint.setStyle(SkPaint::kStroke_Style); |
Robert Phillips | eb77a76 | 2020-02-03 11:43:20 -0500 | [diff] [blame] | 398 | paint.setStrokeWidth(2.0f); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 399 | canvas->drawRect(r, paint); |
| 400 | } |
| 401 | } |
| 402 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 403 | DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override { |
| 404 | if (dContext && dContext->abandoned()) { |
| 405 | // This isn't a GpuGM so a null 'context' is okay but an abandoned context |
| 406 | // if forbidden. |
| 407 | return DrawResult::kSkip; |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 408 | } |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 409 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 410 | if (!this->loadImages(dContext)) { |
| 411 | *errorMsg = "Failed to create images."; |
| 412 | return DrawResult::kFail; |
| 413 | } |
Adlai Holler | 4caa935 | 2020-07-16 10:58:58 -0400 | [diff] [blame] | 414 | |
Robert Phillips | a84caa3 | 2020-07-28 09:57:26 -0400 | [diff] [blame] | 415 | return DrawResult::kOk; |
| 416 | } |
| 417 | |
| 418 | void onGpuTeardown() override { |
| 419 | fETC1Image = nullptr; |
| 420 | fBC1Image = nullptr; |
| 421 | } |
| 422 | |
| 423 | void onDraw(SkCanvas* canvas) override { |
| 424 | SkASSERT(fETC1Image && fBC1Image); |
| 425 | |
| 426 | this->drawImage(canvas, fETC1Image.get(), kPad, kPad); |
| 427 | this->drawImage(canvas, fBC1Image.get(), kImgWidthHeight + 2 * kPad, kPad); |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | private: |
| 431 | static const int kImgWidthHeight = 128; |
Robert Phillips | eb77a76 | 2020-02-03 11:43:20 -0500 | [diff] [blame] | 432 | static const int kPad = 4; |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 433 | |
| 434 | sk_sp<SkImage> fETC1Image; |
| 435 | sk_sp<SkImage> fBC1Image; |
| 436 | |
| 437 | typedef GM INHERITED; |
| 438 | }; |
| 439 | |
| 440 | ////////////////////////////////////////////////////////////////////////////// |
| 441 | |
| 442 | DEF_GM(return new ExoticFormatsGM;) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 443 | } // namespace skiagm |