blob: 66ccff7052c0b9e869b42f586183f568607ab14f [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"
robertphillipsfcf78292015-06-19 11:49:52 -070014#include "SkSurfaceProps.h"
joshualitt1de610a2016-01-06 08:26:09 -080015#include "../private/GrSingleOwner.h"
robertphillipsea461502015-05-26 11:38:03 -070016
joshualitt8e84a1e2016-02-16 11:09:25 -080017class GrAtlasTextContext;
joshualittbc907352016-01-13 06:45:40 -080018class GrAuditTrail;
robertphillipsea461502015-05-26 11:38:03 -070019class GrClip;
20class GrContext;
bsalomonabd30f52015-08-13 13:34:48 -070021class GrDrawBatch;
cdalton8ff8d242015-12-08 10:20:32 -080022class GrDrawPathBatchBase;
robertphillips77a2e522015-10-17 07:43:27 -070023class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070024class GrDrawTarget;
25class GrPaint;
26class GrPathProcessor;
robertphillipsea461502015-05-26 11:38:03 -070027class GrPipelineBuilder;
28class GrRenderTarget;
29class GrStrokeInfo;
30class GrSurface;
robertphillips2334fb62015-06-17 05:43:33 -070031class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070032struct SkIPoint;
33struct SkIRect;
34class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070035class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070036class SkPath;
37struct SkPoint;
38struct SkRect;
39class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070040struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070041class SkTextBlob;
robertphillipsea461502015-05-26 11:38:03 -070042
43/*
44 * A helper object to orchestrate draws
45 */
46class SK_API GrDrawContext : public SkRefCnt {
47public:
robertphillips2334fb62015-06-17 05:43:33 -070048 ~GrDrawContext() override;
49
bsalomonb8fea972016-02-16 07:34:17 -080050 bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
robertphillipsea461502015-05-26 11:38:03 -070051
robertphillips2334fb62015-06-17 05:43:33 -070052 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
53 // We should extract the text parameters from SkPaint and pass them separately
54 // akin to GrStrokeInfo (GrTextInfo?)
joshualitt96880d92016-02-16 10:36:53 -080055 virtual void drawText(const GrClip&, const GrPaint&, const SkPaint&,
56 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
57 SkScalar x, SkScalar y, const SkIRect& clipBounds);
58 virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
59 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
60 const SkScalar pos[], int scalarsPerPosition,
61 const SkPoint& offset, const SkIRect& clipBounds);
62 virtual void drawTextBlob(const GrClip&, const SkPaint&,
63 const SkMatrix& viewMatrix, const SkTextBlob*,
64 SkScalar x, SkScalar y,
65 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070066
robertphillipsea461502015-05-26 11:38:03 -070067 /**
68 * Provides a perfomance hint that the render target's contents are allowed
69 * to become undefined.
70 */
robertphillips2e1e51f2015-10-15 08:01:48 -070071 void discard();
robertphillipsea461502015-05-26 11:38:03 -070072
73 /**
74 * Clear the entire or rect of the render target, ignoring any clips.
robertphillipsea461502015-05-26 11:38:03 -070075 * @param rect the rect to clear or the whole thing if rect is NULL.
76 * @param color the color to clear to.
77 * @param canIgnoreRect allows partial clears to be converted to whole
78 * clears on platforms for which that is cheap
79 */
robertphillips2e1e51f2015-10-15 08:01:48 -070080 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
robertphillipsea461502015-05-26 11:38:03 -070081
82 /**
83 * Draw everywhere (respecting the clip) with the paint.
84 */
robertphillips2e1e51f2015-10-15 08:01:48 -070085 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -070086
87 /**
88 * Draw the rect using a paint.
89 * @param paint describes how to color pixels.
90 * @param viewMatrix transformation matrix
91 * @param strokeInfo the stroke information (width, join, cap), and.
92 * the dash information (intervals, count, phase).
93 * If strokeInfo == NULL, then the rect is filled.
94 * Otherwise, if stroke width == 0, then the stroke
95 * is always a single pixel thick, else the rect is
96 * mitered/beveled stroked based on stroke width.
97 * The rects coords are used to access the paint (through texture matrix)
98 */
robertphillips2e1e51f2015-10-15 08:01:48 -070099 void drawRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700100 const GrPaint& paint,
101 const SkMatrix& viewMatrix,
102 const SkRect&,
103 const GrStrokeInfo* strokeInfo = NULL);
104
105 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800106 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
robertphillipsea461502015-05-26 11:38:03 -0700107 *
108 * @param paint describes how to color pixels.
109 * @param viewMatrix transformation matrix which applies to rectToDraw
110 * @param rectToDraw the rectangle to draw
111 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700112 */
bsalomona2e69fc2015-11-05 10:41:43 -0800113 void fillRectToRect(const GrClip&,
114 const GrPaint& paint,
115 const SkMatrix& viewMatrix,
116 const SkRect& rectToDraw,
117 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700118
119 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800120 * Fills a rect with a paint and a localMatrix.
robertphillipsea461502015-05-26 11:38:03 -0700121 */
bsalomona2e69fc2015-11-05 10:41:43 -0800122 void fillRectWithLocalMatrix(const GrClip& clip,
123 const GrPaint& paint,
124 const SkMatrix& viewMatrix,
125 const SkRect& rect,
126 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700127
128 /**
129 * Draw a roundrect using a paint.
130 *
131 * @param paint describes how to color pixels.
132 * @param viewMatrix transformation matrix
133 * @param rrect the roundrect to draw
134 * @param strokeInfo the stroke information (width, join, cap) and
135 * the dash information (intervals, count, phase).
136 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700137 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700138 const GrPaint&,
139 const SkMatrix& viewMatrix,
140 const SkRRect& rrect,
141 const GrStrokeInfo&);
142
143 /**
144 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
145 * Does not support stroking. The result is undefined if outer does not contain
146 * inner.
147 *
148 * @param paint describes how to color pixels.
149 * @param viewMatrix transformation matrix
150 * @param outer the outer roundrect
151 * @param inner the inner roundrect
152 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700153 void drawDRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700154 const GrPaint&,
155 const SkMatrix& viewMatrix,
156 const SkRRect& outer,
157 const SkRRect& inner);
158
159
160 /**
161 * Draws a path.
162 *
163 * @param paint describes how to color pixels.
164 * @param viewMatrix transformation matrix
165 * @param path the path to draw
166 * @param strokeInfo the stroke information (width, join, cap) and
167 * the dash information (intervals, count, phase).
168 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700169 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700170 const GrPaint&,
171 const SkMatrix& viewMatrix,
172 const SkPath&,
173 const GrStrokeInfo&);
174
175 /**
176 * Draws vertices with a paint.
177 *
178 * @param paint describes how to color pixels.
179 * @param viewMatrix transformation matrix
180 * @param primitiveType primitives type to draw.
181 * @param vertexCount number of vertices.
182 * @param positions array of vertex positions, required.
183 * @param texCoords optional array of texture coordinates used
184 * to access the paint.
185 * @param colors optional array of per-vertex colors, supercedes
186 * the paint's color field.
187 * @param indices optional array of indices. If NULL vertices
188 * are drawn non-indexed.
189 * @param indexCount if indices is non-null then this is the
190 * number of indices.
191 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700192 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700193 const GrPaint& paint,
194 const SkMatrix& viewMatrix,
195 GrPrimitiveType primitiveType,
196 int vertexCount,
197 const SkPoint positions[],
198 const SkPoint texs[],
199 const GrColor colors[],
200 const uint16_t indices[],
201 int indexCount);
202
203 /**
jvanverth31ff7622015-08-07 10:09:28 -0700204 * Draws textured sprites from an atlas with a paint.
205 *
206 * @param paint describes how to color pixels.
207 * @param viewMatrix transformation matrix
208 * @param spriteCount number of sprites.
209 * @param xform array of compressed transformation data, required.
210 * @param texRect array of texture rectangles used to access the paint.
211 * @param colors optional array of per-sprite colors, supercedes
212 * the paint's color field.
213 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700214 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700215 const GrPaint& paint,
216 const SkMatrix& viewMatrix,
217 int spriteCount,
218 const SkRSXform xform[],
219 const SkRect texRect[],
220 const SkColor colors[]);
221
222 /**
robertphillipsea461502015-05-26 11:38:03 -0700223 * Draws an oval.
224 *
225 * @param paint describes how to color pixels.
226 * @param viewMatrix transformation matrix
227 * @param oval the bounding rect of the oval.
228 * @param strokeInfo the stroke information (width, join, cap) and
229 * the dash information (intervals, count, phase).
230 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700231 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700232 const GrPaint& paint,
233 const SkMatrix& viewMatrix,
234 const SkRect& oval,
235 const GrStrokeInfo& strokeInfo);
236
joshualitt33a5fce2015-11-18 13:28:51 -0800237 /**
238 * Draw the image stretched differentially to fit into dst.
239 * center is a rect within the image, and logically divides the image
240 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
241 * image is the "center", then the center-rect should be [2, 2, 3, 3].
242 *
243 * If the dst is >= the image size, then...
244 * - The 4 corners are not stretched at all.
245 * - The sides are stretched in only one axis.
246 * - The center is stretched in both axes.
247 * Else, for each axis where dst < image,
248 * - The corners shrink proportionally
249 * - The sides (along the shrink axis) and center are not drawn
250 */
251 void drawImageNine(const GrClip&,
252 const GrPaint& paint,
253 const SkMatrix& viewMatrix,
254 int imageWidth,
255 int imageHeight,
256 const SkIRect& center,
257 const SkRect& dst);
robertphillipsea461502015-05-26 11:38:03 -0700258
joshualittb7ee1bf2015-08-10 11:59:02 -0700259 /**
260 * Draws a batch
261 *
262 * @param paint describes how to color pixels.
263 * @param batch the batch to draw
264 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700265 void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700266
cdalton8ff8d242015-12-08 10:20:32 -0800267 /**
268 * Draws a path batch. This needs to be separate from drawBatch because we install path stencil
269 * settings late.
270 *
271 * TODO: Figure out a better model that allows us to roll this method into drawBatch.
272 */
273 void drawPathBatch(const GrPipelineBuilder&, GrDrawPathBatchBase*);
274
robertphillips7bceedc2015-12-01 12:51:26 -0800275 int width() const { return fRenderTarget->width(); }
276 int height() const { return fRenderTarget->height(); }
277 int numColorSamples() const { return fRenderTarget->numColorSamples(); }
278
robertphillips433625e2015-12-04 06:58:16 -0800279 GrRenderTarget* accessRenderTarget() { return fRenderTarget; }
280
joshualittf5883a62016-01-13 07:47:38 -0800281 ///////////////////////////////////////////////////////////////////////////////////////////////
282 // Functions intended for internal use only.
283 void internal_drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch);
284
joshualitt96880d92016-02-16 10:36:53 -0800285protected:
286 GrDrawContext(GrContext*, GrDrawingManager*, GrRenderTarget*,
287 const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
288
289 GrDrawingManager* drawingManager() { return fDrawingManager; }
290 GrAuditTrail* auditTrail() { return fAuditTrail; }
291 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
292
293 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
294 SkDEBUGCODE(void validate() const;)
295
robertphillipsea461502015-05-26 11:38:03 -0700296private:
joshualitt2e2202e2015-12-10 11:22:08 -0800297 friend class GrAtlasTextBlob; // for access to drawBatch
robertphillips77a2e522015-10-17 07:43:27 -0700298 friend class GrDrawingManager; // for ctor
robertphillipsea461502015-05-26 11:38:03 -0700299
robertphillipsb83bec52015-10-23 09:38:03 -0700300 void internalDrawPath(GrPipelineBuilder*,
robertphillipsea461502015-05-26 11:38:03 -0700301 const SkMatrix& viewMatrix,
302 GrColor,
303 bool useAA,
304 const SkPath&,
305 const GrStrokeInfo&);
306
robertphillips2334fb62015-06-17 05:43:33 -0700307 // This entry point allows the GrTextContext-derived classes to add their batches to
308 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700309 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700310
robertphillipsa106c622015-10-16 09:07:06 -0700311 GrDrawTarget* getDrawTarget();
312
joshualitt8db86782016-02-17 05:40:00 -0800313 GrDrawingManager* fDrawingManager;
314 GrRenderTarget* fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700315
316 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
317 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
joshualitt8db86782016-02-17 05:40:00 -0800318 GrDrawTarget* fDrawTarget;
319 SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
320 GrContext* fContext;
robertphillips2334fb62015-06-17 05:43:33 -0700321
joshualitt8db86782016-02-17 05:40:00 -0800322 SkSurfaceProps fSurfaceProps;
323 GrAuditTrail* fAuditTrail;
joshualitt1de610a2016-01-06 08:26:09 -0800324
325 // In debug builds we guard against improper thread handling
joshualitt6d0872d2016-01-11 08:27:48 -0800326 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
robertphillipsea461502015-05-26 11:38:03 -0700327};
328
329#endif