GrRenderTargetContext.h revision aff329b8e9b239bca1d93b13a914fbef45ccf7fe
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/GrInstancedPipelineInfo.h"
12#include "../private/GrRenderTargetProxy.h"
13#include "GrColor.h"
14#include "GrContext.h"
15#include "GrPaint.h"
16#include "GrSurfaceContext.h"
17#include "GrTypesPriv.h"
18#include "GrXferProcessor.h"
19#include "SkRefCnt.h"
20#include "SkSurfaceProps.h"
21
22class GrBackendSemaphore;
23class GrCCPRAtlas;
24class GrClip;
25class GrCoverageCountingPathRenderer;
26class GrDrawingManager;
27class GrDrawOp;
28class GrFixedClip;
29class GrRenderTarget;
30class GrRenderTargetContextPriv;
31class GrRenderTargetOpList;
32class GrStyle;
33class GrTextureProxy;
34struct GrUserStencilSettings;
35class SkDrawFilter;
36struct SkDrawShadowRec;
37struct SkIPoint;
38struct SkIRect;
39class SkLatticeIter;
40class SkMatrix;
41class SkPaint;
42class SkPath;
43struct SkPoint;
44struct SkRect;
45class SkRegion;
46class SkRRect;
47struct SkRSXform;
48class SkTextBlob;
49class SkVertices;
50
51/**
52 * A helper object to orchestrate commands (draws, etc...) for GrSurfaces that are GrRenderTargets.
53 */
54class SK_API GrRenderTargetContext : public GrSurfaceContext {
55public:
56    ~GrRenderTargetContext() override;
57
58    // We use SkPaint rather than GrPaint here for two reasons:
59    //    * The SkPaint carries extra text settings. If these were extracted to a lighter object
60    //      we could use GrPaint except that
61    //    * SkPaint->GrPaint conversion depends upon whether the glyphs are color or grayscale and
62    //      this can vary within a text run.
63    virtual void drawText(const GrClip&, const SkPaint&, const SkMatrix& viewMatrix,
64                          const char text[], size_t byteLength, SkScalar x, SkScalar y,
65                          const SkIRect& clipBounds);
66    virtual void drawPosText(const GrClip&, const SkPaint&, const SkMatrix& viewMatrix,
67                             const char text[], size_t byteLength, const SkScalar pos[],
68                             int scalarsPerPosition, const SkPoint& offset,
69                             const SkIRect& clipBounds);
70    virtual void drawTextBlob(const GrClip&, const SkPaint&,
71                              const SkMatrix& viewMatrix, const SkTextBlob*,
72                              SkScalar x, SkScalar y,
73                              SkDrawFilter*, const SkIRect& clipBounds);
74
75    /**
76     * Provides a perfomance hint that the render target's contents are allowed
77     * to become undefined.
78     */
79    void discard();
80
81    /**
82     * Clear the entire or rect of the render target, ignoring any clips.
83     * @param rect  the rect to clear or the whole thing if rect is NULL.
84     * @param color the color to clear to.
85     * @param canIgnoreRect allows partial clears to be converted to whole
86     *                      clears on platforms for which that is cheap
87     */
88    void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
89
90    /**
91     *  Draw everywhere (respecting the clip) with the paint.
92     */
93    void drawPaint(const GrClip&, GrPaint&&, const SkMatrix& viewMatrix);
94
95    /**
96     * Draw the rect using a paint.
97     * @param paint        describes how to color pixels.
98     * @param GrAA         Controls whether rect is antialiased
99     * @param viewMatrix   transformation matrix
100     * @param style        The style to apply. Null means fill. Currently path effects are not
101     *                     allowed.
102     * The rects coords are used to access the paint (through texture matrix)
103     */
104    void drawRect(const GrClip&,
105                  GrPaint&& paint,
106                  GrAA,
107                  const SkMatrix& viewMatrix,
108                  const SkRect&,
109                  const GrStyle* style = nullptr);
110
111    /**
112     * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
113     *
114     * @param paint        describes how to color pixels.
115     * @param GrAA         Controls whether rect is antialiased
116     * @param viewMatrix   transformation matrix which applies to rectToDraw
117     * @param rectToDraw   the rectangle to draw
118     * @param localRect    the rectangle of shader coordinates applied to rectToDraw
119     */
120    void fillRectToRect(const GrClip&,
121                        GrPaint&& paint,
122                        GrAA,
123                        const SkMatrix& viewMatrix,
124                        const SkRect& rectToDraw,
125                        const SkRect& localRect);
126
127    /**
128     * Fills a rect with a paint and a localMatrix.
129     */
130    void fillRectWithLocalMatrix(const GrClip& clip,
131                                 GrPaint&& paint,
132                                 GrAA,
133                                 const SkMatrix& viewMatrix,
134                                 const SkRect& rect,
135                                 const SkMatrix& localMatrix);
136
137    /**
138     * Draw a roundrect using a paint.
139     *
140     * @param paint       describes how to color pixels.
141     * @param GrAA        Controls whether rrect is antialiased.
142     * @param viewMatrix  transformation matrix
143     * @param rrect       the roundrect to draw
144     * @param style       style to apply to the rrect. Currently path effects are not allowed.
145     */
146    void drawRRect(const GrClip&,
147                   GrPaint&&,
148                   GrAA,
149                   const SkMatrix& viewMatrix,
150                   const SkRRect& rrect,
151                   const GrStyle& style);
152
153    /**
154     * Use a fast method to render the ambient and spot shadows for a path.
155     * Will return false if not possible for the given path.
156     *
157     * @param color        shadow color.
158     * @param viewMatrix   transformation matrix
159     * @param path         the path to shadow
160     * @param rec          parameters for shadow rendering
161     */
162    bool drawFastShadow(const GrClip&,
163                        GrColor color,
164                        const SkMatrix& viewMatrix,
165                        const SkPath& path,
166                        const SkDrawShadowRec& rec);
167
168    /**
169     * Shortcut for filling a SkPath consisting of nested rrects using a paint. The result is
170     * undefined if outer does not contain inner.
171     *
172     * @param paint        describes how to color pixels.
173     * @param GrAA         Controls whether rrects edges are antialiased
174     * @param viewMatrix   transformation matrix
175     * @param outer        the outer roundrect
176     * @param inner        the inner roundrect
177     */
178    void drawDRRect(const GrClip&,
179                    GrPaint&&,
180                    GrAA,
181                    const SkMatrix& viewMatrix,
182                    const SkRRect& outer,
183                    const SkRRect& inner);
184
185    /**
186     * Draws a path.
187     *
188     * @param paint         describes how to color pixels.
189     * @param GrAA          Controls whether the path is antialiased.
190     * @param viewMatrix    transformation matrix
191     * @param path          the path to draw
192     * @param style         style to apply to the path.
193     */
194    void drawPath(const GrClip&,
195                  GrPaint&&,
196                  GrAA,
197                  const SkMatrix& viewMatrix,
198                  const SkPath&,
199                  const GrStyle& style);
200
201    /**
202     * Draws vertices with a paint.
203     *
204     * @param   paint            describes how to color pixels.
205     * @param   viewMatrix       transformation matrix
206     * @param   vertices         specifies the mesh to draw.
207     * @param   overridePrimType primitive type to draw. If NULL, derive prim type from vertices.
208     */
209    void drawVertices(const GrClip&,
210                      GrPaint&& paint,
211                      const SkMatrix& viewMatrix,
212                      sk_sp<SkVertices> vertices,
213                      GrPrimitiveType* overridePrimType = nullptr);
214
215    /**
216     * Draws textured sprites from an atlas with a paint. This currently does not support AA for the
217     * sprite rectangle edges.
218     *
219     * @param   paint           describes how to color pixels.
220     * @param   viewMatrix      transformation matrix
221     * @param   spriteCount     number of sprites.
222     * @param   xform           array of compressed transformation data, required.
223     * @param   texRect         array of texture rectangles used to access the paint.
224     * @param   colors          optional array of per-sprite colors, supercedes
225     *                          the paint's color field.
226     */
227    void drawAtlas(const GrClip&,
228                   GrPaint&& paint,
229                   const SkMatrix& viewMatrix,
230                   int spriteCount,
231                   const SkRSXform xform[],
232                   const SkRect texRect[],
233                   const SkColor colors[]);
234
235    /**
236     * Draws a region.
237     *
238     * @param paint         describes how to color pixels
239     * @param viewMatrix    transformation matrix
240     * @param aa            should the rects of the region be antialiased.
241     * @param region        the region to be drawn
242     * @param style         style to apply to the region
243     */
244    void drawRegion(const GrClip&,
245                    GrPaint&& paint,
246                    GrAA aa,
247                    const SkMatrix& viewMatrix,
248                    const SkRegion& region,
249                    const GrStyle& style,
250                    const GrUserStencilSettings* ss = nullptr);
251
252    /**
253     * Draws an oval.
254     *
255     * @param paint         describes how to color pixels.
256     * @param GrAA          Controls whether the oval is antialiased.
257     * @param viewMatrix    transformation matrix
258     * @param oval          the bounding rect of the oval.
259     * @param style         style to apply to the oval. Currently path effects are not allowed.
260     */
261    void drawOval(const GrClip&,
262                  GrPaint&& paint,
263                  GrAA,
264                  const SkMatrix& viewMatrix,
265                  const SkRect& oval,
266                  const GrStyle& style);
267    /**
268     * Draws a partial arc of an oval.
269     *
270     * @param paint         describes how to color pixels.
271     * @param GrGrAA        Controls whether the arc is antialiased.
272     * @param viewMatrix    transformation matrix.
273     * @param oval          the bounding rect of the oval.
274     * @param startAngle    starting angle in degrees.
275     * @param sweepAngle    angle to sweep in degrees. Must be in (-360, 360)
276     * @param useCenter     true means that the implied path begins at the oval center, connects as
277     *                      a line to the point indicated by the start contains the arc indicated by
278     *                      the sweep angle. If false the line beginning at the center point is
279     *                      omitted.
280     * @param style         style to apply to the oval.
281     */
282    void drawArc(const GrClip&,
283                 GrPaint&& paint,
284                 GrAA,
285                 const SkMatrix& viewMatrix,
286                 const SkRect& oval,
287                 SkScalar startAngle,
288                 SkScalar sweepAngle,
289                 bool useCenter,
290                 const GrStyle& style);
291
292    /**
293     * Draw the image as a set of rects, specified by |iter|.
294     */
295    void drawImageLattice(const GrClip&,
296                          GrPaint&& paint,
297                          const SkMatrix& viewMatrix,
298                          int imageWidth,
299                          int imageHeight,
300                          std::unique_ptr<SkLatticeIter> iter,
301                          const SkRect& dst);
302
303    /**
304     * After this returns any pending surface IO will be issued to the backend 3D API and
305     * if the surface has MSAA it will be resolved.
306     */
307    GrSemaphoresSubmitted prepareForExternalIO(int numSemaphores,
308                                               GrBackendSemaphore backendSemaphores[]);
309
310    /**
311     *  The next time this GrRenderTargetContext is flushed, the gpu will wait on the passed in
312     *  semaphores before executing any commands.
313     */
314    bool waitOnSemaphores(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
315
316    void insertEventMarker(const SkString&);
317
318    GrFSAAType fsaaType() const { return fRenderTargetProxy->fsaaType(); }
319    const GrCaps* caps() const { return fContext->caps(); }
320    int width() const { return fRenderTargetProxy->width(); }
321    int height() const { return fRenderTargetProxy->height(); }
322    GrPixelConfig config() const { return fRenderTargetProxy->config(); }
323    int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); }
324    int numStencilSamples() const { return fRenderTargetProxy->numStencilSamples(); }
325    const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
326    GrColorSpaceXform* getColorXformFromSRGB() const { return fColorXformFromSRGB.get(); }
327    GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); }
328
329    bool wasAbandoned() const;
330
331    void setNeedsStencil() { fRenderTargetProxy->setNeedsStencil(); }
332
333    GrRenderTarget* accessRenderTarget() {
334        // TODO: usage of this entry point needs to be reduced and potentially eliminated
335        // since it ends the deferral of the GrRenderTarget's allocation
336        if (!fRenderTargetProxy->instantiate(fContext->resourceProvider())) {
337            return nullptr;
338        }
339        return fRenderTargetProxy->priv().peekRenderTarget();
340    }
341
342    GrSurfaceProxy* asSurfaceProxy() override { return fRenderTargetProxy.get(); }
343    const GrSurfaceProxy* asSurfaceProxy() const override { return fRenderTargetProxy.get(); }
344    sk_sp<GrSurfaceProxy> asSurfaceProxyRef() override { return fRenderTargetProxy; }
345
346    GrTextureProxy* asTextureProxy() override;
347    sk_sp<GrTextureProxy> asTextureProxyRef() override;
348
349    GrRenderTargetProxy* asRenderTargetProxy() override { return fRenderTargetProxy.get(); }
350    sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() override { return fRenderTargetProxy; }
351
352    GrRenderTargetContext* asRenderTargetContext() override { return this; }
353
354    // Provides access to functions that aren't part of the public API.
355    GrRenderTargetContextPriv priv();
356    const GrRenderTargetContextPriv priv() const;
357
358    bool isWrapped_ForTesting() const;
359
360protected:
361    GrRenderTargetContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTargetProxy>,
362                          sk_sp<SkColorSpace>, const SkSurfaceProps*, GrAuditTrail*,
363                          GrSingleOwner*, bool managedOpList = true);
364
365    SkDEBUGCODE(void validate() const override;)
366
367private:
368    inline GrAAType chooseAAType(GrAA aa, GrAllowMixedSamples allowMixedSamples) {
369        return GrChooseAAType(aa, this->fsaaType(), allowMixedSamples, *this->caps());
370    }
371
372    friend class GrAtlasTextBlob;               // for access to add[Mesh]DrawOp
373    friend class GrStencilAndCoverTextContext;  // for access to add[Mesh]DrawOp
374
375    friend class GrDrawingManager; // for ctor
376    friend class GrRenderTargetContextPriv;
377    friend class GrSWMaskHelper;  // for access to add[Mesh]DrawOp
378
379    // All the path renderers currently make their own ops
380    friend class GrSoftwarePathRenderer;             // for access to add[Mesh]DrawOp
381    friend class GrAAConvexPathRenderer;             // for access to add[Mesh]DrawOp
382    friend class GrDashLinePathRenderer;             // for access to add[Mesh]DrawOp
383    friend class GrAAHairLinePathRenderer;           // for access to add[Mesh]DrawOp
384    friend class GrAALinearizingConvexPathRenderer;  // for access to add[Mesh]DrawOp
385    friend class GrSmallPathRenderer;                // for access to add[Mesh]DrawOp
386    friend class GrDefaultPathRenderer;              // for access to add[Mesh]DrawOp
387    friend class GrMSAAPathRenderer;                 // for access to add[Mesh]DrawOp
388    friend class GrStencilAndCoverPathRenderer;      // for access to add[Mesh]DrawOp
389    friend class GrTessellatingPathRenderer;         // for access to add[Mesh]DrawOp
390    friend class GrCCPRAtlas;                        // for access to addDrawOp
391    friend class GrCoverageCountingPathRenderer;     // for access to addDrawOp
392    // for a unit test
393    friend void test_draw_op(GrRenderTargetContext*, std::unique_ptr<GrFragmentProcessor>,
394                             sk_sp<GrTextureProxy>);
395
396    void internalClear(const GrFixedClip&, const GrColor, bool canIgnoreClip);
397
398    // Only consumes the GrPaint if successful.
399    bool drawFilledDRRect(const GrClip& clip,
400                          GrPaint&& paint,
401                          GrAA,
402                          const SkMatrix& viewMatrix,
403                          const SkRRect& origOuter,
404                          const SkRRect& origInner);
405
406    // Only consumes the GrPaint if successful.
407    bool drawFilledRect(const GrClip& clip,
408                        GrPaint&& paint,
409                        GrAA,
410                        const SkMatrix& viewMatrix,
411                        const SkRect& rect,
412                        const GrUserStencilSettings* ss);
413
414    void internalDrawPath(
415            const GrClip&, GrPaint&&, GrAA, const SkMatrix&, const SkPath&, const GrStyle&);
416
417    // These perform processing specific to Gr[Mesh]DrawOp-derived ops before recording them into
418    // the op list. They return the id of the opList to which the op was added, or 0, if it was
419    // dropped (e.g., due to clipping).
420    uint32_t addDrawOp(const GrClip&, std::unique_ptr<GrDrawOp>);
421
422    // Makes a copy of the proxy if it is necessary for the draw and places the texture that should
423    // be used by GrXferProcessor to access the destination color in 'result'. If the return
424    // value is false then a texture copy could not be made.
425    bool SK_WARN_UNUSED_RESULT setupDstProxy(GrRenderTargetProxy*,
426                                             const GrClip&,
427                                             const SkRect& opBounds,
428                                             GrXferProcessor::DstProxy* result);
429
430    GrRenderTargetOpList* getRTOpList();
431    GrOpList* getOpList() override;
432
433    sk_sp<GrRenderTargetProxy>        fRenderTargetProxy;
434
435    // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
436    // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
437    sk_sp<GrRenderTargetOpList>       fOpList;
438    GrInstancedPipelineInfo           fInstancedPipelineInfo;
439
440    sk_sp<GrColorSpaceXform>          fColorXformFromSRGB;
441    SkSurfaceProps                    fSurfaceProps;
442    bool                              fManagedOpList;
443
444    typedef GrSurfaceContext INHERITED;
445};
446
447#endif
448