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