blob: 61b39079b0cbd71a0499e577aabba76942516e31 [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"
robertphillips7bceedc2015-12-01 12:51:26 -080012#include "GrRenderTarget.h"
robertphillipsea461502015-05-26 11:38:03 -070013#include "SkRefCnt.h"
robertphillips391395d2016-03-02 09:26:36 -080014#include "SkRegion.h"
robertphillipsfcf78292015-06-19 11:49:52 -070015#include "SkSurfaceProps.h"
joshualitt1de610a2016-01-06 08:26:09 -080016#include "../private/GrSingleOwner.h"
robertphillipsea461502015-05-26 11:38:03 -070017
joshualitt8e84a1e2016-02-16 11:09:25 -080018class GrAtlasTextContext;
joshualittbc907352016-01-13 06:45:40 -080019class GrAuditTrail;
robertphillipsea461502015-05-26 11:38:03 -070020class GrClip;
21class GrContext;
bsalomonabd30f52015-08-13 13:34:48 -070022class GrDrawBatch;
robertphillips391395d2016-03-02 09:26:36 -080023class GrDrawContextPriv;
cdalton8ff8d242015-12-08 10:20:32 -080024class GrDrawPathBatchBase;
robertphillips77a2e522015-10-17 07:43:27 -070025class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070026class GrDrawTarget;
27class GrPaint;
28class GrPathProcessor;
robertphillipsea461502015-05-26 11:38:03 -070029class GrPipelineBuilder;
30class GrRenderTarget;
31class GrStrokeInfo;
32class GrSurface;
robertphillips2334fb62015-06-17 05:43:33 -070033class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070034struct SkIPoint;
35struct SkIRect;
36class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070037class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070038class SkPath;
39struct SkPoint;
40struct SkRect;
41class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070042struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070043class SkTextBlob;
robertphillipsea461502015-05-26 11:38:03 -070044
45/*
46 * A helper object to orchestrate draws
47 */
48class SK_API GrDrawContext : public SkRefCnt {
49public:
robertphillips2334fb62015-06-17 05:43:33 -070050 ~GrDrawContext() override;
51
bsalomonb8fea972016-02-16 07:34:17 -080052 bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
robertphillipsea461502015-05-26 11:38:03 -070053
robertphillips2334fb62015-06-17 05:43:33 -070054 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
55 // We should extract the text parameters from SkPaint and pass them separately
56 // akin to GrStrokeInfo (GrTextInfo?)
joshualitt96880d92016-02-16 10:36:53 -080057 virtual void drawText(const GrClip&, const GrPaint&, const SkPaint&,
58 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
59 SkScalar x, SkScalar y, const SkIRect& clipBounds);
60 virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
61 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
62 const SkScalar pos[], int scalarsPerPosition,
63 const SkPoint& offset, const SkIRect& clipBounds);
64 virtual void drawTextBlob(const GrClip&, const SkPaint&,
65 const SkMatrix& viewMatrix, const SkTextBlob*,
66 SkScalar x, SkScalar y,
67 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070068
robertphillipsea461502015-05-26 11:38:03 -070069 /**
70 * Provides a perfomance hint that the render target's contents are allowed
71 * to become undefined.
72 */
robertphillips2e1e51f2015-10-15 08:01:48 -070073 void discard();
robertphillipsea461502015-05-26 11:38:03 -070074
75 /**
76 * Clear the entire or rect of the render target, ignoring any clips.
robertphillipsea461502015-05-26 11:38:03 -070077 * @param rect the rect to clear or the whole thing if rect is NULL.
78 * @param color the color to clear to.
79 * @param canIgnoreRect allows partial clears to be converted to whole
80 * clears on platforms for which that is cheap
81 */
robertphillips2e1e51f2015-10-15 08:01:48 -070082 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
robertphillipsea461502015-05-26 11:38:03 -070083
84 /**
85 * Draw everywhere (respecting the clip) with the paint.
86 */
robertphillips2e1e51f2015-10-15 08:01:48 -070087 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -070088
89 /**
90 * Draw the rect using a paint.
91 * @param paint describes how to color pixels.
92 * @param viewMatrix transformation matrix
93 * @param strokeInfo the stroke information (width, join, cap), and.
94 * the dash information (intervals, count, phase).
95 * If strokeInfo == NULL, then the rect is filled.
96 * Otherwise, if stroke width == 0, then the stroke
97 * is always a single pixel thick, else the rect is
98 * mitered/beveled stroked based on stroke width.
99 * The rects coords are used to access the paint (through texture matrix)
100 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700101 void drawRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700102 const GrPaint& paint,
103 const SkMatrix& viewMatrix,
104 const SkRect&,
robertphillips4bc31812016-03-01 12:22:49 -0800105 const GrStrokeInfo* strokeInfo = nullptr);
robertphillipsea461502015-05-26 11:38:03 -0700106
107 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800108 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
robertphillipsea461502015-05-26 11:38:03 -0700109 *
110 * @param paint describes how to color pixels.
111 * @param viewMatrix transformation matrix which applies to rectToDraw
112 * @param rectToDraw the rectangle to draw
113 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700114 */
bsalomona2e69fc2015-11-05 10:41:43 -0800115 void fillRectToRect(const GrClip&,
116 const GrPaint& paint,
117 const SkMatrix& viewMatrix,
118 const SkRect& rectToDraw,
119 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700120
121 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800122 * Fills a rect with a paint and a localMatrix.
robertphillipsea461502015-05-26 11:38:03 -0700123 */
bsalomona2e69fc2015-11-05 10:41:43 -0800124 void fillRectWithLocalMatrix(const GrClip& clip,
125 const GrPaint& paint,
126 const SkMatrix& viewMatrix,
127 const SkRect& rect,
128 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700129
130 /**
131 * Draw a roundrect using a paint.
132 *
133 * @param paint describes how to color pixels.
134 * @param viewMatrix transformation matrix
135 * @param rrect the roundrect to draw
136 * @param strokeInfo the stroke information (width, join, cap) and
137 * the dash information (intervals, count, phase).
138 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700139 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700140 const GrPaint&,
141 const SkMatrix& viewMatrix,
142 const SkRRect& rrect,
143 const GrStrokeInfo&);
144
145 /**
robertphillips00095892016-02-29 13:50:40 -0800146 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
147 * Does not support stroking. The result is undefined if outer does not contain
148 * inner.
149 *
150 * @param paint describes how to color pixels.
151 * @param viewMatrix transformation matrix
152 * @param outer the outer roundrect
153 * @param inner the inner roundrect
154 */
155 void drawDRRect(const GrClip&,
156 const GrPaint&,
157 const SkMatrix& viewMatrix,
158 const SkRRect& outer,
159 const SkRRect& inner);
160
161 /**
robertphillipsea461502015-05-26 11:38:03 -0700162 * Draws a path.
163 *
164 * @param paint describes how to color pixels.
165 * @param viewMatrix transformation matrix
166 * @param path the path to draw
167 * @param strokeInfo the stroke information (width, join, cap) and
168 * the dash information (intervals, count, phase).
169 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700170 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700171 const GrPaint&,
172 const SkMatrix& viewMatrix,
173 const SkPath&,
174 const GrStrokeInfo&);
175
176 /**
177 * Draws vertices with a paint.
178 *
179 * @param paint describes how to color pixels.
180 * @param viewMatrix transformation matrix
181 * @param primitiveType primitives type to draw.
182 * @param vertexCount number of vertices.
183 * @param positions array of vertex positions, required.
184 * @param texCoords optional array of texture coordinates used
185 * to access the paint.
186 * @param colors optional array of per-vertex colors, supercedes
187 * the paint's color field.
188 * @param indices optional array of indices. If NULL vertices
189 * are drawn non-indexed.
190 * @param indexCount if indices is non-null then this is the
191 * number of indices.
192 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700193 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700194 const GrPaint& paint,
195 const SkMatrix& viewMatrix,
196 GrPrimitiveType primitiveType,
197 int vertexCount,
198 const SkPoint positions[],
199 const SkPoint texs[],
200 const GrColor colors[],
201 const uint16_t indices[],
202 int indexCount);
203
204 /**
jvanverth31ff7622015-08-07 10:09:28 -0700205 * Draws textured sprites from an atlas with a paint.
206 *
207 * @param paint describes how to color pixels.
208 * @param viewMatrix transformation matrix
209 * @param spriteCount number of sprites.
210 * @param xform array of compressed transformation data, required.
211 * @param texRect array of texture rectangles used to access the paint.
212 * @param colors optional array of per-sprite colors, supercedes
213 * the paint's color field.
214 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700215 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700216 const GrPaint& paint,
217 const SkMatrix& viewMatrix,
218 int spriteCount,
219 const SkRSXform xform[],
220 const SkRect texRect[],
221 const SkColor colors[]);
222
223 /**
robertphillipsea461502015-05-26 11:38:03 -0700224 * Draws an oval.
225 *
226 * @param paint describes how to color pixels.
227 * @param viewMatrix transformation matrix
228 * @param oval the bounding rect of the oval.
229 * @param strokeInfo the stroke information (width, join, cap) and
230 * the dash information (intervals, count, phase).
231 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700232 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700233 const GrPaint& paint,
234 const SkMatrix& viewMatrix,
235 const SkRect& oval,
236 const GrStrokeInfo& strokeInfo);
237
joshualitt33a5fce2015-11-18 13:28:51 -0800238 /**
239 * Draw the image stretched differentially to fit into dst.
240 * center is a rect within the image, and logically divides the image
241 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
242 * image is the "center", then the center-rect should be [2, 2, 3, 3].
243 *
244 * If the dst is >= the image size, then...
245 * - The 4 corners are not stretched at all.
246 * - The sides are stretched in only one axis.
247 * - The center is stretched in both axes.
248 * Else, for each axis where dst < image,
249 * - The corners shrink proportionally
250 * - The sides (along the shrink axis) and center are not drawn
251 */
252 void drawImageNine(const GrClip&,
253 const GrPaint& paint,
254 const SkMatrix& viewMatrix,
255 int imageWidth,
256 int imageHeight,
257 const SkIRect& center,
258 const SkRect& dst);
robertphillipsea461502015-05-26 11:38:03 -0700259
joshualittb7ee1bf2015-08-10 11:59:02 -0700260 /**
261 * Draws a batch
262 *
263 * @param paint describes how to color pixels.
264 * @param batch the batch to draw
265 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700266 void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700267
cdalton8ff8d242015-12-08 10:20:32 -0800268 /**
269 * Draws a path batch. This needs to be separate from drawBatch because we install path stencil
270 * settings late.
271 *
272 * TODO: Figure out a better model that allows us to roll this method into drawBatch.
273 */
274 void drawPathBatch(const GrPipelineBuilder&, GrDrawPathBatchBase*);
275
robertphillips7bceedc2015-12-01 12:51:26 -0800276 int width() const { return fRenderTarget->width(); }
277 int height() const { return fRenderTarget->height(); }
278 int numColorSamples() const { return fRenderTarget->numColorSamples(); }
brianosmanb461d342016-04-13 13:10:14 -0700279 bool isGammaCorrect() const { return fSurfaceProps.isGammaCorrect(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800280
robertphillips433625e2015-12-04 06:58:16 -0800281 GrRenderTarget* accessRenderTarget() { return fRenderTarget; }
282
robertphillips391395d2016-03-02 09:26:36 -0800283 // Provides access to functions that aren't part of the public API.
284 GrDrawContextPriv drawContextPriv();
285 const GrDrawContextPriv drawContextPriv() const;
joshualittf5883a62016-01-13 07:47:38 -0800286
joshualitt96880d92016-02-16 10:36:53 -0800287protected:
288 GrDrawContext(GrContext*, GrDrawingManager*, GrRenderTarget*,
289 const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
290
291 GrDrawingManager* drawingManager() { return fDrawingManager; }
292 GrAuditTrail* auditTrail() { return fAuditTrail; }
293 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
294
295 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
296 SkDEBUGCODE(void validate() const;)
297
robertphillipsea461502015-05-26 11:38:03 -0700298private:
joshualitt2e2202e2015-12-10 11:22:08 -0800299 friend class GrAtlasTextBlob; // for access to drawBatch
robertphillips77a2e522015-10-17 07:43:27 -0700300 friend class GrDrawingManager; // for ctor
robertphillips391395d2016-03-02 09:26:36 -0800301 friend class GrDrawContextPriv;
robertphillipsea461502015-05-26 11:38:03 -0700302
robertphillips00095892016-02-29 13:50:40 -0800303 bool drawFilledDRRect(const GrClip& clip,
304 const GrPaint& paint,
305 const SkMatrix& viewMatrix,
306 const SkRRect& origOuter,
307 const SkRRect& origInner);
308
robertphillips391395d2016-03-02 09:26:36 -0800309 GrDrawBatch* getFillRectBatch(const GrPaint& paint,
310 const SkMatrix& viewMatrix,
311 const SkRect& rect);
312
robertphillips4bc31812016-03-01 12:22:49 -0800313 void internalDrawPath(const GrClip& clip,
314 const GrPaint& paint,
robertphillipsea461502015-05-26 11:38:03 -0700315 const SkMatrix& viewMatrix,
robertphillips4bc31812016-03-01 12:22:49 -0800316 const SkPath& path,
317 const GrStrokeInfo& strokeInfo);
robertphillipsea461502015-05-26 11:38:03 -0700318
robertphillips2334fb62015-06-17 05:43:33 -0700319 // This entry point allows the GrTextContext-derived classes to add their batches to
320 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700321 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700322
robertphillipsa106c622015-10-16 09:07:06 -0700323 GrDrawTarget* getDrawTarget();
324
joshualitt8db86782016-02-17 05:40:00 -0800325 GrDrawingManager* fDrawingManager;
326 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700327
328 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
329 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
joshualitt8db86782016-02-17 05:40:00 -0800330 GrDrawTarget* fDrawTarget;
331 SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
332 GrContext* fContext;
robertphillips2334fb62015-06-17 05:43:33 -0700333
joshualitt8db86782016-02-17 05:40:00 -0800334 SkSurfaceProps fSurfaceProps;
335 GrAuditTrail* fAuditTrail;
joshualitt1de610a2016-01-06 08:26:09 -0800336
337 // In debug builds we guard against improper thread handling
joshualitt6d0872d2016-01-11 08:27:48 -0800338 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
robertphillipsea461502015-05-26 11:38:03 -0700339};
340
341#endif