2014-07-07 22:09:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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.
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
2016-10-27 18:47:55 +00:00
|
|
|
#include "GrRenderTargetContextPriv.h"
|
2017-01-23 20:30:35 +00:00
|
|
|
#include "GrTextureProxy.h"
|
2014-07-07 22:09:48 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkGradientShader.h"
|
2016-02-01 21:16:14 +00:00
|
|
|
#include "effects/GrYUVEffect.h"
|
2016-12-16 14:52:16 +00:00
|
|
|
#include "ops/GrDrawOp.h"
|
|
|
|
#include "ops/GrRectOpFactory.h"
|
2014-07-07 22:09:48 +00:00
|
|
|
|
2015-02-13 21:57:09 +00:00
|
|
|
#define YSIZE 8
|
|
|
|
#define USIZE 4
|
|
|
|
#define VSIZE 4
|
|
|
|
|
2014-07-07 22:09:48 +00:00
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrYUVtoRGBEffect.
|
|
|
|
*/
|
|
|
|
class YUVtoRGBEffect : public GM {
|
|
|
|
public:
|
|
|
|
YUVtoRGBEffect() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-07-07 22:09:48 +00:00
|
|
|
return SkString("yuv_to_rgb_effect");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2015-07-20 22:00:03 +00:00
|
|
|
return SkISize::Make(238, 120);
|
2014-07-07 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-02-13 21:57:09 +00:00
|
|
|
SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
|
|
|
|
fBmp[0].allocPixels(yinfo);
|
|
|
|
SkImageInfo uinfo = SkImageInfo::MakeA8(USIZE, USIZE);
|
|
|
|
fBmp[1].allocPixels(uinfo);
|
|
|
|
SkImageInfo vinfo = SkImageInfo::MakeA8(VSIZE, VSIZE);
|
|
|
|
fBmp[2].allocPixels(vinfo);
|
2014-07-07 22:09:48 +00:00
|
|
|
unsigned char* pixels[3];
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
pixels[i] = (unsigned char*)fBmp[i].getPixels();
|
|
|
|
}
|
|
|
|
int color[] = {0, 85, 170};
|
|
|
|
const int limit[] = {255, 0, 255};
|
|
|
|
const int invl[] = {0, 255, 0};
|
|
|
|
const int inc[] = {1, -1, 1};
|
2015-02-13 21:57:09 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
const size_t nbBytes = fBmp[i].rowBytes() * fBmp[i].height();
|
|
|
|
for (size_t j = 0; j < nbBytes; ++j) {
|
2014-07-07 22:09:48 +00:00
|
|
|
pixels[i][j] = (unsigned char)color[i];
|
|
|
|
color[i] = (color[i] == limit[i]) ? invl[i] : color[i] + inc[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext =
|
|
|
|
canvas->internal_private_accessTopLayerRenderTargetContext();
|
|
|
|
if (!renderTargetContext) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2017-01-23 20:30:35 +00:00
|
|
|
return;
|
2014-07-07 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 21:32:04 +00:00
|
|
|
GrContext* context = canvas->getGrContext();
|
|
|
|
if (!context) {
|
2014-07-07 22:09:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-02 23:18:38 +00:00
|
|
|
sk_sp<GrTextureProxy> proxy[3];
|
2014-10-08 15:40:09 +00:00
|
|
|
|
2017-01-23 20:30:35 +00:00
|
|
|
{
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
desc.fWidth = fBmp[i].width();
|
|
|
|
desc.fHeight = fBmp[i].height();
|
2017-01-30 13:06:27 +00:00
|
|
|
desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[i].info(), *context->caps());
|
2017-01-23 20:30:35 +00:00
|
|
|
|
|
|
|
proxy[i] = GrSurfaceProxy::MakeDeferred(*context->caps(),
|
2017-03-04 13:12:46 +00:00
|
|
|
context->resourceProvider(),
|
2017-01-23 20:30:35 +00:00
|
|
|
desc, SkBudgeted::kYes,
|
|
|
|
fBmp[i].getPixels(), fBmp[i].rowBytes());
|
|
|
|
if (!proxy[i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kDrawPad = 10.f;
|
|
|
|
constexpr SkScalar kTestPad = 10.f;
|
|
|
|
constexpr SkScalar kColorSpaceOffset = 36.f;
|
2015-02-13 21:57:09 +00:00
|
|
|
SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
|
2014-09-13 00:45:58 +00:00
|
|
|
|
2017-01-23 20:30:35 +00:00
|
|
|
for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
|
2014-10-08 15:40:09 +00:00
|
|
|
SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp[0].width()),
|
|
|
|
SkIntToScalar(fBmp[0].height()));
|
|
|
|
renderRect.outset(kDrawPad, kDrawPad);
|
|
|
|
|
|
|
|
SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
|
|
|
|
SkScalar x = kDrawPad + kTestPad;
|
|
|
|
|
|
|
|
const int indices[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
|
|
|
|
{1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
2016-11-04 20:26:16 +00:00
|
|
|
sk_sp<GrFragmentProcessor> fp(
|
2017-01-23 20:30:35 +00:00
|
|
|
GrYUVEffect::MakeYUVToRGB(context,
|
2017-03-02 23:18:38 +00:00
|
|
|
proxy[indices[i][0]],
|
|
|
|
proxy[indices[i][1]],
|
|
|
|
proxy[indices[i][2]],
|
2016-11-04 20:26:16 +00:00
|
|
|
sizes,
|
|
|
|
static_cast<SkYUVColorSpace>(space),
|
|
|
|
false));
|
2014-10-08 15:40:09 +00:00
|
|
|
if (fp) {
|
2017-01-11 18:42:54 +00:00
|
|
|
GrPaint grPaint;
|
|
|
|
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
|
|
|
grPaint.addColorFragmentProcessor(std::move(fp));
|
2014-10-08 15:40:09 +00:00
|
|
|
SkMatrix viewMatrix;
|
|
|
|
viewMatrix.setTranslate(x, y);
|
2017-01-03 14:42:58 +00:00
|
|
|
std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
|
2016-12-14 16:08:17 +00:00
|
|
|
GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
|
2017-01-11 18:42:54 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(
|
|
|
|
std::move(grPaint), GrAAType::kNone, std::move(op));
|
2014-10-08 15:40:09 +00:00
|
|
|
}
|
|
|
|
x += renderRect.width() + kTestPad;
|
|
|
|
}
|
2014-07-07 22:09:48 +00:00
|
|
|
}
|
2014-10-08 15:40:09 +00:00
|
|
|
}
|
2014-07-07 22:09:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBmp[3];
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new YUVtoRGBEffect;)
|
2016-06-09 20:24:48 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class YUVNV12toRGBEffect : public GM {
|
|
|
|
public:
|
|
|
|
YUVNV12toRGBEffect() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("yuv_nv12_to_rgb_effect");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(48, 120);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkImageInfo yinfo = SkImageInfo::MakeA8(YSIZE, YSIZE);
|
|
|
|
fBmp[0].allocPixels(yinfo);
|
|
|
|
SkImageInfo uvinfo = SkImageInfo::MakeN32Premul(USIZE, USIZE);
|
|
|
|
fBmp[1].allocPixels(uvinfo);
|
|
|
|
int color[] = {0, 85, 170};
|
|
|
|
const int limit[] = {255, 0, 255};
|
|
|
|
const int invl[] = {0, 255, 0};
|
|
|
|
const int inc[] = {1, -1, 1};
|
|
|
|
|
|
|
|
{
|
|
|
|
unsigned char* pixels = (unsigned char*)fBmp[0].getPixels();
|
|
|
|
const size_t nbBytes = fBmp[0].rowBytes() * fBmp[0].height();
|
|
|
|
for (size_t j = 0; j < nbBytes; ++j) {
|
|
|
|
pixels[j] = (unsigned char)color[0];
|
|
|
|
color[0] = (color[0] == limit[0]) ? invl[0] : color[0] + inc[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
for (int y = 0; y < fBmp[1].height(); ++y) {
|
|
|
|
uint32_t* pixels = fBmp[1].getAddr32(0, y);
|
|
|
|
for (int j = 0; j < fBmp[1].width(); ++j) {
|
|
|
|
pixels[j] = SkColorSetARGB(0, color[1], color[2], 0);
|
|
|
|
color[1] = (color[1] == limit[1]) ? invl[1] : color[1] + inc[1];
|
|
|
|
color[2] = (color[2] == limit[2]) ? invl[2] : color[2] + inc[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext =
|
|
|
|
canvas->internal_private_accessTopLayerRenderTargetContext();
|
|
|
|
if (!renderTargetContext) {
|
2016-06-09 20:24:48 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrContext* context = canvas->getGrContext();
|
|
|
|
if (!context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-02 23:18:38 +00:00
|
|
|
sk_sp<GrTextureProxy> proxy[3];
|
2016-06-09 20:24:48 +00:00
|
|
|
|
2017-01-23 20:30:35 +00:00
|
|
|
{
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
int index = (0 == i) ? 0 : 1;
|
|
|
|
|
|
|
|
desc.fWidth = fBmp[index].width();
|
|
|
|
desc.fHeight = fBmp[index].height();
|
2017-01-30 13:06:27 +00:00
|
|
|
desc.fConfig = SkImageInfo2GrPixelConfig(fBmp[index].info(), *context->caps());
|
2017-01-23 20:30:35 +00:00
|
|
|
|
|
|
|
proxy[i] = GrSurfaceProxy::MakeDeferred(*context->caps(),
|
2017-03-04 13:12:46 +00:00
|
|
|
context->resourceProvider(),
|
2017-01-23 20:30:35 +00:00
|
|
|
desc, SkBudgeted::kYes,
|
|
|
|
fBmp[index].getPixels(),
|
|
|
|
fBmp[index].rowBytes());
|
|
|
|
if (!proxy[i]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-09 20:24:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kDrawPad = 10.f;
|
|
|
|
constexpr SkScalar kTestPad = 10.f;
|
|
|
|
constexpr SkScalar kColorSpaceOffset = 36.f;
|
2016-06-09 20:24:48 +00:00
|
|
|
SkISize sizes[3] = {{YSIZE, YSIZE}, {USIZE, USIZE}, {VSIZE, VSIZE}};
|
|
|
|
|
|
|
|
for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
|
|
|
|
SkRect renderRect =
|
|
|
|
SkRect::MakeWH(SkIntToScalar(fBmp[0].width()), SkIntToScalar(fBmp[0].height()));
|
|
|
|
renderRect.outset(kDrawPad, kDrawPad);
|
|
|
|
|
|
|
|
SkScalar y = kDrawPad + kTestPad + space * kColorSpaceOffset;
|
|
|
|
SkScalar x = kDrawPad + kTestPad;
|
|
|
|
|
2016-06-23 21:07:00 +00:00
|
|
|
GrPaint grPaint;
|
2017-01-09 16:46:10 +00:00
|
|
|
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
2016-06-09 20:24:48 +00:00
|
|
|
sk_sp<GrFragmentProcessor> fp(
|
2017-03-02 23:18:38 +00:00
|
|
|
GrYUVEffect::MakeYUVToRGB(context, proxy[0], proxy[1], proxy[2],
|
2016-11-04 20:26:16 +00:00
|
|
|
sizes, static_cast<SkYUVColorSpace>(space), true));
|
2016-06-09 20:24:48 +00:00
|
|
|
if (fp) {
|
|
|
|
SkMatrix viewMatrix;
|
|
|
|
viewMatrix.setTranslate(x, y);
|
2016-06-23 21:07:00 +00:00
|
|
|
grPaint.addColorFragmentProcessor(fp);
|
2017-01-03 14:42:58 +00:00
|
|
|
std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
|
|
|
|
GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
|
2017-01-11 18:42:54 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(std::move(grPaint),
|
|
|
|
GrAAType::kNone, std::move(op));
|
2016-06-09 20:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBmp[2];
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new YUVNV12toRGBEffect;)
|
2014-07-07 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|