SkMaskFilterBase.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2018 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 SkMaskFilterBase_DEFINED
  8. #define SkMaskFilterBase_DEFINED
  9. #include "include/core/SkBlurTypes.h"
  10. #include "include/core/SkFlattenable.h"
  11. #include "include/core/SkMaskFilter.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkStrokeRec.h"
  14. #include "include/private/SkNoncopyable.h"
  15. #include "src/core/SkMask.h"
  16. class GrClip;
  17. struct GrFPArgs;
  18. class GrFragmentProcessor;
  19. class GrPaint;
  20. class GrRecordingContext;
  21. class GrRenderTarget;
  22. class GrRenderTargetContext;
  23. class GrResourceProvider;
  24. class GrShape;
  25. class GrTexture;
  26. class GrTextureProxy;
  27. class SkBitmap;
  28. class SkBlitter;
  29. class SkCachedData;
  30. class SkMatrix;
  31. class SkPath;
  32. class SkRasterClip;
  33. class SkRRect;
  34. class SkMaskFilterBase : public SkMaskFilter {
  35. public:
  36. /** Returns the format of the resulting mask that this subclass will return
  37. when its filterMask() method is called.
  38. */
  39. virtual SkMask::Format getFormat() const = 0;
  40. /** Create a new mask by filter the src mask.
  41. If src.fImage == null, then do not allocate or create the dst image
  42. but do fill out the other fields in dstMask.
  43. If you do allocate a dst image, use SkMask::AllocImage()
  44. If this returns false, dst mask is ignored.
  45. @param dst the result of the filter. If src.fImage == null, dst should not allocate its image
  46. @param src the original image to be filtered.
  47. @param matrix the CTM
  48. @param margin if not null, return the buffer dx/dy need when calculating the effect. Used when
  49. drawing a clipped object to know how much larger to allocate the src before
  50. applying the filter. If returning false, ignore this parameter.
  51. @return true if the dst mask was correctly created.
  52. */
  53. virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
  54. SkIPoint* margin) const = 0;
  55. #if SK_SUPPORT_GPU
  56. /**
  57. * Returns a processor if the filter can be expressed a single-pass GrProcessor without
  58. * requiring an explicit input mask. Per-pixel, the effect receives the incoming mask's
  59. * coverage as the input color and outputs the filtered covereage value. This means that each
  60. * pixel's filtered coverage must only depend on the unfiltered mask value for that pixel and
  61. * not on surrounding values.
  62. */
  63. std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs& args) const;
  64. /**
  65. * Returns true iff asFragmentProcessor() will return a processor
  66. */
  67. bool hasFragmentProcessor() const;
  68. /**
  69. * If asFragmentProcessor() fails the filter may be implemented on the GPU by a subclass
  70. * overriding filterMaskGPU (declared below). That code path requires constructing a
  71. * src mask as input. Since that is a potentially expensive operation, the subclass must also
  72. * override this function to indicate whether filterTextureMaskGPU would succeeed if the mask
  73. * were to be created.
  74. *
  75. * 'maskRect' returns the device space portion of the mask that the filter needs. The mask
  76. * passed into 'filterMaskGPU' should have the same extent as 'maskRect' but be
  77. * translated to the upper-left corner of the mask (i.e., (maskRect.fLeft, maskRect.fTop)
  78. * appears at (0, 0) in the mask).
  79. *
  80. * Logically, how this works is:
  81. * canFilterMaskGPU is called
  82. * if (it returns true)
  83. * the returned mask rect is used for quick rejecting
  84. * the mask rect is used to generate the mask
  85. * filterMaskGPU is called to filter the mask
  86. *
  87. * TODO: this should work as:
  88. * if (canFilterMaskGPU(devShape, ...)) // rect, rrect, drrect, path
  89. * filterMaskGPU(devShape, ...)
  90. * this would hide the RRect special case and the mask generation
  91. */
  92. virtual bool canFilterMaskGPU(const GrShape&,
  93. const SkIRect& devSpaceShapeBounds,
  94. const SkIRect& clipBounds,
  95. const SkMatrix& ctm,
  96. SkIRect* maskRect) const;
  97. /**
  98. * Try to directly render the mask filter into the target. Returns true if drawing was
  99. * successful. If false is returned then paint is unmodified.
  100. */
  101. virtual bool directFilterMaskGPU(GrRecordingContext*,
  102. GrRenderTargetContext*,
  103. GrPaint&& paint,
  104. const GrClip&,
  105. const SkMatrix& viewMatrix,
  106. const GrShape& shape) const;
  107. /**
  108. * This function is used to implement filters that require an explicit src mask. It should only
  109. * be called if canFilterMaskGPU returned true and the maskRect param should be the output from
  110. * that call.
  111. * Implementations are free to get the GrContext from the src texture in order to create
  112. * additional textures and perform multiple passes.
  113. */
  114. virtual sk_sp<GrTextureProxy> filterMaskGPU(GrRecordingContext*,
  115. sk_sp<GrTextureProxy> srcProxy,
  116. const SkMatrix& ctm,
  117. const SkIRect& maskRect) const;
  118. #endif
  119. /**
  120. * The fast bounds function is used to enable the paint to be culled early
  121. * in the drawing pipeline. This function accepts the current bounds of the
  122. * paint as its src param and the filter adjust those bounds using its
  123. * current mask and returns the result using the dest param. Callers are
  124. * allowed to provide the same struct for both src and dest so each
  125. * implementation must accomodate that behavior.
  126. *
  127. * The default impl calls filterMask with the src mask having no image,
  128. * but subclasses may override this if they can compute the rect faster.
  129. */
  130. virtual void computeFastBounds(const SkRect& src, SkRect* dest) const;
  131. struct BlurRec {
  132. SkScalar fSigma;
  133. SkBlurStyle fStyle;
  134. };
  135. /**
  136. * If this filter can be represented by a BlurRec, return true and (if not null) fill in the
  137. * provided BlurRec parameter. If this effect cannot be represented as a BlurRec, return false
  138. * and ignore the BlurRec parameter.
  139. */
  140. virtual bool asABlur(BlurRec*) const;
  141. protected:
  142. SkMaskFilterBase() {}
  143. #if SK_SUPPORT_GPU
  144. virtual std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs&) const;
  145. virtual bool onHasFragmentProcessor() const;
  146. #endif
  147. enum FilterReturn {
  148. kFalse_FilterReturn,
  149. kTrue_FilterReturn,
  150. kUnimplemented_FilterReturn
  151. };
  152. class NinePatch : ::SkNoncopyable {
  153. public:
  154. NinePatch() : fCache(nullptr) { }
  155. ~NinePatch();
  156. SkMask fMask; // fBounds must have [0,0] in its top-left
  157. SkIRect fOuterRect; // width/height must be >= fMask.fBounds'
  158. SkIPoint fCenter; // identifies center row/col for stretching
  159. SkCachedData* fCache;
  160. };
  161. /**
  162. * Override if your subclass can filter a rect, and return the answer as
  163. * a ninepatch mask to be stretched over the returned outerRect. On success
  164. * return kTrue_FilterReturn. On failure (e.g. out of memory) return
  165. * kFalse_FilterReturn. If the normal filterMask() entry-point should be
  166. * called (the default) return kUnimplemented_FilterReturn.
  167. *
  168. * By convention, the caller will take the center rol/col from the returned
  169. * mask as the slice it can replicate horizontally and vertically as we
  170. * stretch the mask to fit inside outerRect. It is an error for outerRect
  171. * to be smaller than the mask's bounds. This would imply that the width
  172. * and height of the mask should be odd. This is not required, just that
  173. * the caller will call mask.fBounds.centerX() and centerY() to find the
  174. * strips that will be replicated.
  175. */
  176. virtual FilterReturn filterRectsToNine(const SkRect[], int count,
  177. const SkMatrix&,
  178. const SkIRect& clipBounds,
  179. NinePatch*) const;
  180. /**
  181. * Similar to filterRectsToNine, except it performs the work on a round rect.
  182. */
  183. virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
  184. const SkIRect& clipBounds,
  185. NinePatch*) const;
  186. private:
  187. friend class SkDraw;
  188. /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
  189. and then call filterMask(). If this returns true, the specified blitter will be called
  190. to render that mask. Returns false if filterMask() returned false.
  191. This method is not exported to java.
  192. */
  193. bool filterPath(const SkPath& devPath, const SkMatrix& ctm, const SkRasterClip&, SkBlitter*,
  194. SkStrokeRec::InitStyle) const;
  195. /** Helper method that, given a roundRect in device space, will rasterize it into a kA8_Format
  196. mask and then call filterMask(). If this returns true, the specified blitter will be called
  197. to render that mask. Returns false if filterMask() returned false.
  198. */
  199. bool filterRRect(const SkRRect& devRRect, const SkMatrix& ctm, const SkRasterClip&,
  200. SkBlitter*) const;
  201. typedef SkFlattenable INHERITED;
  202. };
  203. inline SkMaskFilterBase* as_MFB(SkMaskFilter* mf) {
  204. return static_cast<SkMaskFilterBase*>(mf);
  205. }
  206. inline const SkMaskFilterBase* as_MFB(const SkMaskFilter* mf) {
  207. return static_cast<const SkMaskFilterBase*>(mf);
  208. }
  209. inline const SkMaskFilterBase* as_MFB(const sk_sp<SkMaskFilter>& mf) {
  210. return static_cast<SkMaskFilterBase*>(mf.get());
  211. }
  212. #endif