@@ -1253,6 +1253,149 @@ static constexpr uint32_t GrColorTypeComponentFlags(GrColorType ct) {
12531253 SkUNREACHABLE;
12541254}
12551255
1256+ /* *
1257+ * Describes the encoding of channel data in a GrColorType.
1258+ */
1259+ enum class GrColorTypeEncoding {
1260+ kUnorm ,
1261+ // kSRGBUnorm,
1262+ // kSnorm,
1263+ kFloat ,
1264+ // kSint
1265+ // kUint
1266+ };
1267+
1268+ /* *
1269+ * Describes a GrColorType by how many bits are used for each color component and how they are
1270+ * encoded. Currently all the non-zero channels share a single GrColorTypeEncoding. This could be
1271+ * expanded to store separate encodings and to indicate which bits belong to which components.
1272+ */
1273+ struct GrColorTypeDesc {
1274+ public:
1275+ static constexpr GrColorTypeDesc MakeRGBA (int rgba, GrColorTypeEncoding e) {
1276+ return {rgba, rgba, rgba, rgba, 0 , e};
1277+ }
1278+
1279+ static constexpr GrColorTypeDesc MakeRGBA (int rgb, int a, GrColorTypeEncoding e) {
1280+ return {rgb, rgb, rgb, a, 0 , e};
1281+ }
1282+
1283+ static constexpr GrColorTypeDesc MakeRGB (int rgb, GrColorTypeEncoding e) {
1284+ return {rgb, rgb, rgb, 0 , 0 , e};
1285+ }
1286+
1287+ static constexpr GrColorTypeDesc MakeRGB (int r, int g, int b, GrColorTypeEncoding e) {
1288+ return {r, g, b, 0 , 0 , e};
1289+ }
1290+
1291+ static constexpr GrColorTypeDesc MakeAlpha (int a, GrColorTypeEncoding e) {
1292+ return {0 , 0 , 0 , a, 0 , e};
1293+ }
1294+
1295+ static constexpr GrColorTypeDesc MakeR (int r, GrColorTypeEncoding e) {
1296+ return {r, 0 , 0 , 0 , 0 , e};
1297+ }
1298+
1299+ static constexpr GrColorTypeDesc MakeRG (int rg, GrColorTypeEncoding e) {
1300+ return {rg, rg, 0 , 0 , 0 , e};
1301+ }
1302+
1303+ static constexpr GrColorTypeDesc MakeGray (int grayBits, GrColorTypeEncoding e) {
1304+ return {0 , 0 , 0 , 0 , grayBits, e};
1305+ }
1306+
1307+ static constexpr GrColorTypeDesc MakeInvalid () { return {}; }
1308+
1309+ constexpr int r () const { return fRBits ; }
1310+ constexpr int g () const { return fGBits ; }
1311+ constexpr int b () const { return fBBits ; }
1312+ constexpr int a () const { return fABits ; }
1313+
1314+ constexpr int gray () const { return fGrayBits ; }
1315+
1316+ constexpr GrColorTypeEncoding encoding () const { return fEncoding ; }
1317+
1318+ private:
1319+ int fRBits = 0 ;
1320+ int fGBits = 0 ;
1321+ int fBBits = 0 ;
1322+ int fABits = 0 ;
1323+ int fGrayBits = 0 ;
1324+ GrColorTypeEncoding fEncoding = GrColorTypeEncoding::kUnorm ;
1325+
1326+ constexpr GrColorTypeDesc () = default;
1327+
1328+ constexpr GrColorTypeDesc (int r, int g, int b, int a, int gray, GrColorTypeEncoding encoding)
1329+ : fRBits(r), fGBits(g), fBBits(b), fABits(a), fGrayBits(gray), fEncoding(encoding) {
1330+ SkASSERT (r >= 0 && g >= 0 && b >= 0 && a >= 0 && gray >= 0 );
1331+ SkASSERT (!gray || (!r && !g && !b));
1332+ SkASSERT (r || g || b || a || gray);
1333+ }
1334+ };
1335+
1336+ static constexpr GrColorTypeDesc GrGetColorTypeDesc (GrColorType ct) {
1337+ switch (ct) {
1338+ case GrColorType::kUnknown :
1339+ return GrColorTypeDesc::MakeInvalid ();
1340+ case GrColorType::kAlpha_8 :
1341+ return GrColorTypeDesc::MakeAlpha (8 , GrColorTypeEncoding::kUnorm );
1342+ case GrColorType::kBGR_565 :
1343+ return GrColorTypeDesc::MakeRGB (5 , 6 , 5 , GrColorTypeEncoding::kUnorm );
1344+ case GrColorType::kABGR_4444 :
1345+ return GrColorTypeDesc::MakeRGBA (4 , GrColorTypeEncoding::kUnorm );
1346+ case GrColorType::kRGBA_8888 :
1347+ return GrColorTypeDesc::MakeRGBA (8 , GrColorTypeEncoding::kUnorm );
1348+ case GrColorType::kRGB_888x :
1349+ return GrColorTypeDesc::MakeRGB (8 , GrColorTypeEncoding::kUnorm );
1350+ case GrColorType::kRG_88 :
1351+ return GrColorTypeDesc::MakeRG (8 , GrColorTypeEncoding::kUnorm );
1352+ case GrColorType::kBGRA_8888 :
1353+ return GrColorTypeDesc::MakeRGBA (8 , GrColorTypeEncoding::kUnorm );
1354+ case GrColorType::kRGBA_1010102 :
1355+ return GrColorTypeDesc::MakeRGBA (10 , 2 , GrColorTypeEncoding::kUnorm );
1356+ case GrColorType::kGray_8 :
1357+ return GrColorTypeDesc::MakeGray (8 , GrColorTypeEncoding::kUnorm );
1358+ case GrColorType::kAlpha_F16 :
1359+ return GrColorTypeDesc::MakeAlpha (16 , GrColorTypeEncoding::kFloat );
1360+ case GrColorType::kRGBA_F16 :
1361+ return GrColorTypeDesc::MakeRGBA (16 , GrColorTypeEncoding::kFloat );
1362+ case GrColorType::kRGBA_F16_Clamped :
1363+ return GrColorTypeDesc::MakeRGBA (16 , GrColorTypeEncoding::kFloat );
1364+ case GrColorType::kRG_F32 :
1365+ return GrColorTypeDesc::MakeRG (32 , GrColorTypeEncoding::kFloat );
1366+ case GrColorType::kRGBA_F32 :
1367+ return GrColorTypeDesc::MakeRGBA (32 , GrColorTypeEncoding::kFloat );
1368+ case GrColorType::kR_16 :
1369+ return GrColorTypeDesc::MakeR (16 , GrColorTypeEncoding::kUnorm );
1370+ case GrColorType::kRG_1616 :
1371+ return GrColorTypeDesc::MakeRG (16 , GrColorTypeEncoding::kUnorm );
1372+ case GrColorType::kRGBA_16161616 :
1373+ return GrColorTypeDesc::MakeRGBA (16 , GrColorTypeEncoding::kUnorm );
1374+ case GrColorType::kRG_F16 :
1375+ return GrColorTypeDesc::MakeRG (16 , GrColorTypeEncoding::kFloat );
1376+ }
1377+ SkUNREACHABLE;
1378+ }
1379+
1380+ static constexpr GrClampType GrColorTypeClampType (GrColorType colorType) {
1381+ if (GrGetColorTypeDesc (colorType).encoding () == GrColorTypeEncoding::kUnorm ) {
1382+ return GrClampType::kAuto ;
1383+ }
1384+ return GrColorType::kRGBA_F16_Clamped == colorType ? GrClampType::kManual : GrClampType::kNone ;
1385+ }
1386+
1387+ // Consider a color type "wider" than n if it has more than n bits for any its representable
1388+ // channels.
1389+ static constexpr bool GrColorTypeIsWiderThan (GrColorType colorType, int n) {
1390+ SkASSERT (n > 0 );
1391+ auto desc = GrGetColorTypeDesc (colorType);
1392+ return (desc.r () && desc.r () > n )||
1393+ (desc.g () && desc.g () > n) ||
1394+ (desc.b () && desc.b () > n) ||
1395+ (desc.a () && desc.a () > n) ||
1396+ (desc.gray () && desc.gray () > n);
1397+ }
1398+
12561399static constexpr bool GrColorTypeIsAlphaOnly (GrColorType ct) {
12571400 return kAlpha_SkColorTypeComponentFlag == GrColorTypeComponentFlags (ct);
12581401}
0 commit comments