msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 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 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 10 | SkImageGenerator* SkCodecImageGenerator::NewFromEncodedCodec(sk_sp<SkData> data) { |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 11 | SkCodec* codec = SkCodec::NewFromData(data); |
| 12 | if (nullptr == codec) { |
| 13 | return nullptr; |
| 14 | } |
| 15 | |
| 16 | return new SkCodecImageGenerator(codec, data); |
| 17 | } |
| 18 | |
brianosman | 0370712 | 2016-04-21 08:48:18 -0700 | [diff] [blame] | 19 | static SkImageInfo make_premul(const SkImageInfo& info) { |
| 20 | if (kUnpremul_SkAlphaType == info.alphaType()) { |
| 21 | return info.makeAlphaType(kPremul_SkAlphaType); |
herb | 4d1061c | 2016-04-15 12:57:42 -0700 | [diff] [blame] | 22 | } |
msarett | 3ec5db4 | 2016-03-23 06:50:59 -0700 | [diff] [blame] | 23 | |
brianosman | 0370712 | 2016-04-21 08:48:18 -0700 | [diff] [blame] | 24 | return info; |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 25 | } |
| 26 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 27 | SkCodecImageGenerator::SkCodecImageGenerator(SkCodec* codec, sk_sp<SkData> data) |
brianosman | 0370712 | 2016-04-21 08:48:18 -0700 | [diff] [blame] | 28 | : INHERITED(make_premul(codec->getInfo())) |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 29 | , fCodec(codec) |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 30 | , fData(std::move(data)) |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 31 | {} |
| 32 | |
| 33 | SkData* SkCodecImageGenerator::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { |
| 34 | return SkRef(fData.get()); |
| 35 | } |
| 36 | |
| 37 | bool SkCodecImageGenerator::onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 38 | SkPMColor ctable[], int* ctableCount) { |
| 39 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 40 | // 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, |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 46 | ctableCount); |
| 47 | switch (result) { |
| 48 | case SkCodec::kSuccess: |
| 49 | case SkCodec::kIncompleteInput: |
| 50 | return true; |
| 51 | default: |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 56 | bool SkCodecImageGenerator::onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const |
| 57 | { |
| 58 | return fCodec->queryYUV8(sizeInfo, colorSpace); |
| 59 | } |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 60 | |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 61 | bool SkCodecImageGenerator::onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 62 | SkCodec::Result result = fCodec->getYUV8Planes(sizeInfo, planes); |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 63 | |
| 64 | switch (result) { |
| 65 | case SkCodec::kSuccess: |
| 66 | case SkCodec::kIncompleteInput: |
| 67 | return true; |
| 68 | default: |
| 69 | return false; |
| 70 | } |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 71 | } |