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"
|
|
|
|
#include "include/core/SkColorPriv.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/SkPoint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTo.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrGpu.h"
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
class GrRenderTargetContext;
|
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
static sk_sp<SkColorFilter> yuv_to_rgb_colorfilter() {
|
|
|
|
static const float kJPEGConversionMatrix[20] = {
|
2019-04-30 16:18:54 +00:00
|
|
|
1.0f, 0.0f, 1.402f, 0.0f, -180.0f/255,
|
|
|
|
1.0f, -0.344136f, -0.714136f, 0.0f, 136.0f/255,
|
|
|
|
1.0f, 1.772f, 0.0f, 0.0f, -227.6f/255,
|
2019-03-06 17:36:47 +00:00
|
|
|
0.0f, 0.0f, 0.0f, 1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2019-04-30 16:18:54 +00:00
|
|
|
return SkColorFilters::Matrix(kJPEGConversionMatrix);
|
2019-03-06 17:36:47 +00:00
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2019-03-06 17:36:47 +00:00
|
|
|
return SkISize::Make(kBmpSize + 2 * kPad, 390);
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
// We create an RGB bitmap and then extract YUV bmps where the U and V bitmaps are
|
|
|
|
// subsampled by 2 in both dimensions.
|
|
|
|
SkPaint paint;
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor kColors[] =
|
2015-05-29 18:37:25 +00:00
|
|
|
{ SK_ColorBLUE, SK_ColorYELLOW, SK_ColorGREEN, SK_ColorWHITE };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(0,0), kBmpSize / 2.f, kColors,
|
|
|
|
nullptr, SK_ARRAY_COUNT(kColors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror));
|
2015-05-29 18:37:25 +00:00
|
|
|
SkBitmap rgbBmp;
|
|
|
|
rgbBmp.allocN32Pixels(kBmpSize, kBmpSize, true);
|
|
|
|
SkCanvas canvas(rgbBmp);
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
SkPMColor* rgbColors = static_cast<SkPMColor*>(rgbBmp.getPixels());
|
|
|
|
|
|
|
|
SkImageInfo yinfo = SkImageInfo::MakeA8(kBmpSize, kBmpSize);
|
|
|
|
fYUVBmps[0].allocPixels(yinfo);
|
|
|
|
SkImageInfo uinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
|
|
|
|
fYUVBmps[1].allocPixels(uinfo);
|
|
|
|
SkImageInfo vinfo = SkImageInfo::MakeA8(kBmpSize / 2, kBmpSize / 2);
|
|
|
|
fYUVBmps[2].allocPixels(vinfo);
|
|
|
|
unsigned char* yPixels;
|
|
|
|
signed char* uvPixels[2];
|
|
|
|
yPixels = static_cast<unsigned char*>(fYUVBmps[0].getPixels());
|
|
|
|
uvPixels[0] = static_cast<signed char*>(fYUVBmps[1].getPixels());
|
2015-07-16 14:46:07 +00:00
|
|
|
uvPixels[1] = static_cast<signed char*>(fYUVBmps[2].getPixels());
|
2015-05-29 18:37:25 +00:00
|
|
|
|
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
|
2015-05-29 18:37:25 +00:00
|
|
|
for (int i = 0; i < kBmpSize * kBmpSize; ++i) {
|
|
|
|
yPixels[i] = static_cast<unsigned char>(0.299f * SkGetPackedR32(rgbColors[i]) +
|
|
|
|
0.587f * SkGetPackedG32(rgbColors[i]) +
|
|
|
|
0.114f * SkGetPackedB32(rgbColors[i]));
|
|
|
|
}
|
|
|
|
for (int j = 0; j < kBmpSize / 2; ++j) {
|
|
|
|
for (int i = 0; i < kBmpSize / 2; ++i) {
|
|
|
|
// Average together 4 pixels of RGB.
|
|
|
|
int rgb[] = { 0, 0, 0 };
|
|
|
|
for (int y = 0; y < 2; ++y) {
|
|
|
|
for (int x = 0; x < 2; ++x) {
|
|
|
|
int rgbIndex = (2 * j + y) * kBmpSize + 2 * i + x;
|
|
|
|
rgb[0] += SkGetPackedR32(rgbColors[rgbIndex]);
|
|
|
|
rgb[1] += SkGetPackedG32(rgbColors[rgbIndex]);
|
|
|
|
rgb[2] += SkGetPackedB32(rgbColors[rgbIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int c = 0; c < 3; ++c) {
|
|
|
|
rgb[c] /= 4;
|
|
|
|
}
|
|
|
|
int uvIndex = j * kBmpSize / 2 + i;
|
|
|
|
uvPixels[0][uvIndex] = static_cast<signed char>(
|
|
|
|
((-38 * rgb[0] - 74 * rgb[1] + 112 * rgb[2] + 128) >> 8) + 128);
|
|
|
|
uvPixels[1][uvIndex] = static_cast<signed char>(
|
|
|
|
((112 * rgb[0] - 94 * rgb[1] - 18 * rgb[2] + 128) >> 8) + 128);
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
fRGBImage = SkImage::MakeRasterCopy(SkPixmap(rgbBmp.info(), rgbColors, rgbBmp.rowBytes()));
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:22:45 +00:00
|
|
|
void createYUVTextures(GrContext* context, GrBackendTexture yuvTextures[3]) {
|
2015-05-29 18:37:25 +00:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
SkASSERT(fYUVBmps[i].width() == SkToInt(fYUVBmps[i].rowBytes()));
|
2019-06-10 19:09:34 +00:00
|
|
|
yuvTextures[i] = context->priv().createBackendTexture(&fYUVBmps[i].pixmap(), 1,
|
|
|
|
GrRenderable::kNo);
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:19:33 +00:00
|
|
|
void createResultTexture(GrContext* context, int width, int height,
|
|
|
|
GrBackendTexture* resultTexture) {
|
2019-06-04 15:03:06 +00:00
|
|
|
*resultTexture = context->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
width, height, kRGBA_8888_SkColorType, SkColors::kTransparent,
|
|
|
|
GrMipMapped::kNo, GrRenderable::kYes);
|
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 {
|
2019-03-06 17:36:47 +00:00
|
|
|
// draw the original
|
|
|
|
SkScalar yOffset = kPad;
|
|
|
|
canvas->drawImage(fRGBImage.get(), kPad, yOffset);
|
|
|
|
yOffset += kBmpSize + kPad;
|
2015-05-29 18:37:25 +00:00
|
|
|
|
|
|
|
for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
|
2017-12-13 14:22:45 +00:00
|
|
|
GrBackendTexture yuvTextures[3];
|
|
|
|
this->createYUVTextures(context, yuvTextures);
|
2019-03-06 17:36:47 +00:00
|
|
|
auto image = SkImage::MakeFromYUVTexturesCopy(context,
|
|
|
|
static_cast<SkYUVColorSpace>(space),
|
|
|
|
yuvTextures,
|
|
|
|
kTopLeft_GrSurfaceOrigin);
|
2018-09-18 15:19:33 +00:00
|
|
|
this->deleteBackendTextures(context, yuvTextures, 3);
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
SkPaint paint;
|
|
|
|
if (kIdentity_SkYUVColorSpace == space) {
|
|
|
|
// The identity color space needs post-processing to appear correct
|
|
|
|
paint.setColorFilter(yuv_to_rgb_colorfilter());
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawImage(image.get(), kPad, yOffset, &paint);
|
|
|
|
yOffset += kBmpSize + kPad;
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
2018-09-18 01:57:11 +00:00
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
for (int space = kJPEG_SkYUVColorSpace; space <= kLastEnum_SkYUVColorSpace; ++space) {
|
2018-09-18 01:57:11 +00:00
|
|
|
GrBackendTexture yuvTextures[3];
|
2018-09-18 15:19:33 +00:00
|
|
|
GrBackendTexture resultTexture;
|
2018-09-18 01:57:11 +00:00
|
|
|
this->createYUVTextures(context, yuvTextures);
|
2018-09-18 15:19:33 +00:00
|
|
|
this->createResultTexture(
|
|
|
|
context, yuvTextures[0].width(), yuvTextures[0].height(), &resultTexture);
|
2019-03-06 17:36:47 +00:00
|
|
|
auto image = SkImage::MakeFromYUVTexturesCopyWithExternalBackend(
|
|
|
|
context,
|
|
|
|
static_cast<SkYUVColorSpace>(space),
|
|
|
|
yuvTextures,
|
|
|
|
kTopLeft_GrSurfaceOrigin,
|
|
|
|
resultTexture);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
if (kIdentity_SkYUVColorSpace == space) {
|
|
|
|
// The identity color space needs post-processing to appear correct
|
|
|
|
paint.setColorFilter(yuv_to_rgb_colorfilter());
|
|
|
|
}
|
|
|
|
canvas->drawImage(image.get(), kPad, yOffset, &paint);
|
|
|
|
yOffset += kBmpSize + kPad;
|
2018-09-18 01:57:11 +00:00
|
|
|
|
2018-09-18 15:19:33 +00:00
|
|
|
GrBackendTexture texturesToDelete[4]{
|
|
|
|
yuvTextures[0],
|
|
|
|
yuvTextures[1],
|
|
|
|
yuvTextures[2],
|
|
|
|
resultTexture,
|
|
|
|
};
|
|
|
|
this->deleteBackendTextures(context, texturesToDelete, 4);
|
2018-09-18 01:57:11 +00:00
|
|
|
}
|
2015-05-29 18:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fRGBImage;
|
|
|
|
SkBitmap fYUVBmps[3];
|
2015-05-29 18:37:25 +00:00
|
|
|
|
2019-03-06 17:36:47 +00:00
|
|
|
static constexpr SkScalar kPad = 10.0f;
|
|
|
|
static constexpr int kBmpSize = 32;
|
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
|
|
|
}
|