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