robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 14 | #include "SkSurfaceProps.h" |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 15 | #include "../private/GrSingleOwner.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 16 | |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 17 | class GrAtlasTextContext; |
joshualitt | bc90735 | 2016-01-13 06:45:40 -0800 | [diff] [blame] | 18 | class GrAuditTrail; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 19 | class GrClip; |
| 20 | class GrContext; |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 21 | class GrDrawBatch; |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 22 | class GrDrawPathBatchBase; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 23 | class GrDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 24 | class GrDrawTarget; |
| 25 | class GrPaint; |
| 26 | class GrPathProcessor; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 27 | class GrPipelineBuilder; |
| 28 | class GrRenderTarget; |
| 29 | class GrStrokeInfo; |
| 30 | class GrSurface; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 31 | class SkDrawFilter; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 32 | struct SkIPoint; |
| 33 | struct SkIRect; |
| 34 | class SkMatrix; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 35 | class SkPaint; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 36 | class SkPath; |
| 37 | struct SkPoint; |
| 38 | struct SkRect; |
| 39 | class SkRRect; |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 40 | struct SkRSXform; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 41 | class SkTextBlob; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * A helper object to orchestrate draws |
| 45 | */ |
| 46 | class SK_API GrDrawContext : public SkRefCnt { |
| 47 | public: |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 48 | ~GrDrawContext() override; |
| 49 | |
bsalomon | b8fea97 | 2016-02-16 07:34:17 -0800 | [diff] [blame] | 50 | bool copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 51 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 52 | // 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?) |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 55 | 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); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 66 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Provides a perfomance hint that the render target's contents are allowed |
| 69 | * to become undefined. |
| 70 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 71 | void discard(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Clear the entire or rect of the render target, ignoring any clips. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 75 | * @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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 80 | void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Draw everywhere (respecting the clip) with the paint. |
| 84 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 85 | void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 86 | |
| 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 99 | void drawRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 100 | const GrPaint& paint, |
| 101 | const SkMatrix& viewMatrix, |
| 102 | const SkRect&, |
| 103 | const GrStrokeInfo* strokeInfo = NULL); |
| 104 | |
| 105 | /** |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 106 | * Maps a rectangle of shader coordinates to a rectangle and fills that rectangle. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 107 | * |
| 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 |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 112 | */ |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 113 | void fillRectToRect(const GrClip&, |
| 114 | const GrPaint& paint, |
| 115 | const SkMatrix& viewMatrix, |
| 116 | const SkRect& rectToDraw, |
| 117 | const SkRect& localRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 120 | * Fills a rect with a paint and a localMatrix. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 121 | */ |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 122 | void fillRectWithLocalMatrix(const GrClip& clip, |
| 123 | const GrPaint& paint, |
| 124 | const SkMatrix& viewMatrix, |
| 125 | const SkRect& rect, |
| 126 | const SkMatrix& localMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 127 | |
| 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 137 | void drawRRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 138 | 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 153 | void drawDRRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 154 | 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 169 | void drawPath(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 170 | 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 192 | void drawVertices(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 193 | 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 | /** |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 204 | * 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 214 | void drawAtlas(const GrClip&, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 215 | const GrPaint& paint, |
| 216 | const SkMatrix& viewMatrix, |
| 217 | int spriteCount, |
| 218 | const SkRSXform xform[], |
| 219 | const SkRect texRect[], |
| 220 | const SkColor colors[]); |
| 221 | |
| 222 | /** |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 223 | * 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 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 231 | void drawOval(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 232 | const GrPaint& paint, |
| 233 | const SkMatrix& viewMatrix, |
| 234 | const SkRect& oval, |
| 235 | const GrStrokeInfo& strokeInfo); |
| 236 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 237 | /** |
| 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); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 258 | |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Draws a batch |
| 261 | * |
| 262 | * @param paint describes how to color pixels. |
| 263 | * @param batch the batch to draw |
| 264 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 265 | void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 266 | |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 267 | /** |
| 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 | |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 275 | int width() const { return fRenderTarget->width(); } |
| 276 | int height() const { return fRenderTarget->height(); } |
| 277 | int numColorSamples() const { return fRenderTarget->numColorSamples(); } |
| 278 | |
robertphillips | 433625e | 2015-12-04 06:58:16 -0800 | [diff] [blame] | 279 | GrRenderTarget* accessRenderTarget() { return fRenderTarget; } |
| 280 | |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 281 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 282 | // Functions intended for internal use only. |
| 283 | void internal_drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch); |
| 284 | |
joshualitt | 96880d9 | 2016-02-16 10:36:53 -0800 | [diff] [blame] | 285 | protected: |
| 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 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 296 | private: |
joshualitt | 2e2202e | 2015-12-10 11:22:08 -0800 | [diff] [blame] | 297 | friend class GrAtlasTextBlob; // for access to drawBatch |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 298 | friend class GrDrawingManager; // for ctor |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 299 | |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame] | 300 | void internalDrawPath(GrPipelineBuilder*, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 301 | const SkMatrix& viewMatrix, |
| 302 | GrColor, |
| 303 | bool useAA, |
| 304 | const SkPath&, |
| 305 | const GrStrokeInfo&); |
| 306 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 307 | // This entry point allows the GrTextContext-derived classes to add their batches to |
| 308 | // the drawTarget. |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 309 | void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 310 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 311 | GrDrawTarget* getDrawTarget(); |
| 312 | |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 313 | GrDrawingManager* fDrawingManager; |
| 314 | GrRenderTarget* fRenderTarget; |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 315 | |
| 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'. |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 318 | GrDrawTarget* fDrawTarget; |
| 319 | SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext; |
| 320 | GrContext* fContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 321 | |
joshualitt | 8db8678 | 2016-02-17 05:40:00 -0800 | [diff] [blame] | 322 | SkSurfaceProps fSurfaceProps; |
| 323 | GrAuditTrail* fAuditTrail; |
joshualitt | 1de610a | 2016-01-06 08:26:09 -0800 | [diff] [blame] | 324 | |
| 325 | // In debug builds we guard against improper thread handling |
joshualitt | 6d0872d | 2016-01-11 08:27:48 -0800 | [diff] [blame] | 326 | SkDEBUGCODE(mutable GrSingleOwner* fSingleOwner;) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | #endif |