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