2019-03-21 17:08:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/gpu/YUVUtils.h"
|
2019-03-21 17:08:36 +00:00
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
#include "include/core/SkColorPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkData.h"
|
2020-07-29 01:06:43 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2020-10-19 14:55:45 +00:00
|
|
|
#include "include/gpu/GrYUVABackendTextures.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/codec/SkCodecImageGenerator.h"
|
2021-01-11 19:40:44 +00:00
|
|
|
#include "src/core/SkYUVAInfoLocation.h"
|
2020-10-16 14:05:21 +00:00
|
|
|
#include "src/core/SkYUVMath.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2020-07-29 01:06:43 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2020-10-16 14:05:21 +00:00
|
|
|
#include "tools/gpu/ManagedBackendTexture.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static SkPMColor convert_yuva_to_rgba(const float mtx[20], uint8_t yuva[4]) {
|
|
|
|
uint8_t y = yuva[0];
|
|
|
|
uint8_t u = yuva[1];
|
|
|
|
uint8_t v = yuva[2];
|
|
|
|
uint8_t a = yuva[3];
|
|
|
|
|
|
|
|
uint8_t r = SkTPin(SkScalarRoundToInt(mtx[ 0]*y + mtx[ 1]*u + mtx[ 2]*v + mtx[ 4]*255), 0, 255);
|
|
|
|
uint8_t g = SkTPin(SkScalarRoundToInt(mtx[ 5]*y + mtx[ 6]*u + mtx[ 7]*v + mtx[ 9]*255), 0, 255);
|
|
|
|
uint8_t b = SkTPin(SkScalarRoundToInt(mtx[10]*y + mtx[11]*u + mtx[12]*v + mtx[14]*255), 0, 255);
|
|
|
|
|
|
|
|
return SkPremultiplyARGBInline(a, r, g, b);
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:11:07 +00:00
|
|
|
static uint8_t look_up(SkPoint normPt, const SkPixmap& pmap, SkColorChannel channel) {
|
|
|
|
SkASSERT(normPt.x() > 0 && normPt.x() < 1.0f);
|
|
|
|
SkASSERT(normPt.y() > 0 && normPt.y() < 1.0f);
|
|
|
|
int x = SkScalarFloorToInt(normPt.x() * pmap.width());
|
|
|
|
int y = SkScalarFloorToInt(normPt.y() * pmap.height());
|
2020-10-16 14:05:21 +00:00
|
|
|
|
|
|
|
auto ii = pmap.info().makeColorType(kRGBA_8888_SkColorType).makeWH(1, 1);
|
|
|
|
uint32_t pixel;
|
|
|
|
SkAssertResult(pmap.readPixels(ii, &pixel, sizeof(pixel), x, y));
|
|
|
|
int shift = static_cast<int>(channel) * 8;
|
|
|
|
return static_cast<uint8_t>((pixel >> shift) & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Generator : public SkImageGenerator {
|
|
|
|
public:
|
|
|
|
Generator(SkYUVAPixmaps pixmaps, sk_sp<SkColorSpace> cs)
|
|
|
|
: SkImageGenerator(SkImageInfo::Make(pixmaps.yuvaInfo().dimensions(),
|
|
|
|
kN32_SkColorType,
|
|
|
|
kPremul_SkAlphaType,
|
|
|
|
std::move(cs)))
|
|
|
|
, fPixmaps(std::move(pixmaps)) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onGetPixels(const SkImageInfo& info,
|
|
|
|
void* pixels,
|
|
|
|
size_t rowBytes,
|
|
|
|
const Options&) override {
|
|
|
|
if (kUnknown_SkColorType == fFlattened.colorType()) {
|
|
|
|
fFlattened.allocPixels(info);
|
|
|
|
SkASSERT(info == this->getInfo());
|
|
|
|
|
|
|
|
float mtx[20];
|
|
|
|
SkColorMatrix_YUV2RGB(fPixmaps.yuvaInfo().yuvColorSpace(), mtx);
|
2021-01-11 19:40:44 +00:00
|
|
|
SkYUVAInfo::YUVALocations yuvaLocations = fPixmaps.toYUVALocations();
|
|
|
|
SkASSERT(SkYUVAInfo::YUVALocation::AreValidLocations(yuvaLocations));
|
2020-10-16 14:05:21 +00:00
|
|
|
|
2021-01-19 17:11:07 +00:00
|
|
|
SkMatrix om = fPixmaps.yuvaInfo().originMatrix();
|
|
|
|
SkAssertResult(om.invert(&om));
|
|
|
|
float normX = 1.f/info.width();
|
|
|
|
float normY = 1.f/info.height();
|
|
|
|
if (SkEncodedOriginSwapsWidthHeight(fPixmaps.yuvaInfo().origin())) {
|
|
|
|
using std::swap;
|
|
|
|
swap(normX, normY);
|
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
for (int y = 0; y < info.height(); ++y) {
|
|
|
|
for (int x = 0; x < info.width(); ++x) {
|
2021-01-19 17:11:07 +00:00
|
|
|
SkPoint xy1 {(x + 0.5f),
|
|
|
|
(y + 0.5f)};
|
|
|
|
xy1 = om.mapPoint(xy1);
|
|
|
|
xy1.fX *= normX;
|
|
|
|
xy1.fY *= normY;
|
2020-10-16 14:05:21 +00:00
|
|
|
|
|
|
|
uint8_t yuva[4] = {0, 0, 0, 255};
|
|
|
|
|
2021-01-11 19:40:44 +00:00
|
|
|
for (auto c : {SkYUVAInfo::YUVAChannels::kY,
|
|
|
|
SkYUVAInfo::YUVAChannels::kU,
|
|
|
|
SkYUVAInfo::YUVAChannels::kV}) {
|
|
|
|
const auto& pmap = fPixmaps.plane(yuvaLocations[c].fPlane);
|
2021-01-19 17:11:07 +00:00
|
|
|
yuva[c] = look_up(xy1, pmap, yuvaLocations[c].fChannel);
|
2020-10-16 14:05:21 +00:00
|
|
|
}
|
2021-01-19 17:11:07 +00:00
|
|
|
auto [aPlane, aChan] = yuvaLocations[SkYUVAInfo::YUVAChannels::kA];
|
|
|
|
if (aPlane >= 0) {
|
|
|
|
const auto& pmap = fPixmaps.plane(aPlane);
|
|
|
|
yuva[3] = look_up(xy1, pmap, aChan);
|
2020-10-16 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Making premul here.
|
|
|
|
*fFlattened.getAddr32(x, y) = convert_yuva_to_rgba(mtx, yuva);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fFlattened.readPixels(info, pixels, rowBytes, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& types,
|
|
|
|
SkYUVAPixmapInfo* info) const override {
|
|
|
|
*info = fPixmaps.pixmapsInfo();
|
|
|
|
return info->isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onGetYUVAPlanes(const SkYUVAPixmaps& pixmaps) override {
|
|
|
|
SkASSERT(pixmaps.yuvaInfo() == fPixmaps.yuvaInfo());
|
|
|
|
for (int i = 0; i < pixmaps.numPlanes(); ++i) {
|
|
|
|
SkASSERT(fPixmaps.plane(i).colorType() == pixmaps.plane(i).colorType());
|
|
|
|
SkASSERT(fPixmaps.plane(i).dimensions() == pixmaps.plane(i).dimensions());
|
|
|
|
SkASSERT(fPixmaps.plane(i).rowBytes() == pixmaps.plane(i).rowBytes());
|
|
|
|
fPixmaps.plane(i).readPixels(pixmaps.plane(i));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkYUVAPixmaps fPixmaps;
|
|
|
|
SkBitmap fFlattened;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
2019-03-21 17:08:36 +00:00
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(sk_sp<SkData> data,
|
|
|
|
GrMipmapped mipmapped,
|
|
|
|
sk_sp<SkColorSpace> cs) {
|
2019-03-21 17:08:36 +00:00
|
|
|
std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
|
2020-10-16 14:05:21 +00:00
|
|
|
if (image->reset(std::move(data), mipmapped, std::move(cs))) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return image;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
std::unique_ptr<LazyYUVImage> LazyYUVImage::Make(SkYUVAPixmaps pixmaps,
|
|
|
|
GrMipmapped mipmapped,
|
|
|
|
sk_sp<SkColorSpace> cs) {
|
|
|
|
std::unique_ptr<LazyYUVImage> image(new LazyYUVImage());
|
|
|
|
if (image->reset(std::move(pixmaps), mipmapped, std::move(cs))) {
|
|
|
|
return image;
|
2019-03-21 17:08:36 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
sk_sp<SkImage> LazyYUVImage::refImage(GrRecordingContext* rContext, Type type) {
|
|
|
|
if (this->ensureYUVImage(rContext, type)) {
|
|
|
|
size_t idx = static_cast<size_t>(type);
|
2020-10-16 15:11:40 +00:00
|
|
|
SkASSERT(idx < SK_ARRAY_COUNT(fYUVImage));
|
2020-10-16 14:05:21 +00:00
|
|
|
return fYUVImage[idx];
|
2019-03-21 17:08:36 +00:00
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
bool LazyYUVImage::reset(sk_sp<SkData> data, GrMipmapped mipmapped, sk_sp<SkColorSpace> cs) {
|
2020-07-31 12:57:48 +00:00
|
|
|
fMipmapped = mipmapped;
|
2019-03-21 17:08:36 +00:00
|
|
|
auto codec = SkCodecImageGenerator::MakeFromEncodedCodec(data);
|
|
|
|
if (!codec) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:00:04 +00:00
|
|
|
SkYUVAPixmapInfo yuvaPixmapInfo;
|
2020-09-01 19:01:15 +00:00
|
|
|
if (!codec->queryYUVAInfo(SkYUVAPixmapInfo::SupportedDataTypes::All(), &yuvaPixmapInfo)) {
|
2020-08-24 13:18:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-27 15:00:04 +00:00
|
|
|
fPixmaps = SkYUVAPixmaps::Allocate(yuvaPixmapInfo);
|
2020-08-24 13:18:16 +00:00
|
|
|
if (!fPixmaps.isValid()) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:00:04 +00:00
|
|
|
if (!codec->getYUVAPlanes(fPixmaps)) {
|
2019-03-21 17:08:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
fColorSpace = std::move(cs);
|
|
|
|
|
2019-03-21 17:08:36 +00:00
|
|
|
// The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
bool LazyYUVImage::reset(SkYUVAPixmaps pixmaps, GrMipmapped mipmapped, sk_sp<SkColorSpace> cs) {
|
|
|
|
if (!pixmaps.isValid()) {
|
|
|
|
return false;
|
2020-06-26 14:01:19 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
fMipmapped = mipmapped;
|
|
|
|
if (pixmaps.ownsStorage()) {
|
|
|
|
fPixmaps = std::move(pixmaps);
|
2020-10-16 13:30:54 +00:00
|
|
|
} else {
|
2020-10-16 14:05:21 +00:00
|
|
|
fPixmaps = SkYUVAPixmaps::MakeCopy(std::move(pixmaps));
|
2020-06-19 18:27:14 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
fColorSpace = std::move(cs);
|
|
|
|
// The SkPixmap data is fully configured now for MakeFromYUVAPixmaps once we get a GrContext
|
|
|
|
return true;
|
2020-10-16 13:30:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
bool LazyYUVImage::ensureYUVImage(GrRecordingContext* rContext, Type type) {
|
|
|
|
size_t idx = static_cast<size_t>(type);
|
2020-10-16 15:11:40 +00:00
|
|
|
SkASSERT(idx < SK_ARRAY_COUNT(fYUVImage));
|
2020-10-16 14:05:21 +00:00
|
|
|
if (fYUVImage[idx] && fYUVImage[idx]->isValid(rContext)) {
|
|
|
|
return true; // Have already made a YUV image valid for this context.
|
2020-06-26 14:01:19 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
// Try to make a new YUV image for this context.
|
|
|
|
switch (type) {
|
|
|
|
case Type::kFromPixmaps:
|
|
|
|
if (!rContext || rContext->abandoned()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fYUVImage[idx] = SkImage::MakeFromYUVAPixmaps(rContext,
|
|
|
|
fPixmaps,
|
|
|
|
fMipmapped,
|
|
|
|
/*limit to max tex size*/ false,
|
|
|
|
fColorSpace);
|
|
|
|
break;
|
|
|
|
case Type::kFromGenerator: {
|
|
|
|
// Make sure the generator has ownership of its backing planes.
|
|
|
|
auto generator = std::make_unique<Generator>(fPixmaps, fColorSpace);
|
|
|
|
fYUVImage[idx] = SkImage::MakeFromGenerator(std::move(generator));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::kFromTextures:
|
|
|
|
if (!rContext || rContext->abandoned()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-04 21:54:28 +00:00
|
|
|
if (fMipmapped == GrMipmapped::kYes) {
|
|
|
|
// If this becomes necessary we should invoke SkMipmapBuilder here to make mip
|
|
|
|
// maps from our src data (and then pass a pixmaps array to initialize the planar
|
|
|
|
// textures.
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
if (auto direct = rContext->asDirectContext()) {
|
|
|
|
sk_sp<sk_gpu_test::ManagedBackendTexture> mbets[SkYUVAInfo::kMaxPlanes];
|
|
|
|
GrBackendTexture textures[SkYUVAInfo::kMaxPlanes];
|
|
|
|
for (int i = 0; i < fPixmaps.numPlanes(); ++i) {
|
|
|
|
mbets[i] = sk_gpu_test::ManagedBackendTexture::MakeWithData(
|
2020-12-07 16:30:16 +00:00
|
|
|
direct,
|
|
|
|
fPixmaps.plane(i),
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
GrRenderable::kNo,
|
|
|
|
GrProtected::kNo);
|
2020-10-16 14:05:21 +00:00
|
|
|
if (mbets[i]) {
|
|
|
|
textures[i] = mbets[i]->texture();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 14:55:45 +00:00
|
|
|
GrYUVABackendTextures yuvaTextures(fPixmaps.yuvaInfo(),
|
|
|
|
textures,
|
|
|
|
kTopLeft_GrSurfaceOrigin);
|
|
|
|
if (!yuvaTextures.isValid()) {
|
2020-10-16 22:32:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-08 21:39:32 +00:00
|
|
|
void* planeRelContext =
|
|
|
|
sk_gpu_test::ManagedBackendTexture::MakeYUVAReleaseContext(mbets);
|
|
|
|
fYUVImage[idx] = SkImage::MakeFromYUVATextures(
|
|
|
|
direct,
|
|
|
|
yuvaTextures,
|
|
|
|
fColorSpace,
|
|
|
|
sk_gpu_test::ManagedBackendTexture::ReleaseProc,
|
|
|
|
planeRelContext);
|
2020-10-16 14:05:21 +00:00
|
|
|
}
|
2020-10-16 13:30:54 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
return fYUVImage[idx] != nullptr;
|
2020-10-16 13:30:54 +00:00
|
|
|
}
|
2019-03-21 17:08:36 +00:00
|
|
|
} // namespace sk_gpu_test
|