blob: 0c873eee36beb8681465822b97a32fc1c09010a4 [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.comb5b31682011-06-16 18:05:35 +0000119 * Finds a texture that approximately matches the descriptor. Will be
120 * at least as large in width and height as desc specifies. If desc
121 * specifies that texture is a render target then result will be a
122 * render target. If desc specifies a render target and doesn't set the
123 * no stencil flag then result will have a stencil. Format and aa level
124 * will always match.
125 */
126 GrTextureEntry* findApproximateKeylessTexture(const GrTextureDesc& desc);
127
128 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000129 * When done with an entry, call unlockTexture(entry) on it, which returns
130 * it to the cache, where it may be purged.
131 */
132 void unlockTexture(GrTextureEntry* entry);
133
134 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000135 * Creates a texture that is outside the cache. Does not count against
136 * cache's budget.
137 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000138 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000139 void* srcData,
140 size_t rowBytes);
141
142 /**
143 * Returns true if the specified use of an indexed texture is supported.
144 */
145 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
146
147 /**
148 * Return the current texture cache limits.
149 *
150 * @param maxTextures If non-null, returns maximum number of textures that
151 * can be held in the cache.
152 * @param maxTextureBytes If non-null, returns maximum number of bytes of
153 * texture memory that can be held in the cache.
154 */
155 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
156
157 /**
158 * Specify the texture cache limits. If the current cache exceeds either
159 * of these, it will be purged (LRU) to keep the cache within these limits.
160 *
161 * @param maxTextures The maximum number of textures that can be held in
162 * the cache.
163 * @param maxTextureBytes The maximum number of bytes of texture memory
164 * that can be held in the cache.
165 */
166 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
167
168 /**
169 * Return the max width or height of a texture supported by the current gpu
170 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000171 int getMaxTextureSize() const;
172
173 /**
174 * Return the max width or height of a render target supported by the
175 * current gpu
176 */
177 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000178
179 ///////////////////////////////////////////////////////////////////////////
180 // Render targets
181
182 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000183 * Sets the render target.
184 * @param target the render target to set. (should not be NULL.)
185 */
186 void setRenderTarget(GrRenderTarget* target);
187
188 /**
189 * Gets the current render target.
190 * @return the currently bound render target. Should never be NULL.
191 */
192 const GrRenderTarget* getRenderTarget() const;
193 GrRenderTarget* getRenderTarget();
194
195 ///////////////////////////////////////////////////////////////////////////
196 // Platform Surfaces
197
198 // GrContext provides an interface for wrapping externally created textures
199 // and rendertargets in their Gr-equivalents.
200
201 /**
202 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
203 * the type of object returned. If kIsTexture is set the returned object
204 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
205 * are set the render target object is accessible by
206 * GrTexture::asRenderTarget().
207 *
208 * GL: if the object is a texture Gr may change its GL texture parameters
209 * when it is drawn.
210 *
211 * @param desc description of the object to create.
212 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
213 * on failure.
214 */
215 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000216 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000217 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
218 * viewport state from the underlying 3D API and wraps it in a
219 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
220 * underlying object in its destructor and it is up to caller to guarantee
221 * that it remains valid while the GrRenderTarget is used.
222 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000223 * Will not detect that the render target is also a texture. If you need
224 * to also use the render target as a GrTexture use createPlatformSurface.
225 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226 * @return the newly created GrRenderTarget
227 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000228 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229
bsalomon@google.com27847de2011-02-22 20:59:41 +0000230 ///////////////////////////////////////////////////////////////////////////
231 // Matrix state
232
233 /**
234 * Gets the current transformation matrix.
235 * @return the current matrix.
236 */
237 const GrMatrix& getMatrix() const;
238
239 /**
240 * Sets the transformation matrix.
241 * @param m the matrix to set.
242 */
243 void setMatrix(const GrMatrix& m);
244
245 /**
246 * Concats the current matrix. The passed matrix is applied before the
247 * current matrix.
248 * @param m the matrix to concat.
249 */
250 void concatMatrix(const GrMatrix& m) const;
251
252
253 ///////////////////////////////////////////////////////////////////////////
254 // Clip state
255 /**
256 * Gets the current clip.
257 * @return the current clip.
258 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000259 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000260
261 /**
262 * Sets the clip.
263 * @param clip the clip to set.
264 */
265 void setClip(const GrClip& clip);
266
267 /**
268 * Convenience method for setting the clip to a rect.
269 * @param rect the rect to set as the new clip.
270 */
271 void setClip(const GrIRect& rect);
272
273 ///////////////////////////////////////////////////////////////////////////
274 // Draws
275
276 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000277 * Clear the entire or rect of the render target, ignoring any clips.
278 * @param rect the rect to clear or the whole thing if rect is NULL.
279 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000280 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000281 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282
283 /**
284 * Draw everywhere (respecting the clip) with the paint.
285 */
286 void drawPaint(const GrPaint& paint);
287
288 /**
289 * Draw the rect using a paint.
290 * @param paint describes how to color pixels.
291 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
292 * the rect is mitered stroked based on strokeWidth. If
293 * strokeWidth == 0, then the stroke is always a single
294 * pixel thick.
295 * @param matrix Optional matrix applied to the rect. Applied before
296 * context's matrix or the paint's matrix.
297 * The rects coords are used to access the paint (through texture matrix)
298 */
299 void drawRect(const GrPaint& paint,
300 const GrRect&,
301 GrScalar strokeWidth = -1,
302 const GrMatrix* matrix = NULL);
303
304 /**
305 * Maps a rect of paint coordinates onto the a rect of destination
306 * coordinates. Each rect can optionally be transformed. The srcRect
307 * is stretched over the dstRect. The dstRect is transformed by the
308 * context's matrix and the srcRect is transformed by the paint's matrix.
309 * Additional optional matrices can be provided by parameters.
310 *
311 * @param paint describes how to color pixels.
312 * @param dstRect the destination rect to draw.
313 * @param srcRect rect of paint coordinates to be mapped onto dstRect
314 * @param dstMatrix Optional matrix to transform dstRect. Applied before
315 * context's matrix.
316 * @param srcMatrix Optional matrix to transform srcRect Applied before
317 * paint's matrix.
318 */
319 void drawRectToRect(const GrPaint& paint,
320 const GrRect& dstRect,
321 const GrRect& srcRect,
322 const GrMatrix* dstMatrix = NULL,
323 const GrMatrix* srcMatrix = NULL);
324
325 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000326 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000327 *
328 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000329 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330 * @param fill the path filling rule to use.
331 * @param translate optional additional translation applied to the
332 * path.
333 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000334 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000335 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000336
bsalomon@google.com27847de2011-02-22 20:59:41 +0000337 /**
338 * Draws vertices with a paint.
339 *
340 * @param paint describes how to color pixels.
341 * @param primitiveType primitives type to draw.
342 * @param vertexCount number of vertices.
343 * @param positions array of vertex positions, required.
344 * @param texCoords optional array of texture coordinates used
345 * to access the paint.
346 * @param colors optional array of per-vertex colors, supercedes
347 * the paint's color field.
348 * @param indices optional array of indices. If NULL vertices
349 * are drawn non-indexed.
350 * @param indexCount if indices is non-null then this is the
351 * number of indices.
352 */
353 void drawVertices(const GrPaint& paint,
354 GrPrimitiveType primitiveType,
355 int vertexCount,
356 const GrPoint positions[],
357 const GrPoint texs[],
358 const GrColor colors[],
359 const uint16_t indices[],
360 int indexCount);
361
362 /**
363 * Similar to drawVertices but caller provides objects that convert to Gr
364 * types. The count of vertices is given by posSrc.
365 *
366 * @param paint describes how to color pixels.
367 * @param primitiveType primitives type to draw.
368 * @param posSrc Source of vertex positions. Must implement
369 * int count() const;
370 * void writeValue(int i, GrPoint* point) const;
371 * count returns the total number of vertices and
372 * writeValue writes a vertex position to point.
373 * @param texSrc optional, pass NULL to not use explicit tex
374 * coords. If present provides tex coords with
375 * method:
376 * void writeValue(int i, GrPoint* point) const;
377 * @param texSrc optional, pass NULL to not use per-vertex colors
378 * If present provides colors with method:
379 * void writeValue(int i, GrColor* point) const;
380 * @param indices optional, pass NULL for non-indexed drawing. If
381 * present supplies indices for indexed drawing
382 * with following methods:
383 * int count() const;
384 * void writeValue(int i, uint16_t* point) const;
385 * count returns the number of indices and
386 * writeValue supplies each index.
387 */
388 template <typename POS_SRC,
389 typename TEX_SRC,
390 typename COL_SRC,
391 typename IDX_SRC>
392 void drawCustomVertices(const GrPaint& paint,
393 GrPrimitiveType primitiveType,
394 const POS_SRC& posSrc,
395 const TEX_SRC* texCoordSrc,
396 const COL_SRC* colorSrc,
397 const IDX_SRC* idxSrc);
398 /**
399 * To avoid the problem of having to create a typename for NULL parameters,
400 * these reduced versions of drawCustomVertices are provided.
401 */
402 template <typename POS_SRC>
403 void drawCustomVertices(const GrPaint& paint,
404 GrPrimitiveType primitiveType,
405 const POS_SRC& posSrc);
406 template <typename POS_SRC, typename TEX_SRC>
407 void drawCustomVertices(const GrPaint& paint,
408 GrPrimitiveType primitiveType,
409 const POS_SRC& posSrc,
410 const TEX_SRC* texCoordSrc);
411 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
412 void drawCustomVertices(const GrPaint& paint,
413 GrPrimitiveType primitiveType,
414 const POS_SRC& posSrc,
415 const TEX_SRC* texCoordSrc,
416 const COL_SRC* colorSrc);
417
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 ///////////////////////////////////////////////////////////////////////////
419 // Misc.
420
421 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000422 * Flags that affect flush() behavior.
423 */
424 enum FlushBits {
425 /**
426 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
427 * it can be rendered to directly. However, Gr lazily sets state. Simply
428 * calling setRenderTarget() followed by flush() without flags may not
429 * bind the render target. This flag forces the context to bind the last
430 * set render target in the 3D API.
431 */
432 kForceCurrentRenderTarget_FlushBit = 0x1,
433 /**
434 * A client may reach a point where it has partially rendered a frame
435 * through a GrContext that it knows the user will never see. This flag
436 * causes the flush to skip submission of deferred content to the 3D API
437 * during the flush.
438 */
439 kDiscard_FlushBit = 0x2,
440 };
441
442 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 * Call to ensure all drawing to the context has been issued to the
444 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000445 * @param flagsBitfield flags that control the flushing behavior. See
446 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000447 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000448 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000449
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000451 * Reads a rectangle of pixels from a render target.
452 * @param renderTarget the render target to read from. NULL means the
453 * current render target.
454 * @param left left edge of the rectangle to read (inclusive)
455 * @param top top edge of the rectangle to read (inclusive)
456 * @param width width of rectangle to read in pixels.
457 * @param height height of rectangle to read in pixels.
458 * @param config the pixel config of the destination buffer
459 * @param buffer memory to read the rectangle into.
460 *
461 * @return true if the read succeeded, false if not. The read can fail
462 * because of a unsupported pixel config or because no render
463 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000464 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000465 bool readRenderTargetPixels(GrRenderTarget* target,
466 int left, int top, int width, int height,
467 GrPixelConfig config, void* buffer);
468
469 /**
470 * Reads a rectangle of pixels from a texture.
471 * @param texture the render target to read from.
472 * @param left left edge of the rectangle to read (inclusive)
473 * @param top top edge of the rectangle to read (inclusive)
474 * @param width width of rectangle to read in pixels.
475 * @param height height of rectangle to read in pixels.
476 * @param config the pixel config of the destination buffer
477 * @param buffer memory to read the rectangle into.
478 *
479 * @return true if the read succeeded, false if not. The read can fail
480 * because of a unsupported pixel config.
481 */
482 bool readTexturePixels(GrTexture* target,
483 int left, int top, int width, int height,
484 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485
486 /**
487 * Copy the src pixels [buffer, stride, pixelconfig] into the current
488 * render-target at the specified rectangle.
489 */
490 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000491 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000492
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000493 /**
494 * Performs a 1D convolution over a rectangle of pixels. Set
495 * imageIncrement to (1/w, 0) for a convolution in X, (0, 1/h) for a
496 * convolution in Y, where w, h are the texture dimensions.
497 * @param srcTexture the texture to read from
498 * @param dstRect the destination rectangle
499 * @param imageIncrement the displacement between pixel samples
500 * @param kernel the convolution kernel (kernelWidth elements)
501 * @param kernelWidth the width of the convolution kernel
502 */
503 void convolveRect(GrTexture* srcTexture,
504 const SkRect& dstRect,
505 float imageIncrement[2],
506 const float* kernel,
507 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000508 ///////////////////////////////////////////////////////////////////////////
509 // Helpers
510
511 class AutoRenderTarget : ::GrNoncopyable {
512 public:
513 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
514 fContext = NULL;
515 fPrevTarget = context->getRenderTarget();
516 if (fPrevTarget != target) {
517 context->setRenderTarget(target);
518 fContext = context;
519 }
520 }
521 ~AutoRenderTarget() {
522 if (fContext) {
523 fContext->setRenderTarget(fPrevTarget);
524 }
525 }
526 private:
527 GrContext* fContext;
528 GrRenderTarget* fPrevTarget;
529 };
530
531
532 ///////////////////////////////////////////////////////////////////////////
533 // Functions intended for internal use only.
534 GrGpu* getGpu() { return fGpu; }
535 GrFontCache* getFontCache() { return fFontCache; }
536 GrDrawTarget* getTextTarget(const GrPaint& paint);
537 void flushText();
538 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000539 void resetStats();
540 const GrGpuStats& getStats() const;
541 void printStats() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542
543private:
544 // used to keep track of when we need to flush the draw buffer
545 enum DrawCategory {
546 kBuffered_DrawCategory, // last draw was inserted in draw buffer
547 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
548 kText_DrawCategory // text context was last to draw
549 };
550 DrawCategory fLastDrawCategory;
551
552 GrGpu* fGpu;
553 GrTextureCache* fTextureCache;
554 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000555
556 GrPathRenderer* fCustomPathRenderer;
557 GrDefaultPathRenderer fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558
559 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
560 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
561 GrInOrderDrawBuffer* fDrawBuffer;
562
bsalomon@google.com205d4602011-04-25 12:43:45 +0000563 GrIndexBuffer* fAAFillRectIndexBuffer;
564 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000565 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000566
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000568
bsalomon@google.com205d4602011-04-25 12:43:45 +0000569 void fillAARect(GrDrawTarget* target,
570 const GrPaint& paint,
571 const GrRect& devRect);
572
573 void strokeAARect(GrDrawTarget* target,
574 const GrPaint& paint,
575 const GrRect& devRect,
576 const GrVec& devStrokeSize);
577
578 inline int aaFillRectIndexCount() const;
579 GrIndexBuffer* aaFillRectIndexBuffer();
580
581 inline int aaStrokeRectIndexCount() const;
582 GrIndexBuffer* aaStrokeRectIndexBuffer();
583
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000584 void setupDrawBuffer();
585
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586 void flushDrawBuffer();
587
588 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
589
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000590 bool finalizeTextureKey(GrTextureKey*,
591 const GrSamplerState&,
592 bool keyless) const;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000593
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
595
596 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000597
bsalomon@google.comee435122011-07-01 14:57:55 +0000598 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000599
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000600 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000601
bsalomon@google.com91958362011-06-13 17:58:13 +0000602 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000603 bool doOffscreenAA(GrDrawTarget* target,
604 const GrPaint& paint,
605 bool isLines) const;
606
bsalomon@google.com91958362011-06-13 17:58:13 +0000607 // attempts to setup offscreen AA. All paint state must be transferred to
608 // target by the time this is called.
609 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000610 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000611 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000612 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000613 OffscreenRecord* record);
614
bsalomon@google.com91958362011-06-13 17:58:13 +0000615 // sets up target to draw coverage to the supersampled render target
616 void setupOffscreenAAPass1(GrDrawTarget* target,
617 const GrIRect& boundRect,
618 int tileX, int tileY,
619 OffscreenRecord* record);
620
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000621 // sets up target to sample coverage of supersampled render target back
622 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000623 void doOffscreenAAPass2(GrDrawTarget* target,
624 const GrPaint& paint,
625 const GrIRect& boundRect,
626 int tileX, int tileY,
627 OffscreenRecord* record);
628
629 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000630 void cleanupOffscreenAA(GrDrawTarget* target,
631 GrPathRenderer* pr,
632 OffscreenRecord* record);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000633
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000634 // computes vertex layout bits based on the paint. If paint expresses
635 // a texture for a stage, the stage coords will be bound to postitions
636 // unless hasTexCoords[s]==true in which case stage s's input coords
637 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
638 // for an array where all the values are false.
639 static int PaintStageVertexLayoutBits(
640 const GrPaint& paint,
641 const bool hasTexCoords[GrPaint::kTotalStages]);
642
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643};
644
645/**
646 * Save/restore the view-matrix in the context.
647 */
648class GrAutoMatrix : GrNoncopyable {
649public:
650 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
651 fMatrix = ctx->getMatrix();
652 }
653 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
654 fMatrix = ctx->getMatrix();
655 ctx->setMatrix(matrix);
656 }
657 ~GrAutoMatrix() {
658 fContext->setMatrix(fMatrix);
659 }
660
661private:
662 GrContext* fContext;
663 GrMatrix fMatrix;
664};
665
666#endif
667
668#include "GrContext_impl.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000669