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