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/SkData.h" 12 #include "include/core/SkImageInfo.h" 13 #include "include/core/SkPixmap.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkYUVAPixmaps.h" 16 #include "include/private/base/SkAPI.h" 17 18 #include <cstddef> 19 #include <cstdint> 20 21 class GrRecordingContext; 22 23 class SK_API SkImageGenerator { 24 public: 25 /** 26 * The PixelRef which takes ownership of this SkImageGenerator 27 * will call the image generator's destructor. 28 */ ~SkImageGenerator()29 virtual ~SkImageGenerator() { } 30 uniqueID()31 uint32_t uniqueID() const { return fUniqueID; } 32 33 /** 34 * Return a ref to the encoded (i.e. compressed) representation 35 * of this data. 36 * 37 * If non-NULL is returned, the caller is responsible for calling 38 * unref() on the data when it is finished. 39 */ refEncodedData()40 sk_sp<SkData> refEncodedData() { 41 return this->onRefEncodedData(); 42 } 43 44 /** 45 * Return the ImageInfo associated with this generator. 46 */ getInfo()47 const SkImageInfo& getInfo() const { return fInfo; } 48 49 /** 50 * Can this generator be used to produce images that will be drawable to the specified context 51 * (or to CPU, if context is nullptr)? 52 */ isValid(GrRecordingContext * context)53 bool isValid(GrRecordingContext* context) const { 54 return this->onIsValid(context); 55 } 56 57 /** 58 * Will this generator produce protected content 59 */ isProtected()60 bool isProtected() const { 61 return this->onIsProtected(); 62 } 63 64 /** 65 * Decode into the given pixels, a block of memory of size at 66 * least (info.fHeight - 1) * rowBytes + (info.fWidth * 67 * bytesPerPixel) 68 * 69 * Repeated calls to this function should give the same results, 70 * allowing the PixelRef to be immutable. 71 * 72 * @param info A description of the format 73 * expected by the caller. This can simply be identical 74 * to the info returned by getInfo(). 75 * 76 * This contract also allows the caller to specify 77 * different output-configs, which the implementation can 78 * decide to support or not. 79 * 80 * A size that does not match getInfo() implies a request 81 * to scale. If the generator cannot perform this scale, 82 * it will return false. 83 * 84 * @return true on success. 85 */ 86 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); 87 getPixels(const SkPixmap & pm)88 bool getPixels(const SkPixmap& pm) { 89 return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes()); 90 } 91 92 /** 93 * If decoding to YUV is supported, this returns true. Otherwise, this 94 * returns false and the caller will ignore output parameter yuvaPixmapInfo. 95 * 96 * @param supportedDataTypes Indicates the data type/planar config combinations that are 97 * supported by the caller. If the generator supports decoding to 98 * YUV(A), but not as a type in supportedDataTypes, this method 99 * returns false. 100 * @param yuvaPixmapInfo Output parameter that specifies the planar configuration, subsampling, 101 * orientation, chroma siting, plane color types, and row bytes. 102 */ 103 bool queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, 104 SkYUVAPixmapInfo* yuvaPixmapInfo) const; 105 106 /** 107 * Returns true on success and false on failure. 108 * This always attempts to perform a full decode. To get the planar 109 * configuration without decoding use queryYUVAInfo(). 110 * 111 * @param yuvaPixmaps Contains preallocated pixmaps configured according to a successful call 112 * to queryYUVAInfo(). 113 */ 114 bool getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps); 115 isTextureGenerator()116 virtual bool isTextureGenerator() const { return false; } 117 118 protected: 119 static constexpr int kNeedNewImageUniqueID = 0; 120 121 SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID); 122 onRefEncodedData()123 virtual sk_sp<SkData> onRefEncodedData() { return nullptr; } 124 struct Options {}; onGetPixels(const SkImageInfo &,void *,size_t,const Options &)125 virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; } onIsValid(GrRecordingContext *)126 virtual bool onIsValid(GrRecordingContext*) const { return true; } onIsProtected()127 virtual bool onIsProtected() const { return false; } onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes &,SkYUVAPixmapInfo *)128 virtual bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&, 129 SkYUVAPixmapInfo*) const { return false; } onGetYUVAPlanes(const SkYUVAPixmaps &)130 virtual bool onGetYUVAPlanes(const SkYUVAPixmaps&) { return false; } 131 132 const SkImageInfo fInfo; 133 134 private: 135 const uint32_t fUniqueID; 136 137 SkImageGenerator(SkImageGenerator&&) = delete; 138 SkImageGenerator(const SkImageGenerator&) = delete; 139 SkImageGenerator& operator=(SkImageGenerator&&) = delete; 140 SkImageGenerator& operator=(const SkImageGenerator&) = delete; 141 }; 142 143 #endif // SkImageGenerator_DEFINED 144