| 1 | /* |
| 2 | * Copyright 2013 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 | #ifndef GrTypesPriv_DEFINED |
| 9 | #define GrTypesPriv_DEFINED |
| 10 | |
| 11 | #include <chrono> |
| 12 | #include "include/core/SkImage.h" |
| 13 | #include "include/core/SkImageInfo.h" |
| 14 | #include "include/core/SkPath.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/gpu/GrTypes.h" |
| 17 | #include "include/private/GrSharedEnums.h" |
| 18 | #include "include/private/SkImageInfoPriv.h" |
| 19 | #include "include/private/SkWeakRefCnt.h" |
| 20 | |
| 21 | class GrBackendFormat; |
| 22 | class GrCaps; |
| 23 | |
| 24 | // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might |
| 25 | // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently |
| 26 | // used for idle resource purging so it shouldn't cause a correctness problem. |
| 27 | #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000) |
| 28 | using GrStdSteadyClock = std::chrono::monotonic_clock; |
| 29 | #else |
| 30 | using GrStdSteadyClock = std::chrono::steady_clock; |
| 31 | #endif |
| 32 | |
| 33 | /** |
| 34 | * divide, rounding up |
| 35 | */ |
| 36 | |
| 37 | static inline constexpr size_t GrSizeDivRoundUp(size_t x, size_t y) { return (x + (y - 1)) / y; } |
| 38 | |
| 39 | /** |
| 40 | * align up to a power of 2 |
| 41 | */ |
| 42 | static inline constexpr size_t GrAlignTo(size_t x, size_t alignment) { |
| 43 | SkASSERT(alignment && SkIsPow2(alignment)); |
| 44 | return (x + alignment - 1) & ~(alignment - 1); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Geometric primitives used for drawing. |
| 49 | */ |
| 50 | enum class GrPrimitiveType : uint8_t { |
| 51 | kTriangles, |
| 52 | kTriangleStrip, |
| 53 | kPoints, |
| 54 | kLines, // 1 pix wide only |
| 55 | kLineStrip, // 1 pix wide only |
| 56 | kPatches, |
| 57 | kPath |
| 58 | }; |
| 59 | static constexpr int kNumGrPrimitiveTypes = (int)GrPrimitiveType::kPath + 1; |
| 60 | |
| 61 | static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) { |
| 62 | return GrPrimitiveType::kLines == type || GrPrimitiveType::kLineStrip == type; |
| 63 | } |
| 64 | |
| 65 | static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) { |
| 66 | return GrPrimitiveType::kTriangles == type || GrPrimitiveType::kTriangleStrip == type; |
| 67 | } |
| 68 | |
| 69 | enum class GrPrimitiveRestart : bool { |
| 70 | kNo = false, |
| 71 | kYes = true |
| 72 | }; |
| 73 | |
| 74 | struct GrDrawIndirectCommand { |
| 75 | uint32_t fVertexCount; |
| 76 | uint32_t fInstanceCount; |
| 77 | uint32_t fBaseVertex; |
| 78 | uint32_t fBaseInstance; |
| 79 | }; |
| 80 | |
| 81 | static_assert(sizeof(GrDrawIndirectCommand) == 16, "GrDrawIndirectCommand must be tightly packed" ); |
| 82 | |
| 83 | struct GrDrawIndexedIndirectCommand { |
| 84 | uint32_t fIndexCount; |
| 85 | uint32_t fInstanceCount; |
| 86 | uint32_t fBaseIndex; |
| 87 | int32_t fBaseVertex; |
| 88 | uint32_t fBaseInstance; |
| 89 | }; |
| 90 | |
| 91 | static_assert(sizeof(GrDrawIndexedIndirectCommand) == 20, |
| 92 | "GrDrawIndexedIndirectCommand must be tightly packed" ); |
| 93 | |
| 94 | /** |
| 95 | * Should a created surface be texturable? |
| 96 | */ |
| 97 | enum class GrTexturable : bool { |
| 98 | kNo = false, |
| 99 | kYes = true |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Formats for masks, used by the font cache. Important that these are 0-based. |
| 104 | */ |
| 105 | enum GrMaskFormat { |
| 106 | kA8_GrMaskFormat, //!< 1-byte per pixel |
| 107 | kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage |
| 108 | kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format |
| 109 | |
| 110 | kLast_GrMaskFormat = kARGB_GrMaskFormat |
| 111 | }; |
| 112 | static const int kMaskFormatCount = kLast_GrMaskFormat + 1; |
| 113 | |
| 114 | /** |
| 115 | * Return the number of bytes-per-pixel for the specified mask format. |
| 116 | */ |
| 117 | static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) { |
| 118 | SkASSERT(format < kMaskFormatCount); |
| 119 | // kA8 (0) -> 1 |
| 120 | // kA565 (1) -> 2 |
| 121 | // kARGB (2) -> 4 |
| 122 | static const int sBytesPerPixel[] = {1, 2, 4}; |
| 123 | static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch" ); |
| 124 | static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency" ); |
| 125 | static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency" ); |
| 126 | static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency" ); |
| 127 | |
| 128 | return sBytesPerPixel[(int)format]; |
| 129 | } |
| 130 | |
| 131 | /** Ownership rules for external GPU resources imported into Skia. */ |
| 132 | enum GrWrapOwnership { |
| 133 | /** Skia will assume the client will keep the resource alive and Skia will not free it. */ |
| 134 | kBorrow_GrWrapOwnership, |
| 135 | |
| 136 | /** Skia will assume ownership of the resource and free it. */ |
| 137 | kAdopt_GrWrapOwnership, |
| 138 | }; |
| 139 | |
| 140 | enum class GrWrapCacheable : bool { |
| 141 | /** |
| 142 | * The wrapped resource will be removed from the cache as soon as it becomes purgeable. It may |
| 143 | * still be assigned and found by a unique key, but the presence of the key will not be used to |
| 144 | * keep the resource alive when it has no references. |
| 145 | */ |
| 146 | kNo = false, |
| 147 | /** |
| 148 | * The wrapped resource is allowed to remain in the GrResourceCache when it has no references |
| 149 | * but has a unique key. Such resources should only be given unique keys when it is known that |
| 150 | * the key will eventually be removed from the resource or invalidated via the message bus. |
| 151 | */ |
| 152 | kYes = true |
| 153 | }; |
| 154 | |
| 155 | enum class GrBudgetedType : uint8_t { |
| 156 | /** The resource is budgeted and is subject to purging under budget pressure. */ |
| 157 | kBudgeted, |
| 158 | /** |
| 159 | * The resource is unbudgeted and is purged as soon as it has no refs regardless of whether |
| 160 | * it has a unique or scratch key. |
| 161 | */ |
| 162 | kUnbudgetedUncacheable, |
| 163 | /** |
| 164 | * The resource is unbudgeted and is allowed to remain in the cache with no refs if it |
| 165 | * has a unique key. Scratch keys are ignored. |
| 166 | */ |
| 167 | kUnbudgetedCacheable, |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Clips are composed from these objects. |
| 172 | */ |
| 173 | enum GrClipType { |
| 174 | kRect_ClipType, |
| 175 | kPath_ClipType |
| 176 | }; |
| 177 | |
| 178 | enum class GrScissorTest : bool { |
| 179 | kDisabled = false, |
| 180 | kEnabled = true |
| 181 | }; |
| 182 | |
| 183 | struct GrMipLevel { |
| 184 | const void* fPixels = nullptr; |
| 185 | size_t fRowBytes = 0; |
| 186 | }; |
| 187 | |
| 188 | /** |
| 189 | * This enum is used to specify the load operation to be used when an GrOpsTask/GrOpsRenderPass |
| 190 | * begins execution. |
| 191 | */ |
| 192 | enum class GrLoadOp { |
| 193 | kLoad, |
| 194 | kClear, |
| 195 | kDiscard, |
| 196 | }; |
| 197 | |
| 198 | /** |
| 199 | * This enum is used to specify the store operation to be used when an GrOpsTask/GrOpsRenderPass |
| 200 | * ends execution. |
| 201 | */ |
| 202 | enum class GrStoreOp { |
| 203 | kStore, |
| 204 | kDiscard, |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * Used to control antialiasing in draw calls. |
| 209 | */ |
| 210 | enum class GrAA : bool { |
| 211 | kNo = false, |
| 212 | kYes = true |
| 213 | }; |
| 214 | |
| 215 | enum class GrFillRule : bool { |
| 216 | kNonzero, |
| 217 | kEvenOdd |
| 218 | }; |
| 219 | |
| 220 | inline GrFillRule GrFillRuleForSkPath(const SkPath& path) { |
| 221 | switch (path.getFillType()) { |
| 222 | case SkPathFillType::kWinding: |
| 223 | case SkPathFillType::kInverseWinding: |
| 224 | return GrFillRule::kNonzero; |
| 225 | case SkPathFillType::kEvenOdd: |
| 226 | case SkPathFillType::kInverseEvenOdd: |
| 227 | return GrFillRule::kEvenOdd; |
| 228 | } |
| 229 | SkUNREACHABLE; |
| 230 | } |
| 231 | |
| 232 | /** This enum indicates the type of antialiasing to be performed. */ |
| 233 | enum class GrAAType : unsigned { |
| 234 | /** No antialiasing */ |
| 235 | kNone, |
| 236 | /** Use fragment shader code or mixed samples to blend with a fractional pixel coverage. */ |
| 237 | kCoverage, |
| 238 | /** Use normal MSAA. */ |
| 239 | kMSAA, |
| 240 | |
| 241 | kLast = kMSAA |
| 242 | }; |
| 243 | static const int kGrAATypeCount = static_cast<int>(GrAAType::kLast) + 1; |
| 244 | |
| 245 | static constexpr bool GrAATypeIsHW(GrAAType type) { |
| 246 | switch (type) { |
| 247 | case GrAAType::kNone: |
| 248 | return false; |
| 249 | case GrAAType::kCoverage: |
| 250 | return false; |
| 251 | case GrAAType::kMSAA: |
| 252 | return true; |
| 253 | } |
| 254 | SkUNREACHABLE; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Some pixel configs are inherently clamped to [0,1], some are allowed to go outside that range, |
| 259 | * and some are FP but manually clamped in the XP. |
| 260 | */ |
| 261 | enum class GrClampType { |
| 262 | kAuto, // Normalized, fixed-point configs |
| 263 | kManual, // Clamped FP configs |
| 264 | kNone, // Normal (unclamped) FP configs |
| 265 | }; |
| 266 | |
| 267 | /** |
| 268 | * A number of rectangle/quadrilateral drawing APIs can control anti-aliasing on a per edge basis. |
| 269 | * These masks specify which edges are AA'ed. The intent for this is to support tiling with seamless |
| 270 | * boundaries, where the inner edges are non-AA and the outer edges are AA. Regular draws (where AA |
| 271 | * is specified by GrAA) is almost equivalent to kNone or kAll, with the exception of how MSAA is |
| 272 | * handled. |
| 273 | * |
| 274 | * When tiling and there is MSAA, mixed edge rectangles are processed with MSAA, so in order for the |
| 275 | * tiled edges to remain seamless, inner tiles with kNone must also be processed with MSAA. In |
| 276 | * regular drawing, however, kNone should disable MSAA (if it's supported) to match the expected |
| 277 | * appearance. |
| 278 | * |
| 279 | * Therefore, APIs that use per-edge AA flags also take a GrAA value so that they can differentiate |
| 280 | * between the regular and tiling use case behaviors. Tiling operations should always pass |
| 281 | * GrAA::kYes while regular options should pass GrAA based on the SkPaint's anti-alias state. |
| 282 | * |
| 283 | * These values are identical to SkCanvas::QuadAAFlags. |
| 284 | */ |
| 285 | enum class GrQuadAAFlags { |
| 286 | kLeft = 0b0001, |
| 287 | kTop = 0b0010, |
| 288 | kRight = 0b0100, |
| 289 | kBottom = 0b1000, |
| 290 | |
| 291 | kNone = 0b0000, |
| 292 | kAll = 0b1111, |
| 293 | }; |
| 294 | |
| 295 | GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags) |
| 296 | |
| 297 | static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) { |
| 298 | return static_cast<GrQuadAAFlags>(flags); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Types of shader-language-specific boxed variables we can create. |
| 303 | */ |
| 304 | enum GrSLType { |
| 305 | kVoid_GrSLType, |
| 306 | kBool_GrSLType, |
| 307 | kByte_GrSLType, |
| 308 | kByte2_GrSLType, |
| 309 | kByte3_GrSLType, |
| 310 | kByte4_GrSLType, |
| 311 | kUByte_GrSLType, |
| 312 | kUByte2_GrSLType, |
| 313 | kUByte3_GrSLType, |
| 314 | kUByte4_GrSLType, |
| 315 | kShort_GrSLType, |
| 316 | kShort2_GrSLType, |
| 317 | kShort3_GrSLType, |
| 318 | kShort4_GrSLType, |
| 319 | kUShort_GrSLType, |
| 320 | kUShort2_GrSLType, |
| 321 | kUShort3_GrSLType, |
| 322 | kUShort4_GrSLType, |
| 323 | kFloat_GrSLType, |
| 324 | kFloat2_GrSLType, |
| 325 | kFloat3_GrSLType, |
| 326 | kFloat4_GrSLType, |
| 327 | kFloat2x2_GrSLType, |
| 328 | kFloat3x3_GrSLType, |
| 329 | kFloat4x4_GrSLType, |
| 330 | kHalf_GrSLType, |
| 331 | kHalf2_GrSLType, |
| 332 | kHalf3_GrSLType, |
| 333 | kHalf4_GrSLType, |
| 334 | kHalf2x2_GrSLType, |
| 335 | kHalf3x3_GrSLType, |
| 336 | kHalf4x4_GrSLType, |
| 337 | kInt_GrSLType, |
| 338 | kInt2_GrSLType, |
| 339 | kInt3_GrSLType, |
| 340 | kInt4_GrSLType, |
| 341 | kUint_GrSLType, |
| 342 | kUint2_GrSLType, |
| 343 | kTexture2DSampler_GrSLType, |
| 344 | kTextureExternalSampler_GrSLType, |
| 345 | kTexture2DRectSampler_GrSLType, |
| 346 | kTexture2D_GrSLType, |
| 347 | kSampler_GrSLType, |
| 348 | |
| 349 | kLast_GrSLType = kSampler_GrSLType |
| 350 | }; |
| 351 | static const int kGrSLTypeCount = kLast_GrSLType + 1; |
| 352 | |
| 353 | /** |
| 354 | * The type of texture. Backends other than GL currently only use the 2D value but the type must |
| 355 | * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability, |
| 356 | * and sampling parameters are legal for proxies that will be instantiated with wrapped textures. |
| 357 | */ |
| 358 | enum class GrTextureType { |
| 359 | kNone, |
| 360 | k2D, |
| 361 | /* Rectangle uses unnormalized texture coordinates. */ |
| 362 | kRectangle, |
| 363 | kExternal |
| 364 | }; |
| 365 | |
| 366 | enum GrShaderType { |
| 367 | kVertex_GrShaderType, |
| 368 | kGeometry_GrShaderType, |
| 369 | kFragment_GrShaderType, |
| 370 | |
| 371 | kLastkFragment_GrShaderType = kFragment_GrShaderType |
| 372 | }; |
| 373 | static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1; |
| 374 | |
| 375 | enum GrShaderFlags { |
| 376 | kNone_GrShaderFlags = 0, |
| 377 | kVertex_GrShaderFlag = 1, |
| 378 | kTessControl_GrShaderFlag = 1 << 2, |
| 379 | kTessEvaluation_GrShaderFlag = 1 << 2, |
| 380 | kGeometry_GrShaderFlag = 1 << 3, |
| 381 | kFragment_GrShaderFlag = 1 << 4 |
| 382 | }; |
| 383 | GR_MAKE_BITFIELD_OPS(GrShaderFlags) |
| 384 | |
| 385 | /** Is the shading language type float (including vectors/matrices)? */ |
| 386 | static constexpr bool GrSLTypeIsFloatType(GrSLType type) { |
| 387 | switch (type) { |
| 388 | case kFloat_GrSLType: |
| 389 | case kFloat2_GrSLType: |
| 390 | case kFloat3_GrSLType: |
| 391 | case kFloat4_GrSLType: |
| 392 | case kFloat2x2_GrSLType: |
| 393 | case kFloat3x3_GrSLType: |
| 394 | case kFloat4x4_GrSLType: |
| 395 | case kHalf_GrSLType: |
| 396 | case kHalf2_GrSLType: |
| 397 | case kHalf3_GrSLType: |
| 398 | case kHalf4_GrSLType: |
| 399 | case kHalf2x2_GrSLType: |
| 400 | case kHalf3x3_GrSLType: |
| 401 | case kHalf4x4_GrSLType: |
| 402 | return true; |
| 403 | |
| 404 | case kVoid_GrSLType: |
| 405 | case kTexture2DSampler_GrSLType: |
| 406 | case kTextureExternalSampler_GrSLType: |
| 407 | case kTexture2DRectSampler_GrSLType: |
| 408 | case kBool_GrSLType: |
| 409 | case kByte_GrSLType: |
| 410 | case kByte2_GrSLType: |
| 411 | case kByte3_GrSLType: |
| 412 | case kByte4_GrSLType: |
| 413 | case kUByte_GrSLType: |
| 414 | case kUByte2_GrSLType: |
| 415 | case kUByte3_GrSLType: |
| 416 | case kUByte4_GrSLType: |
| 417 | case kShort_GrSLType: |
| 418 | case kShort2_GrSLType: |
| 419 | case kShort3_GrSLType: |
| 420 | case kShort4_GrSLType: |
| 421 | case kUShort_GrSLType: |
| 422 | case kUShort2_GrSLType: |
| 423 | case kUShort3_GrSLType: |
| 424 | case kUShort4_GrSLType: |
| 425 | case kInt_GrSLType: |
| 426 | case kInt2_GrSLType: |
| 427 | case kInt3_GrSLType: |
| 428 | case kInt4_GrSLType: |
| 429 | case kUint_GrSLType: |
| 430 | case kUint2_GrSLType: |
| 431 | case kTexture2D_GrSLType: |
| 432 | case kSampler_GrSLType: |
| 433 | return false; |
| 434 | } |
| 435 | SkUNREACHABLE; |
| 436 | } |
| 437 | |
| 438 | /** If the type represents a single value or vector return the vector length, else -1. */ |
| 439 | static constexpr int GrSLTypeVecLength(GrSLType type) { |
| 440 | switch (type) { |
| 441 | case kFloat_GrSLType: |
| 442 | case kHalf_GrSLType: |
| 443 | case kBool_GrSLType: |
| 444 | case kByte_GrSLType: |
| 445 | case kUByte_GrSLType: |
| 446 | case kShort_GrSLType: |
| 447 | case kUShort_GrSLType: |
| 448 | case kInt_GrSLType: |
| 449 | case kUint_GrSLType: |
| 450 | return 1; |
| 451 | |
| 452 | case kFloat2_GrSLType: |
| 453 | case kHalf2_GrSLType: |
| 454 | case kByte2_GrSLType: |
| 455 | case kUByte2_GrSLType: |
| 456 | case kShort2_GrSLType: |
| 457 | case kUShort2_GrSLType: |
| 458 | case kInt2_GrSLType: |
| 459 | case kUint2_GrSLType: |
| 460 | return 2; |
| 461 | |
| 462 | case kFloat3_GrSLType: |
| 463 | case kHalf3_GrSLType: |
| 464 | case kByte3_GrSLType: |
| 465 | case kUByte3_GrSLType: |
| 466 | case kShort3_GrSLType: |
| 467 | case kUShort3_GrSLType: |
| 468 | case kInt3_GrSLType: |
| 469 | return 3; |
| 470 | |
| 471 | case kFloat4_GrSLType: |
| 472 | case kHalf4_GrSLType: |
| 473 | case kByte4_GrSLType: |
| 474 | case kUByte4_GrSLType: |
| 475 | case kShort4_GrSLType: |
| 476 | case kUShort4_GrSLType: |
| 477 | case kInt4_GrSLType: |
| 478 | return 4; |
| 479 | |
| 480 | case kFloat2x2_GrSLType: |
| 481 | case kFloat3x3_GrSLType: |
| 482 | case kFloat4x4_GrSLType: |
| 483 | case kHalf2x2_GrSLType: |
| 484 | case kHalf3x3_GrSLType: |
| 485 | case kHalf4x4_GrSLType: |
| 486 | case kVoid_GrSLType: |
| 487 | case kTexture2DSampler_GrSLType: |
| 488 | case kTextureExternalSampler_GrSLType: |
| 489 | case kTexture2DRectSampler_GrSLType: |
| 490 | case kTexture2D_GrSLType: |
| 491 | case kSampler_GrSLType: |
| 492 | return -1; |
| 493 | } |
| 494 | SkUNREACHABLE; |
| 495 | } |
| 496 | |
| 497 | static inline GrSLType GrSLCombinedSamplerTypeForTextureType(GrTextureType type) { |
| 498 | switch (type) { |
| 499 | case GrTextureType::k2D: |
| 500 | return kTexture2DSampler_GrSLType; |
| 501 | case GrTextureType::kRectangle: |
| 502 | return kTexture2DRectSampler_GrSLType; |
| 503 | case GrTextureType::kExternal: |
| 504 | return kTextureExternalSampler_GrSLType; |
| 505 | default: |
| 506 | SK_ABORT("Unexpected texture type" ); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** Rectangle and external textures only support the clamp wrap mode and do not support |
| 511 | * MIP maps. |
| 512 | */ |
| 513 | static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) { |
| 514 | switch (type) { |
| 515 | case GrTextureType::k2D: |
| 516 | return false; |
| 517 | case GrTextureType::kRectangle: |
| 518 | return true; |
| 519 | case GrTextureType::kExternal: |
| 520 | return true; |
| 521 | default: |
| 522 | SK_ABORT("Unexpected texture type" ); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static constexpr bool GrSLTypeIsCombinedSamplerType(GrSLType type) { |
| 527 | switch (type) { |
| 528 | case kTexture2DSampler_GrSLType: |
| 529 | case kTextureExternalSampler_GrSLType: |
| 530 | case kTexture2DRectSampler_GrSLType: |
| 531 | return true; |
| 532 | |
| 533 | case kVoid_GrSLType: |
| 534 | case kFloat_GrSLType: |
| 535 | case kFloat2_GrSLType: |
| 536 | case kFloat3_GrSLType: |
| 537 | case kFloat4_GrSLType: |
| 538 | case kFloat2x2_GrSLType: |
| 539 | case kFloat3x3_GrSLType: |
| 540 | case kFloat4x4_GrSLType: |
| 541 | case kHalf_GrSLType: |
| 542 | case kHalf2_GrSLType: |
| 543 | case kHalf3_GrSLType: |
| 544 | case kHalf4_GrSLType: |
| 545 | case kHalf2x2_GrSLType: |
| 546 | case kHalf3x3_GrSLType: |
| 547 | case kHalf4x4_GrSLType: |
| 548 | case kInt_GrSLType: |
| 549 | case kInt2_GrSLType: |
| 550 | case kInt3_GrSLType: |
| 551 | case kInt4_GrSLType: |
| 552 | case kUint_GrSLType: |
| 553 | case kUint2_GrSLType: |
| 554 | case kBool_GrSLType: |
| 555 | case kByte_GrSLType: |
| 556 | case kByte2_GrSLType: |
| 557 | case kByte3_GrSLType: |
| 558 | case kByte4_GrSLType: |
| 559 | case kUByte_GrSLType: |
| 560 | case kUByte2_GrSLType: |
| 561 | case kUByte3_GrSLType: |
| 562 | case kUByte4_GrSLType: |
| 563 | case kShort_GrSLType: |
| 564 | case kShort2_GrSLType: |
| 565 | case kShort3_GrSLType: |
| 566 | case kShort4_GrSLType: |
| 567 | case kUShort_GrSLType: |
| 568 | case kUShort2_GrSLType: |
| 569 | case kUShort3_GrSLType: |
| 570 | case kUShort4_GrSLType: |
| 571 | case kTexture2D_GrSLType: |
| 572 | case kSampler_GrSLType: |
| 573 | return false; |
| 574 | } |
| 575 | SkUNREACHABLE; |
| 576 | } |
| 577 | |
| 578 | ////////////////////////////////////////////////////////////////////////////// |
| 579 | |
| 580 | /** |
| 581 | * Types used to describe format of vertices in arrays. |
| 582 | */ |
| 583 | enum GrVertexAttribType { |
| 584 | kFloat_GrVertexAttribType = 0, |
| 585 | kFloat2_GrVertexAttribType, |
| 586 | kFloat3_GrVertexAttribType, |
| 587 | kFloat4_GrVertexAttribType, |
| 588 | kHalf_GrVertexAttribType, |
| 589 | kHalf2_GrVertexAttribType, |
| 590 | kHalf4_GrVertexAttribType, |
| 591 | |
| 592 | kInt2_GrVertexAttribType, // vector of 2 32-bit ints |
| 593 | kInt3_GrVertexAttribType, // vector of 3 32-bit ints |
| 594 | kInt4_GrVertexAttribType, // vector of 4 32-bit ints |
| 595 | |
| 596 | |
| 597 | kByte_GrVertexAttribType, // signed byte |
| 598 | kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes |
| 599 | kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes |
| 600 | kUByte_GrVertexAttribType, // unsigned byte |
| 601 | kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes |
| 602 | kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes |
| 603 | |
| 604 | kUByte_norm_GrVertexAttribType, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f. |
| 605 | kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f, |
| 606 | // 255 -> 1.0f. |
| 607 | |
| 608 | kShort2_GrVertexAttribType, // vector of 2 16-bit shorts. |
| 609 | kShort4_GrVertexAttribType, // vector of 4 16-bit shorts. |
| 610 | |
| 611 | kUShort2_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535. |
| 612 | kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f. |
| 613 | |
| 614 | kInt_GrVertexAttribType, |
| 615 | kUint_GrVertexAttribType, |
| 616 | |
| 617 | kUShort_norm_GrVertexAttribType, |
| 618 | |
| 619 | kUShort4_norm_GrVertexAttribType, // vector of 4 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f. |
| 620 | |
| 621 | kLast_GrVertexAttribType = kUShort4_norm_GrVertexAttribType |
| 622 | }; |
| 623 | static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1; |
| 624 | |
| 625 | ////////////////////////////////////////////////////////////////////////////// |
| 626 | |
| 627 | static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1; |
| 628 | |
| 629 | static constexpr bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) { |
| 630 | return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType); |
| 631 | } |
| 632 | |
| 633 | static constexpr bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) { |
| 634 | return (GrClipEdgeType::kInverseFillAA == edgeType || |
| 635 | GrClipEdgeType::kInverseFillBW == edgeType); |
| 636 | } |
| 637 | |
| 638 | static constexpr bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) { |
| 639 | return (GrClipEdgeType::kFillBW != edgeType && |
| 640 | GrClipEdgeType::kInverseFillBW != edgeType); |
| 641 | } |
| 642 | |
| 643 | static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) { |
| 644 | switch (edgeType) { |
| 645 | case GrClipEdgeType::kFillBW: |
| 646 | return GrClipEdgeType::kInverseFillBW; |
| 647 | case GrClipEdgeType::kFillAA: |
| 648 | return GrClipEdgeType::kInverseFillAA; |
| 649 | case GrClipEdgeType::kInverseFillBW: |
| 650 | return GrClipEdgeType::kFillBW; |
| 651 | case GrClipEdgeType::kInverseFillAA: |
| 652 | return GrClipEdgeType::kFillAA; |
| 653 | case GrClipEdgeType::kHairlineAA: |
| 654 | SK_ABORT("Hairline fill isn't invertible." ); |
| 655 | } |
| 656 | return GrClipEdgeType::kFillAA; // suppress warning. |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Indicates the type of pending IO operations that can be recorded for gpu resources. |
| 661 | */ |
| 662 | enum GrIOType { |
| 663 | kRead_GrIOType, |
| 664 | kWrite_GrIOType, |
| 665 | kRW_GrIOType |
| 666 | }; |
| 667 | |
| 668 | /** |
| 669 | * Indicates the type of data that a GPU buffer will be used for. |
| 670 | */ |
| 671 | enum class GrGpuBufferType { |
| 672 | kVertex, |
| 673 | kIndex, |
| 674 | kDrawIndirect, |
| 675 | kXferCpuToGpu, |
| 676 | kXferGpuToCpu, |
| 677 | }; |
| 678 | static const int kGrGpuBufferTypeCount = static_cast<int>(GrGpuBufferType::kXferGpuToCpu) + 1; |
| 679 | |
| 680 | /** |
| 681 | * Provides a performance hint regarding the frequency at which a data store will be accessed. |
| 682 | */ |
| 683 | enum GrAccessPattern { |
| 684 | /** Data store will be respecified repeatedly and used many times. */ |
| 685 | kDynamic_GrAccessPattern, |
| 686 | /** Data store will be specified once and used many times. (Thus disqualified from caching.) */ |
| 687 | kStatic_GrAccessPattern, |
| 688 | /** Data store will be specified once and used at most a few times. (Also can't be cached.) */ |
| 689 | kStream_GrAccessPattern, |
| 690 | |
| 691 | kLast_GrAccessPattern = kStream_GrAccessPattern |
| 692 | }; |
| 693 | |
| 694 | // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies |
| 695 | enum class GrInternalSurfaceFlags { |
| 696 | kNone = 0, |
| 697 | |
| 698 | // Texture-level |
| 699 | |
| 700 | // Means the pixels in the texture are read-only. Cannot also be a GrRenderTarget[Proxy]. |
| 701 | kReadOnly = 1 << 0, |
| 702 | |
| 703 | // RT-level |
| 704 | |
| 705 | // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0. |
| 706 | kGLRTFBOIDIs0 = 1 << 1, |
| 707 | |
| 708 | // This means the render target is multisampled, and internally holds a non-msaa texture for |
| 709 | // resolving into. The render target resolves itself by blitting into this internal texture. |
| 710 | // (asTexture() might or might not return the internal texture, but if it does, we always |
| 711 | // resolve the render target before accessing this texture's data.) |
| 712 | kRequiresManualMSAAResolve = 1 << 2, |
| 713 | |
| 714 | // This means the pixels in the render target are write-only. This is used for Dawn and Metal |
| 715 | // swap chain targets which can be rendered to, but not read or copied. |
| 716 | kFramebufferOnly = 1 << 3, |
| 717 | }; |
| 718 | |
| 719 | GR_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags) |
| 720 | |
| 721 | // 'GR_MAKE_BITFIELD_CLASS_OPS' defines the & operator on GrInternalSurfaceFlags to return bool. |
| 722 | // We want to find the bitwise & with these masks, so we declare them as ints. |
| 723 | constexpr static int kGrInternalTextureFlagsMask = static_cast<int>( |
| 724 | GrInternalSurfaceFlags::kReadOnly); |
| 725 | |
| 726 | constexpr static int kGrInternalRenderTargetFlagsMask = static_cast<int>( |
| 727 | GrInternalSurfaceFlags::kGLRTFBOIDIs0 | GrInternalSurfaceFlags::kRequiresManualMSAAResolve); |
| 728 | |
| 729 | constexpr static int kGrInternalTextureRenderTargetFlagsMask = |
| 730 | kGrInternalTextureFlagsMask | kGrInternalRenderTargetFlagsMask; |
| 731 | |
| 732 | #ifdef SK_DEBUG |
| 733 | // Takes a pointer to a GrCaps, and will suppress prints if required |
| 734 | #define GrCapsDebugf(caps, ...) if (!(caps)->suppressPrints()) SkDebugf(__VA_ARGS__) |
| 735 | #else |
| 736 | #define GrCapsDebugf(caps, ...) do {} while (0) |
| 737 | #endif |
| 738 | |
| 739 | /** |
| 740 | * Specifies if the holder owns the backend, OpenGL or Vulkan, object. |
| 741 | */ |
| 742 | enum class GrBackendObjectOwnership : bool { |
| 743 | /** Holder does not destroy the backend object. */ |
| 744 | kBorrowed = false, |
| 745 | /** Holder destroys the backend object. */ |
| 746 | kOwned = true |
| 747 | }; |
| 748 | |
| 749 | /* |
| 750 | * Object for CPU-GPU synchronization |
| 751 | */ |
| 752 | typedef uint64_t GrFence; |
| 753 | |
| 754 | /** |
| 755 | * Used to include or exclude specific GPU path renderers for testing purposes. |
| 756 | */ |
| 757 | enum class GpuPathRenderers { |
| 758 | kNone = 0, // Always use software masks and/or GrDefaultPathRenderer. |
| 759 | kDashLine = 1 << 0, |
| 760 | kTessellation = 1 << 1, |
| 761 | kStencilAndCover = 1 << 2, |
| 762 | kCoverageCounting = 1 << 3, |
| 763 | kAAHairline = 1 << 4, |
| 764 | kAAConvex = 1 << 5, |
| 765 | kAALinearizing = 1 << 6, |
| 766 | kSmall = 1 << 7, |
| 767 | kTriangulating = 1 << 8, |
| 768 | kDefault = ((1 << 9) - 1) & ~kTessellation // All but kTessellation. |
| 769 | }; |
| 770 | |
| 771 | /** |
| 772 | * Used to describe the current state of Mips on a GrTexture |
| 773 | */ |
| 774 | enum class GrMipMapsStatus { |
| 775 | kNotAllocated, // Mips have not been allocated |
| 776 | kDirty, // Mips are allocated but the full mip tree does not have valid data |
| 777 | kValid, // All levels fully allocated and have valid data in them |
| 778 | }; |
| 779 | |
| 780 | GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers) |
| 781 | |
| 782 | /** |
| 783 | * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels, |
| 784 | * their type, and width. This exists so that the GPU backend can have private types that have no |
| 785 | * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend. |
| 786 | * It does not refer to a texture format and the mapping to texture formats may be many-to-many. |
| 787 | * It does not specify the sRGB encoding of the stored values. The components are listed in order of |
| 788 | * where they appear in memory. In other words the first component listed is in the low bits and |
| 789 | * the last component in the high bits. |
| 790 | */ |
| 791 | enum class GrColorType { |
| 792 | kUnknown, |
| 793 | kAlpha_8, |
| 794 | kBGR_565, |
| 795 | kABGR_4444, // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed. |
| 796 | kRGBA_8888, |
| 797 | kRGBA_8888_SRGB, |
| 798 | kRGB_888x, |
| 799 | kRG_88, |
| 800 | kBGRA_8888, |
| 801 | kRGBA_1010102, |
| 802 | kGray_8, |
| 803 | kAlpha_F16, |
| 804 | kRGBA_F16, |
| 805 | kRGBA_F16_Clamped, |
| 806 | kRGBA_F32, |
| 807 | |
| 808 | kAlpha_16, |
| 809 | kRG_1616, |
| 810 | kRG_F16, |
| 811 | kRGBA_16161616, |
| 812 | |
| 813 | // Unusual types that come up after reading back in cases where we are reassigning the meaning |
| 814 | // of a texture format's channels to use for a particular color format but have to read back the |
| 815 | // data to a full RGBA quadruple. (e.g. using a R8 texture format as A8 color type but the API |
| 816 | // only supports reading to RGBA8.) None of these have SkColorType equivalents. |
| 817 | kAlpha_8xxx, |
| 818 | kAlpha_F32xxx, |
| 819 | kGray_8xxx, |
| 820 | |
| 821 | // Types used to initialize backend textures. |
| 822 | kRGB_888, |
| 823 | kR_8, |
| 824 | kR_16, |
| 825 | kR_F16, |
| 826 | kGray_F16, |
| 827 | |
| 828 | kLast = kGray_F16 |
| 829 | }; |
| 830 | |
| 831 | static const int kGrColorTypeCnt = static_cast<int>(GrColorType::kLast) + 1; |
| 832 | |
| 833 | static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct) { |
| 834 | switch (ct) { |
| 835 | case GrColorType::kUnknown: return kUnknown_SkColorType; |
| 836 | case GrColorType::kAlpha_8: return kAlpha_8_SkColorType; |
| 837 | case GrColorType::kBGR_565: return kRGB_565_SkColorType; |
| 838 | case GrColorType::kABGR_4444: return kARGB_4444_SkColorType; |
| 839 | case GrColorType::kRGBA_8888: return kRGBA_8888_SkColorType; |
| 840 | // Once we add kRGBA_8888_SRGB_SkColorType we should return that here. |
| 841 | case GrColorType::kRGBA_8888_SRGB: return kRGBA_8888_SkColorType; |
| 842 | case GrColorType::kRGB_888x: return kRGB_888x_SkColorType; |
| 843 | case GrColorType::kRG_88: return kR8G8_unorm_SkColorType; |
| 844 | case GrColorType::kBGRA_8888: return kBGRA_8888_SkColorType; |
| 845 | case GrColorType::kRGBA_1010102: return kRGBA_1010102_SkColorType; |
| 846 | case GrColorType::kGray_8: return kGray_8_SkColorType; |
| 847 | case GrColorType::kAlpha_F16: return kA16_float_SkColorType; |
| 848 | case GrColorType::kRGBA_F16: return kRGBA_F16_SkColorType; |
| 849 | case GrColorType::kRGBA_F16_Clamped: return kRGBA_F16Norm_SkColorType; |
| 850 | case GrColorType::kRGBA_F32: return kRGBA_F32_SkColorType; |
| 851 | case GrColorType::kAlpha_8xxx: return kUnknown_SkColorType; |
| 852 | case GrColorType::kAlpha_F32xxx: return kUnknown_SkColorType; |
| 853 | case GrColorType::kGray_8xxx: return kUnknown_SkColorType; |
| 854 | case GrColorType::kAlpha_16: return kA16_unorm_SkColorType; |
| 855 | case GrColorType::kRG_1616: return kR16G16_unorm_SkColorType; |
| 856 | case GrColorType::kRGBA_16161616: return kR16G16B16A16_unorm_SkColorType; |
| 857 | case GrColorType::kRG_F16: return kR16G16_float_SkColorType; |
| 858 | case GrColorType::kRGB_888: return kUnknown_SkColorType; |
| 859 | case GrColorType::kR_8: return kUnknown_SkColorType; |
| 860 | case GrColorType::kR_16: return kUnknown_SkColorType; |
| 861 | case GrColorType::kR_F16: return kUnknown_SkColorType; |
| 862 | case GrColorType::kGray_F16: return kUnknown_SkColorType; |
| 863 | } |
| 864 | SkUNREACHABLE; |
| 865 | } |
| 866 | |
| 867 | static constexpr GrColorType SkColorTypeToGrColorType(SkColorType ct) { |
| 868 | switch (ct) { |
| 869 | case kUnknown_SkColorType: return GrColorType::kUnknown; |
| 870 | case kAlpha_8_SkColorType: return GrColorType::kAlpha_8; |
| 871 | case kRGB_565_SkColorType: return GrColorType::kBGR_565; |
| 872 | case kARGB_4444_SkColorType: return GrColorType::kABGR_4444; |
| 873 | case kRGBA_8888_SkColorType: return GrColorType::kRGBA_8888; |
| 874 | case kRGB_888x_SkColorType: return GrColorType::kRGB_888x; |
| 875 | case kBGRA_8888_SkColorType: return GrColorType::kBGRA_8888; |
| 876 | case kGray_8_SkColorType: return GrColorType::kGray_8; |
| 877 | case kRGBA_F16Norm_SkColorType: return GrColorType::kRGBA_F16_Clamped; |
| 878 | case kRGBA_F16_SkColorType: return GrColorType::kRGBA_F16; |
| 879 | case kRGBA_1010102_SkColorType: return GrColorType::kRGBA_1010102; |
| 880 | case kRGB_101010x_SkColorType: return GrColorType::kUnknown; |
| 881 | case kBGRA_1010102_SkColorType: return GrColorType::kUnknown; |
| 882 | case kBGR_101010x_SkColorType: return GrColorType::kUnknown; |
| 883 | case kRGBA_F32_SkColorType: return GrColorType::kRGBA_F32; |
| 884 | case kR8G8_unorm_SkColorType: return GrColorType::kRG_88; |
| 885 | case kA16_unorm_SkColorType: return GrColorType::kAlpha_16; |
| 886 | case kR16G16_unorm_SkColorType: return GrColorType::kRG_1616; |
| 887 | case kA16_float_SkColorType: return GrColorType::kAlpha_F16; |
| 888 | case kR16G16_float_SkColorType: return GrColorType::kRG_F16; |
| 889 | case kR16G16B16A16_unorm_SkColorType: return GrColorType::kRGBA_16161616; |
| 890 | } |
| 891 | SkUNREACHABLE; |
| 892 | } |
| 893 | |
| 894 | // This is a temporary means of mapping an SkColorType and format to a |
| 895 | // GrColorType::kRGBA_8888_SRGB. Once we have an SRGB SkColorType this can go away. |
| 896 | GrColorType SkColorTypeAndFormatToGrColorType(const GrCaps* caps, |
| 897 | SkColorType skCT, |
| 898 | const GrBackendFormat& format); |
| 899 | |
| 900 | static constexpr uint32_t GrColorTypeChannelFlags(GrColorType ct) { |
| 901 | switch (ct) { |
| 902 | case GrColorType::kUnknown: return 0; |
| 903 | case GrColorType::kAlpha_8: return kAlpha_SkColorChannelFlag; |
| 904 | case GrColorType::kBGR_565: return kRGB_SkColorChannelFlags; |
| 905 | case GrColorType::kABGR_4444: return kRGBA_SkColorChannelFlags; |
| 906 | case GrColorType::kRGBA_8888: return kRGBA_SkColorChannelFlags; |
| 907 | case GrColorType::kRGBA_8888_SRGB: return kRGBA_SkColorChannelFlags; |
| 908 | case GrColorType::kRGB_888x: return kRGB_SkColorChannelFlags; |
| 909 | case GrColorType::kRG_88: return kRG_SkColorChannelFlags; |
| 910 | case GrColorType::kBGRA_8888: return kRGBA_SkColorChannelFlags; |
| 911 | case GrColorType::kRGBA_1010102: return kRGBA_SkColorChannelFlags; |
| 912 | case GrColorType::kGray_8: return kGray_SkColorChannelFlag; |
| 913 | case GrColorType::kAlpha_F16: return kAlpha_SkColorChannelFlag; |
| 914 | case GrColorType::kRGBA_F16: return kRGBA_SkColorChannelFlags; |
| 915 | case GrColorType::kRGBA_F16_Clamped: return kRGBA_SkColorChannelFlags; |
| 916 | case GrColorType::kRGBA_F32: return kRGBA_SkColorChannelFlags; |
| 917 | case GrColorType::kAlpha_8xxx: return kAlpha_SkColorChannelFlag; |
| 918 | case GrColorType::kAlpha_F32xxx: return kAlpha_SkColorChannelFlag; |
| 919 | case GrColorType::kGray_8xxx: return kGray_SkColorChannelFlag; |
| 920 | case GrColorType::kAlpha_16: return kAlpha_SkColorChannelFlag; |
| 921 | case GrColorType::kRG_1616: return kRG_SkColorChannelFlags; |
| 922 | case GrColorType::kRGBA_16161616: return kRGBA_SkColorChannelFlags; |
| 923 | case GrColorType::kRG_F16: return kRG_SkColorChannelFlags; |
| 924 | case GrColorType::kRGB_888: return kRGB_SkColorChannelFlags; |
| 925 | case GrColorType::kR_8: return kRed_SkColorChannelFlag; |
| 926 | case GrColorType::kR_16: return kRed_SkColorChannelFlag; |
| 927 | case GrColorType::kR_F16: return kRed_SkColorChannelFlag; |
| 928 | case GrColorType::kGray_F16: return kGray_SkColorChannelFlag; |
| 929 | } |
| 930 | SkUNREACHABLE; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Describes the encoding of channel data in a GrColorType. |
| 935 | */ |
| 936 | enum class GrColorTypeEncoding { |
| 937 | kUnorm, |
| 938 | kSRGBUnorm, |
| 939 | // kSnorm, |
| 940 | kFloat, |
| 941 | // kSint |
| 942 | // kUint |
| 943 | }; |
| 944 | |
| 945 | /** |
| 946 | * Describes a GrColorType by how many bits are used for each color component and how they are |
| 947 | * encoded. Currently all the non-zero channels share a single GrColorTypeEncoding. This could be |
| 948 | * expanded to store separate encodings and to indicate which bits belong to which components. |
| 949 | */ |
| 950 | struct GrColorTypeDesc { |
| 951 | public: |
| 952 | static constexpr GrColorTypeDesc MakeRGBA(int rgba, GrColorTypeEncoding e) { |
| 953 | return {rgba, rgba, rgba, rgba, 0, e}; |
| 954 | } |
| 955 | |
| 956 | static constexpr GrColorTypeDesc MakeRGBA(int rgb, int a, GrColorTypeEncoding e) { |
| 957 | return {rgb, rgb, rgb, a, 0, e}; |
| 958 | } |
| 959 | |
| 960 | static constexpr GrColorTypeDesc MakeRGB(int rgb, GrColorTypeEncoding e) { |
| 961 | return {rgb, rgb, rgb, 0, 0, e}; |
| 962 | } |
| 963 | |
| 964 | static constexpr GrColorTypeDesc MakeRGB(int r, int g, int b, GrColorTypeEncoding e) { |
| 965 | return {r, g, b, 0, 0, e}; |
| 966 | } |
| 967 | |
| 968 | static constexpr GrColorTypeDesc MakeAlpha(int a, GrColorTypeEncoding e) { |
| 969 | return {0, 0, 0, a, 0, e}; |
| 970 | } |
| 971 | |
| 972 | static constexpr GrColorTypeDesc MakeR(int r, GrColorTypeEncoding e) { |
| 973 | return {r, 0, 0, 0, 0, e}; |
| 974 | } |
| 975 | |
| 976 | static constexpr GrColorTypeDesc MakeRG(int rg, GrColorTypeEncoding e) { |
| 977 | return {rg, rg, 0, 0, 0, e}; |
| 978 | } |
| 979 | |
| 980 | static constexpr GrColorTypeDesc MakeGray(int grayBits, GrColorTypeEncoding e) { |
| 981 | return {0, 0, 0, 0, grayBits, e}; |
| 982 | } |
| 983 | |
| 984 | static constexpr GrColorTypeDesc MakeInvalid() { return {}; } |
| 985 | |
| 986 | constexpr int r() const { return fRBits; } |
| 987 | constexpr int g() const { return fGBits; } |
| 988 | constexpr int b() const { return fBBits; } |
| 989 | constexpr int a() const { return fABits; } |
| 990 | constexpr int operator[](int c) const { |
| 991 | switch (c) { |
| 992 | case 0: return this->r(); |
| 993 | case 1: return this->g(); |
| 994 | case 2: return this->b(); |
| 995 | case 3: return this->a(); |
| 996 | } |
| 997 | SkUNREACHABLE; |
| 998 | } |
| 999 | |
| 1000 | constexpr int gray() const { return fGrayBits; } |
| 1001 | |
| 1002 | constexpr GrColorTypeEncoding encoding() const { return fEncoding; } |
| 1003 | |
| 1004 | private: |
| 1005 | int fRBits = 0; |
| 1006 | int fGBits = 0; |
| 1007 | int fBBits = 0; |
| 1008 | int fABits = 0; |
| 1009 | int fGrayBits = 0; |
| 1010 | GrColorTypeEncoding fEncoding = GrColorTypeEncoding::kUnorm; |
| 1011 | |
| 1012 | constexpr GrColorTypeDesc() = default; |
| 1013 | |
| 1014 | constexpr GrColorTypeDesc(int r, int g, int b, int a, int gray, GrColorTypeEncoding encoding) |
| 1015 | : fRBits(r), fGBits(g), fBBits(b), fABits(a), fGrayBits(gray), fEncoding(encoding) { |
| 1016 | SkASSERT(r >= 0 && g >= 0 && b >= 0 && a >= 0 && gray >= 0); |
| 1017 | SkASSERT(!gray || (!r && !g && !b)); |
| 1018 | SkASSERT(r || g || b || a || gray); |
| 1019 | } |
| 1020 | }; |
| 1021 | |
| 1022 | static constexpr GrColorTypeDesc GrGetColorTypeDesc(GrColorType ct) { |
| 1023 | switch (ct) { |
| 1024 | case GrColorType::kUnknown: |
| 1025 | return GrColorTypeDesc::MakeInvalid(); |
| 1026 | case GrColorType::kAlpha_8: |
| 1027 | return GrColorTypeDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm); |
| 1028 | case GrColorType::kBGR_565: |
| 1029 | return GrColorTypeDesc::MakeRGB(5, 6, 5, GrColorTypeEncoding::kUnorm); |
| 1030 | case GrColorType::kABGR_4444: |
| 1031 | return GrColorTypeDesc::MakeRGBA(4, GrColorTypeEncoding::kUnorm); |
| 1032 | case GrColorType::kRGBA_8888: |
| 1033 | return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm); |
| 1034 | case GrColorType::kRGBA_8888_SRGB: |
| 1035 | return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kSRGBUnorm); |
| 1036 | case GrColorType::kRGB_888x: |
| 1037 | return GrColorTypeDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm); |
| 1038 | case GrColorType::kRG_88: |
| 1039 | return GrColorTypeDesc::MakeRG(8, GrColorTypeEncoding::kUnorm); |
| 1040 | case GrColorType::kBGRA_8888: |
| 1041 | return GrColorTypeDesc::MakeRGBA(8, GrColorTypeEncoding::kUnorm); |
| 1042 | case GrColorType::kRGBA_1010102: |
| 1043 | return GrColorTypeDesc::MakeRGBA(10, 2, GrColorTypeEncoding::kUnorm); |
| 1044 | case GrColorType::kGray_8: |
| 1045 | return GrColorTypeDesc::MakeGray(8, GrColorTypeEncoding::kUnorm); |
| 1046 | case GrColorType::kAlpha_F16: |
| 1047 | return GrColorTypeDesc::MakeAlpha(16, GrColorTypeEncoding::kFloat); |
| 1048 | case GrColorType::kRGBA_F16: |
| 1049 | return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat); |
| 1050 | case GrColorType::kRGBA_F16_Clamped: |
| 1051 | return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kFloat); |
| 1052 | case GrColorType::kRGBA_F32: |
| 1053 | return GrColorTypeDesc::MakeRGBA(32, GrColorTypeEncoding::kFloat); |
| 1054 | case GrColorType::kAlpha_8xxx: |
| 1055 | return GrColorTypeDesc::MakeAlpha(8, GrColorTypeEncoding::kUnorm); |
| 1056 | case GrColorType::kAlpha_F32xxx: |
| 1057 | return GrColorTypeDesc::MakeAlpha(32, GrColorTypeEncoding::kFloat); |
| 1058 | case GrColorType::kGray_8xxx: |
| 1059 | return GrColorTypeDesc::MakeGray(8, GrColorTypeEncoding::kUnorm); |
| 1060 | case GrColorType::kAlpha_16: |
| 1061 | return GrColorTypeDesc::MakeAlpha(16, GrColorTypeEncoding::kUnorm); |
| 1062 | case GrColorType::kRG_1616: |
| 1063 | return GrColorTypeDesc::MakeRG(16, GrColorTypeEncoding::kUnorm); |
| 1064 | case GrColorType::kRGBA_16161616: |
| 1065 | return GrColorTypeDesc::MakeRGBA(16, GrColorTypeEncoding::kUnorm); |
| 1066 | case GrColorType::kRG_F16: |
| 1067 | return GrColorTypeDesc::MakeRG(16, GrColorTypeEncoding::kFloat); |
| 1068 | case GrColorType::kRGB_888: |
| 1069 | return GrColorTypeDesc::MakeRGB(8, GrColorTypeEncoding::kUnorm); |
| 1070 | case GrColorType::kR_8: |
| 1071 | return GrColorTypeDesc::MakeR(8, GrColorTypeEncoding::kUnorm); |
| 1072 | case GrColorType::kR_16: |
| 1073 | return GrColorTypeDesc::MakeR(16, GrColorTypeEncoding::kUnorm); |
| 1074 | case GrColorType::kR_F16: |
| 1075 | return GrColorTypeDesc::MakeR(16, GrColorTypeEncoding::kFloat); |
| 1076 | case GrColorType::kGray_F16: |
| 1077 | return GrColorTypeDesc::MakeGray(16, GrColorTypeEncoding::kFloat); |
| 1078 | } |
| 1079 | SkUNREACHABLE; |
| 1080 | } |
| 1081 | |
| 1082 | static constexpr GrClampType GrColorTypeClampType(GrColorType colorType) { |
| 1083 | if (GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kUnorm || |
| 1084 | GrGetColorTypeDesc(colorType).encoding() == GrColorTypeEncoding::kSRGBUnorm) { |
| 1085 | return GrClampType::kAuto; |
| 1086 | } |
| 1087 | return GrColorType::kRGBA_F16_Clamped == colorType ? GrClampType::kManual : GrClampType::kNone; |
| 1088 | } |
| 1089 | |
| 1090 | // Consider a color type "wider" than n if it has more than n bits for any its representable |
| 1091 | // channels. |
| 1092 | static constexpr bool GrColorTypeIsWiderThan(GrColorType colorType, int n) { |
| 1093 | SkASSERT(n > 0); |
| 1094 | auto desc = GrGetColorTypeDesc(colorType); |
| 1095 | return (desc.r() && desc.r() > n )|| |
| 1096 | (desc.g() && desc.g() > n) || |
| 1097 | (desc.b() && desc.b() > n) || |
| 1098 | (desc.a() && desc.a() > n) || |
| 1099 | (desc.gray() && desc.gray() > n); |
| 1100 | } |
| 1101 | |
| 1102 | static constexpr bool GrColorTypeIsAlphaOnly(GrColorType ct) { |
| 1103 | return GrColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag; |
| 1104 | } |
| 1105 | |
| 1106 | static constexpr bool GrColorTypeHasAlpha(GrColorType ct) { |
| 1107 | return GrColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag; |
| 1108 | } |
| 1109 | |
| 1110 | static constexpr size_t GrColorTypeBytesPerPixel(GrColorType ct) { |
| 1111 | switch (ct) { |
| 1112 | case GrColorType::kUnknown: return 0; |
| 1113 | case GrColorType::kAlpha_8: return 1; |
| 1114 | case GrColorType::kBGR_565: return 2; |
| 1115 | case GrColorType::kABGR_4444: return 2; |
| 1116 | case GrColorType::kRGBA_8888: return 4; |
| 1117 | case GrColorType::kRGBA_8888_SRGB: return 4; |
| 1118 | case GrColorType::kRGB_888x: return 4; |
| 1119 | case GrColorType::kRG_88: return 2; |
| 1120 | case GrColorType::kBGRA_8888: return 4; |
| 1121 | case GrColorType::kRGBA_1010102: return 4; |
| 1122 | case GrColorType::kGray_8: return 1; |
| 1123 | case GrColorType::kAlpha_F16: return 2; |
| 1124 | case GrColorType::kRGBA_F16: return 8; |
| 1125 | case GrColorType::kRGBA_F16_Clamped: return 8; |
| 1126 | case GrColorType::kRGBA_F32: return 16; |
| 1127 | case GrColorType::kAlpha_8xxx: return 4; |
| 1128 | case GrColorType::kAlpha_F32xxx: return 16; |
| 1129 | case GrColorType::kGray_8xxx: return 4; |
| 1130 | case GrColorType::kAlpha_16: return 2; |
| 1131 | case GrColorType::kRG_1616: return 4; |
| 1132 | case GrColorType::kRGBA_16161616: return 8; |
| 1133 | case GrColorType::kRG_F16: return 4; |
| 1134 | case GrColorType::kRGB_888: return 3; |
| 1135 | case GrColorType::kR_8: return 1; |
| 1136 | case GrColorType::kR_16: return 2; |
| 1137 | case GrColorType::kR_F16: return 2; |
| 1138 | case GrColorType::kGray_F16: return 2; |
| 1139 | } |
| 1140 | SkUNREACHABLE; |
| 1141 | } |
| 1142 | |
| 1143 | // In general we try to not mix CompressionType and ColorType, but currently SkImage still requires |
| 1144 | // an SkColorType even for CompressedTypes so we need some conversion. |
| 1145 | static constexpr SkColorType GrCompressionTypeToSkColorType(SkImage::CompressionType compression) { |
| 1146 | switch (compression) { |
| 1147 | case SkImage::CompressionType::kNone: return kUnknown_SkColorType; |
| 1148 | case SkImage::CompressionType::kETC2_RGB8_UNORM: return kRGB_888x_SkColorType; |
| 1149 | case SkImage::CompressionType::kBC1_RGB8_UNORM: return kRGB_888x_SkColorType; |
| 1150 | case SkImage::CompressionType::kBC1_RGBA8_UNORM: return kRGBA_8888_SkColorType; |
| 1151 | } |
| 1152 | |
| 1153 | SkUNREACHABLE; |
| 1154 | } |
| 1155 | |
| 1156 | static constexpr GrColorType GrMaskFormatToColorType(GrMaskFormat format) { |
| 1157 | switch (format) { |
| 1158 | case kA8_GrMaskFormat: |
| 1159 | return GrColorType::kAlpha_8; |
| 1160 | case kA565_GrMaskFormat: |
| 1161 | return GrColorType::kBGR_565; |
| 1162 | case kARGB_GrMaskFormat: |
| 1163 | return GrColorType::kRGBA_8888; |
| 1164 | } |
| 1165 | SkUNREACHABLE; |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * Ref-counted object that calls a callback from its destructor. |
| 1170 | */ |
| 1171 | class GrRefCntedCallback : public SkRefCnt { |
| 1172 | public: |
| 1173 | using Context = void*; |
| 1174 | using Callback = void (*)(Context); |
| 1175 | |
| 1176 | GrRefCntedCallback(Callback proc, Context ctx) : fReleaseProc(proc), fReleaseCtx(ctx) { |
| 1177 | SkASSERT(proc); |
| 1178 | } |
| 1179 | ~GrRefCntedCallback() override { fReleaseProc ? fReleaseProc(fReleaseCtx) : void(); } |
| 1180 | |
| 1181 | Context context() const { return fReleaseCtx; } |
| 1182 | |
| 1183 | private: |
| 1184 | Callback fReleaseProc; |
| 1185 | Context fReleaseCtx; |
| 1186 | }; |
| 1187 | |
| 1188 | #if GR_TEST_UTILS || defined(SK_ENABLE_DUMP_GPU) |
| 1189 | static constexpr const char* GrBackendApiToStr(GrBackendApi api) { |
| 1190 | switch (api) { |
| 1191 | case GrBackendApi::kOpenGL: return "OpenGL" ; |
| 1192 | case GrBackendApi::kVulkan: return "Vulkan" ; |
| 1193 | case GrBackendApi::kMetal: return "Metal" ; |
| 1194 | case GrBackendApi::kDirect3D: return "Direct3D" ; |
| 1195 | case GrBackendApi::kDawn: return "Dawn" ; |
| 1196 | case GrBackendApi::kMock: return "Mock" ; |
| 1197 | } |
| 1198 | SkUNREACHABLE; |
| 1199 | } |
| 1200 | |
| 1201 | static constexpr const char* GrColorTypeToStr(GrColorType ct) { |
| 1202 | switch (ct) { |
| 1203 | case GrColorType::kUnknown: return "kUnknown" ; |
| 1204 | case GrColorType::kAlpha_8: return "kAlpha_8" ; |
| 1205 | case GrColorType::kBGR_565: return "kRGB_565" ; |
| 1206 | case GrColorType::kABGR_4444: return "kABGR_4444" ; |
| 1207 | case GrColorType::kRGBA_8888: return "kRGBA_8888" ; |
| 1208 | case GrColorType::kRGBA_8888_SRGB: return "kRGBA_8888_SRGB" ; |
| 1209 | case GrColorType::kRGB_888x: return "kRGB_888x" ; |
| 1210 | case GrColorType::kRG_88: return "kRG_88" ; |
| 1211 | case GrColorType::kBGRA_8888: return "kBGRA_8888" ; |
| 1212 | case GrColorType::kRGBA_1010102: return "kRGBA_1010102" ; |
| 1213 | case GrColorType::kGray_8: return "kGray_8" ; |
| 1214 | case GrColorType::kAlpha_F16: return "kAlpha_F16" ; |
| 1215 | case GrColorType::kRGBA_F16: return "kRGBA_F16" ; |
| 1216 | case GrColorType::kRGBA_F16_Clamped: return "kRGBA_F16_Clamped" ; |
| 1217 | case GrColorType::kRGBA_F32: return "kRGBA_F32" ; |
| 1218 | case GrColorType::kAlpha_8xxx: return "kAlpha_8xxx" ; |
| 1219 | case GrColorType::kAlpha_F32xxx: return "kAlpha_F32xxx" ; |
| 1220 | case GrColorType::kGray_8xxx: return "kGray_8xxx" ; |
| 1221 | case GrColorType::kAlpha_16: return "kAlpha_16" ; |
| 1222 | case GrColorType::kRG_1616: return "kRG_1616" ; |
| 1223 | case GrColorType::kRGBA_16161616: return "kRGBA_16161616" ; |
| 1224 | case GrColorType::kRG_F16: return "kRG_F16" ; |
| 1225 | case GrColorType::kRGB_888: return "kRGB_888" ; |
| 1226 | case GrColorType::kR_8: return "kR_8" ; |
| 1227 | case GrColorType::kR_16: return "kR_16" ; |
| 1228 | case GrColorType::kR_F16: return "kR_F16" ; |
| 1229 | case GrColorType::kGray_F16: return "kGray_F16" ; |
| 1230 | } |
| 1231 | SkUNREACHABLE; |
| 1232 | } |
| 1233 | |
| 1234 | static constexpr const char* GrCompressionTypeToStr(SkImage::CompressionType compression) { |
| 1235 | switch (compression) { |
| 1236 | case SkImage::CompressionType::kNone: return "kNone" ; |
| 1237 | case SkImage::CompressionType::kETC2_RGB8_UNORM: return "kETC2_RGB8_UNORM" ; |
| 1238 | case SkImage::CompressionType::kBC1_RGB8_UNORM: return "kBC1_RGB8_UNORM" ; |
| 1239 | case SkImage::CompressionType::kBC1_RGBA8_UNORM: return "kBC1_RGBA8_UNORM" ; |
| 1240 | } |
| 1241 | SkUNREACHABLE; |
| 1242 | } |
| 1243 | #endif |
| 1244 | |
| 1245 | #endif |
| 1246 | |