1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkCodecImageGenerator.h"
9 #include "SkMakeUnique.h"
10
MakeFromEncodedCodec(sk_sp<SkData> data)11 std::unique_ptr<SkImageGenerator> SkCodecImageGenerator::MakeFromEncodedCodec(sk_sp<SkData> data) {
12 SkCodec* codec = SkCodec::NewFromData(data);
13 if (nullptr == codec) {
14 return nullptr;
15 }
16
17 return std::unique_ptr<SkImageGenerator>(new SkCodecImageGenerator(codec, data));
18 }
19
adjust_info(const SkImageInfo & info)20 static SkImageInfo adjust_info(const SkImageInfo& info) {
21 SkImageInfo newInfo = info;
22 if (kUnpremul_SkAlphaType == info.alphaType()) {
23 newInfo = newInfo.makeAlphaType(kPremul_SkAlphaType);
24 }
25 return newInfo;
26 }
27
SkCodecImageGenerator(SkCodec * codec,sk_sp<SkData> data)28 SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData> data)
29 : INHERITED(adjust_info(codec->getInfo()))
30 , fCodec(codec)
31 , fData(std::move(data))
32 {}
33
onRefEncodedData()34 SkData* SkCodecImageGenerator::onRefEncodedData() {
35 return SkRef(fData.get());
36 }
37
onGetPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options & opts)38 bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
39 const Options& opts) {
40 SkCodec::Options codecOpts;
41 codecOpts.fPremulBehavior = opts.fBehavior;
42 SkCodec::Result result = fCodec->getPixels(info, pixels, rowBytes, &codecOpts);
43 switch (result) {
44 case SkCodec::kSuccess:
45 case SkCodec::kIncompleteInput:
46 case SkCodec::kErrorInInput:
47 return true;
48 default:
49 return false;
50 }
51 }
52
onQueryYUV8(SkYUVSizeInfo * sizeInfo,SkYUVColorSpace * colorSpace) const53 bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const
54 {
55 return fCodec->queryYUV8(sizeInfo, colorSpace);
56 }
57
onGetYUV8Planes(const SkYUVSizeInfo & sizeInfo,void * planes[3])58 bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
59 SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes);
60
61 switch (result) {
62 case SkCodec::kSuccess:
63 case SkCodec::kIncompleteInput:
64 case SkCodec::kErrorInInput:
65 return true;
66 default:
67 return false;
68 }
69 }
70