Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef GrPathRenderer_DEFINED |
michael@0 | 10 | #define GrPathRenderer_DEFINED |
michael@0 | 11 | |
michael@0 | 12 | #include "GrDrawTarget.h" |
michael@0 | 13 | #include "GrPathRendererChain.h" |
michael@0 | 14 | #include "GrStencil.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "SkDrawProcs.h" |
michael@0 | 17 | #include "SkStrokeRec.h" |
michael@0 | 18 | #include "SkTArray.h" |
michael@0 | 19 | |
michael@0 | 20 | class SkPath; |
michael@0 | 21 | |
michael@0 | 22 | struct GrPoint; |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Base class for drawing paths into a GrDrawTarget. |
michael@0 | 26 | * |
michael@0 | 27 | * Derived classes can use stages GrPaint::kTotalStages through GrDrawState::kNumStages-1. The |
michael@0 | 28 | * stages before GrPaint::kTotalStages are reserved for setting up the draw (i.e., textures and |
michael@0 | 29 | * filter masks). |
michael@0 | 30 | */ |
michael@0 | 31 | class SK_API GrPathRenderer : public SkRefCnt { |
michael@0 | 32 | public: |
michael@0 | 33 | SK_DECLARE_INST_COUNT(GrPathRenderer) |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * This is called to install custom path renderers in every GrContext at create time. The |
michael@0 | 37 | * default implementation in GrCreatePathRenderer_none.cpp does not add any additional |
michael@0 | 38 | * renderers. Link against another implementation to install your own. The first added is the |
michael@0 | 39 | * most preferred path renderer, second is second most preferred, etc. |
michael@0 | 40 | * |
michael@0 | 41 | * @param context the context that will use the path renderer |
michael@0 | 42 | * @param prChain the chain to add path renderers to. |
michael@0 | 43 | */ |
michael@0 | 44 | static void AddPathRenderers(GrContext* context, GrPathRendererChain* prChain); |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | GrPathRenderer(); |
michael@0 | 48 | |
michael@0 | 49 | /** |
michael@0 | 50 | * A caller may wish to use a path renderer to draw a path into the stencil buffer. However, |
michael@0 | 51 | * the path renderer itself may require use of the stencil buffer. Also a path renderer may |
michael@0 | 52 | * use a GrEffect coverage stage that sets coverage to zero to eliminate pixels that are covered |
michael@0 | 53 | * by bounding geometry but outside the path. These exterior pixels would still be rendered into |
michael@0 | 54 | * the stencil. |
michael@0 | 55 | * |
michael@0 | 56 | * A GrPathRenderer can provide three levels of support for stenciling paths: |
michael@0 | 57 | * 1) kNoRestriction: This is the most general. The caller sets up the GrDrawState on the target |
michael@0 | 58 | * and calls drawPath(). The path is rendered exactly as the draw state |
michael@0 | 59 | * indicates including support for simultaneous color and stenciling with |
michael@0 | 60 | * arbitrary stenciling rules. Pixels partially covered by AA paths are |
michael@0 | 61 | * affected by the stencil settings. |
michael@0 | 62 | * 2) kStencilOnly: The path renderer cannot apply arbitrary stencil rules nor shade and stencil |
michael@0 | 63 | * simultaneously. The path renderer does support the stencilPath() function |
michael@0 | 64 | * which performs no color writes and writes a non-zero stencil value to pixels |
michael@0 | 65 | * covered by the path. |
michael@0 | 66 | * 3) kNoSupport: This path renderer cannot be used to stencil the path. |
michael@0 | 67 | */ |
michael@0 | 68 | typedef GrPathRendererChain::StencilSupport StencilSupport; |
michael@0 | 69 | static const StencilSupport kNoSupport_StencilSupport = |
michael@0 | 70 | GrPathRendererChain::kNoSupport_StencilSupport; |
michael@0 | 71 | static const StencilSupport kStencilOnly_StencilSupport = |
michael@0 | 72 | GrPathRendererChain::kStencilOnly_StencilSupport; |
michael@0 | 73 | static const StencilSupport kNoRestriction_StencilSupport = |
michael@0 | 74 | GrPathRendererChain::kNoRestriction_StencilSupport; |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * This function is to get the stencil support for a particular path. The path's fill must |
michael@0 | 78 | * not be an inverse type. |
michael@0 | 79 | * |
michael@0 | 80 | * @param target target that the path will be rendered to |
michael@0 | 81 | * @param path the path that will be drawn |
michael@0 | 82 | * @param stroke the stroke information (width, join, cap). |
michael@0 | 83 | */ |
michael@0 | 84 | StencilSupport getStencilSupport(const SkPath& path, |
michael@0 | 85 | const SkStrokeRec& stroke, |
michael@0 | 86 | const GrDrawTarget* target) const { |
michael@0 | 87 | SkASSERT(!path.isInverseFillType()); |
michael@0 | 88 | return this->onGetStencilSupport(path, stroke, target); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * Returns true if this path renderer is able to render the path. Returning false allows the |
michael@0 | 93 | * caller to fallback to another path renderer This function is called when searching for a path |
michael@0 | 94 | * renderer capable of rendering a path. |
michael@0 | 95 | * |
michael@0 | 96 | * @param path The path to draw |
michael@0 | 97 | * @param stroke The stroke information (width, join, cap) |
michael@0 | 98 | * @param target The target that the path will be rendered to |
michael@0 | 99 | * @param antiAlias True if anti-aliasing is required. |
michael@0 | 100 | * |
michael@0 | 101 | * @return true if the path can be drawn by this object, false otherwise. |
michael@0 | 102 | */ |
michael@0 | 103 | virtual bool canDrawPath(const SkPath& path, |
michael@0 | 104 | const SkStrokeRec& rec, |
michael@0 | 105 | const GrDrawTarget* target, |
michael@0 | 106 | bool antiAlias) const = 0; |
michael@0 | 107 | /** |
michael@0 | 108 | * Draws the path into the draw target. If getStencilSupport() would return kNoRestriction then |
michael@0 | 109 | * the subclass must respect the stencil settings of the target's draw state. |
michael@0 | 110 | * |
michael@0 | 111 | * @param path the path to draw. |
michael@0 | 112 | * @param stroke the stroke information (width, join, cap) |
michael@0 | 113 | * @param target target that the path will be rendered to |
michael@0 | 114 | * @param antiAlias true if anti-aliasing is required. |
michael@0 | 115 | */ |
michael@0 | 116 | bool drawPath(const SkPath& path, |
michael@0 | 117 | const SkStrokeRec& stroke, |
michael@0 | 118 | GrDrawTarget* target, |
michael@0 | 119 | bool antiAlias) { |
michael@0 | 120 | SkASSERT(!path.isEmpty()); |
michael@0 | 121 | SkASSERT(this->canDrawPath(path, stroke, target, antiAlias)); |
michael@0 | 122 | SkASSERT(target->drawState()->getStencil().isDisabled() || |
michael@0 | 123 | kNoRestriction_StencilSupport == this->getStencilSupport(path, stroke, target)); |
michael@0 | 124 | return this->onDrawPath(path, stroke, target, antiAlias); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Draws the path to the stencil buffer. Assume the writable stencil bits are already |
michael@0 | 129 | * initialized to zero. The pixels inside the path will have non-zero stencil values afterwards. |
michael@0 | 130 | * |
michael@0 | 131 | * @param path the path to draw. |
michael@0 | 132 | * @param stroke the stroke information (width, join, cap) |
michael@0 | 133 | * @param target target that the path will be rendered to |
michael@0 | 134 | */ |
michael@0 | 135 | void stencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) { |
michael@0 | 136 | SkASSERT(!path.isEmpty()); |
michael@0 | 137 | SkASSERT(kNoSupport_StencilSupport != this->getStencilSupport(path, stroke, target)); |
michael@0 | 138 | this->onStencilPath(path, stroke, target); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | // Helper for determining if we can treat a thin stroke as a hairline w/ coverage. |
michael@0 | 142 | // If we can, we draw lots faster (raster device does this same test). |
michael@0 | 143 | static bool IsStrokeHairlineOrEquivalent(const SkStrokeRec& stroke, const SkMatrix& matrix, |
michael@0 | 144 | SkScalar* outCoverage) { |
michael@0 | 145 | if (stroke.isHairlineStyle()) { |
michael@0 | 146 | if (NULL != outCoverage) { |
michael@0 | 147 | *outCoverage = SK_Scalar1; |
michael@0 | 148 | } |
michael@0 | 149 | return true; |
michael@0 | 150 | } |
michael@0 | 151 | return stroke.getStyle() == SkStrokeRec::kStroke_Style && |
michael@0 | 152 | SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | protected: |
michael@0 | 156 | /** |
michael@0 | 157 | * Subclass overrides if it has any limitations of stenciling support. |
michael@0 | 158 | */ |
michael@0 | 159 | virtual StencilSupport onGetStencilSupport(const SkPath&, |
michael@0 | 160 | const SkStrokeRec&, |
michael@0 | 161 | const GrDrawTarget*) const { |
michael@0 | 162 | return kNoRestriction_StencilSupport; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | /** |
michael@0 | 166 | * Subclass implementation of drawPath() |
michael@0 | 167 | */ |
michael@0 | 168 | virtual bool onDrawPath(const SkPath& path, |
michael@0 | 169 | const SkStrokeRec& stroke, |
michael@0 | 170 | GrDrawTarget* target, |
michael@0 | 171 | bool antiAlias) = 0; |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * Subclass implementation of stencilPath(). Subclass must override iff it ever returns |
michael@0 | 175 | * kStencilOnly in onGetStencilSupport(). |
michael@0 | 176 | */ |
michael@0 | 177 | virtual void onStencilPath(const SkPath& path, const SkStrokeRec& stroke, GrDrawTarget* target) { |
michael@0 | 178 | GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit); |
michael@0 | 179 | GrDrawState* drawState = target->drawState(); |
michael@0 | 180 | GR_STATIC_CONST_SAME_STENCIL(kIncrementStencil, |
michael@0 | 181 | kReplace_StencilOp, |
michael@0 | 182 | kReplace_StencilOp, |
michael@0 | 183 | kAlways_StencilFunc, |
michael@0 | 184 | 0xffff, |
michael@0 | 185 | 0xffff, |
michael@0 | 186 | 0xffff); |
michael@0 | 187 | drawState->setStencil(kIncrementStencil); |
michael@0 | 188 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
michael@0 | 189 | this->drawPath(path, stroke, target, false); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | // Helper for getting the device bounds of a path. Inverse filled paths will have bounds set |
michael@0 | 193 | // by devSize. Non-inverse path bounds will not necessarily be clipped to devSize. |
michael@0 | 194 | static void GetPathDevBounds(const SkPath& path, |
michael@0 | 195 | int devW, |
michael@0 | 196 | int devH, |
michael@0 | 197 | const SkMatrix& matrix, |
michael@0 | 198 | SkRect* bounds); |
michael@0 | 199 | |
michael@0 | 200 | // Helper version that gets the dev width and height from a GrSurface. |
michael@0 | 201 | static void GetPathDevBounds(const SkPath& path, |
michael@0 | 202 | const GrSurface* device, |
michael@0 | 203 | const SkMatrix& matrix, |
michael@0 | 204 | SkRect* bounds) { |
michael@0 | 205 | GetPathDevBounds(path, device->width(), device->height(), matrix, bounds); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | private: |
michael@0 | 209 | |
michael@0 | 210 | typedef SkRefCnt INHERITED; |
michael@0 | 211 | }; |
michael@0 | 212 | |
michael@0 | 213 | #endif |