GrRenderTargetContext.h revision 2de8cfadc34cd92a6f99659fa565c137b386fa5f
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 GrClip;
24class GrDrawingManager;
25class GrDrawOp;
26class GrFixedClip;
27class GrLegacyMeshDrawOp;
28class GrPipelineBuilder;
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 paint        describes how to color pixels.
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                        GrPaint&&,
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
251    /**
252     * Draws an oval.
253     *
254     * @param paint         describes how to color pixels.
255     * @param GrAA          Controls whether the oval is antialiased.
256     * @param viewMatrix    transformation matrix
257     * @param oval          the bounding rect of the oval.
258     * @param style         style to apply to the oval. Currently path effects are not allowed.
259     */
260    void drawOval(const GrClip&,
261                  GrPaint&& paint,
262                  GrAA,
263                  const SkMatrix& viewMatrix,
264                  const SkRect& oval,
265                  const GrStyle& style);
266    /**
267     * Draws a partial arc of an oval.
268     *
269     * @param paint         describes how to color pixels.
270     * @param GrGrAA        Controls whether the arc is antialiased.
271     * @param viewMatrix    transformation matrix.
272     * @param oval          the bounding rect of the oval.
273     * @param startAngle    starting angle in degrees.
274     * @param sweepAngle    angle to sweep in degrees. Must be in (-360, 360)
275     * @param useCenter     true means that the implied path begins at the oval center, connects as
276     *                      a line to the point indicated by the start contains the arc indicated by
277     *                      the sweep angle. If false the line beginning at the center point is
278     *                      omitted.
279     * @param style         style to apply to the oval.
280     */
281    void drawArc(const GrClip&,
282                 GrPaint&& paint,
283                 GrAA,
284                 const SkMatrix& viewMatrix,
285                 const SkRect& oval,
286                 SkScalar startAngle,
287                 SkScalar sweepAngle,
288                 bool useCenter,
289                 const GrStyle& style);
290
291    /**
292     * Draw the image as a set of rects, specified by |iter|.
293     */
294    void drawImageLattice(const GrClip&,
295                          GrPaint&& paint,
296                          const SkMatrix& viewMatrix,
297                          int imageWidth,
298                          int imageHeight,
299                          std::unique_ptr<SkLatticeIter> iter,
300                          const SkRect& dst);
301
302    /**
303     * After this returns any pending surface IO will be issued to the backend 3D API and
304     * if the surface has MSAA it will be resolved.
305     */
306    bool prepareForExternalIO(int numSemaphores, GrBackendSemaphore* backendSemaphores);
307
308    /**
309     *  The next time this GrRenderTargetContext is flushed, the gpu will wait on the passed in
310     *  semaphores before executing any commands.
311     */
312    bool waitOnSemaphores(int numSemaphores, const GrBackendSemaphore* waitSemaphores);
313
314    GrFSAAType fsaaType() const { return fRenderTargetProxy->fsaaType(); }
315    const GrCaps* caps() const { return fContext->caps(); }
316    int width() const { return fRenderTargetProxy->width(); }
317    int height() const { return fRenderTargetProxy->height(); }
318    GrPixelConfig config() const { return fRenderTargetProxy->config(); }
319    int numColorSamples() const { return fRenderTargetProxy->numColorSamples(); }
320    int numStencilSamples() const { return fRenderTargetProxy->numStencilSamples(); }
321    const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
322    GrColorSpaceXform* getColorXformFromSRGB() const { return fColorXformFromSRGB.get(); }
323    GrSurfaceOrigin origin() const { return fRenderTargetProxy->origin(); }
324
325    bool wasAbandoned() const;
326
327    GrRenderTarget* accessRenderTarget() {
328        // TODO: usage of this entry point needs to be reduced and potentially eliminated
329        // since it ends the deferral of the GrRenderTarget's allocation
330        if (!fRenderTargetProxy->instantiate(fContext->resourceProvider())) {
331            return nullptr;
332        }
333        return fRenderTargetProxy->priv().peekRenderTarget();
334    }
335
336    GrSurfaceProxy* asSurfaceProxy() override { return fRenderTargetProxy.get(); }
337    const GrSurfaceProxy* asSurfaceProxy() const override { return fRenderTargetProxy.get(); }
338    sk_sp<GrSurfaceProxy> asSurfaceProxyRef() override { return fRenderTargetProxy; }
339
340    GrTextureProxy* asTextureProxy() override;
341    sk_sp<GrTextureProxy> asTextureProxyRef() override;
342
343    GrRenderTargetProxy* asRenderTargetProxy() override { return fRenderTargetProxy.get(); }
344    sk_sp<GrRenderTargetProxy> asRenderTargetProxyRef() override { return fRenderTargetProxy; }
345
346    GrRenderTargetContext* asRenderTargetContext() override { return this; }
347
348    // Provides access to functions that aren't part of the public API.
349    GrRenderTargetContextPriv priv();
350    const GrRenderTargetContextPriv priv() const;
351
352    bool isWrapped_ForTesting() const;
353
354protected:
355    GrRenderTargetContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTargetProxy>,
356                          sk_sp<SkColorSpace>, const SkSurfaceProps*, GrAuditTrail*,
357                          GrSingleOwner*, bool managedOpList = true);
358
359    SkDEBUGCODE(void validate() const override;)
360
361private:
362    inline GrAAType chooseAAType(GrAA aa, GrAllowMixedSamples allowMixedSamples) {
363        return GrChooseAAType(aa, this->fsaaType(), allowMixedSamples, *this->caps());
364    }
365
366    friend class GrAtlasTextBlob;               // for access to add[Mesh]DrawOp
367    friend class GrStencilAndCoverTextContext;  // for access to add[Mesh]DrawOp
368
369    friend class GrDrawingManager; // for ctor
370    friend class GrRenderTargetContextPriv;
371    friend class GrSWMaskHelper;  // for access to add[Mesh]DrawOp
372
373    // All the path renderers currently make their own ops
374    friend class GrSoftwarePathRenderer;             // for access to add[Mesh]DrawOp
375    friend class GrAAConvexPathRenderer;             // for access to add[Mesh]DrawOp
376    friend class GrDashLinePathRenderer;             // for access to add[Mesh]DrawOp
377    friend class GrAAHairLinePathRenderer;           // for access to add[Mesh]DrawOp
378    friend class GrAALinearizingConvexPathRenderer;  // for access to add[Mesh]DrawOp
379    friend class GrSmallPathRenderer;                // for access to add[Mesh]DrawOp
380    friend class GrDefaultPathRenderer;              // for access to add[Mesh]DrawOp
381    friend class GrMSAAPathRenderer;                 // for access to add[Mesh]DrawOp
382    friend class GrStencilAndCoverPathRenderer;      // for access to add[Mesh]DrawOp
383    friend class GrTessellatingPathRenderer;         // for access to add[Mesh]DrawOp
384    // for a unit test
385    friend void test_draw_op(GrRenderTargetContext*,
386                             sk_sp<GrFragmentProcessor>, sk_sp<GrTextureProxy>);
387
388    void internalClear(const GrFixedClip&, const GrColor, bool canIgnoreClip);
389
390    // Only consumes the GrPaint if successful.
391    bool drawFilledDRRect(const GrClip& clip,
392                          GrPaint&& paint,
393                          GrAA,
394                          const SkMatrix& viewMatrix,
395                          const SkRRect& origOuter,
396                          const SkRRect& origInner);
397
398    // Only consumes the GrPaint if successful.
399    bool drawFilledRect(const GrClip& clip,
400                        GrPaint&& paint,
401                        GrAA,
402                        const SkMatrix& viewMatrix,
403                        const SkRect& rect,
404                        const GrUserStencilSettings* ss);
405
406    void internalDrawPath(
407            const GrClip&, GrPaint&&, GrAA, const SkMatrix&, const SkPath&, const GrStyle&);
408
409    // These perform processing specific to Gr[Mesh]DrawOp-derived ops before recording them into
410    // the op list. They return the id of the opList to which the op was added, or 0, if it was
411    // dropped (e.g., due to clipping).
412    uint32_t addDrawOp(const GrClip&, std::unique_ptr<GrDrawOp>);
413    uint32_t addLegacyMeshDrawOp(GrPipelineBuilder&&, const GrClip&,
414                                 std::unique_ptr<GrLegacyMeshDrawOp>);
415
416    // Makes a copy of the proxy if it is necessary for the draw and places the texture that should
417    // be used by GrXferProcessor to access the destination color in 'result'. If the return
418    // value is false then a texture copy could not be made.
419    bool SK_WARN_UNUSED_RESULT setupDstProxy(GrRenderTargetProxy*,
420                                             const GrClip&,
421                                             const SkRect& opBounds,
422                                             GrXferProcessor::DstProxy* result);
423
424    GrRenderTargetOpList* getRTOpList();
425    GrOpList* getOpList() override;
426
427    sk_sp<GrRenderTargetProxy>        fRenderTargetProxy;
428
429    // In MDB-mode the GrOpList can be closed by some other renderTargetContext that has picked
430    // it up. For this reason, the GrOpList should only ever be accessed via 'getOpList'.
431    sk_sp<GrRenderTargetOpList>       fOpList;
432    GrInstancedPipelineInfo           fInstancedPipelineInfo;
433
434    sk_sp<GrColorSpaceXform>          fColorXformFromSRGB;
435    SkSurfaceProps                    fSurfaceProps;
436    bool                              fManagedOpList;
437
438    typedef GrSurfaceContext INHERITED;
439};
440
441#endif
442