1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
* Copyright 2020 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkYUVAPixmaps_DEFINED
#define SkYUVAPixmaps_DEFINED
#include "include/core/SkColorType.h"
#include "include/core/SkData.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSize.h"
#include "include/core/SkTypes.h"
#include "include/core/SkYUVAInfo.h"
#include "include/private/base/SkTo.h"
#include <array>
#include <bitset>
#include <cstddef>
#include <tuple>
/**
* SkYUVAInfo combined with per-plane SkColorTypes and row bytes. Fully specifies the SkPixmaps
* for a YUVA image without the actual pixel memory and data.
*/
class SK_API SkYUVAPixmapInfo {
public:
static constexpr auto kMaxPlanes = SkYUVAInfo::kMaxPlanes;
using PlaneConfig = SkYUVAInfo::PlaneConfig;
using Subsampling = SkYUVAInfo::Subsampling;
/**
* Data type for Y, U, V, and possibly A channels independent of how values are packed into
* planes.
**/
enum class DataType {
kUnorm8, ///< 8 bit unsigned normalized
kUnorm16, ///< 16 bit unsigned normalized
kFloat16, ///< 16 bit (half) floating point
kUnorm10_Unorm2, ///< 10 bit unorm for Y, U, and V. 2 bit unorm for alpha (if present).
kLast = kUnorm10_Unorm2
};
static constexpr int kDataTypeCnt = static_cast<int>(DataType::kLast) + 1;
class SK_API SupportedDataTypes {
public:
/** Defaults to nothing supported. */
constexpr SupportedDataTypes() = default;
/** All legal combinations of PlaneConfig and DataType are supported. */
static constexpr SupportedDataTypes All();
/**
* Checks whether there is a supported combination of color types for planes structured
* as indicated by PlaneConfig with channel data types as indicated by DataType.
*/
constexpr bool supported(PlaneConfig, DataType) const;
/**
* Update to add support for pixmaps with numChannel channels where each channel is
* represented as DataType.
*/
void enableDataType(DataType, int numChannels);
private:
// The bit for DataType dt with n channels is at index kDataTypeCnt*(n-1) + dt.
std::bitset<kDataTypeCnt*4> fDataTypeSupport = {};
};
/**
* Gets the default SkColorType to use with numChannels channels, each represented as DataType.
* Returns kUnknown_SkColorType if no such color type.
*/
static constexpr SkColorType DefaultColorTypeForDataType(DataType dataType, int numChannels);
/**
* If the SkColorType is supported for YUVA pixmaps this will return the number of YUVA channels
* that can be stored in a plane of this color type and what the DataType is of those channels.
* If the SkColorType is not supported as a YUVA plane the number of channels is reported as 0
* and the DataType returned should be ignored.
*/
static std::tuple<int, DataType> NumChannelsAndDataType(SkColorType);
/** Default SkYUVAPixmapInfo is invalid. */
SkYUVAPixmapInfo() = default;
/**
* Initializes the SkYUVAPixmapInfo from a SkYUVAInfo with per-plane color types and row bytes.
* This will be invalid if the colorTypes aren't compatible with the SkYUVAInfo or if a
* rowBytes entry is not valid for the plane dimensions and color type. Color type and
* row byte values beyond the number of planes in SkYUVAInfo are ignored. All SkColorTypes
* must have the same DataType or this will be invalid.
*
* If rowBytes is nullptr then bpp*width is assumed for each plane.
*/
SkYUVAPixmapInfo(const SkYUVAInfo&,
const SkColorType[kMaxPlanes],
const size_t rowBytes[kMaxPlanes]);
/**
* Like above but uses DefaultColorTypeForDataType to determine each plane's SkColorType. If
* rowBytes is nullptr then bpp*width is assumed for each plane.
*/
SkYUVAPixmapInfo(const SkYUVAInfo&, DataType, const size_t rowBytes[kMaxPlanes]);
SkYUVAPixmapInfo(const SkYUVAPixmapInfo&) = default;
SkYUVAPixmapInfo& operator=(const SkYUVAPixmapInfo&) = default;
bool operator==(const SkYUVAPixmapInfo&) const;
bool operator!=(const SkYUVAPixmapInfo& that) const { return !(*this == that); }
const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
SkYUVColorSpace yuvColorSpace() const { return fYUVAInfo.yuvColorSpace(); }
/** The number of SkPixmap planes, 0 if this SkYUVAPixmapInfo is invalid. */
int numPlanes() const { return fYUVAInfo.numPlanes(); }
/** The per-YUV[A] channel data type. */
DataType dataType() const { return fDataType; }
/**
* Row bytes for the ith plane. Returns zero if i >= numPlanes() or this SkYUVAPixmapInfo is
* invalid.
*/
size_t rowBytes(int i) const { return fRowBytes[static_cast<size_t>(i)]; }
/** Image info for the ith plane, or default SkImageInfo if i >= numPlanes() */
const SkImageInfo& planeInfo(int i) const { return fPlaneInfos[static_cast<size_t>(i)]; }
/**
* Determine size to allocate for all planes. Optionally retrieves the per-plane sizes in
* planeSizes if not null. If total size overflows will return SIZE_MAX and set all planeSizes
* to SIZE_MAX. Returns 0 and fills planesSizes with 0 if this SkYUVAPixmapInfo is not valid.
*/
size_t computeTotalBytes(size_t planeSizes[kMaxPlanes] = nullptr) const;
/**
* Takes an allocation that is assumed to be at least computeTotalBytes() in size and configures
* the first numPlanes() entries in pixmaps array to point into that memory. The remaining
* entries of pixmaps are default initialized. Fails if this SkYUVAPixmapInfo not valid.
*/
bool initPixmapsFromSingleAllocation(void* memory, SkPixmap pixmaps[kMaxPlanes]) const;
/**
* Returns true if this has been configured with a non-empty dimensioned SkYUVAInfo with
* compatible color types and row bytes.
*/
bool isValid() const { return fYUVAInfo.isValid(); }
/** Is this valid and does it use color types allowed by the passed SupportedDataTypes? */
bool isSupported(const SupportedDataTypes&) const;
private:
SkYUVAInfo fYUVAInfo;
std::array<SkImageInfo, kMaxPlanes> fPlaneInfos = {};
std::array<size_t, kMaxPlanes> fRowBytes = {};
DataType fDataType = DataType::kUnorm8;
static_assert(kUnknown_SkColorType == 0, "default init isn't kUnknown");
};
/**
* Helper to store SkPixmap planes as described by a SkYUVAPixmapInfo. Can be responsible for
* allocating/freeing memory for pixmaps or use external memory.
*/
class SK_API SkYUVAPixmaps {
public:
using DataType = SkYUVAPixmapInfo::DataType;
static constexpr auto kMaxPlanes = SkYUVAPixmapInfo::kMaxPlanes;
static SkColorType RecommendedRGBAColorType(DataType);
/** Allocate space for pixmaps' pixels in the SkYUVAPixmaps. */
static SkYUVAPixmaps Allocate(const SkYUVAPixmapInfo& yuvaPixmapInfo);
/**
* Use storage in SkData as backing store for pixmaps' pixels. SkData is retained by the
* SkYUVAPixmaps.
*/
static SkYUVAPixmaps FromData(const SkYUVAPixmapInfo&, sk_sp<SkData>);
/**
* Makes a deep copy of the src SkYUVAPixmaps. The returned SkYUVAPixmaps owns its planes'
* backing stores.
*/
static SkYUVAPixmaps MakeCopy(const SkYUVAPixmaps& src);
/**
* Use passed in memory as backing store for pixmaps' pixels. Caller must ensure memory remains
* allocated while pixmaps are in use. There must be at least
* SkYUVAPixmapInfo::computeTotalBytes() allocated starting at memory.
*/
static SkYUVAPixmaps FromExternalMemory(const SkYUVAPixmapInfo&, void* memory);
/**
* Wraps existing SkPixmaps. The SkYUVAPixmaps will have no ownership of the SkPixmaps' pixel
* memory so the caller must ensure it remains valid. Will return an invalid SkYUVAPixmaps if
* the SkYUVAInfo isn't compatible with the SkPixmap array (number of planes, plane dimensions,
* sufficient color channels in planes, ...).
*/
static SkYUVAPixmaps FromExternalPixmaps(const SkYUVAInfo&, const SkPixmap[kMaxPlanes]);
/** Default SkYUVAPixmaps is invalid. */
SkYUVAPixmaps() = default;
~SkYUVAPixmaps() = default;
SkYUVAPixmaps(SkYUVAPixmaps&& that) = default;
SkYUVAPixmaps& operator=(SkYUVAPixmaps&& that) = default;
SkYUVAPixmaps(const SkYUVAPixmaps&) = default;
SkYUVAPixmaps& operator=(const SkYUVAPixmaps& that) = default;
/** Does have initialized pixmaps compatible with its SkYUVAInfo. */
bool isValid() const { return !fYUVAInfo.dimensions().isEmpty(); }
const SkYUVAInfo& yuvaInfo() const { return fYUVAInfo; }
DataType dataType() const { return fDataType; }
SkYUVAPixmapInfo pixmapsInfo() const;
/** Number of pixmap planes or 0 if this SkYUVAPixmaps is invalid. */
int numPlanes() const { return this->isValid() ? fYUVAInfo.numPlanes() : 0; }
/**
* Access the SkPixmap planes. They are default initialized if this is not a valid
* SkYUVAPixmaps.
*/
const std::array<SkPixmap, kMaxPlanes>& planes() const { return fPlanes; }
/**
* Get the ith SkPixmap plane. SkPixmap will be default initialized if i >= numPlanes or this
* SkYUVAPixmaps is invalid.
*/
const SkPixmap& plane(int i) const { return fPlanes[SkToSizeT(i)]; }
/**
* Computes a YUVALocations representation of the planar layout. The result is guaranteed to be
* valid if this->isValid().
*/
SkYUVAInfo::YUVALocations toYUVALocations() const;
/** Does this SkPixmaps own the backing store of the planes? */
bool ownsStorage() const { return SkToBool(fData); }
private:
SkYUVAPixmaps(const SkYUVAPixmapInfo&, sk_sp<SkData>);
SkYUVAPixmaps(const SkYUVAInfo&, DataType, const SkPixmap[kMaxPlanes]);
std::array<SkPixmap, kMaxPlanes> fPlanes = {};
sk_sp<SkData> fData;
SkYUVAInfo fYUVAInfo;
DataType fDataType;
};
//////////////////////////////////////////////////////////////////////////////
constexpr SkYUVAPixmapInfo::SupportedDataTypes SkYUVAPixmapInfo::SupportedDataTypes::All() {
using ULL = unsigned long long; // bitset cons. takes this.
ULL bits = 0;
for (ULL c = 1; c <= 4; ++c) {
for (ULL dt = 0; dt <= ULL(kDataTypeCnt); ++dt) {
if (DefaultColorTypeForDataType(static_cast<DataType>(dt),
static_cast<int>(c)) != kUnknown_SkColorType) {
bits |= ULL(1) << (dt + static_cast<ULL>(kDataTypeCnt)*(c - 1));
}
}
}
SupportedDataTypes combinations;
combinations.fDataTypeSupport = bits;
return combinations;
}
constexpr bool SkYUVAPixmapInfo::SupportedDataTypes::supported(PlaneConfig config,
DataType type) const {
int n = SkYUVAInfo::NumPlanes(config);
for (int i = 0; i < n; ++i) {
auto c = static_cast<size_t>(SkYUVAInfo::NumChannelsInPlane(config, i));
SkASSERT(c >= 1 && c <= 4);
if (!fDataTypeSupport[static_cast<size_t>(type) +
(c - 1)*static_cast<size_t>(kDataTypeCnt)]) {
return false;
}
}
return true;
}
constexpr SkColorType SkYUVAPixmapInfo::DefaultColorTypeForDataType(DataType dataType,
int numChannels) {
switch (numChannels) {
case 1:
switch (dataType) {
case DataType::kUnorm8: return kGray_8_SkColorType;
case DataType::kUnorm16: return kA16_unorm_SkColorType;
case DataType::kFloat16: return kA16_float_SkColorType;
case DataType::kUnorm10_Unorm2: return kUnknown_SkColorType;
}
break;
case 2:
switch (dataType) {
case DataType::kUnorm8: return kR8G8_unorm_SkColorType;
case DataType::kUnorm16: return kR16G16_unorm_SkColorType;
case DataType::kFloat16: return kR16G16_float_SkColorType;
case DataType::kUnorm10_Unorm2: return kUnknown_SkColorType;
}
break;
case 3:
// None of these are tightly packed. The intended use case is for interleaved YUVA
// planes where we're forcing opaqueness by ignoring the alpha values.
// There are "x" rather than "A" variants for Unorm8 and Unorm10_Unorm2 but we don't
// choose them because 1) there is no inherent advantage and 2) there is better support
// in the GPU backend for the "A" versions.
switch (dataType) {
case DataType::kUnorm8: return kRGBA_8888_SkColorType;
case DataType::kUnorm16: return kR16G16B16A16_unorm_SkColorType;
case DataType::kFloat16: return kRGBA_F16_SkColorType;
case DataType::kUnorm10_Unorm2: return kRGBA_1010102_SkColorType;
}
break;
case 4:
switch (dataType) {
case DataType::kUnorm8: return kRGBA_8888_SkColorType;
case DataType::kUnorm16: return kR16G16B16A16_unorm_SkColorType;
case DataType::kFloat16: return kRGBA_F16_SkColorType;
case DataType::kUnorm10_Unorm2: return kRGBA_1010102_SkColorType;
}
break;
}
return kUnknown_SkColorType;
}
#endif
|