blob: d1bb43f2fd58ab2ccd80025ee6d8b5a1ece7b806 [file] [log] [blame]
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkCodecImageGenerator.h"
#include "SkMakeUnique.h"
std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromEncodedCodec(sk_sp<SkData> data) {
SkCodec* codec = SkCodec::NewFromData(data);
if (nullptr == codec) {
return nullptr;
}
return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(codec, data));
}
static SkImageInfo make_premul(const SkImageInfo& info) {
if (kUnpremul_SkAlphaType == info.alphaType()) {
return info.makeAlphaType(kPremul_SkAlphaType);
}
return info;
}
SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData> data)
: INHERITED(make_premul(codec->getInfo()))
, fCodec(codec)
, fData(std::move(data))
{}
SkData* SkCodecImageGenerator::onRefEncodedData(
#ifdef SK_SUPPORT_GPU_REF_ENCODED_DATA
GrContext* ctx
#endif
) {
return SkRef(fData.get());
}
bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
SkPMColor ctable[], int* ctableCount) {
Options opts;
opts.fColorTable = ctable;
opts.fColorTableCount = ctableCount;
opts.fBehavior = SkTransferFunctionBehavior::kRespect;
return this->onGetPixels(info, pixels, rowBytes, opts);
}
bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
const Options& opts) {
SkCodec::Options codecOpts;
codecOpts.fPremulBehavior = opts.fBehavior;
SkCodec::Result result = fCodec->getPixels(info, pixels, rowBytes, &codecOpts, opts.fColorTable,
opts.fColorTableCount);
switch (result) {
case SkCodec::kSuccess:
case SkCodec::kIncompleteInput:
return true;
default:
return false;
}
}
bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const
{
return fCodec->queryYUV8(sizeInfo, colorSpace);
}
bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes);
switch (result) {
case SkCodec::kSuccess:
case SkCodec::kIncompleteInput:
return true;
default:
return false;
}
}