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"
|
|
|
|
#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"
|
|
|
|
#include "include/gpu/GrContext.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"
|
2015-05-29 18:37:25 +00:00
|
|
|
|
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-03-25 15:19:37 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2020-03-25 18:26:01 +00:00
|
|
|
fRGBABmp = this->createBmpAndPlanes("images/mandrill_32.png", fYUVABmps);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap createBmpAndPlanes(const char* name, SkBitmap yuvaBmps[4]) {
|
|
|
|
SkBitmap bmp;
|
|
|
|
if (!GetResourceAsBitmap(name, &bmp)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto ii = SkImageInfo::Make(bmp.dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkBitmap rgbaBmp;
|
|
|
|
rgbaBmp.allocPixels(ii);
|
|
|
|
bmp.readPixels(rgbaBmp.pixmap(), 0, 0);
|
|
|
|
|
|
|
|
SkImageInfo yaInfo = SkImageInfo::Make(rgbaBmp.dimensions(), kAlpha_8_SkColorType,
|
|
|
|
kUnpremul_SkAlphaType);
|
|
|
|
yuvaBmps[0].allocPixels(yaInfo);
|
|
|
|
SkISize uvSize = {rgbaBmp.width()/2, rgbaBmp.height()/2};
|
|
|
|
SkImageInfo uvInfo = SkImageInfo::Make(uvSize, kAlpha_8_SkColorType, kUnpremul_SkAlphaType);
|
|
|
|
yuvaBmps[1].allocPixels(uvInfo);
|
|
|
|
yuvaBmps[2].allocPixels(uvInfo);
|
|
|
|
yuvaBmps[3].allocPixels(yaInfo);
|
|
|
|
|
|
|
|
unsigned char* yuvPixels[] = {
|
|
|
|
static_cast<unsigned char*>(yuvaBmps[0].getPixels()),
|
|
|
|
static_cast<unsigned char*>(yuvaBmps[1].getPixels()),
|
|
|
|
static_cast<unsigned char*>(yuvaBmps[2].getPixels()),
|
|
|
|
static_cast<unsigned char*>(yuvaBmps[3].getPixels()),
|
|
|
|
};
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2020-03-18 21:24:02 +00:00
|
|
|
float m[20];
|
|
|
|
SkColorMatrix_RGB2YUV(kJPEG_SkYUVColorSpace, 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-03-25 18:26:01 +00:00
|
|
|
for (int j = 0; j < yaInfo.height(); ++j) {
|
|
|
|
for (int i = 0; i < yaInfo.width(); ++i) {
|
|
|
|
auto rgba = *rgbaBmp.getAddr32(i, j);
|
|
|
|
auto r = (rgba & 0x000000ff) >> 0;
|
|
|
|
auto g = (rgba & 0x0000ff00) >> 8;
|
|
|
|
auto b = (rgba & 0x00ff0000) >> 16;
|
|
|
|
auto a = (rgba & 0xff000000) >> 24;
|
|
|
|
yuvPixels[0][j*yaInfo.width() + i] = SkToU8(
|
|
|
|
sk_float_round2int(m[0]*r + m[1]*g + m[2]*b + m[3]*a + 255*m[4]));
|
|
|
|
yuvPixels[3][j*yaInfo.width() + i] = SkToU8(sk_float_round2int(
|
|
|
|
m[15]*r + m[16]*g + m[17]*b + m[18]*a + 255*m[19]));
|
|
|
|
}
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
2020-03-25 18:26:01 +00:00
|
|
|
for (int j = 0; j < uvInfo.height(); ++j) {
|
|
|
|
for (int i = 0; i < uvInfo.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};
|
2015-05-29 18:37:25 +00:00
|
|
|
for (int y = 0; y < 2; ++y) {
|
|
|
|
for (int x = 0; x < 2; ++x) {
|
2020-03-25 18:26:01 +00:00
|
|
|
auto src = *rgbaBmp.getAddr32(2 * i + x, 2 * j + y);
|
|
|
|
rgba[0] += (src & 0x000000ff) >> 0;
|
|
|
|
rgba[1] += (src & 0x0000ff00) >> 8;
|
|
|
|
rgba[2] += (src & 0x00ff0000) >> 16;
|
|
|
|
rgba[3] += (src & 0xff000000) >> 24;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:24:02 +00:00
|
|
|
for (int c = 0; c < 4; ++c) {
|
|
|
|
rgba[c] /= 4;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
2020-03-25 18:26:01 +00:00
|
|
|
int uvIndex = j*uvInfo.width() + i;
|
|
|
|
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-03-25 18:26:01 +00:00
|
|
|
return rgbaBmp;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
void createYUVTextures(SkBitmap bmps[4], GrContext* context, GrBackendTexture textures[4]) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2020-04-06 14:29:02 +00:00
|
|
|
textures[i] = context->createBackendTexture(bmps[i].pixmap(), GrRenderable::kNo,
|
2020-03-25 18:26:01 +00:00
|
|
|
GrProtected::kNo);
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
void createResultTexture(GrContext* context, SkISize size, GrBackendTexture* resultTexture) {
|
2019-06-04 15:03:06 +00:00
|
|
|
*resultTexture = context->createBackendTexture(
|
2020-03-25 18:26:01 +00:00
|
|
|
size.width(), size.height(), kRGBA_8888_SkColorType, SkColors::kTransparent,
|
2019-07-01 19:04:06 +00:00
|
|
|
GrMipMapped::kNo, GrRenderable::kYes, GrProtected::kNo);
|
2018-09-18 01:57:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 15:19:33 +00:00
|
|
|
void deleteBackendTextures(GrContext* context, GrBackendTexture textures[], int n) {
|
2018-06-26 21:38:34 +00:00
|
|
|
if (context->abandoned()) {
|
2018-06-25 12:53:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-13 14:19:57 +00:00
|
|
|
|
2019-06-10 19:09:34 +00:00
|
|
|
GrFlushInfo flushInfo;
|
|
|
|
flushInfo.fFlags = kSyncCpu_GrFlushFlag;
|
|
|
|
context->flush(flushInfo);
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2018-09-18 15:19:33 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-05-20 12:38:07 +00:00
|
|
|
context->deleteBackendTexture(textures[i]);
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 22:23:36 +00:00
|
|
|
void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
|
2020-03-25 18:26:01 +00:00
|
|
|
GrBackendTexture yuvaTextures[4];
|
2020-04-06 14:29:02 +00:00
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
this->createYUVTextures(fYUVABmps, context, yuvaTextures);
|
2020-04-06 14:29:02 +00:00
|
|
|
SkYUVAIndex indices[4];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!yuvaTextures[i].isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto chanMask = yuvaTextures[i].getBackendFormat().channelMask();
|
|
|
|
// We expect the single channel bitmaps to produce single channel textures.
|
|
|
|
SkASSERT(chanMask && SkIsPow2(chanMask));
|
|
|
|
if (chanMask & kGray_SkColorChannelFlag) {
|
|
|
|
indices[i].fChannel = SkColorChannel::kR;
|
|
|
|
} else {
|
|
|
|
indices[i].fChannel = static_cast<SkColorChannel>(31 - SkCLZ(chanMask));
|
|
|
|
}
|
|
|
|
indices[i].fIndex = i;
|
|
|
|
}
|
2020-04-02 20:59:19 +00:00
|
|
|
|
|
|
|
// We remake this image before each draw because if any draw flattens it to RGBA then
|
|
|
|
// all subsequent draws use the RGBA texture.
|
|
|
|
auto makeImage1 = [&]() {
|
|
|
|
return SkImage::MakeFromYUVATextures(context,
|
|
|
|
kJPEG_SkYUVColorSpace,
|
|
|
|
yuvaTextures,
|
2020-04-06 14:29:02 +00:00
|
|
|
indices,
|
2020-04-02 20:59:19 +00:00
|
|
|
fRGBABmp.dimensions(),
|
|
|
|
kTopLeft_GrSurfaceOrigin);
|
|
|
|
};
|
2020-03-24 02:49:51 +00:00
|
|
|
|
2020-03-25 18:26:01 +00:00
|
|
|
GrBackendTexture resultTexture;
|
|
|
|
this->createResultTexture(context, fRGBABmp.dimensions(), &resultTexture);
|
|
|
|
auto image2 = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(context,
|
|
|
|
kJPEG_SkYUVColorSpace,
|
|
|
|
yuvaTextures,
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
resultTexture);
|
2020-03-24 18:59:25 +00:00
|
|
|
|
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);
|
|
|
|
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-04-02 20:59:19 +00:00
|
|
|
auto s1 = draw(makeImage1().get(), 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);
|
|
|
|
auto s2 = draw(image2.get(), fq);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-03-25 18:26:01 +00:00
|
|
|
|
|
|
|
this->deleteBackendTextures(context, &resultTexture, 1);
|
|
|
|
this->deleteBackendTextures(context, yuvaTextures, 4);
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-25 18:26:01 +00:00
|
|
|
SkBitmap fRGBABmp;
|
|
|
|
SkBitmap fYUVABmps[4];
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
static constexpr SkScalar kPad = 10.0f;
|
2015-05-29 18:37:25 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new ImageFromYUVTextures;)
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|