2015-05-29 18:37:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This test only works with the GPU backend.
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPixmap.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2020-10-02 18:37:57 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2020-03-25 18:26:01 +00:00
|
|
|
#include "include/core/SkYUVAIndex.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
2020-07-01 18:45:24 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTo.h"
|
2020-04-06 14:29:02 +00:00
|
|
|
#include "src/core/SkMathPriv.h"
|
2020-03-18 21:24:02 +00:00
|
|
|
#include "src/core/SkYUVMath.h"
|
2020-03-25 18:26:01 +00:00
|
|
|
#include "tools/Resources.h"
|
2020-06-19 18:27:14 +00:00
|
|
|
#include "tools/gpu/YUVUtils.h"
|
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
class GrRenderTargetContext;
|
|
|
|
|
2015-05-29 18:37:25 +00:00
|
|
|
namespace skiagm {
|
2019-02-07 22:23:36 +00:00
|
|
|
class ImageFromYUVTextures : public GpuGM {
|
2015-05-29 18:37:25 +00:00
|
|
|
public:
|
|
|
|
ImageFromYUVTextures() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("image_from_yuv_textures");
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
SkISize onISize() override { return {1420, 610}; }
|
2020-03-24 20:47:52 +00:00
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
static std::unique_ptr<sk_gpu_test::LazyYUVImage> CreatePlanes(const char* name) {
|
2020-03-25 18:26:01 +00:00
|
|
|
SkBitmap bmp;
|
|
|
|
if (!GetResourceAsBitmap(name, &bmp)) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
if (bmp.colorType() != kRGBA_8888_SkColorType) {
|
|
|
|
auto info = bmp.info().makeColorType(kRGBA_8888_SkColorType);
|
|
|
|
SkBitmap copy;
|
|
|
|
copy.allocPixels(info);
|
|
|
|
SkAssertResult(bmp.readPixels(copy.pixmap()));
|
|
|
|
bmp = copy;
|
|
|
|
}
|
|
|
|
SkYUVAPixmapInfo pixmapInfo({bmp.dimensions(),
|
|
|
|
SkYUVAInfo::PlanarConfig::kY_U_V_A_4204,
|
|
|
|
kJPEG_Full_SkYUVColorSpace},
|
|
|
|
SkYUVAPixmapInfo::DataType::kUnorm8,
|
|
|
|
nullptr);
|
|
|
|
auto pixmaps = SkYUVAPixmaps::Allocate(pixmapInfo);
|
2020-03-25 18:26:01 +00:00
|
|
|
|
|
|
|
unsigned char* yuvPixels[] = {
|
2020-10-16 14:05:21 +00:00
|
|
|
static_cast<unsigned char*>(pixmaps.planes()[0].writable_addr()),
|
|
|
|
static_cast<unsigned char*>(pixmaps.planes()[1].writable_addr()),
|
|
|
|
static_cast<unsigned char*>(pixmaps.planes()[2].writable_addr()),
|
|
|
|
static_cast<unsigned char*>(pixmaps.planes()[3].writable_addr()),
|
2020-03-25 18:26:01 +00:00
|
|
|
};
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2020-03-18 21:24:02 +00:00
|
|
|
float m[20];
|
2020-10-16 14:05:21 +00:00
|
|
|
SkColorMatrix_RGB2YUV(pixmaps.yuvaInfo().yuvColorSpace(), m);
|
2019-03-06 17:36:47 +00:00
|
|
|
// Here we encode using the kJPEG_SkYUVColorSpace (i.e., full-swing Rec 601) even though
|
|
|
|
// we will draw it with all the supported yuv color spaces when converted back to RGB
|
2020-10-16 14:05:21 +00:00
|
|
|
for (int j = 0; j < pixmaps.planes()[0].height(); ++j) {
|
|
|
|
for (int i = 0; i < pixmaps.planes()[0].width(); ++i) {
|
|
|
|
auto rgba = *bmp.getAddr32(i, j);
|
2020-03-25 18:26:01 +00:00
|
|
|
auto r = (rgba & 0x000000ff) >> 0;
|
|
|
|
auto g = (rgba & 0x0000ff00) >> 8;
|
|
|
|
auto b = (rgba & 0x00ff0000) >> 16;
|
|
|
|
auto a = (rgba & 0xff000000) >> 24;
|
2020-10-16 14:05:21 +00:00
|
|
|
yuvPixels[0][j*pixmaps.planes()[0].width() + i] = SkToU8(
|
2020-03-25 18:26:01 +00:00
|
|
|
sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
|
2020-10-16 14:05:21 +00:00
|
|
|
yuvPixels[3][j*pixmaps.planes()[0].width() + i] = SkToU8(sk_float_round2int(
|
2020-03-25 18:26:01 +00:00
|
|
|
m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
|
|
|
|
}
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
for (int j = 0; j < pixmaps.planes()[1].height(); ++j) {
|
|
|
|
for (int i = 0; i < pixmaps.planes()[1].width(); ++i) {
|
2015-05-29 18:37:25 +00:00
|
|
|
// Average together 4 pixels of RGB.
|
2020-03-18 21:24:02 +00:00
|
|
|
int rgba[] = {0, 0, 0, 0};
|
2020-10-16 14:05:21 +00:00
|
|
|
int denom = 0;
|
|
|
|
int ylimit = std::min(2*j + 2, pixmaps.planes()[0].height());
|
|
|
|
int xlimit = std::min(2*i + 2, pixmaps.planes()[0].width());
|
|
|
|
for (int y = 2*j; y < ylimit; ++y) {
|
|
|
|
for (int x = 2*i; x < xlimit; ++x) {
|
|
|
|
auto src = *bmp.getAddr32(x, y);
|
2020-03-25 18:26:01 +00:00
|
|
|
rgba[0] += (src & 0x000000ff) >> 0;
|
|
|
|
rgba[1] += (src & 0x0000ff00) >> 8;
|
|
|
|
rgba[2] += (src & 0x00ff0000) >> 16;
|
|
|
|
rgba[3] += (src & 0xff000000) >> 24;
|
2020-10-16 14:05:21 +00:00
|
|
|
++denom;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:24:02 +00:00
|
|
|
for (int c = 0; c < 4; ++c) {
|
2020-10-16 14:05:21 +00:00
|
|
|
rgba[c] /= denom;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
int uvIndex = j*pixmaps.planes()[1].width() + i;
|
2020-03-25 18:26:01 +00:00
|
|
|
yuvPixels[1][uvIndex] = SkToU8(sk_float_round2int(
|
2020-03-18 21:24:02 +00:00
|
|
|
m[5]*rgba[0] + m[6]*rgba[1] + m[7]*rgba[2] + m[8]*rgba[3] + 255*m[9]));
|
2020-03-25 18:26:01 +00:00
|
|
|
yuvPixels[2][uvIndex] = SkToU8(sk_float_round2int(
|
2020-03-18 21:24:02 +00:00
|
|
|
m[10]*rgba[0] + m[11]*rgba[1] + m[12]*rgba[2] + m[13]*rgba[3] + 255*m[14]));
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-16 14:05:21 +00:00
|
|
|
return sk_gpu_test::LazyYUVImage::Make(std::move(pixmaps));
|
2020-10-15 14:31:43 +00:00
|
|
|
}
|
2020-06-19 18:27:14 +00:00
|
|
|
|
2020-10-16 13:30:54 +00:00
|
|
|
sk_sp<SkImage> makeYUVAImage(GrDirectContext* context) {
|
2020-10-16 14:05:21 +00:00
|
|
|
return fLazyYUVImage->refImage(context, sk_gpu_test::LazyYUVImage::Type::kFromTextures);
|
|
|
|
}
|
2020-10-16 13:30:54 +00:00
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
sk_sp<SkImage> createReferenceImage(GrDirectContext* dContext) {
|
|
|
|
auto planarImage = this->makeYUVAImage(dContext);
|
|
|
|
if (!planarImage) {
|
2020-06-19 18:27:14 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
auto resultInfo = SkImageInfo::Make(fLazyYUVImage->dimensions(),
|
2020-10-02 18:37:57 +00:00
|
|
|
kRGBA_8888_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
auto resultSurface = SkSurface::MakeRenderTarget(dContext,
|
|
|
|
SkBudgeted::kYes,
|
|
|
|
resultInfo,
|
|
|
|
1,
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
nullptr);
|
|
|
|
if (!resultSurface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
resultSurface->getCanvas()->drawImage(std::move(planarImage), 0, 0);
|
2020-10-02 18:37:57 +00:00
|
|
|
return resultSurface->makeImageSnapshot();
|
2020-06-19 18:27:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 18:45:24 +00:00
|
|
|
DrawResult onGpuSetup(GrDirectContext* context, SkString* errorMsg) override {
|
2020-06-19 18:27:14 +00:00
|
|
|
if (!context || context->abandoned()) {
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:05:21 +00:00
|
|
|
if (!fLazyYUVImage) {
|
|
|
|
fLazyYUVImage = CreatePlanes("images/mandrill_32.png");
|
|
|
|
}
|
2020-06-19 18:27:14 +00:00
|
|
|
|
|
|
|
// We make a version of this image for each draw because, if any draw flattens it to
|
|
|
|
// RGBA, then all subsequent draws would use the RGBA texture.
|
|
|
|
for (int i = 0; i < kNumImages; ++i) {
|
|
|
|
fYUVAImages[i] = this->makeYUVAImage(context);
|
|
|
|
if (!fYUVAImages[i]) {
|
|
|
|
*errorMsg = "Couldn't create src YUVA image.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fReferenceImage = this->createReferenceImage(context);
|
|
|
|
if (!fReferenceImage) {
|
|
|
|
*errorMsg = "Couldn't create reference YUVA image.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some backends (e.g., Vulkan) require all work be completed for backend textures
|
|
|
|
// before they are deleted. Since we don't know when we'll next have access to a
|
|
|
|
// direct context, flush all the work now.
|
2020-06-30 17:42:46 +00:00
|
|
|
context->flush();
|
2020-06-19 18:27:14 +00:00
|
|
|
context->submit(true);
|
|
|
|
|
|
|
|
return DrawResult::kOk;
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:38:53 +00:00
|
|
|
void onGpuTeardown() override {
|
|
|
|
for (sk_sp<SkImage>& image : fYUVAImages) {
|
|
|
|
image.reset();
|
|
|
|
}
|
|
|
|
fReferenceImage.reset();
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:27:14 +00:00
|
|
|
SkImage* getYUVAImage(int index) {
|
|
|
|
SkASSERT(index >= 0 && index < kNumImages);
|
|
|
|
return fYUVAImages[index].get();
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:36:12 +00:00
|
|
|
void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas* canvas) override {
|
2020-03-25 18:26:01 +00:00
|
|
|
auto draw_image = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
|
|
|
|
if (!image) {
|
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(fq);
|
|
|
|
canvas->drawImage(image, 0, 0, &paint);
|
|
|
|
return {SkIntToScalar(image->width()), SkIntToScalar(image->height())};
|
|
|
|
};
|
2020-03-24 20:47:52 +00:00
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
auto draw_image_rect = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
|
|
|
|
if (!image) {
|
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(fq);
|
|
|
|
auto subset = SkRect::Make(image->dimensions());
|
|
|
|
subset.inset(subset.width() * .05f, subset.height() * .1f);
|
|
|
|
auto dst = SkRect::MakeWH(subset.width(), subset.height());
|
|
|
|
canvas->drawImageRect(image, subset, dst, &paint);
|
|
|
|
return {dst.width(), dst.height()};
|
|
|
|
};
|
2020-03-25 15:19:37 +00:00
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
auto draw_image_shader = [canvas](SkImage* image, SkFilterQuality fq) -> SkSize {
|
|
|
|
if (!image) {
|
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
SkMatrix m;
|
|
|
|
m.setRotate(45, image->width()/2.f, image->height()/2.f);
|
|
|
|
auto shader = image->makeShader(SkTileMode::kMirror, SkTileMode::kDecal, m);
|
2020-03-25 15:19:37 +00:00
|
|
|
SkPaint paint;
|
2020-03-25 18:26:01 +00:00
|
|
|
paint.setFilterQuality(fq);
|
|
|
|
paint.setShader(std::move(shader));
|
|
|
|
auto rect = SkRect::MakeWH(image->width() * 1.3f, image->height());
|
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
return {rect.width(), rect.height()};
|
|
|
|
};
|
|
|
|
|
|
|
|
canvas->translate(kPad, kPad);
|
2020-06-19 18:27:14 +00:00
|
|
|
int imageIndex = 0;
|
2020-03-25 18:26:01 +00:00
|
|
|
using DrawSig = SkSize(SkImage* image, SkFilterQuality fq);
|
|
|
|
using DF = std::function<DrawSig>;
|
|
|
|
for (const auto& draw : {DF(draw_image), DF(draw_image_rect), DF(draw_image_shader)}) {
|
|
|
|
for (auto scale : {1.f, 4.f, 0.75f}) {
|
|
|
|
SkScalar h = 0;
|
|
|
|
canvas->save();
|
|
|
|
for (auto fq : {kNone_SkFilterQuality, kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality, kHigh_SkFilterQuality}) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(scale, scale);
|
2020-06-19 18:27:14 +00:00
|
|
|
auto s1 = draw(this->getYUVAImage(imageIndex++), fq);
|
2020-03-25 18:26:01 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kPad + SkScalarCeilToScalar(scale*s1.width()), 0);
|
|
|
|
canvas->save();
|
|
|
|
canvas->scale(scale, scale);
|
2020-06-19 18:27:14 +00:00
|
|
|
auto s2 = draw(fReferenceImage.get(), fq);
|
2020-03-25 18:26:01 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(kPad + SkScalarCeilToScalar(scale*s2.width()), 0);
|
|
|
|
h = std::max({h, s1.height(), s2.height()});
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, kPad + SkScalarCeilToScalar(scale*h));
|
2020-03-24 18:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-16 14:05:21 +00:00
|
|
|
std::unique_ptr<sk_gpu_test::LazyYUVImage> fLazyYUVImage;
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2020-06-19 18:27:14 +00:00
|
|
|
// 3 draws x 3 scales x 4 filter qualities
|
|
|
|
static constexpr int kNumImages = 3 * 3 * 4;
|
|
|
|
sk_sp<SkImage> fYUVAImages[kNumImages];
|
|
|
|
sk_sp<SkImage> fReferenceImage;
|
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
static constexpr SkScalar kPad = 10.0f;
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-05-29 18:37:25 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ImageFromYUVTextures;)
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|