| 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 |
#include "include/core/SkYUVAPixmaps.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/private/base/SkDebug.h" |
| 12 |
#include "src/base/SkRectMemcpy.h" |
| 13 |
#include "src/core/SkImageInfoPriv.h" |
| 14 |
#include "src/core/SkYUVAInfoLocation.h" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <cstdint> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
void SkYUVAPixmapInfo::SupportedDataTypes::enableDataType(DataType type, int numChannels) { |
| 21 |
if (numChannels < 1 || numChannels > 4) { |
| 22 |
return; |
| 23 |
} |
| 24 |
fDataTypeSupport[static_cast<size_t>(type) + (numChannels - 1)*kDataTypeCnt] = true; |
| 25 |
} |
| 26 |
|
| 27 |
////////////////////////////////////////////////////////////////////////////// |
| 28 |
|
| 29 |
std::tuple<int, SkYUVAPixmapInfo::DataType> SkYUVAPixmapInfo::NumChannelsAndDataType( |
| 30 |
SkColorType ct) { |
| 31 |
// We could allow BGR[A] color types, but then we'd have to decide whether B should be the 0th |
| 32 |
// or 2nd channel. Our docs currently say channel order is always R=0, G=1, B=2[, A=3]. |
| 33 |
switch (ct) { |
| 34 |
case kR8_unorm_SkColorType: |
| 35 |
case kAlpha_8_SkColorType: |
| 36 |
case kGray_8_SkColorType: return {1, DataType::kUnorm8 }; |
| 37 |
case kR16_unorm_SkColorType: |
| 38 |
case kA16_unorm_SkColorType: return {1, DataType::kUnorm16}; |
| 39 |
case kA16_float_SkColorType: return {1, DataType::kFloat16}; |
| 40 |
|
| 41 |
case kR8G8_unorm_SkColorType: return {2, DataType::kUnorm8 }; |
| 42 |
case kR16G16_unorm_SkColorType: return {2, DataType::kUnorm16 }; |
| 43 |
case kR16G16_float_SkColorType: return {2, DataType::kFloat16 }; |
| 44 |
|
| 45 |
case kRGB_888x_SkColorType: return {3, DataType::kUnorm8 }; |
| 46 |
case kRGB_101010x_SkColorType: return {3, DataType::kUnorm10_Unorm2 }; |
| 47 |
case kRGB_F16F16F16x_SkColorType: return {3, DataType::kFloat16 }; |
| 48 |
|
| 49 |
case kRGBA_8888_SkColorType: return {4, DataType::kUnorm8 }; |
| 50 |
case kR16G16B16A16_unorm_SkColorType: return {4, DataType::kUnorm16 }; |
| 51 |
case kRGBA_F16_SkColorType: return {4, DataType::kFloat16 }; |
| 52 |
case kRGBA_F16Norm_SkColorType: return {4, DataType::kFloat16 }; |
| 53 |
case kRGBA_1010102_SkColorType: return {4, DataType::kUnorm10_Unorm2 }; |
| 54 |
|
| 55 |
default: return {0, DataType::kUnorm8 }; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
SkYUVAPixmapInfo::SkYUVAPixmapInfo(const SkYUVAInfo& yuvaInfo, |
| 60 |
const SkColorType colorTypes[kMaxPlanes], |
| 61 |
const size_t rowBytes[kMaxPlanes]) |
| 62 |
: fYUVAInfo(yuvaInfo) { |
| 63 |
if (!yuvaInfo.isValid()) { |
| 64 |
*this = {}; |
| 65 |
SkASSERT(!this->isValid()); |
| 66 |
return; |
| 67 |
} |
| 68 |
SkISize planeDimensions[4]; |
| 69 |
int n = yuvaInfo.planeDimensions(planeDimensions); |
| 70 |
size_t tempRowBytes[kMaxPlanes]; |
| 71 |
if (!rowBytes) { |
| 72 |
for (int i = 0; i < n; ++i) { |
| 73 |
tempRowBytes[i] = SkColorTypeBytesPerPixel(colorTypes[i]) * planeDimensions[i].width(); |
| 74 |
} |
| 75 |
rowBytes = tempRowBytes; |
| 76 |
} |
| 77 |
bool ok = true; |
| 78 |
for (size_t i = 0; i < static_cast<size_t>(n); ++i) { |
| 79 |
fRowBytes[i] = rowBytes[i]; |
| 80 |
// Use kUnpremul so that we never multiply alpha when copying data in. |
| 81 |
fPlaneInfos[i] = SkImageInfo::Make(planeDimensions[i], |
| 82 |
colorTypes[i], |
| 83 |
kUnpremul_SkAlphaType); |
| 84 |
int numRequiredChannels = yuvaInfo.numChannelsInPlane(i); |
| 85 |
SkASSERT(numRequiredChannels > 0); |
| 86 |
auto [numColorTypeChannels, colorTypeDataType] = NumChannelsAndDataType(colorTypes[i]); |
| 87 |
ok &= i == 0 || colorTypeDataType == fDataType; |
| 88 |
ok &= numColorTypeChannels >= numRequiredChannels; |
| 89 |
ok &= fPlaneInfos[i].validRowBytes(fRowBytes[i]); |
| 90 |
fDataType = colorTypeDataType; |
| 91 |
} |
| 92 |
if (!ok) { |
| 93 |
*this = {}; |
| 94 |
SkASSERT(!this->isValid()); |
| 95 |
} else { |
| 96 |
SkASSERT(this->isValid()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
SkYUVAPixmapInfo::SkYUVAPixmapInfo(const SkYUVAInfo& yuvaInfo, |
| 101 |
DataType dataType, |
| 102 |
const size_t rowBytes[kMaxPlanes]) { |
| 103 |
SkColorType colorTypes[kMaxPlanes] = {}; |
| 104 |
int numPlanes = yuvaInfo.numPlanes(); |
| 105 |
for (int i = 0; i < numPlanes; ++i) { |
| 106 |
int numChannels = yuvaInfo.numChannelsInPlane(i); |
| 107 |
colorTypes[i] = DefaultColorTypeForDataType(dataType, numChannels); |
| 108 |
} |
| 109 |
*this = SkYUVAPixmapInfo(yuvaInfo, colorTypes, rowBytes); |
| 110 |
} |
| 111 |
|
| 112 |
bool SkYUVAPixmapInfo::operator==(const SkYUVAPixmapInfo& that) const { |
| 113 |
bool result = fYUVAInfo == that.fYUVAInfo && |
| 114 |
fPlaneInfos == that.fPlaneInfos && |
| 115 |
fRowBytes == that.fRowBytes; |
| 116 |
SkASSERT(!result || fDataType == that.fDataType); |
| 117 |
return result; |
| 118 |
} |
| 119 |
|
| 120 |
size_t SkYUVAPixmapInfo::computeTotalBytes(size_t planeSizes[kMaxPlanes]) const { |
| 121 |
if (!this->isValid()) { |
| 122 |
if (planeSizes) { |
| 123 |
std::fill_n(planeSizes, kMaxPlanes, 0); |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
return fYUVAInfo.computeTotalBytes(fRowBytes.data(), planeSizes); |
| 128 |
} |
| 129 |
|
| 130 |
bool SkYUVAPixmapInfo::initPixmapsFromSingleAllocation(void* memory, |
| 131 |
SkPixmap pixmaps[kMaxPlanes]) const { |
| 132 |
if (!this->isValid()) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
SkASSERT(pixmaps); |
| 136 |
char* addr = static_cast<char*>(memory); |
| 137 |
int n = this->numPlanes(); |
| 138 |
for (int i = 0; i < n; ++i) { |
| 139 |
SkASSERT(fPlaneInfos[i].validRowBytes(fRowBytes[i])); |
| 140 |
pixmaps[i].reset(fPlaneInfos[i], addr, fRowBytes[i]); |
| 141 |
size_t planeSize = pixmaps[i].rowBytes()*pixmaps[i].height(); |
| 142 |
SkASSERT(planeSize); |
| 143 |
addr += planeSize; |
| 144 |
} |
| 145 |
for (int i = n; i < kMaxPlanes; ++i) { |
| 146 |
pixmaps[i] = {}; |
| 147 |
} |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
bool SkYUVAPixmapInfo::isSupported(const SupportedDataTypes& supportedDataTypes) const { |
| 152 |
if (!this->isValid()) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
return supportedDataTypes.supported(fYUVAInfo.planeConfig(), fDataType); |
| 156 |
} |
| 157 |
|
| 158 |
////////////////////////////////////////////////////////////////////////////// |
| 159 |
|
| 160 |
SkColorType SkYUVAPixmaps::RecommendedRGBAColorType(DataType dataType) { |
| 161 |
switch (dataType) { |
| 162 |
case DataType::kUnorm8: return kRGBA_8888_SkColorType; |
| 163 |
// F16 has better GPU support than 16 bit unorm. Often "16" bit unorm values are actually |
| 164 |
// lower precision. |
| 165 |
case DataType::kUnorm16: return kRGBA_F16_SkColorType; |
| 166 |
case DataType::kFloat16: return kRGBA_F16_SkColorType; |
| 167 |
case DataType::kUnorm10_Unorm2: return kRGBA_1010102_SkColorType; |
| 168 |
} |
| 169 |
SkUNREACHABLE; |
| 170 |
} |
| 171 |
|
| 172 |
SkYUVAPixmaps SkYUVAPixmaps::Allocate(const SkYUVAPixmapInfo& yuvaPixmapInfo) { |
| 173 |
if (!yuvaPixmapInfo.isValid()) { |
| 174 |
return {}; |
| 175 |
} |
| 176 |
return SkYUVAPixmaps(yuvaPixmapInfo, |
| 177 |
SkData::MakeUninitialized(yuvaPixmapInfo.computeTotalBytes())); |
| 178 |
} |
| 179 |
|
| 180 |
SkYUVAPixmaps SkYUVAPixmaps::FromData(const SkYUVAPixmapInfo& yuvaPixmapInfo, sk_sp<SkData> data) { |
| 181 |
if (!yuvaPixmapInfo.isValid()) { |
| 182 |
return {}; |
| 183 |
} |
| 184 |
if (yuvaPixmapInfo.computeTotalBytes() > data->size()) { |
| 185 |
return {}; |
| 186 |
} |
| 187 |
return SkYUVAPixmaps(yuvaPixmapInfo, std::move(data)); |
| 188 |
} |
| 189 |
|
| 190 |
SkYUVAPixmaps SkYUVAPixmaps::MakeCopy(const SkYUVAPixmaps& src) { |
| 191 |
if (!src.isValid()) { |
| 192 |
return {}; |
| 193 |
} |
| 194 |
SkYUVAPixmaps result = Allocate(src.pixmapsInfo()); |
| 195 |
int n = result.numPlanes(); |
| 196 |
for (int i = 0; i < n; ++i) { |
| 197 |
// We use SkRectMemCpy rather than readPixels to ensure that we don't do any alpha type |
| 198 |
// conversion. |
| 199 |
const SkPixmap& s = src.plane(i); |
| 200 |
const SkPixmap& d = result.plane(i); |
| 201 |
SkRectMemcpy(d.writable_addr(), |
| 202 |
d.rowBytes(), |
| 203 |
s.addr(), |
| 204 |
s.rowBytes(), |
| 205 |
s.info().minRowBytes(), |
| 206 |
s.height()); |
| 207 |
} |
| 208 |
return result; |
| 209 |
} |
| 210 |
|
| 211 |
SkYUVAPixmaps SkYUVAPixmaps::FromExternalMemory(const SkYUVAPixmapInfo& yuvaPixmapInfo, |
| 212 |
void* memory) { |
| 213 |
if (!yuvaPixmapInfo.isValid()) { |
| 214 |
return {}; |
| 215 |
} |
| 216 |
SkPixmap pixmaps[kMaxPlanes]; |
| 217 |
yuvaPixmapInfo.initPixmapsFromSingleAllocation(memory, pixmaps); |
| 218 |
return SkYUVAPixmaps(yuvaPixmapInfo.yuvaInfo(), yuvaPixmapInfo.dataType(), pixmaps); |
| 219 |
} |
| 220 |
|
| 221 |
SkYUVAPixmaps SkYUVAPixmaps::FromExternalPixmaps(const SkYUVAInfo& yuvaInfo, |
| 222 |
const SkPixmap pixmaps[kMaxPlanes]) { |
| 223 |
SkColorType colorTypes[kMaxPlanes] = {}; |
| 224 |
size_t rowBytes[kMaxPlanes] = {}; |
| 225 |
int numPlanes = yuvaInfo.numPlanes(); |
| 226 |
for (int i = 0; i < numPlanes; ++i) { |
| 227 |
colorTypes[i] = pixmaps[i].colorType(); |
| 228 |
rowBytes[i] = pixmaps[i].rowBytes(); |
| 229 |
} |
| 230 |
SkYUVAPixmapInfo yuvaPixmapInfo(yuvaInfo, colorTypes, rowBytes); |
| 231 |
if (!yuvaPixmapInfo.isValid()) { |
| 232 |
return {}; |
| 233 |
} |
| 234 |
return SkYUVAPixmaps(yuvaInfo, yuvaPixmapInfo.dataType(), pixmaps); |
| 235 |
} |
| 236 |
|
| 237 |
SkYUVAPixmaps::SkYUVAPixmaps(const SkYUVAPixmapInfo& yuvaPixmapInfo, sk_sp<SkData> data) |
| 238 |
: fData(std::move(data)) |
| 239 |
, fYUVAInfo(yuvaPixmapInfo.yuvaInfo()) |
| 240 |
, fDataType(yuvaPixmapInfo.dataType()) { |
| 241 |
SkASSERT(yuvaPixmapInfo.isValid()); |
| 242 |
SkASSERT(yuvaPixmapInfo.computeTotalBytes() <= fData->size()); |
| 243 |
SkAssertResult(yuvaPixmapInfo.initPixmapsFromSingleAllocation(fData->writable_data(), |
| 244 |
fPlanes.data())); |
| 245 |
} |
| 246 |
|
| 247 |
SkYUVAPixmaps::SkYUVAPixmaps(const SkYUVAInfo& yuvaInfo, |
| 248 |
DataType dataType, |
| 249 |
const SkPixmap pixmaps[kMaxPlanes]) |
| 250 |
: fYUVAInfo(yuvaInfo), fDataType(dataType) { |
| 251 |
std::copy_n(pixmaps, yuvaInfo.numPlanes(), fPlanes.data()); |
| 252 |
} |
| 253 |
|
| 254 |
SkYUVAPixmapInfo SkYUVAPixmaps::pixmapsInfo() const { |
| 255 |
if (!this->isValid()) { |
| 256 |
return {}; |
| 257 |
} |
| 258 |
SkColorType colorTypes[kMaxPlanes] = {}; |
| 259 |
size_t rowBytes[kMaxPlanes] = {}; |
| 260 |
int numPlanes = this->numPlanes(); |
| 261 |
for (int i = 0; i < numPlanes; ++i) { |
| 262 |
colorTypes[i] = fPlanes[i].colorType(); |
| 263 |
rowBytes[i] = fPlanes[i].rowBytes(); |
| 264 |
} |
| 265 |
return {fYUVAInfo, colorTypes, rowBytes}; |
| 266 |
} |
| 267 |
|
| 268 |
SkYUVAInfo::YUVALocations SkYUVAPixmaps::toYUVALocations() const { |
| 269 |
uint32_t channelFlags[] = {SkColorTypeChannelFlags(fPlanes[0].colorType()), |
| 270 |
SkColorTypeChannelFlags(fPlanes[1].colorType()), |
| 271 |
SkColorTypeChannelFlags(fPlanes[2].colorType()), |
| 272 |
SkColorTypeChannelFlags(fPlanes[3].colorType())}; |
| 273 |
auto result = fYUVAInfo.toYUVALocations(channelFlags); |
| 274 |
SkDEBUGCODE(int numPlanes;) |
| 275 |
SkASSERT(SkYUVAInfo::YUVALocation::AreValidLocations(result, &numPlanes)); |
| 276 |
SkASSERT(numPlanes == this->numPlanes()); |
| 277 |
return result; |
| 278 |
} |