Skia
2D Graphics Library
SkYUVAPixmaps.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Google LLC
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 SkYUVAPixmaps_DEFINED
9 #define SkYUVAPixmaps_DEFINED
10 
11 #include "include/core/SkData.h"
13 #include "include/core/SkPixmap.h"
15 #include "include/private/SkTo.h"
16 
17 #include <array>
18 #include <bitset>
19 
20 class GrImageContext;
21 
27 public:
28  static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
29 
32 
37  enum class DataType {
38  kUnorm8,
39  kUnorm16,
40  kFloat16,
41  kUnorm10_Unorm2,
42 
43  kLast = kUnorm10_Unorm2
44  };
45  static constexpr int kDataTypeCnt = static_cast<int>(DataType::kLast) + 1;
46 
48  public:
50  constexpr SupportedDataTypes() = default;
51 
53  SupportedDataTypes(const GrImageContext&);
54 
56  static constexpr SupportedDataTypes All();
57 
62  constexpr bool supported(PlaneConfig, DataType) const;
63 
68  void enableDataType(DataType, int numChannels);
69 
70  private:
71  // The bit for DataType dt with n channels is at index kDataTypeCnt*(n-1) + dt.
72  std::bitset<kDataTypeCnt*4> fDataTypeSupport = {};
73  };
74 
79  static constexpr SkColorType DefaultColorTypeForDataType(DataType dataType, int numChannels);
80 
87  static std::tuple<int, DataType> NumChannelsAndDataType(SkColorType);
88 
90  SkYUVAPixmapInfo() = default;
91 
102  const SkColorType[kMaxPlanes],
103  const size_t rowBytes[kMaxPlanes]);
108  SkYUVAPixmapInfo(const SkYUVAInfo&, DataType, const size_t rowBytes[kMaxPlanes]);
109 
111 
113 
114  bool operator==(const SkYUVAPixmapInfo&) const;
115  bool operator!=(const SkYUVAPixmapInfo& that) const { return !(*this == that); }
116 
117  const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
118 
119  SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
120 
122  int numPlanes() const { return fYUVAInfo.numPlanes(); }
123 
125  DataType dataType() const { return fDataType; }
126 
131  size_t rowBytes(int i) const { return fRowBytes[static_cast<size_t>(i)]; }
132 
134  const SkImageInfo& planeInfo(int i) const { return fPlaneInfos[static_cast<size_t>(i)]; }
135 
141  size_t computeTotalBytes(size_t planeSizes[kMaxPlanes] = nullptr) const;
142 
148  bool initPixmapsFromSingleAllocation(void* memory, SkPixmap pixmaps[kMaxPlanes]) const;
149 
154  bool isValid() const { return fYUVAInfo.isValid(); }
155 
157  bool isSupported(const SupportedDataTypes&) const;
158 
159 private:
160  SkYUVAInfo fYUVAInfo;
161  std::array<SkImageInfo, kMaxPlanes> fPlaneInfos = {};
162  std::array<size_t, kMaxPlanes> fRowBytes = {};
163  DataType fDataType = DataType::kUnorm8;
164  static_assert(kUnknown_SkColorType == 0, "default init isn't kUnknown");
165 };
166 
172 public:
174  static constexpr auto kMaxPlanes = SkYUVAPixmapInfo::kMaxPlanes;
175 
177 
179  static SkYUVAPixmaps Allocate(const SkYUVAPixmapInfo& yuvaPixmapInfo);
180 
186 
192 
198  static SkYUVAPixmaps FromExternalMemory(const SkYUVAPixmapInfo&, void* memory);
199 
206  static SkYUVAPixmaps FromExternalPixmaps(const SkYUVAInfo&, const SkPixmap[kMaxPlanes]);
207 
209  SkYUVAPixmaps() = default;
210  ~SkYUVAPixmaps() = default;
211 
212  SkYUVAPixmaps(SkYUVAPixmaps&& that) = default;
214  SkYUVAPixmaps(const SkYUVAPixmaps&) = default;
215  SkYUVAPixmaps& operator=(const SkYUVAPixmaps& that) = default;
216 
218  bool isValid() const { return !fYUVAInfo.dimensions().isEmpty(); }
219 
220  const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
221 
222  DataType dataType() const { return fDataType; }
223 
225 
227  int numPlanes() const { return this->isValid() ? fYUVAInfo.numPlanes() : 0; }
228 
233  const std::array<SkPixmap, kMaxPlanes>& planes() const { return fPlanes; }
234 
239  const SkPixmap& plane(int i) const { return fPlanes[SkToSizeT(i)]; }
240 
246 
248  bool ownsStorage() const { return SkToBool(fData); }
249 
250 private:
252  SkYUVAPixmaps(const SkYUVAInfo&, DataType, const SkPixmap[kMaxPlanes]);
253 
254  std::array<SkPixmap, kMaxPlanes> fPlanes = {};
255  sk_sp<SkData> fData;
256  SkYUVAInfo fYUVAInfo;
257  DataType fDataType;
258 };
259 
261 
263  using ULL = unsigned long long; // bitset cons. takes this.
264  ULL bits = 0;
265  for (ULL c = 1; c <= 4; ++c) {
266  for (ULL dt = 0; dt <= ULL(kDataTypeCnt); ++dt) {
267  if (DefaultColorTypeForDataType(static_cast<DataType>(dt),
268  static_cast<int>(c)) != kUnknown_SkColorType) {
269  bits |= ULL(1) << (dt + static_cast<ULL>(kDataTypeCnt)*(c - 1));
270  }
271  }
272  }
273  SupportedDataTypes combinations;
274  combinations.fDataTypeSupport = bits;
275  return combinations;
276 }
277 
279  DataType type) const {
280  int n = SkYUVAInfo::NumPlanes(config);
281  for (int i = 0; i < n; ++i) {
282  auto c = static_cast<size_t>(SkYUVAInfo::NumChannelsInPlane(config, i));
283  SkASSERT(c >= 1 && c <= 4);
284  if (!fDataTypeSupport[static_cast<size_t>(type) +
285  (c - 1)*static_cast<size_t>(kDataTypeCnt)]) {
286  return false;
287  }
288  }
289  return true;
290 }
291 
293  int numChannels) {
294  switch (numChannels) {
295  case 1:
296  switch (dataType) {
301  }
302  break;
303  case 2:
304  switch (dataType) {
309  }
310  break;
311  case 3:
312  // None of these are tightly packed. The intended use case is for interleaved YUVA
313  // planes where we're forcing opaqueness by ignoring the alpha values.
314  // There are "x" rather than "A" variants for Unorm8 and Unorm10_Unorm2 but we don't
315  // choose them because 1) there is no inherent advantage and 2) there is better support
316  // in the GPU backend for the "A" versions.
317  switch (dataType) {
322  }
323  break;
324  case 4:
325  switch (dataType) {
330  }
331  break;
332  }
333  return kUnknown_SkColorType;
334 }
335 
336 #endif
SkYUVAPixmapInfo::SupportedDataTypes::All
static constexpr SupportedDataTypes All()
All legal combinations of PlaneConfig and DataType are supported.
Definition: SkYUVAPixmaps.h:262
kR16G16_unorm_SkColorType
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkImageInfo.h:90
SkYUVAInfo::NumPlanes
static constexpr int NumPlanes(PlaneConfig)
Number of planes for a given PlaneConfig.
Definition: SkYUVAInfo.h:249
kGray_8_SkColorType
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkImageInfo.h:76
SkYUVAPixmaps
Helper to store SkPixmap planes as described by a SkYUVAPixmapInfo.
Definition: SkYUVAPixmaps.h:171
SkYUVAPixmaps::numPlanes
int numPlanes() const
Number of pixmap planes or 0 if this SkYUVAPixmaps is invalid.
Definition: SkYUVAPixmaps.h:227
SkYUVAPixmaps::pixmapsInfo
SkYUVAPixmapInfo pixmapsInfo() const
SkYUVColorSpace
SkYUVColorSpace
Definition: SkImageInfo.h:148
SkYUVAPixmapInfo::operator!=
bool operator!=(const SkYUVAPixmapInfo &that) const
Definition: SkYUVAPixmaps.h:115
SkYUVAInfo.h
SkYUVAPixmaps::Allocate
static SkYUVAPixmaps Allocate(const SkYUVAPixmapInfo &yuvaPixmapInfo)
Allocate space for pixmaps' pixels in the SkYUVAPixmaps.
SkYUVAPixmapInfo::isValid
bool isValid() const
Returns true if this has been configured with a non-empty dimensioned SkYUVAInfo with compatible colo...
Definition: SkYUVAPixmaps.h:154
SkYUVAPixmapInfo::SupportedDataTypes::SupportedDataTypes
constexpr SupportedDataTypes()=default
Defaults to nothing supported.
kR16G16B16A16_unorm_SkColorType
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkImageInfo.h:91
SkYUVAPixmapInfo::initPixmapsFromSingleAllocation
bool initPixmapsFromSingleAllocation(void *memory, SkPixmap pixmaps[kMaxPlanes]) const
Takes an allocation that is assumed to be at least computeTotalBytes() in size and configures the fir...
SkYUVAPixmaps::~SkYUVAPixmaps
~SkYUVAPixmaps()=default
SkYUVAPixmapInfo::SupportedDataTypes::supported
constexpr bool supported(PlaneConfig, DataType) const
Checks whether there is a supported combination of color types for planes structured as indicated by ...
Definition: SkYUVAPixmaps.h:278
SkPixmap.h
SkYUVAPixmapInfo::SkYUVAPixmapInfo
SkYUVAPixmapInfo(const SkYUVAPixmapInfo &)=default
SkYUVAPixmapInfo::DataType
DataType
Data type for Y, U, V, and possibly A channels independent of how values are packed into planes.
Definition: SkYUVAPixmaps.h:37
SkYUVAPixmapInfo::kMaxPlanes
static constexpr auto kMaxPlanes
Definition: SkYUVAPixmaps.h:28
SkYUVAPixmapInfo::DataType::kUnorm16
@ kUnorm16
16 bit unsigned normalized
SkYUVAPixmapInfo::computeTotalBytes
size_t computeTotalBytes(size_t planeSizes[kMaxPlanes]=nullptr) const
Determine size to allocate for all planes.
SkYUVAPixmaps::plane
const SkPixmap & plane(int i) const
Get the ith SkPixmap plane.
Definition: SkYUVAPixmaps.h:239
SkYUVAPixmapInfo::SupportedDataTypes
Definition: SkYUVAPixmaps.h:47
sk_sp< SkData >
SkYUVAPixmapInfo::DataType::kUnorm8
@ kUnorm8
8 bit unsigned normalized
kR16G16_float_SkColorType
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkImageInfo.h:87
SkYUVAPixmaps::SkYUVAPixmaps
SkYUVAPixmaps(SkYUVAPixmaps &&that)=default
SkYUVAPixmaps::RecommendedRGBAColorType
static SkColorType RecommendedRGBAColorType(DataType)
SkToBool
static constexpr bool SkToBool(const T &x)
Definition: SkTypes.h:497
SkYUVAInfo::Subsampling
Subsampling
UV subsampling is also specified in the enum value names using J:a:b notation (e.g.
Definition: SkYUVAInfo.h:69
SkYUVAPixmapInfo
SkYUVAInfo combined with per-plane SkColorTypes and row bytes.
Definition: SkYUVAPixmaps.h:26
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkYUVAPixmapInfo::rowBytes
size_t rowBytes(int i) const
Row bytes for the ith plane.
Definition: SkYUVAPixmaps.h:131
kRGBA_F16_SkColorType
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkImageInfo.h:79
SkImageInfo
Definition: SkImageInfo.h:283
bits
uint32_t bits
Definition: skcms.cc:51
SkYUVAPixmaps::toYUVALocations
SkYUVAInfo::YUVALocations toYUVALocations() const
Computes a YUVALocations representation of the planar layout.
SkYUVAInfo::NumChannelsInPlane
static constexpr int NumChannelsInPlane(PlaneConfig, int i)
Number of Y, U, V, A channels in the ith plane for a given PlaneConfig (or 0 if i is invalid).
Definition: SkYUVAInfo.h:268
SkYUVAPixmapInfo::operator==
bool operator==(const SkYUVAPixmapInfo &) const
SkData.h
SkYUVAPixmapInfo::NumChannelsAndDataType
static std::tuple< int, DataType > NumChannelsAndDataType(SkColorType)
If the SkColorType is supported for YUVA pixmaps this will return the number of YUVA channels that ca...
SkYUVAPixmaps::ownsStorage
bool ownsStorage() const
Does this SkPixmaps own the backing store of the planes?
Definition: SkYUVAPixmaps.h:248
SkYUVAPixmaps::operator=
SkYUVAPixmaps & operator=(SkYUVAPixmaps &&that)=default
SkYUVAPixmapInfo::SupportedDataTypes::SupportedDataTypes
SupportedDataTypes(const GrImageContext &)
Init based on texture formats supported by the context.
SkYUVAPixmaps::SkYUVAPixmaps
SkYUVAPixmaps()=default
Default SkYUVAPixmaps is invalid.
SkYUVAPixmapInfo::DataType::kUnorm10_Unorm2
@ kUnorm10_Unorm2
10 bit unorm for Y, U, and V. 2 bit unorm for alpha (if present).
kRGBA_1010102_SkColorType
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkImageInfo.h:72
SkYUVAPixmapInfo::SupportedDataTypes::enableDataType
void enableDataType(DataType, int numChannels)
Update to add support for pixmaps with numChannel channels where each channel is represented as DataT...
SkYUVAPixmaps::dataType
DataType dataType() const
Definition: SkYUVAPixmaps.h:222
SkYUVAPixmapInfo::yuvColorSpace
SkYUVColorSpace yuvColorSpace() const
Definition: SkYUVAPixmaps.h:119
kR8G8_unorm_SkColorType
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition: SkImageInfo.h:84
SkYUVAPixmapInfo::planeInfo
const SkImageInfo & planeInfo(int i) const
Image info for the ith plane, or default SkImageInfo if i >= numPlanes()
Definition: SkYUVAPixmaps.h:134
SkYUVAPixmaps::MakeCopy
static SkYUVAPixmaps MakeCopy(const SkYUVAPixmaps &src)
Makes a deep copy of the src SkYUVAPixmaps.
SkYUVAPixmaps::FromExternalMemory
static SkYUVAPixmaps FromExternalMemory(const SkYUVAPixmapInfo &, void *memory)
Use passed in memory as backing store for pixmaps' pixels.
SkYUVAPixmaps::planes
const std::array< SkPixmap, kMaxPlanes > & planes() const
Access the SkPixmap planes.
Definition: SkYUVAPixmaps.h:233
SkYUVAPixmapInfo::numPlanes
int numPlanes() const
The number of SkPixmap planes, 0 if this SkYUVAPixmapInfo is invalid.
Definition: SkYUVAPixmaps.h:122
kA16_unorm_SkColorType
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkImageInfo.h:89
SkYUVAPixmapInfo::isSupported
bool isSupported(const SupportedDataTypes &) const
Is this valid and does it use color types allowed by the passed SupportedDataTypes?
SkYUVAInfo::YUVALocations
std::array< YUVALocation, kYUVAChannelCount > YUVALocations
Definition: SkYUVAInfo.h:28
SkPixmap
Definition: SkPixmap.h:31
kRGBA_8888_SkColorType
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkImageInfo.h:69
kA16_float_SkColorType
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkImageInfo.h:86
SkYUVAPixmapInfo::DataType::kFloat16
@ kFloat16
16 bit (half) floating point
SkYUVAPixmapInfo::SkYUVAPixmapInfo
SkYUVAPixmapInfo()=default
Default SkYUVAPixmapInfo is invalid.
SkYUVAPixmapInfo::operator=
SkYUVAPixmapInfo & operator=(const SkYUVAPixmapInfo &)=default
SkYUVAPixmapInfo::kDataTypeCnt
static constexpr int kDataTypeCnt
Definition: SkYUVAPixmaps.h:45
SkYUVAInfo::kMaxPlanes
static constexpr int kMaxPlanes
Definition: SkYUVAInfo.h:94
SkYUVAPixmapInfo::SkYUVAPixmapInfo
SkYUVAPixmapInfo(const SkYUVAInfo &, DataType, const size_t rowBytes[kMaxPlanes])
Like above but uses DefaultColorTypeForDataType to determine each plane's SkColorType.
SkYUVAPixmaps::SkYUVAPixmaps
SkYUVAPixmaps(const SkYUVAPixmaps &)=default
SkYUVAPixmaps::yuvaInfo
const SkYUVAInfo & yuvaInfo() const
Definition: SkYUVAPixmaps.h:220
SkCoverageMode::kLast
@ kLast
SkYUVAPixmaps::isValid
bool isValid() const
Does have initialized pixmaps compatible with its SkYUVAInfo.
Definition: SkYUVAPixmaps.h:218
SkYUVAInfo
Specifies the structure of planes for a YUV image with optional alpha.
Definition: SkYUVAInfo.h:22
SkYUVAPixmaps::FromData
static SkYUVAPixmaps FromData(const SkYUVAPixmapInfo &, sk_sp< SkData >)
Use storage in SkData as backing store for pixmaps' pixels.
SkYUVAInfo::PlaneConfig
PlaneConfig
Specifies how YUV (and optionally A) are divided among planes.
Definition: SkYUVAInfo.h:43
SkYUVAPixmapInfo::DefaultColorTypeForDataType
static constexpr SkColorType DefaultColorTypeForDataType(DataType dataType, int numChannels)
Gets the default SkColorType to use with numChannels channels, each represented as DataType.
Definition: SkYUVAPixmaps.h:292
SkYUVAPixmapInfo::yuvaInfo
const SkYUVAInfo & yuvaInfo() const
Definition: SkYUVAPixmaps.h:117
SkYUVAPixmaps::FromExternalPixmaps
static SkYUVAPixmaps FromExternalPixmaps(const SkYUVAInfo &, const SkPixmap[kMaxPlanes])
Wraps existing SkPixmaps.
SkYUVAPixmapInfo::SkYUVAPixmapInfo
SkYUVAPixmapInfo(const SkYUVAInfo &, const SkColorType[kMaxPlanes], const size_t rowBytes[kMaxPlanes])
Initializes the SkYUVAPixmapInfo from a SkYUVAInfo with per-plane color types and row bytes.
SkColorType
SkColorType
Definition: SkImageInfo.h:64
kUnknown_SkColorType
@ kUnknown_SkColorType
uninitialized
Definition: SkImageInfo.h:65
SkYUVAPixmapInfo::dataType
DataType dataType() const
The per-YUV[A] channel data type.
Definition: SkYUVAPixmaps.h:125
SkImageInfo.h
SkYUVAPixmaps::operator=
SkYUVAPixmaps & operator=(const SkYUVAPixmaps &that)=default