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 | */ |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 7 | #ifndef SkCodecImageGenerator_DEFINED |
| 8 | #define SkCodecImageGenerator_DEFINED |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/codec/SkCodec.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkImageGenerator.h" |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 13 | |
| 14 | class SkCodecImageGenerator : public SkImageGenerator { |
| 15 | public: |
| 16 | /* |
| 17 | * If this data represents an encoded image that we know how to decode, |
| 18 | * return an SkCodecImageGenerator. Otherwise return nullptr. |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 19 | */ |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 20 | static std::unique_ptr<SkImageGenerator> MakeFromEncodedCodec(sk_sp<SkData>); |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 21 | |
Florin Malita | f8776c2 | 2018-11-06 19:16:28 -0500 | [diff] [blame] | 22 | static std::unique_ptr<SkImageGenerator> MakeFromCodec(std::unique_ptr<SkCodec>); |
| 23 | |
Brian Osman | d9eb219 | 2020-07-09 15:45:56 -0400 | [diff] [blame] | 24 | /** |
| 25 | * Return a size that approximately supports the desired scale factor. The codec may not be able |
| 26 | * to scale efficiently to the exact scale factor requested, so return a size that approximates |
| 27 | * that scale. The returned value is the codec's suggestion for the closest valid scale that it |
| 28 | * can natively support. |
| 29 | * |
| 30 | * This is similar to SkCodec::getScaledDimensions, but adjusts the returned dimensions based |
| 31 | * on the image's EXIF orientation. |
| 32 | */ |
| 33 | SkISize getScaledDimensions(float desiredScale) const; |
| 34 | |
Dan Field | 439709a | 2020-07-13 10:18:15 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Decode into the given pixels, a block of memory of size at |
| 37 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 38 | * bytesPerPixel) |
| 39 | * |
| 40 | * Repeated calls to this function should give the same results, |
| 41 | * allowing the PixelRef to be immutable. |
| 42 | * |
| 43 | * @param info A description of the format |
| 44 | * expected by the caller. This can simply be identical |
| 45 | * to the info returned by getInfo(). |
| 46 | * |
| 47 | * This contract also allows the caller to specify |
| 48 | * different output-configs, which the implementation can |
| 49 | * decide to support or not. |
| 50 | * |
| 51 | * A size that does not match getInfo() implies a request |
| 52 | * to scale. If the generator cannot perform this scale, |
| 53 | * it will return false. |
| 54 | * |
| 55 | * @return true on success. |
| 56 | */ |
| 57 | bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const SkCodec::Options* options = nullptr); |
| 58 | |
| 59 | /** |
| 60 | * Return the number of frames in the image. |
| 61 | * |
| 62 | * May require reading through the stream. |
| 63 | */ |
| 64 | int getFrameCount() { return fCodec->getFrameCount(); } |
| 65 | |
| 66 | /** |
| 67 | * Return info about a single frame. |
| 68 | * |
| 69 | * Only supported by multi-frame images. Does not read through the stream, |
| 70 | * so it should be called after getFrameCount() to parse any frames that |
| 71 | * have not already been parsed. |
| 72 | */ |
| 73 | bool getFrameInfo(int index, SkCodec::FrameInfo* info) const { |
| 74 | return fCodec->getFrameInfo(index, info); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return the number of times to repeat, if this image is animated. This number does not |
| 79 | * include the first play through of each frame. For example, a repetition count of 4 means |
| 80 | * that each frame is played 5 times and then the animation stops. |
| 81 | * |
| 82 | * It can return kRepetitionCountInfinite, a negative number, meaning that the animation |
| 83 | * should loop forever. |
| 84 | * |
| 85 | * May require reading the stream to find the repetition count. |
| 86 | * |
| 87 | * As such, future decoding calls may require a rewind. |
| 88 | * |
| 89 | * For still (non-animated) image codecs, this will return 0. |
| 90 | */ |
| 91 | int getRepetitionCount() { return fCodec->getRepetitionCount(); } |
| 92 | |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 93 | protected: |
Ben Wagner | bdf5433 | 2018-05-15 14:12:14 -0400 | [diff] [blame] | 94 | sk_sp<SkData> onRefEncodedData() override; |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 95 | |
Dan Field | 439709a | 2020-07-13 10:18:15 -0700 | [diff] [blame] | 96 | bool onGetPixels(const SkImageInfo& info, |
| 97 | void* pixels, |
| 98 | size_t rowBytes, |
| 99 | const Options& opts) override; |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 100 | |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame] | 101 | bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&, |
| 102 | SkYUVAPixmapInfo*) const override; |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 103 | |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 104 | bool onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) override; |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | /* |
| 108 | * Takes ownership of codec |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 109 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 110 | SkCodecImageGenerator(std::unique_ptr<SkCodec>, sk_sp<SkData>); |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 111 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 112 | std::unique_ptr<SkCodec> fCodec; |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 113 | sk_sp<SkData> fData; |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 114 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 115 | using INHERITED = SkImageGenerator; |
msarett | edd2dcf | 2016-01-14 13:12:26 -0800 | [diff] [blame] | 116 | }; |
Hal Canary | 03a7f5f | 2017-02-10 09:06:38 -0500 | [diff] [blame] | 117 | #endif // SkCodecImageGenerator_DEFINED |