blob: 2f649966b5876764c1d55dab24950729d423e24b [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"
bsalomon@google.comc287a892011-08-19 14:49:36 +000015// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
16// remove.
17#include "GrRenderTarget.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018
bsalomon@google.com583a1e32011-08-17 13:42:46 +000019class GrDefaultPathRenderer;
20class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000021class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000022class GrGpu;
23struct GrGpuStats;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000024class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000025class GrIndexBufferAllocPool;
26class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000027class GrPathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028class GrResourceEntry;
29class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000030class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000031class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000032class GrVertexBufferAllocPool;
33
bsalomon@google.com91826102011-03-21 19:51:57 +000034class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000035public:
36 /**
37 * Creates a GrContext from within a 3D context.
38 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000039 static GrContext* Create(GrEngine engine,
40 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000041
42 /**
43 * Helper to create a opengl-shader based context
44 */
45 static GrContext* CreateGLShaderContext();
46
47 virtual ~GrContext();
48
49 /**
50 * The GrContext normally assumes that no outsider is setting state
51 * within the underlying 3D API's context/device/whatever. This call informs
52 * the context that the state was modified and it should resend. Shouldn't
53 * be called frequently for good performance.
54 */
55 void resetContext();
56
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057 /**
58 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
59 * if you have lost the associated GPU context, and thus internal texture,
60 * buffer, etc. references/IDs are now invalid. Should be called even when
61 * GrContext is no longer going to be used for two reasons:
62 * 1) ~GrContext will not try to free the objects in the 3D API.
63 * 2) If you've created GrResources that outlive the GrContext they will
64 * be marked as invalid (GrResource::isValid()) and won't attempt to
65 * free their underlying resource in the 3D API.
66 * Content drawn since the last GrContext::flush() may be lost.
67 */
68 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000069
70 /**
junov@google.com53a55842011-06-08 22:55:10 +000071 * Similar to contextLost, but makes no attempt to reset state.
72 * Use this method when GrContext destruction is pending, but
73 * the graphics context is destroyed first.
74 */
75 void contextDestroyed();
76
77 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 * Frees gpu created by the context. Can be called to reduce GPU memory
79 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000080 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 void freeGpuResources();
82
83 ///////////////////////////////////////////////////////////////////////////
84 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000085
86 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000087 * Token that refers to an entry in the texture cache. Returned by
88 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000089 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 class TextureCacheEntry {
91 public:
92 TextureCacheEntry() : fEntry(NULL) {}
93 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
94 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
95 fEntry = e.fEntry;
96 return *this;
97 }
98 GrTexture* texture() const;
99 void reset() { fEntry = NULL; }
100 private:
101 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
102 void set(GrResourceEntry* entry) { fEntry = entry; }
103 GrResourceEntry* cacheEntry() { return fEntry; }
104 GrResourceEntry* fEntry;
105 friend class GrContext;
106 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000107
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000108 /**
109 * Key generated by client. Should be a unique key on the texture data.
110 * Does not need to consider that width and height of the texture. Two
111 * textures with the same TextureKey but different bounds will not collide.
112 */
113 typedef uint64_t TextureKey;
114
115 /**
116 * Search for an entry based on key and dimensions. If found, "lock" it and
117 * return it. The entry's texture() function will return NULL if not found.
118 * Must call be balanced with an unlockTexture() call.
119 */
120 TextureCacheEntry findAndLockTexture(TextureKey key,
121 int width,
122 int height,
123 const GrSamplerState&);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000124
125 /**
126 * Create a new entry, based on the specified key and texture, and return
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127 * its "locked" entry. Must call be balanced with an unlockTexture() call.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000128 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 TextureCacheEntry createAndLockTexture(TextureKey key,
130 const GrSamplerState&,
131 const GrTextureDesc&,
132 void* srcData, size_t rowBytes);
133
134 /**
135 * Enum that determines how closely a returned scratch texture must match
136 * a provided GrTextureDesc.
137 */
138 enum ScratchTexMatch {
139 /**
140 * Finds a texture that exactly matches the descriptor.
141 */
142 kExact_ScratchTexMatch,
143 /**
144 * Finds a texture that approximately matches the descriptor. Will be
145 * at least as large in width and height as desc specifies. If desc
146 * specifies that texture is a render target then result will be a
147 * render target. If desc specifies a render target and doesn't set the
148 * no stencil flag then result will have a stencil. Format and aa level
149 * will always match.
150 */
151 kApprox_ScratchTexMatch
152 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000153
154 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000155 * Returns a texture matching the desc. It's contents are unknown. Subsequent
156 * requests with the same descriptor are not guaranteed to return the same
157 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000158 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000159 *
160 * Textures created by createAndLockTexture() hide the complications of
161 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000162 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000163 * such an API will create gaps in the tiling pattern. This includes clamp
164 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000166 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000167
168 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000169 * When done with an entry, call unlockTexture(entry) on it, which returns
170 * it to the cache, where it may be purged.
171 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173
174 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000175 * Creates a texture that is outside the cache. Does not count against
176 * cache's budget.
177 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000178 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000179 void* srcData,
180 size_t rowBytes);
181
182 /**
183 * Returns true if the specified use of an indexed texture is supported.
184 */
185 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
186
187 /**
188 * Return the current texture cache limits.
189 *
190 * @param maxTextures If non-null, returns maximum number of textures that
191 * can be held in the cache.
192 * @param maxTextureBytes If non-null, returns maximum number of bytes of
193 * texture memory that can be held in the cache.
194 */
195 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
196
197 /**
198 * Specify the texture cache limits. If the current cache exceeds either
199 * of these, it will be purged (LRU) to keep the cache within these limits.
200 *
201 * @param maxTextures The maximum number of textures that can be held in
202 * the cache.
203 * @param maxTextureBytes The maximum number of bytes of texture memory
204 * that can be held in the cache.
205 */
206 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
207
208 /**
209 * Return the max width or height of a texture supported by the current gpu
210 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000211 int getMaxTextureSize() const;
212
213 /**
214 * Return the max width or height of a render target supported by the
215 * current gpu
216 */
217 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218
219 ///////////////////////////////////////////////////////////////////////////
220 // Render targets
221
222 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000223 * Sets the render target.
224 * @param target the render target to set. (should not be NULL.)
225 */
226 void setRenderTarget(GrRenderTarget* target);
227
228 /**
229 * Gets the current render target.
230 * @return the currently bound render target. Should never be NULL.
231 */
232 const GrRenderTarget* getRenderTarget() const;
233 GrRenderTarget* getRenderTarget();
234
235 ///////////////////////////////////////////////////////////////////////////
236 // Platform Surfaces
237
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000238 /**
239 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
240 * the type of object returned. If kIsTexture is set the returned object
241 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
242 * are set the render target object is accessible by
243 * GrTexture::asRenderTarget().
244 *
245 * GL: if the object is a texture Gr may change its GL texture parameters
246 * when it is drawn.
247 *
248 * @param desc description of the object to create.
249 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
250 * on failure.
251 */
252 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000254 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000255 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
256 * viewport state from the underlying 3D API and wraps it in a
257 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
258 * underlying object in its destructor and it is up to caller to guarantee
259 * that it remains valid while the GrRenderTarget is used.
260 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000261 * Will not detect that the render target is also a texture. If you need
262 * to also use the render target as a GrTexture use createPlatformSurface.
263 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264 * @return the newly created GrRenderTarget
265 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000266 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000267
bsalomon@google.com27847de2011-02-22 20:59:41 +0000268 ///////////////////////////////////////////////////////////////////////////
269 // Matrix state
270
271 /**
272 * Gets the current transformation matrix.
273 * @return the current matrix.
274 */
275 const GrMatrix& getMatrix() const;
276
277 /**
278 * Sets the transformation matrix.
279 * @param m the matrix to set.
280 */
281 void setMatrix(const GrMatrix& m);
282
283 /**
284 * Concats the current matrix. The passed matrix is applied before the
285 * current matrix.
286 * @param m the matrix to concat.
287 */
288 void concatMatrix(const GrMatrix& m) const;
289
290
291 ///////////////////////////////////////////////////////////////////////////
292 // Clip state
293 /**
294 * Gets the current clip.
295 * @return the current clip.
296 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000297 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298
299 /**
300 * Sets the clip.
301 * @param clip the clip to set.
302 */
303 void setClip(const GrClip& clip);
304
305 /**
306 * Convenience method for setting the clip to a rect.
307 * @param rect the rect to set as the new clip.
308 */
309 void setClip(const GrIRect& rect);
310
311 ///////////////////////////////////////////////////////////////////////////
312 // Draws
313
314 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000315 * Clear the entire or rect of the render target, ignoring any clips.
316 * @param rect the rect to clear or the whole thing if rect is NULL.
317 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000318 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000319 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000320
321 /**
322 * Draw everywhere (respecting the clip) with the paint.
323 */
324 void drawPaint(const GrPaint& paint);
325
326 /**
327 * Draw the rect using a paint.
328 * @param paint describes how to color pixels.
329 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
330 * the rect is mitered stroked based on strokeWidth. If
331 * strokeWidth == 0, then the stroke is always a single
332 * pixel thick.
333 * @param matrix Optional matrix applied to the rect. Applied before
334 * context's matrix or the paint's matrix.
335 * The rects coords are used to access the paint (through texture matrix)
336 */
337 void drawRect(const GrPaint& paint,
338 const GrRect&,
339 GrScalar strokeWidth = -1,
340 const GrMatrix* matrix = NULL);
341
342 /**
343 * Maps a rect of paint coordinates onto the a rect of destination
344 * coordinates. Each rect can optionally be transformed. The srcRect
345 * is stretched over the dstRect. The dstRect is transformed by the
346 * context's matrix and the srcRect is transformed by the paint's matrix.
347 * Additional optional matrices can be provided by parameters.
348 *
349 * @param paint describes how to color pixels.
350 * @param dstRect the destination rect to draw.
351 * @param srcRect rect of paint coordinates to be mapped onto dstRect
352 * @param dstMatrix Optional matrix to transform dstRect. Applied before
353 * context's matrix.
354 * @param srcMatrix Optional matrix to transform srcRect Applied before
355 * paint's matrix.
356 */
357 void drawRectToRect(const GrPaint& paint,
358 const GrRect& dstRect,
359 const GrRect& srcRect,
360 const GrMatrix* dstMatrix = NULL,
361 const GrMatrix* srcMatrix = NULL);
362
363 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000365 *
366 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000367 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000368 * @param fill the path filling rule to use.
369 * @param translate optional additional translation applied to the
370 * path.
371 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000372 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000374
bsalomon@google.com27847de2011-02-22 20:59:41 +0000375 /**
376 * Draws vertices with a paint.
377 *
378 * @param paint describes how to color pixels.
379 * @param primitiveType primitives type to draw.
380 * @param vertexCount number of vertices.
381 * @param positions array of vertex positions, required.
382 * @param texCoords optional array of texture coordinates used
383 * to access the paint.
384 * @param colors optional array of per-vertex colors, supercedes
385 * the paint's color field.
386 * @param indices optional array of indices. If NULL vertices
387 * are drawn non-indexed.
388 * @param indexCount if indices is non-null then this is the
389 * number of indices.
390 */
391 void drawVertices(const GrPaint& paint,
392 GrPrimitiveType primitiveType,
393 int vertexCount,
394 const GrPoint positions[],
395 const GrPoint texs[],
396 const GrColor colors[],
397 const uint16_t indices[],
398 int indexCount);
399
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400 ///////////////////////////////////////////////////////////////////////////
401 // Misc.
402
403 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000404 * Flags that affect flush() behavior.
405 */
406 enum FlushBits {
407 /**
408 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
409 * it can be rendered to directly. However, Gr lazily sets state. Simply
410 * calling setRenderTarget() followed by flush() without flags may not
411 * bind the render target. This flag forces the context to bind the last
412 * set render target in the 3D API.
413 */
414 kForceCurrentRenderTarget_FlushBit = 0x1,
415 /**
416 * A client may reach a point where it has partially rendered a frame
417 * through a GrContext that it knows the user will never see. This flag
418 * causes the flush to skip submission of deferred content to the 3D API
419 * during the flush.
420 */
421 kDiscard_FlushBit = 0x2,
422 };
423
424 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 * Call to ensure all drawing to the context has been issued to the
426 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000427 * @param flagsBitfield flags that control the flushing behavior. See
428 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000429 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000430 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000431
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000433 * Reads a rectangle of pixels from a render target.
434 * @param renderTarget the render target to read from. NULL means the
435 * current render target.
436 * @param left left edge of the rectangle to read (inclusive)
437 * @param top top edge of the rectangle to read (inclusive)
438 * @param width width of rectangle to read in pixels.
439 * @param height height of rectangle to read in pixels.
440 * @param config the pixel config of the destination buffer
441 * @param buffer memory to read the rectangle into.
442 *
443 * @return true if the read succeeded, false if not. The read can fail
444 * because of a unsupported pixel config or because no render
445 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000447 bool readRenderTargetPixels(GrRenderTarget* target,
448 int left, int top, int width, int height,
449 GrPixelConfig config, void* buffer);
450
451 /**
452 * Reads a rectangle of pixels from a texture.
453 * @param texture the render target to read from.
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.
463 */
464 bool readTexturePixels(GrTexture* target,
465 int left, int top, int width, int height,
466 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467
468 /**
469 * Copy the src pixels [buffer, stride, pixelconfig] into the current
470 * render-target at the specified rectangle.
471 */
472 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000473 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000475 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000476 * Applies a 1D convolution kernel in the X direction to a rectangle of
477 * pixels from a given texture.
478 * @param texture the texture to read from
479 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000480 * @param kernel the convolution kernel (kernelWidth elements)
481 * @param kernelWidth the width of the convolution kernel
482 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000483 void convolveInX(GrTexture* texture,
484 const SkRect& rect,
485 const float* kernel,
486 int kernelWidth);
487 /**
488 * Applies a 1D convolution kernel in the Y direction to a rectangle of
489 * pixels from a given texture.
490 * direction.
491 * @param texture the texture to read from
492 * @param rect the destination rectangle
493 * @param kernel the convolution kernel (kernelWidth elements)
494 * @param kernelWidth the width of the convolution kernel
495 */
496 void convolveInY(GrTexture* texture,
497 const SkRect& rect,
498 const float* kernel,
499 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000500 ///////////////////////////////////////////////////////////////////////////
501 // Helpers
502
503 class AutoRenderTarget : ::GrNoncopyable {
504 public:
505 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
506 fContext = NULL;
507 fPrevTarget = context->getRenderTarget();
508 if (fPrevTarget != target) {
509 context->setRenderTarget(target);
510 fContext = context;
511 }
512 }
513 ~AutoRenderTarget() {
514 if (fContext) {
515 fContext->setRenderTarget(fPrevTarget);
516 }
517 }
518 private:
519 GrContext* fContext;
520 GrRenderTarget* fPrevTarget;
521 };
522
523
524 ///////////////////////////////////////////////////////////////////////////
525 // Functions intended for internal use only.
526 GrGpu* getGpu() { return fGpu; }
527 GrFontCache* getFontCache() { return fFontCache; }
528 GrDrawTarget* getTextTarget(const GrPaint& paint);
529 void flushText();
530 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000531 void resetStats();
532 const GrGpuStats& getStats() const;
533 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000534 /**
535 * Stencil buffers add themselves to the cache using
536 * addAndLockStencilBuffer. When a SB's RT-attachment count
537 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
538 * eligible for purging. findStencilBuffer is called to check the cache for
539 * a SB that matching an RT's criteria. If a match is found that has been
540 * unlocked (its attachment count has reached 0) then it will be relocked.
541 */
542 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
543 void unlockStencilBuffer(GrResourceEntry* sbEntry);
544 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545
546private:
547 // used to keep track of when we need to flush the draw buffer
548 enum DrawCategory {
549 kBuffered_DrawCategory, // last draw was inserted in draw buffer
550 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
551 kText_DrawCategory // text context was last to draw
552 };
553 DrawCategory fLastDrawCategory;
554
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000555 GrGpu* fGpu;
556 GrResourceCache* fTextureCache;
557 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000558
559 GrPathRenderer* fCustomPathRenderer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +0000560 GrDefaultPathRenderer* fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561
562 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
563 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
564 GrInOrderDrawBuffer* fDrawBuffer;
565
bsalomon@google.com205d4602011-04-25 12:43:45 +0000566 GrIndexBuffer* fAAFillRectIndexBuffer;
567 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000568 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000569
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000571
bsalomon@google.com205d4602011-04-25 12:43:45 +0000572 void fillAARect(GrDrawTarget* target,
573 const GrPaint& paint,
574 const GrRect& devRect);
575
576 void strokeAARect(GrDrawTarget* target,
577 const GrPaint& paint,
578 const GrRect& devRect,
579 const GrVec& devStrokeSize);
580
581 inline int aaFillRectIndexCount() const;
582 GrIndexBuffer* aaFillRectIndexBuffer();
583
584 inline int aaStrokeRectIndexCount() const;
585 GrIndexBuffer* aaStrokeRectIndexBuffer();
586
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000587 void setupDrawBuffer();
588
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589 void flushDrawBuffer();
590
591 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
592
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
594
595 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000596
bsalomon@google.comee435122011-07-01 14:57:55 +0000597 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000598
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000599 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000600
bsalomon@google.com91958362011-06-13 17:58:13 +0000601 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000602 bool doOffscreenAA(GrDrawTarget* target,
603 const GrPaint& paint,
604 bool isLines) const;
605
bsalomon@google.com91958362011-06-13 17:58:13 +0000606 // attempts to setup offscreen AA. All paint state must be transferred to
607 // target by the time this is called.
608 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000609 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000610 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000611 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000612 OffscreenRecord* record);
613
bsalomon@google.com91958362011-06-13 17:58:13 +0000614 // sets up target to draw coverage to the supersampled render target
615 void setupOffscreenAAPass1(GrDrawTarget* target,
616 const GrIRect& boundRect,
617 int tileX, int tileY,
618 OffscreenRecord* record);
619
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000620 // sets up target to sample coverage of supersampled render target back
621 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000622 void doOffscreenAAPass2(GrDrawTarget* target,
623 const GrPaint& paint,
624 const GrIRect& boundRect,
625 int tileX, int tileY,
626 OffscreenRecord* record);
627
628 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000629 void cleanupOffscreenAA(GrDrawTarget* target,
630 GrPathRenderer* pr,
631 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000632
633 void convolve(GrTexture* texture,
634 const SkRect& rect,
635 float imageIncrement[2],
636 const float* kernel,
637 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000638
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000639 // computes vertex layout bits based on the paint. If paint expresses
640 // a texture for a stage, the stage coords will be bound to postitions
641 // unless hasTexCoords[s]==true in which case stage s's input coords
642 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
643 // for an array where all the values are false.
644 static int PaintStageVertexLayoutBits(
645 const GrPaint& paint,
646 const bool hasTexCoords[GrPaint::kTotalStages]);
647
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648};
649
650/**
651 * Save/restore the view-matrix in the context.
652 */
653class GrAutoMatrix : GrNoncopyable {
654public:
655 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
656 fMatrix = ctx->getMatrix();
657 }
658 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
659 fMatrix = ctx->getMatrix();
660 ctx->setMatrix(matrix);
661 }
662 ~GrAutoMatrix() {
663 fContext->setMatrix(fMatrix);
664 }
665
666private:
667 GrContext* fContext;
668 GrMatrix fMatrix;
669};
670
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000671/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000672 * Gets and locks a scratch texture from a descriptor using
673 * either exact or approximate criteria. Unlocks texture in
674 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000675 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000676class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000677public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000678 GrAutoScratchTexture()
679 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000680 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000681
682 GrAutoScratchTexture(GrContext* context,
683 const GrTextureDesc& desc,
684 GrContext::ScratchTexMatch match =
685 GrContext::kApprox_ScratchTexMatch)
686 : fContext(NULL) {
687 this->set(context, desc, match);
688 }
689
690 ~GrAutoScratchTexture() {
691 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000692 fContext->unlockTexture(fEntry);
693 }
694 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000695
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000696 GrTexture* set(GrContext* context,
697 const GrTextureDesc& desc,
698 GrContext::ScratchTexMatch match =
699 GrContext::kApprox_ScratchTexMatch) {
700 if (NULL != fContext) {
701 fContext->unlockTexture(fEntry);
702 }
703 fContext = context;
704 if (NULL != fContext) {
705 fEntry = fContext->lockScratchTexture(desc, match);
706 GrTexture* ret = fEntry.texture();
707 if (NULL == ret) {
708 fContext = NULL;
709 }
710 return ret;
711 } else {
712 return NULL;
713 }
714 }
715
716 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000717private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000718 GrContext* fContext;
719 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000720};
721
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722#endif
723