Home | History | Annotate | Download | only in gpu
      1 /*
      2  * Copyright 2014 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 
      8 #ifndef GrPathRendering_DEFINED
      9 #define GrPathRendering_DEFINED
     10 
     11 #include "SkPath.h"
     12 #include "GrPathRange.h"
     13 #include "GrPipeline.h"
     14 
     15 class SkDescriptor;
     16 class SkTypeface;
     17 class GrGpu;
     18 class GrPath;
     19 class GrStencilSettings;
     20 class GrStyle;
     21 
     22 /**
     23  * Abstract class wrapping HW path rendering API.
     24  *
     25  * The subclasses of this class use the possible HW API to render paths (as opposed to path
     26  * rendering implemented in Skia on top of a "3d" HW API).
     27  * The subclasses hold the global state needed to render paths, including shadow of the global HW
     28  * API state. Similar to GrGpu.
     29  *
     30  * It is expected that the lifetimes of GrGpuXX and GrXXPathRendering are the same. The call context
     31  * interface (eg.  * the concrete instance of GrGpu subclass) should be provided to the instance
     32  * during construction.
     33  */
     34 class GrPathRendering {
     35 public:
     36     virtual ~GrPathRendering() { }
     37 
     38     typedef GrPathRange::PathIndexType PathIndexType;
     39 
     40     enum PathTransformType {
     41         kNone_PathTransformType,        //!< []
     42         kTranslateX_PathTransformType,  //!< [kMTransX]
     43         kTranslateY_PathTransformType,  //!< [kMTransY]
     44         kTranslate_PathTransformType,   //!< [kMTransX, kMTransY]
     45         kAffine_PathTransformType,      //!< [kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY]
     46 
     47         kLast_PathTransformType = kAffine_PathTransformType
     48     };
     49 
     50     static inline int PathTransformSize(PathTransformType type) {
     51         switch (type) {
     52             case kNone_PathTransformType:
     53                 return 0;
     54             case kTranslateX_PathTransformType:
     55             case kTranslateY_PathTransformType:
     56                 return 1;
     57             case kTranslate_PathTransformType:
     58                 return 2;
     59             case kAffine_PathTransformType:
     60                 return 6;
     61 
     62             default:
     63                 SkFAIL("Unknown path transform type");
     64                 return 0;
     65         }
     66     }
     67 
     68     // No native support for inverse at this time
     69     enum FillType {
     70         /** Specifies that "inside" is computed by a non-zero sum of signed
     71             edge crossings
     72         */
     73         kWinding_FillType,
     74         /** Specifies that "inside" is computed by an odd number of edge
     75             crossings
     76         */
     77         kEvenOdd_FillType,
     78     };
     79 
     80     static const GrUserStencilSettings& GetStencilPassSettings(FillType);
     81 
     82     /**
     83      * Creates a new gpu path, based on the specified path and stroke and returns it.
     84      *
     85      * @param SkPath    the geometry.
     86      * @param GrStyle   the style applied to the path. Styles with non-dash path effects are not
     87      *                  allowed.
     88      * @return a new GPU path object.
     89      */
     90     virtual sk_sp<GrPath> createPath(const SkPath&, const GrStyle&) = 0;
     91 
     92     /**
     93      * Creates a range of gpu paths with a common style.
     94      *
     95      * @param PathGenerator class that generates SkPath objects for each path in the range.
     96      * @param GrStyle   the common style applied to each path in the range. Styles with non-dash
     97      *                  path effects are not allowed.
     98      * @return a new path range.
     99      */
    100     virtual sk_sp<GrPathRange> createPathRange(GrPathRange::PathGenerator*, const GrStyle&) = 0;
    101 
    102     /**
    103      * Creates a range of glyph paths, indexed by glyph id. The glyphs will have an
    104      * inverted y-direction in order to match the raw font path data.
    105      *
    106      * @param SkTypeface   Typeface that defines the glyphs.
    107      *                     If null, the default typeface will be used.
    108      *
    109      * @param SkDescriptor Additional font configuration that specifies the font's size,
    110      *                     stroke, and other flags. This will generally come from an
    111      *                     SkGlyphCache.
    112      *
    113      *                     It is recommended to leave this value null when possible, in
    114      *                     which case the glyphs will be loaded directly from the font's
    115      *                     raw path data and sized at SkPaint::kCanonicalTextSizeForPaths.
    116      *                     This will result in less memory usage and more efficient paths.
    117      *
    118      *                     If non-null, the glyph paths will match the font descriptor,
    119      *                     including with the stroke information baked directly into
    120      *                     the outlines.
    121      *
    122      * @param GrStyle      Common style that the GPU will apply to every path. Note that
    123      *                     if the glyph outlines contain baked-in styles from the font
    124      *                     descriptor, the GPU style will be applied on top of those
    125      *                     outlines.
    126      *
    127      * @return a new path range populated with glyphs.
    128      */
    129     sk_sp<GrPathRange> createGlyphs(const SkTypeface*, const SkScalerContextEffects&,
    130                                     const SkDescriptor*, const GrStyle&);
    131 
    132     /** None of these params are optional, pointers used just to avoid making copies. */
    133     struct StencilPathArgs {
    134         StencilPathArgs(bool useHWAA,
    135                         GrRenderTarget* renderTarget,
    136                         const SkMatrix* viewMatrix,
    137                         const GrScissorState* scissor,
    138                         const GrStencilSettings* stencil)
    139             : fUseHWAA(useHWAA)
    140             , fRenderTarget(renderTarget)
    141             , fViewMatrix(viewMatrix)
    142             , fScissor(scissor)
    143             , fStencil(stencil) {
    144         }
    145         bool fUseHWAA;
    146         GrRenderTarget* fRenderTarget;
    147         const SkMatrix* fViewMatrix;
    148         const GrScissorState* fScissor;
    149         const GrStencilSettings* fStencil;
    150     };
    151 
    152     void stencilPath(const StencilPathArgs& args, const GrPath* path);
    153 
    154     void drawPath(const GrPipeline& pipeline,
    155                   const GrPrimitiveProcessor& primProc,
    156                   const GrStencilSettings& stencilPassSettings, // Cover pass settings in pipeline.
    157                   const GrPath* path);
    158 
    159     void drawPaths(const GrPipeline& pipeline,
    160                    const GrPrimitiveProcessor& primProc,
    161                    const GrStencilSettings& stencilPassSettings, // Cover pass settings in pipeline.
    162                    const GrPathRange* pathRange,
    163                    const void* indices,
    164                    PathIndexType indexType,
    165                    const float transformValues[],
    166                    PathTransformType transformType,
    167                    int count);
    168 
    169 protected:
    170     GrPathRendering(GrGpu* gpu) : fGpu(gpu) { }
    171 
    172     virtual void onStencilPath(const StencilPathArgs&, const GrPath*) = 0;
    173     virtual void onDrawPath(const GrPipeline&,
    174                             const GrPrimitiveProcessor&,
    175                             const GrStencilSettings&,
    176                             const GrPath*) = 0;
    177     virtual void onDrawPaths(const GrPipeline&,
    178                              const GrPrimitiveProcessor&,
    179                              const GrStencilSettings&,
    180                              const GrPathRange*,
    181                              const void* indices,
    182                              PathIndexType,
    183                              const float transformValues[],
    184                              PathTransformType,
    185                              int count) = 0;
    186 
    187     GrGpu* fGpu;
    188 private:
    189     GrPathRendering& operator=(const GrPathRendering&);
    190 };
    191 
    192 #endif
    193