blob: bf9a2fd476592172ad165719fba5cf3eb6b2669d [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"
csmartdaltona7f29642016-07-07 08:49:11 -070017#include "../private/GrInstancedPipelineInfo.h"
joshualitt1de610a2016-01-06 08:26:09 -080018#include "../private/GrSingleOwner.h"
robertphillipsea461502015-05-26 11:38:03 -070019
joshualitt8e84a1e2016-02-16 11:09:25 -080020class GrAtlasTextContext;
joshualittbc907352016-01-13 06:45:40 -080021class GrAuditTrail;
robertphillipsea461502015-05-26 11:38:03 -070022class GrClip;
23class GrContext;
bsalomonabd30f52015-08-13 13:34:48 -070024class GrDrawBatch;
robertphillips391395d2016-03-02 09:26:36 -080025class GrDrawContextPriv;
cdalton8ff8d242015-12-08 10:20:32 -080026class GrDrawPathBatchBase;
robertphillips77a2e522015-10-17 07:43:27 -070027class GrDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -070028class GrDrawTarget;
29class GrPaint;
30class GrPathProcessor;
robertphillipsea461502015-05-26 11:38:03 -070031class GrPipelineBuilder;
32class GrRenderTarget;
bsalomon6663acf2016-05-10 09:14:17 -070033class GrStyle;
robertphillipsea461502015-05-26 11:38:03 -070034class GrSurface;
robertphillips44302392016-07-08 14:43:03 -070035struct GrUserStencilSettings;
robertphillips2334fb62015-06-17 05:43:33 -070036class SkDrawFilter;
robertphillipsea461502015-05-26 11:38:03 -070037struct SkIPoint;
38struct SkIRect;
39class SkMatrix;
robertphillips2334fb62015-06-17 05:43:33 -070040class SkPaint;
robertphillipsea461502015-05-26 11:38:03 -070041class SkPath;
42struct SkPoint;
43struct SkRect;
44class SkRRect;
jvanverth31ff7622015-08-07 10:09:28 -070045struct SkRSXform;
robertphillips2334fb62015-06-17 05:43:33 -070046class SkTextBlob;
robertphillips2895eeb2016-07-21 12:04:08 -070047struct GrUserStencilSettings;
robertphillipsea461502015-05-26 11:38:03 -070048
49/*
50 * A helper object to orchestrate draws
51 */
52class SK_API GrDrawContext : public SkRefCnt {
53public:
robertphillips2334fb62015-06-17 05:43:33 -070054 ~GrDrawContext() override;
55
bsalomonb8fea972016-02-16 07:34:17 -080056 bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint);
robertphillipsea461502015-05-26 11:38:03 -070057
robertphillips2334fb62015-06-17 05:43:33 -070058 // TODO: it is odd that we need both the SkPaint in the following 3 methods.
59 // We should extract the text parameters from SkPaint and pass them separately
bsalomon6663acf2016-05-10 09:14:17 -070060 // akin to GrStyle (GrTextInfo?)
joshualitt96880d92016-02-16 10:36:53 -080061 virtual void drawText(const GrClip&, const GrPaint&, const SkPaint&,
62 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
63 SkScalar x, SkScalar y, const SkIRect& clipBounds);
64 virtual void drawPosText(const GrClip&, const GrPaint&, const SkPaint&,
65 const SkMatrix& viewMatrix, const char text[], size_t byteLength,
66 const SkScalar pos[], int scalarsPerPosition,
67 const SkPoint& offset, const SkIRect& clipBounds);
68 virtual void drawTextBlob(const GrClip&, const SkPaint&,
69 const SkMatrix& viewMatrix, const SkTextBlob*,
70 SkScalar x, SkScalar y,
71 SkDrawFilter*, const SkIRect& clipBounds);
robertphillipsea461502015-05-26 11:38:03 -070072
robertphillipsea461502015-05-26 11:38:03 -070073 /**
74 * Provides a perfomance hint that the render target's contents are allowed
75 * to become undefined.
76 */
robertphillips2e1e51f2015-10-15 08:01:48 -070077 void discard();
robertphillipsea461502015-05-26 11:38:03 -070078
79 /**
80 * Clear the entire or rect of the render target, ignoring any clips.
robertphillipsea461502015-05-26 11:38:03 -070081 * @param rect the rect to clear or the whole thing if rect is NULL.
82 * @param color the color to clear to.
83 * @param canIgnoreRect allows partial clears to be converted to whole
84 * clears on platforms for which that is cheap
85 */
robertphillips2e1e51f2015-10-15 08:01:48 -070086 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect);
robertphillipsea461502015-05-26 11:38:03 -070087
88 /**
89 * Draw everywhere (respecting the clip) with the paint.
90 */
robertphillips2e1e51f2015-10-15 08:01:48 -070091 void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
robertphillipsea461502015-05-26 11:38:03 -070092
93 /**
94 * Draw the rect using a paint.
95 * @param paint describes how to color pixels.
96 * @param viewMatrix transformation matrix
bsalomon6663acf2016-05-10 09:14:17 -070097 * @param style The style to apply. Null means fill. Currently path effects are not
98 * allowed.
robertphillipsea461502015-05-26 11:38:03 -070099 * 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&,
bsalomon6663acf2016-05-10 09:14:17 -0700105 const GrStyle* style = 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
bsalomon6663acf2016-05-10 09:14:17 -0700136 * @param style style to apply to the rrect. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700137 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700138 void drawRRect(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700139 const GrPaint&,
140 const SkMatrix& viewMatrix,
141 const SkRRect& rrect,
bsalomon6663acf2016-05-10 09:14:17 -0700142 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700143
144 /**
robertphillips00095892016-02-29 13:50:40 -0800145 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
146 * Does not support stroking. The result is undefined if outer does not contain
147 * inner.
148 *
149 * @param paint describes how to color pixels.
150 * @param viewMatrix transformation matrix
151 * @param outer the outer roundrect
152 * @param inner the inner roundrect
153 */
154 void drawDRRect(const GrClip&,
155 const GrPaint&,
156 const SkMatrix& viewMatrix,
157 const SkRRect& outer,
158 const SkRRect& inner);
159
160 /**
robertphillipsea461502015-05-26 11:38:03 -0700161 * Draws a path.
162 *
163 * @param paint describes how to color pixels.
164 * @param viewMatrix transformation matrix
165 * @param path the path to draw
bsalomon6663acf2016-05-10 09:14:17 -0700166 * @param style style to apply to the path.
robertphillipsea461502015-05-26 11:38:03 -0700167 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700168 void drawPath(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700169 const GrPaint&,
170 const SkMatrix& viewMatrix,
171 const SkPath&,
bsalomon6663acf2016-05-10 09:14:17 -0700172 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700173
174 /**
175 * Draws vertices with a paint.
176 *
177 * @param paint describes how to color pixels.
178 * @param viewMatrix transformation matrix
179 * @param primitiveType primitives type to draw.
180 * @param vertexCount number of vertices.
181 * @param positions array of vertex positions, required.
182 * @param texCoords optional array of texture coordinates used
183 * to access the paint.
184 * @param colors optional array of per-vertex colors, supercedes
185 * the paint's color field.
186 * @param indices optional array of indices. If NULL vertices
187 * are drawn non-indexed.
188 * @param indexCount if indices is non-null then this is the
189 * number of indices.
190 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700191 void drawVertices(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700192 const GrPaint& paint,
193 const SkMatrix& viewMatrix,
194 GrPrimitiveType primitiveType,
195 int vertexCount,
196 const SkPoint positions[],
197 const SkPoint texs[],
198 const GrColor colors[],
199 const uint16_t indices[],
200 int indexCount);
201
202 /**
jvanverth31ff7622015-08-07 10:09:28 -0700203 * Draws textured sprites from an atlas with a paint.
204 *
205 * @param paint describes how to color pixels.
206 * @param viewMatrix transformation matrix
207 * @param spriteCount number of sprites.
208 * @param xform array of compressed transformation data, required.
209 * @param texRect array of texture rectangles used to access the paint.
210 * @param colors optional array of per-sprite colors, supercedes
211 * the paint's color field.
212 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700213 void drawAtlas(const GrClip&,
jvanverth31ff7622015-08-07 10:09:28 -0700214 const GrPaint& paint,
215 const SkMatrix& viewMatrix,
216 int spriteCount,
217 const SkRSXform xform[],
218 const SkRect texRect[],
219 const SkColor colors[]);
220
221 /**
robertphillipsea461502015-05-26 11:38:03 -0700222 * Draws an oval.
223 *
224 * @param paint describes how to color pixels.
225 * @param viewMatrix transformation matrix
226 * @param oval the bounding rect of the oval.
bsalomon6663acf2016-05-10 09:14:17 -0700227 * @param style style to apply to the oval. Currently path effects are not allowed.
robertphillipsea461502015-05-26 11:38:03 -0700228 */
robertphillips2e1e51f2015-10-15 08:01:48 -0700229 void drawOval(const GrClip&,
robertphillipsea461502015-05-26 11:38:03 -0700230 const GrPaint& paint,
231 const SkMatrix& viewMatrix,
232 const SkRect& oval,
bsalomon6663acf2016-05-10 09:14:17 -0700233 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700234
joshualitt33a5fce2015-11-18 13:28:51 -0800235 /**
236 * Draw the image stretched differentially to fit into dst.
237 * center is a rect within the image, and logically divides the image
238 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
239 * image is the "center", then the center-rect should be [2, 2, 3, 3].
240 *
241 * If the dst is >= the image size, then...
242 * - The 4 corners are not stretched at all.
243 * - The sides are stretched in only one axis.
244 * - The center is stretched in both axes.
245 * Else, for each axis where dst < image,
246 * - The corners shrink proportionally
247 * - The sides (along the shrink axis) and center are not drawn
248 */
249 void drawImageNine(const GrClip&,
250 const GrPaint& paint,
251 const SkMatrix& viewMatrix,
252 int imageWidth,
253 int imageHeight,
254 const SkIRect& center,
255 const SkRect& dst);
robertphillipsea461502015-05-26 11:38:03 -0700256
robertphillips976f5f02016-06-03 10:59:20 -0700257 bool isStencilBufferMultisampled() const {
258 return fRenderTarget->isStencilBufferMultisampled();
259 }
robertphillips55fdccc2016-06-06 06:16:20 -0700260 bool isUnifiedMultisampled() const { return fRenderTarget->isUnifiedMultisampled(); }
robertphillips82ec6e52016-05-19 14:01:05 -0700261 bool hasMixedSamples() const { return fRenderTarget->hasMixedSamples(); }
262
csmartdaltonecbc12b2016-06-08 10:08:43 -0700263 bool mustUseHWAA(const GrPaint& paint) const {
robertphillips2895eeb2016-07-21 12:04:08 -0700264 return paint.isAntiAlias() &&
265 (fRenderTarget->isUnifiedMultisampled() || fRenderTarget->hasMixedSamples());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700266 }
267
robertphillipse5768742016-05-13 11:20:46 -0700268 const GrSurfaceDesc& desc() const { return fRenderTarget->desc(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800269 int width() const { return fRenderTarget->width(); }
270 int height() const { return fRenderTarget->height(); }
robertphillipsca6eafc2016-05-17 09:57:46 -0700271 GrPixelConfig config() const { return fRenderTarget->config(); }
robertphillips7bceedc2015-12-01 12:51:26 -0800272 int numColorSamples() const { return fRenderTarget->numColorSamples(); }
brianosmanb461d342016-04-13 13:10:14 -0700273 bool isGammaCorrect() const { return fSurfaceProps.isGammaCorrect(); }
robertphillipsca6eafc2016-05-17 09:57:46 -0700274 const SkSurfaceProps& surfaceProps() const { return fSurfaceProps; }
robertphillips7bceedc2015-12-01 12:51:26 -0800275
robertphillips7761d612016-05-16 09:14:53 -0700276 bool wasAbandoned() const;
277
robertphillips6c7e3252016-04-27 10:47:51 -0700278 GrRenderTarget* accessRenderTarget() { return fRenderTarget.get(); }
279
280 sk_sp<GrRenderTarget> renderTarget() { return fRenderTarget; }
281
282 sk_sp<GrTexture> asTexture() { return sk_ref_sp(fRenderTarget->asTexture()); }
robertphillips433625e2015-12-04 06:58:16 -0800283
robertphillips391395d2016-03-02 09:26:36 -0800284 // Provides access to functions that aren't part of the public API.
285 GrDrawContextPriv drawContextPriv();
286 const GrDrawContextPriv drawContextPriv() const;
joshualittf5883a62016-01-13 07:47:38 -0800287
robertphillips976f5f02016-06-03 10:59:20 -0700288 GrAuditTrail* auditTrail() { return fAuditTrail; }
289
joshualitt96880d92016-02-16 10:36:53 -0800290protected:
robertphillips6c7e3252016-04-27 10:47:51 -0700291 GrDrawContext(GrContext*, GrDrawingManager*, sk_sp<GrRenderTarget>,
joshualitt96880d92016-02-16 10:36:53 -0800292 const SkSurfaceProps* surfaceProps, GrAuditTrail*, GrSingleOwner*);
293
294 GrDrawingManager* drawingManager() { return fDrawingManager; }
robertphillipsca6eafc2016-05-17 09:57:46 -0700295
joshualitt96880d92016-02-16 10:36:53 -0800296 SkDEBUGCODE(GrSingleOwner* singleOwner() { return fSingleOwner; })
297 SkDEBUGCODE(void validate() const;)
298
robertphillipsea461502015-05-26 11:38:03 -0700299private:
joshualitt2e2202e2015-12-10 11:22:08 -0800300 friend class GrAtlasTextBlob; // for access to drawBatch
cdalton193d9cf2016-05-12 11:52:02 -0700301 friend class GrStencilAndCoverTextContext; // for access to drawBatch
robertphillips55fdccc2016-06-06 06:16:20 -0700302
robertphillips77a2e522015-10-17 07:43:27 -0700303 friend class GrDrawingManager; // for ctor
robertphillips391395d2016-03-02 09:26:36 -0800304 friend class GrDrawContextPriv;
robertphillips87f15c82016-05-20 11:14:33 -0700305 friend class GrTestTarget; // for access to getDrawTarget
robertphillips976f5f02016-06-03 10:59:20 -0700306 friend class GrSWMaskHelper; // for access to drawBatch
307 friend class GrClipMaskManager; // for access to drawBatch
308
309 // All the path renderers currently make their own batches
310 friend class GrSoftwarePathRenderer; // for access to drawBatch
311 friend class GrAAConvexPathRenderer; // for access to drawBatch
312 friend class GrDashLinePathRenderer; // for access to drawBatch
313 friend class GrAAHairLinePathRenderer; // for access to drawBatch
314 friend class GrAALinearizingConvexPathRenderer; // for access to drawBatch
315 friend class GrAADistanceFieldPathRenderer; // for access to drawBatch
316 friend class GrDefaultPathRenderer; // for access to drawBatch
317 friend class GrPLSPathRenderer; // for access to drawBatch
318 friend class GrMSAAPathRenderer; // for access to drawBatch
319 friend class GrStencilAndCoverPathRenderer; // for access to drawBatch
320 friend class GrTessellatingPathRenderer; // for access to drawBatch
robertphillipsea461502015-05-26 11:38:03 -0700321
robertphillips00095892016-02-29 13:50:40 -0800322 bool drawFilledDRRect(const GrClip& clip,
323 const GrPaint& paint,
324 const SkMatrix& viewMatrix,
325 const SkRRect& origOuter,
326 const SkRRect& origInner);
327
robertphillips44302392016-07-08 14:43:03 -0700328 bool drawFilledRect(const GrClip& clip,
329 const GrPaint& paint,
330 const SkMatrix& viewMatrix,
331 const SkRect& rect,
332 const GrUserStencilSettings* ss);
333
334 void drawNonAAFilledRect(const GrClip&,
335 const GrPaint&,
336 const SkMatrix& viewMatrix,
337 const SkRect& rect,
338 const SkRect* localRect,
339 const SkMatrix* localMatrix,
340 const GrUserStencilSettings* ss);
robertphillips391395d2016-03-02 09:26:36 -0800341
robertphillips4bc31812016-03-01 12:22:49 -0800342 void internalDrawPath(const GrClip& clip,
343 const GrPaint& paint,
robertphillipsea461502015-05-26 11:38:03 -0700344 const SkMatrix& viewMatrix,
robertphillips4bc31812016-03-01 12:22:49 -0800345 const SkPath& path,
bsalomon6663acf2016-05-10 09:14:17 -0700346 const GrStyle& style);
robertphillipsea461502015-05-26 11:38:03 -0700347
robertphillips2334fb62015-06-17 05:43:33 -0700348 // This entry point allows the GrTextContext-derived classes to add their batches to
349 // the drawTarget.
robertphillips2895eeb2016-07-21 12:04:08 -0700350 void drawBatch(const GrPaint&, const GrClip&, const GrUserStencilSettings&, GrDrawBatch* batch,
351 GrDrawFace = GrDrawFace::kBoth);
robertphillips2334fb62015-06-17 05:43:33 -0700352
robertphillipsa106c622015-10-16 09:07:06 -0700353 GrDrawTarget* getDrawTarget();
354
joshualitt8db86782016-02-17 05:40:00 -0800355 GrDrawingManager* fDrawingManager;
robertphillips6c7e3252016-04-27 10:47:51 -0700356 sk_sp<GrRenderTarget> fRenderTarget;
robertphillipsa106c622015-10-16 09:07:06 -0700357
358 // In MDB-mode the drawTarget can be closed by some other drawContext that has picked
359 // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'.
joshualitt8db86782016-02-17 05:40:00 -0800360 GrDrawTarget* fDrawTarget;
361 SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
362 GrContext* fContext;
csmartdaltona7f29642016-07-07 08:49:11 -0700363 GrInstancedPipelineInfo fInstancedPipelineInfo;
robertphillips2334fb62015-06-17 05:43:33 -0700364
joshualitt8db86782016-02-17 05:40:00 -0800365 SkSurfaceProps fSurfaceProps;
366 GrAuditTrail* fAuditTrail;
joshualitt1de610a2016-01-06 08:26:09 -0800367
368 // In debug builds we guard against improper thread handling
joshualitt6d0872d2016-01-11 08:27:48 -0800369 SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;)
robertphillipsea461502015-05-26 11:38:03 -0700370};
371
372#endif