Home | History | Annotate | Download | only in include
      1 /*
      2     Copyright 2011 Google Inc.
      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 GrPathRenderer_DEFINED
     18 #define GrPathRenderer_DEFINED
     19 
     20 #include "GrDrawTarget.h"
     21 
     22 class SkPath;
     23 struct GrPoint;
     24 
     25 /**
     26  *  Base class for drawing paths into a GrDrawTarget.
     27  */
     28 class GR_API GrPathRenderer : public GrRefCnt {
     29 public:
     30     /**
     31      * Returns true if this path renderer is able to render the path.
     32      * Returning false allows the caller to fallback to another path renderer.
     33      *
     34      * @param target    The target to draw into
     35      * @param path      The path to draw
     36      * @param fill      The fill rule to use
     37      *
     38      * @return  true if the path can be drawn by this object, false otherwise.
     39      */
     40     virtual bool canDrawPath(const GrDrawTarget* target, const SkPath& path,
     41                              GrPathFill fill) const = 0;
     42 
     43     /**
     44      * Draws a path into the draw target. The target will already have its draw
     45      * state configured for the draw.
     46      * @param target                the target to draw into.
     47      * @param stages                indicates which stages the are already
     48      *                              in use. All enabled stages expect positions
     49      *                              as texture coordinates. The path renderer
     50      *                              use the remaining stages for its path
     51      *                              filling algorithm.
     52      * @param path                  the path to draw.
     53      * @param fill                  the fill rule to apply.
     54      * @param translate             optional additional translation to apply to
     55      *                              the path. NULL means (0,0).
     56      */
     57     virtual void drawPath(GrDrawTarget* target,
     58                           GrDrawTarget::StageBitfield stages,
     59                           const SkPath& path,
     60                           GrPathFill fill,
     61                           const GrPoint* translate) = 0;
     62 
     63     /**
     64      * For complex clips Gr uses the stencil buffer. The path renderer must be
     65      * able to render paths into the stencil buffer. However, the path renderer
     66      * itself may require the stencil buffer to resolve the path fill rule. This
     67      * function queries whether the path render needs its own stencil
     68      * pass. If this returns false then drawPath() should not modify the
     69      * the target's stencil settings but use those already set on target.
     70      *
     71      * @param target target that the path will be rendered to
     72      * @param path   the path that will be drawn
     73      * @param fill   the fill rule that will be used, will never be an inverse
     74      *               rule.
     75      *
     76      * @return false if this path renderer can generate interior-only fragments
     77      *         without changing the stencil settings on the target. If it
     78      *         returns true the drawPathToStencil will be used when rendering
     79      *         clips.
     80      */
     81     virtual bool requiresStencilPass(const GrDrawTarget* target,
     82                                      const SkPath& path,
     83                                      GrPathFill fill) const { return false; }
     84 
     85     /**
     86      * Draws a path to the stencil buffer. Assume the writable stencil bits
     87      * are already initialized to zero. Fill will always be either
     88      * kWinding_PathFill or kEvenOdd_PathFill.
     89      *
     90      * Only called if requiresStencilPass returns true for the same combo of
     91      * target, path, and fill. Never called with an inverse fill.
     92      *
     93      * The default implementation assumes the path filling algorithm doesn't
     94      * require a separate stencil pass and so crashes.
     95      *
     96      *
     97      * @param target                the target to draw into.
     98      * @param path                  the path to draw.
     99      * @param fill                  the fill rule to apply.
    100      * @param translate             optional additional translation to apply to
    101      *                              the path. NULL means (0,0).
    102      */
    103     virtual void drawPathToStencil(GrDrawTarget* target,
    104                                    const SkPath& path,
    105                                    GrPathFill fill,
    106                                    const GrPoint* translate) {
    107         GrCrash("Unexpected call to drawPathToStencil.");
    108     }
    109 
    110     /**
    111      * @return true if the path renderer can perform anti-aliasing (aside from
    112      * having FSAA enabled for a render target)
    113      */
    114     virtual bool supportsAA(GrDrawTarget* target,
    115                             const SkPath& path,
    116                             GrPathFill fill) { return false; }
    117 
    118     /**
    119      * This is called to install a custom path renderer in every GrContext at
    120      * create time. The default implementation in GrCreatePathRenderer_none.cpp
    121      * returns NULL. Link against another implementation to install your own.
    122      */
    123     static GrPathRenderer* CreatePathRenderer();
    124 
    125 private:
    126 
    127     typedef GrRefCnt INHERITED;
    128 };
    129 
    130 /**
    131  *  Subclass that renders the path using the stencil buffer to resolve fill
    132  *  rules (e.g. winding, even-odd)
    133  */
    134 class GR_API GrDefaultPathRenderer : public GrPathRenderer {
    135 public:
    136     GrDefaultPathRenderer(bool separateStencilSupport,
    137                           bool stencilWrapOpsSupport);
    138 
    139     virtual bool canDrawPath(const GrDrawTarget* target,
    140                              const SkPath& path,
    141                              GrPathFill fill) const { return true; }
    142 
    143     virtual void drawPath(GrDrawTarget* target,
    144                           GrDrawTarget::StageBitfield stages,
    145                           const SkPath& path,
    146                           GrPathFill fill,
    147                           const GrPoint* translate);
    148     virtual bool requiresStencilPass(const GrDrawTarget* target,
    149                                      const SkPath& path,
    150                                      GrPathFill fill) const;
    151     virtual void drawPathToStencil(GrDrawTarget* target,
    152                                    const SkPath& path,
    153                                    GrPathFill fill,
    154                                    const GrPoint* translate);
    155 private:
    156 
    157     void onDrawPath(GrDrawTarget* target,
    158                     GrDrawTarget::StageBitfield stages,
    159                     const SkPath& path,
    160                     GrPathFill fill,
    161                     const GrPoint* translate,
    162                     bool stencilOnly);
    163 
    164     bool    fSeparateStencil;
    165     bool    fStencilWrapOps;
    166 
    167     typedef GrPathRenderer INHERITED;
    168 };
    169 
    170 #endif
    171