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 #include "gl/GrGLPathRendering.h"
9 #include "gl/GrGLUtil.h"
10 #include "gl/GrGLGpu.h"
11 
12 #include "GrGLPath.h"
13 #include "GrGLPathRendering.h"
14 #include "GrRenderTargetProxy.h"
15 
16 #include "SkStream.h"
17 #include "SkTypeface.h"
18 
19 #define GL_CALL(X) GR_GL_CALL(this->gpu()->glInterface(), X)
20 #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(this->gpu()->glInterface(), RET, X)
21 
22 // Number of paths to allocate per glGenPaths call. The call can be overly slow on command buffer GL
23 // implementation. The call has a result value, and thus waiting for the call completion is needed.
24 static const GrGLsizei kPathIDPreallocationAmount = 65536;
25 
26 GR_STATIC_ASSERT(0 == GrPathRendering::kNone_PathTransformType);
27 GR_STATIC_ASSERT(1 == GrPathRendering::kTranslateX_PathTransformType);
28 GR_STATIC_ASSERT(2 == GrPathRendering::kTranslateY_PathTransformType);
29 GR_STATIC_ASSERT(3 == GrPathRendering::kTranslate_PathTransformType);
30 GR_STATIC_ASSERT(4 == GrPathRendering::kAffine_PathTransformType);
31 GR_STATIC_ASSERT(GrPathRendering::kAffine_PathTransformType == GrPathRendering::kLast_PathTransformType);
32 
33 #ifdef SK_DEBUG
34 
verify_floats(const float * floats,int count)35 static void verify_floats(const float* floats, int count) {
36     for (int i = 0; i < count; ++i) {
37         SkASSERT(!SkScalarIsNaN(SkFloatToScalar(floats[i])));
38     }
39 }
40 #endif
41 
gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op)42 static GrGLenum gr_stencil_op_to_gl_path_rendering_fill_mode(GrStencilOp op) {
43     switch (op) {
44         default:
45             SK_ABORT("Unexpected path fill.");
46             /* fallthrough */
47         case GrStencilOp::kIncWrap:
48             return GR_GL_COUNT_UP;
49         case GrStencilOp::kInvert:
50             return GR_GL_INVERT;
51     }
52 }
53 
GrGLPathRendering(GrGLGpu * gpu)54 GrGLPathRendering::GrGLPathRendering(GrGLGpu* gpu)
55     : GrPathRendering(gpu)
56     , fPreallocatedPathCount(0) {
57     const GrGLInterface* glInterface = gpu->glInterface();
58     fCaps.bindFragmentInputSupport = (bool)glInterface->fFunctions.fBindFragmentInputLocation;
59 }
60 
~GrGLPathRendering()61 GrGLPathRendering::~GrGLPathRendering() {
62     if (fPreallocatedPathCount > 0) {
63         this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
64     }
65 }
66 
disconnect(GrGpu::DisconnectType type)67 void GrGLPathRendering::disconnect(GrGpu::DisconnectType type) {
68     if (GrGpu::DisconnectType::kCleanup == type) {
69         this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
70     }
71     fPreallocatedPathCount = 0;
72 }
73 
resetContext()74 void GrGLPathRendering::resetContext() {
75     fHWProjectionMatrixState.invalidate();
76     // we don't use the model view matrix.
77     GL_CALL(MatrixLoadIdentity(GR_GL_PATH_MODELVIEW));
78 
79     fHWPathStencilSettings.invalidate();
80 }
81 
createPath(const SkPath & inPath,const GrStyle & style)82 sk_sp<GrPath> GrGLPathRendering::createPath(const SkPath& inPath, const GrStyle& style) {
83     return sk_make_sp<GrGLPath>(this->gpu(), inPath, style);
84 }
85 
onStencilPath(const StencilPathArgs & args,const GrPath * path)86 void GrGLPathRendering::onStencilPath(const StencilPathArgs& args, const GrPath* path) {
87     GrGLGpu* gpu = this->gpu();
88     SkASSERT(gpu->caps()->shaderCaps()->pathRenderingSupport());
89     gpu->flushColorWrite(false);
90 
91     GrGLRenderTarget* rt = static_cast<GrGLRenderTarget*>(args.fProxy->peekRenderTarget());
92     SkISize size = SkISize::Make(rt->width(), rt->height());
93     this->setProjectionMatrix(*args.fViewMatrix, size, args.fProxy->origin());
94     gpu->flushScissor(*args.fScissor, rt->getViewport(), args.fProxy->origin());
95     gpu->flushHWAAState(rt, args.fUseHWAA, true);
96     gpu->flushRenderTarget(rt);
97 
98     const GrGLPath* glPath = static_cast<const GrGLPath*>(path);
99 
100     this->flushPathStencilSettings(*args.fStencil);
101     SkASSERT(!fHWPathStencilSettings.isTwoSided());
102 
103     GrGLenum fillMode =
104         gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp);
105     GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask;
106 
107     if (glPath->shouldFill()) {
108         GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask));
109     }
110     if (glPath->shouldStroke()) {
111         GL_CALL(StencilStrokePath(glPath->pathID(), 0xffff, writeMask));
112     }
113 }
114 
onDrawPath(GrRenderTarget * renderTarget,GrSurfaceOrigin origin,const GrPrimitiveProcessor & primProc,const GrPipeline & pipeline,const GrPipeline::FixedDynamicState & fixedDynamicState,const GrStencilSettings & stencilPassSettings,const GrPath * path)115 void GrGLPathRendering::onDrawPath(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
116                                    const GrPrimitiveProcessor& primProc,
117                                    const GrPipeline& pipeline,
118                                    const GrPipeline::FixedDynamicState& fixedDynamicState,
119                                    const GrStencilSettings& stencilPassSettings,
120                                    const GrPath* path) {
121     if (!this->gpu()->flushGLState(renderTarget, origin, primProc, pipeline,
122                                    &fixedDynamicState, nullptr, 1, false)) {
123         return;
124     }
125     const GrGLPath* glPath = static_cast<const GrGLPath*>(path);
126 
127     this->flushPathStencilSettings(stencilPassSettings);
128     SkASSERT(!fHWPathStencilSettings.isTwoSided());
129 
130     GrGLenum fillMode =
131         gr_stencil_op_to_gl_path_rendering_fill_mode(fHWPathStencilSettings.front().fPassOp);
132     GrGLint writeMask = fHWPathStencilSettings.front().fWriteMask;
133 
134     if (glPath->shouldStroke()) {
135         if (glPath->shouldFill()) {
136             GL_CALL(StencilFillPath(glPath->pathID(), fillMode, writeMask));
137         }
138         GL_CALL(StencilThenCoverStrokePath(glPath->pathID(), 0xffff, writeMask,
139                                            GR_GL_BOUNDING_BOX));
140     } else {
141         GL_CALL(StencilThenCoverFillPath(glPath->pathID(), fillMode, writeMask,
142                                          GR_GL_BOUNDING_BOX));
143     }
144 }
145 
setProgramPathFragmentInputTransform(GrGLuint program,GrGLint location,GrGLenum genMode,GrGLint components,const SkMatrix & matrix)146 void GrGLPathRendering::setProgramPathFragmentInputTransform(GrGLuint program, GrGLint location,
147                                                              GrGLenum genMode, GrGLint components,
148                                                              const SkMatrix& matrix) {
149     float coefficients[3 * 3];
150     SkASSERT(components >= 1 && components <= 3);
151 
152     coefficients[0] = SkScalarToFloat(matrix[SkMatrix::kMScaleX]);
153     coefficients[1] = SkScalarToFloat(matrix[SkMatrix::kMSkewX]);
154     coefficients[2] = SkScalarToFloat(matrix[SkMatrix::kMTransX]);
155 
156     if (components >= 2) {
157         coefficients[3] = SkScalarToFloat(matrix[SkMatrix::kMSkewY]);
158         coefficients[4] = SkScalarToFloat(matrix[SkMatrix::kMScaleY]);
159         coefficients[5] = SkScalarToFloat(matrix[SkMatrix::kMTransY]);
160     }
161 
162     if (components >= 3) {
163         coefficients[6] = SkScalarToFloat(matrix[SkMatrix::kMPersp0]);
164         coefficients[7] = SkScalarToFloat(matrix[SkMatrix::kMPersp1]);
165         coefficients[8] = SkScalarToFloat(matrix[SkMatrix::kMPersp2]);
166     }
167     SkDEBUGCODE(verify_floats(coefficients, components * 3));
168 
169     GL_CALL(ProgramPathFragmentInputGen(program, location, genMode, components, coefficients));
170 }
171 
setProjectionMatrix(const SkMatrix & matrix,const SkISize & renderTargetSize,GrSurfaceOrigin renderTargetOrigin)172 void GrGLPathRendering::setProjectionMatrix(const SkMatrix& matrix,
173                                             const SkISize& renderTargetSize,
174                                             GrSurfaceOrigin renderTargetOrigin) {
175 
176     SkASSERT(this->gpu()->glCaps().shaderCaps()->pathRenderingSupport());
177 
178     if (renderTargetOrigin == fHWProjectionMatrixState.fRenderTargetOrigin &&
179         renderTargetSize == fHWProjectionMatrixState.fRenderTargetSize &&
180         matrix.cheapEqualTo(fHWProjectionMatrixState.fViewMatrix)) {
181         return;
182     }
183 
184     fHWProjectionMatrixState.fViewMatrix = matrix;
185     fHWProjectionMatrixState.fRenderTargetSize = renderTargetSize;
186     fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin;
187 
188     float glMatrix[4 * 4];
189     fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix);
190     SkDEBUGCODE(verify_floats(glMatrix, SK_ARRAY_COUNT(glMatrix)));
191     GL_CALL(MatrixLoadf(GR_GL_PATH_PROJECTION, glMatrix));
192 }
193 
genPaths(GrGLsizei range)194 GrGLuint GrGLPathRendering::genPaths(GrGLsizei range) {
195     SkASSERT(range > 0);
196     GrGLuint firstID;
197     if (fPreallocatedPathCount >= range) {
198         firstID = fFirstPreallocatedPathID;
199         fPreallocatedPathCount -= range;
200         fFirstPreallocatedPathID += range;
201         return firstID;
202     }
203     // Allocate range + the amount to fill up preallocation amount. If succeed, either join with
204     // the existing preallocation range or delete the existing and use the new (potentially partial)
205     // preallocation range.
206     GrGLsizei allocAmount = range + (kPathIDPreallocationAmount - fPreallocatedPathCount);
207     if (allocAmount >= range) {
208         GL_CALL_RET(firstID, GenPaths(allocAmount));
209 
210         if (firstID != 0) {
211             if (fPreallocatedPathCount > 0 &&
212                 firstID == fFirstPreallocatedPathID + fPreallocatedPathCount) {
213                 firstID = fFirstPreallocatedPathID;
214                 fPreallocatedPathCount += allocAmount - range;
215                 fFirstPreallocatedPathID += range;
216                 return firstID;
217             }
218 
219             if (allocAmount > range) {
220                 if (fPreallocatedPathCount > 0) {
221                     this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
222                 }
223                 fFirstPreallocatedPathID = firstID + range;
224                 fPreallocatedPathCount = allocAmount - range;
225             }
226             // Special case: if allocAmount == range, we have full preallocated range.
227             return firstID;
228         }
229     }
230     // Failed to allocate with preallocation. Remove existing preallocation and try to allocate just
231     // the range.
232     if (fPreallocatedPathCount > 0) {
233         this->deletePaths(fFirstPreallocatedPathID, fPreallocatedPathCount);
234         fPreallocatedPathCount = 0;
235     }
236 
237     GL_CALL_RET(firstID, GenPaths(range));
238     if (firstID == 0) {
239         SkDebugf("Warning: Failed to allocate path\n");
240     }
241     return firstID;
242 }
243 
deletePaths(GrGLuint path,GrGLsizei range)244 void GrGLPathRendering::deletePaths(GrGLuint path, GrGLsizei range) {
245     GL_CALL(DeletePaths(path, range));
246 }
247 
flushPathStencilSettings(const GrStencilSettings & stencilSettings)248 void GrGLPathRendering::flushPathStencilSettings(const GrStencilSettings& stencilSettings) {
249     if (fHWPathStencilSettings != stencilSettings) {
250         SkASSERT(stencilSettings.isValid());
251         // Just the func, ref, and mask is set here. The op and write mask are params to the call
252         // that draws the path to the SB (glStencilFillPath)
253         uint16_t ref = stencilSettings.front().fRef;
254         GrStencilTest test = stencilSettings.front().fTest;
255         uint16_t testMask = stencilSettings.front().fTestMask;
256 
257         if (!fHWPathStencilSettings.isValid() ||
258             ref != fHWPathStencilSettings.front().fRef ||
259             test != fHWPathStencilSettings.front().fTest ||
260             testMask != fHWPathStencilSettings.front().fTestMask) {
261             GL_CALL(PathStencilFunc(GrToGLStencilFunc(test), ref, testMask));
262         }
263         fHWPathStencilSettings = stencilSettings;
264     }
265 }
266 
gpu()267 inline GrGLGpu* GrGLPathRendering::gpu() {
268     return static_cast<GrGLGpu*>(fGpu);
269 }
270