1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkMaskFilter_DEFINED 11 #define SkMaskFilter_DEFINED 12 13 #include "SkFlattenable.h" 14 #include "SkMask.h" 15 #include "SkPaint.h" 16 17 class SkBitmap; 18 class SkBlitter; 19 class SkBounder; 20 class SkMatrix; 21 class SkPath; 22 class SkRasterClip; 23 24 /** \class SkMaskFilter 25 26 SkMaskFilter is the base class for object that perform transformations on 27 an alpha-channel mask before drawing it. A subclass of SkMaskFilter may be 28 installed into a SkPaint. Once there, each time a primitive is drawn, it 29 is first scan converted into a SkMask::kA8_Format mask, and handed to the 30 filter, calling its filterMask() method. If this returns true, then the 31 new mask is used to render into the device. 32 33 Blur and emboss are implemented as subclasses of SkMaskFilter. 34 */ 35 class SK_API SkMaskFilter : public SkFlattenable { 36 public: 37 SK_DECLARE_INST_COUNT(SkMaskFilter) 38 39 SkMaskFilter() {} 40 41 /** Returns the format of the resulting mask that this subclass will return 42 when its filterMask() method is called. 43 */ 44 virtual SkMask::Format getFormat() const = 0; 45 46 /** Create a new mask by filter the src mask. 47 If src.fImage == null, then do not allocate or create the dst image 48 but do fill out the other fields in dstMask. 49 If you do allocate a dst image, use SkMask::AllocImage() 50 If this returns false, dst mask is ignored. 51 @param dst the result of the filter. If src.fImage == null, dst should not allocate its image 52 @param src the original image to be filtered. 53 @param matrix the CTM 54 @param margin if not null, return the buffer dx/dy need when calculating the effect. Used when 55 drawing a clipped object to know how much larger to allocate the src before 56 applying the filter. If returning false, ignore this parameter. 57 @return true if the dst mask was correctly created. 58 */ 59 virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, 60 SkIPoint* margin) const; 61 62 #if SK_SUPPORT_GPU 63 /** 64 * Returns true if the filter can be expressed a single-pass 65 * GrEffect, used to process this filter on the GPU, or false if 66 * not. 67 * 68 * If effect is non-NULL, a new GrEffect instance is stored 69 * in it. The caller assumes ownership of the stage, and it is up to the 70 * caller to unref it. 71 */ 72 virtual bool asNewEffect(GrEffectRef** effect, GrTexture*) const; 73 74 /** 75 * Returns true if the filter can be processed on the GPU. This is most 76 * often used for multi-pass effects, where intermediate results must be 77 * rendered to textures. For single-pass effects, use asNewEffect(). 78 * 79 * 'maskRect' returns the device space portion of the mask the the filter 80 * needs. The mask passed into 'filterMaskGPU' should have the same extent 81 * as 'maskRect' but be translated to the upper-left corner of the mask 82 * (i.e., (maskRect.fLeft, maskRect.fTop) appears at (0, 0) in the mask). 83 */ 84 virtual bool canFilterMaskGPU(const SkRect& devBounds, 85 const SkIRect& clipBounds, 86 const SkMatrix& ctm, 87 SkRect* maskRect) const; 88 89 /** 90 * Perform this mask filter on the GPU. This is most often used for 91 * multi-pass effects, where intermediate results must be rendered to 92 * textures. For single-pass effects, use asNewEffect(). 'src' is the 93 * source image for processing, as a texture-backed bitmap. 'result' is 94 * the destination bitmap, which should contain a texture-backed pixelref 95 * on success. 'maskRect' should be the rect returned from canFilterMaskGPU. 96 */ 97 bool filterMaskGPU(GrContext* context, 98 const SkBitmap& src, 99 const SkRect& maskRect, 100 SkBitmap* result) const; 101 102 /** 103 * This flavor of 'filterMaskGPU' provides a more direct means of accessing 104 * the filtering capabilities. Setting 'canOverwriteSrc' can allow some 105 * filters to skip the allocation of an additional texture. 106 */ 107 virtual bool filterMaskGPU(GrTexture* src, 108 const SkRect& maskRect, 109 GrTexture** result, 110 bool canOverwriteSrc) const; 111 #endif 112 113 /** 114 * The fast bounds function is used to enable the paint to be culled early 115 * in the drawing pipeline. This function accepts the current bounds of the 116 * paint as its src param and the filter adjust those bounds using its 117 * current mask and returns the result using the dest param. Callers are 118 * allowed to provide the same struct for both src and dest so each 119 * implementation must accomodate that behavior. 120 * 121 * The default impl calls filterMask with the src mask having no image, 122 * but subclasses may override this if they can compute the rect faster. 123 */ 124 virtual void computeFastBounds(const SkRect& src, SkRect* dest) const; 125 126 SkDEVCODE(virtual void toString(SkString* str) const = 0;) 127 128 protected: 129 // empty for now, but lets get our subclass to remember to init us for the future 130 SkMaskFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} 131 132 enum FilterReturn { 133 kFalse_FilterReturn, 134 kTrue_FilterReturn, 135 kUnimplemented_FilterReturn 136 }; 137 138 struct NinePatch { 139 SkMask fMask; // fBounds must have [0,0] in its top-left 140 SkIRect fOuterRect; // width/height must be >= fMask.fBounds' 141 SkIPoint fCenter; // identifies center row/col for stretching 142 }; 143 144 /** 145 * Override if your subclass can filter a rect, and return the answer as 146 * a ninepatch mask to be stretched over the returned outerRect. On success 147 * return kTrue_FilterReturn. On failure (e.g. out of memory) return 148 * kFalse_FilterReturn. If the normal filterMask() entry-point should be 149 * called (the default) return kUnimplemented_FilterReturn. 150 * 151 * By convention, the caller will take the center rol/col from the returned 152 * mask as the slice it can replicate horizontally and vertically as we 153 * stretch the mask to fit inside outerRect. It is an error for outerRect 154 * to be smaller than the mask's bounds. This would imply that the width 155 * and height of the mask should be odd. This is not required, just that 156 * the caller will call mask.fBounds.centerX() and centerY() to find the 157 * strips that will be replicated. 158 */ 159 virtual FilterReturn filterRectsToNine(const SkRect[], int count, 160 const SkMatrix&, 161 const SkIRect& clipBounds, 162 NinePatch*) const; 163 164 private: 165 friend class SkDraw; 166 167 /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask 168 and then call filterMask(). If this returns true, the specified blitter will be called 169 to render that mask. Returns false if filterMask() returned false. 170 This method is not exported to java. 171 */ 172 bool filterPath(const SkPath& devPath, const SkMatrix& devMatrix, 173 const SkRasterClip&, SkBounder*, SkBlitter* blitter, 174 SkPaint::Style style) const; 175 176 typedef SkFlattenable INHERITED; 177 }; 178 179 #endif 180