blob: 97cb72a7f2e530166874b8998dd132b5a95653d1 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
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 "SkRefCnt.h"
robertphillipsfcf78292015-06-19 11:49:52 -070013#include "SkSurfaceProps.h"
robertphillipsea461502015-05-26 11:38:03 -070014
robertphillipsea461502015-05-26 11:38:03 -070015class GrClip;
16class GrContext;
bsalomonabd30f52015-08-13 13:34:48 -070017class GrDrawBatch;
robertphillipsea461502015-05-26 11:38:03 -070018class GrDrawTarget;
19class GrPaint;
20class GrPathProcessor;
bsalomon1fcc01c2015-09-09 09:48:06 -070021class GrPathRangeDraw;
robertphillipsea461502015-05-26 11:38:03 -070022class GrPipelineBuilder;
23class GrRenderTarget;
24class GrStrokeInfo;
25class GrSurface;
robertphillips2334fb62015-06-17 05:43:33 -070026class GrTextContext;
robertphillips2334fb62015-06-17 05:43:33 -070027class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070028struct SkIPoint;
29struct SkIRect;
30class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070031class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070032class SkPath;
33struct SkPoint;
34struct SkRect;
35class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070036struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070037class SkTextBlob;
robertphillipsea461502015-05-26 11:38:03 -070038
39/*
40 * A helper object to orchestrate draws
41 */
42class SK_API GrDrawContext : public SkRefCnt {
43public:
robertphillips2334fb62015-06-17 05:43:33 -070044 ~GrDrawContext() override;
45
robertphillipsea461502015-05-26 11:38:03 -070046 void copySurface(GrRenderTarget* dst, GrSurface* src,
47 const SkIRect& srcRect, const SkIPoint& dstPoint);
48
robertphillips2334fb62015-06-17 05:43:33 -070049 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
50 // We should extract the text parameters from SkPaint and pass them separately
51 // akin to GrStrokeInfo (GrTextInfo?)
52 void drawText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
53 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
54 SkScalar x, SkScalar y, const SkIRect& clipBounds);
55 void drawPosText(GrRenderTarget*, const GrClip&, const GrPaint&, const SkPaint&,
56 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
57 const SkScalar pos[], int scalarsPerPosition,
58 const SkPoint& offset, const SkIRect& clipBounds);
59 void drawTextBlob(GrRenderTarget*, const GrClip&, const SkPaint&,
60 const SkMatrix& viewMatrix, const SkTextBlob*,
61 SkScalar x, SkScalar y,
62 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070063
bsalomon1fcc01c2015-09-09 09:48:06 -070064 // drawPathsFromRange is thanks to GrStencilAndCoverTextContext
65 // TODO: remove once path batches can be created external to GrDrawTarget.
66 void drawPathsFromRange(const GrPipelineBuilder*,
joshualittf2384692015-09-10 11:00:51 -070067 const SkMatrix& viewMatrix,
68 const SkMatrix& localMatrix,
69 GrColor color,
bsalomon1fcc01c2015-09-09 09:48:06 -070070 GrPathRangeDraw* draw,
71 int /*GrPathRendering::FillType*/ fill);
robertphillipsea461502015-05-26 11:38:03 -070072
73 /**
74 * Provides a perfomance hint that the render target's contents are allowed
75 * to become undefined.
76 */
77 void discard(GrRenderTarget*);
78
79 /**
80 * Clear the entire or rect of the render target, ignoring any clips.
81 * @param target The render target to clear.
82 * @param rect the rect to clear or the whole thing if rect is NULL.
83 * @param color the color to clear to.
84 * @param canIgnoreRect allows partial clears to be converted to whole
85 * clears on platforms for which that is cheap
86 */
87 void clear(GrRenderTarget*, const SkIRect* rect, GrColor color, bool canIgnoreRect);
88
89 /**
90 * Draw everywhere (respecting the clip) with the paint.
91 */
92 void drawPaint(GrRenderTarget*, const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
93
94 /**
95 * Draw the rect using a paint.
96 * @param paint describes how to color pixels.
97 * @param viewMatrix transformation matrix
98 * @param strokeInfo the stroke information (width, join, cap), and.
99 * the dash information (intervals, count, phase).
100 * If strokeInfo == NULL, then the rect is filled.
101 * Otherwise, if stroke width == 0, then the stroke
102 * is always a single pixel thick, else the rect is
103 * mitered/beveled stroked based on stroke width.
104 * The rects coords are used to access the paint (through texture matrix)
105 */
106 void drawRect(GrRenderTarget*,
107 const GrClip&,
108 const GrPaint& paint,
109 const SkMatrix& viewMatrix,
110 const SkRect&,
111 const GrStrokeInfo* strokeInfo = NULL);
112
113 /**
114 * Maps a rectangle of shader coordinates to a rectangle and draws that rectangle
115 *
116 * @param paint describes how to color pixels.
117 * @param viewMatrix transformation matrix which applies to rectToDraw
118 * @param rectToDraw the rectangle to draw
119 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700120 */
121 void drawNonAARectToRect(GrRenderTarget*,
122 const GrClip&,
123 const GrPaint& paint,
124 const SkMatrix& viewMatrix,
125 const SkRect& rectToDraw,
joshualittb6b513b2015-08-21 10:25:18 -0700126 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700127
128 /**
129 * Draws a non-AA rect with paint and a localMatrix
130 */
131 void drawNonAARectWithLocalMatrix(GrRenderTarget* rt,
132 const GrClip& clip,
133 const GrPaint& paint,
134 const SkMatrix& viewMatrix,
135 const SkRect& rect,
joshualittb6b513b2015-08-21 10:25:18 -0700136 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700137
138 /**
139 * Draw a roundrect using a paint.
140 *
141 * @param paint describes how to color pixels.
142 * @param viewMatrix transformation matrix
143 * @param rrect the roundrect to draw
144 * @param strokeInfo the stroke information (width, join, cap) and
145 * the dash information (intervals, count, phase).
146 */
147 void drawRRect(GrRenderTarget*,
148 const GrClip&,
149 const GrPaint&,
150 const SkMatrix& viewMatrix,
151 const SkRRect& rrect,
152 const GrStrokeInfo&);
153
154 /**
155 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
156 * Does not support stroking. The result is undefined if outer does not contain
157 * inner.
158 *
159 * @param paint describes how to color pixels.
160 * @param viewMatrix transformation matrix
161 * @param outer the outer roundrect
162 * @param inner the inner roundrect
163 */
164 void drawDRRect(GrRenderTarget*,
165 const GrClip&,
166 const GrPaint&,
167 const SkMatrix& viewMatrix,
168 const SkRRect& outer,
169 const SkRRect& inner);
170
171
172 /**
173 * Draws a path.
174 *
175 * @param paint describes how to color pixels.
176 * @param viewMatrix transformation matrix
177 * @param path the path to draw
178 * @param strokeInfo the stroke information (width, join, cap) and
179 * the dash information (intervals, count, phase).
180 */
181 void drawPath(GrRenderTarget*,
182 const GrClip&,
183 const GrPaint&,
184 const SkMatrix& viewMatrix,
185 const SkPath&,
186 const GrStrokeInfo&);
187
188 /**
189 * Draws vertices with a paint.
190 *
191 * @param paint describes how to color pixels.
192 * @param viewMatrix transformation matrix
193 * @param primitiveType primitives type to draw.
194 * @param vertexCount number of vertices.
195 * @param positions array of vertex positions, required.
196 * @param texCoords optional array of texture coordinates used
197 * to access the paint.
198 * @param colors optional array of per-vertex colors, supercedes
199 * the paint's color field.
200 * @param indices optional array of indices. If NULL vertices
201 * are drawn non-indexed.
202 * @param indexCount if indices is non-null then this is the
203 * number of indices.
204 */
205 void drawVertices(GrRenderTarget*,
206 const GrClip&,
207 const GrPaint& paint,
208 const SkMatrix& viewMatrix,
209 GrPrimitiveType primitiveType,
210 int vertexCount,
211 const SkPoint positions[],
212 const SkPoint texs[],
213 const GrColor colors[],
214 const uint16_t indices[],
215 int indexCount);
216
217 /**
jvanverth31ff7622015-08-07 10:09:28 -0700218 * Draws textured sprites from an atlas with a paint.
219 *
220 * @param paint describes how to color pixels.
221 * @param viewMatrix transformation matrix
222 * @param spriteCount number of sprites.
223 * @param xform array of compressed transformation data, required.
224 * @param texRect array of texture rectangles used to access the paint.
225 * @param colors optional array of per-sprite colors, supercedes
226 * the paint's color field.
227 */
228 void drawAtlas(GrRenderTarget*,
229 const GrClip&,
230 const GrPaint& paint,
231 const SkMatrix& viewMatrix,
232 int spriteCount,
233 const SkRSXform xform[],
234 const SkRect texRect[],
235 const SkColor colors[]);
236
237 /**
robertphillipsea461502015-05-26 11:38:03 -0700238 * Draws an oval.
239 *
240 * @param paint describes how to color pixels.
241 * @param viewMatrix transformation matrix
242 * @param oval the bounding rect of the oval.
243 * @param strokeInfo the stroke information (width, join, cap) and
244 * the dash information (intervals, count, phase).
245 */
246 void drawOval(GrRenderTarget*,
247 const GrClip&,
248 const GrPaint& paint,
249 const SkMatrix& viewMatrix,
250 const SkRect& oval,
251 const GrStrokeInfo& strokeInfo);
252
253
joshualittb7ee1bf2015-08-10 11:59:02 -0700254 /**
255 * Draws a batch
256 *
257 * @param paint describes how to color pixels.
258 * @param batch the batch to draw
259 */
bsalomonabd30f52015-08-13 13:34:48 -0700260 void drawBatch(GrRenderTarget*, const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700261
robertphillipsea461502015-05-26 11:38:03 -0700262private:
robertphillips2334fb62015-06-17 05:43:33 -0700263 friend class GrAtlasTextContext; // for access to drawBatch
robertphillipsea461502015-05-26 11:38:03 -0700264 friend class GrContext; // for ctor
265
robertphillipsfcf78292015-06-19 11:49:52 -0700266 GrDrawContext(GrContext*, GrDrawTarget*, const SkSurfaceProps&);
robertphillipsea461502015-05-26 11:38:03 -0700267
robertphillipsfcf78292015-06-19 11:49:52 -0700268 GrTextContext* createTextContext(GrRenderTarget*, const SkSurfaceProps&);
robertphillipsea461502015-05-26 11:38:03 -0700269
joshualitt7b670db2015-07-09 13:25:02 -0700270 // Checks if the context has been abandoned and if the rendertarget is owned by this context
robertphillipsea461502015-05-26 11:38:03 -0700271 bool prepareToDraw(GrRenderTarget* rt);
272
273 void internalDrawPath(GrDrawTarget*,
274 GrPipelineBuilder*,
275 const SkMatrix& viewMatrix,
276 GrColor,
277 bool useAA,
278 const SkPath&,
279 const GrStrokeInfo&);
280
robertphillips2334fb62015-06-17 05:43:33 -0700281 // This entry point allows the GrTextContext-derived classes to add their batches to
282 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700283 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700284
285 GrContext* fContext; // owning context -> no ref
286 GrDrawTarget* fDrawTarget;
287 GrTextContext* fTextContext; // lazily created
288
robertphillipsfcf78292015-06-19 11:49:52 -0700289 SkSurfaceProps fSurfaceProps;
robertphillipsea461502015-05-26 11:38:03 -0700290};
291
292#endif