• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 GrSurfaceDrawContext_DEFINED
9 #define GrSurfaceDrawContext_DEFINED
10 
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkDrawable.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSurface.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "include/private/GrTypesPriv.h"
17 #include "src/core/SkGlyphRunPainter.h"
18 #include "src/gpu/GrOpsTask.h"
19 #include "src/gpu/GrPaint.h"
20 #include "src/gpu/GrRenderTargetProxy.h"
21 #include "src/gpu/GrSurfaceFillContext.h"
22 #include "src/gpu/GrSurfaceProxyView.h"
23 #include "src/gpu/GrXferProcessor.h"
24 #include "src/gpu/geometry/GrQuad.h"
25 
26 class GrBackendSemaphore;
27 class GrClip;
28 class GrColorSpaceXform;
29 class GrCoverageCountingPathRenderer;
30 class GrDrawOp;
31 class GrOp;
32 class GrRenderTarget;
33 class GrStyledShape;
34 class GrStyle;
35 class GrTextureProxy;
36 struct GrUserStencilSettings;
37 struct SkDrawShadowRec;
38 class SkGlyphRunList;
39 struct SkIPoint;
40 struct SkIRect;
41 class SkLatticeIter;
42 class SkMatrixProvider;
43 class SkMatrix;
44 class SkPaint;
45 class SkPath;
46 struct SkPoint;
47 struct SkRect;
48 class SkRegion;
49 class SkRRect;
50 struct SkRSXform;
51 class SkRuntimeEffect;
52 class SkTextBlob;
53 class SkVertices;
54 
55 /**
56  * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
57  */
58 class GrSurfaceDrawContext : public GrSurfaceFillContext {
59 public:
60     static std::unique_ptr<GrSurfaceDrawContext> Make(GrRecordingContext*,
61                                                       GrColorType,
62                                                       sk_sp<SkColorSpace>,
63                                                       sk_sp<GrSurfaceProxy>,
64                                                       GrSurfaceOrigin,
65                                                       const SkSurfaceProps&,
66                                                       bool flushTimeOpsTask = false);
67 
68     /* Uses the default texture format for the color type */
69     static std::unique_ptr<GrSurfaceDrawContext> Make(GrRecordingContext*,
70                                                       GrColorType,
71                                                       sk_sp<SkColorSpace>,
72                                                       SkBackingFit,
73                                                       SkISize dimensions,
74                                                       const SkSurfaceProps&,
75                                                       int sampleCnt = 1,
76                                                       GrMipmapped = GrMipmapped::kNo,
77                                                       GrProtected = GrProtected::kNo,
78                                                       GrSurfaceOrigin = kBottomLeft_GrSurfaceOrigin,
79                                                       SkBudgeted = SkBudgeted::kYes);
80 
81     /**
82      * Takes custom swizzles rather than determining swizzles from color type and format.
83      * It will have color type kUnknown.
84      */
85     static std::unique_ptr<GrSurfaceDrawContext> Make(GrRecordingContext*,
86                                                       sk_sp<SkColorSpace>,
87                                                       SkBackingFit,
88                                                       SkISize dimensions,
89                                                       const GrBackendFormat&,
90                                                       int sampleCnt,
91                                                       GrMipmapped,
92                                                       GrProtected,
93                                                       GrSwizzle readSwizzle,
94                                                       GrSwizzle writeSwizzle,
95                                                       GrSurfaceOrigin,
96                                                       SkBudgeted,
97                                                       const SkSurfaceProps&);
98 
99     // Same as previous factory but will try to use fallback GrColorTypes if the one passed in
100     // fails. The fallback GrColorType will have at least the number of channels and precision per
101     // channel as the passed in GrColorType. It may also swizzle the changes (e.g., BGRA -> RGBA).
102     // SRGB-ness will be preserved.
103     static std::unique_ptr<GrSurfaceDrawContext> MakeWithFallback(
104             GrRecordingContext*,
105             GrColorType,
106             sk_sp<SkColorSpace>,
107             SkBackingFit,
108             SkISize dimensions,
109             const SkSurfaceProps&,
110             int sampleCnt = 1,
111             GrMipmapped = GrMipmapped::kNo,
112             GrProtected = GrProtected::kNo,
113             GrSurfaceOrigin = kBottomLeft_GrSurfaceOrigin,
114             SkBudgeted = SkBudgeted::kYes);
115 
116     // These match the definitions in SkSurface & GrSurface.h, for whence they came
117     typedef void* ReleaseContext;
118     typedef void (*ReleaseProc)(ReleaseContext);
119 
120     // Creates a GrSurfaceDrawContext that wraps the passed in GrBackendTexture.
121     static std::unique_ptr<GrSurfaceDrawContext> MakeFromBackendTexture(
122             GrRecordingContext*, GrColorType, sk_sp<SkColorSpace>, const GrBackendTexture&,
123             int sampleCnt, GrSurfaceOrigin, const SkSurfaceProps&,
124             sk_sp<GrRefCntedCallback> releaseHelper);
125 
126     static std::unique_ptr<GrSurfaceDrawContext> MakeFromBackendRenderTarget(
127             GrRecordingContext*,
128             GrColorType,
129             sk_sp<SkColorSpace>,
130             const GrBackendRenderTarget&,
131             GrSurfaceOrigin,
132             const SkSurfaceProps&,
133             sk_sp<GrRefCntedCallback> releaseHelper);
134 
135     static std::unique_ptr<GrSurfaceDrawContext> MakeFromVulkanSecondaryCB(
136             GrRecordingContext*, const SkImageInfo&, const GrVkDrawableInfo&,
137             const SkSurfaceProps&);
138 
139     GrSurfaceDrawContext(GrRecordingContext*,
140                          GrSurfaceProxyView readView,
141                          GrSurfaceProxyView writeView,
142                          GrColorType,
143                          sk_sp<SkColorSpace>,
144                          const SkSurfaceProps&,
145                          bool flushTimeOpsTask = false);
146 
147     ~GrSurfaceDrawContext() override;
148 
149     /**
150      *  Draw everywhere (respecting the clip) with the paint.
151      */
152     void drawPaint(const GrClip*, GrPaint&&, const SkMatrix& viewMatrix);
153 
154     /**
155      * Draw the rect using a paint.
156      * @param paint        describes how to color pixels.
157      * @param GrAA         Controls whether rect is antialiased
158      * @param viewMatrix   transformation matrix
159      * @param style        The style to apply. Null means fill. Currently path effects are not
160      *                     allowed.
161      * The rects coords are used to access the paint (through texture matrix)
162      */
163     void drawRect(const GrClip*,
164                   GrPaint&& paint,
165                   GrAA,
166                   const SkMatrix& viewMatrix,
167                   const SkRect&,
168                   const GrStyle* style = nullptr);
169 
170     /**
171      * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
172      *
173      * @param GrPaint      describes how to color pixels.
174      * @param GrAA         Controls whether rect is antialiased
175      * @param SkMatrix     transformation matrix which applies to rectToDraw
176      * @param rectToDraw   the rectangle to draw
177      * @param localRect    the rectangle of shader coordinates applied to rectToDraw
178      */
179     void fillRectToRect(const GrClip*,
180                         GrPaint&&,
181                         GrAA,
182                         const SkMatrix&,
183                         const SkRect& rectToDraw,
184                         const SkRect& localRect);
185 
186     /**
187      * Fills a block of pixels with a paint and a localMatrix, respecting the clip.
188      */
fillPixelsWithLocalMatrix(const GrClip * clip,GrPaint && paint,const SkIRect & bounds,const SkMatrix & localMatrix)189     void fillPixelsWithLocalMatrix(const GrClip* clip,
190                                    GrPaint&& paint,
191                                    const SkIRect& bounds,
192                                    const SkMatrix& localMatrix) {
193         SkRect rect = SkRect::Make(bounds);
194         DrawQuad quad{GrQuad::MakeFromRect(rect, SkMatrix::I()),
195                       GrQuad::MakeFromRect(rect, localMatrix), GrQuadAAFlags::kNone};
196         this->drawFilledQuad(clip, std::move(paint), GrAA::kNo, &quad);
197     }
198 
199     /**
200      * Creates an op that draws a fill rect with per-edge control over anti-aliasing.
201      *
202      * This is a specialized version of fillQuadWithEdgeAA, but is kept separate since knowing
203      * the geometry is a rectangle affords more optimizations.
204      */
205     void fillRectWithEdgeAA(const GrClip* clip, GrPaint&& paint, GrAA aa, GrQuadAAFlags edgeAA,
206                             const SkMatrix& viewMatrix, const SkRect& rect,
207                             const SkRect* optionalLocalRect = nullptr) {
208         if (edgeAA == GrQuadAAFlags::kAll) {
209             this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect,
210                                  (optionalLocalRect) ? *optionalLocalRect : rect);
211             return;
212         }
213         const SkRect& localRect = optionalLocalRect ? *optionalLocalRect : rect;
214         DrawQuad quad{GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(localRect), edgeAA};
215         this->drawFilledQuad(clip, std::move(paint), aa, &quad);
216     }
217 
218     /**
219      * Similar to fillRectWithEdgeAA but draws an arbitrary 2D convex quadrilateral transformed
220      * by 'viewMatrix', with per-edge control over anti-aliasing. The quad should follow the
221      * ordering used by SkRect::toQuad(), which determines how the edge AA is applied:
222      *  - "top" = points [0] and [1]
223      *  - "right" = points[1] and [2]
224      *  - "bottom" = points[2] and [3]
225      *  - "left" = points[3] and [0]
226      *
227      * The last argument, 'optionalLocalQuad', can be null if no separate local coordinates are
228      * necessary.
229      */
fillQuadWithEdgeAA(const GrClip * clip,GrPaint && paint,GrAA aa,GrQuadAAFlags edgeAA,const SkMatrix & viewMatrix,const SkPoint points[4],const SkPoint optionalLocalPoints[4])230     void fillQuadWithEdgeAA(const GrClip* clip, GrPaint&& paint, GrAA aa, GrQuadAAFlags edgeAA,
231                             const SkMatrix& viewMatrix, const SkPoint points[4],
232                             const SkPoint optionalLocalPoints[4]) {
233         const SkPoint* localPoints = optionalLocalPoints ? optionalLocalPoints : points;
234         DrawQuad quad{GrQuad::MakeFromSkQuad(points, viewMatrix),
235                       GrQuad::MakeFromSkQuad(localPoints, SkMatrix::I()), edgeAA};
236         this->drawFilledQuad(clip, std::move(paint), aa, &quad);
237     }
238 
239     /** Used with drawQuadSet */
240     struct QuadSetEntry {
241         SkRect fRect;
242         SkPMColor4f fColor; // Overrides any color on the GrPaint
243         SkMatrix fLocalMatrix;
244         GrQuadAAFlags fAAFlags;
245     };
246 
247     // TODO(michaelludwig) - remove if the bulk API is not useful for SkiaRenderer
248     void drawQuadSet(const GrClip* clip, GrPaint&& paint, GrAA aa, const SkMatrix& viewMatrix,
249                      const QuadSetEntry[], int cnt);
250 
251     /**
252      * Creates an op that draws a subrectangle of a texture. The passed color is modulated by the
253      * texture's color. 'srcRect' specifies the rectangle of the texture to draw. 'dstRect'
254      * specifies the rectangle to draw in local coords which will be transformed by 'viewMatrix' to
255      * device space.
256      */
257     void drawTexture(const GrClip*,
258                      GrSurfaceProxyView,
259                      SkAlphaType,
260                      GrSamplerState::Filter,
261                      GrSamplerState::MipmapMode,
262                      SkBlendMode,
263                      const SkPMColor4f&,
264                      const SkRect& srcRect,
265                      const SkRect& dstRect,
266                      GrAA,
267                      GrQuadAAFlags,
268                      SkCanvas::SrcRectConstraint,
269                      const SkMatrix&,
270                      sk_sp<GrColorSpaceXform>);
271 
272     /**
273      * Variant of drawTexture that instead draws the texture applied to 'dstQuad' transformed by
274      * 'viewMatrix', using the 'srcQuad' texture coordinates clamped to the optional 'subset'. If
275      * 'subset' is null, it's equivalent to using the fast src rect constraint. If 'subset' is
276      * provided, the strict src rect constraint is applied using 'subset'.
277      */
drawTextureQuad(const GrClip * clip,GrSurfaceProxyView view,GrColorType srcColorType,SkAlphaType srcAlphaType,GrSamplerState::Filter filter,GrSamplerState::MipmapMode mm,SkBlendMode mode,const SkPMColor4f & color,const SkPoint srcQuad[4],const SkPoint dstQuad[4],GrAA aa,GrQuadAAFlags edgeAA,const SkRect * subset,const SkMatrix & viewMatrix,sk_sp<GrColorSpaceXform> texXform)278     void drawTextureQuad(const GrClip* clip,
279                          GrSurfaceProxyView view,
280                          GrColorType srcColorType,
281                          SkAlphaType srcAlphaType,
282                          GrSamplerState::Filter filter,
283                          GrSamplerState::MipmapMode mm,
284                          SkBlendMode mode,
285                          const SkPMColor4f& color,
286                          const SkPoint srcQuad[4],
287                          const SkPoint dstQuad[4],
288                          GrAA aa,
289                          GrQuadAAFlags edgeAA,
290                          const SkRect* subset,
291                          const SkMatrix& viewMatrix,
292                          sk_sp<GrColorSpaceXform> texXform) {
293         DrawQuad quad{GrQuad::MakeFromSkQuad(dstQuad, viewMatrix),
294                       GrQuad::MakeFromSkQuad(srcQuad, SkMatrix::I()), edgeAA};
295         this->drawTexturedQuad(clip, std::move(view), srcAlphaType, std::move(texXform), filter, mm,
296                                color, mode, aa, &quad, subset);
297     }
298 
299     /** Used with drawTextureSet */
300     struct TextureSetEntry {
301         GrSurfaceProxyView fProxyView;
302         SkAlphaType fSrcAlphaType;
303         SkRect fSrcRect;
304         SkRect fDstRect;
305         const SkPoint* fDstClipQuad; // Must be null, or point to an array of 4 points
306         const SkMatrix* fPreViewMatrix; // If not null, entry's CTM is 'viewMatrix' * fPreViewMatrix
307         SkPMColor4f   fColor; // {a,a,a,a} for rgb textures, {r,g,b,a} for alpha-only textures
308         GrQuadAAFlags fAAFlags;
309     };
310     /**
311      * Draws a set of textures with a shared filter, color, view matrix, color xform, and
312      * texture color xform. The textures must all have the same GrTextureType and GrConfig.
313      *
314      * If any entries provide a non-null fDstClip array, it will be read from immediately based on
315      * fDstClipCount, so the pointer can become invalid after this returns.
316      *
317      * 'proxRunCnt' is the number of proxy changes encountered in the entry array. Technically this
318      * can be inferred from the array within this function, but the information is already known
319      * by SkGpuDevice, so no need to incur another iteration over the array.
320      */
321     void drawTextureSet(const GrClip*,
322                         TextureSetEntry[],
323                         int cnt,
324                         int proxyRunCnt,
325                         GrSamplerState::Filter,
326                         GrSamplerState::MipmapMode,
327                         SkBlendMode mode,
328                         GrAA aa,
329                         SkCanvas::SrcRectConstraint,
330                         const SkMatrix& viewMatrix,
331                         sk_sp<GrColorSpaceXform> texXform);
332 
333     /**
334      * Draw a roundrect using a paint.
335      *
336      * @param paint       describes how to color pixels.
337      * @param GrAA        Controls whether rrect is antialiased.
338      * @param viewMatrix  transformation matrix
339      * @param rrect       the roundrect to draw
340      * @param style       style to apply to the rrect. Currently path effects are not allowed.
341      */
342     void drawRRect(const GrClip*,
343                    GrPaint&&,
344                    GrAA,
345                    const SkMatrix& viewMatrix,
346                    const SkRRect& rrect,
347                    const GrStyle& style);
348 
349     /**
350      * Use a fast method to render the ambient and spot shadows for a path.
351      * Will return false if not possible for the given path.
352      *
353      * @param viewMatrix   transformation matrix
354      * @param path         the path to shadow
355      * @param rec          parameters for shadow rendering
356      */
357     bool drawFastShadow(const GrClip*,
358                         const SkMatrix& viewMatrix,
359                         const SkPath& path,
360                         const SkDrawShadowRec& rec);
361 
362     /**
363      * Draws a path.
364      *
365      * @param paint         describes how to color pixels.
366      * @param GrAA          Controls whether the path is antialiased.
367      * @param viewMatrix    transformation matrix
368      * @param path          the path to draw
369      * @param style         style to apply to the path.
370      */
371     void drawPath(const GrClip*,
372                   GrPaint&&,
373                   GrAA,
374                   const SkMatrix& viewMatrix,
375                   const SkPath&,
376                   const GrStyle&);
377 
378     /**
379      * Draws a shape.
380      *
381      * @param paint         describes how to color pixels.
382      * @param GrAA          Controls whether the path is antialiased.
383      * @param viewMatrix    transformation matrix
384      * @param shape         the shape to draw
385      */
386     void drawShape(const GrClip*,
387                    GrPaint&&,
388                    GrAA,
389                    const SkMatrix& viewMatrix,
390                    GrStyledShape&&);
391 
392 
393     /**
394      * Draws vertices with a paint.
395      *
396      * @param   paint            describes how to color pixels.
397      * @param   viewMatrix       transformation matrix
398      * @param   vertices         specifies the mesh to draw.
399      * @param   overridePrimType primitive type to draw. If NULL, derive prim type from vertices.
400      * @param   effect           runtime effect that will handle custom vertex attributes.
401      */
402     void drawVertices(const GrClip*,
403                       GrPaint&& paint,
404                       const SkMatrixProvider& matrixProvider,
405                       sk_sp<SkVertices> vertices,
406                       GrPrimitiveType* overridePrimType = nullptr,
407                       const SkRuntimeEffect* effect = nullptr);
408 
409     /**
410      * Draws textured sprites from an atlas with a paint. This currently does not support AA for the
411      * sprite rectangle edges.
412      *
413      * @param   paint           describes how to color pixels.
414      * @param   viewMatrix      transformation matrix
415      * @param   spriteCount     number of sprites.
416      * @param   xform           array of compressed transformation data, required.
417      * @param   texRect         array of texture rectangles used to access the paint.
418      * @param   colors          optional array of per-sprite colors, supercedes
419      *                          the paint's color field.
420      */
421     void drawAtlas(const GrClip*,
422                    GrPaint&& paint,
423                    const SkMatrix& viewMatrix,
424                    int spriteCount,
425                    const SkRSXform xform[],
426                    const SkRect texRect[],
427                    const SkColor colors[]);
428 
429     /**
430      * Draws a region.
431      *
432      * @param paint         describes how to color pixels
433      * @param viewMatrix    transformation matrix
434      * @param aa            should the rects of the region be antialiased.
435      * @param region        the region to be drawn
436      * @param style         style to apply to the region
437      */
438     void drawRegion(const GrClip*,
439                     GrPaint&& paint,
440                     GrAA aa,
441                     const SkMatrix& viewMatrix,
442                     const SkRegion& region,
443                     const GrStyle& style,
444                     const GrUserStencilSettings* ss = nullptr);
445 
446     /**
447      * Draws an oval.
448      *
449      * @param paint         describes how to color pixels.
450      * @param GrAA          Controls whether the oval is antialiased.
451      * @param viewMatrix    transformation matrix
452      * @param oval          the bounding rect of the oval.
453      * @param style         style to apply to the oval. Currently path effects are not allowed.
454      */
455     void drawOval(const GrClip*,
456                   GrPaint&& paint,
457                   GrAA,
458                   const SkMatrix& viewMatrix,
459                   const SkRect& oval,
460                   const GrStyle& style);
461 
462     /**
463      * Draws a partial arc of an oval.
464      *
465      * @param paint         describes how to color pixels.
466      * @param GrGrAA        Controls whether the arc is antialiased.
467      * @param viewMatrix    transformation matrix.
468      * @param oval          the bounding rect of the oval.
469      * @param startAngle    starting angle in degrees.
470      * @param sweepAngle    angle to sweep in degrees. Must be in (-360, 360)
471      * @param useCenter     true means that the implied path begins at the oval center, connects as
472      *                      a line to the point indicated by the start contains the arc indicated by
473      *                      the sweep angle. If false the line beginning at the center point is
474      *                      omitted.
475      * @param style         style to apply to the oval.
476      */
477     void drawArc(const GrClip*,
478                  GrPaint&& paint,
479                  GrAA,
480                  const SkMatrix& viewMatrix,
481                  const SkRect& oval,
482                  SkScalar startAngle,
483                  SkScalar sweepAngle,
484                  bool useCenter,
485                  const GrStyle& style);
486 
487     /**
488      * Draw the image as a set of rects, specified by |iter|.
489      */
490     void drawImageLattice(const GrClip*,
491                           GrPaint&&,
492                           const SkMatrix& viewMatrix,
493                           GrSurfaceProxyView,
494                           SkAlphaType alphaType,
495                           sk_sp<GrColorSpaceXform>,
496                           GrSamplerState::Filter,
497                           std::unique_ptr<SkLatticeIter>,
498                           const SkRect& dst);
499 
500     /**
501      * Draw the text specified by the SkGlyphRunList.
502      *
503      * @param viewMatrix      transformationMatrix
504      * @param glyphRunList    text, text positions, and paint.
505      */
506     void drawGlyphRunList(const GrClip*,
507                           const SkMatrixProvider& viewMatrix,
508                           const SkGlyphRunList& glyphRunList,
509                           const SkPaint& paint);
510 
511     /**
512      * Draw the text specified by the SkGlyphRunList.
513      *
514      * @param viewMatrix      transformationMatrix
515      * @param glyphRunList    text, text positions, and paint.
516      */
517     void drawGlyphRunListWithCache(const GrClip*,
518                                    const SkMatrixProvider& viewMatrix,
519                                    const SkGlyphRunList& glyphRunList,
520                                    const SkPaint& paint);
521 
522     /**
523      * Draw the text specified by the SkGlyphRunList.
524      *
525      * @param viewMatrix      transformationMatrix
526      * @param glyphRunList    text, text positions, and paint.
527      */
528     void drawGlyphRunListNoCache(const GrClip*,
529                                  const SkMatrixProvider& viewMatrix,
530                                  const SkGlyphRunList& glyphRunList,
531                                  const SkPaint& paint);
532 
533     /**
534      * Adds the necessary signal and wait semaphores and adds the passed in SkDrawable to the
535      * command stream.
536      */
537     void drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>, const SkRect& bounds);
538 
539     // called to note the last clip drawn to the stencil buffer.
540     // TODO: remove after clipping overhaul.
setLastClip(uint32_t clipStackGenID,const SkIRect & devClipBounds,int numClipAnalyticElements)541     void setLastClip(uint32_t clipStackGenID,
542                      const SkIRect& devClipBounds,
543                      int numClipAnalyticElements) {
544         GrOpsTask* opsTask = this->getOpsTask();
545         opsTask->fLastClipStackGenID = clipStackGenID;
546         opsTask->fLastDevClipBounds = devClipBounds;
547         opsTask->fLastClipNumAnalyticElements = numClipAnalyticElements;
548     }
549 
550     // called to determine if we have to render the clip into SB.
551     // TODO: remove after clipping overhaul.
mustRenderClip(uint32_t clipStackGenID,const SkIRect & devClipBounds,int numClipAnalyticElements)552     bool mustRenderClip(uint32_t clipStackGenID,
553                         const SkIRect& devClipBounds,
554                         int numClipAnalyticElements) {
555         GrOpsTask* opsTask = this->getOpsTask();
556         return opsTask->fLastClipStackGenID != clipStackGenID ||
557                !opsTask->fLastDevClipBounds.contains(devClipBounds) ||
558                opsTask->fLastClipNumAnalyticElements != numClipAnalyticElements;
559     }
560 
clearStencilClip(const SkIRect & scissor,bool insideStencilMask)561     void clearStencilClip(const SkIRect& scissor, bool insideStencilMask) {
562         this->internalStencilClear(&scissor, insideStencilMask);
563     }
564 
565     // While this can take a general clip, since GrReducedClip relies on this function, it must take
566     // care to only provide hard clips or we could get stuck in a loop. The general clip is needed
567     // so that path renderers can use this function.
568     void stencilRect(const GrClip* clip,
569                      const GrUserStencilSettings* ss,
570                      GrPaint&& paint,
571                      GrAA doStencilMSAA,
572                      const SkMatrix& viewMatrix,
573                      const SkRect& rect,
574                      const SkMatrix* localMatrix = nullptr) {
575         // Since this provides stencil settings to drawFilledQuad, it performs a different AA type
576         // resolution compared to regular rect draws, which is the main reason it remains separate.
577         DrawQuad quad{GrQuad::MakeFromRect(rect, viewMatrix),
578                       localMatrix ? GrQuad::MakeFromRect(rect, *localMatrix) : GrQuad(rect),
579                       doStencilMSAA == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone};
580         this->drawFilledQuad(clip, std::move(paint), doStencilMSAA, &quad, ss);
581     }
582 
583     // Fills the user stencil bits with a non-zero value at every sample inside the path. This will
584     // likely be implemented with a Redbook algorithm, but it is not guaranteed. The samples being
585     // rendered to must be zero initially.
586     bool stencilPath(const GrHardClip*,
587                      GrAA doStencilMSAA,
588                      const SkMatrix& viewMatrix,
589                      const SkPath&);
590 
591     /**
592      * Draws a path, either AA or not, and touches the stencil buffer with the user stencil settings
593      * for each color sample written.
594      */
595     bool drawAndStencilPath(const GrHardClip*,
596                             const GrUserStencilSettings*,
597                             SkRegion::Op op,
598                             bool invert,
599                             GrAA doStencilMSAA,
600                             const SkMatrix& viewMatrix,
601                             const SkPath&);
602 
603     SkBudgeted isBudgeted() const;
604 
605     int maxWindowRectangles() const;
606 
glyphRunPainter()607     SkGlyphRunListPainter* glyphRunPainter() { return &fGlyphPainter; }
608 
609     /*
610      * This unique ID will not change for a given RenderTargetContext. However, it is _NOT_
611      * guaranteed to match the uniqueID of the underlying GrRenderTarget - beware!
612      */
uniqueID()613     GrSurfaceProxy::UniqueID uniqueID() const { return this->asSurfaceProxy()->uniqueID(); }
614 
615     // Allows caller of addDrawOp to know which op list an op will be added to.
616     using WillAddOpFn = void(GrOp*, uint32_t opsTaskID);
617     // These perform processing specific to GrDrawOp-derived ops before recording them into an
618     // op list. Before adding the op to an op list the WillAddOpFn is called. Note that it
619     // will not be called in the event that the op is discarded. Moreover, the op may merge into
620     // another op after the function is called (either before addDrawOp returns or some time later).
621     //
622     // If the clip pointer is null, no clipping will be performed.
623     void addDrawOp(const GrClip*,
624                    GrOp::Owner,
625                    const std::function<WillAddOpFn>& = std::function<WillAddOpFn>());
addDrawOp(GrOp::Owner op)626     void addDrawOp(GrOp::Owner op) { this->addDrawOp(nullptr, std::move(op)); }
627 
refsWrappedObjects()628     bool refsWrappedObjects() const { return this->asRenderTargetProxy()->refsWrappedObjects(); }
629 
630     /**
631      *  The next time this GrSurfaceDrawContext is flushed, the gpu will wait on the passed in
632      *  semaphores before executing any commands.
633      */
634     bool waitOnSemaphores(int numSemaphores, const GrBackendSemaphore waitSemaphores[],
635                           bool deleteSemaphoresAfterWait);
636 
numSamples()637     int numSamples() const { return this->asRenderTargetProxy()->numSamples(); }
surfaceProps()638     const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
canUseDynamicMSAA()639     bool canUseDynamicMSAA() const { return fCanUseDynamicMSAA; }
wrapsVkSecondaryCB()640     bool wrapsVkSecondaryCB() const { return this->asRenderTargetProxy()->wrapsVkSecondaryCB(); }
641     GrMipmapped mipmapped() const;
642 
alwaysAntialias()643     bool alwaysAntialias() const {
644         return fSurfaceProps.flags() & kDMSAA_SkSurfacePropsPrivateFlag;
645     }
646 
chooseAA(const SkPaint & paint)647     GrAA chooseAA(const SkPaint& paint) {
648         return GrAA(paint.isAntiAlias() || this->alwaysAntialias());
649     }
650 
651     // This entry point should only be called if the backing GPU object is known to be
652     // instantiated.
accessRenderTarget()653     GrRenderTarget* accessRenderTarget() { return this->asSurfaceProxy()->peekRenderTarget(); }
654 
asRenderTargetContext()655     GrSurfaceDrawContext* asRenderTargetContext() override { return this; }
656 
657 #if GR_TEST_UTILS
testingOnly_SetPreserveOpsOnFullClear()658     void testingOnly_SetPreserveOpsOnFullClear() { fPreserveOpsOnFullClear_TestingOnly = true; }
659 #endif
660 
661 private:
662     enum class QuadOptimization;
663 
664     void willReplaceOpsTask(GrOpsTask* prevTask, GrOpsTask* nextTask) override;
665 
666     GrAAType chooseAAType(GrAA);
667 
668     GrOpsTask::CanDiscardPreviousOps canDiscardPreviousOpsOnFullClear() const override;
669     void setNeedsStencil();
670 
671     void internalStencilClear(const SkIRect* scissor, bool insideStencilMask);
672 
673     // 'stencilSettings' are provided merely for decision making purposes; When non-null,
674     // optimization strategies that submit special ops are avoided.
675     //
676     // 'aa' and 'quad' should be the original draw request on input, and will be updated as
677     // appropriate depending on the returned optimization level.
678     //
679     // If kSubmitted is returned, the provided paint was consumed. Otherwise it is left unchanged.
680     QuadOptimization attemptQuadOptimization(const GrClip* clip,
681                                              const GrUserStencilSettings* stencilSettings,
682                                              GrAA* aa,
683                                              DrawQuad* quad,
684                                              GrPaint* paint);
685 
686     // If stencil settings, 'ss', are non-null, AA controls MSAA or no AA. If they are null, then AA
687     // can choose between coverage, MSAA as per chooseAAType(). This will always attempt to apply
688     // quad optimizations, so all quad/rect public APIs should rely on this function for consistent
689     // clipping behavior. 'quad' will be modified in place to reflect final rendered geometry.
690     void drawFilledQuad(const GrClip* clip,
691                         GrPaint&& paint,
692                         GrAA aa,
693                         DrawQuad* quad,
694                         const GrUserStencilSettings* ss = nullptr);
695 
696     // Like drawFilledQuad but does not require using a GrPaint or FP for texturing.
697     // 'quad' may be modified in place to reflect final geometry.
698     void drawTexturedQuad(const GrClip* clip,
699                           GrSurfaceProxyView proxyView,
700                           SkAlphaType alphaType,
701                           sk_sp<GrColorSpaceXform> textureXform,
702                           GrSamplerState::Filter filter,
703                           GrSamplerState::MipmapMode,
704                           const SkPMColor4f& color,
705                           SkBlendMode blendMode,
706                           GrAA aa,
707                           DrawQuad* quad,
708                           const SkRect* subset = nullptr);
709 
710     void drawStrokedLine(const GrClip*, GrPaint&&, GrAA, const SkMatrix&, const SkPoint[2],
711                          const SkStrokeRec&);
712 
713     // Tries to detect if the given shape is a simple, and draws it without path rendering if
714     // we know how.
715     bool drawSimpleShape(const GrClip*, GrPaint*, GrAA, const SkMatrix&, const GrStyledShape&);
716 
717     // If 'attemptDrawSimple' is true, of if the original shape is marked as having been simplfied,
718     // this will attempt to re-route through drawSimpleShape() to see if we can avoid path rendering
719     // one more time.
720     void drawShapeUsingPathRenderer(const GrClip*, GrPaint&&, GrAA, const SkMatrix&,
721                                     GrStyledShape&&, bool attemptDrawSimple = false);
722 
723     // Makes a copy of the proxy if it is necessary for the draw and places the texture that should
724     // be used by GrXferProcessor to access the destination color in 'result'. If the return
725     // value is false then a texture copy could not be made.
726     //
727     // The op should have already had setClippedBounds called on it.
728     bool SK_WARN_UNUSED_RESULT setupDstProxyView(const GrOp& op,
729                                                  GrXferProcessor::DstProxyView* result);
730 
glyphPainter()731     SkGlyphRunListPainter* glyphPainter() { return &fGlyphPainter; }
732 
733     const SkSurfaceProps fSurfaceProps;
734     const bool fCanUseDynamicMSAA;
735 
736     bool fNeedsStencil = false;
737 
738     GrDstSampleType fDstSampleType = GrDstSampleType::kNone;
739 
740 #if GR_TEST_UTILS
741     bool fPreserveOpsOnFullClear_TestingOnly = false;
742 #endif
743     SkGlyphRunListPainter fGlyphPainter;
744     using INHERITED = GrSurfaceFillContext;
745 };
746 
747 #endif
748