| 1 |
/* |
| 2 |
* Copyright 2018 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 |
|
| 8 |
#include "src/gpu/ganesh/effects/GrYUVtoRGBEffect.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkSamplingOptions.h" |
| 14 |
#include "include/core/SkYUVAInfo.h" |
| 15 |
#include "include/private/SkSLSampleUsage.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "include/private/base/SkTo.h" |
| 18 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 19 |
#include "src/core/SkSLTypeShared.h" |
| 20 |
#include "src/core/SkYUVAInfoLocation.h" |
| 21 |
#include "src/core/SkYUVMath.h" |
| 22 |
#include "src/gpu/KeyBuilder.h" |
| 23 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 24 |
#include "src/gpu/ganesh/GrYUVATextureProxies.h" |
| 25 |
#include "src/gpu/ganesh/effects/GrMatrixEffect.h" |
| 26 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 27 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 28 |
#include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h" |
| 29 |
#include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h" |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <array> |
| 33 |
#include <cmath> |
| 34 |
#include <cstdint> |
| 35 |
#include <string> |
| 36 |
#include <utility> |
| 37 |
|
| 38 |
struct GrShaderCaps; |
| 39 |
|
| 40 |
static void border_colors(const GrYUVATextureProxies& yuvaProxies, float planeBorders[4][4]) { |
| 41 |
float m[20]; |
| 42 |
SkColorMatrix_RGB2YUV(yuvaProxies.yuvaInfo().yuvColorSpace(), m); |
| 43 |
for (int i = 0; i < SkYUVAInfo::kYUVAChannelCount; ++i) { |
| 44 |
auto [plane, channel] = yuvaProxies.yuvaLocations()[i]; |
| 45 |
if (plane == -1) { |
| 46 |
return; |
| 47 |
} |
| 48 |
auto c = static_cast<int>(channel); |
| 49 |
planeBorders[plane][c] = m[i*5 + 4]; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::Make(const GrYUVATextureProxies& yuvaProxies, |
| 54 |
GrSamplerState samplerState, |
| 55 |
const GrCaps& caps, |
| 56 |
const SkMatrix& localMatrix, |
| 57 |
const SkRect* subset, |
| 58 |
const SkRect* domain) { |
| 59 |
SkASSERT(!subset || SkRect::Make(yuvaProxies.yuvaInfo().dimensions()).contains(*subset)); |
| 60 |
|
| 61 |
int numPlanes = yuvaProxies.yuvaInfo().numPlanes(); |
| 62 |
if (!yuvaProxies.isValid()) { |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
bool usesBorder = samplerState.wrapModeX() == GrSamplerState::WrapMode::kClampToBorder || |
| 67 |
samplerState.wrapModeY() == GrSamplerState::WrapMode::kClampToBorder; |
| 68 |
float planeBorders[4][4] = {}; |
| 69 |
if (usesBorder) { |
| 70 |
border_colors(yuvaProxies, planeBorders); |
| 71 |
} |
| 72 |
|
| 73 |
bool snap[2] = {false, false}; |
| 74 |
std::unique_ptr<GrFragmentProcessor> planeFPs[SkYUVAInfo::kMaxPlanes]; |
| 75 |
for (int i = 0; i < numPlanes; ++i) { |
| 76 |
bool useSubset = SkToBool(subset); |
| 77 |
GrSurfaceProxyView view = yuvaProxies.makeView(i); |
| 78 |
SkMatrix planeMatrix = yuvaProxies.yuvaInfo().inverseOriginMatrix(); |
| 79 |
SkRect planeSubset; |
| 80 |
SkRect planeDomain; |
| 81 |
bool makeLinearWithSnap = false; |
| 82 |
auto [ssx, ssy] = yuvaProxies.yuvaInfo().planeSubsamplingFactors(i); |
| 83 |
SkASSERT(ssx > 0 && ssx <= 4); |
| 84 |
SkASSERT(ssy > 0 && ssy <= 2); |
| 85 |
float scaleX = 1.f; |
| 86 |
float scaleY = 1.f; |
| 87 |
if (ssx > 1 || ssy > 1) { |
| 88 |
scaleX = 1.f/ssx; |
| 89 |
scaleY = 1.f/ssy; |
| 90 |
// We would want to add a translation to this matrix to handle other sitings. |
| 91 |
SkASSERT(yuvaProxies.yuvaInfo().sitingX() == SkYUVAInfo::Siting::kCentered); |
| 92 |
SkASSERT(yuvaProxies.yuvaInfo().sitingY() == SkYUVAInfo::Siting::kCentered); |
| 93 |
planeMatrix.postConcat(SkMatrix::Scale(scaleX, scaleY)); |
| 94 |
if (subset) { |
| 95 |
planeSubset = {subset->fLeft *scaleX, |
| 96 |
subset->fTop *scaleY, |
| 97 |
subset->fRight *scaleX, |
| 98 |
subset->fBottom*scaleY}; |
| 99 |
} else { |
| 100 |
planeSubset = SkRect::Make(view.dimensions()); |
| 101 |
} |
| 102 |
if (domain) { |
| 103 |
planeDomain = {domain->fLeft *scaleX, |
| 104 |
domain->fTop *scaleY, |
| 105 |
domain->fRight *scaleX, |
| 106 |
domain->fBottom*scaleY}; |
| 107 |
} |
| 108 |
// If the image is not a multiple of the subsampling then the subsampled plane needs to |
| 109 |
// be tiled at less than its full width/height. This only matters when the mode is not |
| 110 |
// clamp. |
| 111 |
if (samplerState.wrapModeX() != GrSamplerState::WrapMode::kClamp) { |
| 112 |
int dx = (ssx*view.width() - yuvaProxies.yuvaInfo().width()); |
| 113 |
float maxRight = view.width() - dx*scaleX; |
| 114 |
if (planeSubset.fRight > maxRight) { |
| 115 |
planeSubset.fRight = maxRight; |
| 116 |
useSubset = true; |
| 117 |
} |
| 118 |
} |
| 119 |
if (samplerState.wrapModeY() != GrSamplerState::WrapMode::kClamp) { |
| 120 |
int dy = (ssy*view.height() - yuvaProxies.yuvaInfo().height()); |
| 121 |
float maxBottom = view.height() - dy*scaleY; |
| 122 |
if (planeSubset.fBottom > maxBottom) { |
| 123 |
planeSubset.fBottom = maxBottom; |
| 124 |
useSubset = true; |
| 125 |
} |
| 126 |
} |
| 127 |
// This promotion of nearest to linear filtering for UV planes exists to mimic |
| 128 |
// libjpeg[-turbo]'s do_fancy_upsampling option. We will filter the subsampled plane, |
| 129 |
// however we want to filter at a fixed point for each logical image pixel to simulate |
| 130 |
// nearest neighbor. |
| 131 |
if (samplerState.filter() == GrSamplerState::Filter::kNearest) { |
| 132 |
bool snapX = (ssx != 1), |
| 133 |
snapY = (ssy != 1); |
| 134 |
makeLinearWithSnap = snapX || snapY; |
| 135 |
snap[0] |= snapX; |
| 136 |
snap[1] |= snapY; |
| 137 |
if (domain) { |
| 138 |
// The outer YUVToRGB effect will ensure sampling happens at pixel centers |
| 139 |
// within this plane. |
| 140 |
planeDomain = {std::floor(planeDomain.fLeft) + 0.5f, |
| 141 |
std::floor(planeDomain.fTop) + 0.5f, |
| 142 |
std::floor(planeDomain.fRight) + 0.5f, |
| 143 |
std::floor(planeDomain.fBottom) + 0.5f}; |
| 144 |
} |
| 145 |
} |
| 146 |
} else { |
| 147 |
if (subset) { |
| 148 |
planeSubset = *subset; |
| 149 |
} |
| 150 |
if (domain) { |
| 151 |
planeDomain = *domain; |
| 152 |
} |
| 153 |
} |
| 154 |
if (useSubset) { |
| 155 |
if (makeLinearWithSnap) { |
| 156 |
// The plane is subsampled and we have an overall subset on the image. We're |
| 157 |
// emulating do_fancy_upsampling using linear filtering but snapping look ups to the |
| 158 |
// y-plane pixel centers. Consider a logical image pixel at the edge of the subset. |
| 159 |
// When computing the logical pixel color value we should use a 50/50 blend of two |
| 160 |
// values from the subsampled plane. Depending on where the subset edge falls in |
| 161 |
// actual subsampled plane, one of those values may come from outside the subset. |
| 162 |
// Hence, we use this custom inset factory which applies the wrap mode to |
| 163 |
// planeSubset but allows linear filtering to read pixels from the plane that are |
| 164 |
// just outside planeSubset. |
| 165 |
SkRect* domainRect = domain ? &planeDomain : nullptr; |
| 166 |
planeFPs[i] = GrTextureEffect::MakeCustomLinearFilterInset(std::move(view), |
| 167 |
kUnknown_SkAlphaType, |
| 168 |
planeMatrix, |
| 169 |
samplerState.wrapModeX(), |
| 170 |
samplerState.wrapModeY(), |
| 171 |
planeSubset, |
| 172 |
domainRect, |
| 173 |
{scaleX/2.f, scaleY/2.f}, |
| 174 |
caps, |
| 175 |
planeBorders[i]); |
| 176 |
} else if (domain) { |
| 177 |
planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view), |
| 178 |
kUnknown_SkAlphaType, |
| 179 |
planeMatrix, |
| 180 |
samplerState, |
| 181 |
planeSubset, |
| 182 |
planeDomain, |
| 183 |
caps, |
| 184 |
planeBorders[i]); |
| 185 |
} else { |
| 186 |
planeFPs[i] = GrTextureEffect::MakeSubset(std::move(view), |
| 187 |
kUnknown_SkAlphaType, |
| 188 |
planeMatrix, |
| 189 |
samplerState, |
| 190 |
planeSubset, |
| 191 |
caps, |
| 192 |
planeBorders[i]); |
| 193 |
} |
| 194 |
} else { |
| 195 |
GrSamplerState planeSampler = samplerState; |
| 196 |
if (makeLinearWithSnap) { |
| 197 |
planeSampler = GrSamplerState(samplerState.wrapModeX(), |
| 198 |
samplerState.wrapModeY(), |
| 199 |
GrSamplerState::Filter::kLinear, |
| 200 |
samplerState.mipmapMode()); |
| 201 |
} |
| 202 |
planeFPs[i] = GrTextureEffect::Make(std::move(view), |
| 203 |
kUnknown_SkAlphaType, |
| 204 |
planeMatrix, |
| 205 |
planeSampler, |
| 206 |
caps, |
| 207 |
planeBorders[i]); |
| 208 |
} |
| 209 |
} |
| 210 |
std::unique_ptr<GrFragmentProcessor> fp( |
| 211 |
new GrYUVtoRGBEffect(planeFPs, |
| 212 |
numPlanes, |
| 213 |
yuvaProxies.yuvaLocations(), |
| 214 |
snap, |
| 215 |
yuvaProxies.yuvaInfo().yuvColorSpace())); |
| 216 |
return GrMatrixEffect::Make(localMatrix, std::move(fp)); |
| 217 |
} |
| 218 |
|
| 219 |
static SkAlphaType alpha_type(const SkYUVAInfo::YUVALocations locations) { |
| 220 |
return locations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0 ? kPremul_SkAlphaType |
| 221 |
: kOpaque_SkAlphaType; |
| 222 |
} |
| 223 |
|
| 224 |
GrYUVtoRGBEffect::GrYUVtoRGBEffect(std::unique_ptr<GrFragmentProcessor> planeFPs[4], |
| 225 |
int numPlanes, |
| 226 |
const SkYUVAInfo::YUVALocations& locations, |
| 227 |
const bool snap[2], |
| 228 |
SkYUVColorSpace yuvColorSpace) |
| 229 |
: GrFragmentProcessor(kGrYUVtoRGBEffect_ClassID, |
| 230 |
ModulateForClampedSamplerOptFlags(alpha_type(locations))) |
| 231 |
, fLocations(locations) |
| 232 |
, fYUVColorSpace(yuvColorSpace) { |
| 233 |
std::copy_n(snap, 2, fSnap); |
| 234 |
|
| 235 |
if (fSnap[0] || fSnap[1]) { |
| 236 |
// Need this so that we can access coords in SKSL to perform snapping. |
| 237 |
this->setUsesSampleCoordsDirectly(); |
| 238 |
for (int i = 0; i < numPlanes; ++i) { |
| 239 |
this->registerChild(std::move(planeFPs[i]), SkSL::SampleUsage::Explicit()); |
| 240 |
} |
| 241 |
} else { |
| 242 |
for (int i = 0; i < numPlanes; ++i) { |
| 243 |
this->registerChild(std::move(planeFPs[i])); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
#if defined(GPU_TEST_UTILS) |
| 249 |
SkString GrYUVtoRGBEffect::onDumpInfo() const { |
| 250 |
SkString str("("); |
| 251 |
for (int i = 0; i < SkYUVAInfo::kYUVAChannelCount; ++i) { |
| 252 |
str.appendf("Locations[%d]=%d %d, ", |
| 253 |
i, fLocations[i].fPlane, static_cast<int>(fLocations[i].fChannel)); |
| 254 |
} |
| 255 |
str.appendf("YUVColorSpace=%d, snap=(%d, %d))", |
| 256 |
static_cast<int>(fYUVColorSpace), fSnap[0], fSnap[1]); |
| 257 |
return str; |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrYUVtoRGBEffect::onMakeProgramImpl() const { |
| 262 |
class Impl : public ProgramImpl { |
| 263 |
public: |
| 264 |
void emitCode(EmitArgs& args) override { |
| 265 |
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 266 |
const GrYUVtoRGBEffect& yuvEffect = args.fFp.cast<GrYUVtoRGBEffect>(); |
| 267 |
|
| 268 |
int numPlanes = yuvEffect.numChildProcessors(); |
| 269 |
|
| 270 |
const char* sampleCoords = ""; |
| 271 |
if (yuvEffect.fSnap[0] || yuvEffect.fSnap[1]) { |
| 272 |
fragBuilder->codeAppendf("float2 snappedCoords = %s;", args.fSampleCoord); |
| 273 |
if (yuvEffect.fSnap[0]) { |
| 274 |
fragBuilder->codeAppend("snappedCoords.x = floor(snappedCoords.x) + 0.5;"); |
| 275 |
} |
| 276 |
if (yuvEffect.fSnap[1]) { |
| 277 |
fragBuilder->codeAppend("snappedCoords.y = floor(snappedCoords.y) + 0.5;"); |
| 278 |
} |
| 279 |
sampleCoords = "snappedCoords"; |
| 280 |
} |
| 281 |
|
| 282 |
fragBuilder->codeAppendf("half4 color;"); |
| 283 |
const bool hasAlpha = yuvEffect.fLocations[SkYUVAInfo::YUVAChannels::kA].fPlane >= 0; |
| 284 |
|
| 285 |
for (int planeIdx = 0; planeIdx < numPlanes; ++planeIdx) { |
| 286 |
std::string colorChannel; |
| 287 |
std::string planeChannel; |
| 288 |
for (int locIdx = 0; locIdx < (hasAlpha ? 4 : 3); ++locIdx) { |
| 289 |
auto [yuvPlane, yuvChannel] = yuvEffect.fLocations[locIdx]; |
| 290 |
if (yuvPlane == planeIdx) { |
| 291 |
colorChannel.push_back("rgba"[locIdx]); |
| 292 |
planeChannel.push_back("rgba"[static_cast<int>(yuvChannel)]); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
SkASSERT(colorChannel.size() == planeChannel.size()); |
| 297 |
if (!colorChannel.empty()) { |
| 298 |
fragBuilder->codeAppendf( |
| 299 |
"color.%s = (%s).%s;", |
| 300 |
colorChannel.c_str(), |
| 301 |
this->invokeChild(planeIdx, args, sampleCoords).c_str(), |
| 302 |
planeChannel.c_str()); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (!hasAlpha) { |
| 307 |
fragBuilder->codeAppendf("color.a = 1;"); |
| 308 |
} |
| 309 |
|
| 310 |
if (kIdentity_SkYUVColorSpace != yuvEffect.fYUVColorSpace) { |
| 311 |
fColorSpaceMatrixVar = args.fUniformHandler->addUniform(&yuvEffect, |
| 312 |
kFragment_GrShaderFlag, SkSLType::kHalf3x3, "colorSpaceMatrix"); |
| 313 |
fColorSpaceTranslateVar = args.fUniformHandler->addUniform(&yuvEffect, |
| 314 |
kFragment_GrShaderFlag, SkSLType::kHalf3, "colorSpaceTranslate"); |
| 315 |
fragBuilder->codeAppendf( |
| 316 |
"color.rgb = saturate(color.rgb * %s + %s);", |
| 317 |
args.fUniformHandler->getUniformCStr(fColorSpaceMatrixVar), |
| 318 |
args.fUniformHandler->getUniformCStr(fColorSpaceTranslateVar)); |
| 319 |
} |
| 320 |
if (hasAlpha) { |
| 321 |
// premultiply alpha |
| 322 |
fragBuilder->codeAppendf("color.rgb *= color.a;"); |
| 323 |
} |
| 324 |
fragBuilder->codeAppendf("return color;"); |
| 325 |
} |
| 326 |
|
| 327 |
private: |
| 328 |
void onSetData(const GrGLSLProgramDataManager& pdman, |
| 329 |
const GrFragmentProcessor& proc) override { |
| 330 |
const GrYUVtoRGBEffect& yuvEffect = proc.cast<GrYUVtoRGBEffect>(); |
| 331 |
|
| 332 |
if (yuvEffect.fYUVColorSpace != kIdentity_SkYUVColorSpace) { |
| 333 |
SkASSERT(fColorSpaceMatrixVar.isValid()); |
| 334 |
float yuvM[20]; |
| 335 |
SkColorMatrix_YUV2RGB(yuvEffect.fYUVColorSpace, yuvM); |
| 336 |
// We drop the fourth column entirely since the transformation |
| 337 |
// should not depend on alpha. The fifth column is sent as a separate |
| 338 |
// vector. The fourth row is also dropped entirely because alpha should |
| 339 |
// never be modified. |
| 340 |
SkASSERT(yuvM[3] == 0 && yuvM[8] == 0 && yuvM[13] == 0 && yuvM[18] == 1); |
| 341 |
SkASSERT(yuvM[15] == 0 && yuvM[16] == 0 && yuvM[17] == 0 && yuvM[19] == 0); |
| 342 |
float mtx[9] = { |
| 343 |
yuvM[ 0], yuvM[ 1], yuvM[ 2], |
| 344 |
yuvM[ 5], yuvM[ 6], yuvM[ 7], |
| 345 |
yuvM[10], yuvM[11], yuvM[12], |
| 346 |
}; |
| 347 |
float v[3] = {yuvM[4], yuvM[9], yuvM[14]}; |
| 348 |
pdman.setMatrix3f(fColorSpaceMatrixVar, mtx); |
| 349 |
pdman.set3fv(fColorSpaceTranslateVar, 1, v); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
UniformHandle fColorSpaceMatrixVar; |
| 354 |
UniformHandle fColorSpaceTranslateVar; |
| 355 |
}; |
| 356 |
|
| 357 |
return std::make_unique<Impl>(); |
| 358 |
} |
| 359 |
|
| 360 |
void GrYUVtoRGBEffect::onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const { |
| 361 |
uint32_t packed = 0; |
| 362 |
int i = 0; |
| 363 |
for (auto [plane, channel] : fLocations) { |
| 364 |
if (plane < 0) { |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
uint8_t chann = static_cast<int>(channel); |
| 369 |
|
| 370 |
SkASSERT(plane < 4 && chann < 4); |
| 371 |
|
| 372 |
packed |= (plane | (chann << 2)) << (i++ * 4); |
| 373 |
} |
| 374 |
if (fYUVColorSpace == kIdentity_SkYUVColorSpace) { |
| 375 |
packed |= 1 << 16; |
| 376 |
} |
| 377 |
if (fSnap[0]) { |
| 378 |
packed |= 1 << 17; |
| 379 |
} |
| 380 |
if (fSnap[1]) { |
| 381 |
packed |= 1 << 18; |
| 382 |
} |
| 383 |
b->add32(packed); |
| 384 |
} |
| 385 |
|
| 386 |
bool GrYUVtoRGBEffect::onIsEqual(const GrFragmentProcessor& other) const { |
| 387 |
const GrYUVtoRGBEffect& that = other.cast<GrYUVtoRGBEffect>(); |
| 388 |
|
| 389 |
return fLocations == that.fLocations && |
| 390 |
std::equal(fSnap, fSnap + 2, that.fSnap) && |
| 391 |
fYUVColorSpace == that.fYUVColorSpace; |
| 392 |
} |
| 393 |
|
| 394 |
GrYUVtoRGBEffect::GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src) |
| 395 |
: GrFragmentProcessor(src) |
| 396 |
, fLocations((src.fLocations)) |
| 397 |
, fYUVColorSpace(src.fYUVColorSpace) { |
| 398 |
std::copy_n(src.fSnap, 2, fSnap); |
| 399 |
} |
| 400 |
|
| 401 |
std::unique_ptr<GrFragmentProcessor> GrYUVtoRGBEffect::clone() const { |
| 402 |
return std::unique_ptr<GrFragmentProcessor>(new GrYUVtoRGBEffect(*this)); |
| 403 |
} |