blob: 8108f0de441c64744faf5f6ffe9626798c027356 [file] [log] [blame]
msarettedd2dcf2016-01-14 13:12:26 -08001/*
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
reed42943c82016-09-12 12:01:44 -070010SkImageGenerator* SkCodecImageGenerator::NewFromEncodedCodec(sk_sp<SkData> data) {
msarettedd2dcf2016-01-14 13:12:26 -080011 SkCodec* codec = SkCodec::NewFromData(data);
12 if (nullptr == codec) {
13 return nullptr;
14 }
15
16 return new SkCodecImageGenerator(codec, data);
17}
18
brianosman03707122016-04-21 08:48:18 -070019static SkImageInfo make_premul(const SkImageInfo& info) {
20 if (kUnpremul_SkAlphaType == info.alphaType()) {
21 return info.makeAlphaType(kPremul_SkAlphaType);
herb4d1061c2016-04-15 12:57:42 -070022 }
msarett3ec5db42016-03-23 06:50:59 -070023
brianosman03707122016-04-21 08:48:18 -070024 return info;
msarettb714fb02016-01-22 14:46:42 -080025}
26
reed42943c82016-09-12 12:01:44 -070027SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData> data)
brianosman03707122016-04-21 08:48:18 -070028 : INHERITED(make_premul(codec->getInfo()))
msarettedd2dcf2016-01-14 13:12:26 -080029 , fCodec(codec)
reed42943c82016-09-12 12:01:44 -070030 , fData(std::move(data))
msarettedd2dcf2016-01-14 13:12:26 -080031{}
32
33SkData* SkCodecImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) {
34 return SkRef(fData.get());
35}
36
37bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
38 SkPMColor ctable[], int* ctableCount) {
39
msarettdcd5e652016-08-22 08:48:40 -070040 // FIXME (msarett):
41 // We don't give the client the chance to request an SkColorSpace. Until we improve
42 // the API, let's assume that they want legacy mode.
43 SkImageInfo decodeInfo = info.makeColorSpace(nullptr);
44
45 SkCodec::Result result = fCodec->getPixels(decodeInfo, pixels, rowBytes, nullptr, ctable,
msarettedd2dcf2016-01-14 13:12:26 -080046 ctableCount);
47 switch (result) {
48 case SkCodec::kSuccess:
49 case SkCodec::kIncompleteInput:
50 return true;
51 default:
52 return false;
53 }
54}
55
msarett4984c3c2016-03-10 05:44:43 -080056bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const
57{
58 return fCodec->queryYUV8(sizeInfo, colorSpace);
59}
msarettb714fb02016-01-22 14:46:42 -080060
msarett4984c3c2016-03-10 05:44:43 -080061bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
msarettb714fb02016-01-22 14:46:42 -080062 SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes);
msarettb714fb02016-01-22 14:46:42 -080063
64 switch (result) {
65 case SkCodec::kSuccess:
66 case SkCodec::kIncompleteInput:
67 return true;
68 default:
69 return false;
70 }
msarettedd2dcf2016-01-14 13:12:26 -080071}