|
1 /* |
|
2 * Copyright 2010 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 GrDrawTarget_DEFINED |
|
9 #define GrDrawTarget_DEFINED |
|
10 |
|
11 #include "GrClipData.h" |
|
12 #include "GrDrawState.h" |
|
13 #include "GrIndexBuffer.h" |
|
14 |
|
15 #include "SkClipStack.h" |
|
16 #include "SkMatrix.h" |
|
17 #include "SkPath.h" |
|
18 #include "SkTArray.h" |
|
19 #include "SkTLazy.h" |
|
20 #include "SkTypes.h" |
|
21 #include "SkXfermode.h" |
|
22 |
|
23 class GrClipData; |
|
24 class GrDrawTargetCaps; |
|
25 class GrPath; |
|
26 class GrVertexBuffer; |
|
27 class SkStrokeRec; |
|
28 |
|
29 class GrDrawTarget : public SkRefCnt { |
|
30 protected: |
|
31 class DrawInfo; |
|
32 |
|
33 public: |
|
34 SK_DECLARE_INST_COUNT(GrDrawTarget) |
|
35 |
|
36 /////////////////////////////////////////////////////////////////////////// |
|
37 |
|
38 // The context may not be fully constructed and should not be used during GrDrawTarget |
|
39 // construction. |
|
40 GrDrawTarget(GrContext* context); |
|
41 virtual ~GrDrawTarget(); |
|
42 |
|
43 /** |
|
44 * Gets the capabilities of the draw target. |
|
45 */ |
|
46 const GrDrawTargetCaps* caps() const { return fCaps.get(); } |
|
47 |
|
48 /** |
|
49 * Sets the current clip to the region specified by clip. All draws will be |
|
50 * clipped against this clip if kClip_StateBit is enabled. |
|
51 * |
|
52 * Setting the clip may (or may not) zero out the client's stencil bits. |
|
53 * |
|
54 * @param description of the clipping region |
|
55 */ |
|
56 void setClip(const GrClipData* clip); |
|
57 |
|
58 /** |
|
59 * Gets the current clip. |
|
60 * |
|
61 * @return the clip. |
|
62 */ |
|
63 const GrClipData* getClip() const; |
|
64 |
|
65 /** |
|
66 * Sets the draw state object for the draw target. Note that this does not |
|
67 * make a copy. The GrDrawTarget will take a reference to passed object. |
|
68 * Passing NULL will cause the GrDrawTarget to use its own internal draw |
|
69 * state object rather than an externally provided one. |
|
70 */ |
|
71 void setDrawState(GrDrawState* drawState); |
|
72 |
|
73 /** |
|
74 * Read-only access to the GrDrawTarget's current draw state. |
|
75 */ |
|
76 const GrDrawState& getDrawState() const { return *fDrawState; } |
|
77 |
|
78 /** |
|
79 * Read-write access to the GrDrawTarget's current draw state. Note that |
|
80 * this doesn't ref. |
|
81 */ |
|
82 GrDrawState* drawState() { return fDrawState; } |
|
83 |
|
84 /** |
|
85 * Color alpha and coverage are two inputs to the drawing pipeline. For some |
|
86 * blend modes it is safe to fold the coverage into constant or per-vertex |
|
87 * color alpha value. For other blend modes they must be handled separately. |
|
88 * Depending on features available in the underlying 3D API this may or may |
|
89 * not be possible. |
|
90 * |
|
91 * This function considers the current draw state and the draw target's |
|
92 * capabilities to determine whether coverage can be handled correctly. The |
|
93 * following assumptions are made: |
|
94 * 1. The caller intends to somehow specify coverage. This can be |
|
95 * specified either by enabling a coverage stage on the GrDrawState or |
|
96 * via the vertex layout. |
|
97 * 2. Other than enabling coverage stages or enabling coverage in the |
|
98 * layout, the current configuration of the target's GrDrawState is as |
|
99 * it will be at draw time. |
|
100 */ |
|
101 bool canApplyCoverage() const; |
|
102 |
|
103 /** When we're using coverage AA but the blend is incompatible (given gpu |
|
104 * limitations) we should disable AA. */ |
|
105 bool shouldDisableCoverageAAForBlend() { |
|
106 // Enable below if we should draw with AA even when it produces |
|
107 // incorrect blending. |
|
108 // return false; |
|
109 return !this->canApplyCoverage(); |
|
110 } |
|
111 |
|
112 /** |
|
113 * Given the current draw state and hw support, will HW AA lines be used (if |
|
114 * a line primitive type is drawn)? |
|
115 */ |
|
116 bool willUseHWAALines() const; |
|
117 |
|
118 /** |
|
119 * There are three types of "sources" of geometry (vertices and indices) for |
|
120 * draw calls made on the target. When performing an indexed draw, the |
|
121 * indices and vertices can use different source types. Once a source is |
|
122 * specified it can be used for multiple draws. However, the time at which |
|
123 * the geometry data is no longer editable depends on the source type. |
|
124 * |
|
125 * Sometimes it is necessary to perform a draw while upstack code has |
|
126 * already specified geometry that it isn't finished with. So there are push |
|
127 * and pop methods. This allows the client to push the sources, draw |
|
128 * something using alternate sources, and then pop to restore the original |
|
129 * sources. |
|
130 * |
|
131 * Aside from pushes and pops, a source remains valid until another source |
|
132 * is set or resetVertexSource / resetIndexSource is called. Drawing from |
|
133 * a reset source is an error. |
|
134 * |
|
135 * The three types of sources are: |
|
136 * |
|
137 * 1. A cpu array (set*SourceToArray). This is useful when the caller |
|
138 * already provided vertex data in a format compatible with a |
|
139 * GrVertexLayout. The data in the array is consumed at the time that |
|
140 * set*SourceToArray is called and subsequent edits to the array will not |
|
141 * be reflected in draws. |
|
142 * |
|
143 * 2. Reserve. This is most useful when the caller has data it must |
|
144 * transform before drawing and is not long-lived. The caller requests |
|
145 * that the draw target make room for some amount of vertex and/or index |
|
146 * data. The target provides ptrs to hold the vertex and/or index data. |
|
147 * |
|
148 * The data is writable up until the next drawIndexed, drawNonIndexed, |
|
149 * drawIndexedInstances, drawRect, copySurface, or pushGeometrySource. At |
|
150 * this point the data is frozen and the ptrs are no longer valid. |
|
151 * |
|
152 * Where the space is allocated and how it is uploaded to the GPU is |
|
153 * subclass-dependent. |
|
154 * |
|
155 * 3. Vertex and Index Buffers. This is most useful for geometry that will |
|
156 * is long-lived. When the data in the buffer is consumed depends on the |
|
157 * GrDrawTarget subclass. For deferred subclasses the caller has to |
|
158 * guarantee that the data is still available in the buffers at playback. |
|
159 * (TODO: Make this more automatic as we have done for read/write pixels) |
|
160 * |
|
161 * The size of each vertex is determined by querying the current GrDrawState. |
|
162 */ |
|
163 |
|
164 /** |
|
165 * Reserves space for vertices and/or indices. Zero can be specifed as |
|
166 * either the vertex or index count if the caller desires to only reserve |
|
167 * space for only indices or only vertices. If zero is specifed for |
|
168 * vertexCount then the vertex source will be unmodified and likewise for |
|
169 * indexCount. |
|
170 * |
|
171 * If the function returns true then the reserve suceeded and the vertices |
|
172 * and indices pointers will point to the space created. |
|
173 * |
|
174 * If the target cannot make space for the request then this function will |
|
175 * return false. If vertexCount was non-zero then upon failure the vertex |
|
176 * source is reset and likewise for indexCount. |
|
177 * |
|
178 * The pointers to the space allocated for vertices and indices remain valid |
|
179 * until a drawIndexed, drawNonIndexed, drawIndexedInstances, drawRect, |
|
180 * copySurface, or push/popGeomtrySource is called. At that point logically a |
|
181 * snapshot of the data is made and the pointers are invalid. |
|
182 * |
|
183 * @param vertexCount the number of vertices to reserve space for. Can be |
|
184 * 0. Vertex size is queried from the current GrDrawState. |
|
185 * @param indexCount the number of indices to reserve space for. Can be 0. |
|
186 * @param vertices will point to reserved vertex space if vertexCount is |
|
187 * non-zero. Illegal to pass NULL if vertexCount > 0. |
|
188 * @param indices will point to reserved index space if indexCount is |
|
189 * non-zero. Illegal to pass NULL if indexCount > 0. |
|
190 */ |
|
191 bool reserveVertexAndIndexSpace(int vertexCount, |
|
192 int indexCount, |
|
193 void** vertices, |
|
194 void** indices); |
|
195 |
|
196 /** |
|
197 * Provides hints to caller about the number of vertices and indices |
|
198 * that can be allocated cheaply. This can be useful if caller is reserving |
|
199 * space but doesn't know exactly how much geometry is needed. |
|
200 * |
|
201 * Also may hint whether the draw target should be flushed first. This is |
|
202 * useful for deferred targets. |
|
203 * |
|
204 * @param vertexCount in: hint about how many vertices the caller would |
|
205 * like to allocate. Vertex size is queried from the |
|
206 * current GrDrawState. |
|
207 * out: a hint about the number of vertices that can be |
|
208 * allocated cheaply. Negative means no hint. |
|
209 * Ignored if NULL. |
|
210 * @param indexCount in: hint about how many indices the caller would |
|
211 * like to allocate. |
|
212 * out: a hint about the number of indices that can be |
|
213 * allocated cheaply. Negative means no hint. |
|
214 * Ignored if NULL. |
|
215 * |
|
216 * @return true if target should be flushed based on the input values. |
|
217 */ |
|
218 virtual bool geometryHints(int* vertexCount, |
|
219 int* indexCount) const; |
|
220 |
|
221 /** |
|
222 * Sets source of vertex data for the next draw. Array must contain |
|
223 * the vertex data when this is called. |
|
224 * |
|
225 * @param vertexArray cpu array containing vertex data. |
|
226 * @param vertexCount the number of vertices in the array. Vertex size is |
|
227 * queried from the current GrDrawState. |
|
228 */ |
|
229 void setVertexSourceToArray(const void* vertexArray, int vertexCount); |
|
230 |
|
231 /** |
|
232 * Sets source of index data for the next indexed draw. Array must contain |
|
233 * the indices when this is called. |
|
234 * |
|
235 * @param indexArray cpu array containing index data. |
|
236 * @param indexCount the number of indices in the array. |
|
237 */ |
|
238 void setIndexSourceToArray(const void* indexArray, int indexCount); |
|
239 |
|
240 /** |
|
241 * Sets source of vertex data for the next draw. Data does not have to be |
|
242 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances. |
|
243 * |
|
244 * @param buffer vertex buffer containing vertex data. Must be |
|
245 * unlocked before draw call. Vertex size is queried |
|
246 * from current GrDrawState. |
|
247 */ |
|
248 void setVertexSourceToBuffer(const GrVertexBuffer* buffer); |
|
249 |
|
250 /** |
|
251 * Sets source of index data for the next indexed draw. Data does not have |
|
252 * to be in the buffer until drawIndexed. |
|
253 * |
|
254 * @param buffer index buffer containing indices. Must be unlocked |
|
255 * before indexed draw call. |
|
256 */ |
|
257 void setIndexSourceToBuffer(const GrIndexBuffer* buffer); |
|
258 |
|
259 /** |
|
260 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex |
|
261 * source to reserved, array, or buffer before next draw. May be able to free |
|
262 * up temporary storage allocated by setVertexSourceToArray or |
|
263 * reserveVertexSpace. |
|
264 */ |
|
265 void resetVertexSource(); |
|
266 |
|
267 /** |
|
268 * Resets index source. Indexed Drawing from reset indices is illegal. Set |
|
269 * index source to reserved, array, or buffer before next indexed draw. May |
|
270 * be able to free up temporary storage allocated by setIndexSourceToArray |
|
271 * or reserveIndexSpace. |
|
272 */ |
|
273 void resetIndexSource(); |
|
274 |
|
275 /** |
|
276 * Query to find out if the vertex or index source is reserved. |
|
277 */ |
|
278 bool hasReservedVerticesOrIndices() const { |
|
279 return kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc || |
|
280 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; |
|
281 } |
|
282 |
|
283 /** |
|
284 * Pushes and resets the vertex/index sources. Any reserved vertex / index |
|
285 * data is finalized (i.e. cannot be updated after the matching pop but can |
|
286 * be drawn from). Must be balanced by a pop. |
|
287 */ |
|
288 void pushGeometrySource(); |
|
289 |
|
290 /** |
|
291 * Pops the vertex / index sources from the matching push. |
|
292 */ |
|
293 void popGeometrySource(); |
|
294 |
|
295 /** |
|
296 * Draws indexed geometry using the current state and current vertex / index |
|
297 * sources. |
|
298 * |
|
299 * @param type The type of primitives to draw. |
|
300 * @param startVertex the vertex in the vertex array/buffer corresponding |
|
301 * to index 0 |
|
302 * @param startIndex first index to read from index src. |
|
303 * @param vertexCount one greater than the max index. |
|
304 * @param indexCount the number of index elements to read. The index count |
|
305 * is effectively trimmed to the last completely |
|
306 * specified primitive. |
|
307 * @param devBounds optional bounds hint. This is a promise from the caller, |
|
308 * not a request for clipping. |
|
309 */ |
|
310 void drawIndexed(GrPrimitiveType type, |
|
311 int startVertex, |
|
312 int startIndex, |
|
313 int vertexCount, |
|
314 int indexCount, |
|
315 const SkRect* devBounds = NULL); |
|
316 |
|
317 /** |
|
318 * Draws non-indexed geometry using the current state and current vertex |
|
319 * sources. |
|
320 * |
|
321 * @param type The type of primitives to draw. |
|
322 * @param startVertex the vertex in the vertex array/buffer corresponding |
|
323 * to index 0 |
|
324 * @param vertexCount one greater than the max index. |
|
325 * @param devBounds optional bounds hint. This is a promise from the caller, |
|
326 * not a request for clipping. |
|
327 */ |
|
328 void drawNonIndexed(GrPrimitiveType type, |
|
329 int startVertex, |
|
330 int vertexCount, |
|
331 const SkRect* devBounds = NULL); |
|
332 |
|
333 /** |
|
334 * Draws path into the stencil buffer. The fill must be either even/odd or |
|
335 * winding (not inverse or hairline). It will respect the HW antialias flag |
|
336 * on the draw state (if possible in the 3D API). |
|
337 */ |
|
338 void stencilPath(const GrPath*, SkPath::FillType fill); |
|
339 |
|
340 /** |
|
341 * Draws a path. Fill must not be a hairline. It will respect the HW |
|
342 * antialias flag on the draw state (if possible in the 3D API). |
|
343 */ |
|
344 void drawPath(const GrPath*, SkPath::FillType fill); |
|
345 |
|
346 /** |
|
347 * Helper function for drawing rects. It performs a geometry src push and pop |
|
348 * and thus will finalize any reserved geometry. |
|
349 * |
|
350 * @param rect the rect to draw |
|
351 * @param matrix optional matrix applied to rect (before viewMatrix) |
|
352 * @param localRect optional rect that specifies local coords to map onto |
|
353 * rect. If NULL then rect serves as the local coords. |
|
354 * @param localMatrix optional matrix applied to localRect. If |
|
355 * srcRect is non-NULL and srcMatrix is non-NULL |
|
356 * then srcRect will be transformed by srcMatrix. |
|
357 * srcMatrix can be NULL when no srcMatrix is desired. |
|
358 */ |
|
359 void drawRect(const SkRect& rect, |
|
360 const SkMatrix* matrix, |
|
361 const SkRect* localRect, |
|
362 const SkMatrix* localMatrix) { |
|
363 AutoGeometryPush agp(this); |
|
364 this->onDrawRect(rect, matrix, localRect, localMatrix); |
|
365 } |
|
366 |
|
367 /** |
|
368 * Helper for drawRect when the caller doesn't need separate local rects or matrices. |
|
369 */ |
|
370 void drawSimpleRect(const SkRect& rect, const SkMatrix* matrix = NULL) { |
|
371 this->drawRect(rect, matrix, NULL, NULL); |
|
372 } |
|
373 void drawSimpleRect(const SkIRect& irect, const SkMatrix* matrix = NULL) { |
|
374 SkRect rect = SkRect::Make(irect); |
|
375 this->drawRect(rect, matrix, NULL, NULL); |
|
376 } |
|
377 |
|
378 /** |
|
379 * This call is used to draw multiple instances of some geometry with a |
|
380 * given number of vertices (V) and indices (I) per-instance. The indices in |
|
381 * the index source must have the form i[k+I] == i[k] + V. Also, all indices |
|
382 * i[kI] ... i[(k+1)I-1] must be elements of the range kV ... (k+1)V-1. As a |
|
383 * concrete example, the following index buffer for drawing a series of |
|
384 * quads each as two triangles each satisfies these conditions with V=4 and |
|
385 * I=6: |
|
386 * (0,1,2,0,2,3, 4,5,6,4,6,7, 8,9,10,8,10,11, ...) |
|
387 * |
|
388 * The call assumes that the pattern of indices fills the entire index |
|
389 * source. The size of the index buffer limits the number of instances that |
|
390 * can be drawn by the GPU in a single draw. However, the caller may specify |
|
391 * any (positive) number for instanceCount and if necessary multiple GPU |
|
392 * draws will be issued. Moreover, when drawIndexedInstances is called |
|
393 * multiple times it may be possible for GrDrawTarget to group them into a |
|
394 * single GPU draw. |
|
395 * |
|
396 * @param type the type of primitives to draw |
|
397 * @param instanceCount the number of instances to draw. Each instance |
|
398 * consists of verticesPerInstance vertices indexed by |
|
399 * indicesPerInstance indices drawn as the primitive |
|
400 * type specified by type. |
|
401 * @param verticesPerInstance The number of vertices in each instance (V |
|
402 * in the above description). |
|
403 * @param indicesPerInstance The number of indices in each instance (I |
|
404 * in the above description). |
|
405 * @param devBounds optional bounds hint. This is a promise from the caller, |
|
406 * not a request for clipping. |
|
407 */ |
|
408 void drawIndexedInstances(GrPrimitiveType type, |
|
409 int instanceCount, |
|
410 int verticesPerInstance, |
|
411 int indicesPerInstance, |
|
412 const SkRect* devBounds = NULL); |
|
413 |
|
414 /** |
|
415 * Clear the current render target if one isn't passed in. Ignores the |
|
416 * clip and all other draw state (blend mode, stages, etc). Clears the |
|
417 * whole thing if rect is NULL, otherwise just the rect. If canIgnoreRect |
|
418 * is set then the entire render target can be optionally cleared. |
|
419 */ |
|
420 virtual void clear(const SkIRect* rect, |
|
421 GrColor color, |
|
422 bool canIgnoreRect, |
|
423 GrRenderTarget* renderTarget = NULL) = 0; |
|
424 |
|
425 /** |
|
426 * instantGpuTraceEvent places a single "sign post" type marker into command stream. The |
|
427 * argument marker will be the name of the annotation that is added. |
|
428 */ |
|
429 void instantGpuTraceEvent(const char* marker); |
|
430 /** |
|
431 * The following two functions are used for marking groups of commands. Use pushGpuTraceEvent |
|
432 * to set the beginning of a command set, and popGpuTraceEvent is be called at end of the |
|
433 * command set. The argument marker is the name for the annotation that is added. The push and |
|
434 * pops can be used hierarchically, but every push must have a match pop. |
|
435 */ |
|
436 void pushGpuTraceEvent(const char* marker); |
|
437 void popGpuTraceEvent(); |
|
438 |
|
439 /** |
|
440 * Copies a pixel rectangle from one surface to another. This call may finalize |
|
441 * reserved vertex/index data (as though a draw call was made). The src pixels |
|
442 * copied are specified by srcRect. They are copied to a rect of the same |
|
443 * size in dst with top left at dstPoint. If the src rect is clipped by the |
|
444 * src bounds then pixel values in the dst rect corresponding to area clipped |
|
445 * by the src rect are not overwritten. This method can fail and return false |
|
446 * depending on the type of surface, configs, etc, and the backend-specific |
|
447 * limitations. If rect is clipped out entirely by the src or dst bounds then |
|
448 * true is returned since there is no actual copy necessary to succeed. |
|
449 */ |
|
450 bool copySurface(GrSurface* dst, |
|
451 GrSurface* src, |
|
452 const SkIRect& srcRect, |
|
453 const SkIPoint& dstPoint); |
|
454 /** |
|
455 * Function that determines whether a copySurface call would succeed without |
|
456 * performing the copy. |
|
457 */ |
|
458 bool canCopySurface(GrSurface* dst, |
|
459 GrSurface* src, |
|
460 const SkIRect& srcRect, |
|
461 const SkIPoint& dstPoint); |
|
462 |
|
463 /** |
|
464 * This is can be called before allocating a texture to be a dst for copySurface. It will |
|
465 * populate the origin, config, and flags fields of the desc such that copySurface is more |
|
466 * likely to succeed and be efficient. |
|
467 */ |
|
468 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc); |
|
469 |
|
470 |
|
471 /** |
|
472 * Release any resources that are cached but not currently in use. This |
|
473 * is intended to give an application some recourse when resources are low. |
|
474 */ |
|
475 virtual void purgeResources() {}; |
|
476 |
|
477 /** |
|
478 * For subclass internal use to invoke a call to onDraw(). See DrawInfo below. |
|
479 */ |
|
480 void executeDraw(const DrawInfo& info) { this->onDraw(info); } |
|
481 |
|
482 /** |
|
483 * For subclass internal use to invoke a call to onDrawPath(). |
|
484 */ |
|
485 void executeDrawPath(const GrPath* path, SkPath::FillType fill, |
|
486 const GrDeviceCoordTexture* dstCopy) { |
|
487 this->onDrawPath(path, fill, dstCopy); |
|
488 } |
|
489 |
|
490 //////////////////////////////////////////////////////////////////////////// |
|
491 |
|
492 /** |
|
493 * See AutoStateRestore below. |
|
494 */ |
|
495 enum ASRInit { |
|
496 kPreserve_ASRInit, |
|
497 kReset_ASRInit |
|
498 }; |
|
499 |
|
500 /** |
|
501 * Saves off the current state and restores it in the destructor. It will |
|
502 * install a new GrDrawState object on the target (setDrawState) and restore |
|
503 * the previous one in the destructor. The caller should call drawState() to |
|
504 * get the new draw state after the ASR is installed. |
|
505 * |
|
506 * GrDrawState* state = target->drawState(); |
|
507 * AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit). |
|
508 * state->setRenderTarget(rt); // state refers to the GrDrawState set on |
|
509 * // target before asr was initialized. |
|
510 * // Therefore, rt is set on the GrDrawState |
|
511 * // that will be restored after asr's |
|
512 * // destructor rather than target's current |
|
513 * // GrDrawState. |
|
514 */ |
|
515 class AutoStateRestore : public ::SkNoncopyable { |
|
516 public: |
|
517 /** |
|
518 * Default ASR will have no effect unless set() is subsequently called. |
|
519 */ |
|
520 AutoStateRestore(); |
|
521 |
|
522 /** |
|
523 * Saves the state on target. The state will be restored when the ASR |
|
524 * is destroyed. If this constructor is used do not call set(). |
|
525 * |
|
526 * @param init Should the newly installed GrDrawState be a copy of the |
|
527 * previous state or a default-initialized GrDrawState. |
|
528 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's |
|
529 * matrix will be preconcat'ed with the param. All stages will be |
|
530 updated to compensate for the matrix change. If init == kReset |
|
531 then the draw state's matrix will be this matrix. |
|
532 */ |
|
533 AutoStateRestore(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL); |
|
534 |
|
535 ~AutoStateRestore(); |
|
536 |
|
537 /** |
|
538 * Saves the state on target. The state will be restored when the ASR |
|
539 * is destroyed. This should only be called once per ASR object and only |
|
540 * when the default constructor was used. For nested saves use multiple |
|
541 * ASR objects. |
|
542 * |
|
543 * @param init Should the newly installed GrDrawState be a copy of the |
|
544 * previous state or a default-initialized GrDrawState. |
|
545 * @param viewMatrix Optional view matrix. If init = kPreserve then the draw state's |
|
546 * matrix will be preconcat'ed with the param. All stages will be |
|
547 updated to compensate for the matrix change. If init == kReset |
|
548 then the draw state's matrix will be this matrix. |
|
549 */ |
|
550 void set(GrDrawTarget* target, ASRInit init, const SkMatrix* viewMatrix = NULL); |
|
551 |
|
552 /** |
|
553 * Like set() but makes the view matrix identity. When init is kReset it is as though |
|
554 * NULL was passed to set's viewMatrix param. When init is kPreserve it is as though |
|
555 * the inverse view matrix was passed. If kPreserve is passed and the draw state's matrix |
|
556 * is not invertible then this may fail. |
|
557 */ |
|
558 bool setIdentity(GrDrawTarget* target, ASRInit init); |
|
559 |
|
560 private: |
|
561 GrDrawTarget* fDrawTarget; |
|
562 SkTLazy<GrDrawState> fTempState; |
|
563 GrDrawState* fSavedState; |
|
564 }; |
|
565 |
|
566 //////////////////////////////////////////////////////////////////////////// |
|
567 |
|
568 class AutoReleaseGeometry : public ::SkNoncopyable { |
|
569 public: |
|
570 AutoReleaseGeometry(GrDrawTarget* target, |
|
571 int vertexCount, |
|
572 int indexCount); |
|
573 AutoReleaseGeometry(); |
|
574 ~AutoReleaseGeometry(); |
|
575 bool set(GrDrawTarget* target, |
|
576 int vertexCount, |
|
577 int indexCount); |
|
578 bool succeeded() const { return NULL != fTarget; } |
|
579 void* vertices() const { SkASSERT(this->succeeded()); return fVertices; } |
|
580 void* indices() const { SkASSERT(this->succeeded()); return fIndices; } |
|
581 GrPoint* positions() const { |
|
582 return static_cast<GrPoint*>(this->vertices()); |
|
583 } |
|
584 |
|
585 private: |
|
586 void reset(); |
|
587 |
|
588 GrDrawTarget* fTarget; |
|
589 void* fVertices; |
|
590 void* fIndices; |
|
591 }; |
|
592 |
|
593 //////////////////////////////////////////////////////////////////////////// |
|
594 |
|
595 class AutoClipRestore : public ::SkNoncopyable { |
|
596 public: |
|
597 AutoClipRestore(GrDrawTarget* target) { |
|
598 fTarget = target; |
|
599 fClip = fTarget->getClip(); |
|
600 } |
|
601 |
|
602 AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip); |
|
603 |
|
604 ~AutoClipRestore() { |
|
605 fTarget->setClip(fClip); |
|
606 } |
|
607 private: |
|
608 GrDrawTarget* fTarget; |
|
609 const GrClipData* fClip; |
|
610 SkTLazy<SkClipStack> fStack; |
|
611 GrClipData fReplacementClip; |
|
612 }; |
|
613 |
|
614 //////////////////////////////////////////////////////////////////////////// |
|
615 |
|
616 /** |
|
617 * Saves the geometry src state at construction and restores in the destructor. It also saves |
|
618 * and then restores the vertex attrib state. |
|
619 */ |
|
620 class AutoGeometryPush : public ::SkNoncopyable { |
|
621 public: |
|
622 AutoGeometryPush(GrDrawTarget* target) |
|
623 : fAttribRestore(target->drawState()) { |
|
624 SkASSERT(NULL != target); |
|
625 fTarget = target; |
|
626 target->pushGeometrySource(); |
|
627 } |
|
628 |
|
629 ~AutoGeometryPush() { fTarget->popGeometrySource(); } |
|
630 |
|
631 private: |
|
632 GrDrawTarget* fTarget; |
|
633 GrDrawState::AutoVertexAttribRestore fAttribRestore; |
|
634 }; |
|
635 |
|
636 /** |
|
637 * Combination of AutoGeometryPush and AutoStateRestore. The vertex attribs will be in default |
|
638 * state regardless of ASRInit value. |
|
639 */ |
|
640 class AutoGeometryAndStatePush : public ::SkNoncopyable { |
|
641 public: |
|
642 AutoGeometryAndStatePush(GrDrawTarget* target, |
|
643 ASRInit init, |
|
644 const SkMatrix* viewMatrix = NULL) |
|
645 : fState(target, init, viewMatrix) { |
|
646 SkASSERT(NULL != target); |
|
647 fTarget = target; |
|
648 target->pushGeometrySource(); |
|
649 if (kPreserve_ASRInit == init) { |
|
650 target->drawState()->setDefaultVertexAttribs(); |
|
651 } |
|
652 } |
|
653 |
|
654 ~AutoGeometryAndStatePush() { fTarget->popGeometrySource(); } |
|
655 |
|
656 private: |
|
657 AutoStateRestore fState; |
|
658 GrDrawTarget* fTarget; |
|
659 }; |
|
660 |
|
661 /////////////////////////////////////////////////////////////////////////// |
|
662 // Draw execution tracking (for font atlases and other resources) |
|
663 class DrawToken { |
|
664 public: |
|
665 DrawToken(GrDrawTarget* drawTarget, uint32_t drawID) : |
|
666 fDrawTarget(drawTarget), fDrawID(drawID) {} |
|
667 |
|
668 bool isIssued() { return NULL != fDrawTarget && fDrawTarget->isIssued(fDrawID); } |
|
669 |
|
670 private: |
|
671 GrDrawTarget* fDrawTarget; |
|
672 uint32_t fDrawID; // this may wrap, but we're doing direct comparison |
|
673 // so that should be okay |
|
674 }; |
|
675 |
|
676 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, 0); } |
|
677 |
|
678 protected: |
|
679 |
|
680 enum GeometrySrcType { |
|
681 kNone_GeometrySrcType, //<! src has not been specified |
|
682 kReserved_GeometrySrcType, //<! src was set using reserve*Space |
|
683 kArray_GeometrySrcType, //<! src was set using set*SourceToArray |
|
684 kBuffer_GeometrySrcType //<! src was set using set*SourceToBuffer |
|
685 }; |
|
686 |
|
687 struct GeometrySrcState { |
|
688 GeometrySrcType fVertexSrc; |
|
689 union { |
|
690 // valid if src type is buffer |
|
691 const GrVertexBuffer* fVertexBuffer; |
|
692 // valid if src type is reserved or array |
|
693 int fVertexCount; |
|
694 }; |
|
695 |
|
696 GeometrySrcType fIndexSrc; |
|
697 union { |
|
698 // valid if src type is buffer |
|
699 const GrIndexBuffer* fIndexBuffer; |
|
700 // valid if src type is reserved or array |
|
701 int fIndexCount; |
|
702 }; |
|
703 |
|
704 size_t fVertexSize; |
|
705 }; |
|
706 |
|
707 int indexCountInCurrentSource() const { |
|
708 const GeometrySrcState& src = this->getGeomSrc(); |
|
709 switch (src.fIndexSrc) { |
|
710 case kNone_GeometrySrcType: |
|
711 return 0; |
|
712 case kReserved_GeometrySrcType: |
|
713 case kArray_GeometrySrcType: |
|
714 return src.fIndexCount; |
|
715 case kBuffer_GeometrySrcType: |
|
716 return static_cast<int>(src.fIndexBuffer->sizeInBytes() / sizeof(uint16_t)); |
|
717 default: |
|
718 GrCrash("Unexpected Index Source."); |
|
719 return 0; |
|
720 } |
|
721 } |
|
722 |
|
723 // This method is called by copySurface The srcRect is guaranteed to be entirely within the |
|
724 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls |
|
725 // entirely within the dst. The default implementation will draw a rect from the src to the |
|
726 // dst if the src is a texture and the dst is a render target and fail otherwise. |
|
727 virtual bool onCopySurface(GrSurface* dst, |
|
728 GrSurface* src, |
|
729 const SkIRect& srcRect, |
|
730 const SkIPoint& dstPoint); |
|
731 |
|
732 // Called to determine whether an onCopySurface call would succeed or not. This is useful for |
|
733 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived |
|
734 // classes must keep this consistent with their implementation of onCopySurface(). The inputs |
|
735 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src |
|
736 // and dst bounds. |
|
737 virtual bool onCanCopySurface(GrSurface* dst, |
|
738 GrSurface* src, |
|
739 const SkIRect& srcRect, |
|
740 const SkIPoint& dstPoint); |
|
741 |
|
742 GrContext* getContext() { return fContext; } |
|
743 const GrContext* getContext() const { return fContext; } |
|
744 |
|
745 // A subclass may override this function if it wishes to be notified when the clip is changed. |
|
746 // The override should call INHERITED::clipWillBeSet(). |
|
747 virtual void clipWillBeSet(const GrClipData* clipData); |
|
748 |
|
749 // subclasses must call this in their destructors to ensure all vertex |
|
750 // and index sources have been released (including those held by |
|
751 // pushGeometrySource()) |
|
752 void releaseGeometry(); |
|
753 |
|
754 // accessors for derived classes |
|
755 const GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); } |
|
756 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert. |
|
757 size_t getVertexSize() const { |
|
758 // the vertex layout is only valid if a vertex source has been specified. |
|
759 SkASSERT(this->getGeomSrc().fVertexSrc != kNone_GeometrySrcType); |
|
760 return this->getGeomSrc().fVertexSize; |
|
761 } |
|
762 |
|
763 // Subclass must initialize this in its constructor. |
|
764 SkAutoTUnref<const GrDrawTargetCaps> fCaps; |
|
765 |
|
766 /** |
|
767 * Used to communicate draws to subclass's onDraw function. |
|
768 */ |
|
769 class DrawInfo { |
|
770 public: |
|
771 DrawInfo(const DrawInfo& di) { (*this) = di; } |
|
772 DrawInfo& operator =(const DrawInfo& di); |
|
773 |
|
774 GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
|
775 int startVertex() const { return fStartVertex; } |
|
776 int startIndex() const { return fStartIndex; } |
|
777 int vertexCount() const { return fVertexCount; } |
|
778 int indexCount() const { return fIndexCount; } |
|
779 int verticesPerInstance() const { return fVerticesPerInstance; } |
|
780 int indicesPerInstance() const { return fIndicesPerInstance; } |
|
781 int instanceCount() const { return fInstanceCount; } |
|
782 |
|
783 bool isIndexed() const { return fIndexCount > 0; } |
|
784 #ifdef SK_DEBUG |
|
785 bool isInstanced() const; // this version is longer because of asserts |
|
786 #else |
|
787 bool isInstanced() const { return fInstanceCount > 0; } |
|
788 #endif |
|
789 |
|
790 // adds or remove instances |
|
791 void adjustInstanceCount(int instanceOffset); |
|
792 // shifts the start vertex |
|
793 void adjustStartVertex(int vertexOffset); |
|
794 // shifts the start index |
|
795 void adjustStartIndex(int indexOffset); |
|
796 |
|
797 void setDevBounds(const SkRect& bounds) { |
|
798 fDevBoundsStorage = bounds; |
|
799 fDevBounds = &fDevBoundsStorage; |
|
800 } |
|
801 const SkRect* getDevBounds() const { return fDevBounds; } |
|
802 |
|
803 // NULL if no copy of the dst is needed for the draw. |
|
804 const GrDeviceCoordTexture* getDstCopy() const { |
|
805 if (NULL != fDstCopy.texture()) { |
|
806 return &fDstCopy; |
|
807 } else { |
|
808 return NULL; |
|
809 } |
|
810 } |
|
811 |
|
812 private: |
|
813 DrawInfo() { fDevBounds = NULL; } |
|
814 |
|
815 friend class GrDrawTarget; |
|
816 |
|
817 GrPrimitiveType fPrimitiveType; |
|
818 |
|
819 int fStartVertex; |
|
820 int fStartIndex; |
|
821 int fVertexCount; |
|
822 int fIndexCount; |
|
823 |
|
824 int fInstanceCount; |
|
825 int fVerticesPerInstance; |
|
826 int fIndicesPerInstance; |
|
827 |
|
828 SkRect fDevBoundsStorage; |
|
829 SkRect* fDevBounds; |
|
830 |
|
831 GrDeviceCoordTexture fDstCopy; |
|
832 }; |
|
833 |
|
834 private: |
|
835 // A subclass can optionally overload this function to be notified before |
|
836 // vertex and index space is reserved. |
|
837 virtual void willReserveVertexAndIndexSpace(int vertexCount, int indexCount) {} |
|
838 |
|
839 // implemented by subclass to allocate space for reserved geom |
|
840 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) = 0; |
|
841 virtual bool onReserveIndexSpace(int indexCount, void** indices) = 0; |
|
842 // implemented by subclass to handle release of reserved geom space |
|
843 virtual void releaseReservedVertexSpace() = 0; |
|
844 virtual void releaseReservedIndexSpace() = 0; |
|
845 // subclass must consume array contents when set |
|
846 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) = 0; |
|
847 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) = 0; |
|
848 // subclass is notified that geom source will be set away from an array |
|
849 virtual void releaseVertexArray() = 0; |
|
850 virtual void releaseIndexArray() = 0; |
|
851 // subclass overrides to be notified just before geo src state is pushed/popped. |
|
852 virtual void geometrySourceWillPush() = 0; |
|
853 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) = 0; |
|
854 // subclass called to perform drawing |
|
855 virtual void onDraw(const DrawInfo&) = 0; |
|
856 // Implementation of drawRect. The geometry src and vertex attribs will already |
|
857 // be saved before this is called and restored afterwards. A subclass may override |
|
858 // this to perform more optimal rect rendering. Its draws should be funneled through |
|
859 // one of the public GrDrawTarget draw methods (e.g. drawNonIndexed, |
|
860 // drawIndexedInstances, ...). The base class draws a two triangle fan using |
|
861 // drawNonIndexed from reserved vertex space. |
|
862 virtual void onDrawRect(const SkRect& rect, |
|
863 const SkMatrix* matrix, |
|
864 const SkRect* localRect, |
|
865 const SkMatrix* localMatrix); |
|
866 |
|
867 virtual void onStencilPath(const GrPath*, SkPath::FillType) = 0; |
|
868 virtual void onDrawPath(const GrPath*, SkPath::FillType, |
|
869 const GrDeviceCoordTexture* dstCopy) = 0; |
|
870 |
|
871 virtual void onInstantGpuTraceEvent(const char* marker) = 0; |
|
872 virtual void onPushGpuTraceEvent(const char* marker) = 0; |
|
873 virtual void onPopGpuTraceEvent() = 0; |
|
874 |
|
875 // helpers for reserving vertex and index space. |
|
876 bool reserveVertexSpace(size_t vertexSize, |
|
877 int vertexCount, |
|
878 void** vertices); |
|
879 bool reserveIndexSpace(int indexCount, void** indices); |
|
880 |
|
881 // called by drawIndexed and drawNonIndexed. Use a negative indexCount to |
|
882 // indicate non-indexed drawing. |
|
883 bool checkDraw(GrPrimitiveType type, int startVertex, |
|
884 int startIndex, int vertexCount, |
|
885 int indexCount) const; |
|
886 // called when setting a new vert/idx source to unref prev vb/ib |
|
887 void releasePreviousVertexSource(); |
|
888 void releasePreviousIndexSource(); |
|
889 |
|
890 // Makes a copy of the dst if it is necessary for the draw. Returns false if a copy is required |
|
891 // but couldn't be made. Otherwise, returns true. |
|
892 bool setupDstReadIfNecessary(DrawInfo* info) { |
|
893 return this->setupDstReadIfNecessary(&info->fDstCopy, info->getDevBounds()); |
|
894 } |
|
895 bool setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds); |
|
896 |
|
897 // Check to see if this set of draw commands has been sent out |
|
898 virtual bool isIssued(uint32_t drawID) { return true; } |
|
899 |
|
900 enum { |
|
901 kPreallocGeoSrcStateStackCnt = 4, |
|
902 }; |
|
903 SkSTArray<kPreallocGeoSrcStateStackCnt, GeometrySrcState, true> fGeoSrcStateStack; |
|
904 const GrClipData* fClip; |
|
905 GrDrawState* fDrawState; |
|
906 GrDrawState fDefaultDrawState; |
|
907 // The context owns us, not vice-versa, so this ptr is not ref'ed by DrawTarget. |
|
908 GrContext* fContext; |
|
909 // To keep track that we always have at least as many debug marker pushes as pops |
|
910 int fPushGpuTraceCount; |
|
911 |
|
912 typedef SkRefCnt INHERITED; |
|
913 }; |
|
914 |
|
915 #endif |