1ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen/*
2ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * Copyright 2014 Google Inc.
3ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen *
4ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * Use of this source code is governed by a BSD-style license that can be
5ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * found in the LICENSE file.
6ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen */
7ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
8ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen#ifndef GrPathRendering_DEFINED
9ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen#define GrPathRendering_DEFINED
10ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
11ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen#include "SkPath.h"
12855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton#include "GrPathRange.h"
13ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
14ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenclass SkStrokeRec;
15855d83ff79c6c822b2ad653f2f890178ad0f637bcdaltonclass SkDescriptor;
16855d83ff79c6c822b2ad653f2f890178ad0f637bcdaltonclass SkTypeface;
17ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenclass GrPath;
18ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenclass GrGpu;
19ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
20ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen/**
21ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * Abstract class wrapping HW path rendering API.
22ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen *
23ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * The subclasses of this class use the possible HW API to render paths (as opposed to path
24ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * rendering implemented in Skia on top of a "3d" HW API).
25ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * The subclasses hold the global state needed to render paths, including shadow of the global HW
26ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * API state. Similar to GrGpu.
27ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen *
28ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * It is expected that the lifetimes of GrGpuXX and GrXXPathRendering are the same. The call context
29ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * interface (eg.  * the concrete instance of GrGpu subclass) should be provided to the instance
30ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen * during construction.
31ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen */
32ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenclass GrPathRendering {
33ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenpublic:
34ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    virtual ~GrPathRendering() { }
35ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
36ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    enum PathTransformType {
37ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kNone_PathTransformType,        //!< []
38ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kTranslateX_PathTransformType,  //!< [kMTransX]
39ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kTranslateY_PathTransformType,  //!< [kMTransY]
40ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kTranslate_PathTransformType,   //!< [kMTransX, kMTransY]
41ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kAffine_PathTransformType,      //!< [kMScaleX, kMSkewX, kMTransX, kMSkewY, kMScaleY, kMTransY]
42ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
43ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        kLast_PathTransformType = kAffine_PathTransformType
44ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    };
45ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
46ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    static inline int PathTransformSize(PathTransformType type) {
47ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        switch (type) {
48ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            case kNone_PathTransformType:
49ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                return 0;
50ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            case kTranslateX_PathTransformType:
51ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            case kTranslateY_PathTransformType:
52ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                return 1;
53ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            case kTranslate_PathTransformType:
54ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                return 2;
55ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            case kAffine_PathTransformType:
56ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                return 6;
57ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
58ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen            default:
59ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                SkFAIL("Unknown path transform type");
60ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                return 0;
61ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen        }
62ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    }
63ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
644e205b10799730b887ae5d6ac7207570292c177fcdalton    /**
654e205b10799730b887ae5d6ac7207570292c177fcdalton     * Creates a new gpu path, based on the specified path and stroke and returns it.
664e205b10799730b887ae5d6ac7207570292c177fcdalton     * The caller owns a ref on the returned path which must be balanced by a call to unref.
674e205b10799730b887ae5d6ac7207570292c177fcdalton     *
684e205b10799730b887ae5d6ac7207570292c177fcdalton     * @param skPath the path geometry.
694e205b10799730b887ae5d6ac7207570292c177fcdalton     * @param stroke the path stroke.
704e205b10799730b887ae5d6ac7207570292c177fcdalton     * @return a new path.
714e205b10799730b887ae5d6ac7207570292c177fcdalton     */
72ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    virtual GrPath* createPath(const SkPath&, const SkStrokeRec&) = 0;
734e205b10799730b887ae5d6ac7207570292c177fcdalton
744e205b10799730b887ae5d6ac7207570292c177fcdalton    /**
754e205b10799730b887ae5d6ac7207570292c177fcdalton     * Creates a range of gpu paths with a common stroke. The caller owns a ref on the
764e205b10799730b887ae5d6ac7207570292c177fcdalton     * returned path range which must be balanced by a call to unref.
774e205b10799730b887ae5d6ac7207570292c177fcdalton     *
784e205b10799730b887ae5d6ac7207570292c177fcdalton     * @param PathGenerator class that generates SkPath objects for each path in the range.
794e205b10799730b887ae5d6ac7207570292c177fcdalton     * @param SkStrokeRec   the common stroke applied to each path in the range.
804e205b10799730b887ae5d6ac7207570292c177fcdalton     * @return a new path range.
814e205b10799730b887ae5d6ac7207570292c177fcdalton     */
82855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton    virtual GrPathRange* createPathRange(GrPathRange::PathGenerator*, const SkStrokeRec&) = 0;
83855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton
84855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton    /**
85855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * Creates a range of glyph paths, indexed by glyph id. The glyphs will have an
86855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * inverted y-direction in order to match the raw font path data. The caller owns
87855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * a ref on the returned path range which must be balanced by a call to unref.
88855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
89855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * @param SkTypeface   Typeface that defines the glyphs.
90855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     If null, the default typeface will be used.
91855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
92855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * @param SkDescriptor Additional font configuration that specifies the font's size,
93855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     stroke, and other flags. This will generally come from an
94855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     SkGlyphCache.
95855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
96855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     It is recommended to leave this value null when possible, in
97855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     which case the glyphs will be loaded directly from the font's
98855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     raw path data and sized at SkPaint::kCanonicalTextSizeForPaths.
99855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     This will result in less memory usage and more efficient paths.
100855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
101855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     If non-null, the glyph paths will match the font descriptor,
102855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     including with the stroke information baked directly into
103855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     the outlines.
104855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
105855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * @param SkStrokeRec  Common stroke that the GPU will apply to every path. Note that
106855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     if the glyph outlines contain baked-in strokes from the font
107855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     descriptor, the GPU stroke will be applied on top of those
108855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *                     outlines.
109855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     *
110855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     * @return a new path range populated with glyphs.
111855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton     */
112855d83ff79c6c822b2ad653f2f890178ad0f637bcdalton    virtual GrPathRange* createGlyphs(const SkTypeface*, const SkDescriptor*, const SkStrokeRec&) = 0;
1134e205b10799730b887ae5d6ac7207570292c177fcdalton
114ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    virtual void stencilPath(const GrPath*, SkPath::FillType) = 0;
115ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    virtual void drawPath(const GrPath*, SkPath::FillType) = 0;
116ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    virtual void drawPaths(const GrPathRange*, const uint32_t indices[], int count,
117ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen                           const float transforms[], PathTransformType, SkPath::FillType) = 0;
118ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenprotected:
119ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    GrPathRendering() { }
120ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
121ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunenprivate:
122ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen    GrPathRendering& operator=(const GrPathRendering&);
123ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen};
124ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen
125ccdaa0422501e5cbcba53d6bd19f2736f1beaef3kkinnunen#endif
126