blob: 7a2223f78f8030575c1fd7847dfa9b4ecc8b6484 [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
15class GrBatch;
16class GrClip;
17class GrContext;
18class 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
123 * @param localMatrix an optional matrix to transform the shader coordinates before applying
124 * to rectToDraw
125 */
126 void drawNonAARectToRect(GrRenderTarget*,
127 const GrClip&,
128 const GrPaint& paint,
129 const SkMatrix& viewMatrix,
130 const SkRect& rectToDraw,
131 const SkRect& localRect,
132 const SkMatrix* localMatrix = NULL);
133
134 /**
135 * Draws a non-AA rect with paint and a localMatrix
136 */
137 void drawNonAARectWithLocalMatrix(GrRenderTarget* rt,
138 const GrClip& clip,
139 const GrPaint& paint,
140 const SkMatrix& viewMatrix,
141 const SkRect& rect,
142 const SkMatrix& localMatrix) {
143 this->drawNonAARectToRect(rt, clip, paint, viewMatrix, rect, rect, &localMatrix);
144 }
145
146 /**
147 * Draw a roundrect using a paint.
148 *
149 * @param paint describes how to color pixels.
150 * @param viewMatrix transformation matrix
151 * @param rrect the roundrect to draw
152 * @param strokeInfo the stroke information (width, join, cap) and
153 * the dash information (intervals, count, phase).
154 */
155 void drawRRect(GrRenderTarget*,
156 const GrClip&,
157 const GrPaint&,
158 const SkMatrix& viewMatrix,
159 const SkRRect& rrect,
160 const GrStrokeInfo&);
161
162 /**
163 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
164 * Does not support stroking. The result is undefined if outer does not contain
165 * inner.
166 *
167 * @param paint describes how to color pixels.
168 * @param viewMatrix transformation matrix
169 * @param outer the outer roundrect
170 * @param inner the inner roundrect
171 */
172 void drawDRRect(GrRenderTarget*,
173 const GrClip&,
174 const GrPaint&,
175 const SkMatrix& viewMatrix,
176 const SkRRect& outer,
177 const SkRRect& inner);
178
179
180 /**
181 * Draws a path.
182 *
183 * @param paint describes how to color pixels.
184 * @param viewMatrix transformation matrix
185 * @param path the path to draw
186 * @param strokeInfo the stroke information (width, join, cap) and
187 * the dash information (intervals, count, phase).
188 */
189 void drawPath(GrRenderTarget*,
190 const GrClip&,
191 const GrPaint&,
192 const SkMatrix& viewMatrix,
193 const SkPath&,
194 const GrStrokeInfo&);
195
196 /**
197 * Draws vertices with a paint.
198 *
199 * @param paint describes how to color pixels.
200 * @param viewMatrix transformation matrix
201 * @param primitiveType primitives type to draw.
202 * @param vertexCount number of vertices.
203 * @param positions array of vertex positions, required.
204 * @param texCoords optional array of texture coordinates used
205 * to access the paint.
206 * @param colors optional array of per-vertex colors, supercedes
207 * the paint's color field.
208 * @param indices optional array of indices. If NULL vertices
209 * are drawn non-indexed.
210 * @param indexCount if indices is non-null then this is the
211 * number of indices.
212 */
213 void drawVertices(GrRenderTarget*,
214 const GrClip&,
215 const GrPaint& paint,
216 const SkMatrix& viewMatrix,
217 GrPrimitiveType primitiveType,
218 int vertexCount,
219 const SkPoint positions[],
220 const SkPoint texs[],
221 const GrColor colors[],
222 const uint16_t indices[],
223 int indexCount);
224
225 /**
jvanverth31ff7622015-08-07 10:09:28 -0700226 * Draws textured sprites from an atlas with a paint.
227 *
228 * @param paint describes how to color pixels.
229 * @param viewMatrix transformation matrix
230 * @param spriteCount number of sprites.
231 * @param xform array of compressed transformation data, required.
232 * @param texRect array of texture rectangles used to access the paint.
233 * @param colors optional array of per-sprite colors, supercedes
234 * the paint's color field.
235 */
236 void drawAtlas(GrRenderTarget*,
237 const GrClip&,
238 const GrPaint& paint,
239 const SkMatrix& viewMatrix,
240 int spriteCount,
241 const SkRSXform xform[],
242 const SkRect texRect[],
243 const SkColor colors[]);
244
245 /**
robertphillipsea461502015-05-26 11:38:03 -0700246 * Draws an oval.
247 *
248 * @param paint describes how to color pixels.
249 * @param viewMatrix transformation matrix
250 * @param oval the bounding rect of the oval.
251 * @param strokeInfo the stroke information (width, join, cap) and
252 * the dash information (intervals, count, phase).
253 */
254 void drawOval(GrRenderTarget*,
255 const GrClip&,
256 const GrPaint& paint,
257 const SkMatrix& viewMatrix,
258 const SkRect& oval,
259 const GrStrokeInfo& strokeInfo);
260
261
262private:
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.
283 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrBatch* batch);
284
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