SkGr.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2017 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 SkGr_DEFINED
  8. #define SkGr_DEFINED
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFilterQuality.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkMatrix.h"
  14. #include "include/core/SkVertices.h"
  15. #include "include/gpu/GrBlend.h"
  16. #include "include/gpu/GrSamplerState.h"
  17. #include "include/gpu/GrTypes.h"
  18. #include "include/private/SkColorData.h"
  19. #include "src/core/SkBlendModePriv.h"
  20. #include "src/gpu/GrCaps.h"
  21. #include "src/gpu/GrColor.h"
  22. class GrCaps;
  23. class GrColorSpaceInfo;
  24. class GrColorSpaceXform;
  25. class GrContext;
  26. class GrFragmentProcessor;
  27. class GrPaint;
  28. class GrRecordingContext;
  29. class GrResourceProvider;
  30. class GrTextureProxy;
  31. class GrUniqueKey;
  32. class SkBitmap;
  33. class SkData;
  34. class SkPaint;
  35. class SkPixelRef;
  36. class SkPixmap;
  37. struct SkIRect;
  38. ////////////////////////////////////////////////////////////////////////////////
  39. // Color type conversions
  40. static inline GrColor SkColorToPremulGrColor(SkColor c) {
  41. SkPMColor pm = SkPreMultiplyColor(c);
  42. unsigned r = SkGetPackedR32(pm);
  43. unsigned g = SkGetPackedG32(pm);
  44. unsigned b = SkGetPackedB32(pm);
  45. unsigned a = SkGetPackedA32(pm);
  46. return GrColorPackRGBA(r, g, b, a);
  47. }
  48. static inline GrColor SkColorToUnpremulGrColor(SkColor c) {
  49. unsigned r = SkColorGetR(c);
  50. unsigned g = SkColorGetG(c);
  51. unsigned b = SkColorGetB(c);
  52. unsigned a = SkColorGetA(c);
  53. return GrColorPackRGBA(r, g, b, a);
  54. }
  55. /** Similar, but using SkPMColor4f. */
  56. SkPMColor4f SkColorToPMColor4f(SkColor, const GrColorSpaceInfo&);
  57. /** Converts an SkColor4f to the destination color space. */
  58. SkColor4f SkColor4fPrepForDst(SkColor4f, const GrColorSpaceInfo&);
  59. /** Returns true if half-floats are required to store the color in a vertex (and half-floats
  60. are supported). */
  61. static inline bool SkPMColor4fNeedsWideColor(SkPMColor4f color, GrClampType clampType,
  62. const GrCaps& caps) {
  63. return GrClampType::kNone == clampType &&
  64. caps.halfFloatVertexAttributeSupport() &&
  65. !color.fitsInBytes();
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // Paint conversion
  69. /** Converts an SkPaint to a GrPaint for a given GrRecordingContext. The matrix is required in order
  70. to convert the SkShader (if any) on the SkPaint. The primitive itself has no color. */
  71. bool SkPaintToGrPaint(GrRecordingContext*,
  72. const GrColorSpaceInfo& dstColorSpaceInfo,
  73. const SkPaint& skPaint,
  74. const SkMatrix& viewM,
  75. GrPaint* grPaint);
  76. /** Same as above but ignores the SkShader (if any) on skPaint. */
  77. bool SkPaintToGrPaintNoShader(GrRecordingContext*,
  78. const GrColorSpaceInfo& dstColorSpaceInfo,
  79. const SkPaint& skPaint,
  80. GrPaint* grPaint);
  81. /** Replaces the SkShader (if any) on skPaint with the passed in GrFragmentProcessor. The processor
  82. should expect an unpremul input color and produce a premultiplied output color. There is
  83. no primitive color. */
  84. bool SkPaintToGrPaintReplaceShader(GrRecordingContext*,
  85. const GrColorSpaceInfo& dstColorSpaceInfo,
  86. const SkPaint& skPaint,
  87. std::unique_ptr<GrFragmentProcessor> shaderFP,
  88. GrPaint* grPaint);
  89. /** Blends the SkPaint's shader (or color if no shader) with the color which specified via a
  90. GrOp's GrPrimitiveProcesssor. */
  91. bool SkPaintToGrPaintWithXfermode(GrRecordingContext*,
  92. const GrColorSpaceInfo& dstColorSpaceInfo,
  93. const SkPaint& skPaint,
  94. const SkMatrix& viewM,
  95. SkBlendMode primColorMode,
  96. GrPaint* grPaint);
  97. /** This is used when there is a primitive color, but the shader should be ignored. Currently,
  98. the expectation is that the primitive color will be premultiplied, though it really should be
  99. unpremultiplied so that interpolation is done in unpremul space. The paint's alpha will be
  100. applied to the primitive color after interpolation. */
  101. inline bool SkPaintToGrPaintWithPrimitiveColor(GrRecordingContext* context,
  102. const GrColorSpaceInfo& dstColorSpaceInfo,
  103. const SkPaint& skPaint,
  104. GrPaint* grPaint) {
  105. return SkPaintToGrPaintWithXfermode(context, dstColorSpaceInfo, skPaint, SkMatrix::I(),
  106. SkBlendMode::kDst, grPaint);
  107. }
  108. /** This is used when there may or may not be a shader, and the caller wants to plugin a texture
  109. lookup. If there is a shader, then its output will only be used if the texture is alpha8. */
  110. bool SkPaintToGrPaintWithTexture(GrRecordingContext*,
  111. const GrColorSpaceInfo& dstColorSpaceInfo,
  112. const SkPaint& skPaint,
  113. const SkMatrix& viewM,
  114. std::unique_ptr<GrFragmentProcessor> fp,
  115. bool textureIsAlphaOnly,
  116. GrPaint* grPaint);
  117. ////////////////////////////////////////////////////////////////////////////////
  118. // Misc Sk to Gr type conversions
  119. GrSurfaceDesc GrImageInfoToSurfaceDesc(const SkImageInfo&);
  120. GrPixelConfig SkColorType2GrPixelConfig(const SkColorType);
  121. GrPixelConfig SkImageInfo2GrPixelConfig(const SkImageInfo& info);
  122. bool GrPixelConfigToColorType(GrPixelConfig, SkColorType*);
  123. GrSamplerState::Filter GrSkFilterQualityToGrFilterMode(SkFilterQuality paintFilterQuality,
  124. const SkMatrix& viewM,
  125. const SkMatrix& localM,
  126. bool sharpenMipmappedTextures,
  127. bool* doBicubic);
  128. //////////////////////////////////////////////////////////////////////////////
  129. static inline GrPrimitiveType SkVertexModeToGrPrimitiveType(SkVertices::VertexMode mode) {
  130. switch (mode) {
  131. case SkVertices::kTriangles_VertexMode:
  132. return GrPrimitiveType::kTriangles;
  133. case SkVertices::kTriangleStrip_VertexMode:
  134. return GrPrimitiveType::kTriangleStrip;
  135. case SkVertices::kTriangleFan_VertexMode:
  136. break;
  137. }
  138. SK_ABORT("Invalid mode");
  139. return GrPrimitiveType::kPoints;
  140. }
  141. //////////////////////////////////////////////////////////////////////////////
  142. GR_STATIC_ASSERT((int)kZero_GrBlendCoeff == (int)SkBlendModeCoeff::kZero);
  143. GR_STATIC_ASSERT((int)kOne_GrBlendCoeff == (int)SkBlendModeCoeff::kOne);
  144. GR_STATIC_ASSERT((int)kSC_GrBlendCoeff == (int)SkBlendModeCoeff::kSC);
  145. GR_STATIC_ASSERT((int)kISC_GrBlendCoeff == (int)SkBlendModeCoeff::kISC);
  146. GR_STATIC_ASSERT((int)kDC_GrBlendCoeff == (int)SkBlendModeCoeff::kDC);
  147. GR_STATIC_ASSERT((int)kIDC_GrBlendCoeff == (int)SkBlendModeCoeff::kIDC);
  148. GR_STATIC_ASSERT((int)kSA_GrBlendCoeff == (int)SkBlendModeCoeff::kSA);
  149. GR_STATIC_ASSERT((int)kISA_GrBlendCoeff == (int)SkBlendModeCoeff::kISA);
  150. GR_STATIC_ASSERT((int)kDA_GrBlendCoeff == (int)SkBlendModeCoeff::kDA);
  151. GR_STATIC_ASSERT((int)kIDA_GrBlendCoeff == (int)SkBlendModeCoeff::kIDA);
  152. //GR_STATIC_ASSERT(SkXfermode::kCoeffCount == 10);
  153. ////////////////////////////////////////////////////////////////////////////////
  154. // Texture management
  155. /** Returns a texture representing the bitmap that is compatible with the GrSamplerState. The
  156. * texture is inserted into the cache (unless the bitmap is marked volatile) and can be
  157. * retrieved again via this function.
  158. * The 'scaleAdjust' in/out parameter will be updated to hold any rescaling that needs to be
  159. * performed on the absolute texture coordinates (e.g., if the texture is resized out to
  160. * the next power of two). It can be null if the caller is sure the bitmap won't be resized.
  161. */
  162. sk_sp<GrTextureProxy> GrRefCachedBitmapTextureProxy(GrRecordingContext*,
  163. const SkBitmap&,
  164. const GrSamplerState&,
  165. SkScalar scaleAdjust[2]);
  166. /**
  167. * Creates a new texture with mipmap levels and copies the baseProxy into the base layer.
  168. */
  169. sk_sp<GrTextureProxy> GrCopyBaseMipMapToTextureProxy(GrRecordingContext*,
  170. GrTextureProxy* baseProxy);
  171. /*
  172. * Create a texture proxy from the provided bitmap by wrapping it in an image and calling
  173. * GrMakeCachedImageProxy.
  174. */
  175. sk_sp<GrTextureProxy> GrMakeCachedBitmapProxy(GrProxyProvider*, const SkBitmap& bitmap,
  176. SkBackingFit fit = SkBackingFit::kExact);
  177. /*
  178. * Create a texture proxy from the provided 'srcImage' and add it to the texture cache
  179. * using the key also extracted from 'srcImage'.
  180. */
  181. sk_sp<GrTextureProxy> GrMakeCachedImageProxy(GrProxyProvider*, sk_sp<SkImage> srcImage,
  182. SkBackingFit fit = SkBackingFit::kExact);
  183. /**
  184. * Our key includes the offset, width, and height so that bitmaps created by extractSubset()
  185. * are unique.
  186. *
  187. * The imageID is in the shared namespace (see SkNextID::ImageID())
  188. * - SkBitmap/SkPixelRef
  189. * - SkImage
  190. * - SkImageGenerator
  191. *
  192. * Note: width/height must fit in 16bits for this impl.
  193. */
  194. void GrMakeKeyFromImageID(GrUniqueKey* key, uint32_t imageID, const SkIRect& imageBounds);
  195. /** Call this after installing a GrUniqueKey on texture. It will cause the texture's key to be
  196. removed should the bitmap's contents change or be destroyed. */
  197. void GrInstallBitmapUniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextID,
  198. SkPixelRef* pixelRef);
  199. #endif