blob: b6fc0dfa231a5c3a657f03eea564b52d6a1e54e3 [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;
robertphillips77a2e522015-10-17 07:43:27 -070018class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070019class GrDrawTarget;
20class GrPaint;
21class GrPathProcessor;
cdalton8585dd22015-10-08 08:04:09 -070022class GrPathRange;
bsalomon1fcc01c2015-09-09 09:48:06 -070023class GrPathRangeDraw;
robertphillipsea461502015-05-26 11:38:03 -070024class GrPipelineBuilder;
25class GrRenderTarget;
26class GrStrokeInfo;
27class GrSurface;
robertphillips2334fb62015-06-17 05:43:33 -070028class GrTextContext;
robertphillips2334fb62015-06-17 05:43:33 -070029class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070030struct SkIPoint;
31struct SkIRect;
32class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070033class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070034class SkPath;
35struct SkPoint;
36struct SkRect;
37class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070038struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070039class SkTextBlob;
robertphillipsea461502015-05-26 11:38:03 -070040
41/*
42 * A helper object to orchestrate draws
43 */
44class SK_API GrDrawContext : public SkRefCnt {
45public:
robertphillips2334fb62015-06-17 05:43:33 -070046 ~GrDrawContext() override;
47
robertphillips2e1e51f2015-10-15 08:01:48 -070048 void copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
robertphillipsea461502015-05-26 11:38:03 -070049
robertphillips2334fb62015-06-17 05:43:33 -070050 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
51 // We should extract the text parameters from SkPaint and pass them separately
52 // akin to GrStrokeInfo (GrTextInfo?)
robertphillips2e1e51f2015-10-15 08:01:48 -070053 void drawText(const GrClip&, const GrPaint&, const SkPaint&,
robertphillips2334fb62015-06-17 05:43:33 -070054 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
55 SkScalar x, SkScalar y, const SkIRect& clipBounds);
robertphillips2e1e51f2015-10-15 08:01:48 -070056 void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
robertphillips2334fb62015-06-17 05:43:33 -070057 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
58 const SkScalar pos[], int scalarsPerPosition,
59 const SkPoint& offset, const SkIRect& clipBounds);
robertphillips2e1e51f2015-10-15 08:01:48 -070060 void drawTextBlob(const GrClip&, const SkPaint&,
robertphillips2334fb62015-06-17 05:43:33 -070061 const SkMatrix& viewMatrix, const SkTextBlob*,
62 SkScalar x, SkScalar y,
63 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070064
bsalomon1fcc01c2015-09-09 09:48:06 -070065 // drawPathsFromRange is thanks to GrStencilAndCoverTextContext
66 // TODO: remove once path batches can be created external to GrDrawTarget.
67 void drawPathsFromRange(const GrPipelineBuilder*,
joshualittf2384692015-09-10 11:00:51 -070068 const SkMatrix& viewMatrix,
69 const SkMatrix& localMatrix,
70 GrColor color,
cdalton8585dd22015-10-08 08:04:09 -070071 GrPathRange* range,
bsalomon1fcc01c2015-09-09 09:48:06 -070072 GrPathRangeDraw* draw,
73 int /*GrPathRendering::FillType*/ fill);
robertphillipsea461502015-05-26 11:38:03 -070074
75 /**
76 * Provides a perfomance hint that the render target's contents are allowed
77 * to become undefined.
78 */
robertphillips2e1e51f2015-10-15 08:01:48 -070079 void discard();
robertphillipsea461502015-05-26 11:38:03 -070080
81 /**
82 * Clear the entire or rect of the render target, ignoring any clips.
robertphillipsea461502015-05-26 11:38:03 -070083 * @param rect the rect to clear or the whole thing if rect is NULL.
84 * @param color the color to clear to.
85 * @param canIgnoreRect allows partial clears to be converted to whole
86 * clears on platforms for which that is cheap
87 */
robertphillips2e1e51f2015-10-15 08:01:48 -070088 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
robertphillipsea461502015-05-26 11:38:03 -070089
90 /**
91 * Draw everywhere (respecting the clip) with the paint.
92 */
robertphillips2e1e51f2015-10-15 08:01:48 -070093 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -070094
95 /**
96 * Draw the rect using a paint.
97 * @param paint describes how to color pixels.
98 * @param viewMatrix transformation matrix
99 * @param strokeInfo the stroke information (width, join, cap), and.
100 * the dash information (intervals, count, phase).
101 * If strokeInfo == NULL, then the rect is filled.
102 * Otherwise, if stroke width == 0, then the stroke
103 * is always a single pixel thick, else the rect is
104 * mitered/beveled stroked based on stroke width.
105 * The rects coords are used to access the paint (through texture matrix)
106 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700107 void drawRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700108 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 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700121 void drawNonAARectToRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700122 const GrPaint& paint,
123 const SkMatrix& viewMatrix,
124 const SkRect& rectToDraw,
joshualittb6b513b2015-08-21 10:25:18 -0700125 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700126
127 /**
128 * Draws a non-AA rect with paint and a localMatrix
129 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700130 void drawNonAARectWithLocalMatrix(const GrClip& clip,
robertphillipsea461502015-05-26 11:38:03 -0700131 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 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700145 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700146 const GrPaint&,
147 const SkMatrix& viewMatrix,
148 const SkRRect& rrect,
149 const GrStrokeInfo&);
150
151 /**
152 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
153 * Does not support stroking. The result is undefined if outer does not contain
154 * inner.
155 *
156 * @param paint describes how to color pixels.
157 * @param viewMatrix transformation matrix
158 * @param outer the outer roundrect
159 * @param inner the inner roundrect
160 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700161 void drawDRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700162 const GrPaint&,
163 const SkMatrix& viewMatrix,
164 const SkRRect& outer,
165 const SkRRect& inner);
166
167
168 /**
169 * Draws a path.
170 *
171 * @param paint describes how to color pixels.
172 * @param viewMatrix transformation matrix
173 * @param path the path to draw
174 * @param strokeInfo the stroke information (width, join, cap) and
175 * the dash information (intervals, count, phase).
176 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700177 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700178 const GrPaint&,
179 const SkMatrix& viewMatrix,
180 const SkPath&,
181 const GrStrokeInfo&);
182
183 /**
184 * Draws vertices with a paint.
185 *
186 * @param paint describes how to color pixels.
187 * @param viewMatrix transformation matrix
188 * @param primitiveType primitives type to draw.
189 * @param vertexCount number of vertices.
190 * @param positions array of vertex positions, required.
191 * @param texCoords optional array of texture coordinates used
192 * to access the paint.
193 * @param colors optional array of per-vertex colors, supercedes
194 * the paint's color field.
195 * @param indices optional array of indices. If NULL vertices
196 * are drawn non-indexed.
197 * @param indexCount if indices is non-null then this is the
198 * number of indices.
199 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700200 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700201 const GrPaint& paint,
202 const SkMatrix& viewMatrix,
203 GrPrimitiveType primitiveType,
204 int vertexCount,
205 const SkPoint positions[],
206 const SkPoint texs[],
207 const GrColor colors[],
208 const uint16_t indices[],
209 int indexCount);
210
211 /**
jvanverth31ff7622015-08-07 10:09:28 -0700212 * Draws textured sprites from an atlas with a paint.
213 *
214 * @param paint describes how to color pixels.
215 * @param viewMatrix transformation matrix
216 * @param spriteCount number of sprites.
217 * @param xform array of compressed transformation data, required.
218 * @param texRect array of texture rectangles used to access the paint.
219 * @param colors optional array of per-sprite colors, supercedes
220 * the paint's color field.
221 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700222 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700223 const GrPaint& paint,
224 const SkMatrix& viewMatrix,
225 int spriteCount,
226 const SkRSXform xform[],
227 const SkRect texRect[],
228 const SkColor colors[]);
229
230 /**
robertphillipsea461502015-05-26 11:38:03 -0700231 * Draws an oval.
232 *
233 * @param paint describes how to color pixels.
234 * @param viewMatrix transformation matrix
235 * @param oval the bounding rect of the oval.
236 * @param strokeInfo the stroke information (width, join, cap) and
237 * the dash information (intervals, count, phase).
238 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700239 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700240 const GrPaint& paint,
241 const SkMatrix& viewMatrix,
242 const SkRect& oval,
243 const GrStrokeInfo& strokeInfo);
244
245
joshualittb7ee1bf2015-08-10 11:59:02 -0700246 /**
247 * Draws a batch
248 *
249 * @param paint describes how to color pixels.
250 * @param batch the batch to draw
251 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700252 void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700253
robertphillipsea461502015-05-26 11:38:03 -0700254private:
robertphillips2334fb62015-06-17 05:43:33 -0700255 friend class GrAtlasTextContext; // for access to drawBatch
robertphillips77a2e522015-10-17 07:43:27 -0700256 friend class GrDrawingManager; // for ctor
robertphillipsea461502015-05-26 11:38:03 -0700257
robertphillips2e1e51f2015-10-15 08:01:48 -0700258 SkDEBUGCODE(void validate() const;)
robertphillipsea461502015-05-26 11:38:03 -0700259
robertphillips77a2e522015-10-17 07:43:27 -0700260 GrDrawContext(GrDrawingManager*, GrRenderTarget*, const SkSurfaceProps* surfaceProps);
robertphillipsea461502015-05-26 11:38:03 -0700261
robertphillipsb83bec52015-10-23 09:38:03 -0700262 void internalDrawPath(GrPipelineBuilder*,
robertphillipsea461502015-05-26 11:38:03 -0700263 const SkMatrix& viewMatrix,
264 GrColor,
265 bool useAA,
266 const SkPath&,
267 const GrStrokeInfo&);
268
robertphillips2334fb62015-06-17 05:43:33 -0700269 // This entry point allows the GrTextContext-derived classes to add their batches to
270 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700271 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700272
robertphillipsa106c622015-10-16 09:07:06 -0700273 GrDrawTarget* getDrawTarget();
274
robertphillips77a2e522015-10-17 07:43:27 -0700275 GrDrawingManager* fDrawingManager;
276 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700277
278 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
279 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
robertphillips77a2e522015-10-17 07:43:27 -0700280 GrDrawTarget* fDrawTarget;
281 GrTextContext* fTextContext; // lazily gotten from GrContext::DrawingManager
robertphillips2334fb62015-06-17 05:43:33 -0700282
robertphillips77a2e522015-10-17 07:43:27 -0700283 SkSurfaceProps fSurfaceProps;
robertphillipsea461502015-05-26 11:38:03 -0700284};
285
286#endif