blob: 5dfe2c998e41bb06b2fb271f5c25087c9d5fc650 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17#ifndef GrContext_DEFINED
18#define GrContext_DEFINED
19
20#include "GrClip.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021#include "GrTextureCache.h"
22#include "GrPaint.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000023#include "GrPathRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000024
25class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000026class GrGpu;
27struct GrGpuStats;
bsalomon@google.com27847de2011-02-22 20:59:41 +000028class GrVertexBufferAllocPool;
29class GrIndexBufferAllocPool;
30class GrInOrderDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
bsalomon@google.com91826102011-03-21 19:51:57 +000032class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000033public:
34 /**
35 * Creates a GrContext from within a 3D context.
36 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000037 static GrContext* Create(GrEngine engine,
38 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000039
40 /**
41 * Helper to create a opengl-shader based context
42 */
43 static GrContext* CreateGLShaderContext();
44
45 virtual ~GrContext();
46
47 /**
48 * The GrContext normally assumes that no outsider is setting state
49 * within the underlying 3D API's context/device/whatever. This call informs
50 * the context that the state was modified and it should resend. Shouldn't
51 * be called frequently for good performance.
52 */
53 void resetContext();
54
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 /**
56 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
57 * if you have lost the associated GPU context, and thus internal texture,
58 * buffer, etc. references/IDs are now invalid. Should be called even when
59 * GrContext is no longer going to be used for two reasons:
60 * 1) ~GrContext will not try to free the objects in the 3D API.
61 * 2) If you've created GrResources that outlive the GrContext they will
62 * be marked as invalid (GrResource::isValid()) and won't attempt to
63 * free their underlying resource in the 3D API.
64 * Content drawn since the last GrContext::flush() may be lost.
65 */
66 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000067
68 /**
junov@google.com53a55842011-06-08 22:55:10 +000069 * Similar to contextLost, but makes no attempt to reset state.
70 * Use this method when GrContext destruction is pending, but
71 * the graphics context is destroyed first.
72 */
73 void contextDestroyed();
74
75 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 * Frees gpu created by the context. Can be called to reduce GPU memory
77 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 void freeGpuResources();
80
81 ///////////////////////////////////////////////////////////////////////////
82 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000083
84 /**
85 * Search for an entry with the same Key. If found, "lock" it and return it.
86 * If not found, return null.
87 */
88 GrTextureEntry* findAndLockTexture(GrTextureKey*,
89 const GrSamplerState&);
90
91
92 /**
93 * Create a new entry, based on the specified key and texture, and return
94 * its "locked" entry.
95 *
96 * Ownership of the texture is transferred to the Entry, which will unref()
97 * it when we are purged or deleted.
98 */
99 GrTextureEntry* createAndLockTexture(GrTextureKey* key,
100 const GrSamplerState&,
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000101 const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000102 void* srcData, size_t rowBytes);
103
104 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000105 * Returns a texture matching the desc. It's contents are unknown. Subsequent
106 * requests with the same descriptor are not guaranteed to return the same
107 * texture. The same texture is guaranteed not be returned again until it is
108 * unlocked.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000109 *
110 * Textures created by createAndLockTexture() hide the complications of
111 * tiling non-power-of-two textures on APIs that don't support this (e.g.
112 * unextended GLES2). Tiling a npot texture created by lockKeylessTexture on
113 * such an API will create gaps in the tiling pattern. This includes clamp
114 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000115 */
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000116 GrTextureEntry* lockKeylessTexture(const GrTextureDesc& desc);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000117
118 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000119 * When done with an entry, call unlockTexture(entry) on it, which returns
120 * it to the cache, where it may be purged.
121 */
122 void unlockTexture(GrTextureEntry* entry);
123
124 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000125 * Creates a texture that is outside the cache. Does not count against
126 * cache's budget.
127 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000128 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000129 void* srcData,
130 size_t rowBytes);
131
132 /**
133 * Returns true if the specified use of an indexed texture is supported.
134 */
135 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
136
137 /**
138 * Return the current texture cache limits.
139 *
140 * @param maxTextures If non-null, returns maximum number of textures that
141 * can be held in the cache.
142 * @param maxTextureBytes If non-null, returns maximum number of bytes of
143 * texture memory that can be held in the cache.
144 */
145 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
146
147 /**
148 * Specify the texture cache limits. If the current cache exceeds either
149 * of these, it will be purged (LRU) to keep the cache within these limits.
150 *
151 * @param maxTextures The maximum number of textures that can be held in
152 * the cache.
153 * @param maxTextureBytes The maximum number of bytes of texture memory
154 * that can be held in the cache.
155 */
156 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
157
158 /**
159 * Return the max width or height of a texture supported by the current gpu
160 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000161 int getMaxTextureSize() const;
162
163 /**
164 * Return the max width or height of a render target supported by the
165 * current gpu
166 */
167 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000168
169 ///////////////////////////////////////////////////////////////////////////
170 // Render targets
171
172 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000173 * Sets the render target.
174 * @param target the render target to set. (should not be NULL.)
175 */
176 void setRenderTarget(GrRenderTarget* target);
177
178 /**
179 * Gets the current render target.
180 * @return the currently bound render target. Should never be NULL.
181 */
182 const GrRenderTarget* getRenderTarget() const;
183 GrRenderTarget* getRenderTarget();
184
185 ///////////////////////////////////////////////////////////////////////////
186 // Platform Surfaces
187
188 // GrContext provides an interface for wrapping externally created textures
189 // and rendertargets in their Gr-equivalents.
190
191 /**
192 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
193 * the type of object returned. If kIsTexture is set the returned object
194 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
195 * are set the render target object is accessible by
196 * GrTexture::asRenderTarget().
197 *
198 * GL: if the object is a texture Gr may change its GL texture parameters
199 * when it is drawn.
200 *
201 * @param desc description of the object to create.
202 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
203 * on failure.
204 */
205 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000206 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
208 * viewport state from the underlying 3D API and wraps it in a
209 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
210 * underlying object in its destructor and it is up to caller to guarantee
211 * that it remains valid while the GrRenderTarget is used.
212 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000213 * Will not detect that the render target is also a texture. If you need
214 * to also use the render target as a GrTexture use createPlatformSurface.
215 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216 * @return the newly created GrRenderTarget
217 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000218 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220 ///////////////////////////////////////////////////////////////////////////
221 // Matrix state
222
223 /**
224 * Gets the current transformation matrix.
225 * @return the current matrix.
226 */
227 const GrMatrix& getMatrix() const;
228
229 /**
230 * Sets the transformation matrix.
231 * @param m the matrix to set.
232 */
233 void setMatrix(const GrMatrix& m);
234
235 /**
236 * Concats the current matrix. The passed matrix is applied before the
237 * current matrix.
238 * @param m the matrix to concat.
239 */
240 void concatMatrix(const GrMatrix& m) const;
241
242
243 ///////////////////////////////////////////////////////////////////////////
244 // Clip state
245 /**
246 * Gets the current clip.
247 * @return the current clip.
248 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000249 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250
251 /**
252 * Sets the clip.
253 * @param clip the clip to set.
254 */
255 void setClip(const GrClip& clip);
256
257 /**
258 * Convenience method for setting the clip to a rect.
259 * @param rect the rect to set as the new clip.
260 */
261 void setClip(const GrIRect& rect);
262
263 ///////////////////////////////////////////////////////////////////////////
264 // Draws
265
266 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000267 * Clear the entire or rect of the render target, ignoring any clips.
268 * @param rect the rect to clear or the whole thing if rect is NULL.
269 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000270 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000271 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272
273 /**
274 * Draw everywhere (respecting the clip) with the paint.
275 */
276 void drawPaint(const GrPaint& paint);
277
278 /**
279 * Draw the rect using a paint.
280 * @param paint describes how to color pixels.
281 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
282 * the rect is mitered stroked based on strokeWidth. If
283 * strokeWidth == 0, then the stroke is always a single
284 * pixel thick.
285 * @param matrix Optional matrix applied to the rect. Applied before
286 * context's matrix or the paint's matrix.
287 * The rects coords are used to access the paint (through texture matrix)
288 */
289 void drawRect(const GrPaint& paint,
290 const GrRect&,
291 GrScalar strokeWidth = -1,
292 const GrMatrix* matrix = NULL);
293
294 /**
295 * Maps a rect of paint coordinates onto the a rect of destination
296 * coordinates. Each rect can optionally be transformed. The srcRect
297 * is stretched over the dstRect. The dstRect is transformed by the
298 * context's matrix and the srcRect is transformed by the paint's matrix.
299 * Additional optional matrices can be provided by parameters.
300 *
301 * @param paint describes how to color pixels.
302 * @param dstRect the destination rect to draw.
303 * @param srcRect rect of paint coordinates to be mapped onto dstRect
304 * @param dstMatrix Optional matrix to transform dstRect. Applied before
305 * context's matrix.
306 * @param srcMatrix Optional matrix to transform srcRect Applied before
307 * paint's matrix.
308 */
309 void drawRectToRect(const GrPaint& paint,
310 const GrRect& dstRect,
311 const GrRect& srcRect,
312 const GrMatrix* dstMatrix = NULL,
313 const GrMatrix* srcMatrix = NULL);
314
315 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000316 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000317 *
318 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000319 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000320 * @param fill the path filling rule to use.
321 * @param translate optional additional translation applied to the
322 * path.
323 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000324 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000325 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000326
bsalomon@google.com27847de2011-02-22 20:59:41 +0000327 /**
328 * Draws vertices with a paint.
329 *
330 * @param paint describes how to color pixels.
331 * @param primitiveType primitives type to draw.
332 * @param vertexCount number of vertices.
333 * @param positions array of vertex positions, required.
334 * @param texCoords optional array of texture coordinates used
335 * to access the paint.
336 * @param colors optional array of per-vertex colors, supercedes
337 * the paint's color field.
338 * @param indices optional array of indices. If NULL vertices
339 * are drawn non-indexed.
340 * @param indexCount if indices is non-null then this is the
341 * number of indices.
342 */
343 void drawVertices(const GrPaint& paint,
344 GrPrimitiveType primitiveType,
345 int vertexCount,
346 const GrPoint positions[],
347 const GrPoint texs[],
348 const GrColor colors[],
349 const uint16_t indices[],
350 int indexCount);
351
352 /**
353 * Similar to drawVertices but caller provides objects that convert to Gr
354 * types. The count of vertices is given by posSrc.
355 *
356 * @param paint describes how to color pixels.
357 * @param primitiveType primitives type to draw.
358 * @param posSrc Source of vertex positions. Must implement
359 * int count() const;
360 * void writeValue(int i, GrPoint* point) const;
361 * count returns the total number of vertices and
362 * writeValue writes a vertex position to point.
363 * @param texSrc optional, pass NULL to not use explicit tex
364 * coords. If present provides tex coords with
365 * method:
366 * void writeValue(int i, GrPoint* point) const;
367 * @param texSrc optional, pass NULL to not use per-vertex colors
368 * If present provides colors with method:
369 * void writeValue(int i, GrColor* point) const;
370 * @param indices optional, pass NULL for non-indexed drawing. If
371 * present supplies indices for indexed drawing
372 * with following methods:
373 * int count() const;
374 * void writeValue(int i, uint16_t* point) const;
375 * count returns the number of indices and
376 * writeValue supplies each index.
377 */
378 template <typename POS_SRC,
379 typename TEX_SRC,
380 typename COL_SRC,
381 typename IDX_SRC>
382 void drawCustomVertices(const GrPaint& paint,
383 GrPrimitiveType primitiveType,
384 const POS_SRC& posSrc,
385 const TEX_SRC* texCoordSrc,
386 const COL_SRC* colorSrc,
387 const IDX_SRC* idxSrc);
388 /**
389 * To avoid the problem of having to create a typename for NULL parameters,
390 * these reduced versions of drawCustomVertices are provided.
391 */
392 template <typename POS_SRC>
393 void drawCustomVertices(const GrPaint& paint,
394 GrPrimitiveType primitiveType,
395 const POS_SRC& posSrc);
396 template <typename POS_SRC, typename TEX_SRC>
397 void drawCustomVertices(const GrPaint& paint,
398 GrPrimitiveType primitiveType,
399 const POS_SRC& posSrc,
400 const TEX_SRC* texCoordSrc);
401 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
402 void drawCustomVertices(const GrPaint& paint,
403 GrPrimitiveType primitiveType,
404 const POS_SRC& posSrc,
405 const TEX_SRC* texCoordSrc,
406 const COL_SRC* colorSrc);
407
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408 ///////////////////////////////////////////////////////////////////////////
409 // Misc.
410
411 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000412 * Flags that affect flush() behavior.
413 */
414 enum FlushBits {
415 /**
416 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
417 * it can be rendered to directly. However, Gr lazily sets state. Simply
418 * calling setRenderTarget() followed by flush() without flags may not
419 * bind the render target. This flag forces the context to bind the last
420 * set render target in the 3D API.
421 */
422 kForceCurrentRenderTarget_FlushBit = 0x1,
423 /**
424 * A client may reach a point where it has partially rendered a frame
425 * through a GrContext that it knows the user will never see. This flag
426 * causes the flush to skip submission of deferred content to the 3D API
427 * during the flush.
428 */
429 kDiscard_FlushBit = 0x2,
430 };
431
432 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000433 * Call to ensure all drawing to the context has been issued to the
434 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000435 * @param flagsBitfield flags that control the flushing behavior. See
436 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000437 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000438 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000439
bsalomon@google.com27847de2011-02-22 20:59:41 +0000440 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000441 * Reads a rectangle of pixels from a render target.
442 * @param renderTarget the render target to read from. NULL means the
443 * current render target.
444 * @param left left edge of the rectangle to read (inclusive)
445 * @param top top edge of the rectangle to read (inclusive)
446 * @param width width of rectangle to read in pixels.
447 * @param height height of rectangle to read in pixels.
448 * @param config the pixel config of the destination buffer
449 * @param buffer memory to read the rectangle into.
450 *
451 * @return true if the read succeeded, false if not. The read can fail
452 * because of a unsupported pixel config or because no render
453 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000454 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000455 bool readRenderTargetPixels(GrRenderTarget* target,
456 int left, int top, int width, int height,
457 GrPixelConfig config, void* buffer);
458
459 /**
460 * Reads a rectangle of pixels from a texture.
461 * @param texture the render target to read from.
462 * @param left left edge of the rectangle to read (inclusive)
463 * @param top top edge of the rectangle to read (inclusive)
464 * @param width width of rectangle to read in pixels.
465 * @param height height of rectangle to read in pixels.
466 * @param config the pixel config of the destination buffer
467 * @param buffer memory to read the rectangle into.
468 *
469 * @return true if the read succeeded, false if not. The read can fail
470 * because of a unsupported pixel config.
471 */
472 bool readTexturePixels(GrTexture* target,
473 int left, int top, int width, int height,
474 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000475
476 /**
477 * Copy the src pixels [buffer, stride, pixelconfig] into the current
478 * render-target at the specified rectangle.
479 */
480 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000482
bsalomon@google.com27847de2011-02-22 20:59:41 +0000483 ///////////////////////////////////////////////////////////////////////////
484 // Helpers
485
486 class AutoRenderTarget : ::GrNoncopyable {
487 public:
488 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
489 fContext = NULL;
490 fPrevTarget = context->getRenderTarget();
491 if (fPrevTarget != target) {
492 context->setRenderTarget(target);
493 fContext = context;
494 }
495 }
496 ~AutoRenderTarget() {
497 if (fContext) {
498 fContext->setRenderTarget(fPrevTarget);
499 }
500 }
501 private:
502 GrContext* fContext;
503 GrRenderTarget* fPrevTarget;
504 };
505
506
507 ///////////////////////////////////////////////////////////////////////////
508 // Functions intended for internal use only.
509 GrGpu* getGpu() { return fGpu; }
510 GrFontCache* getFontCache() { return fFontCache; }
511 GrDrawTarget* getTextTarget(const GrPaint& paint);
512 void flushText();
513 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000514 void resetStats();
515 const GrGpuStats& getStats() const;
516 void printStats() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517
518private:
519 // used to keep track of when we need to flush the draw buffer
520 enum DrawCategory {
521 kBuffered_DrawCategory, // last draw was inserted in draw buffer
522 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
523 kText_DrawCategory // text context was last to draw
524 };
525 DrawCategory fLastDrawCategory;
526
527 GrGpu* fGpu;
528 GrTextureCache* fTextureCache;
529 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000530
531 GrPathRenderer* fCustomPathRenderer;
532 GrDefaultPathRenderer fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533
534 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
535 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
536 GrInOrderDrawBuffer* fDrawBuffer;
537
bsalomon@google.com205d4602011-04-25 12:43:45 +0000538 GrIndexBuffer* fAAFillRectIndexBuffer;
539 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000540 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000541
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000543
bsalomon@google.com205d4602011-04-25 12:43:45 +0000544 void fillAARect(GrDrawTarget* target,
545 const GrPaint& paint,
546 const GrRect& devRect);
547
548 void strokeAARect(GrDrawTarget* target,
549 const GrPaint& paint,
550 const GrRect& devRect,
551 const GrVec& devStrokeSize);
552
553 inline int aaFillRectIndexCount() const;
554 GrIndexBuffer* aaFillRectIndexBuffer();
555
556 inline int aaStrokeRectIndexCount() const;
557 GrIndexBuffer* aaStrokeRectIndexBuffer();
558
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000559 void setupDrawBuffer();
560
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561 void flushDrawBuffer();
562
563 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
564
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000565 bool finalizeTextureKey(GrTextureKey*,
566 const GrSamplerState&,
567 bool keyless) const;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000568
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
570
571 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000572
reed@google.com07f3ee12011-05-16 17:21:57 +0000573 GrPathRenderer* getPathRenderer(const GrDrawTarget*, const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000574
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000575 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000576
bsalomon@google.com91958362011-06-13 17:58:13 +0000577 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000578 bool doOffscreenAA(GrDrawTarget* target,
579 const GrPaint& paint,
580 bool isLines) const;
581
bsalomon@google.com91958362011-06-13 17:58:13 +0000582 // attempts to setup offscreen AA. All paint state must be transferred to
583 // target by the time this is called.
584 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000585 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000586 const GrIRect& boundRect,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000587 OffscreenRecord* record);
588
bsalomon@google.com91958362011-06-13 17:58:13 +0000589 // sets up target to draw coverage to the supersampled render target
590 void setupOffscreenAAPass1(GrDrawTarget* target,
591 const GrIRect& boundRect,
592 int tileX, int tileY,
593 OffscreenRecord* record);
594
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000595 // sets up target to sample coverage of supersampled render target back
596 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000597 void doOffscreenAAPass2(GrDrawTarget* target,
598 const GrPaint& paint,
599 const GrIRect& boundRect,
600 int tileX, int tileY,
601 OffscreenRecord* record);
602
603 // restored the draw target state and releases offscreen target to cache
604 void cleanupOffscreenAA(GrDrawTarget* target, OffscreenRecord* record);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000605
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000606 // computes vertex layout bits based on the paint. If paint expresses
607 // a texture for a stage, the stage coords will be bound to postitions
608 // unless hasTexCoords[s]==true in which case stage s's input coords
609 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
610 // for an array where all the values are false.
611 static int PaintStageVertexLayoutBits(
612 const GrPaint& paint,
613 const bool hasTexCoords[GrPaint::kTotalStages]);
614
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615};
616
617/**
618 * Save/restore the view-matrix in the context.
619 */
620class GrAutoMatrix : GrNoncopyable {
621public:
622 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
623 fMatrix = ctx->getMatrix();
624 }
625 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
626 fMatrix = ctx->getMatrix();
627 ctx->setMatrix(matrix);
628 }
629 ~GrAutoMatrix() {
630 fContext->setMatrix(fMatrix);
631 }
632
633private:
634 GrContext* fContext;
635 GrMatrix fMatrix;
636};
637
638#endif
639
640#include "GrContext_impl.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000641