Update Skia's YUV API

We should match the recently designed API in SkCodec.
https://codereview.chromium.org/1549473003/

This requires changes in Chromium as well.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1716523002

Review URL: https://codereview.chromium.org/1716523002
This commit is contained in:
msarett 2016-03-07 08:39:12 -08:00 committed by Commit bot
parent a9101eef5f
commit 095d31c8a0
19 changed files with 262 additions and 286 deletions

View File

@ -15,6 +15,7 @@
#include "SkSize.h"
#include "SkStream.h"
#include "SkTypes.h"
#include "SkYUVSizeInfo.h"
class SkColorSpace;
class SkData;
@ -285,28 +286,6 @@ public:
*/
Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
struct YUVSizeInfo {
SkISize fYSize;
SkISize fUSize;
SkISize fVSize;
/**
* While the widths of the Y, U, and V planes are not restricted, the
* implementation requires that the width of the memory allocated for
* each plane be a multiple of DCTSIZE (which is always 8).
*
* This struct allows us to inform the client how many "widthBytes"
* that we need. Note that we use the new idea of "widthBytes"
* because this idea is distinct from "rowBytes" (used elsewhere in
* Skia). "rowBytes" allow the last row of the allocation to not
* include any extra padding, while, in this case, every single row of
* the allocation must be at least "widthBytes".
*/
size_t fYWidthBytes;
size_t fUWidthBytes;
size_t fVWidthBytes;
};
/**
* If decoding to YUV is supported, this returns true. Otherwise, this
* returns false and does not modify any of the parameters.
@ -316,7 +295,7 @@ public:
* @param colorSpace Output parameter. If non-NULL this is set to kJPEG,
* otherwise this is ignored.
*/
bool queryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
if (nullptr == sizeInfo) {
return false;
}
@ -334,7 +313,7 @@ public:
* recommendation (but not smaller).
* @param planes Memory for each of the Y, U, and V planes.
*/
Result getYUV8Planes(const YUVSizeInfo& sizeInfo, void* planes[3]) {
Result getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
if (nullptr == planes || nullptr == planes[0] || nullptr == planes[1] ||
nullptr == planes[2]) {
return kInvalidInput;
@ -543,11 +522,11 @@ protected:
SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) = 0;
virtual bool onQueryYUV8(YUVSizeInfo*, SkYUVColorSpace*) const {
virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
return false;
}
virtual Result onGetYUV8Planes(const YUVSizeInfo&, void*[3] /*planes*/) {
virtual Result onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return kUnimplemented;
}

View File

@ -11,6 +11,7 @@
#include "SkBitmap.h"
#include "SkColor.h"
#include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class GrContext;
class GrTexture;
@ -129,18 +130,26 @@ public:
bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
/**
* If planes or rowBytes is NULL or if any entry in planes is NULL or if any entry in rowBytes
* is 0, this imagegenerator should output the sizes and return true if it can efficiently
* return YUV planar data. If it cannot, it should return false. Note that either planes and
* rowBytes are both fully defined and non NULL/non 0 or they are both NULL or have NULL or 0
* entries only. Having only partial planes/rowBytes information is not supported.
* If decoding to YUV is supported, this returns true. Otherwise, this
* returns false and does not modify any of the parameters.
*
* If all planes and rowBytes entries are non NULL or non 0, then it should copy the
* associated YUV data into those planes of memory supplied by the caller. It should validate
* that the sizes match what it expected. If the sizes do not match, it should return false.
* @param sizeInfo Output parameter indicating the sizes and required
* allocation widths of the Y, U, and V planes.
* @param colorSpace Output parameter.
*/
bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace);
bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const;
/**
* Returns true on success and false on failure.
* This always attempts to perform a full decode. If the client only
* wants size, it should call queryYUV8().
*
* @param sizeInfo Needs to exactly match the values returned by the
* query, except the WidthBytes may be larger than the
* recommendation (but not smaller).
* @param planes Memory for each of the Y, U, and V planes.
*/
bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]);
/**
* If the generator can natively/efficiently return its pixels as a GPU image (backed by a
@ -248,9 +257,13 @@ protected:
virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
SkPMColor ctable[], int* ctableCount);
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]);
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace);
virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
return false;
}
virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return false;
}
virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) {
return nullptr;

View File

@ -18,6 +18,7 @@
#include "SkRefCnt.h"
#include "SkSize.h"
#include "SkString.h"
#include "SkYUVSizeInfo.h"
class SkColorTable;
class SkData;
@ -210,19 +211,28 @@ public:
virtual GrTexture* getTexture() { return NULL; }
/**
* If any planes or rowBytes is NULL, this should output the sizes and return true
* if it can efficiently return YUV planar data. If it cannot, it should return false.
* If this can efficiently return YUV data, this should return true.
* Otherwise this returns false and does not modify any of the parameters.
*
* If all planes and rowBytes are not NULL, then it should copy the associated Y,U,V data
* into those planes of memory supplied by the caller. It should validate that the sizes
* match what it expected. If the sizes do not match, it should return false.
*
* If colorSpace is not NULL, the YUV color space of the data should be stored in the address
* it points at.
* @param sizeInfo Output parameter indicating the sizes and required
* allocation widths of the Y, U, and V planes.
* @param colorSpace Output parameter.
*/
bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) {
return this->onGetYUV8Planes(sizes, planes, rowBytes, colorSpace);
bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
return this->onQueryYUV8(sizeInfo, colorSpace);
}
/**
* Returns true on success and false on failure.
* Copies YUV data into the provided YUV planes.
*
* @param sizeInfo Needs to exactly match the values returned by the
* query, except the WidthBytes may be larger than the
* recommendation (but not smaller).
* @param planes Memory for each of the Y, U, and V planes.
*/
bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
return this->onGetYUV8Planes(sizeInfo, planes);
}
/** Populates dst with the pixels of this pixelRef, converting them to colorType. */
@ -308,9 +318,12 @@ protected:
// default impl does nothing.
virtual void onNotifyPixelsChanged();
// default impl returns false.
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace);
virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
return false;
}
virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return false;
}
/**
* Returns the size (in bytes) of the internally allocated memory.

View File

@ -0,0 +1,34 @@
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkYUVSizeInfo_DEFINED
#define SkYUVSizeInfo_DEFINED
struct SkYUVSizeInfo {
enum {
kY = 0,
kU = 1,
kV = 2,
};
SkISize fSizes[3];
/**
* While the widths of the Y, U, and V planes are not restricted, the
* implementation often requires that the width of the memory allocated
* for each plane be a multiple of 8.
*
* This struct allows us to inform the client how many "widthBytes"
* that we need. Note that we use the new idea of "widthBytes"
* because this idea is distinct from "rowBytes" (used elsewhere in
* Skia). "rowBytes" allow the last row of the allocation to not
* include any extra padding, while, in this case, every single row of
* the allocation must be at least "widthBytes".
*/
size_t fWidthBytes[3];
};
#endif // SkYUVSizeInfo_DEFINED

View File

@ -28,9 +28,6 @@ SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, SkData* data)
: INHERITED(make_premul(codec->getInfo()))
, fCodec(codec)
, fData(SkRef(data))
, fYWidth(0)
, fUWidth(0)
, fVWidth(0)
{}
SkData* SkCodecImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) {
@ -51,47 +48,13 @@ bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, s
}
}
bool SkCodecImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) {
// TODO (msarett): Change the YUV API in ImageGenerator to match SkCodec.
// This function is currently a hack to match the implementation
// in SkCodec with the old API.
SkCodec::YUVSizeInfo sizeInfo;
bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const
{
return fCodec->queryYUV8(sizeInfo, colorSpace);
}
// If planes is NULL, we just need to return the size.
if (nullptr == planes) {
bool result = fCodec->queryYUV8(&sizeInfo, colorSpace);
if (result) {
// Save the true widths
fYWidth = sizeInfo.fYSize.width();
fUWidth = sizeInfo.fUSize.width();
fVWidth = sizeInfo.fVSize.width();
// Set the sizes so that the client allocates enough memory
sizes[0].fWidth = (int) sizeInfo.fYWidthBytes;
sizes[0].fHeight = sizeInfo.fYSize.height();
sizes[1].fWidth = (int) sizeInfo.fUWidthBytes;
sizes[1].fHeight = sizeInfo.fUSize.height();
sizes[2].fWidth = (int) sizeInfo.fVWidthBytes;
sizes[2].fHeight = sizeInfo.fVSize.height();
}
return result;
}
// Set the sizeInfo with the true widths and heights
SkASSERT(fYWidth != 0 && fUWidth != 0 && fVWidth != 0);
sizeInfo.fYSize.set(fYWidth, sizes[0].height());
sizeInfo.fUSize.set(fUWidth, sizes[1].height());
sizeInfo.fVSize.set(fVWidth, sizes[2].height());
// Set the sizeInfo with the allocated widths
sizeInfo.fYWidthBytes = sizes[0].width();
sizeInfo.fUWidthBytes = sizes[1].width();
sizeInfo.fVWidthBytes = sizes[2].width();
bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes);
if ((result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput) && colorSpace) {
*colorSpace = kJPEG_SkYUVColorSpace;
}
switch (result) {
case SkCodec::kSuccess:

View File

@ -26,8 +26,9 @@ protected:
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[],
int* ctableCount) override;
bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) override;
bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const override;
bool onGetYUV8Planes(const SkYUVSizeInfo&, void* planes[3]) override;
private:
/*
@ -39,11 +40,5 @@ private:
SkAutoTDelete<SkCodec> fCodec;
SkAutoTUnref<SkData> fData;
// FIXME: These fields are necessary only until we change the API of SkImageGenerator
// to match SkCodec. Once the API is changed, they should be removed.
int fYWidth;
int fUWidth;
int fVWidth;
typedef SkImageGenerator INHERITED;
};

View File

@ -594,21 +594,21 @@ static bool is_yuv_supported(jpeg_decompress_struct* dinfo) {
(4 == hSampY && 2 == vSampY);
}
bool SkJpegCodec::onQueryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
bool SkJpegCodec::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
jpeg_decompress_struct* dinfo = fDecoderMgr->dinfo();
if (!is_yuv_supported(dinfo)) {
return false;
}
sizeInfo->fYSize.set(dinfo->comp_info[0].downsampled_width,
dinfo->comp_info[0].downsampled_height);
sizeInfo->fUSize.set(dinfo->comp_info[1].downsampled_width,
dinfo->comp_info[1].downsampled_height);
sizeInfo->fVSize.set(dinfo->comp_info[2].downsampled_width,
dinfo->comp_info[2].downsampled_height);
sizeInfo->fYWidthBytes = dinfo->comp_info[0].width_in_blocks * DCTSIZE;
sizeInfo->fUWidthBytes = dinfo->comp_info[1].width_in_blocks * DCTSIZE;
sizeInfo->fVWidthBytes = dinfo->comp_info[2].width_in_blocks * DCTSIZE;
sizeInfo->fSizes[SkYUVSizeInfo::kY].set(dinfo->comp_info[0].downsampled_width,
dinfo->comp_info[0].downsampled_height);
sizeInfo->fSizes[SkYUVSizeInfo::kU].set(dinfo->comp_info[1].downsampled_width,
dinfo->comp_info[1].downsampled_height);
sizeInfo->fSizes[SkYUVSizeInfo::kV].set(dinfo->comp_info[2].downsampled_width,
dinfo->comp_info[2].downsampled_height);
sizeInfo->fWidthBytes[SkYUVSizeInfo::kY] = dinfo->comp_info[0].width_in_blocks * DCTSIZE;
sizeInfo->fWidthBytes[SkYUVSizeInfo::kU] = dinfo->comp_info[1].width_in_blocks * DCTSIZE;
sizeInfo->fWidthBytes[SkYUVSizeInfo::kV] = dinfo->comp_info[2].width_in_blocks * DCTSIZE;
if (colorSpace) {
*colorSpace = kJPEG_SkYUVColorSpace;
@ -617,17 +617,18 @@ bool SkJpegCodec::onQueryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace
return true;
}
SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void* pixels[3]) {
YUVSizeInfo defaultInfo;
SkCodec::Result SkJpegCodec::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
SkYUVSizeInfo defaultInfo;
// This will check is_yuv_supported(), so we don't need to here.
bool supportsYUV = this->onQueryYUV8(&defaultInfo, nullptr);
if (!supportsYUV || sizeInfo.fYSize != defaultInfo.fYSize ||
sizeInfo.fUSize != defaultInfo.fUSize ||
sizeInfo.fVSize != defaultInfo.fVSize ||
sizeInfo.fYWidthBytes < defaultInfo.fYWidthBytes ||
sizeInfo.fUWidthBytes < defaultInfo.fUWidthBytes ||
sizeInfo.fVWidthBytes < defaultInfo.fVWidthBytes) {
if (!supportsYUV ||
sizeInfo.fSizes[SkYUVSizeInfo::kY] != defaultInfo.fSizes[SkYUVSizeInfo::kY] ||
sizeInfo.fSizes[SkYUVSizeInfo::kU] != defaultInfo.fSizes[SkYUVSizeInfo::kU] ||
sizeInfo.fSizes[SkYUVSizeInfo::kV] != defaultInfo.fSizes[SkYUVSizeInfo::kV] ||
sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kY] ||
sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kU] ||
sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] < defaultInfo.fWidthBytes[SkYUVSizeInfo::kV]) {
return fDecoderMgr->returnFailure("onGetYUV8Planes", kInvalidInput);
}
@ -651,9 +652,9 @@ SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void*
// Currently, we require that the Y plane dimensions match the image dimensions
// and that the U and V planes are the same dimensions.
SkASSERT(sizeInfo.fUSize == sizeInfo.fVSize);
SkASSERT((uint32_t) sizeInfo.fYSize.width() == dinfo->output_width &&
(uint32_t) sizeInfo.fYSize.height() == dinfo->output_height);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU] == sizeInfo.fSizes[SkYUVSizeInfo::kV]);
SkASSERT((uint32_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].width() == dinfo->output_width &&
(uint32_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].height() == dinfo->output_height);
// Build a JSAMPIMAGE to handle output from libjpeg-turbo. A JSAMPIMAGE has
// a 2-D array of pixels for each of the components (Y, U, V) in the image.
@ -670,17 +671,20 @@ SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void*
// Initialize rowptrs.
int numYRowsPerBlock = DCTSIZE * dinfo->comp_info[0].v_samp_factor;
for (int i = 0; i < numYRowsPerBlock; i++) {
rowptrs[i] = SkTAddOffset<JSAMPLE>(pixels[0], i * sizeInfo.fYWidthBytes);
rowptrs[i] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kY],
i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kY]);
}
for (int i = 0; i < DCTSIZE; i++) {
rowptrs[i + 2 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[1], i * sizeInfo.fUWidthBytes);
rowptrs[i + 3 * DCTSIZE] = SkTAddOffset<JSAMPLE>(pixels[2], i * sizeInfo.fVWidthBytes);
rowptrs[i + 2 * DCTSIZE] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kU],
i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kU]);
rowptrs[i + 3 * DCTSIZE] = SkTAddOffset<JSAMPLE>(planes[SkYUVSizeInfo::kV],
i * sizeInfo.fWidthBytes[SkYUVSizeInfo::kV]);
}
// After each loop iteration, we will increment pointers to Y, U, and V.
size_t blockIncrementY = numYRowsPerBlock * sizeInfo.fYWidthBytes;
size_t blockIncrementU = DCTSIZE * sizeInfo.fUWidthBytes;
size_t blockIncrementV = DCTSIZE * sizeInfo.fVWidthBytes;
size_t blockIncrementY = numYRowsPerBlock * sizeInfo.fWidthBytes[SkYUVSizeInfo::kY];
size_t blockIncrementU = DCTSIZE * sizeInfo.fWidthBytes[SkYUVSizeInfo::kU];
size_t blockIncrementV = DCTSIZE * sizeInfo.fWidthBytes[SkYUVSizeInfo::kV];
uint32_t numRowsPerBlock = numYRowsPerBlock;
@ -713,7 +717,7 @@ SkCodec::Result SkJpegCodec::onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void*
// this requirement using a dummy row buffer.
// FIXME: Should SkCodec have an extra memory buffer that can be shared among
// all of the implementations that use temporary/garbage memory?
SkAutoTMalloc<JSAMPLE> dummyRow(sizeInfo.fYWidthBytes);
SkAutoTMalloc<JSAMPLE> dummyRow(sizeInfo.fWidthBytes[SkYUVSizeInfo::kY]);
for (int i = remainingRows; i < numYRowsPerBlock; i++) {
rowptrs[i] = dummyRow.get();
}

View File

@ -46,9 +46,9 @@ protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
SkPMColor*, int*, int*) override;
bool onQueryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
Result onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void* pixels[3]) override;
Result onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override;
SkEncodedFormat onGetEncodedFormat() const override {
return kJPEG_SkEncodedFormat;

View File

@ -213,12 +213,11 @@ public:
Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
uint32_t onGetID() override { return fGen->uniqueID(); }
bool onGetYUVSizes(SkISize sizes[3]) override {
return fGen->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
return fGen->queryYUV8(sizeInfo, colorSpace);
}
bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* space) override {
return fGen->getYUV8Planes(sizes, planes, rowBytes, space);
bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
return fGen->getYUV8Planes(sizeInfo, planes);
}
};

View File

@ -52,56 +52,28 @@ bool SkImageGenerator::getPixels(const SkImageInfo& info, void* pixels, size_t r
return this->getPixels(info, pixels, rowBytes, nullptr, nullptr);
}
bool SkImageGenerator::getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) {
#ifdef SK_DEBUG
// In all cases, we need the sizes array
SkASSERT(sizes);
bool SkImageGenerator::queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
SkASSERT(sizeInfo);
bool isValidWithPlanes = (planes) && (rowBytes) &&
((planes[0]) && (planes[1]) && (planes[2]) &&
(0 != rowBytes[0]) && (0 != rowBytes[1]) && (0 != rowBytes[2]));
bool isValidWithoutPlanes =
((nullptr == planes) ||
((nullptr == planes[0]) && (nullptr == planes[1]) && (nullptr == planes[2]))) &&
((nullptr == rowBytes) ||
((0 == rowBytes[0]) && (0 == rowBytes[1]) && (0 == rowBytes[2])));
// Either we have all planes and rowBytes information or we have none of it
// Having only partial information is not supported
SkASSERT(isValidWithPlanes || isValidWithoutPlanes);
// If we do have planes information, make sure all sizes are non 0
// and all rowBytes are valid
SkASSERT(!isValidWithPlanes ||
((sizes[0].fWidth >= 0) &&
(sizes[0].fHeight >= 0) &&
(sizes[1].fWidth >= 0) &&
(sizes[1].fHeight >= 0) &&
(sizes[2].fWidth >= 0) &&
(sizes[2].fHeight >= 0) &&
(rowBytes[0] >= (size_t)sizes[0].fWidth) &&
(rowBytes[1] >= (size_t)sizes[1].fWidth) &&
(rowBytes[2] >= (size_t)sizes[2].fWidth)));
#endif
return this->onGetYUV8Planes(sizes, planes, rowBytes, colorSpace);
return this->onQueryYUV8(sizeInfo, colorSpace);
}
bool SkImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]) {
return false;
}
bool SkImageGenerator::getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth >= 0);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight >= 0);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU].fWidth >= 0);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight >= 0);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kV].fWidth >= 0);
SkASSERT(sizeInfo.fSizes[SkYUVSizeInfo::kV].fHeight >= 0);
SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] >=
(size_t) sizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth);
SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] >=
(size_t) sizeInfo.fSizes[SkYUVSizeInfo::kU].fWidth);
SkASSERT(sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] >=
(size_t) sizeInfo.fSizes[SkYUVSizeInfo::kV].fWidth);
SkASSERT(planes && planes[0] && planes[1] && planes[2]);
bool SkImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) {
// In order to maintain compatibility with clients that implemented the original
// onGetYUV8Planes interface, we assume that the color space is JPEG.
// TODO(rileya): remove this and the old onGetYUV8Planes once clients switch over to
// the new interface.
if (colorSpace) {
*colorSpace = kJPEG_SkYUVColorSpace;
}
return this->onGetYUV8Planes(sizes, planes, rowBytes);
return this->onGetYUV8Planes(sizeInfo, planes);
}
GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkIRect* subset) {

View File

@ -323,11 +323,6 @@ SkData* SkPixelRef::onRefEncodedData() {
return nullptr;
}
bool SkPixelRef::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* colorSpace) {
return false;
}
size_t SkPixelRef::getAllocatedSizeInBytes() const {
return 0;
}

View File

@ -10,6 +10,7 @@
#include "SkCachedData.h"
#include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class SkResourceCache;
@ -19,17 +20,11 @@ public:
* The Info struct contains data about the 3 Y, U and V planes of memory stored
* contiguously, in that order, as a single block of memory within SkYUVPlanesCache.
*
* fSize: Width and height of each of the 3 planes (in pixels).
* fSizeInMemory: Amount of memory allocated for each plane (may be different from
"height * rowBytes", depending on the jpeg decoder's block size).
* The sum of these is the total size stored within SkYUVPlanesCache.
* fRowBytes: rowBytes for each of the 3 planes (in bytes).
* fSizeInfo: fWidth, fHeight, and fWidthBytes of each of the Y, U, and V planes.
* fColorSpace: color space that will be used for the YUV -> RGB conversion.
*/
struct Info {
SkISize fSize[3];
size_t fSizeInMemory[3];
size_t fRowBytes[3];
SkYUVSizeInfo fSizeInfo;
SkYUVColorSpace fColorSpace;
};
/**

View File

@ -40,21 +40,20 @@ bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v
if (fCachedData.get()) {
planes[0] = (void*)fCachedData->data();
planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0];
planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1];
planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
} else {
// Fetch yuv plane sizes for memory allocation. Here, width and height can be
// rounded up to JPEG block size and be larger than the image's width and height.
if (!provider->onGetYUVSizes(yuvInfo->fSize)) {
// Fetch yuv plane sizes for memory allocation.
if (!provider->onQueryYUV8(&yuvInfo->fSizeInfo, &yuvInfo->fColorSpace)) {
return false;
}
// Allocate the memory for YUV
size_t totalSize(0);
for (int i = 0; i < GrYUVProvider::kPlaneCount; ++i) {
yuvInfo->fRowBytes[i] = yuvInfo->fSize[i].fWidth; // we assume snug fit: rb == width
yuvInfo->fSizeInMemory[i] = yuvInfo->fRowBytes[i] * yuvInfo->fSize[i].fHeight;
totalSize += yuvInfo->fSizeInMemory[i];
for (int i = 0; i < 3; i++) {
totalSize += yuvInfo->fSizeInfo.fWidthBytes[i] * yuvInfo->fSizeInfo.fSizes[i].fHeight;
}
if (useCache) {
fCachedData.reset(SkResourceCache::NewCachedData(totalSize));
@ -63,12 +62,13 @@ bool YUVScoper::init(GrYUVProvider* provider, SkYUVPlanesCache::Info* yuvInfo, v
fStorage.reset(totalSize);
planes[0] = fStorage.get();
}
planes[1] = (uint8_t*)planes[0] + yuvInfo->fSizeInMemory[0];
planes[2] = (uint8_t*)planes[1] + yuvInfo->fSizeInMemory[1];
planes[1] = (uint8_t*)planes[0] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kY] *
yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
planes[2] = (uint8_t*)planes[1] + (yuvInfo->fSizeInfo.fWidthBytes[SkYUVSizeInfo::kU] *
yuvInfo->fSizeInfo.fSizes[SkYUVSizeInfo::kU].fHeight);
// Get the YUV planes and update plane sizes to actual image size
if (!provider->onGetYUVPlanes(yuvInfo->fSize, planes, yuvInfo->fRowBytes,
&yuvInfo->fColorSpace)) {
// Get the YUV planes.
if (!provider->onGetYUV8Planes(yuvInfo->fSizeInfo, planes)) {
return false;
}
@ -91,20 +91,21 @@ GrTexture* GrYUVProvider::refAsTexture(GrContext* ctx, const GrSurfaceDesc& desc
GrSurfaceDesc yuvDesc;
yuvDesc.fConfig = kAlpha_8_GrPixelConfig;
SkAutoTUnref<GrTexture> yuvTextures[3];
for (int i = 0; i < 3; ++i) {
yuvDesc.fWidth = yuvInfo.fSize[i].fWidth;
yuvDesc.fHeight = yuvInfo.fSize[i].fHeight;
for (int i = 0; i < 3; i++) {
yuvDesc.fWidth = yuvInfo.fSizeInfo.fSizes[i].fWidth;
yuvDesc.fHeight = yuvInfo.fSizeInfo.fSizes[i].fHeight;
// TODO: why do we need this check?
bool needsExactTexture = (yuvDesc.fWidth != yuvInfo.fSize[0].fWidth) ||
(yuvDesc.fHeight != yuvInfo.fSize[0].fHeight);
bool needsExactTexture =
(yuvDesc.fWidth != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth) ||
(yuvDesc.fHeight != yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
if (needsExactTexture) {
yuvTextures[i].reset(ctx->textureProvider()->createTexture(yuvDesc, SkBudgeted::kYes));
} else {
yuvTextures[i].reset(ctx->textureProvider()->createApproxTexture(yuvDesc));
}
if (!yuvTextures[i] ||
!yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight,
yuvDesc.fConfig, planes[i], yuvInfo.fRowBytes[i])) {
!yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, yuvDesc.fConfig,
planes[i], yuvInfo.fSizeInfo.fWidthBytes[i])) {
return nullptr;
}
}
@ -126,11 +127,12 @@ GrTexture* GrYUVProvider::refAsTexture(GrContext* ctx, const GrSurfaceDesc& desc
GrYUVEffect::CreateYUVToRGB(yuvTextures[0],
yuvTextures[1],
yuvTextures[2],
yuvInfo.fSize,
yuvInfo.fSizeInfo.fSizes,
yuvInfo.fColorSpace));
paint.addColorFragmentProcessor(yuvToRgbProcessor);
paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
const SkRect r = SkRect::MakeIWH(yuvInfo.fSize[0].fWidth, yuvInfo.fSize[0].fHeight);
const SkRect r = SkRect::MakeIWH(yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fWidth,
yuvInfo.fSizeInfo.fSizes[SkYUVSizeInfo::kY].fHeight);
SkAutoTUnref<GrDrawContext> drawContext(ctx->drawContext(renderTarget));
if (!drawContext) {

View File

@ -10,6 +10,7 @@
#include "GrTypes.h"
#include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class GrContext;
class GrTexture;
@ -38,32 +39,29 @@ public:
virtual uint32_t onGetID() = 0;
enum {
kY_Index = 0,
kU_Index = 1,
kV_Index = 2,
kPlaneCount = 3
};
// These are not meant to be called by a client, only by the implementation
/**
* Return the 3 dimensions for each plane and return true. On failure, return false and
* ignore the sizes parameter. Typical failure is that the provider does not contain YUV
* data, and may just be an RGB src.
* If decoding to YUV is supported, this returns true. Otherwise, this
* returns false and does not modify any of the parameters.
*
* @param sizeInfo Output parameter indicating the sizes and required
* allocation widths of the Y, U, and V planes.
* @param colorSpace Output parameter.
*/
virtual bool onGetYUVSizes(SkISize sizes[kPlaneCount]) = 0;
virtual bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const = 0;
/**
* On success, return true, and set sizes, rowbytes and colorspace to the appropriate values.
* planes[] will have already been allocated by the client (based on the worst-case sizes
* returned by onGetYUVSizes(). This method copies its planar data into those buffers.
* Returns true on success and false on failure.
* This always attempts to perform a full decode. If the client only
* wants size, it should call onQueryYUV8().
*
* On failure, return false and ignore other parameters.
* @param sizeInfo Needs to exactly match the values returned by the
* query, except the WidthBytes may be larger than the
* recommendation (but not smaller).
* @param planes Memory for each of the Y, U, and V planes.
*/
virtual bool onGetYUVPlanes(SkISize sizes[kPlaneCount], void* planes[kPlaneCount],
size_t rowBytes[kPlaneCount], SkYUVColorSpace*) = 0;
virtual bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) = 0;
};
#endif

View File

@ -178,12 +178,11 @@ public:
PixelRef_GrYUVProvider(SkPixelRef* pr) : fPR(pr) {}
uint32_t onGetID() override { return fPR->getGenerationID(); }
bool onGetYUVSizes(SkISize sizes[3]) override {
return fPR->getYUV8Planes(sizes, nullptr, nullptr, nullptr);
bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
return fPR->queryYUV8(sizeInfo, colorSpace);
}
bool onGetYUVPlanes(SkISize sizes[3], void* planes[3], size_t rowBytes[3],
SkYUVColorSpace* space) override {
return fPR->getYUV8Planes(sizes, planes, rowBytes, space);
bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
return fPR->getYUV8Planes(sizeInfo, planes);
}
};

View File

@ -54,16 +54,22 @@ private:
size_t rowBytes,
SkDiscardableMemory::Factory* factory);
bool onGetYUV8Planes(SkISize sizes[3],
void* planes[3],
size_t rowBytes[3],
SkYUVColorSpace* colorSpace) override {
bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override {
// If the image was already decoded with lockPixels(), favor not
// re-decoding to YUV8 planes.
if (fDiscardableMemory) {
return false;
}
return fGenerator->getYUV8Planes(sizes, planes, rowBytes, colorSpace);
return fGenerator->queryYUV8(sizeInfo, colorSpace);
}
bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override {
// If the image was already decoded with lockPixels(), favor not
// re-decoding to YUV8 planes.
if (fDiscardableMemory) {
return false;
}
return fGenerator->getYUV8Planes(sizeInfo, planes);
}
friend bool SkDEPRECATED_InstallDiscardablePixelRef(SkImageGenerator*, const SkIRect*, SkBitmap*,

View File

@ -46,27 +46,25 @@ public:
DEF_TEST(ImageGenerator, reporter) {
MyImageGenerator ig;
SkISize sizes[3];
sizes[0] = SkISize::Make(200, 200);
sizes[1] = SkISize::Make(100, 100);
sizes[2] = SkISize::Make( 50, 50);
void* planes[3] = { nullptr };
size_t rowBytes[3] = { 0 };
SkYUVSizeInfo sizeInfo;
sizeInfo.fSizes[SkYUVSizeInfo::kY] = SkISize::Make(200, 200);
sizeInfo.fSizes[SkYUVSizeInfo::kU] = SkISize::Make(100, 100);
sizeInfo.fSizes[SkYUVSizeInfo::kV] = SkISize::Make( 50, 50);
sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 0;
sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 0;
sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 0;
void* planes[3] = { nullptr };
SkYUVColorSpace colorSpace;
// Check that the YUV decoding API does not cause any crashes
ig.getYUV8Planes(sizes, nullptr, nullptr, &colorSpace);
ig.getYUV8Planes(sizes, nullptr, nullptr, nullptr);
ig.getYUV8Planes(sizes, planes, nullptr, nullptr);
ig.getYUV8Planes(sizes, nullptr, rowBytes, nullptr);
ig.getYUV8Planes(sizes, planes, rowBytes, nullptr);
ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
ig.queryYUV8(&sizeInfo, nullptr);
ig.queryYUV8(&sizeInfo, &colorSpace);
sizeInfo.fWidthBytes[SkYUVSizeInfo::kY] = 250;
sizeInfo.fWidthBytes[SkYUVSizeInfo::kU] = 250;
sizeInfo.fWidthBytes[SkYUVSizeInfo::kV] = 250;
int dummy;
planes[0] = planes[1] = planes[2] = &dummy;
rowBytes[0] = rowBytes[1] = rowBytes[2] = 250;
ig.getYUV8Planes(sizes, planes, rowBytes, &colorSpace);
planes[SkYUVSizeInfo::kY] = planes[SkYUVSizeInfo::kU] = planes[SkYUVSizeInfo::kV] = &dummy;
ig.getYUV8Planes(sizeInfo, planes);
// Suppressed due to https://code.google.com/p/skia/issues/detail?id=4339
if (false) {

View File

@ -32,11 +32,10 @@ DEF_TEST(YUVPlanesCache, reporter) {
SkResourceCache cache(1024);
SkYUVPlanesCache::Info yuvInfo;
for (int i = 0; i < 3; ++i) {
yuvInfo.fSize[i].fWidth = 20 * i;
yuvInfo.fSize[i].fHeight = 10 * i;
yuvInfo.fSizeInMemory[i] = 800 * i;
yuvInfo.fRowBytes[i] = 80 * i;
for (int i = 0; i < 3; i++) {
yuvInfo.fSizeInfo.fSizes[i].fWidth = 20 * i;
yuvInfo.fSizeInfo.fSizes[i].fHeight = 10 * i;
yuvInfo.fSizeInfo.fWidthBytes[i] = 80 * i;
}
yuvInfo.fColorSpace = kRec601_SkYUVColorSpace;
@ -61,10 +60,12 @@ DEF_TEST(YUVPlanesCache, reporter) {
REPORTER_ASSERT(reporter, data);
REPORTER_ASSERT(reporter, data->size() == size);
for (int i = 0; i < 3; ++i) {
REPORTER_ASSERT(reporter, yuvInfo.fSize[i].fWidth == yuvInfoRead.fSize[i].fWidth);
REPORTER_ASSERT(reporter, yuvInfo.fSize[i].fHeight == yuvInfoRead.fSize[i].fHeight);
REPORTER_ASSERT(reporter, yuvInfo.fSizeInMemory[i] == yuvInfoRead.fSizeInMemory[i]);
REPORTER_ASSERT(reporter, yuvInfo.fRowBytes[i] == yuvInfoRead.fRowBytes[i]);
REPORTER_ASSERT(reporter, yuvInfo.fSizeInfo.fSizes[i].fWidth ==
yuvInfoRead.fSizeInfo.fSizes[i].fWidth);
REPORTER_ASSERT(reporter, yuvInfo.fSizeInfo.fSizes[i].fHeight ==
yuvInfoRead.fSizeInfo.fSizes[i].fHeight);
REPORTER_ASSERT(reporter, yuvInfo.fSizeInfo.fWidthBytes[i] ==
yuvInfoRead.fSizeInfo.fWidthBytes[i]);
}
REPORTER_ASSERT(reporter, yuvInfo.fColorSpace == yuvInfoRead.fColorSpace);

View File

@ -9,6 +9,7 @@
#include "Resources.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkYUVSizeInfo.h"
#include "Test.h"
static SkStreamAsset* resource(const char path[]) {
@ -31,7 +32,7 @@ static void codec_yuv(skiatest::Reporter* reporter,
}
// Test queryYUV8()
SkCodec::YUVSizeInfo info;
SkYUVSizeInfo info;
bool success = codec->queryYUV8(nullptr, nullptr);
REPORTER_ASSERT(reporter, !success);
success = codec->queryYUV8(&info, nullptr);
@ -41,27 +42,36 @@ static void codec_yuv(skiatest::Reporter* reporter,
}
REPORTER_ASSERT(reporter,
0 == memcmp((const void*) &info, (const void*) expectedSizes, 3 * sizeof(SkISize)));
REPORTER_ASSERT(reporter, info.fYWidthBytes == (uint32_t) SkAlign8(info.fYSize.width()));
REPORTER_ASSERT(reporter, info.fUWidthBytes == (uint32_t) SkAlign8(info.fUSize.width()));
REPORTER_ASSERT(reporter, info.fVWidthBytes == (uint32_t) SkAlign8(info.fVSize.width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kY] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kY].width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kU] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kU].width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kV] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kV].width()));
SkYUVColorSpace colorSpace;
success = codec->queryYUV8(&info, &colorSpace);
REPORTER_ASSERT(reporter,
0 == memcmp((const void*) &info, (const void*) expectedSizes, 3 * sizeof(SkISize)));
REPORTER_ASSERT(reporter, info.fYWidthBytes == (uint32_t) SkAlign8(info.fYSize.width()));
REPORTER_ASSERT(reporter, info.fUWidthBytes == (uint32_t) SkAlign8(info.fUSize.width()));
REPORTER_ASSERT(reporter, info.fVWidthBytes == (uint32_t) SkAlign8(info.fVSize.width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kY] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kY].width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kU] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kU].width()));
REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kV] ==
(uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kV].width()));
REPORTER_ASSERT(reporter, kJPEG_SkYUVColorSpace == colorSpace);
// Allocate the memory for the YUV decode
size_t totalBytes = info.fYWidthBytes * info.fYSize.height() +
info.fUWidthBytes * info.fUSize.height() +
info.fVWidthBytes * info.fVSize.height();
size_t totalBytes =
info.fWidthBytes[SkYUVSizeInfo::kY] * info.fSizes[SkYUVSizeInfo::kY].height() +
info.fWidthBytes[SkYUVSizeInfo::kU] * info.fSizes[SkYUVSizeInfo::kU].height() +
info.fWidthBytes[SkYUVSizeInfo::kV] * info.fSizes[SkYUVSizeInfo::kV].height();
SkAutoMalloc storage(totalBytes);
void* planes[3];
planes[0] = storage.get();
planes[1] = SkTAddOffset<void>(planes[0], info.fYWidthBytes * info.fYSize.height());
planes[2] = SkTAddOffset<void>(planes[1], info.fUWidthBytes * info.fUSize.height());
planes[1] = SkTAddOffset<void>(planes[0],
info.fWidthBytes[SkYUVSizeInfo::kY] * info.fSizes[SkYUVSizeInfo::kY].height());
planes[2] = SkTAddOffset<void>(planes[1],
info.fWidthBytes[SkYUVSizeInfo::kU] * info.fSizes[SkYUVSizeInfo::kU].height());
// Test getYUV8Planes()
REPORTER_ASSERT(reporter, SkCodec::kInvalidInput ==