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 "SkBitmap.h" 12 #include "SkColor.h" 13 #include "SkImage.h" 14 #include "SkImageInfo.h" 15 #include "SkYUVAIndex.h" 16 #include "SkYUVASizeInfo.h" 17 18 class GrRecordingContext; 19 class GrTextureProxy; 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 */ 33 virtual ~SkImageGenerator() { } 34 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 */ 44 sk_sp<SkData> refEncodedData() { 45 return this->onRefEncodedData(); 46 } 47 48 /** 49 * Return the ImageInfo associated with this generator. 50 */ 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 */ 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 sk_sp<GrTextureProxy> generateTexture(GrRecordingContext*, const SkImageInfo& info, 143 const SkIPoint& origin, 144 bool willNeedMipMaps); 145 #endif 146 147 /** 148 * If the default image decoder system can interpret the specified (encoded) data, then 149 * this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way 150 * the caller is still responsible for managing their ownership of the data. 151 */ 152 static std::unique_ptr<SkImageGenerator> MakeFromEncoded(sk_sp<SkData>); 153 154 /** Return a new image generator backed by the specified picture. If the size is empty or 155 * the picture is NULL, this returns NULL. 156 * The optional matrix and paint arguments are passed to drawPicture() at rasterization 157 * time. 158 */ 159 static std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize&, sk_sp<SkPicture>, 160 const SkMatrix*, const SkPaint*, 161 SkImage::BitDepth, 162 sk_sp<SkColorSpace>); 163 164 protected: 165 static constexpr int kNeedNewImageUniqueID = 0; 166 167 SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID); 168 169 virtual sk_sp<SkData> onRefEncodedData() { return nullptr; } 170 struct Options {}; 171 virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; } 172 virtual bool onIsValid(GrContext*) const { return true; } 173 virtual bool onQueryYUVA8(SkYUVASizeInfo*, SkYUVAIndex[SkYUVAIndex::kIndexCount], 174 SkYUVColorSpace*) const { return false; } 175 virtual bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount], 176 void*[4] /*planes*/) { return false; } 177 #if SK_SUPPORT_GPU 178 enum class TexGenType { 179 kNone, //image generator does not implement onGenerateTexture 180 kCheap, //onGenerateTexture is implemented and it is fast (does not render offscreen) 181 kExpensive, //onGenerateTexture is implemented and it is relatively slow 182 }; 183 184 virtual TexGenType onCanGenerateTexture() const { return TexGenType::kNone; } 185 virtual sk_sp<GrTextureProxy> onGenerateTexture(GrRecordingContext*, const SkImageInfo&, 186 const SkIPoint&, 187 bool willNeedMipMaps); // returns nullptr 188 #endif 189 190 private: 191 const SkImageInfo fInfo; 192 const uint32_t fUniqueID; 193 194 friend class SkImage_Lazy; 195 196 // This is our default impl, which may be different on different platforms. 197 // It is called from NewFromEncoded() after it has checked for any runtime factory. 198 // The SkData will never be NULL, as that will have been checked by NewFromEncoded. 199 static std::unique_ptr<SkImageGenerator> MakeFromEncodedImpl(sk_sp<SkData>); 200 201 SkImageGenerator(SkImageGenerator&&) = delete; 202 SkImageGenerator(const SkImageGenerator&) = delete; 203 SkImageGenerator& operator=(SkImageGenerator&&) = delete; 204 SkImageGenerator& operator=(const SkImageGenerator&) = delete; 205 }; 206 207 #endif // SkImageGenerator_DEFINED 208