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