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