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