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