GrRenderTargetContext.h revision 391395dcfbff09a83f8f0e9d3e02d38c855ae2e9
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 GrDrawContext_DEFINED
9#define GrDrawContext_DEFINED
10
11#include "GrColor.h"
12#include "GrRenderTarget.h"
13#include "SkRefCnt.h"
14#include "SkRegion.h"
15#include "SkSurfaceProps.h"
16#include "../private/GrSingleOwner.h"
17
18class GrAtlasTextContext;
19class GrAuditTrail;
20class GrClip;
21class GrContext;
22class GrDrawBatch;
23class GrDrawContextPriv;
24class GrDrawPathBatchBase;
25class GrDrawingManager;
26class GrDrawTarget;
27class GrPaint;
28class GrPathProcessor;
29class GrPipelineBuilder;
30class GrRenderTarget;
31class GrStrokeInfo;
32class GrSurface;
33class SkDrawFilter;
34struct SkIPoint;
35struct SkIRect;
36class SkMatrix;
37class SkPaint;
38class SkPath;
39struct SkPoint;
40struct SkRect;
41class SkRRect;
42struct SkRSXform;
43class SkTextBlob;
44
45/*
46 * A helper object to orchestrate draws
47 */
48class SK_API GrDrawContext : public SkRefCnt {
49public:
50    ~GrDrawContext() override;
51
52    bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
53
54    // TODO: it is odd that we need both the SkPaint in the following 3 methods.
55    // We should extract the text parameters from SkPaint and pass them separately
56    // akin to GrStrokeInfo (GrTextInfo?)
57    virtual void drawText(const GrClip&,  const GrPaint&, const SkPaint&,
58                          const SkMatrix& viewMatrix, const char text[], size_t byteLength,
59                          SkScalar x, SkScalar y, const SkIRect& clipBounds);
60    virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
61                             const SkMatrix& viewMatrix, const char text[], size_t byteLength,
62                             const SkScalar pos[], int scalarsPerPosition,
63                             const SkPoint& offset, const SkIRect& clipBounds);
64    virtual void drawTextBlob(const GrClip&, const SkPaint&,
65                              const SkMatrix& viewMatrix, const SkTextBlob*,
66                              SkScalar x, SkScalar y,
67                              SkDrawFilter*, const SkIRect& clipBounds);
68
69    /**
70     * Provides a perfomance hint that the render target's contents are allowed
71     * to become undefined.
72     */
73    void discard();
74
75    /**
76     * Clear the entire or rect of the render target, ignoring any clips.
77     * @param rect  the rect to clear or the whole thing if rect is NULL.
78     * @param color the color to clear to.
79     * @param canIgnoreRect allows partial clears to be converted to whole
80     *                      clears on platforms for which that is cheap
81     */
82    void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
83
84    /**
85     *  Draw everywhere (respecting the clip) with the paint.
86     */
87    void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
88
89    /**
90     *  Draw the rect using a paint.
91     *  @param paint        describes how to color pixels.
92     *  @param viewMatrix   transformation matrix
93     *  @param strokeInfo   the stroke information (width, join, cap), and.
94     *                      the dash information (intervals, count, phase).
95     *                      If strokeInfo == NULL, then the rect is filled.
96     *                      Otherwise, if stroke width == 0, then the stroke
97     *                      is always a single pixel thick, else the rect is
98     *                      mitered/beveled stroked based on stroke width.
99     *  The rects coords are used to access the paint (through texture matrix)
100     */
101    void drawRect(const GrClip&,
102                  const GrPaint& paint,
103                  const SkMatrix& viewMatrix,
104                  const SkRect&,
105                  const GrStrokeInfo* strokeInfo = nullptr);
106
107    /**
108     * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
109     *
110     * @param paint         describes how to color pixels.
111     * @param viewMatrix    transformation matrix which applies to rectToDraw
112     * @param rectToDraw    the rectangle to draw
113     * @param localRect     the rectangle of shader coordinates applied to rectToDraw
114     */
115    void fillRectToRect(const GrClip&,
116                        const GrPaint& paint,
117                        const SkMatrix& viewMatrix,
118                        const SkRect& rectToDraw,
119                        const SkRect& localRect);
120
121    /**
122     * Fills a rect with a paint and a localMatrix.
123     */
124    void fillRectWithLocalMatrix(const GrClip& clip,
125                                 const GrPaint& paint,
126                                 const SkMatrix& viewMatrix,
127                                 const SkRect& rect,
128                                 const SkMatrix& localMatrix);
129
130    /**
131     *  Draw a roundrect using a paint.
132     *
133     *  @param paint        describes how to color pixels.
134     *  @param viewMatrix   transformation matrix
135     *  @param rrect        the roundrect to draw
136     *  @param strokeInfo   the stroke information (width, join, cap) and
137     *                      the dash information (intervals, count, phase).
138     */
139    void drawRRect(const GrClip&,
140                   const GrPaint&,
141                   const SkMatrix& viewMatrix,
142                   const SkRRect& rrect,
143                   const GrStrokeInfo&);
144
145    /**
146     *  Shortcut for drawing an SkPath consisting of nested rrects using a paint.
147     *  Does not support stroking. The result is undefined if outer does not contain
148     *  inner.
149     *
150     *  @param paint        describes how to color pixels.
151     *  @param viewMatrix   transformation matrix
152     *  @param outer        the outer roundrect
153     *  @param inner        the inner roundrect
154     */
155    void drawDRRect(const GrClip&,
156                    const GrPaint&,
157                    const SkMatrix& viewMatrix,
158                    const SkRRect& outer,
159                    const SkRRect& inner);
160
161    /**
162     * Draws a path.
163     *
164     * @param paint         describes how to color pixels.
165     * @param viewMatrix    transformation matrix
166     * @param path          the path to draw
167     * @param strokeInfo    the stroke information (width, join, cap) and
168     *                      the dash information (intervals, count, phase).
169     */
170    void drawPath(const GrClip&,
171                  const GrPaint&,
172                  const SkMatrix& viewMatrix,
173                  const SkPath&,
174                  const GrStrokeInfo&);
175
176    /**
177     * Draws vertices with a paint.
178     *
179     * @param   paint           describes how to color pixels.
180     * @param   viewMatrix      transformation matrix
181     * @param   primitiveType   primitives type to draw.
182     * @param   vertexCount     number of vertices.
183     * @param   positions       array of vertex positions, required.
184     * @param   texCoords       optional array of texture coordinates used
185     *                          to access the paint.
186     * @param   colors          optional array of per-vertex colors, supercedes
187     *                          the paint's color field.
188     * @param   indices         optional array of indices. If NULL vertices
189     *                          are drawn non-indexed.
190     * @param   indexCount      if indices is non-null then this is the
191     *                          number of indices.
192     */
193    void drawVertices(const GrClip&,
194                      const GrPaint& paint,
195                      const SkMatrix& viewMatrix,
196                      GrPrimitiveType primitiveType,
197                      int vertexCount,
198                      const SkPoint positions[],
199                      const SkPoint texs[],
200                      const GrColor colors[],
201                      const uint16_t indices[],
202                      int indexCount);
203
204    /**
205     * Draws textured sprites from an atlas with a paint.
206     *
207     * @param   paint           describes how to color pixels.
208     * @param   viewMatrix      transformation matrix
209     * @param   spriteCount     number of sprites.
210     * @param   xform           array of compressed transformation data, required.
211     * @param   texRect         array of texture rectangles used to access the paint.
212     * @param   colors          optional array of per-sprite colors, supercedes
213     *                          the paint's color field.
214     */
215    void drawAtlas(const GrClip&,
216                   const GrPaint& paint,
217                   const SkMatrix& viewMatrix,
218                   int spriteCount,
219                   const SkRSXform xform[],
220                   const SkRect texRect[],
221                   const SkColor colors[]);
222
223    /**
224     * Draws an oval.
225     *
226     * @param paint         describes how to color pixels.
227     * @param viewMatrix    transformation matrix
228     * @param oval          the bounding rect of the oval.
229     * @param strokeInfo    the stroke information (width, join, cap) and
230     *                      the dash information (intervals, count, phase).
231     */
232    void drawOval(const GrClip&,
233                  const GrPaint& paint,
234                  const SkMatrix& viewMatrix,
235                  const SkRect& oval,
236                  const GrStrokeInfo& strokeInfo);
237
238    /**
239     *  Draw the image stretched differentially to fit into dst.
240     *  center is a rect within the image, and logically divides the image
241     *  into 9 sections (3x3). For example, if the middle pixel of a [5x5]
242     *  image is the "center", then the center-rect should be [2, 2, 3, 3].
243     *
244     *  If the dst is >= the image size, then...
245     *  - The 4 corners are not stretched at all.
246     *  - The sides are stretched in only one axis.
247     *  - The center is stretched in both axes.
248     * Else, for each axis where dst < image,
249     *  - The corners shrink proportionally
250     *  - The sides (along the shrink axis) and center are not drawn
251     */
252    void drawImageNine(const GrClip&,
253                       const GrPaint& paint,
254                       const SkMatrix& viewMatrix,
255                       int imageWidth,
256                       int imageHeight,
257                       const SkIRect& center,
258                       const SkRect& dst);
259
260    /**
261     * Draws a batch
262     *
263     * @param paint    describes how to color pixels.
264     * @param batch    the batch to draw
265     */
266    void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*);
267
268    /**
269     * Draws a path batch. This needs to be separate from drawBatch because we install path stencil
270     * settings late.
271     *
272     * TODO: Figure out a better model that allows us to roll this method into drawBatch.
273     */
274    void drawPathBatch(const GrPipelineBuilder&, GrDrawPathBatchBase*);
275
276    int width() const { return fRenderTarget->width(); }
277    int height() const { return fRenderTarget->height(); }
278    int numColorSamples() const { return fRenderTarget->numColorSamples(); }
279
280    GrRenderTarget* accessRenderTarget() { return fRenderTarget; }
281
282    // Provides access to functions that aren't part of the public API.
283    GrDrawContextPriv drawContextPriv();
284    const GrDrawContextPriv drawContextPriv() const;
285
286protected:
287    GrDrawContext(GrContext*, GrDrawingManager*, GrRenderTarget*,
288                  const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
289
290    GrDrawingManager* drawingManager() { return fDrawingManager; }
291    GrAuditTrail* auditTrail() { return fAuditTrail; }
292    const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
293
294    SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
295    SkDEBUGCODE(void validate() const;)
296
297private:
298    friend class GrAtlasTextBlob; // for access to drawBatch
299    friend class GrDrawingManager; // for ctor
300    friend class GrDrawContextPriv;
301
302    bool drawFilledDRRect(const GrClip& clip,
303                          const GrPaint& paint,
304                          const SkMatrix& viewMatrix,
305                          const SkRRect& origOuter,
306                          const SkRRect& origInner);
307
308    GrDrawBatch* getFillRectBatch(const GrPaint& paint,
309                                  const SkMatrix& viewMatrix,
310                                  const SkRect& rect);
311
312    void internalDrawPath(const GrClip& clip,
313                          const GrPaint& paint,
314                          const SkMatrix& viewMatrix,
315                          const SkPath& path,
316                          const GrStrokeInfo& strokeInfo);
317
318    // This entry point allows the GrTextContext-derived classes to add their batches to
319    // the drawTarget.
320    void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
321
322    GrDrawTarget* getDrawTarget();
323
324    GrDrawingManager*                 fDrawingManager;
325    GrRenderTarget*                   fRenderTarget;
326
327    // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
328    // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
329    GrDrawTarget*                     fDrawTarget;
330    SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
331    GrContext*                        fContext;
332
333    SkSurfaceProps                    fSurfaceProps;
334    GrAuditTrail*                     fAuditTrail;
335
336    // In debug builds we guard against improper thread handling
337    SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
338};
339
340#endif
341