• 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 GrRenderTargetContext_DEFINED
9 #define GrRenderTargetContext_DEFINED
10 
11 #include "../private/GrRenderTargetProxy.h"
12 #include "GrContext.h"
13 #include "GrContextPriv.h"
14 #include "GrPaint.h"
15 #include "GrSurfaceContext.h"
16 #include "GrTypesPriv.h"
17 #include "GrXferProcessor.h"
18 #include "SkCanvas.h"
19 #include "SkDrawable.h"
20 #include "SkRefCnt.h"
21 #include "SkSurfaceProps.h"
22 #include "text/GrTextTarget.h"
23 
24 class GrBackendSemaphore;
25 class GrClip;
26 class GrColorSpaceXform;
27 class GrCoverageCountingPathRenderer;
28 class GrDrawingManager;
29 class GrDrawOp;
30 class GrFixedClip;
31 class GrRenderTarget;
32 class GrRenderTargetContextPriv;
33 class GrRenderTargetOpList;
34 class GrShape;
35 class GrStyle;
36 class GrTextureProxy;
37 struct GrUserStencilSettings;
38 struct SkDrawShadowRec;
39 class SkGlyphRunList;
40 struct SkIPoint;
41 struct SkIRect;
42 class SkLatticeIter;
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 SkTextBlob;
52 class SkVertices;
53 
54 /**
55  * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
56  */
57 class SK_API GrRenderTargetContext : public GrSurfaceContext {
58 public:
59     ~GrRenderTargetContext() override;
60 
61     virtual void drawGlyphRunList(const GrClip&, const SkMatrix& viewMatrix, const SkGlyphRunList&);
62 
63     /**
64      * Provides a perfomance hint that the render target's contents are allowed
65      * to become undefined.
66      */
67     void discard();
68 
69     enum class CanClearFullscreen : bool {
70         kNo = false,
71         kYes = true
72     };
73 
74     /**
75      * Clear the entire or rect of the render target, ignoring any clips.
76      * @param rect  the rect to clear or the whole thing if rect is NULL.
77      * @param color the color to clear to.
78      * @param CanClearFullscreen allows partial clears to be converted to fullscreen clears on
79      *                           tiling platforms where that is an optimization.
80      */
81     void clear(const SkIRect* rect, const SkPMColor4f& color, CanClearFullscreen);
82 
83     /**
84      *  Draw everywhere (respecting the clip) with the paint.
85      */
86     void drawPaint(const GrClip&, GrPaint&&, const SkMatrix& viewMatrix);
87 
88     /**
89      * Draw the rect using a paint.
90      * @param paint        describes how to color pixels.
91      * @param GrAA         Controls whether rect is antialiased
92      * @param viewMatrix   transformation matrix
93      * @param style        The style to apply. Null means fill. Currently path effects are not
94      *                     allowed.
95      * The rects coords are used to access the paint (through texture matrix)
96      */
97     void drawRect(const GrClip&,
98                   GrPaint&& paint,
99                   GrAA,
100                   const SkMatrix& viewMatrix,
101                   const SkRect&,
102                   const GrStyle* style = nullptr);
103 
104     /**
105      * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
106      *
107      * @param paint        describes how to color pixels.
108      * @param GrAA         Controls whether rect is antialiased
109      * @param viewMatrix   transformation matrix which applies to rectToDraw
110      * @param rectToDraw   the rectangle to draw
111      * @param localRect    the rectangle of shader coordinates applied to rectToDraw
112      */
113     void fillRectToRect(const GrClip&,
114                         GrPaint&& paint,
115                         GrAA,
116                         const SkMatrix& viewMatrix,
117                         const SkRect& rectToDraw,
118                         const SkRect& localRect);
119 
120     /**
121      * Fills a rect with a paint and a localMatrix.
122      */
123     void fillRectWithLocalMatrix(const GrClip& clip,
124                                  GrPaint&& paint,
125                                  GrAA,
126                                  const SkMatrix& viewMatrix,
127                                  const SkRect& rect,
128                                  const SkMatrix& localMatrix);
129 
130     /**
131      * Creates an op that draws a fill rect with per-edge control over anti-aliasing.
132      */
133     void fillRectWithEdgeAA(const GrClip& clip, GrPaint&& paint, GrQuadAAFlags edgeAA,
134                             const SkMatrix& viewMatrix, const SkRect& rect);
135 
136     /** Used with drawQuadSet */
137     struct QuadSetEntry {
138         SkRect fRect;
139         SkPMColor4f fColor; // Overrides any color on the GrPaint
140         SkMatrix fLocalMatrix;
141         GrQuadAAFlags fAAFlags;
142     };
143 
144     // TODO(michaelludwig) - remove if the bulk API is not useful for SkiaRenderer
145     void drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa, const SkMatrix& viewMatrix,
146                      const QuadSetEntry[], int cnt);
147 
148     /**
149      * Creates an op that draws a subrectangle of a texture. The passed color is modulated by the
150      * texture's color. 'srcRect' specifies the rectangle of the texture to draw. 'dstRect'
151      * specifies the rectangle to draw in local coords which will be transformed by 'viewMatrix' to
152      * device space.
153      */
154     void drawTexture(const GrClip& clip, sk_sp<GrTextureProxy>, GrSamplerState::Filter,
155                      const SkPMColor4f&, const SkRect& srcRect, const SkRect& dstRect,
156                      GrQuadAAFlags, SkCanvas::SrcRectConstraint, const SkMatrix& viewMatrix,
157                      sk_sp<GrColorSpaceXform> texXform);
158 
159     /** Used with drawTextureSet */
160     struct TextureSetEntry {
161         sk_sp<GrTextureProxy> fProxy;
162         SkRect fSrcRect;
163         SkRect fDstRect;
164         float fAlpha;
165         GrQuadAAFlags fAAFlags;
166     };
167     /**
168      * Draws a set of textures with a shared filter, color, view matrix, color xform, and
169      * texture color xform. The textures must all have the same GrTextureType and GrConfig.
170      */
171     void drawTextureSet(const GrClip&, const TextureSetEntry[], int cnt, GrSamplerState::Filter,
172                         SkBlendMode mode, const SkMatrix& viewMatrix,
173                         sk_sp<GrColorSpaceXform> texXform);
174 
175     /**
176      * Draw a roundrect using a paint.
177      *
178      * @param paint       describes how to color pixels.
179      * @param GrAA        Controls whether rrect is antialiased.
180      * @param viewMatrix  transformation matrix
181      * @param rrect       the roundrect to draw
182      * @param style       style to apply to the rrect. Currently path effects are not allowed.
183      */
184     void drawRRect(const GrClip&,
185                    GrPaint&&,
186                    GrAA,
187                    const SkMatrix& viewMatrix,
188                    const SkRRect& rrect,
189                    const GrStyle& style);
190 
191     /**
192      * Use a fast method to render the ambient and spot shadows for a path.
193      * Will return false if not possible for the given path.
194      *
195      * @param viewMatrix   transformation matrix
196      * @param path         the path to shadow
197      * @param rec          parameters for shadow rendering
198      */
199     bool drawFastShadow(const GrClip&,
200                         const SkMatrix& viewMatrix,
201                         const SkPath& path,
202                         const SkDrawShadowRec& rec);
203 
204     /**
205      * Shortcut for filling a SkPath consisting of nested rrects using a paint. The result is
206      * undefined if outer does not contain inner.
207      *
208      * @param paint        describes how to color pixels.
209      * @param GrAA         Controls whether rrects edges are antialiased
210      * @param viewMatrix   transformation matrix
211      * @param outer        the outer roundrect
212      * @param inner        the inner roundrect
213      */
214     void drawDRRect(const GrClip&,
215                     GrPaint&&,
216                     GrAA,
217                     const SkMatrix& viewMatrix,
218                     const SkRRect& outer,
219                     const SkRRect& inner);
220 
221     /**
222      * Draws a path.
223      *
224      * @param paint         describes how to color pixels.
225      * @param GrAA          Controls whether the path is antialiased.
226      * @param viewMatrix    transformation matrix
227      * @param path          the path to draw
228      * @param style         style to apply to the path.
229      */
230     void drawPath(const GrClip&,
231                   GrPaint&&,
232                   GrAA,
233                   const SkMatrix& viewMatrix,
234                   const SkPath&,
235                   const GrStyle&);
236 
237     /**
238      * Draws a shape.
239      *
240      * @param paint         describes how to color pixels.
241      * @param GrAA          Controls whether the path is antialiased.
242      * @param viewMatrix    transformation matrix
243      * @param shape         the shape to draw
244      */
245     void drawShape(const GrClip&,
246                    GrPaint&&,
247                    GrAA,
248                    const SkMatrix& viewMatrix,
249                    const GrShape&);
250 
251 
252     /**
253      * Draws vertices with a paint.
254      *
255      * @param   paint            describes how to color pixels.
256      * @param   viewMatrix       transformation matrix
257      * @param   vertices         specifies the mesh to draw.
258      * @param   bones            bone deformation matrices.
259      * @param   boneCount        number of bone matrices.
260      * @param   overridePrimType primitive type to draw. If NULL, derive prim type from vertices.
261      */
262     void drawVertices(const GrClip&,
263                       GrPaint&& paint,
264                       const SkMatrix& viewMatrix,
265                       sk_sp<SkVertices> vertices,
266                       const SkVertices::Bone bones[],
267                       int boneCount,
268                       GrPrimitiveType* overridePrimType = nullptr);
269 
270     /**
271      * Draws textured sprites from an atlas with a paint. This currently does not support AA for the
272      * sprite rectangle edges.
273      *
274      * @param   paint           describes how to color pixels.
275      * @param   viewMatrix      transformation matrix
276      * @param   spriteCount     number of sprites.
277      * @param   xform           array of compressed transformation data, required.
278      * @param   texRect         array of texture rectangles used to access the paint.
279      * @param   colors          optional array of per-sprite colors, supercedes
280      *                          the paint's color field.
281      */
282     void drawAtlas(const GrClip&,
283                    GrPaint&& paint,
284                    const SkMatrix& viewMatrix,
285                    int spriteCount,
286                    const SkRSXform xform[],
287                    const SkRect texRect[],
288                    const SkColor colors[]);
289 
290     /**
291      * Draws a region.
292      *
293      * @param paint         describes how to color pixels
294      * @param viewMatrix    transformation matrix
295      * @param aa            should the rects of the region be antialiased.
296      * @param region        the region to be drawn
297      * @param style         style to apply to the region
298      */
299     void drawRegion(const GrClip&,
300                     GrPaint&& paint,
301                     GrAA aa,
302                     const SkMatrix& viewMatrix,
303                     const SkRegion& region,
304                     const GrStyle& style,
305                     const GrUserStencilSettings* ss = nullptr);
306 
307     /**
308      * Draws an oval.
309      *
310      * @param paint         describes how to color pixels.
311      * @param GrAA          Controls whether the oval is antialiased.
312      * @param viewMatrix    transformation matrix
313      * @param oval          the bounding rect of the oval.
314      * @param style         style to apply to the oval. Currently path effects are not allowed.
315      */
316     void drawOval(const GrClip&,
317                   GrPaint&& paint,
318                   GrAA,
319                   const SkMatrix& viewMatrix,
320                   const SkRect& oval,
321                   const GrStyle& style);
322     /**
323      * Draws a partial arc of an oval.
324      *
325      * @param paint         describes how to color pixels.
326      * @param GrGrAA        Controls whether the arc is antialiased.
327      * @param viewMatrix    transformation matrix.
328      * @param oval          the bounding rect of the oval.
329      * @param startAngle    starting angle in degrees.
330      * @param sweepAngle    angle to sweep in degrees. Must be in (-360, 360)
331      * @param useCenter     true means that the implied path begins at the oval center, connects as
332      *                      a line to the point indicated by the start contains the arc indicated by
333      *                      the sweep angle. If false the line beginning at the center point is
334      *                      omitted.
335      * @param style         style to apply to the oval.
336      */
337     void drawArc(const GrClip&,
338                  GrPaint&& paint,
339                  GrAA,
340                  const SkMatrix& viewMatrix,
341                  const SkRect& oval,
342                  SkScalar startAngle,
343                  SkScalar sweepAngle,
344                  bool useCenter,
345                  const GrStyle& style);
346 
347     /**
348      * Draw the image as a set of rects, specified by |iter|.
349      */
350     void drawImageLattice(const GrClip&,
351                           GrPaint&&,
352                           const SkMatrix& viewMatrix,
353                           sk_sp<GrTextureProxy>,
354                           sk_sp<GrColorSpaceXform>,
355                           GrSamplerState::Filter,
356                           std::unique_ptr<SkLatticeIter>,
357                           const SkRect& dst);
358 
359     /**
360      * Adds the necessary signal and wait semaphores and adds the passed in SkDrawable to the
361      * command stream.
362      */
363     void drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler>, const SkRect& bounds);
364 
365     /**
366      * After this returns any pending surface IO will be issued to the backend 3D API and
367      * if the surface has MSAA it will be resolved.
368      */
369     GrSemaphoresSubmitted prepareForExternalIO(int numSemaphores,
370                                                GrBackendSemaphore backendSemaphores[]);
371 
372     /**
373      *  The next time this GrRenderTargetContext is flushed, the gpu will wait on the passed in
374      *  semaphores before executing any commands.
375      */
376     bool waitOnSemaphores(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
377 
378     void insertEventMarker(const SkString&);
379 
fsaaType()380     GrFSAAType fsaaType() const { return fRenderTargetProxy->fsaaType(); }
caps()381     const GrCaps* caps() const { return fContext->contextPriv().caps(); }
width()382     int width() const { return fRenderTargetProxy->width(); }
height()383     int height() const { return fRenderTargetProxy->height(); }
numColorSamples()384     int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); }
numStencilSamples()385     int numStencilSamples() const { return fRenderTargetProxy->numStencilSamples(); }
surfaceProps()386     const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
origin()387     GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); }
wrapsVkSecondaryCB()388     bool wrapsVkSecondaryCB() const { return fRenderTargetProxy->wrapsVkSecondaryCB(); }
389     GrMipMapped mipMapped() const;
390 
391     bool wasAbandoned() const;
392 
setNeedsStencil()393     void setNeedsStencil() { fRenderTargetProxy->setNeedsStencil(); }
394 
accessRenderTarget()395     GrRenderTarget* accessRenderTarget() {
396         // TODO: usage of this entry point needs to be reduced and potentially eliminated
397         // since it ends the deferral of the GrRenderTarget's allocation
398         if (!fRenderTargetProxy->instantiate(fContext->contextPriv().resourceProvider())) {
399             return nullptr;
400         }
401         return fRenderTargetProxy->peekRenderTarget();
402     }
403 
asSurfaceProxy()404     GrSurfaceProxy* asSurfaceProxy() override { return fRenderTargetProxy.get(); }
asSurfaceProxy()405     const GrSurfaceProxy* asSurfaceProxy() const override { return fRenderTargetProxy.get(); }
asSurfaceProxyRef()406     sk_sp<GrSurfaceProxy> asSurfaceProxyRef() override { return fRenderTargetProxy; }
407 
408     GrTextureProxy* asTextureProxy() override;
409     const GrTextureProxy* asTextureProxy() const override;
410     sk_sp<GrTextureProxy> asTextureProxyRef() override;
411 
asRenderTargetProxy()412     GrRenderTargetProxy* asRenderTargetProxy() override { return fRenderTargetProxy.get(); }
asRenderTargetProxyRef()413     sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() override { return fRenderTargetProxy; }
414 
asRenderTargetContext()415     GrRenderTargetContext* asRenderTargetContext() override { return this; }
416 
417     // Provides access to functions that aren't part of the public API.
418     GrRenderTargetContextPriv priv();
419     const GrRenderTargetContextPriv priv() const;
420 
textTarget()421     GrTextTarget* textTarget() { return fTextTarget.get(); }
422 
423     bool isWrapped_ForTesting() const;
424 
425 protected:
426     GrRenderTargetContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTargetProxy>,
427                           sk_sp<SkColorSpace>, const SkSurfaceProps*, GrAuditTrail*,
428                           GrSingleOwner*, bool managedOpList = true);
429 
430     SkDEBUGCODE(void validate() const override;)
431 
432 private:
433     class TextTarget;
434 
chooseAAType(GrAA aa,GrAllowMixedSamples allowMixedSamples)435     inline GrAAType chooseAAType(GrAA aa, GrAllowMixedSamples allowMixedSamples) {
436         return GrChooseAAType(aa, this->fsaaType(), allowMixedSamples, *this->caps());
437     }
438 
439     friend class GrAtlasTextBlob;               // for access to add[Mesh]DrawOp
440     friend class GrClipStackClip;               // for access to getOpList
441 
442     friend class GrDrawingManager; // for ctor
443     friend class GrRenderTargetContextPriv;
444 
445     // All the path renderers currently make their own ops
446     friend class GrSoftwarePathRenderer;             // for access to add[Mesh]DrawOp
447     friend class GrAAConvexPathRenderer;             // for access to add[Mesh]DrawOp
448     friend class GrDashLinePathRenderer;             // for access to add[Mesh]DrawOp
449     friend class GrAAHairLinePathRenderer;           // for access to add[Mesh]DrawOp
450     friend class GrAALinearizingConvexPathRenderer;  // for access to add[Mesh]DrawOp
451     friend class GrSmallPathRenderer;                // for access to add[Mesh]DrawOp
452     friend class GrDefaultPathRenderer;              // for access to add[Mesh]DrawOp
453     friend class GrStencilAndCoverPathRenderer;      // for access to add[Mesh]DrawOp
454     friend class GrTessellatingPathRenderer;         // for access to add[Mesh]DrawOp
455     friend class GrCCPerFlushResources;              // for access to addDrawOp
456     friend class GrCoverageCountingPathRenderer;     // for access to addDrawOp
457     // for a unit test
458     friend void test_draw_op(GrContext*,
459                              GrRenderTargetContext*,
460                              std::unique_ptr<GrFragmentProcessor>,
461                              sk_sp<GrTextureProxy>);
462 
463     void internalClear(const GrFixedClip&, const SkPMColor4f&, CanClearFullscreen);
464     void internalStencilClear(const GrFixedClip&, bool insideStencilMask);
465 
466     // Only consumes the GrPaint if successful.
467     bool drawFilledDRRect(const GrClip& clip,
468                           GrPaint&& paint,
469                           GrAA,
470                           const SkMatrix& viewMatrix,
471                           const SkRRect& origOuter,
472                           const SkRRect& origInner);
473 
474     void drawFilledRect(const GrClip& clip,
475                         GrPaint&& paint,
476                         GrAA,
477                         const SkMatrix& viewMatrix,
478                         const SkRect& rect,
479                         const GrUserStencilSettings* ss = nullptr);
480 
481     // Only consumes the GrPaint if successful.
482     bool drawFilledRectAsClear(const GrClip& clip,
483                                GrPaint&& paint,
484                                GrAA aa,
485                                const SkMatrix& viewMatrix,
486                                const SkRect& rect);
487 
488     void drawShapeUsingPathRenderer(const GrClip&, GrPaint&&, GrAA, const SkMatrix&,
489                                     const GrShape&);
490 
491     // Allows caller of addDrawOp to know which op list an op will be added to.
492     using WillAddOpFn = void(GrOp*, uint32_t opListID);
493     // These perform processing specific to GrDrawOp-derived ops before recording them into an
494     // op list. Before adding the op to an op list the WillAddOpFn is called. Note that it
495     // will not be called in the event that the op is discarded. Moreover, the op may merge into
496     // another op after the function is called (either before addDrawOp returns or some time later).
497     void addDrawOp(const GrClip&, std::unique_ptr<GrDrawOp>,
498                    const std::function<WillAddOpFn>& = std::function<WillAddOpFn>());
499 
500     // Makes a copy of the proxy if it is necessary for the draw and places the texture that should
501     // be used by GrXferProcessor to access the destination color in 'result'. If the return
502     // value is false then a texture copy could not be made.
503     bool SK_WARN_UNUSED_RESULT setupDstProxy(GrRenderTargetProxy*,
504                                              const GrClip&,
505                                              const GrOp& op,
506                                              GrXferProcessor::DstProxy* result);
507 
508     GrRenderTargetOpList* getRTOpList();
509     GrOpList* getOpList() override;
510 
511     std::unique_ptr<GrTextTarget> fTextTarget;
512     sk_sp<GrRenderTargetProxy> fRenderTargetProxy;
513 
514     // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
515     // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
516     sk_sp<GrRenderTargetOpList> fOpList;
517 
518     SkSurfaceProps fSurfaceProps;
519     bool fManagedOpList;
520 
521     typedef GrSurfaceContext INHERITED;
522 };
523 
524 #endif
525