GrTypesPriv.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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. #ifndef GrTypesPriv_DEFINED
  8. #define GrTypesPriv_DEFINED
  9. #include <chrono>
  10. #include "GrSharedEnums.h"
  11. #include "GrTypes.h"
  12. #include "SkCanvas.h"
  13. #include "SkImageInfo.h"
  14. #include "SkImageInfoPriv.h"
  15. #include "SkRefCnt.h"
  16. #include "SkWeakRefCnt.h"
  17. class GrCaps;
  18. // The old libstdc++ uses the draft name "monotonic_clock" rather than "steady_clock". This might
  19. // not actually be monotonic, depending on how libstdc++ was built. However, this is only currently
  20. // used for idle resource purging so it shouldn't cause a correctness problem.
  21. #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000)
  22. using GrStdSteadyClock = std::chrono::monotonic_clock;
  23. #else
  24. using GrStdSteadyClock = std::chrono::steady_clock;
  25. #endif
  26. /**
  27. * Pixel configurations. This type conflates texture formats, CPU pixel formats, and
  28. * premultipliedness. We are moving away from it towards SkColorType and backend API (GL, Vulkan)
  29. * texture formats in the pulbic API. Right now this mostly refers to texture formats as we're
  30. * migrating.
  31. */
  32. enum GrPixelConfig {
  33. kUnknown_GrPixelConfig,
  34. kAlpha_8_GrPixelConfig,
  35. kGray_8_GrPixelConfig,
  36. kRGB_565_GrPixelConfig,
  37. kRGBA_4444_GrPixelConfig,
  38. kRGBA_8888_GrPixelConfig,
  39. kRGB_888_GrPixelConfig,
  40. kBGRA_8888_GrPixelConfig,
  41. kSRGBA_8888_GrPixelConfig,
  42. kSBGRA_8888_GrPixelConfig,
  43. kRGBA_1010102_GrPixelConfig,
  44. kRGBA_float_GrPixelConfig,
  45. kRG_float_GrPixelConfig,
  46. kAlpha_half_GrPixelConfig,
  47. kRGBA_half_GrPixelConfig,
  48. /** For internal usage. */
  49. kPrivateConfig1_GrPixelConfig,
  50. kPrivateConfig2_GrPixelConfig,
  51. kPrivateConfig3_GrPixelConfig,
  52. kPrivateConfig4_GrPixelConfig,
  53. kPrivateConfig5_GrPixelConfig,
  54. kLast_GrPixelConfig = kPrivateConfig5_GrPixelConfig
  55. };
  56. static const int kGrPixelConfigCnt = kLast_GrPixelConfig + 1;
  57. // Aliases for pixel configs that match skia's byte order.
  58. #ifndef SK_CPU_LENDIAN
  59. #error "Skia gpu currently assumes little endian"
  60. #endif
  61. #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
  62. static const GrPixelConfig kSkia8888_GrPixelConfig = kBGRA_8888_GrPixelConfig;
  63. #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
  64. static const GrPixelConfig kSkia8888_GrPixelConfig = kRGBA_8888_GrPixelConfig;
  65. #else
  66. #error "SK_*32_SHIFT values must correspond to GL_BGRA or GL_RGBA format."
  67. #endif
  68. /**
  69. * Geometric primitives used for drawing.
  70. */
  71. enum class GrPrimitiveType {
  72. kTriangles,
  73. kTriangleStrip,
  74. kPoints,
  75. kLines, // 1 pix wide only
  76. kLineStrip, // 1 pix wide only
  77. kLinesAdjacency // requires geometry shader support.
  78. };
  79. static constexpr int kNumGrPrimitiveTypes = (int)GrPrimitiveType::kLinesAdjacency + 1;
  80. static constexpr bool GrIsPrimTypeLines(GrPrimitiveType type) {
  81. return GrPrimitiveType::kLines == type ||
  82. GrPrimitiveType::kLineStrip == type ||
  83. GrPrimitiveType::kLinesAdjacency == type;
  84. }
  85. static constexpr bool GrIsPrimTypeTris(GrPrimitiveType type) {
  86. return GrPrimitiveType::kTriangles == type || GrPrimitiveType::kTriangleStrip == type;
  87. }
  88. static constexpr bool GrPrimTypeRequiresGeometryShaderSupport(GrPrimitiveType type) {
  89. return GrPrimitiveType::kLinesAdjacency == type;
  90. }
  91. enum class GrPrimitiveRestart : bool {
  92. kNo = false,
  93. kYes = true
  94. };
  95. /**
  96. * Formats for masks, used by the font cache. Important that these are 0-based.
  97. */
  98. enum GrMaskFormat {
  99. kA8_GrMaskFormat, //!< 1-byte per pixel
  100. kA565_GrMaskFormat, //!< 2-bytes per pixel, RGB represent 3-channel LCD coverage
  101. kARGB_GrMaskFormat, //!< 4-bytes per pixel, color format
  102. kLast_GrMaskFormat = kARGB_GrMaskFormat
  103. };
  104. static const int kMaskFormatCount = kLast_GrMaskFormat + 1;
  105. /**
  106. * Return the number of bytes-per-pixel for the specified mask format.
  107. */
  108. static inline int GrMaskFormatBytesPerPixel(GrMaskFormat format) {
  109. SkASSERT(format < kMaskFormatCount);
  110. // kA8 (0) -> 1
  111. // kA565 (1) -> 2
  112. // kARGB (2) -> 4
  113. static const int sBytesPerPixel[] = {1, 2, 4};
  114. static_assert(SK_ARRAY_COUNT(sBytesPerPixel) == kMaskFormatCount, "array_size_mismatch");
  115. static_assert(kA8_GrMaskFormat == 0, "enum_order_dependency");
  116. static_assert(kA565_GrMaskFormat == 1, "enum_order_dependency");
  117. static_assert(kARGB_GrMaskFormat == 2, "enum_order_dependency");
  118. return sBytesPerPixel[(int)format];
  119. }
  120. /**
  121. * Optional bitfield flags that can be set on GrSurfaceDesc (below).
  122. */
  123. enum GrSurfaceFlags {
  124. kNone_GrSurfaceFlags = 0x0,
  125. /**
  126. * Creates a texture that can be rendered to as a GrRenderTarget. Use
  127. * GrTexture::asRenderTarget() to access.
  128. */
  129. kRenderTarget_GrSurfaceFlag = 0x1,
  130. /**
  131. * Clears to zero on creation. It will cause creation failure if initial data is supplied to the
  132. * texture. This only affects the base level if the texture is created with MIP levels.
  133. */
  134. kPerformInitialClear_GrSurfaceFlag = 0x2
  135. };
  136. GR_MAKE_BITFIELD_OPS(GrSurfaceFlags)
  137. typedef GrSurfaceFlags GrSurfaceDescFlags;
  138. /**
  139. * Describes a surface to be created.
  140. */
  141. struct GrSurfaceDesc {
  142. GrSurfaceDesc()
  143. : fFlags(kNone_GrSurfaceFlags)
  144. , fWidth(0)
  145. , fHeight(0)
  146. , fConfig(kUnknown_GrPixelConfig)
  147. , fSampleCnt(1) {}
  148. GrSurfaceDescFlags fFlags; //!< bitfield of TextureFlags
  149. int fWidth; //!< Width of the texture
  150. int fHeight; //!< Height of the texture
  151. /**
  152. * Format of source data of the texture. Not guaranteed to be the same as
  153. * internal format used by 3D API.
  154. */
  155. GrPixelConfig fConfig;
  156. /**
  157. * The number of samples per pixel. Zero is treated equivalently to 1. This only
  158. * applies if the kRenderTarget_GrSurfaceFlag is set. The actual number
  159. * of samples may not exactly match the request. The request will be rounded
  160. * up to the next supported sample count. A value larger than the largest
  161. * supported sample count will fail.
  162. */
  163. int fSampleCnt;
  164. };
  165. /** Ownership rules for external GPU resources imported into Skia. */
  166. enum GrWrapOwnership {
  167. /** Skia will assume the client will keep the resource alive and Skia will not free it. */
  168. kBorrow_GrWrapOwnership,
  169. /** Skia will assume ownership of the resource and free it. */
  170. kAdopt_GrWrapOwnership,
  171. };
  172. /**
  173. * Clips are composed from these objects.
  174. */
  175. enum GrClipType {
  176. kRect_ClipType,
  177. kPath_ClipType
  178. };
  179. enum class GrScissorTest : bool {
  180. kDisabled = false,
  181. kEnabled = true
  182. };
  183. struct GrMipLevel {
  184. const void* fPixels;
  185. size_t fRowBytes;
  186. };
  187. /**
  188. * This enum is used to specify the load operation to be used when an opList/GrGpuCommandBuffer
  189. * begins execution.
  190. */
  191. enum class GrLoadOp {
  192. kLoad,
  193. kClear,
  194. kDiscard,
  195. };
  196. /**
  197. * This enum is used to specify the store operation to be used when an opList/GrGpuCommandBuffer
  198. * ends execution.
  199. */
  200. enum class GrStoreOp {
  201. kStore,
  202. kDiscard,
  203. };
  204. /**
  205. * Used to control antialiasing in draw calls.
  206. */
  207. enum class GrAA : bool {
  208. kNo = false,
  209. kYes = true
  210. };
  211. /** This enum indicates the type of antialiasing to be performed. */
  212. enum class GrAAType : unsigned {
  213. /** No antialiasing */
  214. kNone,
  215. /** Use fragment shader code to compute a fractional pixel coverage. */
  216. kCoverage,
  217. /** Use normal MSAA. */
  218. kMSAA,
  219. /**
  220. * Use "mixed samples" MSAA such that the stencil buffer is multisampled but the color buffer is
  221. * not.
  222. */
  223. kMixedSamples
  224. };
  225. static inline bool GrAATypeIsHW(GrAAType type) {
  226. switch (type) {
  227. case GrAAType::kNone:
  228. return false;
  229. case GrAAType::kCoverage:
  230. return false;
  231. case GrAAType::kMSAA:
  232. return true;
  233. case GrAAType::kMixedSamples:
  234. return true;
  235. }
  236. SK_ABORT("Unknown AA Type");
  237. return false;
  238. }
  239. /** The type of full scene antialiasing supported by a render target. */
  240. enum class GrFSAAType {
  241. /** No FSAA */
  242. kNone,
  243. /** Regular MSAA where each attachment has the same sample count. */
  244. kUnifiedMSAA,
  245. /** One color sample, N stencil samples. */
  246. kMixedSamples,
  247. };
  248. /**
  249. * Not all drawing code paths support using mixed samples when available and instead use
  250. * coverage-based aa.
  251. */
  252. enum class GrAllowMixedSamples : bool { kNo = false, kYes = true };
  253. GrAAType GrChooseAAType(GrAA, GrFSAAType, GrAllowMixedSamples, const GrCaps&);
  254. enum class GrQuadAAFlags {
  255. kLeft = SkCanvas::kLeft_QuadAAFlag,
  256. kTop = SkCanvas::kTop_QuadAAFlag,
  257. kRight = SkCanvas::kRight_QuadAAFlag,
  258. kBottom = SkCanvas::kBottom_QuadAAFlag,
  259. kNone = SkCanvas::kNone_QuadAAFlags,
  260. kAll = SkCanvas::kAll_QuadAAFlags
  261. };
  262. GR_MAKE_BITFIELD_CLASS_OPS(GrQuadAAFlags)
  263. static inline GrQuadAAFlags SkToGrQuadAAFlags(unsigned flags) {
  264. return static_cast<GrQuadAAFlags>(flags);
  265. }
  266. /**
  267. * Types of shader-language-specific boxed variables we can create.
  268. */
  269. enum GrSLType {
  270. kVoid_GrSLType,
  271. kBool_GrSLType,
  272. kByte_GrSLType,
  273. kByte2_GrSLType,
  274. kByte3_GrSLType,
  275. kByte4_GrSLType,
  276. kUByte_GrSLType,
  277. kUByte2_GrSLType,
  278. kUByte3_GrSLType,
  279. kUByte4_GrSLType,
  280. kShort_GrSLType,
  281. kShort2_GrSLType,
  282. kShort3_GrSLType,
  283. kShort4_GrSLType,
  284. kUShort_GrSLType,
  285. kUShort2_GrSLType,
  286. kUShort3_GrSLType,
  287. kUShort4_GrSLType,
  288. kFloat_GrSLType,
  289. kFloat2_GrSLType,
  290. kFloat3_GrSLType,
  291. kFloat4_GrSLType,
  292. kFloat2x2_GrSLType,
  293. kFloat3x3_GrSLType,
  294. kFloat4x4_GrSLType,
  295. kHalf_GrSLType,
  296. kHalf2_GrSLType,
  297. kHalf3_GrSLType,
  298. kHalf4_GrSLType,
  299. kHalf2x2_GrSLType,
  300. kHalf3x3_GrSLType,
  301. kHalf4x4_GrSLType,
  302. kInt_GrSLType,
  303. kInt2_GrSLType,
  304. kInt3_GrSLType,
  305. kInt4_GrSLType,
  306. kUint_GrSLType,
  307. kUint2_GrSLType,
  308. kTexture2DSampler_GrSLType,
  309. kTextureExternalSampler_GrSLType,
  310. kTexture2DRectSampler_GrSLType,
  311. };
  312. /**
  313. * The type of texture. Backends other than GL currently only use the 2D value but the type must
  314. * still be known at the API-neutral layer as it used to determine whether MIP maps, renderability,
  315. * and sampling parameters are legal for proxies that will be instantiated with wrapped textures.
  316. */
  317. enum class GrTextureType {
  318. k2D,
  319. kRectangle,
  320. kExternal
  321. };
  322. enum GrShaderType {
  323. kVertex_GrShaderType,
  324. kGeometry_GrShaderType,
  325. kFragment_GrShaderType,
  326. kLastkFragment_GrShaderType = kFragment_GrShaderType
  327. };
  328. static const int kGrShaderTypeCount = kLastkFragment_GrShaderType + 1;
  329. enum GrShaderFlags {
  330. kNone_GrShaderFlags = 0,
  331. kVertex_GrShaderFlag = 1 << kVertex_GrShaderType,
  332. kGeometry_GrShaderFlag = 1 << kGeometry_GrShaderType,
  333. kFragment_GrShaderFlag = 1 << kFragment_GrShaderType
  334. };
  335. GR_MAKE_BITFIELD_OPS(GrShaderFlags);
  336. /**
  337. * Precisions of shader language variables. Not all shading languages support precisions or actually
  338. * vary the internal precision based on the qualifiers. These currently only apply to float types (
  339. * including float vectors and matrices).
  340. */
  341. enum GrSLPrecision : int {
  342. kLow_GrSLPrecision,
  343. kMedium_GrSLPrecision,
  344. kHigh_GrSLPrecision,
  345. // Default precision is a special tag that means "whatever the default for the program/type
  346. // combination is". In other words, it maps to the empty string in shader code. There are some
  347. // scenarios where kDefault is not allowed (as the default precision for a program, or for
  348. // varyings, for example).
  349. kDefault_GrSLPrecision,
  350. // We only consider the "real" precisions here
  351. kLast_GrSLPrecision = kHigh_GrSLPrecision,
  352. };
  353. static const int kGrSLPrecisionCount = kLast_GrSLPrecision + 1;
  354. /** Is the shading language type float (including vectors/matrices)? */
  355. static inline bool GrSLTypeIsFloatType(GrSLType type) {
  356. switch (type) {
  357. case kFloat_GrSLType:
  358. case kFloat2_GrSLType:
  359. case kFloat3_GrSLType:
  360. case kFloat4_GrSLType:
  361. case kFloat2x2_GrSLType:
  362. case kFloat3x3_GrSLType:
  363. case kFloat4x4_GrSLType:
  364. case kHalf_GrSLType:
  365. case kHalf2_GrSLType:
  366. case kHalf3_GrSLType:
  367. case kHalf4_GrSLType:
  368. case kHalf2x2_GrSLType:
  369. case kHalf3x3_GrSLType:
  370. case kHalf4x4_GrSLType:
  371. return true;
  372. case kVoid_GrSLType:
  373. case kTexture2DSampler_GrSLType:
  374. case kTextureExternalSampler_GrSLType:
  375. case kTexture2DRectSampler_GrSLType:
  376. case kBool_GrSLType:
  377. case kByte_GrSLType:
  378. case kByte2_GrSLType:
  379. case kByte3_GrSLType:
  380. case kByte4_GrSLType:
  381. case kUByte_GrSLType:
  382. case kUByte2_GrSLType:
  383. case kUByte3_GrSLType:
  384. case kUByte4_GrSLType:
  385. case kShort_GrSLType:
  386. case kShort2_GrSLType:
  387. case kShort3_GrSLType:
  388. case kShort4_GrSLType:
  389. case kUShort_GrSLType:
  390. case kUShort2_GrSLType:
  391. case kUShort3_GrSLType:
  392. case kUShort4_GrSLType:
  393. case kInt_GrSLType:
  394. case kInt2_GrSLType:
  395. case kInt3_GrSLType:
  396. case kInt4_GrSLType:
  397. case kUint_GrSLType:
  398. case kUint2_GrSLType:
  399. return false;
  400. }
  401. SK_ABORT("Unexpected type");
  402. return false;
  403. }
  404. /** If the type represents a single value or vector return the vector length, else -1. */
  405. static inline int GrSLTypeVecLength(GrSLType type) {
  406. switch (type) {
  407. case kFloat_GrSLType:
  408. case kHalf_GrSLType:
  409. case kBool_GrSLType:
  410. case kByte_GrSLType:
  411. case kUByte_GrSLType:
  412. case kShort_GrSLType:
  413. case kUShort_GrSLType:
  414. case kInt_GrSLType:
  415. case kUint_GrSLType:
  416. return 1;
  417. case kFloat2_GrSLType:
  418. case kHalf2_GrSLType:
  419. case kByte2_GrSLType:
  420. case kUByte2_GrSLType:
  421. case kShort2_GrSLType:
  422. case kUShort2_GrSLType:
  423. case kInt2_GrSLType:
  424. case kUint2_GrSLType:
  425. return 2;
  426. case kFloat3_GrSLType:
  427. case kHalf3_GrSLType:
  428. case kByte3_GrSLType:
  429. case kUByte3_GrSLType:
  430. case kShort3_GrSLType:
  431. case kUShort3_GrSLType:
  432. case kInt3_GrSLType:
  433. return 3;
  434. case kFloat4_GrSLType:
  435. case kHalf4_GrSLType:
  436. case kByte4_GrSLType:
  437. case kUByte4_GrSLType:
  438. case kShort4_GrSLType:
  439. case kUShort4_GrSLType:
  440. case kInt4_GrSLType:
  441. return 4;
  442. case kFloat2x2_GrSLType:
  443. case kFloat3x3_GrSLType:
  444. case kFloat4x4_GrSLType:
  445. case kHalf2x2_GrSLType:
  446. case kHalf3x3_GrSLType:
  447. case kHalf4x4_GrSLType:
  448. case kVoid_GrSLType:
  449. case kTexture2DSampler_GrSLType:
  450. case kTextureExternalSampler_GrSLType:
  451. case kTexture2DRectSampler_GrSLType:
  452. return -1;
  453. }
  454. SK_ABORT("Unexpected type");
  455. return -1;
  456. }
  457. static inline GrSLType GrSLCombinedSamplerTypeForTextureType(GrTextureType type) {
  458. switch (type) {
  459. case GrTextureType::k2D:
  460. return kTexture2DSampler_GrSLType;
  461. case GrTextureType::kRectangle:
  462. return kTexture2DRectSampler_GrSLType;
  463. case GrTextureType::kExternal:
  464. return kTextureExternalSampler_GrSLType;
  465. }
  466. SK_ABORT("Unexpected texture type");
  467. return kTexture2DSampler_GrSLType;
  468. }
  469. /** Rectangle and external textures ony support the clamp wrap mode and do not support MIP maps. */
  470. static inline bool GrTextureTypeHasRestrictedSampling(GrTextureType type) {
  471. switch (type) {
  472. case GrTextureType::k2D:
  473. return false;
  474. case GrTextureType::kRectangle:
  475. return true;
  476. case GrTextureType::kExternal:
  477. return true;
  478. }
  479. SK_ABORT("Unexpected texture type");
  480. return false;
  481. }
  482. static inline bool GrSLTypeIsCombinedSamplerType(GrSLType type) {
  483. switch (type) {
  484. case kTexture2DSampler_GrSLType:
  485. case kTextureExternalSampler_GrSLType:
  486. case kTexture2DRectSampler_GrSLType:
  487. return true;
  488. case kVoid_GrSLType:
  489. case kFloat_GrSLType:
  490. case kFloat2_GrSLType:
  491. case kFloat3_GrSLType:
  492. case kFloat4_GrSLType:
  493. case kFloat2x2_GrSLType:
  494. case kFloat3x3_GrSLType:
  495. case kFloat4x4_GrSLType:
  496. case kHalf_GrSLType:
  497. case kHalf2_GrSLType:
  498. case kHalf3_GrSLType:
  499. case kHalf4_GrSLType:
  500. case kHalf2x2_GrSLType:
  501. case kHalf3x3_GrSLType:
  502. case kHalf4x4_GrSLType:
  503. case kInt_GrSLType:
  504. case kInt2_GrSLType:
  505. case kInt3_GrSLType:
  506. case kInt4_GrSLType:
  507. case kUint_GrSLType:
  508. case kUint2_GrSLType:
  509. case kBool_GrSLType:
  510. case kByte_GrSLType:
  511. case kByte2_GrSLType:
  512. case kByte3_GrSLType:
  513. case kByte4_GrSLType:
  514. case kUByte_GrSLType:
  515. case kUByte2_GrSLType:
  516. case kUByte3_GrSLType:
  517. case kUByte4_GrSLType:
  518. case kShort_GrSLType:
  519. case kShort2_GrSLType:
  520. case kShort3_GrSLType:
  521. case kShort4_GrSLType:
  522. case kUShort_GrSLType:
  523. case kUShort2_GrSLType:
  524. case kUShort3_GrSLType:
  525. case kUShort4_GrSLType:
  526. return false;
  527. }
  528. SK_ABORT("Unexpected type");
  529. return false;
  530. }
  531. static inline bool GrSLTypeAcceptsPrecision(GrSLType type) {
  532. switch (type) {
  533. case kTexture2DSampler_GrSLType:
  534. case kTextureExternalSampler_GrSLType:
  535. case kTexture2DRectSampler_GrSLType:
  536. return true;
  537. case kVoid_GrSLType:
  538. case kBool_GrSLType:
  539. case kByte_GrSLType:
  540. case kByte2_GrSLType:
  541. case kByte3_GrSLType:
  542. case kByte4_GrSLType:
  543. case kUByte_GrSLType:
  544. case kUByte2_GrSLType:
  545. case kUByte3_GrSLType:
  546. case kUByte4_GrSLType:
  547. case kShort_GrSLType:
  548. case kShort2_GrSLType:
  549. case kShort3_GrSLType:
  550. case kShort4_GrSLType:
  551. case kUShort_GrSLType:
  552. case kUShort2_GrSLType:
  553. case kUShort3_GrSLType:
  554. case kUShort4_GrSLType:
  555. case kFloat_GrSLType:
  556. case kFloat2_GrSLType:
  557. case kFloat3_GrSLType:
  558. case kFloat4_GrSLType:
  559. case kFloat2x2_GrSLType:
  560. case kFloat3x3_GrSLType:
  561. case kFloat4x4_GrSLType:
  562. case kHalf_GrSLType:
  563. case kHalf2_GrSLType:
  564. case kHalf3_GrSLType:
  565. case kHalf4_GrSLType:
  566. case kHalf2x2_GrSLType:
  567. case kHalf3x3_GrSLType:
  568. case kHalf4x4_GrSLType:
  569. case kInt_GrSLType:
  570. case kInt2_GrSLType:
  571. case kInt3_GrSLType:
  572. case kInt4_GrSLType:
  573. case kUint_GrSLType:
  574. case kUint2_GrSLType:
  575. return false;
  576. }
  577. SK_ABORT("Unexpected type");
  578. return false;
  579. }
  580. // temporarily accepting (but ignoring) precision modifiers on the new types; this will be killed
  581. // in a future CL
  582. static inline bool GrSLTypeTemporarilyAcceptsPrecision(GrSLType type) {
  583. switch (type) {
  584. case kShort_GrSLType:
  585. case kUShort_GrSLType:
  586. case kFloat_GrSLType:
  587. case kFloat2_GrSLType:
  588. case kFloat3_GrSLType:
  589. case kFloat4_GrSLType:
  590. case kFloat2x2_GrSLType:
  591. case kFloat3x3_GrSLType:
  592. case kFloat4x4_GrSLType:
  593. case kHalf_GrSLType:
  594. case kHalf2_GrSLType:
  595. case kHalf3_GrSLType:
  596. case kHalf4_GrSLType:
  597. case kHalf2x2_GrSLType:
  598. case kHalf3x3_GrSLType:
  599. case kHalf4x4_GrSLType:
  600. case kInt_GrSLType:
  601. case kInt2_GrSLType:
  602. case kInt3_GrSLType:
  603. case kInt4_GrSLType:
  604. case kUint_GrSLType:
  605. case kUint2_GrSLType:
  606. case kTexture2DSampler_GrSLType:
  607. case kTextureExternalSampler_GrSLType:
  608. case kTexture2DRectSampler_GrSLType:
  609. return true;
  610. case kVoid_GrSLType:
  611. case kBool_GrSLType:
  612. case kByte_GrSLType:
  613. case kByte2_GrSLType:
  614. case kByte3_GrSLType:
  615. case kByte4_GrSLType:
  616. case kUByte_GrSLType:
  617. case kUByte2_GrSLType:
  618. case kUByte3_GrSLType:
  619. case kUByte4_GrSLType:
  620. case kShort2_GrSLType:
  621. case kShort3_GrSLType:
  622. case kShort4_GrSLType:
  623. case kUShort2_GrSLType:
  624. case kUShort3_GrSLType:
  625. case kUShort4_GrSLType:
  626. return false;
  627. }
  628. SK_ABORT("Unexpected type");
  629. return false;
  630. }
  631. //////////////////////////////////////////////////////////////////////////////
  632. /**
  633. * Types used to describe format of vertices in arrays.
  634. */
  635. enum GrVertexAttribType {
  636. kFloat_GrVertexAttribType = 0,
  637. kFloat2_GrVertexAttribType,
  638. kFloat3_GrVertexAttribType,
  639. kFloat4_GrVertexAttribType,
  640. kHalf_GrVertexAttribType,
  641. kHalf2_GrVertexAttribType,
  642. kHalf3_GrVertexAttribType,
  643. kHalf4_GrVertexAttribType,
  644. kInt2_GrVertexAttribType, // vector of 2 32-bit ints
  645. kInt3_GrVertexAttribType, // vector of 3 32-bit ints
  646. kInt4_GrVertexAttribType, // vector of 4 32-bit ints
  647. kByte_GrVertexAttribType, // signed byte
  648. kByte2_GrVertexAttribType, // vector of 2 8-bit signed bytes
  649. kByte3_GrVertexAttribType, // vector of 3 8-bit signed bytes
  650. kByte4_GrVertexAttribType, // vector of 4 8-bit signed bytes
  651. kUByte_GrVertexAttribType, // unsigned byte
  652. kUByte2_GrVertexAttribType, // vector of 2 8-bit unsigned bytes
  653. kUByte3_GrVertexAttribType, // vector of 3 8-bit unsigned bytes
  654. kUByte4_GrVertexAttribType, // vector of 4 8-bit unsigned bytes
  655. kUByte_norm_GrVertexAttribType, // unsigned byte, e.g. coverage, 0 -> 0.0f, 255 -> 1.0f.
  656. kUByte4_norm_GrVertexAttribType, // vector of 4 unsigned bytes, e.g. colors, 0 -> 0.0f,
  657. // 255 -> 1.0f.
  658. kShort2_GrVertexAttribType, // vector of 2 16-bit shorts.
  659. kShort4_GrVertexAttribType, // vector of 4 16-bit shorts.
  660. kUShort2_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0, 65535 -> 65535.
  661. kUShort2_norm_GrVertexAttribType, // vector of 2 unsigned shorts. 0 -> 0.0f, 65535 -> 1.0f.
  662. kInt_GrVertexAttribType,
  663. kUint_GrVertexAttribType,
  664. kLast_GrVertexAttribType = kUint_GrVertexAttribType
  665. };
  666. static const int kGrVertexAttribTypeCount = kLast_GrVertexAttribType + 1;
  667. //////////////////////////////////////////////////////////////////////////////
  668. static const int kGrClipEdgeTypeCnt = (int) GrClipEdgeType::kLast + 1;
  669. static inline bool GrProcessorEdgeTypeIsFill(const GrClipEdgeType edgeType) {
  670. return (GrClipEdgeType::kFillAA == edgeType || GrClipEdgeType::kFillBW == edgeType);
  671. }
  672. static inline bool GrProcessorEdgeTypeIsInverseFill(const GrClipEdgeType edgeType) {
  673. return (GrClipEdgeType::kInverseFillAA == edgeType ||
  674. GrClipEdgeType::kInverseFillBW == edgeType);
  675. }
  676. static inline bool GrProcessorEdgeTypeIsAA(const GrClipEdgeType edgeType) {
  677. return (GrClipEdgeType::kFillBW != edgeType &&
  678. GrClipEdgeType::kInverseFillBW != edgeType);
  679. }
  680. static inline GrClipEdgeType GrInvertProcessorEdgeType(const GrClipEdgeType edgeType) {
  681. switch (edgeType) {
  682. case GrClipEdgeType::kFillBW:
  683. return GrClipEdgeType::kInverseFillBW;
  684. case GrClipEdgeType::kFillAA:
  685. return GrClipEdgeType::kInverseFillAA;
  686. case GrClipEdgeType::kInverseFillBW:
  687. return GrClipEdgeType::kFillBW;
  688. case GrClipEdgeType::kInverseFillAA:
  689. return GrClipEdgeType::kFillAA;
  690. case GrClipEdgeType::kHairlineAA:
  691. SK_ABORT("Hairline fill isn't invertible.");
  692. }
  693. return GrClipEdgeType::kFillAA; // suppress warning.
  694. }
  695. /**
  696. * Indicates the type of pending IO operations that can be recorded for gpu resources.
  697. */
  698. enum GrIOType {
  699. kRead_GrIOType,
  700. kWrite_GrIOType,
  701. kRW_GrIOType
  702. };
  703. /**
  704. * Indicates the type of data that a GPU buffer will be used for.
  705. */
  706. enum GrBufferType {
  707. kVertex_GrBufferType,
  708. kIndex_GrBufferType,
  709. kTexel_GrBufferType,
  710. kDrawIndirect_GrBufferType,
  711. kXferCpuToGpu_GrBufferType,
  712. kXferGpuToCpu_GrBufferType,
  713. kLast_GrBufferType = kXferGpuToCpu_GrBufferType
  714. };
  715. static const int kGrBufferTypeCount = kLast_GrBufferType + 1;
  716. static inline bool GrBufferTypeIsVertexOrIndex(GrBufferType type) {
  717. SkASSERT(type >= 0 && type < kGrBufferTypeCount);
  718. return type <= kIndex_GrBufferType;
  719. GR_STATIC_ASSERT(0 == kVertex_GrBufferType);
  720. GR_STATIC_ASSERT(1 == kIndex_GrBufferType);
  721. }
  722. /**
  723. * Provides a performance hint regarding the frequency at which a data store will be accessed.
  724. */
  725. enum GrAccessPattern {
  726. /** Data store will be respecified repeatedly and used many times. */
  727. kDynamic_GrAccessPattern,
  728. /** Data store will be specified once and used many times. (Thus disqualified from caching.) */
  729. kStatic_GrAccessPattern,
  730. /** Data store will be specified once and used at most a few times. (Also can't be cached.) */
  731. kStream_GrAccessPattern,
  732. kLast_GrAccessPattern = kStream_GrAccessPattern
  733. };
  734. // Flags shared between the GrSurface & GrSurfaceProxy class hierarchies
  735. enum class GrInternalSurfaceFlags {
  736. kNone = 0,
  737. // Surface-level
  738. kNoPendingIO = 1 << 0,
  739. kSurfaceMask = kNoPendingIO,
  740. // RT-only
  741. // For internal resources:
  742. // this is enabled whenever MSAA is enabled and GrCaps reports mixed samples are supported
  743. // For wrapped resources:
  744. // this is disabled for FBO0
  745. // but, otherwise, is enabled whenever MSAA is enabled and GrCaps reports mixed samples
  746. // are supported
  747. kMixedSampled = 1 << 2,
  748. // For internal resources:
  749. // this is enabled whenever GrCaps reports window rect support
  750. // For wrapped resources1
  751. // this is disabled for FBO0
  752. // but, otherwise, is enabled whenever GrCaps reports window rect support
  753. kWindowRectsSupport = 1 << 3,
  754. // This flag is for use with GL only. It tells us that the internal render target wraps FBO 0.
  755. kGLRTFBOIDIs0 = 1 << 4,
  756. kRenderTargetMask = kMixedSampled | kWindowRectsSupport | kGLRTFBOIDIs0,
  757. };
  758. GR_MAKE_BITFIELD_CLASS_OPS(GrInternalSurfaceFlags)
  759. #ifdef SK_DEBUG
  760. // Takes a pointer to a GrCaps, and will suppress prints if required
  761. #define GrCapsDebugf(caps, ...) \
  762. if (!(caps)->suppressPrints()) { \
  763. SkDebugf(__VA_ARGS__); \
  764. }
  765. #else
  766. #define GrCapsDebugf(caps, ...)
  767. #endif
  768. /**
  769. * Specifies if the holder owns the backend, OpenGL or Vulkan, object.
  770. */
  771. enum class GrBackendObjectOwnership : bool {
  772. /** Holder does not destroy the backend object. */
  773. kBorrowed = false,
  774. /** Holder destroys the backend object. */
  775. kOwned = true
  776. };
  777. template <typename T>
  778. T* const* unique_ptr_address_as_pointer_address(std::unique_ptr<T> const* up) {
  779. static_assert(sizeof(T*) == sizeof(std::unique_ptr<T>), "unique_ptr not expected size.");
  780. return reinterpret_cast<T* const*>(up);
  781. }
  782. /*
  783. * Object for CPU-GPU synchronization
  784. */
  785. typedef uint64_t GrFence;
  786. /**
  787. * Used to include or exclude specific GPU path renderers for testing purposes.
  788. */
  789. enum class GpuPathRenderers {
  790. kNone = 0, // Always use sofware masks and/or GrDefaultPathRenderer.
  791. kDashLine = 1 << 0,
  792. kStencilAndCover = 1 << 1,
  793. kCoverageCounting = 1 << 2,
  794. kAAHairline = 1 << 3,
  795. kAAConvex = 1 << 4,
  796. kAALinearizing = 1 << 5,
  797. kSmall = 1 << 6,
  798. kTessellating = 1 << 7,
  799. kAll = (kTessellating | (kTessellating - 1))
  800. };
  801. /**
  802. * Used to describe the current state of Mips on a GrTexture
  803. */
  804. enum class GrMipMapsStatus {
  805. kNotAllocated, // Mips have not been allocated
  806. kDirty, // Mips are allocated but the full mip tree does not have valid data
  807. kValid, // All levels fully allocated and have valid data in them
  808. };
  809. GR_MAKE_BITFIELD_CLASS_OPS(GpuPathRenderers)
  810. /**
  811. * We want to extend the GrPixelConfig enum to add cases for dealing with alpha_8 which is
  812. * internally either alpha8 or red8. Also for Gray_8 which can be luminance_8 or red_8.
  813. */
  814. static constexpr GrPixelConfig kAlpha_8_as_Alpha_GrPixelConfig = kPrivateConfig1_GrPixelConfig;
  815. static constexpr GrPixelConfig kAlpha_8_as_Red_GrPixelConfig = kPrivateConfig2_GrPixelConfig;
  816. static constexpr GrPixelConfig kAlpha_half_as_Red_GrPixelConfig = kPrivateConfig3_GrPixelConfig;
  817. static constexpr GrPixelConfig kGray_8_as_Lum_GrPixelConfig = kPrivateConfig4_GrPixelConfig;
  818. static constexpr GrPixelConfig kGray_8_as_Red_GrPixelConfig = kPrivateConfig5_GrPixelConfig;
  819. /**
  820. * Refers to the encoding of a GPU buffer as it will be interpreted by the GPU when sampling and
  821. * blending.
  822. */
  823. enum class GrSRGBEncoded : bool { kNo = false, kYes = true };
  824. /**
  825. * Describes whether pixel data encoding should be converted to/from linear/sRGB encoding.
  826. */
  827. enum class GrSRGBConversion {
  828. kNone,
  829. kSRGBToLinear,
  830. kLinearToSRGB,
  831. };
  832. /**
  833. * Utility functions for GrPixelConfig
  834. */
  835. // Returns whether the config's color channels are sRGB encoded.
  836. static inline GrSRGBEncoded GrPixelConfigIsSRGBEncoded(GrPixelConfig config) {
  837. switch (config) {
  838. case kSRGBA_8888_GrPixelConfig:
  839. case kSBGRA_8888_GrPixelConfig:
  840. return GrSRGBEncoded::kYes;
  841. case kUnknown_GrPixelConfig:
  842. case kAlpha_8_GrPixelConfig:
  843. case kAlpha_8_as_Alpha_GrPixelConfig:
  844. case kAlpha_8_as_Red_GrPixelConfig:
  845. case kGray_8_GrPixelConfig:
  846. case kGray_8_as_Lum_GrPixelConfig:
  847. case kGray_8_as_Red_GrPixelConfig:
  848. case kRGB_565_GrPixelConfig:
  849. case kRGBA_4444_GrPixelConfig:
  850. case kRGB_888_GrPixelConfig:
  851. case kRGBA_8888_GrPixelConfig:
  852. case kBGRA_8888_GrPixelConfig:
  853. case kRGBA_1010102_GrPixelConfig:
  854. case kRGBA_float_GrPixelConfig:
  855. case kRG_float_GrPixelConfig:
  856. case kAlpha_half_GrPixelConfig:
  857. case kAlpha_half_as_Red_GrPixelConfig:
  858. case kRGBA_half_GrPixelConfig:
  859. return GrSRGBEncoded::kNo;
  860. }
  861. SK_ABORT("Invalid pixel config");
  862. return GrSRGBEncoded::kNo;
  863. }
  864. static inline bool GrPixelConfigIsSRGB(GrPixelConfig config) {
  865. return GrSRGBEncoded::kYes == GrPixelConfigIsSRGBEncoded(config);
  866. }
  867. static inline size_t GrBytesPerPixel(GrPixelConfig config) {
  868. switch (config) {
  869. case kAlpha_8_GrPixelConfig:
  870. case kAlpha_8_as_Alpha_GrPixelConfig:
  871. case kAlpha_8_as_Red_GrPixelConfig:
  872. case kGray_8_GrPixelConfig:
  873. case kGray_8_as_Lum_GrPixelConfig:
  874. case kGray_8_as_Red_GrPixelConfig:
  875. return 1;
  876. case kRGB_565_GrPixelConfig:
  877. case kRGBA_4444_GrPixelConfig:
  878. case kAlpha_half_GrPixelConfig:
  879. case kAlpha_half_as_Red_GrPixelConfig:
  880. return 2;
  881. case kRGBA_8888_GrPixelConfig:
  882. case kRGB_888_GrPixelConfig: // Assuming GPUs store this 4-byte aligned.
  883. case kBGRA_8888_GrPixelConfig:
  884. case kSRGBA_8888_GrPixelConfig:
  885. case kSBGRA_8888_GrPixelConfig:
  886. case kRGBA_1010102_GrPixelConfig:
  887. return 4;
  888. case kRGBA_half_GrPixelConfig:
  889. return 8;
  890. case kRGBA_float_GrPixelConfig:
  891. return 16;
  892. case kRG_float_GrPixelConfig:
  893. return 8;
  894. case kUnknown_GrPixelConfig:
  895. return 0;
  896. }
  897. SK_ABORT("Invalid pixel config");
  898. return 0;
  899. }
  900. static inline bool GrPixelConfigIsOpaque(GrPixelConfig config) {
  901. switch (config) {
  902. case kRGB_565_GrPixelConfig:
  903. case kRGB_888_GrPixelConfig:
  904. case kGray_8_GrPixelConfig:
  905. case kGray_8_as_Lum_GrPixelConfig:
  906. case kGray_8_as_Red_GrPixelConfig:
  907. case kRG_float_GrPixelConfig:
  908. return true;
  909. case kAlpha_8_GrPixelConfig:
  910. case kAlpha_8_as_Alpha_GrPixelConfig:
  911. case kAlpha_8_as_Red_GrPixelConfig:
  912. case kRGBA_4444_GrPixelConfig:
  913. case kAlpha_half_GrPixelConfig:
  914. case kAlpha_half_as_Red_GrPixelConfig:
  915. case kRGBA_8888_GrPixelConfig:
  916. case kBGRA_8888_GrPixelConfig:
  917. case kSRGBA_8888_GrPixelConfig:
  918. case kSBGRA_8888_GrPixelConfig:
  919. case kRGBA_1010102_GrPixelConfig:
  920. case kRGBA_half_GrPixelConfig:
  921. case kRGBA_float_GrPixelConfig:
  922. case kUnknown_GrPixelConfig:
  923. return false;
  924. }
  925. SK_ABORT("Invalid pixel config");
  926. return false;
  927. }
  928. static inline bool GrPixelConfigIsAlphaOnly(GrPixelConfig config) {
  929. switch (config) {
  930. case kAlpha_8_GrPixelConfig:
  931. case kAlpha_8_as_Alpha_GrPixelConfig:
  932. case kAlpha_8_as_Red_GrPixelConfig:
  933. case kAlpha_half_GrPixelConfig:
  934. case kAlpha_half_as_Red_GrPixelConfig:
  935. return true;
  936. case kUnknown_GrPixelConfig:
  937. case kGray_8_GrPixelConfig:
  938. case kGray_8_as_Lum_GrPixelConfig:
  939. case kGray_8_as_Red_GrPixelConfig:
  940. case kRGB_565_GrPixelConfig:
  941. case kRGBA_4444_GrPixelConfig:
  942. case kRGBA_8888_GrPixelConfig:
  943. case kRGB_888_GrPixelConfig:
  944. case kBGRA_8888_GrPixelConfig:
  945. case kSRGBA_8888_GrPixelConfig:
  946. case kSBGRA_8888_GrPixelConfig:
  947. case kRGBA_1010102_GrPixelConfig:
  948. case kRGBA_float_GrPixelConfig:
  949. case kRG_float_GrPixelConfig:
  950. case kRGBA_half_GrPixelConfig:
  951. return false;
  952. }
  953. SK_ABORT("Invalid pixel config.");
  954. return false;
  955. }
  956. static inline bool GrPixelConfigIsFloatingPoint(GrPixelConfig config) {
  957. switch (config) {
  958. case kUnknown_GrPixelConfig:
  959. case kAlpha_8_GrPixelConfig:
  960. case kAlpha_8_as_Alpha_GrPixelConfig:
  961. case kAlpha_8_as_Red_GrPixelConfig:
  962. case kGray_8_GrPixelConfig:
  963. case kGray_8_as_Lum_GrPixelConfig:
  964. case kGray_8_as_Red_GrPixelConfig:
  965. case kRGB_565_GrPixelConfig:
  966. case kRGBA_4444_GrPixelConfig:
  967. case kRGB_888_GrPixelConfig:
  968. case kRGBA_8888_GrPixelConfig:
  969. case kBGRA_8888_GrPixelConfig:
  970. case kSRGBA_8888_GrPixelConfig:
  971. case kSBGRA_8888_GrPixelConfig:
  972. case kRGBA_1010102_GrPixelConfig:
  973. return false;
  974. case kRGBA_float_GrPixelConfig:
  975. case kRG_float_GrPixelConfig:
  976. case kAlpha_half_GrPixelConfig:
  977. case kAlpha_half_as_Red_GrPixelConfig:
  978. case kRGBA_half_GrPixelConfig:
  979. return true;
  980. }
  981. SK_ABORT("Invalid pixel config.");
  982. return false;
  983. }
  984. /**
  985. * Precision qualifier that should be used with a sampler.
  986. */
  987. static inline GrSLPrecision GrSLSamplerPrecision(GrPixelConfig config) {
  988. switch (config) {
  989. case kUnknown_GrPixelConfig:
  990. case kAlpha_8_GrPixelConfig:
  991. case kAlpha_8_as_Alpha_GrPixelConfig:
  992. case kAlpha_8_as_Red_GrPixelConfig:
  993. case kGray_8_GrPixelConfig:
  994. case kGray_8_as_Lum_GrPixelConfig:
  995. case kGray_8_as_Red_GrPixelConfig:
  996. case kRGB_565_GrPixelConfig:
  997. case kRGBA_4444_GrPixelConfig:
  998. case kRGBA_8888_GrPixelConfig:
  999. case kRGB_888_GrPixelConfig:
  1000. case kBGRA_8888_GrPixelConfig:
  1001. case kSRGBA_8888_GrPixelConfig:
  1002. case kSBGRA_8888_GrPixelConfig:
  1003. return kLow_GrSLPrecision;
  1004. case kRGBA_float_GrPixelConfig:
  1005. case kRG_float_GrPixelConfig:
  1006. return kHigh_GrSLPrecision;
  1007. case kAlpha_half_GrPixelConfig:
  1008. case kAlpha_half_as_Red_GrPixelConfig:
  1009. case kRGBA_half_GrPixelConfig:
  1010. case kRGBA_1010102_GrPixelConfig:
  1011. return kMedium_GrSLPrecision;
  1012. }
  1013. SK_ABORT("Unexpected type");
  1014. return kHigh_GrSLPrecision;
  1015. }
  1016. /**
  1017. * Like SkColorType this describes a layout of pixel data in CPU memory. It specifies the channels,
  1018. * their type, and width. This exists so that the GPU backend can have private types that have no
  1019. * analog in the public facing SkColorType enum and omit types not implemented in the GPU backend.
  1020. * It does not refer to a texture format and the mapping to texture formats may be many-to-many.
  1021. * It does not specify the sRGB encding of the stored values.
  1022. */
  1023. enum class GrColorType {
  1024. kUnknown,
  1025. kAlpha_8,
  1026. kRGB_565,
  1027. kABGR_4444, // This name differs from SkColorType. kARGB_4444_SkColorType is misnamed.
  1028. kRGBA_8888,
  1029. kRGB_888x,
  1030. kBGRA_8888,
  1031. kRGBA_1010102,
  1032. kGray_8,
  1033. kAlpha_F16,
  1034. kRGBA_F16,
  1035. kRG_F32,
  1036. kRGBA_F32,
  1037. };
  1038. static inline SkColorType GrColorTypeToSkColorType(GrColorType ct) {
  1039. switch (ct) {
  1040. case GrColorType::kUnknown: return kUnknown_SkColorType;
  1041. case GrColorType::kAlpha_8: return kAlpha_8_SkColorType;
  1042. case GrColorType::kRGB_565: return kRGB_565_SkColorType;
  1043. case GrColorType::kABGR_4444: return kARGB_4444_SkColorType;
  1044. case GrColorType::kRGBA_8888: return kRGBA_8888_SkColorType;
  1045. case GrColorType::kRGB_888x: return kRGB_888x_SkColorType;
  1046. case GrColorType::kBGRA_8888: return kBGRA_8888_SkColorType;
  1047. case GrColorType::kRGBA_1010102: return kRGBA_1010102_SkColorType;
  1048. case GrColorType::kGray_8: return kGray_8_SkColorType;
  1049. case GrColorType::kAlpha_F16: return kUnknown_SkColorType;
  1050. case GrColorType::kRGBA_F16: return kRGBA_F16_SkColorType;
  1051. case GrColorType::kRG_F32: return kUnknown_SkColorType;
  1052. case GrColorType::kRGBA_F32: return kRGBA_F32_SkColorType;
  1053. }
  1054. SK_ABORT("Invalid GrColorType");
  1055. return kUnknown_SkColorType;
  1056. }
  1057. static inline GrColorType SkColorTypeToGrColorType(SkColorType ct) {
  1058. switch (ct) {
  1059. case kUnknown_SkColorType: return GrColorType::kUnknown;
  1060. case kAlpha_8_SkColorType: return GrColorType::kAlpha_8;
  1061. case kRGB_565_SkColorType: return GrColorType::kRGB_565;
  1062. case kARGB_4444_SkColorType: return GrColorType::kABGR_4444;
  1063. case kRGBA_8888_SkColorType: return GrColorType::kRGBA_8888;
  1064. case kRGB_888x_SkColorType: return GrColorType::kRGB_888x;
  1065. case kBGRA_8888_SkColorType: return GrColorType::kBGRA_8888;
  1066. case kGray_8_SkColorType: return GrColorType::kGray_8;
  1067. case kRGBA_F16_SkColorType: return GrColorType::kRGBA_F16;
  1068. case kRGBA_1010102_SkColorType: return GrColorType::kRGBA_1010102;
  1069. case kRGB_101010x_SkColorType: return GrColorType::kUnknown;
  1070. case kRGBA_F32_SkColorType: return GrColorType::kRGBA_F32;
  1071. }
  1072. SK_ABORT("Invalid SkColorType");
  1073. return GrColorType::kUnknown;
  1074. }
  1075. static inline uint32_t GrColorTypeComponentFlags(GrColorType ct) {
  1076. switch (ct) {
  1077. case GrColorType::kUnknown: return 0;
  1078. case GrColorType::kAlpha_8: return kAlpha_SkColorTypeComponentFlag;
  1079. case GrColorType::kRGB_565: return kRGB_SkColorTypeComponentFlags;
  1080. case GrColorType::kABGR_4444: return kRGBA_SkColorTypeComponentFlags;
  1081. case GrColorType::kRGBA_8888: return kRGBA_SkColorTypeComponentFlags;
  1082. case GrColorType::kRGB_888x: return kRGB_SkColorTypeComponentFlags;
  1083. case GrColorType::kBGRA_8888: return kRGBA_SkColorTypeComponentFlags;
  1084. case GrColorType::kRGBA_1010102: return kRGBA_SkColorTypeComponentFlags;
  1085. case GrColorType::kGray_8: return kGray_SkColorTypeComponentFlag;
  1086. case GrColorType::kAlpha_F16: return kAlpha_SkColorTypeComponentFlag;
  1087. case GrColorType::kRGBA_F16: return kRGBA_SkColorTypeComponentFlags;
  1088. case GrColorType::kRG_F32: return kRed_SkColorTypeComponentFlag |
  1089. kGreen_SkColorTypeComponentFlag;
  1090. case GrColorType::kRGBA_F32: return kRGBA_SkColorTypeComponentFlags;
  1091. }
  1092. SK_ABORT("Invalid GrColorType");
  1093. return kUnknown_SkColorType;
  1094. }
  1095. static inline bool GrColorTypeIsAlphaOnly(GrColorType ct) {
  1096. return kAlpha_SkColorTypeComponentFlag == GrColorTypeComponentFlags(ct);
  1097. }
  1098. static inline bool GrColorTypeHasAlpha(GrColorType ct) {
  1099. return kAlpha_SkColorTypeComponentFlag & GrColorTypeComponentFlags(ct);
  1100. }
  1101. static inline int GrColorTypeBytesPerPixel(GrColorType ct) {
  1102. switch (ct) {
  1103. case GrColorType::kUnknown: return 0;
  1104. case GrColorType::kAlpha_8: return 1;
  1105. case GrColorType::kRGB_565: return 2;
  1106. case GrColorType::kABGR_4444: return 2;
  1107. case GrColorType::kRGBA_8888: return 4;
  1108. case GrColorType::kRGB_888x: return 4;
  1109. case GrColorType::kBGRA_8888: return 4;
  1110. case GrColorType::kRGBA_1010102: return 4;
  1111. case GrColorType::kGray_8: return 1;
  1112. case GrColorType::kAlpha_F16: return 2;
  1113. case GrColorType::kRGBA_F16: return 8;
  1114. case GrColorType::kRG_F32: return 8;
  1115. case GrColorType::kRGBA_F32: return 16;
  1116. }
  1117. SK_ABORT("Invalid GrColorType");
  1118. return 0;
  1119. }
  1120. static inline GrColorType GrPixelConfigToColorTypeAndEncoding(GrPixelConfig config,
  1121. GrSRGBEncoded* srgbEncoded) {
  1122. SkASSERT(srgbEncoded);
  1123. switch (config) {
  1124. case kUnknown_GrPixelConfig:
  1125. return GrColorType::kUnknown;
  1126. case kAlpha_8_GrPixelConfig:
  1127. *srgbEncoded = GrSRGBEncoded::kNo;
  1128. return GrColorType::kAlpha_8;
  1129. case kGray_8_GrPixelConfig:
  1130. *srgbEncoded = GrSRGBEncoded::kNo;
  1131. return GrColorType::kGray_8;
  1132. case kRGB_565_GrPixelConfig:
  1133. *srgbEncoded = GrSRGBEncoded::kNo;
  1134. return GrColorType::kRGB_565;
  1135. case kRGBA_4444_GrPixelConfig:
  1136. *srgbEncoded = GrSRGBEncoded::kNo;
  1137. return GrColorType::kABGR_4444;
  1138. case kRGBA_8888_GrPixelConfig:
  1139. *srgbEncoded = GrSRGBEncoded::kNo;
  1140. return GrColorType::kRGBA_8888;
  1141. case kRGB_888_GrPixelConfig:
  1142. *srgbEncoded = GrSRGBEncoded::kNo;
  1143. return GrColorType::kRGB_888x;
  1144. case kBGRA_8888_GrPixelConfig:
  1145. *srgbEncoded = GrSRGBEncoded::kNo;
  1146. return GrColorType::kBGRA_8888;
  1147. case kSRGBA_8888_GrPixelConfig:
  1148. *srgbEncoded = GrSRGBEncoded::kYes;
  1149. return GrColorType::kRGBA_8888;
  1150. case kSBGRA_8888_GrPixelConfig:
  1151. *srgbEncoded = GrSRGBEncoded::kYes;
  1152. return GrColorType::kBGRA_8888;
  1153. case kRGBA_1010102_GrPixelConfig:
  1154. *srgbEncoded = GrSRGBEncoded::kNo;
  1155. return GrColorType::kRGBA_1010102;
  1156. case kRGBA_float_GrPixelConfig:
  1157. *srgbEncoded = GrSRGBEncoded::kNo;
  1158. return GrColorType::kRGBA_F32;
  1159. case kRG_float_GrPixelConfig:
  1160. *srgbEncoded = GrSRGBEncoded::kNo;
  1161. return GrColorType::kRG_F32;
  1162. case kAlpha_half_GrPixelConfig:
  1163. *srgbEncoded = GrSRGBEncoded::kNo;
  1164. return GrColorType::kAlpha_F16;
  1165. case kRGBA_half_GrPixelConfig:
  1166. *srgbEncoded = GrSRGBEncoded::kNo;
  1167. return GrColorType::kRGBA_F16;
  1168. case kAlpha_8_as_Alpha_GrPixelConfig:
  1169. *srgbEncoded = GrSRGBEncoded::kNo;
  1170. return GrColorType::kAlpha_8;
  1171. case kAlpha_8_as_Red_GrPixelConfig:
  1172. *srgbEncoded = GrSRGBEncoded::kNo;
  1173. return GrColorType::kAlpha_8;
  1174. case kAlpha_half_as_Red_GrPixelConfig:
  1175. *srgbEncoded = GrSRGBEncoded::kNo;
  1176. return GrColorType::kAlpha_F16;
  1177. case kGray_8_as_Lum_GrPixelConfig:
  1178. *srgbEncoded = GrSRGBEncoded::kNo;
  1179. return GrColorType::kGray_8;
  1180. case kGray_8_as_Red_GrPixelConfig:
  1181. *srgbEncoded = GrSRGBEncoded::kNo;
  1182. return GrColorType::kGray_8;
  1183. }
  1184. SK_ABORT("Invalid GrPixelConfig");
  1185. return GrColorType::kUnknown;
  1186. }
  1187. static inline GrColorType GrPixelConfigToColorType(GrPixelConfig config) {
  1188. GrSRGBEncoded bogusEncoded;
  1189. return GrPixelConfigToColorTypeAndEncoding(config, &bogusEncoded);
  1190. }
  1191. static inline GrPixelConfig GrColorTypeToPixelConfig(GrColorType config,
  1192. GrSRGBEncoded srgbEncoded) {
  1193. switch (config) {
  1194. case GrColorType::kUnknown:
  1195. return kUnknown_GrPixelConfig;
  1196. case GrColorType::kAlpha_8:
  1197. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1198. : kAlpha_8_GrPixelConfig;
  1199. case GrColorType::kGray_8:
  1200. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1201. : kGray_8_GrPixelConfig;
  1202. case GrColorType::kRGB_565:
  1203. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1204. : kRGB_565_GrPixelConfig;
  1205. case GrColorType::kABGR_4444:
  1206. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1207. : kRGBA_4444_GrPixelConfig;
  1208. case GrColorType::kRGBA_8888:
  1209. return (GrSRGBEncoded::kYes == srgbEncoded) ? kSRGBA_8888_GrPixelConfig
  1210. : kRGBA_8888_GrPixelConfig;
  1211. case GrColorType::kRGB_888x:
  1212. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1213. : kRGB_888_GrPixelConfig;
  1214. case GrColorType::kBGRA_8888:
  1215. return (GrSRGBEncoded::kYes == srgbEncoded) ? kSBGRA_8888_GrPixelConfig
  1216. : kBGRA_8888_GrPixelConfig;
  1217. case GrColorType::kRGBA_1010102:
  1218. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1219. : kRGBA_1010102_GrPixelConfig;
  1220. case GrColorType::kRGBA_F32:
  1221. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1222. : kRGBA_float_GrPixelConfig;
  1223. case GrColorType::kRG_F32:
  1224. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1225. : kRG_float_GrPixelConfig;
  1226. case GrColorType::kAlpha_F16:
  1227. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1228. : kAlpha_half_GrPixelConfig;
  1229. case GrColorType::kRGBA_F16:
  1230. return (GrSRGBEncoded::kYes == srgbEncoded) ? kUnknown_GrPixelConfig
  1231. : kRGBA_half_GrPixelConfig;
  1232. }
  1233. SK_ABORT("Invalid GrColorType");
  1234. return kUnknown_GrPixelConfig;
  1235. }
  1236. class GrReleaseProcHelper : public SkWeakRefCnt {
  1237. public:
  1238. // These match the definitions in SkImage, from whence they came
  1239. typedef void* ReleaseCtx;
  1240. typedef void (*ReleaseProc)(ReleaseCtx);
  1241. GrReleaseProcHelper(ReleaseProc proc, ReleaseCtx ctx) : fReleaseProc(proc), fReleaseCtx(ctx) {}
  1242. ~GrReleaseProcHelper() override {}
  1243. void weak_dispose() const override {
  1244. fReleaseProc(fReleaseCtx);
  1245. }
  1246. private:
  1247. ReleaseProc fReleaseProc;
  1248. ReleaseCtx fReleaseCtx;
  1249. };
  1250. #endif