Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkMaskFilter_DEFINED
     18 #define SkMaskFilter_DEFINED
     19 
     20 #include "SkFlattenable.h"
     21 #include "SkMask.h"
     22 
     23 class SkBlitter;
     24 class SkBounder;
     25 class SkMatrix;
     26 class SkPath;
     27 class SkRegion;
     28 
     29 /** \class SkMaskFilter
     30 
     31     SkMaskFilter is the base class for object that perform transformations on
     32     an alpha-channel mask before drawing it. A subclass of SkMaskFilter may be
     33     installed into a SkPaint. Once there, each time a primitive is drawn, it
     34     is first scan converted into a SkMask::kA8_Format mask, and handed to the
     35     filter, calling its filterMask() method. If this returns true, then the
     36     new mask is used to render into the device.
     37 
     38     Blur and emboss are implemented as subclasses of SkMaskFilter.
     39 */
     40 class SkMaskFilter : public SkFlattenable {
     41 public:
     42     SkMaskFilter() {}
     43 
     44     /** Returns the format of the resulting mask that this subclass will return
     45         when its filterMask() method is called.
     46     */
     47     virtual SkMask::Format getFormat() = 0;
     48 
     49     /** Create a new mask by filter the src mask.
     50         If src.fImage == null, then do not allocate or create the dst image
     51         but do fill out the other fields in dstMask.
     52         If you do allocate a dst image, use SkMask::AllocImage()
     53         If this returns false, dst mask is ignored.
     54         @param  dst the result of the filter. If src.fImage == null, dst should not allocate its image
     55         @param src the original image to be filtered.
     56         @param matrix the CTM
     57         @param margin   if not null, return the buffer dx/dy need when calculating the effect. Used when
     58                         drawing a clipped object to know how much larger to allocate the src before
     59                         applying the filter. If returning false, ignore this parameter.
     60         @return true if the dst mask was correctly created.
     61     */
     62     virtual bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
     63                             SkIPoint* margin);
     64 
     65     /** Helper method that, given a path in device space, will rasterize it into a kA8_Format mask
     66         and then call filterMask(). If this returns true, the specified blitter will be called
     67         to render that mask. Returns false if filterMask() returned false.
     68         This method is not exported to java.
     69     */
     70     bool filterPath(const SkPath& devPath, const SkMatrix& devMatrix,
     71                     const SkRegion& devClip, SkBounder*, SkBlitter* blitter);
     72 
     73     virtual void flatten(SkFlattenableWriteBuffer& ) {}
     74 
     75     /**
     76      * The fast bounds function is used to enable the paint to be culled early
     77      * in the drawing pipeline. This function accepts the current bounds of the
     78      * paint as its src param and the filter adjust those bounds using its
     79      * current mask and returns the result using the dest param. Callers are
     80      * allowed to provide the same struct for both src and dest so each
     81      * implementation must accomodate that behavior.
     82      *
     83      *  The default impl calls filterMask with the src mask having no image,
     84      *  but subclasses may override this if they can compute the rect faster.
     85      */
     86     virtual void computeFastBounds(const SkRect& src, SkRect* dest);
     87 
     88 protected:
     89     // empty for now, but lets get our subclass to remember to init us for the future
     90     SkMaskFilter(SkFlattenableReadBuffer&) {}
     91 };
     92 
     93 /** \class SkAutoMaskImage
     94 
     95     Stack class used to manage the fImage buffer in a SkMask.
     96     When this object loses scope, the buffer is freed with SkMask::FreeImage().
     97 */
     98 class SkAutoMaskImage {
     99 public:
    100     SkAutoMaskImage(SkMask* mask, bool alloc) {
    101         if (alloc) {
    102             mask->fImage = SkMask::AllocImage(mask->computeImageSize());
    103         }
    104         fImage = mask->fImage;
    105     }
    106 
    107     ~SkAutoMaskImage() {
    108         SkMask::FreeImage(fImage);
    109     }
    110 private:
    111     uint8_t*    fImage;
    112 };
    113 
    114 #endif
    115 
    116