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 "SkSize.h"
#include "SkStream.h" #include "SkStream.h"
#include "SkTypes.h" #include "SkTypes.h"
#include "SkYUVSizeInfo.h"
class SkColorSpace; class SkColorSpace;
class SkData; class SkData;
@ -285,28 +286,6 @@ public:
*/ */
Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); 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 * If decoding to YUV is supported, this returns true. Otherwise, this
* returns false and does not modify any of the parameters. * 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, * @param colorSpace Output parameter. If non-NULL this is set to kJPEG,
* otherwise this is ignored. * otherwise this is ignored.
*/ */
bool queryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const { bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
if (nullptr == sizeInfo) { if (nullptr == sizeInfo) {
return false; return false;
} }
@ -334,7 +313,7 @@ public:
* recommendation (but not smaller). * recommendation (but not smaller).
* @param planes Memory for each of the Y, U, and V planes. * @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] || if (nullptr == planes || nullptr == planes[0] || nullptr == planes[1] ||
nullptr == planes[2]) { nullptr == planes[2]) {
return kInvalidInput; return kInvalidInput;
@ -543,11 +522,11 @@ protected:
SkPMColor ctable[], int* ctableCount, SkPMColor ctable[], int* ctableCount,
int* rowsDecoded) = 0; int* rowsDecoded) = 0;
virtual bool onQueryYUV8(YUVSizeInfo*, SkYUVColorSpace*) const { virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
return false; return false;
} }
virtual Result onGetYUV8Planes(const YUVSizeInfo&, void*[3] /*planes*/) { virtual Result onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return kUnimplemented; return kUnimplemented;
} }

View File

@ -11,6 +11,7 @@
#include "SkBitmap.h" #include "SkBitmap.h"
#include "SkColor.h" #include "SkColor.h"
#include "SkImageInfo.h" #include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class GrContext; class GrContext;
class GrTexture; class GrTexture;
@ -129,18 +130,26 @@ public:
bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); 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 * If decoding to YUV is supported, this returns true. Otherwise, this
* is 0, this imagegenerator should output the sizes and return true if it can efficiently * returns false and does not modify any of the parameters.
* 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 all planes and rowBytes entries are non NULL or non 0, then it should copy the * @param sizeInfo Output parameter indicating the sizes and required
* associated YUV data into those planes of memory supplied by the caller. It should validate * allocation widths of the Y, U, and V planes.
* that the sizes match what it expected. If the sizes do not match, it should return false. * @param colorSpace Output parameter.
*/ */
bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const;
SkYUVColorSpace* colorSpace);
/**
* 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 * 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, virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
SkPMColor ctable[], int* ctableCount); 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], virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
SkYUVColorSpace* colorSpace); return false;
}
virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return false;
}
virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) { virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) {
return nullptr; return nullptr;

View File

@ -18,6 +18,7 @@
#include "SkRefCnt.h" #include "SkRefCnt.h"
#include "SkSize.h" #include "SkSize.h"
#include "SkString.h" #include "SkString.h"
#include "SkYUVSizeInfo.h"
class SkColorTable; class SkColorTable;
class SkData; class SkData;
@ -210,19 +211,28 @@ public:
virtual GrTexture* getTexture() { return NULL; } virtual GrTexture* getTexture() { return NULL; }
/** /**
* If any planes or rowBytes is NULL, this should output the sizes and return true * If this can efficiently return YUV data, this should return true.
* if it can efficiently return YUV planar data. If it cannot, it should return false. * 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 * @param sizeInfo Output parameter indicating the sizes and required
* into those planes of memory supplied by the caller. It should validate that the sizes * allocation widths of the Y, U, and V planes.
* match what it expected. If the sizes do not match, it should return false. * @param colorSpace Output parameter.
*
* If colorSpace is not NULL, the YUV color space of the data should be stored in the address
* it points at.
*/ */
bool getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
SkYUVColorSpace* colorSpace) { return this->onQueryYUV8(sizeInfo, colorSpace);
return this->onGetYUV8Planes(sizes, planes, rowBytes, 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. */ /** Populates dst with the pixels of this pixelRef, converting them to colorType. */
@ -308,9 +318,12 @@ protected:
// default impl does nothing. // default impl does nothing.
virtual void onNotifyPixelsChanged(); virtual void onNotifyPixelsChanged();
// default impl returns false. virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
virtual bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], return false;
SkYUVColorSpace* colorSpace); }
virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
return false;
}
/** /**
* Returns the size (in bytes) of the internally allocated memory. * 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())) : INHERITED(make_premul(codec->getInfo()))
, fCodec(codec) , fCodec(codec)
, fData(SkRef(data)) , fData(SkRef(data))
, fYWidth(0)
, fUWidth(0)
, fVWidth(0)
{} {}
SkData* SkCodecImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { 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], bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const
SkYUVColorSpace* colorSpace) { {
// TODO (msarett): Change the YUV API in ImageGenerator to match SkCodec. return fCodec->queryYUV8(sizeInfo, colorSpace);
// This function is currently a hack to match the implementation }
// in SkCodec with the old API.
SkCodec::YUVSizeInfo sizeInfo;
// If planes is NULL, we just need to return the size. bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
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();
SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes); SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes);
if ((result == SkCodec::kSuccess || result == SkCodec::kIncompleteInput) && colorSpace) {
*colorSpace = kJPEG_SkYUVColorSpace;
}
switch (result) { switch (result) {
case SkCodec::kSuccess: case SkCodec::kSuccess:

View File

@ -26,8 +26,9 @@ protected:
bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[], bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, SkPMColor ctable[],
int* ctableCount) override; int* ctableCount) override;
bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const override;
SkYUVColorSpace* colorSpace) override;
bool onGetYUV8Planes(const SkYUVSizeInfo&, void* planes[3]) override;
private: private:
/* /*
@ -39,11 +40,5 @@ private:
SkAutoTDelete<SkCodec> fCodec; SkAutoTDelete<SkCodec> fCodec;
SkAutoTUnref<SkData> fData; 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; typedef SkImageGenerator INHERITED;
}; };

View File

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

View File

@ -46,9 +46,9 @@ protected:
Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
SkPMColor*, int*, int*) override; 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 { SkEncodedFormat onGetEncodedFormat() const override {
return kJPEG_SkEncodedFormat; return kJPEG_SkEncodedFormat;

View File

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

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); return this->getPixels(info, pixels, rowBytes, nullptr, nullptr);
} }
bool SkImageGenerator::getYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], bool SkImageGenerator::queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
SkYUVColorSpace* colorSpace) { SkASSERT(sizeInfo);
#ifdef SK_DEBUG
// In all cases, we need the sizes array
SkASSERT(sizes);
bool isValidWithPlanes = (planes) && (rowBytes) && return this->onQueryYUV8(sizeInfo, colorSpace);
((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);
} }
bool SkImageGenerator::onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3]) { bool SkImageGenerator::getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
return false; 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], return this->onGetYUV8Planes(sizeInfo, planes);
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);
} }
GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkIRect* subset) { GrTexture* SkImageGenerator::generateTexture(GrContext* ctx, const SkIRect* subset) {

View File

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

View File

@ -10,6 +10,7 @@
#include "SkCachedData.h" #include "SkCachedData.h"
#include "SkImageInfo.h" #include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class SkResourceCache; class SkResourceCache;
@ -19,17 +20,11 @@ public:
* The Info struct contains data about the 3 Y, U and V planes of memory stored * 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. * contiguously, in that order, as a single block of memory within SkYUVPlanesCache.
* *
* fSize: Width and height of each of the 3 planes (in pixels). * fSizeInfo: fWidth, fHeight, and fWidthBytes of each of the Y, U, and V planes.
* 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).
* fColorSpace: color space that will be used for the YUV -> RGB conversion. * fColorSpace: color space that will be used for the YUV -> RGB conversion.
*/ */
struct Info { struct Info {
SkISize fSize[3]; SkYUVSizeInfo fSizeInfo;
size_t fSizeInMemory[3];
size_t fRowBytes[3];
SkYUVColorSpace fColorSpace; SkYUVColorSpace fColorSpace;
}; };
/** /**

View File

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

View File

@ -10,6 +10,7 @@
#include "GrTypes.h" #include "GrTypes.h"
#include "SkImageInfo.h" #include "SkImageInfo.h"
#include "SkYUVSizeInfo.h"
class GrContext; class GrContext;
class GrTexture; class GrTexture;
@ -38,32 +39,29 @@ public:
virtual uint32_t onGetID() = 0; 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 // 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 * If decoding to YUV is supported, this returns true. Otherwise, this
* ignore the sizes parameter. Typical failure is that the provider does not contain YUV * returns false and does not modify any of the parameters.
* data, and may just be an RGB src. *
* @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. * Returns true on success and false on failure.
* planes[] will have already been allocated by the client (based on the worst-case sizes * This always attempts to perform a full decode. If the client only
* returned by onGetYUVSizes(). This method copies its planar data into those buffers. * 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], virtual bool onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) = 0;
size_t rowBytes[kPlaneCount], SkYUVColorSpace*) = 0;
}; };
#endif #endif

View File

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

View File

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

View File

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

View File

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

View File

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