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" |
| 12 | #include "SkRefCnt.h" |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 13 | #include "SkSurfaceProps.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 15 | class GrClip; |
| 16 | class GrContext; |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 17 | class GrDrawBatch; |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 18 | class GrDrawingManager; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 19 | class GrDrawTarget; |
| 20 | class GrPaint; |
| 21 | class GrPathProcessor; |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 22 | class GrPathRange; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 23 | class GrPathRangeDraw; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 24 | class GrPipelineBuilder; |
| 25 | class GrRenderTarget; |
| 26 | class GrStrokeInfo; |
| 27 | class GrSurface; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 28 | class GrTextContext; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 29 | class SkDrawFilter; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 30 | struct SkIPoint; |
| 31 | struct SkIRect; |
| 32 | class SkMatrix; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 33 | class SkPaint; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 34 | class SkPath; |
| 35 | struct SkPoint; |
| 36 | struct SkRect; |
| 37 | class SkRRect; |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 38 | struct SkRSXform; |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 39 | class SkTextBlob; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * A helper object to orchestrate draws |
| 43 | */ |
| 44 | class SK_API GrDrawContext : public SkRefCnt { |
| 45 | public: |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 46 | ~GrDrawContext() override; |
| 47 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 48 | void copySurface(GrSurface* src, const SkIRect& srcRect, const SkIPoint& dstPoint); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 49 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 50 | // TODO: it is odd that we need both the SkPaint in the following 3 methods. |
| 51 | // We should extract the text parameters from SkPaint and pass them separately |
| 52 | // akin to GrStrokeInfo (GrTextInfo?) |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 53 | void drawText(const GrClip&, const GrPaint&, const SkPaint&, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 54 | const SkMatrix& viewMatrix, const char text[], size_t byteLength, |
| 55 | SkScalar x, SkScalar y, const SkIRect& clipBounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 56 | void drawPosText(const GrClip&, const GrPaint&, const SkPaint&, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 57 | const SkMatrix& viewMatrix, const char text[], size_t byteLength, |
| 58 | const SkScalar pos[], int scalarsPerPosition, |
| 59 | const SkPoint& offset, const SkIRect& clipBounds); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 60 | void drawTextBlob(const GrClip&, const SkPaint&, |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 61 | const SkMatrix& viewMatrix, const SkTextBlob*, |
| 62 | SkScalar x, SkScalar y, |
| 63 | SkDrawFilter*, const SkIRect& clipBounds); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 64 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 65 | // drawPathsFromRange is thanks to GrStencilAndCoverTextContext |
| 66 | // TODO: remove once path batches can be created external to GrDrawTarget. |
| 67 | void drawPathsFromRange(const GrPipelineBuilder*, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 68 | const SkMatrix& viewMatrix, |
| 69 | const SkMatrix& localMatrix, |
| 70 | GrColor color, |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 71 | GrPathRange* range, |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 72 | GrPathRangeDraw* draw, |
| 73 | int /*GrPathRendering::FillType*/ fill); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Provides a perfomance hint that the render target's contents are allowed |
| 77 | * to become undefined. |
| 78 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 79 | void discard(); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Clear the entire or rect of the render target, ignoring any clips. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 83 | * @param rect the rect to clear or the whole thing if rect is NULL. |
| 84 | * @param color the color to clear to. |
| 85 | * @param canIgnoreRect allows partial clears to be converted to whole |
| 86 | * clears on platforms for which that is cheap |
| 87 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 88 | void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Draw everywhere (respecting the clip) with the paint. |
| 92 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 93 | void drawPaint(const GrClip&, const GrPaint&, const SkMatrix& viewMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Draw the rect using a paint. |
| 97 | * @param paint describes how to color pixels. |
| 98 | * @param viewMatrix transformation matrix |
| 99 | * @param strokeInfo the stroke information (width, join, cap), and. |
| 100 | * the dash information (intervals, count, phase). |
| 101 | * If strokeInfo == NULL, then the rect is filled. |
| 102 | * Otherwise, if stroke width == 0, then the stroke |
| 103 | * is always a single pixel thick, else the rect is |
| 104 | * mitered/beveled stroked based on stroke width. |
| 105 | * The rects coords are used to access the paint (through texture matrix) |
| 106 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 107 | void drawRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 108 | const GrPaint& paint, |
| 109 | const SkMatrix& viewMatrix, |
| 110 | const SkRect&, |
| 111 | const GrStrokeInfo* strokeInfo = NULL); |
| 112 | |
| 113 | /** |
| 114 | * Maps a rectangle of shader coordinates to a rectangle and draws that rectangle |
| 115 | * |
| 116 | * @param paint describes how to color pixels. |
| 117 | * @param viewMatrix transformation matrix which applies to rectToDraw |
| 118 | * @param rectToDraw the rectangle to draw |
| 119 | * @param localRect the rectangle of shader coordinates applied to rectToDraw |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 120 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 121 | void drawNonAARectToRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 122 | const GrPaint& paint, |
| 123 | const SkMatrix& viewMatrix, |
| 124 | const SkRect& rectToDraw, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 125 | const SkRect& localRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Draws a non-AA rect with paint and a localMatrix |
| 129 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 130 | void drawNonAARectWithLocalMatrix(const GrClip& clip, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 131 | const GrPaint& paint, |
| 132 | const SkMatrix& viewMatrix, |
| 133 | const SkRect& rect, |
joshualitt | b6b513b | 2015-08-21 10:25:18 -0700 | [diff] [blame] | 134 | const SkMatrix& localMatrix); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Draw a roundrect using a paint. |
| 138 | * |
| 139 | * @param paint describes how to color pixels. |
| 140 | * @param viewMatrix transformation matrix |
| 141 | * @param rrect the roundrect to draw |
| 142 | * @param strokeInfo the stroke information (width, join, cap) and |
| 143 | * the dash information (intervals, count, phase). |
| 144 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 145 | void drawRRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 146 | const GrPaint&, |
| 147 | const SkMatrix& viewMatrix, |
| 148 | const SkRRect& rrect, |
| 149 | const GrStrokeInfo&); |
| 150 | |
| 151 | /** |
| 152 | * Shortcut for drawing an SkPath consisting of nested rrects using a paint. |
| 153 | * Does not support stroking. The result is undefined if outer does not contain |
| 154 | * inner. |
| 155 | * |
| 156 | * @param paint describes how to color pixels. |
| 157 | * @param viewMatrix transformation matrix |
| 158 | * @param outer the outer roundrect |
| 159 | * @param inner the inner roundrect |
| 160 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 161 | void drawDRRect(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 162 | const GrPaint&, |
| 163 | const SkMatrix& viewMatrix, |
| 164 | const SkRRect& outer, |
| 165 | const SkRRect& inner); |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Draws a path. |
| 170 | * |
| 171 | * @param paint describes how to color pixels. |
| 172 | * @param viewMatrix transformation matrix |
| 173 | * @param path the path to draw |
| 174 | * @param strokeInfo the stroke information (width, join, cap) and |
| 175 | * the dash information (intervals, count, phase). |
| 176 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 177 | void drawPath(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 178 | const GrPaint&, |
| 179 | const SkMatrix& viewMatrix, |
| 180 | const SkPath&, |
| 181 | const GrStrokeInfo&); |
| 182 | |
| 183 | /** |
| 184 | * Draws vertices with a paint. |
| 185 | * |
| 186 | * @param paint describes how to color pixels. |
| 187 | * @param viewMatrix transformation matrix |
| 188 | * @param primitiveType primitives type to draw. |
| 189 | * @param vertexCount number of vertices. |
| 190 | * @param positions array of vertex positions, required. |
| 191 | * @param texCoords optional array of texture coordinates used |
| 192 | * to access the paint. |
| 193 | * @param colors optional array of per-vertex colors, supercedes |
| 194 | * the paint's color field. |
| 195 | * @param indices optional array of indices. If NULL vertices |
| 196 | * are drawn non-indexed. |
| 197 | * @param indexCount if indices is non-null then this is the |
| 198 | * number of indices. |
| 199 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 200 | void drawVertices(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 201 | const GrPaint& paint, |
| 202 | const SkMatrix& viewMatrix, |
| 203 | GrPrimitiveType primitiveType, |
| 204 | int vertexCount, |
| 205 | const SkPoint positions[], |
| 206 | const SkPoint texs[], |
| 207 | const GrColor colors[], |
| 208 | const uint16_t indices[], |
| 209 | int indexCount); |
| 210 | |
| 211 | /** |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 212 | * Draws textured sprites from an atlas with a paint. |
| 213 | * |
| 214 | * @param paint describes how to color pixels. |
| 215 | * @param viewMatrix transformation matrix |
| 216 | * @param spriteCount number of sprites. |
| 217 | * @param xform array of compressed transformation data, required. |
| 218 | * @param texRect array of texture rectangles used to access the paint. |
| 219 | * @param colors optional array of per-sprite colors, supercedes |
| 220 | * the paint's color field. |
| 221 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 222 | void drawAtlas(const GrClip&, |
jvanverth | 31ff762 | 2015-08-07 10:09:28 -0700 | [diff] [blame] | 223 | const GrPaint& paint, |
| 224 | const SkMatrix& viewMatrix, |
| 225 | int spriteCount, |
| 226 | const SkRSXform xform[], |
| 227 | const SkRect texRect[], |
| 228 | const SkColor colors[]); |
| 229 | |
| 230 | /** |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 231 | * Draws an oval. |
| 232 | * |
| 233 | * @param paint describes how to color pixels. |
| 234 | * @param viewMatrix transformation matrix |
| 235 | * @param oval the bounding rect of the oval. |
| 236 | * @param strokeInfo the stroke information (width, join, cap) and |
| 237 | * the dash information (intervals, count, phase). |
| 238 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 239 | void drawOval(const GrClip&, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 240 | const GrPaint& paint, |
| 241 | const SkMatrix& viewMatrix, |
| 242 | const SkRect& oval, |
| 243 | const GrStrokeInfo& strokeInfo); |
| 244 | |
| 245 | |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Draws a batch |
| 248 | * |
| 249 | * @param paint describes how to color pixels. |
| 250 | * @param batch the batch to draw |
| 251 | */ |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 252 | void drawBatch(const GrClip&, const GrPaint&, GrDrawBatch*); |
joshualitt | b7ee1bf | 2015-08-10 11:59:02 -0700 | [diff] [blame] | 253 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 254 | private: |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 255 | friend class GrAtlasTextContext; // for access to drawBatch |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 256 | friend class GrDrawingManager; // for ctor |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 257 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 258 | SkDEBUGCODE(void validate() const;) |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 259 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 260 | GrDrawContext(GrDrawingManager*, GrRenderTarget*, const SkSurfaceProps* surfaceProps); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 261 | |
robertphillips | b83bec5 | 2015-10-23 09:38:03 -0700 | [diff] [blame^] | 262 | void internalDrawPath(GrPipelineBuilder*, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 263 | const SkMatrix& viewMatrix, |
| 264 | GrColor, |
| 265 | bool useAA, |
| 266 | const SkPath&, |
| 267 | const GrStrokeInfo&); |
| 268 | |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 269 | // This entry point allows the GrTextContext-derived classes to add their batches to |
| 270 | // the drawTarget. |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 271 | void drawBatch(GrPipelineBuilder* pipelineBuilder, GrDrawBatch* batch); |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 272 | |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 273 | GrDrawTarget* getDrawTarget(); |
| 274 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 275 | GrDrawingManager* fDrawingManager; |
| 276 | GrRenderTarget* fRenderTarget; |
robertphillips | a106c62 | 2015-10-16 09:07:06 -0700 | [diff] [blame] | 277 | |
| 278 | // In MDB-mode the drawTarget can be closed by some other drawContext that has picked |
| 279 | // it up. For this reason, the drawTarget should only ever be accessed via 'getDrawTarget'. |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 280 | GrDrawTarget* fDrawTarget; |
| 281 | GrTextContext* fTextContext; // lazily gotten from GrContext::DrawingManager |
robertphillips | 2334fb6 | 2015-06-17 05:43:33 -0700 | [diff] [blame] | 282 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 283 | SkSurfaceProps fSurfaceProps; |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | #endif |