SkImageGenerator.h 8.3 KB

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