blob: b2928f8af20a20160f04f49b27a2508836b41937 [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.comdfe75bc2011-03-25 12:31:16 +000015#include "GrPathRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000016
17class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000018class GrGpu;
19struct GrGpuStats;
bsalomon@google.com27847de2011-02-22 20:59:41 +000020class GrIndexBufferAllocPool;
21class GrInOrderDrawBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022class GrResourceEntry;
23class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024class GrStencilBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000025class GrVertexBufferAllocPool;
26
bsalomon@google.com27847de2011-02-22 20:59:41 +000027
bsalomon@google.com91826102011-03-21 19:51:57 +000028class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000029public:
30 /**
31 * Creates a GrContext from within a 3D context.
32 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000033 static GrContext* Create(GrEngine engine,
34 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
36 /**
37 * Helper to create a opengl-shader based context
38 */
39 static GrContext* CreateGLShaderContext();
40
41 virtual ~GrContext();
42
43 /**
44 * The GrContext normally assumes that no outsider is setting state
45 * within the underlying 3D API's context/device/whatever. This call informs
46 * the context that the state was modified and it should resend. Shouldn't
47 * be called frequently for good performance.
48 */
49 void resetContext();
50
bsalomon@google.com8fe72472011-03-30 21:26:44 +000051 /**
52 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
53 * if you have lost the associated GPU context, and thus internal texture,
54 * buffer, etc. references/IDs are now invalid. Should be called even when
55 * GrContext is no longer going to be used for two reasons:
56 * 1) ~GrContext will not try to free the objects in the 3D API.
57 * 2) If you've created GrResources that outlive the GrContext they will
58 * be marked as invalid (GrResource::isValid()) and won't attempt to
59 * free their underlying resource in the 3D API.
60 * Content drawn since the last GrContext::flush() may be lost.
61 */
62 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000063
64 /**
junov@google.com53a55842011-06-08 22:55:10 +000065 * Similar to contextLost, but makes no attempt to reset state.
66 * Use this method when GrContext destruction is pending, but
67 * the graphics context is destroyed first.
68 */
69 void contextDestroyed();
70
71 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 * Frees gpu created by the context. Can be called to reduce GPU memory
73 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000074 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 void freeGpuResources();
76
77 ///////////////////////////////////////////////////////////////////////////
78 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000079
80 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000081 * Token that refers to an entry in the texture cache. Returned by
82 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000083 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000084 class TextureCacheEntry {
85 public:
86 TextureCacheEntry() : fEntry(NULL) {}
87 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
88 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
89 fEntry = e.fEntry;
90 return *this;
91 }
92 GrTexture* texture() const;
93 void reset() { fEntry = NULL; }
94 private:
95 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
96 void set(GrResourceEntry* entry) { fEntry = entry; }
97 GrResourceEntry* cacheEntry() { return fEntry; }
98 GrResourceEntry* fEntry;
99 friend class GrContext;
100 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000101
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000102 /**
103 * Key generated by client. Should be a unique key on the texture data.
104 * Does not need to consider that width and height of the texture. Two
105 * textures with the same TextureKey but different bounds will not collide.
106 */
107 typedef uint64_t TextureKey;
108
109 /**
110 * Search for an entry based on key and dimensions. If found, "lock" it and
111 * return it. The entry's texture() function will return NULL if not found.
112 * Must call be balanced with an unlockTexture() call.
113 */
114 TextureCacheEntry findAndLockTexture(TextureKey key,
115 int width,
116 int height,
117 const GrSamplerState&);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000118
119 /**
120 * Create a new entry, based on the specified key and texture, and return
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000121 * its "locked" entry. Must call be balanced with an unlockTexture() call.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000122 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000123 TextureCacheEntry createAndLockTexture(TextureKey key,
124 const GrSamplerState&,
125 const GrTextureDesc&,
126 void* srcData, size_t rowBytes);
127
128 /**
129 * Enum that determines how closely a returned scratch texture must match
130 * a provided GrTextureDesc.
131 */
132 enum ScratchTexMatch {
133 /**
134 * Finds a texture that exactly matches the descriptor.
135 */
136 kExact_ScratchTexMatch,
137 /**
138 * Finds a texture that approximately matches the descriptor. Will be
139 * at least as large in width and height as desc specifies. If desc
140 * specifies that texture is a render target then result will be a
141 * render target. If desc specifies a render target and doesn't set the
142 * no stencil flag then result will have a stencil. Format and aa level
143 * will always match.
144 */
145 kApprox_ScratchTexMatch
146 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147
148 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000149 * Returns a texture matching the desc. It's contents are unknown. Subsequent
150 * requests with the same descriptor are not guaranteed to return the same
151 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000153 *
154 * Textures created by createAndLockTexture() hide the complications of
155 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000157 * such an API will create gaps in the tiling pattern. This includes clamp
158 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000159 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000160 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000161
162 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000163 * When done with an entry, call unlockTexture(entry) on it, which returns
164 * it to the cache, where it may be purged.
165 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000166 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000167
168 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000169 * Creates a texture that is outside the cache. Does not count against
170 * cache's budget.
171 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000172 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173 void* srcData,
174 size_t rowBytes);
175
176 /**
177 * Returns true if the specified use of an indexed texture is supported.
178 */
179 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
180
181 /**
182 * Return the current texture cache limits.
183 *
184 * @param maxTextures If non-null, returns maximum number of textures that
185 * can be held in the cache.
186 * @param maxTextureBytes If non-null, returns maximum number of bytes of
187 * texture memory that can be held in the cache.
188 */
189 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
190
191 /**
192 * Specify the texture cache limits. If the current cache exceeds either
193 * of these, it will be purged (LRU) to keep the cache within these limits.
194 *
195 * @param maxTextures The maximum number of textures that can be held in
196 * the cache.
197 * @param maxTextureBytes The maximum number of bytes of texture memory
198 * that can be held in the cache.
199 */
200 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
201
202 /**
203 * Return the max width or height of a texture supported by the current gpu
204 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000205 int getMaxTextureSize() const;
206
207 /**
208 * Return the max width or height of a render target supported by the
209 * current gpu
210 */
211 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000212
213 ///////////////////////////////////////////////////////////////////////////
214 // Render targets
215
216 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000217 * Sets the render target.
218 * @param target the render target to set. (should not be NULL.)
219 */
220 void setRenderTarget(GrRenderTarget* target);
221
222 /**
223 * Gets the current render target.
224 * @return the currently bound render target. Should never be NULL.
225 */
226 const GrRenderTarget* getRenderTarget() const;
227 GrRenderTarget* getRenderTarget();
228
229 ///////////////////////////////////////////////////////////////////////////
230 // Platform Surfaces
231
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000232 /**
233 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
234 * the type of object returned. If kIsTexture is set the returned object
235 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
236 * are set the render target object is accessible by
237 * GrTexture::asRenderTarget().
238 *
239 * GL: if the object is a texture Gr may change its GL texture parameters
240 * when it is drawn.
241 *
242 * @param desc description of the object to create.
243 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
244 * on failure.
245 */
246 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000247
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000248 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000249 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
250 * viewport state from the underlying 3D API and wraps it in a
251 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
252 * underlying object in its destructor and it is up to caller to guarantee
253 * that it remains valid while the GrRenderTarget is used.
254 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000255 * Will not detect that the render target is also a texture. If you need
256 * to also use the render target as a GrTexture use createPlatformSurface.
257 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000258 * @return the newly created GrRenderTarget
259 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000260 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000261
bsalomon@google.com27847de2011-02-22 20:59:41 +0000262 ///////////////////////////////////////////////////////////////////////////
263 // Matrix state
264
265 /**
266 * Gets the current transformation matrix.
267 * @return the current matrix.
268 */
269 const GrMatrix& getMatrix() const;
270
271 /**
272 * Sets the transformation matrix.
273 * @param m the matrix to set.
274 */
275 void setMatrix(const GrMatrix& m);
276
277 /**
278 * Concats the current matrix. The passed matrix is applied before the
279 * current matrix.
280 * @param m the matrix to concat.
281 */
282 void concatMatrix(const GrMatrix& m) const;
283
284
285 ///////////////////////////////////////////////////////////////////////////
286 // Clip state
287 /**
288 * Gets the current clip.
289 * @return the current clip.
290 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000291 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000292
293 /**
294 * Sets the clip.
295 * @param clip the clip to set.
296 */
297 void setClip(const GrClip& clip);
298
299 /**
300 * Convenience method for setting the clip to a rect.
301 * @param rect the rect to set as the new clip.
302 */
303 void setClip(const GrIRect& rect);
304
305 ///////////////////////////////////////////////////////////////////////////
306 // Draws
307
308 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000309 * Clear the entire or rect of the render target, ignoring any clips.
310 * @param rect the rect to clear or the whole thing if rect is NULL.
311 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000312 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000313 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314
315 /**
316 * Draw everywhere (respecting the clip) with the paint.
317 */
318 void drawPaint(const GrPaint& paint);
319
320 /**
321 * Draw the rect using a paint.
322 * @param paint describes how to color pixels.
323 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
324 * the rect is mitered stroked based on strokeWidth. If
325 * strokeWidth == 0, then the stroke is always a single
326 * pixel thick.
327 * @param matrix Optional matrix applied to the rect. Applied before
328 * context's matrix or the paint's matrix.
329 * The rects coords are used to access the paint (through texture matrix)
330 */
331 void drawRect(const GrPaint& paint,
332 const GrRect&,
333 GrScalar strokeWidth = -1,
334 const GrMatrix* matrix = NULL);
335
336 /**
337 * Maps a rect of paint coordinates onto the a rect of destination
338 * coordinates. Each rect can optionally be transformed. The srcRect
339 * is stretched over the dstRect. The dstRect is transformed by the
340 * context's matrix and the srcRect is transformed by the paint's matrix.
341 * Additional optional matrices can be provided by parameters.
342 *
343 * @param paint describes how to color pixels.
344 * @param dstRect the destination rect to draw.
345 * @param srcRect rect of paint coordinates to be mapped onto dstRect
346 * @param dstMatrix Optional matrix to transform dstRect. Applied before
347 * context's matrix.
348 * @param srcMatrix Optional matrix to transform srcRect Applied before
349 * paint's matrix.
350 */
351 void drawRectToRect(const GrPaint& paint,
352 const GrRect& dstRect,
353 const GrRect& srcRect,
354 const GrMatrix* dstMatrix = NULL,
355 const GrMatrix* srcMatrix = NULL);
356
357 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000359 *
360 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000361 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000362 * @param fill the path filling rule to use.
363 * @param translate optional additional translation applied to the
364 * path.
365 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000366 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000368
bsalomon@google.com27847de2011-02-22 20:59:41 +0000369 /**
370 * Draws vertices with a paint.
371 *
372 * @param paint describes how to color pixels.
373 * @param primitiveType primitives type to draw.
374 * @param vertexCount number of vertices.
375 * @param positions array of vertex positions, required.
376 * @param texCoords optional array of texture coordinates used
377 * to access the paint.
378 * @param colors optional array of per-vertex colors, supercedes
379 * the paint's color field.
380 * @param indices optional array of indices. If NULL vertices
381 * are drawn non-indexed.
382 * @param indexCount if indices is non-null then this is the
383 * number of indices.
384 */
385 void drawVertices(const GrPaint& paint,
386 GrPrimitiveType primitiveType,
387 int vertexCount,
388 const GrPoint positions[],
389 const GrPoint texs[],
390 const GrColor colors[],
391 const uint16_t indices[],
392 int indexCount);
393
394 /**
395 * Similar to drawVertices but caller provides objects that convert to Gr
396 * types. The count of vertices is given by posSrc.
397 *
398 * @param paint describes how to color pixels.
399 * @param primitiveType primitives type to draw.
400 * @param posSrc Source of vertex positions. Must implement
401 * int count() const;
402 * void writeValue(int i, GrPoint* point) const;
403 * count returns the total number of vertices and
404 * writeValue writes a vertex position to point.
405 * @param texSrc optional, pass NULL to not use explicit tex
406 * coords. If present provides tex coords with
407 * method:
408 * void writeValue(int i, GrPoint* point) const;
409 * @param texSrc optional, pass NULL to not use per-vertex colors
410 * If present provides colors with method:
411 * void writeValue(int i, GrColor* point) const;
412 * @param indices optional, pass NULL for non-indexed drawing. If
413 * present supplies indices for indexed drawing
414 * with following methods:
415 * int count() const;
416 * void writeValue(int i, uint16_t* point) const;
417 * count returns the number of indices and
418 * writeValue supplies each index.
419 */
420 template <typename POS_SRC,
421 typename TEX_SRC,
422 typename COL_SRC,
423 typename IDX_SRC>
424 void drawCustomVertices(const GrPaint& paint,
425 GrPrimitiveType primitiveType,
426 const POS_SRC& posSrc,
427 const TEX_SRC* texCoordSrc,
428 const COL_SRC* colorSrc,
429 const IDX_SRC* idxSrc);
430 /**
431 * To avoid the problem of having to create a typename for NULL parameters,
432 * these reduced versions of drawCustomVertices are provided.
433 */
434 template <typename POS_SRC>
435 void drawCustomVertices(const GrPaint& paint,
436 GrPrimitiveType primitiveType,
437 const POS_SRC& posSrc);
438 template <typename POS_SRC, typename TEX_SRC>
439 void drawCustomVertices(const GrPaint& paint,
440 GrPrimitiveType primitiveType,
441 const POS_SRC& posSrc,
442 const TEX_SRC* texCoordSrc);
443 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
444 void drawCustomVertices(const GrPaint& paint,
445 GrPrimitiveType primitiveType,
446 const POS_SRC& posSrc,
447 const TEX_SRC* texCoordSrc,
448 const COL_SRC* colorSrc);
449
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450 ///////////////////////////////////////////////////////////////////////////
451 // Misc.
452
453 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000454 * Flags that affect flush() behavior.
455 */
456 enum FlushBits {
457 /**
458 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
459 * it can be rendered to directly. However, Gr lazily sets state. Simply
460 * calling setRenderTarget() followed by flush() without flags may not
461 * bind the render target. This flag forces the context to bind the last
462 * set render target in the 3D API.
463 */
464 kForceCurrentRenderTarget_FlushBit = 0x1,
465 /**
466 * A client may reach a point where it has partially rendered a frame
467 * through a GrContext that it knows the user will never see. This flag
468 * causes the flush to skip submission of deferred content to the 3D API
469 * during the flush.
470 */
471 kDiscard_FlushBit = 0x2,
472 };
473
474 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000475 * Call to ensure all drawing to the context has been issued to the
476 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000477 * @param flagsBitfield flags that control the flushing behavior. See
478 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000479 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000480 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481
bsalomon@google.com27847de2011-02-22 20:59:41 +0000482 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000483 * Reads a rectangle of pixels from a render target.
484 * @param renderTarget the render target to read from. NULL means the
485 * current render target.
486 * @param left left edge of the rectangle to read (inclusive)
487 * @param top top edge of the rectangle to read (inclusive)
488 * @param width width of rectangle to read in pixels.
489 * @param height height of rectangle to read in pixels.
490 * @param config the pixel config of the destination buffer
491 * @param buffer memory to read the rectangle into.
492 *
493 * @return true if the read succeeded, false if not. The read can fail
494 * because of a unsupported pixel config or because no render
495 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497 bool readRenderTargetPixels(GrRenderTarget* target,
498 int left, int top, int width, int height,
499 GrPixelConfig config, void* buffer);
500
501 /**
502 * Reads a rectangle of pixels from a texture.
503 * @param texture the render target to read from.
504 * @param left left edge of the rectangle to read (inclusive)
505 * @param top top edge of the rectangle to read (inclusive)
506 * @param width width of rectangle to read in pixels.
507 * @param height height of rectangle to read in pixels.
508 * @param config the pixel config of the destination buffer
509 * @param buffer memory to read the rectangle into.
510 *
511 * @return true if the read succeeded, false if not. The read can fail
512 * because of a unsupported pixel config.
513 */
514 bool readTexturePixels(GrTexture* target,
515 int left, int top, int width, int height,
516 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517
518 /**
519 * Copy the src pixels [buffer, stride, pixelconfig] into the current
520 * render-target at the specified rectangle.
521 */
522 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000524
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000525 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000526 * Applies a 1D convolution kernel in the X direction to a rectangle of
527 * pixels from a given texture.
528 * @param texture the texture to read from
529 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000530 * @param kernel the convolution kernel (kernelWidth elements)
531 * @param kernelWidth the width of the convolution kernel
532 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000533 void convolveInX(GrTexture* texture,
534 const SkRect& rect,
535 const float* kernel,
536 int kernelWidth);
537 /**
538 * Applies a 1D convolution kernel in the Y direction to a rectangle of
539 * pixels from a given texture.
540 * direction.
541 * @param texture the texture to read from
542 * @param rect the destination rectangle
543 * @param kernel the convolution kernel (kernelWidth elements)
544 * @param kernelWidth the width of the convolution kernel
545 */
546 void convolveInY(GrTexture* texture,
547 const SkRect& rect,
548 const float* kernel,
549 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550 ///////////////////////////////////////////////////////////////////////////
551 // Helpers
552
553 class AutoRenderTarget : ::GrNoncopyable {
554 public:
555 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
556 fContext = NULL;
557 fPrevTarget = context->getRenderTarget();
558 if (fPrevTarget != target) {
559 context->setRenderTarget(target);
560 fContext = context;
561 }
562 }
563 ~AutoRenderTarget() {
564 if (fContext) {
565 fContext->setRenderTarget(fPrevTarget);
566 }
567 }
568 private:
569 GrContext* fContext;
570 GrRenderTarget* fPrevTarget;
571 };
572
573
574 ///////////////////////////////////////////////////////////////////////////
575 // Functions intended for internal use only.
576 GrGpu* getGpu() { return fGpu; }
577 GrFontCache* getFontCache() { return fFontCache; }
578 GrDrawTarget* getTextTarget(const GrPaint& paint);
579 void flushText();
580 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000581 void resetStats();
582 const GrGpuStats& getStats() const;
583 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000584 /**
585 * Stencil buffers add themselves to the cache using
586 * addAndLockStencilBuffer. When a SB's RT-attachment count
587 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
588 * eligible for purging. findStencilBuffer is called to check the cache for
589 * a SB that matching an RT's criteria. If a match is found that has been
590 * unlocked (its attachment count has reached 0) then it will be relocked.
591 */
592 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
593 void unlockStencilBuffer(GrResourceEntry* sbEntry);
594 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595
596private:
597 // used to keep track of when we need to flush the draw buffer
598 enum DrawCategory {
599 kBuffered_DrawCategory, // last draw was inserted in draw buffer
600 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
601 kText_DrawCategory // text context was last to draw
602 };
603 DrawCategory fLastDrawCategory;
604
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000605 GrGpu* fGpu;
606 GrResourceCache* fTextureCache;
607 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000608
609 GrPathRenderer* fCustomPathRenderer;
610 GrDefaultPathRenderer fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000611
612 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
613 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
614 GrInOrderDrawBuffer* fDrawBuffer;
615
bsalomon@google.com205d4602011-04-25 12:43:45 +0000616 GrIndexBuffer* fAAFillRectIndexBuffer;
617 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000618 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000619
bsalomon@google.com27847de2011-02-22 20:59:41 +0000620 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000621
bsalomon@google.com205d4602011-04-25 12:43:45 +0000622 void fillAARect(GrDrawTarget* target,
623 const GrPaint& paint,
624 const GrRect& devRect);
625
626 void strokeAARect(GrDrawTarget* target,
627 const GrPaint& paint,
628 const GrRect& devRect,
629 const GrVec& devStrokeSize);
630
631 inline int aaFillRectIndexCount() const;
632 GrIndexBuffer* aaFillRectIndexBuffer();
633
634 inline int aaStrokeRectIndexCount() const;
635 GrIndexBuffer* aaStrokeRectIndexBuffer();
636
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000637 void setupDrawBuffer();
638
bsalomon@google.com27847de2011-02-22 20:59:41 +0000639 void flushDrawBuffer();
640
641 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
642
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
644
645 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000646
bsalomon@google.comee435122011-07-01 14:57:55 +0000647 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000648
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000649 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000650
bsalomon@google.com91958362011-06-13 17:58:13 +0000651 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000652 bool doOffscreenAA(GrDrawTarget* target,
653 const GrPaint& paint,
654 bool isLines) const;
655
bsalomon@google.com91958362011-06-13 17:58:13 +0000656 // attempts to setup offscreen AA. All paint state must be transferred to
657 // target by the time this is called.
658 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000659 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000660 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000661 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000662 OffscreenRecord* record);
663
bsalomon@google.com91958362011-06-13 17:58:13 +0000664 // sets up target to draw coverage to the supersampled render target
665 void setupOffscreenAAPass1(GrDrawTarget* target,
666 const GrIRect& boundRect,
667 int tileX, int tileY,
668 OffscreenRecord* record);
669
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000670 // sets up target to sample coverage of supersampled render target back
671 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000672 void doOffscreenAAPass2(GrDrawTarget* target,
673 const GrPaint& paint,
674 const GrIRect& boundRect,
675 int tileX, int tileY,
676 OffscreenRecord* record);
677
678 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000679 void cleanupOffscreenAA(GrDrawTarget* target,
680 GrPathRenderer* pr,
681 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000682
683 void convolve(GrTexture* texture,
684 const SkRect& rect,
685 float imageIncrement[2],
686 const float* kernel,
687 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000688
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000689 // computes vertex layout bits based on the paint. If paint expresses
690 // a texture for a stage, the stage coords will be bound to postitions
691 // unless hasTexCoords[s]==true in which case stage s's input coords
692 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
693 // for an array where all the values are false.
694 static int PaintStageVertexLayoutBits(
695 const GrPaint& paint,
696 const bool hasTexCoords[GrPaint::kTotalStages]);
697
bsalomon@google.com27847de2011-02-22 20:59:41 +0000698};
699
700/**
701 * Save/restore the view-matrix in the context.
702 */
703class GrAutoMatrix : GrNoncopyable {
704public:
705 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
706 fMatrix = ctx->getMatrix();
707 }
708 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
709 fMatrix = ctx->getMatrix();
710 ctx->setMatrix(matrix);
711 }
712 ~GrAutoMatrix() {
713 fContext->setMatrix(fMatrix);
714 }
715
716private:
717 GrContext* fContext;
718 GrMatrix fMatrix;
719};
720
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000721/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000722 * Gets and locks a scratch texture from a descriptor using
723 * either exact or approximate criteria. Unlocks texture in
724 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000725 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000726class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000727public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000728 GrAutoScratchTexture()
729 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000730 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000731
732 GrAutoScratchTexture(GrContext* context,
733 const GrTextureDesc& desc,
734 GrContext::ScratchTexMatch match =
735 GrContext::kApprox_ScratchTexMatch)
736 : fContext(NULL) {
737 this->set(context, desc, match);
738 }
739
740 ~GrAutoScratchTexture() {
741 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000742 fContext->unlockTexture(fEntry);
743 }
744 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000745
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000746 GrTexture* set(GrContext* context,
747 const GrTextureDesc& desc,
748 GrContext::ScratchTexMatch match =
749 GrContext::kApprox_ScratchTexMatch) {
750 if (NULL != fContext) {
751 fContext->unlockTexture(fEntry);
752 }
753 fContext = context;
754 if (NULL != fContext) {
755 fEntry = fContext->lockScratchTexture(desc, match);
756 GrTexture* ret = fEntry.texture();
757 if (NULL == ret) {
758 fContext = NULL;
759 }
760 return ret;
761 } else {
762 return NULL;
763 }
764 }
765
766 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000767private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000768 GrContext* fContext;
769 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000770};
771
bsalomon@google.com27847de2011-02-22 20:59:41 +0000772#endif
773
774#include "GrContext_impl.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000775