blob: 44e0c041ccf2a692f4bed78231af3824fb7e7bfb [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrContext_DEFINED
11#define GrContext_DEFINED
12
13#include "GrClip.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000014#include "GrPaint.h"
15
bsalomon@google.com583a1e32011-08-17 13:42:46 +000016class GrDefaultPathRenderer;
17class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000018class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000019class GrGpu;
20struct GrGpuStats;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000021class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000022class GrIndexBufferAllocPool;
23class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000024class GrPathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000025class GrResourceEntry;
26class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000027class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029class GrVertexBufferAllocPool;
30
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 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000085 * Token that refers to an entry in the texture cache. Returned by
86 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000087 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000088 class TextureCacheEntry {
89 public:
90 TextureCacheEntry() : fEntry(NULL) {}
91 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
92 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
93 fEntry = e.fEntry;
94 return *this;
95 }
96 GrTexture* texture() const;
97 void reset() { fEntry = NULL; }
98 private:
99 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
100 void set(GrResourceEntry* entry) { fEntry = entry; }
101 GrResourceEntry* cacheEntry() { return fEntry; }
102 GrResourceEntry* fEntry;
103 friend class GrContext;
104 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000105
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000106 /**
107 * Key generated by client. Should be a unique key on the texture data.
108 * Does not need to consider that width and height of the texture. Two
109 * textures with the same TextureKey but different bounds will not collide.
110 */
111 typedef uint64_t TextureKey;
112
113 /**
114 * Search for an entry based on key and dimensions. If found, "lock" it and
115 * return it. The entry's texture() function will return NULL if not found.
116 * Must call be balanced with an unlockTexture() call.
117 */
118 TextureCacheEntry findAndLockTexture(TextureKey key,
119 int width,
120 int height,
121 const GrSamplerState&);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000122
123 /**
124 * Create a new entry, based on the specified key and texture, and return
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000125 * its "locked" entry. Must call be balanced with an unlockTexture() call.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000126 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127 TextureCacheEntry createAndLockTexture(TextureKey key,
128 const GrSamplerState&,
129 const GrTextureDesc&,
130 void* srcData, size_t rowBytes);
131
132 /**
133 * Enum that determines how closely a returned scratch texture must match
134 * a provided GrTextureDesc.
135 */
136 enum ScratchTexMatch {
137 /**
138 * Finds a texture that exactly matches the descriptor.
139 */
140 kExact_ScratchTexMatch,
141 /**
142 * Finds a texture that approximately matches the descriptor. Will be
143 * at least as large in width and height as desc specifies. If desc
144 * specifies that texture is a render target then result will be a
145 * render target. If desc specifies a render target and doesn't set the
146 * no stencil flag then result will have a stencil. Format and aa level
147 * will always match.
148 */
149 kApprox_ScratchTexMatch
150 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000151
152 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000153 * Returns a texture matching the desc. It's contents are unknown. Subsequent
154 * requests with the same descriptor are not guaranteed to return the same
155 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000157 *
158 * Textures created by createAndLockTexture() hide the complications of
159 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000160 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000161 * such an API will create gaps in the tiling pattern. This includes clamp
162 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000163 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000164 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000165
166 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000167 * When done with an entry, call unlockTexture(entry) on it, which returns
168 * it to the cache, where it may be purged.
169 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000170 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000171
172 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173 * Creates a texture that is outside the cache. Does not count against
174 * cache's budget.
175 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000176 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177 void* srcData,
178 size_t rowBytes);
179
180 /**
181 * Returns true if the specified use of an indexed texture is supported.
182 */
183 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
184
185 /**
186 * Return the current texture cache limits.
187 *
188 * @param maxTextures If non-null, returns maximum number of textures that
189 * can be held in the cache.
190 * @param maxTextureBytes If non-null, returns maximum number of bytes of
191 * texture memory that can be held in the cache.
192 */
193 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
194
195 /**
196 * Specify the texture cache limits. If the current cache exceeds either
197 * of these, it will be purged (LRU) to keep the cache within these limits.
198 *
199 * @param maxTextures The maximum number of textures that can be held in
200 * the cache.
201 * @param maxTextureBytes The maximum number of bytes of texture memory
202 * that can be held in the cache.
203 */
204 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
205
206 /**
207 * Return the max width or height of a texture supported by the current gpu
208 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000209 int getMaxTextureSize() const;
210
211 /**
212 * Return the max width or height of a render target supported by the
213 * current gpu
214 */
215 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216
217 ///////////////////////////////////////////////////////////////////////////
218 // Render targets
219
220 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000221 * Sets the render target.
222 * @param target the render target to set. (should not be NULL.)
223 */
224 void setRenderTarget(GrRenderTarget* target);
225
226 /**
227 * Gets the current render target.
228 * @return the currently bound render target. Should never be NULL.
229 */
230 const GrRenderTarget* getRenderTarget() const;
231 GrRenderTarget* getRenderTarget();
232
233 ///////////////////////////////////////////////////////////////////////////
234 // Platform Surfaces
235
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000236 /**
237 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
238 * the type of object returned. If kIsTexture is set the returned object
239 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
240 * are set the render target object is accessible by
241 * GrTexture::asRenderTarget().
242 *
243 * GL: if the object is a texture Gr may change its GL texture parameters
244 * when it is drawn.
245 *
246 * @param desc description of the object to create.
247 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
248 * on failure.
249 */
250 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000251
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000252 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000253 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
254 * viewport state from the underlying 3D API and wraps it in a
255 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
256 * underlying object in its destructor and it is up to caller to guarantee
257 * that it remains valid while the GrRenderTarget is used.
258 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000259 * Will not detect that the render target is also a texture. If you need
260 * to also use the render target as a GrTexture use createPlatformSurface.
261 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000262 * @return the newly created GrRenderTarget
263 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000264 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000265
bsalomon@google.com27847de2011-02-22 20:59:41 +0000266 ///////////////////////////////////////////////////////////////////////////
267 // Matrix state
268
269 /**
270 * Gets the current transformation matrix.
271 * @return the current matrix.
272 */
273 const GrMatrix& getMatrix() const;
274
275 /**
276 * Sets the transformation matrix.
277 * @param m the matrix to set.
278 */
279 void setMatrix(const GrMatrix& m);
280
281 /**
282 * Concats the current matrix. The passed matrix is applied before the
283 * current matrix.
284 * @param m the matrix to concat.
285 */
286 void concatMatrix(const GrMatrix& m) const;
287
288
289 ///////////////////////////////////////////////////////////////////////////
290 // Clip state
291 /**
292 * Gets the current clip.
293 * @return the current clip.
294 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000295 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000296
297 /**
298 * Sets the clip.
299 * @param clip the clip to set.
300 */
301 void setClip(const GrClip& clip);
302
303 /**
304 * Convenience method for setting the clip to a rect.
305 * @param rect the rect to set as the new clip.
306 */
307 void setClip(const GrIRect& rect);
308
309 ///////////////////////////////////////////////////////////////////////////
310 // Draws
311
312 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000313 * Clear the entire or rect of the render target, ignoring any clips.
314 * @param rect the rect to clear or the whole thing if rect is NULL.
315 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000316 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000317 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000318
319 /**
320 * Draw everywhere (respecting the clip) with the paint.
321 */
322 void drawPaint(const GrPaint& paint);
323
324 /**
325 * Draw the rect using a paint.
326 * @param paint describes how to color pixels.
327 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
328 * the rect is mitered stroked based on strokeWidth. If
329 * strokeWidth == 0, then the stroke is always a single
330 * pixel thick.
331 * @param matrix Optional matrix applied to the rect. Applied before
332 * context's matrix or the paint's matrix.
333 * The rects coords are used to access the paint (through texture matrix)
334 */
335 void drawRect(const GrPaint& paint,
336 const GrRect&,
337 GrScalar strokeWidth = -1,
338 const GrMatrix* matrix = NULL);
339
340 /**
341 * Maps a rect of paint coordinates onto the a rect of destination
342 * coordinates. Each rect can optionally be transformed. The srcRect
343 * is stretched over the dstRect. The dstRect is transformed by the
344 * context's matrix and the srcRect is transformed by the paint's matrix.
345 * Additional optional matrices can be provided by parameters.
346 *
347 * @param paint describes how to color pixels.
348 * @param dstRect the destination rect to draw.
349 * @param srcRect rect of paint coordinates to be mapped onto dstRect
350 * @param dstMatrix Optional matrix to transform dstRect. Applied before
351 * context's matrix.
352 * @param srcMatrix Optional matrix to transform srcRect Applied before
353 * paint's matrix.
354 */
355 void drawRectToRect(const GrPaint& paint,
356 const GrRect& dstRect,
357 const GrRect& srcRect,
358 const GrMatrix* dstMatrix = NULL,
359 const GrMatrix* srcMatrix = NULL);
360
361 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000362 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000363 *
364 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000365 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000366 * @param fill the path filling rule to use.
367 * @param translate optional additional translation applied to the
368 * path.
369 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000370 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000371 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000372
bsalomon@google.com27847de2011-02-22 20:59:41 +0000373 /**
374 * Draws vertices with a paint.
375 *
376 * @param paint describes how to color pixels.
377 * @param primitiveType primitives type to draw.
378 * @param vertexCount number of vertices.
379 * @param positions array of vertex positions, required.
380 * @param texCoords optional array of texture coordinates used
381 * to access the paint.
382 * @param colors optional array of per-vertex colors, supercedes
383 * the paint's color field.
384 * @param indices optional array of indices. If NULL vertices
385 * are drawn non-indexed.
386 * @param indexCount if indices is non-null then this is the
387 * number of indices.
388 */
389 void drawVertices(const GrPaint& paint,
390 GrPrimitiveType primitiveType,
391 int vertexCount,
392 const GrPoint positions[],
393 const GrPoint texs[],
394 const GrColor colors[],
395 const uint16_t indices[],
396 int indexCount);
397
bsalomon@google.com27847de2011-02-22 20:59:41 +0000398 ///////////////////////////////////////////////////////////////////////////
399 // Misc.
400
401 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000402 * Flags that affect flush() behavior.
403 */
404 enum FlushBits {
405 /**
406 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
407 * it can be rendered to directly. However, Gr lazily sets state. Simply
408 * calling setRenderTarget() followed by flush() without flags may not
409 * bind the render target. This flag forces the context to bind the last
410 * set render target in the 3D API.
411 */
412 kForceCurrentRenderTarget_FlushBit = 0x1,
413 /**
414 * A client may reach a point where it has partially rendered a frame
415 * through a GrContext that it knows the user will never see. This flag
416 * causes the flush to skip submission of deferred content to the 3D API
417 * during the flush.
418 */
419 kDiscard_FlushBit = 0x2,
420 };
421
422 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000423 * Call to ensure all drawing to the context has been issued to the
424 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000425 * @param flagsBitfield flags that control the flushing behavior. See
426 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000428 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000429
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000431 * Reads a rectangle of pixels from a render target.
432 * @param renderTarget the render target to read from. NULL means the
433 * current render target.
434 * @param left left edge of the rectangle to read (inclusive)
435 * @param top top edge of the rectangle to read (inclusive)
436 * @param width width of rectangle to read in pixels.
437 * @param height height of rectangle to read in pixels.
438 * @param config the pixel config of the destination buffer
439 * @param buffer memory to read the rectangle into.
440 *
441 * @return true if the read succeeded, false if not. The read can fail
442 * because of a unsupported pixel config or because no render
443 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000445 bool readRenderTargetPixels(GrRenderTarget* target,
446 int left, int top, int width, int height,
447 GrPixelConfig config, void* buffer);
448
449 /**
450 * Reads a rectangle of pixels from a texture.
451 * @param texture the render target to read from.
452 * @param left left edge of the rectangle to read (inclusive)
453 * @param top top edge of the rectangle to read (inclusive)
454 * @param width width of rectangle to read in pixels.
455 * @param height height of rectangle to read in pixels.
456 * @param config the pixel config of the destination buffer
457 * @param buffer memory to read the rectangle into.
458 *
459 * @return true if the read succeeded, false if not. The read can fail
460 * because of a unsupported pixel config.
461 */
462 bool readTexturePixels(GrTexture* target,
463 int left, int top, int width, int height,
464 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000465
466 /**
467 * Copy the src pixels [buffer, stride, pixelconfig] into the current
468 * render-target at the specified rectangle.
469 */
470 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000471 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000472
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000473 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000474 * Applies a 1D convolution kernel in the X direction to a rectangle of
475 * pixels from a given texture.
476 * @param texture the texture to read from
477 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000478 * @param kernel the convolution kernel (kernelWidth elements)
479 * @param kernelWidth the width of the convolution kernel
480 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000481 void convolveInX(GrTexture* texture,
482 const SkRect& rect,
483 const float* kernel,
484 int kernelWidth);
485 /**
486 * Applies a 1D convolution kernel in the Y direction to a rectangle of
487 * pixels from a given texture.
488 * direction.
489 * @param texture the texture to read from
490 * @param rect the destination rectangle
491 * @param kernel the convolution kernel (kernelWidth elements)
492 * @param kernelWidth the width of the convolution kernel
493 */
494 void convolveInY(GrTexture* texture,
495 const SkRect& rect,
496 const float* kernel,
497 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000498 ///////////////////////////////////////////////////////////////////////////
499 // Helpers
500
501 class AutoRenderTarget : ::GrNoncopyable {
502 public:
503 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
504 fContext = NULL;
505 fPrevTarget = context->getRenderTarget();
506 if (fPrevTarget != target) {
507 context->setRenderTarget(target);
508 fContext = context;
509 }
510 }
511 ~AutoRenderTarget() {
512 if (fContext) {
513 fContext->setRenderTarget(fPrevTarget);
514 }
515 }
516 private:
517 GrContext* fContext;
518 GrRenderTarget* fPrevTarget;
519 };
520
521
522 ///////////////////////////////////////////////////////////////////////////
523 // Functions intended for internal use only.
524 GrGpu* getGpu() { return fGpu; }
525 GrFontCache* getFontCache() { return fFontCache; }
526 GrDrawTarget* getTextTarget(const GrPaint& paint);
527 void flushText();
528 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000529 void resetStats();
530 const GrGpuStats& getStats() const;
531 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000532 /**
533 * Stencil buffers add themselves to the cache using
534 * addAndLockStencilBuffer. When a SB's RT-attachment count
535 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
536 * eligible for purging. findStencilBuffer is called to check the cache for
537 * a SB that matching an RT's criteria. If a match is found that has been
538 * unlocked (its attachment count has reached 0) then it will be relocked.
539 */
540 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
541 void unlockStencilBuffer(GrResourceEntry* sbEntry);
542 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000543
544private:
545 // used to keep track of when we need to flush the draw buffer
546 enum DrawCategory {
547 kBuffered_DrawCategory, // last draw was inserted in draw buffer
548 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
549 kText_DrawCategory // text context was last to draw
550 };
551 DrawCategory fLastDrawCategory;
552
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000553 GrGpu* fGpu;
554 GrResourceCache* fTextureCache;
555 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000556
557 GrPathRenderer* fCustomPathRenderer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +0000558 GrDefaultPathRenderer* fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559
560 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
561 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
562 GrInOrderDrawBuffer* fDrawBuffer;
563
bsalomon@google.com205d4602011-04-25 12:43:45 +0000564 GrIndexBuffer* fAAFillRectIndexBuffer;
565 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000566 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000567
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000569
bsalomon@google.com205d4602011-04-25 12:43:45 +0000570 void fillAARect(GrDrawTarget* target,
571 const GrPaint& paint,
572 const GrRect& devRect);
573
574 void strokeAARect(GrDrawTarget* target,
575 const GrPaint& paint,
576 const GrRect& devRect,
577 const GrVec& devStrokeSize);
578
579 inline int aaFillRectIndexCount() const;
580 GrIndexBuffer* aaFillRectIndexBuffer();
581
582 inline int aaStrokeRectIndexCount() const;
583 GrIndexBuffer* aaStrokeRectIndexBuffer();
584
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000585 void setupDrawBuffer();
586
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587 void flushDrawBuffer();
588
589 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
590
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
592
593 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000594
bsalomon@google.comee435122011-07-01 14:57:55 +0000595 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000596
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000597 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000598
bsalomon@google.com91958362011-06-13 17:58:13 +0000599 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000600 bool doOffscreenAA(GrDrawTarget* target,
601 const GrPaint& paint,
602 bool isLines) const;
603
bsalomon@google.com91958362011-06-13 17:58:13 +0000604 // attempts to setup offscreen AA. All paint state must be transferred to
605 // target by the time this is called.
606 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000607 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000608 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000609 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000610 OffscreenRecord* record);
611
bsalomon@google.com91958362011-06-13 17:58:13 +0000612 // sets up target to draw coverage to the supersampled render target
613 void setupOffscreenAAPass1(GrDrawTarget* target,
614 const GrIRect& boundRect,
615 int tileX, int tileY,
616 OffscreenRecord* record);
617
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000618 // sets up target to sample coverage of supersampled render target back
619 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000620 void doOffscreenAAPass2(GrDrawTarget* target,
621 const GrPaint& paint,
622 const GrIRect& boundRect,
623 int tileX, int tileY,
624 OffscreenRecord* record);
625
626 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000627 void cleanupOffscreenAA(GrDrawTarget* target,
628 GrPathRenderer* pr,
629 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000630
631 void convolve(GrTexture* texture,
632 const SkRect& rect,
633 float imageIncrement[2],
634 const float* kernel,
635 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000636
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000637 // computes vertex layout bits based on the paint. If paint expresses
638 // a texture for a stage, the stage coords will be bound to postitions
639 // unless hasTexCoords[s]==true in which case stage s's input coords
640 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
641 // for an array where all the values are false.
642 static int PaintStageVertexLayoutBits(
643 const GrPaint& paint,
644 const bool hasTexCoords[GrPaint::kTotalStages]);
645
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646};
647
648/**
649 * Save/restore the view-matrix in the context.
650 */
651class GrAutoMatrix : GrNoncopyable {
652public:
653 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
654 fMatrix = ctx->getMatrix();
655 }
656 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
657 fMatrix = ctx->getMatrix();
658 ctx->setMatrix(matrix);
659 }
660 ~GrAutoMatrix() {
661 fContext->setMatrix(fMatrix);
662 }
663
664private:
665 GrContext* fContext;
666 GrMatrix fMatrix;
667};
668
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000669/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000670 * Gets and locks a scratch texture from a descriptor using
671 * either exact or approximate criteria. Unlocks texture in
672 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000673 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000674class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000675public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000676 GrAutoScratchTexture()
677 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000678 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000679
680 GrAutoScratchTexture(GrContext* context,
681 const GrTextureDesc& desc,
682 GrContext::ScratchTexMatch match =
683 GrContext::kApprox_ScratchTexMatch)
684 : fContext(NULL) {
685 this->set(context, desc, match);
686 }
687
688 ~GrAutoScratchTexture() {
689 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000690 fContext->unlockTexture(fEntry);
691 }
692 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000693
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000694 GrTexture* set(GrContext* context,
695 const GrTextureDesc& desc,
696 GrContext::ScratchTexMatch match =
697 GrContext::kApprox_ScratchTexMatch) {
698 if (NULL != fContext) {
699 fContext->unlockTexture(fEntry);
700 }
701 fContext = context;
702 if (NULL != fContext) {
703 fEntry = fContext->lockScratchTexture(desc, match);
704 GrTexture* ret = fEntry.texture();
705 if (NULL == ret) {
706 fContext = NULL;
707 }
708 return ret;
709 } else {
710 return NULL;
711 }
712 }
713
714 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000715private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000716 GrContext* fContext;
717 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000718};
719
bsalomon@google.com27847de2011-02-22 20:59:41 +0000720#endif
721