blob: 3087ab21f63e54b14447502f3c273bc91e6581f8 [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 */
Hal Canary03a7f5f2017-02-10 09:06:38 -05007#ifndef SkCodecImageGenerator_DEFINED
8#define SkCodecImageGenerator_DEFINED
msarettedd2dcf2016-01-14 13:12:26 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkCodec.h"
11#include "include/core/SkData.h"
12#include "include/core/SkImageGenerator.h"
msarettedd2dcf2016-01-14 13:12:26 -080013
14class SkCodecImageGenerator : public SkImageGenerator {
15public:
16 /*
17 * If this data represents an encoded image that we know how to decode,
18 * return an SkCodecImageGenerator. Otherwise return nullptr.
msarettedd2dcf2016-01-14 13:12:26 -080019 */
Mike Reed185130c2017-02-15 15:14:16 -050020 static std::unique_ptr<SkImageGenerator> MakeFromEncodedCodec(sk_sp<SkData>);
msarettedd2dcf2016-01-14 13:12:26 -080021
Florin Malitaf8776c22018-11-06 19:16:28 -050022 static std::unique_ptr<SkImageGenerator> MakeFromCodec(std::unique_ptr<SkCodec>);
23
Brian Osmand9eb2192020-07-09 15:45:56 -040024 /**
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 Field439709a2020-07-13 10:18:15 -070035 /**
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
msarettedd2dcf2016-01-14 13:12:26 -080093protected:
Ben Wagnerbdf54332018-05-15 14:12:14 -040094 sk_sp<SkData> onRefEncodedData() override;
msarettedd2dcf2016-01-14 13:12:26 -080095
Dan Field439709a2020-07-13 10:18:15 -070096 bool onGetPixels(const SkImageInfo& info,
97 void* pixels,
98 size_t rowBytes,
99 const Options& opts) override;
msarettedd2dcf2016-01-14 13:12:26 -0800100
Brian Salomon59c60b02020-09-01 15:01:15 -0400101 bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&,
102 SkYUVAPixmapInfo*) const override;
msarett4984c3c2016-03-10 05:44:43 -0800103
Brian Salomonbe0e42c2020-08-27 11:00:04 -0400104 bool onGetYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) override;
msarettedd2dcf2016-01-14 13:12:26 -0800105
106private:
107 /*
108 * Takes ownership of codec
msarettedd2dcf2016-01-14 13:12:26 -0800109 */
Mike Reedede7bac2017-07-23 15:30:02 -0400110 SkCodecImageGenerator(std::unique_ptr<SkCodec>, sk_sp<SkData>);
msarettedd2dcf2016-01-14 13:12:26 -0800111
Ben Wagner145dbcd2016-11-03 14:40:50 -0400112 std::unique_ptr<SkCodec> fCodec;
bungemanffae30d2016-08-03 13:32:32 -0700113 sk_sp<SkData> fData;
msarettedd2dcf2016-01-14 13:12:26 -0800114
John Stiles7571f9e2020-09-02 22:42:33 -0400115 using INHERITED = SkImageGenerator;
msarettedd2dcf2016-01-14 13:12:26 -0800116};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500117#endif // SkCodecImageGenerator_DEFINED