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/SkYUVAIndex.h" 16 #include "include/core/SkYUVASizeInfo.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 class SK_API SkImageGenerator { 28 public: 29 /** 30 * The PixelRef which takes ownership of this SkImageGenerator 31 * will call the image generator's destructor. 32 */ ~SkImageGenerator()33 virtual ~SkImageGenerator() { } 34 uniqueID()35 uint32_t uniqueID() const { return fUniqueID; } 36 37 /** 38 * Return a ref to the encoded (i.e. compressed) representation 39 * of this data. 40 * 41 * If non-NULL is returned, the caller is responsible for calling 42 * unref() on the data when it is finished. 43 */ refEncodedData()44 sk_sp<SkData> refEncodedData() { 45 return this->onRefEncodedData(); 46 } 47 48 /** 49 * Return the ImageInfo associated with this generator. 50 */ getInfo()51 const SkImageInfo& getInfo() const { return fInfo; } 52 53 /** 54 * Can this generator be used to produce images that will be drawable to the specified context 55 * (or to CPU, if context is nullptr)? 56 */ isValid(GrContext * context)57 bool isValid(GrContext* context) const { 58 return this->onIsValid(context); 59 } 60 61 /** 62 * Decode into the given pixels, a block of memory of size at 63 * least (info.fHeight - 1) * rowBytes + (info.fWidth * 64 * bytesPerPixel) 65 * 66 * Repeated calls to this function should give the same results, 67 * allowing the PixelRef to be immutable. 68 * 69 * @param info A description of the format 70 * expected by the caller. This can simply be identical 71 * to the info returned by getInfo(). 72 * 73 * This contract also allows the caller to specify 74 * different output-configs, which the implementation can 75 * decide to support or not. 76 * 77 * A size that does not match getInfo() implies a request 78 * to scale. If the generator cannot perform this scale, 79 * it will return false. 80 * 81 * @return true on success. 82 */ 83 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); 84 85 /** 86 * If decoding to YUV is supported, this returns true. Otherwise, this 87 * returns false and does not modify any of the parameters. 88 * 89 * @param sizeInfo Output parameter indicating the sizes and required 90 * allocation widths of the Y, U, V, and A planes. 91 * @param yuvaIndices How the YUVA planes are organized/used 92 * @param colorSpace Output parameter. 93 */ 94 bool queryYUVA8(SkYUVASizeInfo* sizeInfo, 95 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], 96 SkYUVColorSpace* colorSpace) const; 97 98 /** 99 * Returns true on success and false on failure. 100 * This always attempts to perform a full decode. If the client only 101 * wants size, it should call queryYUVA8(). 102 * 103 * @param sizeInfo Needs to exactly match the values returned by the 104 * query, except the WidthBytes may be larger than the 105 * recommendation (but not smaller). 106 * @param yuvaIndices Needs to exactly match the values returned by the query. 107 * @param planes Memory for the Y, U, V, and A planes. Note that, depending on the 108 * settings in yuvaIndices, anywhere from 1..4 planes could be returned. 109 */ 110 bool getYUVA8Planes(const SkYUVASizeInfo& sizeInfo, 111 const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount], 112 void* planes[]); 113 114 #if SK_SUPPORT_GPU 115 /** 116 * If the generator can natively/efficiently return its pixels as a GPU image (backed by a 117 * texture) this will return that image. If not, this will return NULL. 118 * 119 * This routine also supports retrieving only a subset of the pixels. That subset is specified 120 * by the following rectangle: 121 * 122 * subset = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height()) 123 * 124 * If subset is not contained inside the generator's bounds, this returns false. 125 * 126 * whole = SkIRect::MakeWH(getInfo().width(), getInfo().height()) 127 * if (!whole.contains(subset)) { 128 * return false; 129 * } 130 * 131 * Regarding the GrContext parameter: 132 * 133 * It must be non-NULL. The generator should only succeed if: 134 * - its internal context is the same 135 * - it can somehow convert its texture into one that is valid for the provided context. 136 * 137 * If the willNeedMipMaps flag is true, the generator should try to create a TextureProxy that 138 * at least has the mip levels allocated and the base layer filled in. If this is not possible, 139 * the generator is allowed to return a non mipped proxy, but this will have some additional 140 * overhead in later allocating mips and copying of the base layer. 141 */ 142 GrSurfaceProxyView generateTexture(GrRecordingContext*, const SkImageInfo& info, 143 const SkIPoint& origin, bool willNeedMipMaps); 144 texturesAreCacheable()145 bool texturesAreCacheable() const { return this->onTexturesAreCacheable(); } 146 #endif 147 148 /** 149 * If the default image decoder system can interpret the specified (encoded) data, then 150 * this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way 151 * the caller is still responsible for managing their ownership of the data. 152 */ 153 static std::unique_ptr<SkImageGenerator> MakeFromEncoded(sk_sp<SkData>); 154 155 /** Return a new image generator backed by the specified picture. If the size is empty or 156 * the picture is NULL, this returns NULL. 157 * The optional matrix and paint arguments are passed to drawPicture() at rasterization 158 * time. 159 */ 160 static std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize&, sk_sp<SkPicture>, 161 const SkMatrix*, const SkPaint*, 162 SkImage::BitDepth, 163 sk_sp<SkColorSpace>); 164 165 protected: 166 static constexpr int kNeedNewImageUniqueID = 0; 167 168 SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID); 169 onRefEncodedData()170 virtual sk_sp<SkData> onRefEncodedData() { return nullptr; } 171 struct Options {}; onGetPixels(const SkImageInfo &,void *,size_t,const Options &)172 virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; } onIsValid(GrContext *)173 virtual bool onIsValid(GrContext*) const { return true; } onQueryYUVA8(SkYUVASizeInfo *,SkYUVAIndex[SkYUVAIndex::kIndexCount],SkYUVColorSpace *)174 virtual bool onQueryYUVA8(SkYUVASizeInfo*, SkYUVAIndex[SkYUVAIndex::kIndexCount], 175 SkYUVColorSpace*) const { return false; } onGetYUVA8Planes(const SkYUVASizeInfo &,const SkYUVAIndex[SkYUVAIndex::kIndexCount],void * [4])176 virtual bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount], 177 void*[4] /*planes*/) { return false; } 178 #if SK_SUPPORT_GPU 179 enum class TexGenType { 180 kNone, //image generator does not implement onGenerateTexture 181 kCheap, //onGenerateTexture is implemented and it is fast (does not render offscreen) 182 kExpensive, //onGenerateTexture is implemented and it is relatively slow 183 }; 184 onCanGenerateTexture()185 virtual TexGenType onCanGenerateTexture() const { return TexGenType::kNone; } 186 virtual GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&, 187 const SkIPoint&, 188 bool willNeedMipMaps); // returns nullptr onTexturesAreCacheable()189 virtual bool onTexturesAreCacheable() const { return true; } 190 #endif 191 192 private: 193 const SkImageInfo fInfo; 194 const uint32_t fUniqueID; 195 196 friend class SkImage_Lazy; 197 198 // This is our default impl, which may be different on different platforms. 199 // It is called from NewFromEncoded() after it has checked for any runtime factory. 200 // The SkData will never be NULL, as that will have been checked by NewFromEncoded. 201 static std::unique_ptr<SkImageGenerator> MakeFromEncodedImpl(sk_sp<SkData>); 202 203 SkImageGenerator(SkImageGenerator&&) = delete; 204 SkImageGenerator(const SkImageGenerator&) = delete; 205 SkImageGenerator& operator=(SkImageGenerator&&) = delete; 206 SkImageGenerator& operator=(const SkImageGenerator&) = delete; 207 }; 208 209 #endif // SkImageGenerator_DEFINED 210