blob: 1f5b3d2d2b988f1f8b27a36b61e73dbdfd3925ec [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;
bsalomon6663acf2016-05-10 09:14:17 -070031class GrStyle;
robertphillipsea461502015-05-26 11:38:03 -070032class 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
bsalomon6663acf2016-05-10 09:14:17 -070056 // akin to GrStyle (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
bsalomon6663acf2016-05-10 09:14:17 -070093 * @param style The style to apply. Null means fill. Currently path effects are not
94 * allowed.
robertphillipsea461502015-05-26 11:38:03 -070095 * The rects coords are used to access the paint (through texture matrix)
96 */
robertphillips2e1e51f2015-10-15 08:01:48 -070097 void drawRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -070098 const GrPaint& paint,
99 const SkMatrix& viewMatrix,
100 const SkRect&,
bsalomon6663acf2016-05-10 09:14:17 -0700101 const GrStyle* style = nullptr);
robertphillipsea461502015-05-26 11:38:03 -0700102
103 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800104 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
robertphillipsea461502015-05-26 11:38:03 -0700105 *
106 * @param paint describes how to color pixels.
107 * @param viewMatrix transformation matrix which applies to rectToDraw
108 * @param rectToDraw the rectangle to draw
109 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700110 */
bsalomona2e69fc2015-11-05 10:41:43 -0800111 void fillRectToRect(const GrClip&,
112 const GrPaint& paint,
113 const SkMatrix& viewMatrix,
114 const SkRect& rectToDraw,
115 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700116
117 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800118 * Fills a rect with a paint and a localMatrix.
robertphillipsea461502015-05-26 11:38:03 -0700119 */
bsalomona2e69fc2015-11-05 10:41:43 -0800120 void fillRectWithLocalMatrix(const GrClip& clip,
121 const GrPaint& paint,
122 const SkMatrix& viewMatrix,
123 const SkRect& rect,
124 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700125
126 /**
127 * Draw a roundrect using a paint.
128 *
129 * @param paint describes how to color pixels.
130 * @param viewMatrix transformation matrix
131 * @param rrect the roundrect to draw
bsalomon6663acf2016-05-10 09:14:17 -0700132 * @param style style to apply to the rrect. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700133 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700134 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700135 const GrPaint&,
136 const SkMatrix& viewMatrix,
137 const SkRRect& rrect,
bsalomon6663acf2016-05-10 09:14:17 -0700138 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700139
140 /**
robertphillips00095892016-02-29 13:50:40 -0800141 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
142 * Does not support stroking. The result is undefined if outer does not contain
143 * inner.
144 *
145 * @param paint describes how to color pixels.
146 * @param viewMatrix transformation matrix
147 * @param outer the outer roundrect
148 * @param inner the inner roundrect
149 */
150 void drawDRRect(const GrClip&,
151 const GrPaint&,
152 const SkMatrix& viewMatrix,
153 const SkRRect& outer,
154 const SkRRect& inner);
155
156 /**
robertphillipsea461502015-05-26 11:38:03 -0700157 * Draws a path.
158 *
159 * @param paint describes how to color pixels.
160 * @param viewMatrix transformation matrix
161 * @param path the path to draw
bsalomon6663acf2016-05-10 09:14:17 -0700162 * @param style style to apply to the path.
robertphillipsea461502015-05-26 11:38:03 -0700163 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700164 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700165 const GrPaint&,
166 const SkMatrix& viewMatrix,
167 const SkPath&,
bsalomon6663acf2016-05-10 09:14:17 -0700168 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700169
170 /**
171 * Draws vertices with a paint.
172 *
173 * @param paint describes how to color pixels.
174 * @param viewMatrix transformation matrix
175 * @param primitiveType primitives type to draw.
176 * @param vertexCount number of vertices.
177 * @param positions array of vertex positions, required.
178 * @param texCoords optional array of texture coordinates used
179 * to access the paint.
180 * @param colors optional array of per-vertex colors, supercedes
181 * the paint's color field.
182 * @param indices optional array of indices. If NULL vertices
183 * are drawn non-indexed.
184 * @param indexCount if indices is non-null then this is the
185 * number of indices.
186 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700187 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700188 const GrPaint& paint,
189 const SkMatrix& viewMatrix,
190 GrPrimitiveType primitiveType,
191 int vertexCount,
192 const SkPoint positions[],
193 const SkPoint texs[],
194 const GrColor colors[],
195 const uint16_t indices[],
196 int indexCount);
197
198 /**
jvanverth31ff7622015-08-07 10:09:28 -0700199 * Draws textured sprites from an atlas with a paint.
200 *
201 * @param paint describes how to color pixels.
202 * @param viewMatrix transformation matrix
203 * @param spriteCount number of sprites.
204 * @param xform array of compressed transformation data, required.
205 * @param texRect array of texture rectangles used to access the paint.
206 * @param colors optional array of per-sprite colors, supercedes
207 * the paint's color field.
208 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700209 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700210 const GrPaint& paint,
211 const SkMatrix& viewMatrix,
212 int spriteCount,
213 const SkRSXform xform[],
214 const SkRect texRect[],
215 const SkColor colors[]);
216
217 /**
robertphillipsea461502015-05-26 11:38:03 -0700218 * Draws an oval.
219 *
220 * @param paint describes how to color pixels.
221 * @param viewMatrix transformation matrix
222 * @param oval the bounding rect of the oval.
bsalomon6663acf2016-05-10 09:14:17 -0700223 * @param style style to apply to the oval. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700224 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700225 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700226 const GrPaint& paint,
227 const SkMatrix& viewMatrix,
228 const SkRect& oval,
bsalomon6663acf2016-05-10 09:14:17 -0700229 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700230
joshualitt33a5fce2015-11-18 13:28:51 -0800231 /**
232 * Draw the image stretched differentially to fit into dst.
233 * center is a rect within the image, and logically divides the image
234 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
235 * image is the "center", then the center-rect should be [2, 2, 3, 3].
236 *
237 * If the dst is >= the image size, then...
238 * - The 4 corners are not stretched at all.
239 * - The sides are stretched in only one axis.
240 * - The center is stretched in both axes.
241 * Else, for each axis where dst < image,
242 * - The corners shrink proportionally
243 * - The sides (along the shrink axis) and center are not drawn
244 */
245 void drawImageNine(const GrClip&,
246 const GrPaint& paint,
247 const SkMatrix& viewMatrix,
248 int imageWidth,
249 int imageHeight,
250 const SkIRect& center,
251 const SkRect& dst);
robertphillipsea461502015-05-26 11:38:03 -0700252
joshualittb7ee1bf2015-08-10 11:59:02 -0700253 /**
254 * Draws a batch
255 *
256 * @param paint describes how to color pixels.
257 * @param batch the batch to draw
258 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700259 void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*);
joshualittb7ee1bf2015-08-10 11:59:02 -0700260
robertphillips7bceedc2015-12-01 12:51:26 -0800261 int width() const { return fRenderTarget->width(); }
262 int height() const { return fRenderTarget->height(); }
263 int numColorSamples() const { return fRenderTarget->numColorSamples(); }
brianosmanb461d342016-04-13 13:10:14 -0700264 bool isGammaCorrect() const { return fSurfaceProps.isGammaCorrect(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800265
robertphillips6c7e3252016-04-27 10:47:51 -0700266 GrRenderTarget* accessRenderTarget() { return fRenderTarget.get(); }
267
268 sk_sp<GrRenderTarget> renderTarget() { return fRenderTarget; }
269
270 sk_sp<GrTexture> asTexture() { return sk_ref_sp(fRenderTarget->asTexture()); }
robertphillips433625e2015-12-04 06:58:16 -0800271
robertphillips391395d2016-03-02 09:26:36 -0800272 // Provides access to functions that aren't part of the public API.
273 GrDrawContextPriv drawContextPriv();
274 const GrDrawContextPriv drawContextPriv() const;
joshualittf5883a62016-01-13 07:47:38 -0800275
joshualitt96880d92016-02-16 10:36:53 -0800276protected:
robertphillips6c7e3252016-04-27 10:47:51 -0700277 GrDrawContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTarget>,
joshualitt96880d92016-02-16 10:36:53 -0800278 const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
279
280 GrDrawingManager* drawingManager() { return fDrawingManager; }
281 GrAuditTrail* auditTrail() { return fAuditTrail; }
282 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
283
284 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
285 SkDEBUGCODE(void validate() const;)
286
robertphillipsea461502015-05-26 11:38:03 -0700287private:
joshualitt2e2202e2015-12-10 11:22:08 -0800288 friend class GrAtlasTextBlob; // for access to drawBatch
cdalton193d9cf2016-05-12 11:52:02 -0700289 friend class GrStencilAndCoverTextContext; // for access to drawBatch
robertphillips77a2e522015-10-17 07:43:27 -0700290 friend class GrDrawingManager; // for ctor
robertphillips391395d2016-03-02 09:26:36 -0800291 friend class GrDrawContextPriv;
robertphillipsea461502015-05-26 11:38:03 -0700292
robertphillips00095892016-02-29 13:50:40 -0800293 bool drawFilledDRRect(const GrClip& clip,
294 const GrPaint& paint,
295 const SkMatrix& viewMatrix,
296 const SkRRect& origOuter,
297 const SkRRect& origInner);
298
robertphillips391395d2016-03-02 09:26:36 -0800299 GrDrawBatch* getFillRectBatch(const GrPaint& paint,
300 const SkMatrix& viewMatrix,
301 const SkRect& rect);
302
robertphillips4bc31812016-03-01 12:22:49 -0800303 void internalDrawPath(const GrClip& clip,
304 const GrPaint& paint,
robertphillipsea461502015-05-26 11:38:03 -0700305 const SkMatrix& viewMatrix,
robertphillips4bc31812016-03-01 12:22:49 -0800306 const SkPath& path,
bsalomon6663acf2016-05-10 09:14:17 -0700307 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700308
robertphillips2334fb62015-06-17 05:43:33 -0700309 // This entry point allows the GrTextContext-derived classes to add their batches to
310 // the drawTarget.
bsalomonabd30f52015-08-13 13:34:48 -0700311 void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700312
robertphillipsa106c622015-10-16 09:07:06 -0700313 GrDrawTarget* getDrawTarget();
314
joshualitt8db86782016-02-17 05:40:00 -0800315 GrDrawingManager* fDrawingManager;
robertphillips6c7e3252016-04-27 10:47:51 -0700316 sk_sp<GrRenderTarget> fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700317
318 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
319 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
joshualitt8db86782016-02-17 05:40:00 -0800320 GrDrawTarget* fDrawTarget;
321 SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
322 GrContext* fContext;
robertphillips2334fb62015-06-17 05:43:33 -0700323
joshualitt8db86782016-02-17 05:40:00 -0800324 SkSurfaceProps fSurfaceProps;
325 GrAuditTrail* fAuditTrail;
joshualitt1de610a2016-01-06 08:26:09 -0800326
327 // In debug builds we guard against improper thread handling
joshualitt6d0872d2016-01-11 08:27:48 -0800328 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
robertphillipsea461502015-05-26 11:38:03 -0700329};
330
331#endif