blob: f56570b10beaf3cd3ca2e2fb8ca6a2a42f3fee92 [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"
csmartdaltonecbc12b2016-06-08 10:08:43 -070012#include "GrPaint.h"
robertphillips7bceedc2015-12-01 12:51:26 -080013#include "GrRenderTarget.h"
robertphillipsea461502015-05-26 11:38:03 -070014#include "SkRefCnt.h"
robertphillips391395d2016-03-02 09:26:36 -080015#include "SkRegion.h"
robertphillipsfcf78292015-06-19 11:49:52 -070016#include "SkSurfaceProps.h"
joshualitt1de610a2016-01-06 08:26:09 -080017#include "../private/GrSingleOwner.h"
robertphillipsea461502015-05-26 11:38:03 -070018
joshualitt8e84a1e2016-02-16 11:09:25 -080019class GrAtlasTextContext;
joshualittbc907352016-01-13 06:45:40 -080020class GrAuditTrail;
robertphillipsea461502015-05-26 11:38:03 -070021class GrClip;
22class GrContext;
bsalomonabd30f52015-08-13 13:34:48 -070023class GrDrawBatch;
robertphillips391395d2016-03-02 09:26:36 -080024class GrDrawContextPriv;
cdalton8ff8d242015-12-08 10:20:32 -080025class GrDrawPathBatchBase;
robertphillips77a2e522015-10-17 07:43:27 -070026class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070027class GrDrawTarget;
28class GrPaint;
29class GrPathProcessor;
robertphillipsea461502015-05-26 11:38:03 -070030class GrPipelineBuilder;
31class GrRenderTarget;
bsalomon6663acf2016-05-10 09:14:17 -070032class GrStyle;
robertphillipsea461502015-05-26 11:38:03 -070033class GrSurface;
robertphillips2334fb62015-06-17 05:43:33 -070034class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070035struct SkIPoint;
36struct SkIRect;
37class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070038class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070039class SkPath;
40struct SkPoint;
41struct SkRect;
42class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070043struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070044class SkTextBlob;
robertphillipsea461502015-05-26 11:38:03 -070045
46/*
47 * A helper object to orchestrate draws
48 */
49class SK_API GrDrawContext : public SkRefCnt {
50public:
robertphillips2334fb62015-06-17 05:43:33 -070051 ~GrDrawContext() override;
52
bsalomonb8fea972016-02-16 07:34:17 -080053 bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
robertphillipsea461502015-05-26 11:38:03 -070054
robertphillips2334fb62015-06-17 05:43:33 -070055 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
56 // We should extract the text parameters from SkPaint and pass them separately
bsalomon6663acf2016-05-10 09:14:17 -070057 // akin to GrStyle (GrTextInfo?)
joshualitt96880d92016-02-16 10:36:53 -080058 virtual void drawText(const GrClip&, const GrPaint&, const SkPaint&,
59 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
60 SkScalar x, SkScalar y, const SkIRect& clipBounds);
61 virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
62 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
63 const SkScalar pos[], int scalarsPerPosition,
64 const SkPoint& offset, const SkIRect& clipBounds);
65 virtual void drawTextBlob(const GrClip&, const SkPaint&,
66 const SkMatrix& viewMatrix, const SkTextBlob*,
67 SkScalar x, SkScalar y,
68 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070069
robertphillipsea461502015-05-26 11:38:03 -070070 /**
71 * Provides a perfomance hint that the render target's contents are allowed
72 * to become undefined.
73 */
robertphillips2e1e51f2015-10-15 08:01:48 -070074 void discard();
robertphillipsea461502015-05-26 11:38:03 -070075
76 /**
77 * Clear the entire or rect of the render target, ignoring any clips.
robertphillipsea461502015-05-26 11:38:03 -070078 * @param rect the rect to clear or the whole thing if rect is NULL.
79 * @param color the color to clear to.
80 * @param canIgnoreRect allows partial clears to be converted to whole
81 * clears on platforms for which that is cheap
82 */
robertphillips2e1e51f2015-10-15 08:01:48 -070083 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
robertphillipsea461502015-05-26 11:38:03 -070084
85 /**
86 * Draw everywhere (respecting the clip) with the paint.
87 */
robertphillips2e1e51f2015-10-15 08:01:48 -070088 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -070089
90 /**
91 * Draw the rect using a paint.
92 * @param paint describes how to color pixels.
93 * @param viewMatrix transformation matrix
bsalomon6663acf2016-05-10 09:14:17 -070094 * @param style The style to apply. Null means fill. Currently path effects are not
95 * allowed.
robertphillipsea461502015-05-26 11:38:03 -070096 * The rects coords are used to access the paint (through texture matrix)
97 */
robertphillips2e1e51f2015-10-15 08:01:48 -070098 void drawRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -070099 const GrPaint& paint,
100 const SkMatrix& viewMatrix,
101 const SkRect&,
bsalomon6663acf2016-05-10 09:14:17 -0700102 const GrStyle* style = nullptr);
robertphillipsea461502015-05-26 11:38:03 -0700103
104 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800105 * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle.
robertphillipsea461502015-05-26 11:38:03 -0700106 *
107 * @param paint describes how to color pixels.
108 * @param viewMatrix transformation matrix which applies to rectToDraw
109 * @param rectToDraw the rectangle to draw
110 * @param localRect the rectangle of shader coordinates applied to rectToDraw
robertphillipsea461502015-05-26 11:38:03 -0700111 */
bsalomona2e69fc2015-11-05 10:41:43 -0800112 void fillRectToRect(const GrClip&,
113 const GrPaint& paint,
114 const SkMatrix& viewMatrix,
115 const SkRect& rectToDraw,
116 const SkRect& localRect);
robertphillipsea461502015-05-26 11:38:03 -0700117
118 /**
bsalomona2e69fc2015-11-05 10:41:43 -0800119 * Fills a rect with a paint and a localMatrix.
robertphillipsea461502015-05-26 11:38:03 -0700120 */
bsalomona2e69fc2015-11-05 10:41:43 -0800121 void fillRectWithLocalMatrix(const GrClip& clip,
122 const GrPaint& paint,
123 const SkMatrix& viewMatrix,
124 const SkRect& rect,
125 const SkMatrix& localMatrix);
robertphillipsea461502015-05-26 11:38:03 -0700126
127 /**
128 * Draw a roundrect using a paint.
129 *
130 * @param paint describes how to color pixels.
131 * @param viewMatrix transformation matrix
132 * @param rrect the roundrect to draw
bsalomon6663acf2016-05-10 09:14:17 -0700133 * @param style style to apply to the rrect. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700134 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700135 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700136 const GrPaint&,
137 const SkMatrix& viewMatrix,
138 const SkRRect& rrect,
bsalomon6663acf2016-05-10 09:14:17 -0700139 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700140
141 /**
robertphillips00095892016-02-29 13:50:40 -0800142 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
143 * Does not support stroking. The result is undefined if outer does not contain
144 * inner.
145 *
146 * @param paint describes how to color pixels.
147 * @param viewMatrix transformation matrix
148 * @param outer the outer roundrect
149 * @param inner the inner roundrect
150 */
151 void drawDRRect(const GrClip&,
152 const GrPaint&,
153 const SkMatrix& viewMatrix,
154 const SkRRect& outer,
155 const SkRRect& inner);
156
157 /**
robertphillipsea461502015-05-26 11:38:03 -0700158 * Draws a path.
159 *
160 * @param paint describes how to color pixels.
161 * @param viewMatrix transformation matrix
162 * @param path the path to draw
bsalomon6663acf2016-05-10 09:14:17 -0700163 * @param style style to apply to the path.
robertphillipsea461502015-05-26 11:38:03 -0700164 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700165 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700166 const GrPaint&,
167 const SkMatrix& viewMatrix,
168 const SkPath&,
bsalomon6663acf2016-05-10 09:14:17 -0700169 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700170
171 /**
172 * Draws vertices with a paint.
173 *
174 * @param paint describes how to color pixels.
175 * @param viewMatrix transformation matrix
176 * @param primitiveType primitives type to draw.
177 * @param vertexCount number of vertices.
178 * @param positions array of vertex positions, required.
179 * @param texCoords optional array of texture coordinates used
180 * to access the paint.
181 * @param colors optional array of per-vertex colors, supercedes
182 * the paint's color field.
183 * @param indices optional array of indices. If NULL vertices
184 * are drawn non-indexed.
185 * @param indexCount if indices is non-null then this is the
186 * number of indices.
187 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700188 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700189 const GrPaint& paint,
190 const SkMatrix& viewMatrix,
191 GrPrimitiveType primitiveType,
192 int vertexCount,
193 const SkPoint positions[],
194 const SkPoint texs[],
195 const GrColor colors[],
196 const uint16_t indices[],
197 int indexCount);
198
199 /**
jvanverth31ff7622015-08-07 10:09:28 -0700200 * Draws textured sprites from an atlas with a paint.
201 *
202 * @param paint describes how to color pixels.
203 * @param viewMatrix transformation matrix
204 * @param spriteCount number of sprites.
205 * @param xform array of compressed transformation data, required.
206 * @param texRect array of texture rectangles used to access the paint.
207 * @param colors optional array of per-sprite colors, supercedes
208 * the paint's color field.
209 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700210 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700211 const GrPaint& paint,
212 const SkMatrix& viewMatrix,
213 int spriteCount,
214 const SkRSXform xform[],
215 const SkRect texRect[],
216 const SkColor colors[]);
217
218 /**
robertphillipsea461502015-05-26 11:38:03 -0700219 * Draws an oval.
220 *
221 * @param paint describes how to color pixels.
222 * @param viewMatrix transformation matrix
223 * @param oval the bounding rect of the oval.
bsalomon6663acf2016-05-10 09:14:17 -0700224 * @param style style to apply to the oval. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700225 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700226 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700227 const GrPaint& paint,
228 const SkMatrix& viewMatrix,
229 const SkRect& oval,
bsalomon6663acf2016-05-10 09:14:17 -0700230 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700231
joshualitt33a5fce2015-11-18 13:28:51 -0800232 /**
233 * Draw the image stretched differentially to fit into dst.
234 * center is a rect within the image, and logically divides the image
235 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
236 * image is the "center", then the center-rect should be [2, 2, 3, 3].
237 *
238 * If the dst is >= the image size, then...
239 * - The 4 corners are not stretched at all.
240 * - The sides are stretched in only one axis.
241 * - The center is stretched in both axes.
242 * Else, for each axis where dst < image,
243 * - The corners shrink proportionally
244 * - The sides (along the shrink axis) and center are not drawn
245 */
246 void drawImageNine(const GrClip&,
247 const GrPaint& paint,
248 const SkMatrix& viewMatrix,
249 int imageWidth,
250 int imageHeight,
251 const SkIRect& center,
252 const SkRect& dst);
robertphillipsea461502015-05-26 11:38:03 -0700253
robertphillips976f5f02016-06-03 10:59:20 -0700254 bool isStencilBufferMultisampled() const {
255 return fRenderTarget->isStencilBufferMultisampled();
256 }
robertphillips55fdccc2016-06-06 06:16:20 -0700257 bool isUnifiedMultisampled() const { return fRenderTarget->isUnifiedMultisampled(); }
robertphillips82ec6e52016-05-19 14:01:05 -0700258 bool hasMixedSamples() const { return fRenderTarget->hasMixedSamples(); }
259
csmartdaltonecbc12b2016-06-08 10:08:43 -0700260 bool mustUseHWAA(const GrPaint& paint) const {
261 return paint.isAntiAlias() && fRenderTarget->isUnifiedMultisampled();
262 }
263
robertphillipse5768742016-05-13 11:20:46 -0700264 const GrSurfaceDesc& desc() const { return fRenderTarget->desc(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800265 int width() const { return fRenderTarget->width(); }
266 int height() const { return fRenderTarget->height(); }
robertphillipsca6eafc2016-05-17 09:57:46 -0700267 GrPixelConfig config() const { return fRenderTarget->config(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800268 int numColorSamples() const { return fRenderTarget->numColorSamples(); }
brianosmanb461d342016-04-13 13:10:14 -0700269 bool isGammaCorrect() const { return fSurfaceProps.isGammaCorrect(); }
robertphillipsca6eafc2016-05-17 09:57:46 -0700270 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
robertphillips7bceedc2015-12-01 12:51:26 -0800271
robertphillips7761d612016-05-16 09:14:53 -0700272 bool wasAbandoned() const;
273
robertphillips6c7e3252016-04-27 10:47:51 -0700274 GrRenderTarget* accessRenderTarget() { return fRenderTarget.get(); }
275
276 sk_sp<GrRenderTarget> renderTarget() { return fRenderTarget; }
277
278 sk_sp<GrTexture> asTexture() { return sk_ref_sp(fRenderTarget->asTexture()); }
robertphillips433625e2015-12-04 06:58:16 -0800279
robertphillips391395d2016-03-02 09:26:36 -0800280 // Provides access to functions that aren't part of the public API.
281 GrDrawContextPriv drawContextPriv();
282 const GrDrawContextPriv drawContextPriv() const;
joshualittf5883a62016-01-13 07:47:38 -0800283
robertphillips976f5f02016-06-03 10:59:20 -0700284 GrAuditTrail* auditTrail() { return fAuditTrail; }
285
joshualitt96880d92016-02-16 10:36:53 -0800286protected:
robertphillips6c7e3252016-04-27 10:47:51 -0700287 GrDrawContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTarget>,
joshualitt96880d92016-02-16 10:36:53 -0800288 const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
289
290 GrDrawingManager* drawingManager() { return fDrawingManager; }
robertphillipsca6eafc2016-05-17 09:57:46 -0700291
joshualitt96880d92016-02-16 10:36:53 -0800292 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
293 SkDEBUGCODE(void validate() const;)
294
robertphillipsea461502015-05-26 11:38:03 -0700295private:
joshualitt2e2202e2015-12-10 11:22:08 -0800296 friend class GrAtlasTextBlob; // for access to drawBatch
cdalton193d9cf2016-05-12 11:52:02 -0700297 friend class GrStencilAndCoverTextContext; // for access to drawBatch
robertphillips55fdccc2016-06-06 06:16:20 -0700298
robertphillips77a2e522015-10-17 07:43:27 -0700299 friend class GrDrawingManager; // for ctor
robertphillips391395d2016-03-02 09:26:36 -0800300 friend class GrDrawContextPriv;
robertphillips87f15c82016-05-20 11:14:33 -0700301 friend class GrTestTarget; // for access to getDrawTarget
robertphillips976f5f02016-06-03 10:59:20 -0700302 friend class GrSWMaskHelper; // for access to drawBatch
303 friend class GrClipMaskManager; // for access to drawBatch
304
305 // All the path renderers currently make their own batches
306 friend class GrSoftwarePathRenderer; // for access to drawBatch
307 friend class GrAAConvexPathRenderer; // for access to drawBatch
308 friend class GrDashLinePathRenderer; // for access to drawBatch
309 friend class GrAAHairLinePathRenderer; // for access to drawBatch
310 friend class GrAALinearizingConvexPathRenderer; // for access to drawBatch
311 friend class GrAADistanceFieldPathRenderer; // for access to drawBatch
312 friend class GrDefaultPathRenderer; // for access to drawBatch
313 friend class GrPLSPathRenderer; // for access to drawBatch
314 friend class GrMSAAPathRenderer; // for access to drawBatch
315 friend class GrStencilAndCoverPathRenderer; // for access to drawBatch
316 friend class GrTessellatingPathRenderer; // for access to drawBatch
robertphillipsea461502015-05-26 11:38:03 -0700317
robertphillips00095892016-02-29 13:50:40 -0800318 bool drawFilledDRRect(const GrClip& clip,
319 const GrPaint& paint,
320 const SkMatrix& viewMatrix,
321 const SkRRect& origOuter,
322 const SkRRect& origInner);
323
robertphillips391395d2016-03-02 09:26:36 -0800324 GrDrawBatch* getFillRectBatch(const GrPaint& paint,
325 const SkMatrix& viewMatrix,
csmartdaltonecbc12b2016-06-08 10:08:43 -0700326 const SkRect& rect,
327 bool* useHWAA);
robertphillips391395d2016-03-02 09:26:36 -0800328
robertphillips4bc31812016-03-01 12:22:49 -0800329 void internalDrawPath(const GrClip& clip,
330 const GrPaint& paint,
robertphillipsea461502015-05-26 11:38:03 -0700331 const SkMatrix& viewMatrix,
robertphillips4bc31812016-03-01 12:22:49 -0800332 const SkPath& path,
bsalomon6663acf2016-05-10 09:14:17 -0700333 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700334
robertphillips2334fb62015-06-17 05:43:33 -0700335 // This entry point allows the GrTextContext-derived classes to add their batches to
336 // the drawTarget.
robertphillips976f5f02016-06-03 10:59:20 -0700337 void drawBatch(const GrPipelineBuilder& pipelineBuilder, const GrClip&, GrDrawBatch* batch);
robertphillips2334fb62015-06-17 05:43:33 -0700338
robertphillipsa106c622015-10-16 09:07:06 -0700339 GrDrawTarget* getDrawTarget();
340
joshualitt8db86782016-02-17 05:40:00 -0800341 GrDrawingManager* fDrawingManager;
robertphillips6c7e3252016-04-27 10:47:51 -0700342 sk_sp<GrRenderTarget> fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700343
344 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
345 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
joshualitt8db86782016-02-17 05:40:00 -0800346 GrDrawTarget* fDrawTarget;
347 SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
348 GrContext* fContext;
robertphillips2334fb62015-06-17 05:43:33 -0700349
joshualitt8db86782016-02-17 05:40:00 -0800350 SkSurfaceProps fSurfaceProps;
351 GrAuditTrail* fAuditTrail;
joshualitt1de610a2016-01-06 08:26:09 -0800352
353 // In debug builds we guard against improper thread handling
joshualitt6d0872d2016-01-11 08:27:48 -0800354 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
robertphillipsea461502015-05-26 11:38:03 -0700355};
356
357#endif