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