blob: aae4bf00b36aab05047d2953635d4bd35bc350ba [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*,
67 const GrPathProcessor*,
68 GrPathRangeDraw* draw,
69 int /*GrPathRendering::FillType*/ fill);
robertphillipsea461502015-05-26 11:38:03 -070070
71 /**
72 * Provides a perfomance hint that the render target's contents are allowed
73 * to become undefined.
74 */
75 void discard(GrRenderTarget*);
76
77 /**
78 * Clear the entire or rect of the render target, ignoring any clips.
79 * @param target The render target to clear.
80 * @param rect the rect to clear or the whole thing if rect is NULL.
81 * @param color the color to clear to.
82 * @param canIgnoreRect allows partial clears to be converted to whole
83 * clears on platforms for which that is cheap
84 */
85 void clear(GrRenderTarget*, const SkIRect* rect, GrColor color, bool canIgnoreRect);
86
87 /**
88 * Draw everywhere (respecting the clip) with the paint.
89 */
90 void drawPaint(GrRenderTarget*, const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
91
92 /**
93 * Draw the rect using a paint.
94 * @param paint describes how to color pixels.
95 * @param viewMatrix transformation matrix
96 * @param strokeInfo the stroke information (width, join, cap), and.
97 * the dash information (intervals, count, phase).
98 * If strokeInfo == NULL, then the rect is filled.
99 * Otherwise, if stroke width == 0, then the stroke
100 * is always a single pixel thick, else the rect is
101 * mitered/beveled stroked based on stroke width.
102 * The rects coords are used to access the paint (through texture matrix)
103 */
104 void drawRect(GrRenderTarget*,
105 const GrClip&,
106 const GrPaint& paint,
107 const SkMatrix& viewMatrix,
108 const SkRect&,
109 const GrStrokeInfo* strokeInfo = NULL);
110
111 /**
112 * Maps a rectangle of shader coordinates to a rectangle and draws that rectangle
113 *
114 * @param paint describes how to color pixels.
115 * @param viewMatrix transformation matrix which applies to rectToDraw
116 * @param rectToDraw the rectangle to draw
117 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700118 */
119 void drawNonAARectToRect(GrRenderTarget*,
120 const GrClip&,
121 const GrPaint& paint,
122 const SkMatrix& viewMatrix,
123 const SkRect& rectToDraw,
joshualittb6b513b2015-08-21 10:25:18 -0700124 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700125
126 /**
127 * Draws a non-AA rect with paint and a localMatrix
128 */
129 void drawNonAARectWithLocalMatrix(GrRenderTarget* rt,
130 const GrClip& clip,
131 const GrPaint& paint,
132 const SkMatrix& viewMatrix,
133 const SkRect& rect,
joshualittb6b513b2015-08-21 10:25:18 -0700134 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700135
136 /**
137 * Draw a roundrect using a paint.
138 *
139 * @param paint describes how to color pixels.
140 * @param viewMatrix transformation matrix
141 * @param rrect the roundrect to draw
142 * @param strokeInfo the stroke information (width, join, cap) and
143 * the dash information (intervals, count, phase).
144 */
145 void drawRRect(GrRenderTarget*,
146 const GrClip&,
147 const GrPaint&,
148 const SkMatrix& viewMatrix,
149 const SkRRect& rrect,
150 const GrStrokeInfo&);
151
152 /**
153 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
154 * Does not support stroking. The result is undefined if outer does not contain
155 * inner.
156 *
157 * @param paint describes how to color pixels.
158 * @param viewMatrix transformation matrix
159 * @param outer the outer roundrect
160 * @param inner the inner roundrect
161 */
162 void drawDRRect(GrRenderTarget*,
163 const GrClip&,
164 const GrPaint&,
165 const SkMatrix& viewMatrix,
166 const SkRRect& outer,
167 const SkRRect& inner);
168
169
170 /**
171 * Draws a path.
172 *
173 * @param paint describes how to color pixels.
174 * @param viewMatrix transformation matrix
175 * @param path the path to draw
176 * @param strokeInfo the stroke information (width, join, cap) and
177 * the dash information (intervals, count, phase).
178 */
179 void drawPath(GrRenderTarget*,
180 const GrClip&,
181 const GrPaint&,
182 const SkMatrix& viewMatrix,
183 const SkPath&,
184 const GrStrokeInfo&);
185
186 /**
187 * Draws vertices with a paint.
188 *
189 * @param paint describes how to color pixels.
190 * @param viewMatrix transformation matrix
191 * @param primitiveType primitives type to draw.
192 * @param vertexCount number of vertices.
193 * @param positions array of vertex positions, required.
194 * @param texCoords optional array of texture coordinates used
195 * to access the paint.
196 * @param colors optional array of per-vertex colors, supercedes
197 * the paint's color field.
198 * @param indices optional array of indices. If NULL vertices
199 * are drawn non-indexed.
200 * @param indexCount if indices is non-null then this is the
201 * number of indices.
202 */
203 void drawVertices(GrRenderTarget*,
204 const GrClip&,
205 const GrPaint& paint,
206 const SkMatrix& viewMatrix,
207 GrPrimitiveType primitiveType,
208 int vertexCount,
209 const SkPoint positions[],
210 const SkPoint texs[],
211 const GrColor colors[],
212 const uint16_t indices[],
213 int indexCount);
214
215 /**
jvanverth31ff7622015-08-07 10:09:28 -0700216 * Draws textured sprites from an atlas with a paint.
217 *
218 * @param paint describes how to color pixels.
219 * @param viewMatrix transformation matrix
220 * @param spriteCount number of sprites.
221 * @param xform array of compressed transformation data, required.
222 * @param texRect array of texture rectangles used to access the paint.
223 * @param colors optional array of per-sprite colors, supercedes
224 * the paint's color field.
225 */
226 void drawAtlas(GrRenderTarget*,
227 const GrClip&,
228 const GrPaint& paint,
229 const SkMatrix& viewMatrix,
230 int spriteCount,
231 const SkRSXform xform[],
232 const SkRect texRect[],
233 const SkColor colors[]);
234
235 /**
robertphillipsea461502015-05-26 11:38:03 -0700236 * Draws an oval.
237 *
238 * @param paint describes how to color pixels.
239 * @param viewMatrix transformation matrix
240 * @param oval the bounding rect of the oval.
241 * @param strokeInfo the stroke information (width, join, cap) and
242 * the dash information (intervals, count, phase).
243 */
244 void drawOval(GrRenderTarget*,
245 const GrClip&,
246 const GrPaint& paint,
247 const SkMatrix& viewMatrix,
248 const SkRect& oval,
249 const GrStrokeInfo& strokeInfo);
250
251
joshualittb7ee1bf2015-08-10 11:59:02 -0700252 /**
253 * Draws a batch
254 *
255 * @param paint describes how to color pixels.
256 * @param batch the batch to draw
257 */
bsalomonabd30f52015-08-13 13:34:48 -0700258 void drawBatch(GrRenderTarget*, const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700259
robertphillipsea461502015-05-26 11:38:03 -0700260private:
robertphillips2334fb62015-06-17 05:43:33 -0700261 friend class GrAtlasTextContext; // for access to drawBatch
robertphillipsea461502015-05-26 11:38:03 -0700262 friend class GrContext; // for ctor
263
robertphillipsfcf78292015-06-19 11:49:52 -0700264 GrDrawContext(GrContext*, GrDrawTarget*, const SkSurfaceProps&);
robertphillipsea461502015-05-26 11:38:03 -0700265
robertphillipsfcf78292015-06-19 11:49:52 -0700266 GrTextContext* createTextContext(GrRenderTarget*, const SkSurfaceProps&);
robertphillipsea461502015-05-26 11:38:03 -0700267
joshualitt7b670db2015-07-09 13:25:02 -0700268 // Checks if the context has been abandoned and if the rendertarget is owned by this context
robertphillipsea461502015-05-26 11:38:03 -0700269 bool prepareToDraw(GrRenderTarget* rt);
270
271 void internalDrawPath(GrDrawTarget*,
272 GrPipelineBuilder*,
273 const SkMatrix& viewMatrix,
274 GrColor,
275 bool useAA,
276 const SkPath&,
277 const GrStrokeInfo&);
278
robertphillips2334fb62015-06-17 05:43:33 -0700279 // This entry point allows the GrTextContext-derived classes to add their batches to
280 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700281 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700282
283 GrContext* fContext; // owning context -> no ref
284 GrDrawTarget* fDrawTarget;
285 GrTextContext* fTextContext; // lazily created
286
robertphillipsfcf78292015-06-19 11:49:52 -0700287 SkSurfaceProps fSurfaceProps;
robertphillipsea461502015-05-26 11:38:03 -0700288};
289
290#endif