SkSpecialImage.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2016 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 SkSpecialImage_DEFINED
  8. #define SkSpecialImage_DEFINED
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/core/SkSurfaceProps.h"
  11. #include "src/core/SkNextID.h"
  12. #include "include/core/SkImageFilter.h"
  13. #include "include/core/SkImageInfo.h"
  14. class GrRecordingContext;
  15. class GrTextureProxy;
  16. class SkBitmap;
  17. class SkCanvas;
  18. class SkImage;
  19. struct SkImageInfo;
  20. class SkPaint;
  21. class SkPixmap;
  22. class SkSpecialSurface;
  23. class SkSurface;
  24. enum {
  25. kNeedNewImageUniqueID_SpecialImage = 0
  26. };
  27. /**
  28. * This is a restricted form of SkImage solely intended for internal use. It
  29. * differs from SkImage in that:
  30. * - it can only be backed by raster or gpu (no generators)
  31. * - it can be backed by a GrTextureProxy larger than its nominal bounds
  32. * - it can't be drawn tiled
  33. * - it can't be drawn with MIPMAPs
  34. * It is similar to SkImage in that it abstracts how the pixels are stored/represented.
  35. *
  36. * Note: the contents of the backing storage outside of the subset rect are undefined.
  37. */
  38. class SkSpecialImage : public SkRefCnt {
  39. public:
  40. typedef void* ReleaseContext;
  41. typedef void(*RasterReleaseProc)(void* pixels, ReleaseContext);
  42. const SkSurfaceProps& props() const { return fProps; }
  43. int width() const { return fSubset.width(); }
  44. int height() const { return fSubset.height(); }
  45. const SkIRect& subset() const { return fSubset; }
  46. SkColorSpace* getColorSpace() const;
  47. uint32_t uniqueID() const { return fUniqueID; }
  48. virtual SkAlphaType alphaType() const = 0;
  49. virtual size_t getSize() const = 0;
  50. /**
  51. * Ensures that a special image is backed by a texture (when GrRecordingContext is non-null).
  52. * If no transformation is required, the returned image may be the same as this special image.
  53. * If this special image is from a different GrRecordingContext, this will fail.
  54. */
  55. sk_sp<SkSpecialImage> makeTextureImage(GrRecordingContext*);
  56. /**
  57. * Draw this SpecialImage into the canvas, automatically taking into account the image's subset
  58. */
  59. void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const;
  60. static sk_sp<SkSpecialImage> MakeFromImage(GrRecordingContext*,
  61. const SkIRect& subset,
  62. sk_sp<SkImage>,
  63. const SkSurfaceProps* = nullptr);
  64. static sk_sp<SkSpecialImage> MakeFromRaster(const SkIRect& subset,
  65. const SkBitmap&,
  66. const SkSurfaceProps* = nullptr);
  67. static sk_sp<SkSpecialImage> CopyFromRaster(const SkIRect& subset,
  68. const SkBitmap&,
  69. const SkSurfaceProps* = nullptr);
  70. #if SK_SUPPORT_GPU
  71. static sk_sp<SkSpecialImage> MakeDeferredFromGpu(GrRecordingContext*,
  72. const SkIRect& subset,
  73. uint32_t uniqueID,
  74. sk_sp<GrTextureProxy>,
  75. sk_sp<SkColorSpace>,
  76. const SkSurfaceProps* = nullptr,
  77. SkAlphaType at = kPremul_SkAlphaType);
  78. #endif
  79. /**
  80. * Create a new special surface with a backend that is compatible with this special image.
  81. */
  82. sk_sp<SkSpecialSurface> makeSurface(const SkImageFilter::OutputProperties& outProps,
  83. const SkISize& size,
  84. SkAlphaType at = kPremul_SkAlphaType,
  85. const SkSurfaceProps* props = nullptr) const;
  86. /**
  87. * Create a new surface with a backend that is compatible with this special image.
  88. * TODO: switch this to makeSurface once we resolved the naming issue
  89. */
  90. sk_sp<SkSurface> makeTightSurface(const SkImageFilter::OutputProperties& outProps,
  91. const SkISize& size,
  92. SkAlphaType at = kPremul_SkAlphaType) const;
  93. /**
  94. * Extract a subset of this special image and return it as a special image.
  95. * It may or may not point to the same backing memory. The input 'subset' is relative to the
  96. * special image's content rect.
  97. */
  98. sk_sp<SkSpecialImage> makeSubset(const SkIRect& subset) const;
  99. /**
  100. * Create an SkImage from the contents of this special image optionally extracting a subset.
  101. * It may or may not point to the same backing memory.
  102. * Note: when no 'subset' parameter is specified the the entire SkSpecialImage will be
  103. * returned - including whatever extra padding may have resulted from a loose fit!
  104. * When the 'subset' parameter is specified the returned image will be tight even if that
  105. * entails a copy! The 'subset' is relative to this special image's content rect.
  106. */
  107. sk_sp<SkImage> asImage(const SkIRect* subset = nullptr) const;
  108. /**
  109. * If the SpecialImage is backed by a gpu texture, return true.
  110. */
  111. bool isTextureBacked() const;
  112. /**
  113. * Return the GrRecordingContext if the SkSpecialImage is GrTexture-backed
  114. */
  115. GrRecordingContext* getContext() const;
  116. #if SK_SUPPORT_GPU
  117. /**
  118. * Regardless of how the underlying backing data is stored, returns the contents as a
  119. * GrTextureProxy. The returned proxy represents the entire backing image, so texture
  120. * coordinates must be mapped from the content rect (e.g. relative to 'subset()') to the proxy's
  121. * space (offset by subset().topLeft()).
  122. */
  123. sk_sp<GrTextureProxy> asTextureProxyRef(GrRecordingContext*) const;
  124. #endif
  125. /**
  126. * Regardless of the underlying backing store, return the contents as an SkBitmap.
  127. * The returned bitmap represents the subset accessed by this image, thus (0,0) refers to the
  128. * top-left corner of 'subset'.
  129. */
  130. bool getROPixels(SkBitmap*) const;
  131. protected:
  132. SkSpecialImage(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps*);
  133. private:
  134. const SkSurfaceProps fProps;
  135. const SkIRect fSubset;
  136. const uint32_t fUniqueID;
  137. typedef SkRefCnt INHERITED;
  138. };
  139. #endif