1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2013 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef SkImageGenerator_DEFINED
9cb93a386Sopenharmony_ci#define SkImageGenerator_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h"
12cb93a386Sopenharmony_ci#include "include/core/SkColor.h"
13cb93a386Sopenharmony_ci#include "include/core/SkImage.h"
14cb93a386Sopenharmony_ci#include "include/core/SkImageInfo.h"
15cb93a386Sopenharmony_ci#include "include/core/SkYUVAPixmaps.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ciclass GrRecordingContext;
18cb93a386Sopenharmony_ciclass GrSurfaceProxyView;
19cb93a386Sopenharmony_ciclass GrSamplerState;
20cb93a386Sopenharmony_ciclass SkBitmap;
21cb93a386Sopenharmony_ciclass SkData;
22cb93a386Sopenharmony_ciclass SkMatrix;
23cb93a386Sopenharmony_ciclass SkPaint;
24cb93a386Sopenharmony_ciclass SkPicture;
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_cienum class GrImageTexGenPolicy : int;
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ciclass SK_API SkImageGenerator {
29cb93a386Sopenharmony_cipublic:
30cb93a386Sopenharmony_ci    /**
31cb93a386Sopenharmony_ci     *  The PixelRef which takes ownership of this SkImageGenerator
32cb93a386Sopenharmony_ci     *  will call the image generator's destructor.
33cb93a386Sopenharmony_ci     */
34cb93a386Sopenharmony_ci    virtual ~SkImageGenerator() { }
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    uint32_t uniqueID() const { return fUniqueID; }
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci    /**
39cb93a386Sopenharmony_ci     *  Return a ref to the encoded (i.e. compressed) representation
40cb93a386Sopenharmony_ci     *  of this data.
41cb93a386Sopenharmony_ci     *
42cb93a386Sopenharmony_ci     *  If non-NULL is returned, the caller is responsible for calling
43cb93a386Sopenharmony_ci     *  unref() on the data when it is finished.
44cb93a386Sopenharmony_ci     */
45cb93a386Sopenharmony_ci    sk_sp<SkData> refEncodedData() {
46cb93a386Sopenharmony_ci        return this->onRefEncodedData();
47cb93a386Sopenharmony_ci    }
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci    /**
50cb93a386Sopenharmony_ci     *  Return the ImageInfo associated with this generator.
51cb93a386Sopenharmony_ci     */
52cb93a386Sopenharmony_ci    const SkImageInfo& getInfo() const { return fInfo; }
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    /**
55cb93a386Sopenharmony_ci     *  Can this generator be used to produce images that will be drawable to the specified context
56cb93a386Sopenharmony_ci     *  (or to CPU, if context is nullptr)?
57cb93a386Sopenharmony_ci     */
58cb93a386Sopenharmony_ci    bool isValid(GrRecordingContext* context) const {
59cb93a386Sopenharmony_ci        return this->onIsValid(context);
60cb93a386Sopenharmony_ci    }
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    /**
63cb93a386Sopenharmony_ci     *  Decode into the given pixels, a block of memory of size at
64cb93a386Sopenharmony_ci     *  least (info.fHeight - 1) * rowBytes + (info.fWidth *
65cb93a386Sopenharmony_ci     *  bytesPerPixel)
66cb93a386Sopenharmony_ci     *
67cb93a386Sopenharmony_ci     *  Repeated calls to this function should give the same results,
68cb93a386Sopenharmony_ci     *  allowing the PixelRef to be immutable.
69cb93a386Sopenharmony_ci     *
70cb93a386Sopenharmony_ci     *  @param info A description of the format
71cb93a386Sopenharmony_ci     *         expected by the caller.  This can simply be identical
72cb93a386Sopenharmony_ci     *         to the info returned by getInfo().
73cb93a386Sopenharmony_ci     *
74cb93a386Sopenharmony_ci     *         This contract also allows the caller to specify
75cb93a386Sopenharmony_ci     *         different output-configs, which the implementation can
76cb93a386Sopenharmony_ci     *         decide to support or not.
77cb93a386Sopenharmony_ci     *
78cb93a386Sopenharmony_ci     *         A size that does not match getInfo() implies a request
79cb93a386Sopenharmony_ci     *         to scale. If the generator cannot perform this scale,
80cb93a386Sopenharmony_ci     *         it will return false.
81cb93a386Sopenharmony_ci     *
82cb93a386Sopenharmony_ci     *  @return true on success.
83cb93a386Sopenharmony_ci     */
84cb93a386Sopenharmony_ci    bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    bool getPixels(const SkPixmap& pm) {
87cb93a386Sopenharmony_ci        return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes());
88cb93a386Sopenharmony_ci    }
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci    /**
91cb93a386Sopenharmony_ci     *  If decoding to YUV is supported, this returns true. Otherwise, this
92cb93a386Sopenharmony_ci     *  returns false and the caller will ignore output parameter yuvaPixmapInfo.
93cb93a386Sopenharmony_ci     *
94cb93a386Sopenharmony_ci     * @param  supportedDataTypes Indicates the data type/planar config combinations that are
95cb93a386Sopenharmony_ci     *                            supported by the caller. If the generator supports decoding to
96cb93a386Sopenharmony_ci     *                            YUV(A), but not as a type in supportedDataTypes, this method
97cb93a386Sopenharmony_ci     *                            returns false.
98cb93a386Sopenharmony_ci     *  @param yuvaPixmapInfo Output parameter that specifies the planar configuration, subsampling,
99cb93a386Sopenharmony_ci     *                        orientation, chroma siting, plane color types, and row bytes.
100cb93a386Sopenharmony_ci     */
101cb93a386Sopenharmony_ci    bool queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
102cb93a386Sopenharmony_ci                       SkYUVAPixmapInfo* yuvaPixmapInfo) const;
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_ci    /**
105cb93a386Sopenharmony_ci     *  Returns true on success and false on failure.
106cb93a386Sopenharmony_ci     *  This always attempts to perform a full decode. To get the planar
107cb93a386Sopenharmony_ci     *  configuration without decoding use queryYUVAInfo().
108cb93a386Sopenharmony_ci     *
109cb93a386Sopenharmony_ci     *  @param yuvaPixmaps  Contains preallocated pixmaps configured according to a successful call
110cb93a386Sopenharmony_ci     *                      to queryYUVAInfo().
111cb93a386Sopenharmony_ci     */
112cb93a386Sopenharmony_ci    bool getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps);
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
115cb93a386Sopenharmony_ci    /**
116cb93a386Sopenharmony_ci     *  If the generator can natively/efficiently return its pixels as a GPU image (backed by a
117cb93a386Sopenharmony_ci     *  texture) this will return that image. If not, this will return NULL.
118cb93a386Sopenharmony_ci     *
119cb93a386Sopenharmony_ci     *  This routine also supports retrieving only a subset of the pixels. That subset is specified
120cb93a386Sopenharmony_ci     *  by the following rectangle:
121cb93a386Sopenharmony_ci     *
122cb93a386Sopenharmony_ci     *      subset = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height())
123cb93a386Sopenharmony_ci     *
124cb93a386Sopenharmony_ci     *  If subset is not contained inside the generator's bounds, this returns false.
125cb93a386Sopenharmony_ci     *
126cb93a386Sopenharmony_ci     *      whole = SkIRect::MakeWH(getInfo().width(), getInfo().height())
127cb93a386Sopenharmony_ci     *      if (!whole.contains(subset)) {
128cb93a386Sopenharmony_ci     *          return false;
129cb93a386Sopenharmony_ci     *      }
130cb93a386Sopenharmony_ci     *
131cb93a386Sopenharmony_ci     *  Regarding the GrRecordingContext parameter:
132cb93a386Sopenharmony_ci     *
133cb93a386Sopenharmony_ci     *  It must be non-NULL. The generator should only succeed if:
134cb93a386Sopenharmony_ci     *  - its internal context is the same
135cb93a386Sopenharmony_ci     *  - it can somehow convert its texture into one that is valid for the provided context.
136cb93a386Sopenharmony_ci     *
137cb93a386Sopenharmony_ci     *  If the willNeedMipMaps flag is true, the generator should try to create a TextureProxy that
138cb93a386Sopenharmony_ci     *  at least has the mip levels allocated and the base layer filled in. If this is not possible,
139cb93a386Sopenharmony_ci     *  the generator is allowed to return a non mipped proxy, but this will have some additional
140cb93a386Sopenharmony_ci     *  overhead in later allocating mips and copying of the base layer.
141cb93a386Sopenharmony_ci     *
142cb93a386Sopenharmony_ci     *  GrImageTexGenPolicy determines whether or not a new texture must be created (and its budget
143cb93a386Sopenharmony_ci     *  status) or whether this may (but is not required to) return a pre-existing texture that is
144cb93a386Sopenharmony_ci     *  retained by the generator (kDraw).
145cb93a386Sopenharmony_ci     */
146cb93a386Sopenharmony_ci    GrSurfaceProxyView generateTexture(GrRecordingContext*, const SkImageInfo& info,
147cb93a386Sopenharmony_ci                                       const SkIPoint& origin, GrMipmapped, GrImageTexGenPolicy);
148cb93a386Sopenharmony_ci
149cb93a386Sopenharmony_ci#endif
150cb93a386Sopenharmony_ci
151cb93a386Sopenharmony_ci    /**
152cb93a386Sopenharmony_ci     *  If the default image decoder system can interpret the specified (encoded) data, then
153cb93a386Sopenharmony_ci     *  this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way
154cb93a386Sopenharmony_ci     *  the caller is still responsible for managing their ownership of the data.
155cb93a386Sopenharmony_ci     */
156cb93a386Sopenharmony_ci    static std::unique_ptr<SkImageGenerator> MakeFromEncoded(sk_sp<SkData>);
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    /** Return a new image generator backed by the specified picture.  If the size is empty or
159cb93a386Sopenharmony_ci     *  the picture is NULL, this returns NULL.
160cb93a386Sopenharmony_ci     *  The optional matrix and paint arguments are passed to drawPicture() at rasterization
161cb93a386Sopenharmony_ci     *  time.
162cb93a386Sopenharmony_ci     */
163cb93a386Sopenharmony_ci    static std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize&, sk_sp<SkPicture>,
164cb93a386Sopenharmony_ci                                                             const SkMatrix*, const SkPaint*,
165cb93a386Sopenharmony_ci                                                             SkImage::BitDepth,
166cb93a386Sopenharmony_ci                                                             sk_sp<SkColorSpace>);
167cb93a386Sopenharmony_ci
168cb93a386Sopenharmony_ciprotected:
169cb93a386Sopenharmony_ci    static constexpr int kNeedNewImageUniqueID = 0;
170cb93a386Sopenharmony_ci
171cb93a386Sopenharmony_ci    SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID);
172cb93a386Sopenharmony_ci
173cb93a386Sopenharmony_ci    virtual sk_sp<SkData> onRefEncodedData() { return nullptr; }
174cb93a386Sopenharmony_ci    struct Options {};
175cb93a386Sopenharmony_ci    virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; }
176cb93a386Sopenharmony_ci    virtual bool onIsValid(GrRecordingContext*) const { return true; }
177cb93a386Sopenharmony_ci    virtual bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&,
178cb93a386Sopenharmony_ci                                 SkYUVAPixmapInfo*) const { return false; }
179cb93a386Sopenharmony_ci    virtual bool onGetYUVAPlanes(const SkYUVAPixmaps&) { return false; }
180cb93a386Sopenharmony_ci#if SK_SUPPORT_GPU
181cb93a386Sopenharmony_ci    // returns nullptr
182cb93a386Sopenharmony_ci    virtual GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
183cb93a386Sopenharmony_ci                                                 const SkIPoint&, GrMipmapped, GrImageTexGenPolicy);
184cb93a386Sopenharmony_ci#endif
185cb93a386Sopenharmony_ci
186cb93a386Sopenharmony_ciprivate:
187cb93a386Sopenharmony_ci    const SkImageInfo fInfo;
188cb93a386Sopenharmony_ci    const uint32_t fUniqueID;
189cb93a386Sopenharmony_ci
190cb93a386Sopenharmony_ci    friend class SkImage_Lazy;
191cb93a386Sopenharmony_ci
192cb93a386Sopenharmony_ci    // This is our default impl, which may be different on different platforms.
193cb93a386Sopenharmony_ci    // It is called from NewFromEncoded() after it has checked for any runtime factory.
194cb93a386Sopenharmony_ci    // The SkData will never be NULL, as that will have been checked by NewFromEncoded.
195cb93a386Sopenharmony_ci    static std::unique_ptr<SkImageGenerator> MakeFromEncodedImpl(sk_sp<SkData>);
196cb93a386Sopenharmony_ci
197cb93a386Sopenharmony_ci    SkImageGenerator(SkImageGenerator&&) = delete;
198cb93a386Sopenharmony_ci    SkImageGenerator(const SkImageGenerator&) = delete;
199cb93a386Sopenharmony_ci    SkImageGenerator& operator=(SkImageGenerator&&) = delete;
200cb93a386Sopenharmony_ci    SkImageGenerator& operator=(const SkImageGenerator&) = delete;
201cb93a386Sopenharmony_ci};
202cb93a386Sopenharmony_ci
203cb93a386Sopenharmony_ci#endif  // SkImageGenerator_DEFINED
204