blob: cb354f853b4209360d94c9bcf097790a69ea9e52 [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;
24class GrVertexBufferAllocPool;
25
bsalomon@google.com27847de2011-02-22 20:59:41 +000026
bsalomon@google.com91826102011-03-21 19:51:57 +000027class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000028public:
29 /**
30 * Creates a GrContext from within a 3D context.
31 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000032 static GrContext* Create(GrEngine engine,
33 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
35 /**
36 * Helper to create a opengl-shader based context
37 */
38 static GrContext* CreateGLShaderContext();
39
40 virtual ~GrContext();
41
42 /**
43 * The GrContext normally assumes that no outsider is setting state
44 * within the underlying 3D API's context/device/whatever. This call informs
45 * the context that the state was modified and it should resend. Shouldn't
46 * be called frequently for good performance.
47 */
48 void resetContext();
49
bsalomon@google.com8fe72472011-03-30 21:26:44 +000050 /**
51 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
52 * if you have lost the associated GPU context, and thus internal texture,
53 * buffer, etc. references/IDs are now invalid. Should be called even when
54 * GrContext is no longer going to be used for two reasons:
55 * 1) ~GrContext will not try to free the objects in the 3D API.
56 * 2) If you've created GrResources that outlive the GrContext they will
57 * be marked as invalid (GrResource::isValid()) and won't attempt to
58 * free their underlying resource in the 3D API.
59 * Content drawn since the last GrContext::flush() may be lost.
60 */
61 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000062
63 /**
junov@google.com53a55842011-06-08 22:55:10 +000064 * Similar to contextLost, but makes no attempt to reset state.
65 * Use this method when GrContext destruction is pending, but
66 * the graphics context is destroyed first.
67 */
68 void contextDestroyed();
69
70 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000071 * Frees gpu created by the context. Can be called to reduce GPU memory
72 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000073 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000074 void freeGpuResources();
75
76 ///////////////////////////////////////////////////////////////////////////
77 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000078
79 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000080 * Token that refers to an entry in the texture cache. Returned by
81 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000082 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000083 class TextureCacheEntry {
84 public:
85 TextureCacheEntry() : fEntry(NULL) {}
86 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
87 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
88 fEntry = e.fEntry;
89 return *this;
90 }
91 GrTexture* texture() const;
92 void reset() { fEntry = NULL; }
93 private:
94 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
95 void set(GrResourceEntry* entry) { fEntry = entry; }
96 GrResourceEntry* cacheEntry() { return fEntry; }
97 GrResourceEntry* fEntry;
98 friend class GrContext;
99 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000101 /**
102 * Key generated by client. Should be a unique key on the texture data.
103 * Does not need to consider that width and height of the texture. Two
104 * textures with the same TextureKey but different bounds will not collide.
105 */
106 typedef uint64_t TextureKey;
107
108 /**
109 * Search for an entry based on key and dimensions. If found, "lock" it and
110 * return it. The entry's texture() function will return NULL if not found.
111 * Must call be balanced with an unlockTexture() call.
112 */
113 TextureCacheEntry findAndLockTexture(TextureKey key,
114 int width,
115 int height,
116 const GrSamplerState&);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000117
118 /**
119 * Create a new entry, based on the specified key and texture, and return
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000120 * its "locked" entry. Must call be balanced with an unlockTexture() call.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000121 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000122 TextureCacheEntry createAndLockTexture(TextureKey key,
123 const GrSamplerState&,
124 const GrTextureDesc&,
125 void* srcData, size_t rowBytes);
126
127 /**
128 * Enum that determines how closely a returned scratch texture must match
129 * a provided GrTextureDesc.
130 */
131 enum ScratchTexMatch {
132 /**
133 * Finds a texture that exactly matches the descriptor.
134 */
135 kExact_ScratchTexMatch,
136 /**
137 * Finds a texture that approximately matches the descriptor. Will be
138 * at least as large in width and height as desc specifies. If desc
139 * specifies that texture is a render target then result will be a
140 * render target. If desc specifies a render target and doesn't set the
141 * no stencil flag then result will have a stencil. Format and aa level
142 * will always match.
143 */
144 kApprox_ScratchTexMatch
145 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000146
147 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000148 * Returns a texture matching the desc. It's contents are unknown. Subsequent
149 * requests with the same descriptor are not guaranteed to return the same
150 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000151 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000152 *
153 * Textures created by createAndLockTexture() hide the complications of
154 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000155 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000156 * such an API will create gaps in the tiling pattern. This includes clamp
157 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000158 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000159 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000160
161 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162 * When done with an entry, call unlockTexture(entry) on it, which returns
163 * it to the cache, where it may be purged.
164 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000165 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000166
167 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000168 * Creates a texture that is outside the cache. Does not count against
169 * cache's budget.
170 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000171 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000172 void* srcData,
173 size_t rowBytes);
174
175 /**
176 * Returns true if the specified use of an indexed texture is supported.
177 */
178 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
179
180 /**
181 * Return the current texture cache limits.
182 *
183 * @param maxTextures If non-null, returns maximum number of textures that
184 * can be held in the cache.
185 * @param maxTextureBytes If non-null, returns maximum number of bytes of
186 * texture memory that can be held in the cache.
187 */
188 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
189
190 /**
191 * Specify the texture cache limits. If the current cache exceeds either
192 * of these, it will be purged (LRU) to keep the cache within these limits.
193 *
194 * @param maxTextures The maximum number of textures that can be held in
195 * the cache.
196 * @param maxTextureBytes The maximum number of bytes of texture memory
197 * that can be held in the cache.
198 */
199 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
200
201 /**
202 * Return the max width or height of a texture supported by the current gpu
203 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000204 int getMaxTextureSize() const;
205
206 /**
207 * Return the max width or height of a render target supported by the
208 * current gpu
209 */
210 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211
212 ///////////////////////////////////////////////////////////////////////////
213 // Render targets
214
215 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000216 * Sets the render target.
217 * @param target the render target to set. (should not be NULL.)
218 */
219 void setRenderTarget(GrRenderTarget* target);
220
221 /**
222 * Gets the current render target.
223 * @return the currently bound render target. Should never be NULL.
224 */
225 const GrRenderTarget* getRenderTarget() const;
226 GrRenderTarget* getRenderTarget();
227
228 ///////////////////////////////////////////////////////////////////////////
229 // Platform Surfaces
230
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000231 /**
232 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
233 * the type of object returned. If kIsTexture is set the returned object
234 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
235 * are set the render target object is accessible by
236 * GrTexture::asRenderTarget().
237 *
238 * GL: if the object is a texture Gr may change its GL texture parameters
239 * when it is drawn.
240 *
241 * @param desc description of the object to create.
242 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
243 * on failure.
244 */
245 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000246
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000247 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
249 * viewport state from the underlying 3D API and wraps it in a
250 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
251 * underlying object in its destructor and it is up to caller to guarantee
252 * that it remains valid while the GrRenderTarget is used.
253 *
bsalomon@google.com2368f6f2011-05-19 21:22:39 +0000254 * Will not detect that the render target is also a texture. If you need
255 * to also use the render target as a GrTexture use createPlatformSurface.
256 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257 * @return the newly created GrRenderTarget
258 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000259 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000260
bsalomon@google.com27847de2011-02-22 20:59:41 +0000261 ///////////////////////////////////////////////////////////////////////////
262 // Matrix state
263
264 /**
265 * Gets the current transformation matrix.
266 * @return the current matrix.
267 */
268 const GrMatrix& getMatrix() const;
269
270 /**
271 * Sets the transformation matrix.
272 * @param m the matrix to set.
273 */
274 void setMatrix(const GrMatrix& m);
275
276 /**
277 * Concats the current matrix. The passed matrix is applied before the
278 * current matrix.
279 * @param m the matrix to concat.
280 */
281 void concatMatrix(const GrMatrix& m) const;
282
283
284 ///////////////////////////////////////////////////////////////////////////
285 // Clip state
286 /**
287 * Gets the current clip.
288 * @return the current clip.
289 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000290 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000291
292 /**
293 * Sets the clip.
294 * @param clip the clip to set.
295 */
296 void setClip(const GrClip& clip);
297
298 /**
299 * Convenience method for setting the clip to a rect.
300 * @param rect the rect to set as the new clip.
301 */
302 void setClip(const GrIRect& rect);
303
304 ///////////////////////////////////////////////////////////////////////////
305 // Draws
306
307 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000308 * Clear the entire or rect of the render target, ignoring any clips.
309 * @param rect the rect to clear or the whole thing if rect is NULL.
310 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000311 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000312 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000313
314 /**
315 * Draw everywhere (respecting the clip) with the paint.
316 */
317 void drawPaint(const GrPaint& paint);
318
319 /**
320 * Draw the rect using a paint.
321 * @param paint describes how to color pixels.
322 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
323 * the rect is mitered stroked based on strokeWidth. If
324 * strokeWidth == 0, then the stroke is always a single
325 * pixel thick.
326 * @param matrix Optional matrix applied to the rect. Applied before
327 * context's matrix or the paint's matrix.
328 * The rects coords are used to access the paint (through texture matrix)
329 */
330 void drawRect(const GrPaint& paint,
331 const GrRect&,
332 GrScalar strokeWidth = -1,
333 const GrMatrix* matrix = NULL);
334
335 /**
336 * Maps a rect of paint coordinates onto the a rect of destination
337 * coordinates. Each rect can optionally be transformed. The srcRect
338 * is stretched over the dstRect. The dstRect is transformed by the
339 * context's matrix and the srcRect is transformed by the paint's matrix.
340 * Additional optional matrices can be provided by parameters.
341 *
342 * @param paint describes how to color pixels.
343 * @param dstRect the destination rect to draw.
344 * @param srcRect rect of paint coordinates to be mapped onto dstRect
345 * @param dstMatrix Optional matrix to transform dstRect. Applied before
346 * context's matrix.
347 * @param srcMatrix Optional matrix to transform srcRect Applied before
348 * paint's matrix.
349 */
350 void drawRectToRect(const GrPaint& paint,
351 const GrRect& dstRect,
352 const GrRect& srcRect,
353 const GrMatrix* dstMatrix = NULL,
354 const GrMatrix* srcMatrix = NULL);
355
356 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000358 *
359 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000360 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361 * @param fill the path filling rule to use.
362 * @param translate optional additional translation applied to the
363 * path.
364 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000365 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000366 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000367
bsalomon@google.com27847de2011-02-22 20:59:41 +0000368 /**
369 * Draws vertices with a paint.
370 *
371 * @param paint describes how to color pixels.
372 * @param primitiveType primitives type to draw.
373 * @param vertexCount number of vertices.
374 * @param positions array of vertex positions, required.
375 * @param texCoords optional array of texture coordinates used
376 * to access the paint.
377 * @param colors optional array of per-vertex colors, supercedes
378 * the paint's color field.
379 * @param indices optional array of indices. If NULL vertices
380 * are drawn non-indexed.
381 * @param indexCount if indices is non-null then this is the
382 * number of indices.
383 */
384 void drawVertices(const GrPaint& paint,
385 GrPrimitiveType primitiveType,
386 int vertexCount,
387 const GrPoint positions[],
388 const GrPoint texs[],
389 const GrColor colors[],
390 const uint16_t indices[],
391 int indexCount);
392
393 /**
394 * Similar to drawVertices but caller provides objects that convert to Gr
395 * types. The count of vertices is given by posSrc.
396 *
397 * @param paint describes how to color pixels.
398 * @param primitiveType primitives type to draw.
399 * @param posSrc Source of vertex positions. Must implement
400 * int count() const;
401 * void writeValue(int i, GrPoint* point) const;
402 * count returns the total number of vertices and
403 * writeValue writes a vertex position to point.
404 * @param texSrc optional, pass NULL to not use explicit tex
405 * coords. If present provides tex coords with
406 * method:
407 * void writeValue(int i, GrPoint* point) const;
408 * @param texSrc optional, pass NULL to not use per-vertex colors
409 * If present provides colors with method:
410 * void writeValue(int i, GrColor* point) const;
411 * @param indices optional, pass NULL for non-indexed drawing. If
412 * present supplies indices for indexed drawing
413 * with following methods:
414 * int count() const;
415 * void writeValue(int i, uint16_t* point) const;
416 * count returns the number of indices and
417 * writeValue supplies each index.
418 */
419 template <typename POS_SRC,
420 typename TEX_SRC,
421 typename COL_SRC,
422 typename IDX_SRC>
423 void drawCustomVertices(const GrPaint& paint,
424 GrPrimitiveType primitiveType,
425 const POS_SRC& posSrc,
426 const TEX_SRC* texCoordSrc,
427 const COL_SRC* colorSrc,
428 const IDX_SRC* idxSrc);
429 /**
430 * To avoid the problem of having to create a typename for NULL parameters,
431 * these reduced versions of drawCustomVertices are provided.
432 */
433 template <typename POS_SRC>
434 void drawCustomVertices(const GrPaint& paint,
435 GrPrimitiveType primitiveType,
436 const POS_SRC& posSrc);
437 template <typename POS_SRC, typename TEX_SRC>
438 void drawCustomVertices(const GrPaint& paint,
439 GrPrimitiveType primitiveType,
440 const POS_SRC& posSrc,
441 const TEX_SRC* texCoordSrc);
442 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
443 void drawCustomVertices(const GrPaint& paint,
444 GrPrimitiveType primitiveType,
445 const POS_SRC& posSrc,
446 const TEX_SRC* texCoordSrc,
447 const COL_SRC* colorSrc);
448
bsalomon@google.com27847de2011-02-22 20:59:41 +0000449 ///////////////////////////////////////////////////////////////////////////
450 // Misc.
451
452 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000453 * Flags that affect flush() behavior.
454 */
455 enum FlushBits {
456 /**
457 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
458 * it can be rendered to directly. However, Gr lazily sets state. Simply
459 * calling setRenderTarget() followed by flush() without flags may not
460 * bind the render target. This flag forces the context to bind the last
461 * set render target in the 3D API.
462 */
463 kForceCurrentRenderTarget_FlushBit = 0x1,
464 /**
465 * A client may reach a point where it has partially rendered a frame
466 * through a GrContext that it knows the user will never see. This flag
467 * causes the flush to skip submission of deferred content to the 3D API
468 * during the flush.
469 */
470 kDiscard_FlushBit = 0x2,
471 };
472
473 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474 * Call to ensure all drawing to the context has been issued to the
475 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000476 * @param flagsBitfield flags that control the flushing behavior. See
477 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000478 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000479 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000480
bsalomon@google.com27847de2011-02-22 20:59:41 +0000481 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000482 * Reads a rectangle of pixels from a render target.
483 * @param renderTarget the render target to read from. NULL means the
484 * current render target.
485 * @param left left edge of the rectangle to read (inclusive)
486 * @param top top edge of the rectangle to read (inclusive)
487 * @param width width of rectangle to read in pixels.
488 * @param height height of rectangle to read in pixels.
489 * @param config the pixel config of the destination buffer
490 * @param buffer memory to read the rectangle into.
491 *
492 * @return true if the read succeeded, false if not. The read can fail
493 * because of a unsupported pixel config or because no render
494 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000495 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000496 bool readRenderTargetPixels(GrRenderTarget* target,
497 int left, int top, int width, int height,
498 GrPixelConfig config, void* buffer);
499
500 /**
501 * Reads a rectangle of pixels from a texture.
502 * @param texture the render target to read from.
503 * @param left left edge of the rectangle to read (inclusive)
504 * @param top top edge of the rectangle to read (inclusive)
505 * @param width width of rectangle to read in pixels.
506 * @param height height of rectangle to read in pixels.
507 * @param config the pixel config of the destination buffer
508 * @param buffer memory to read the rectangle into.
509 *
510 * @return true if the read succeeded, false if not. The read can fail
511 * because of a unsupported pixel config.
512 */
513 bool readTexturePixels(GrTexture* target,
514 int left, int top, int width, int height,
515 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000516
517 /**
518 * Copy the src pixels [buffer, stride, pixelconfig] into the current
519 * render-target at the specified rectangle.
520 */
521 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000522 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000524 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000525 * Applies a 1D convolution kernel in the X direction to a rectangle of
526 * pixels from a given texture.
527 * @param texture the texture to read from
528 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000529 * @param kernel the convolution kernel (kernelWidth elements)
530 * @param kernelWidth the width of the convolution kernel
531 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000532 void convolveInX(GrTexture* texture,
533 const SkRect& rect,
534 const float* kernel,
535 int kernelWidth);
536 /**
537 * Applies a 1D convolution kernel in the Y direction to a rectangle of
538 * pixels from a given texture.
539 * direction.
540 * @param texture the texture to read from
541 * @param rect the destination rectangle
542 * @param kernel the convolution kernel (kernelWidth elements)
543 * @param kernelWidth the width of the convolution kernel
544 */
545 void convolveInY(GrTexture* texture,
546 const SkRect& rect,
547 const float* kernel,
548 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549 ///////////////////////////////////////////////////////////////////////////
550 // Helpers
551
552 class AutoRenderTarget : ::GrNoncopyable {
553 public:
554 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
555 fContext = NULL;
556 fPrevTarget = context->getRenderTarget();
557 if (fPrevTarget != target) {
558 context->setRenderTarget(target);
559 fContext = context;
560 }
561 }
562 ~AutoRenderTarget() {
563 if (fContext) {
564 fContext->setRenderTarget(fPrevTarget);
565 }
566 }
567 private:
568 GrContext* fContext;
569 GrRenderTarget* fPrevTarget;
570 };
571
572
573 ///////////////////////////////////////////////////////////////////////////
574 // Functions intended for internal use only.
575 GrGpu* getGpu() { return fGpu; }
576 GrFontCache* getFontCache() { return fFontCache; }
577 GrDrawTarget* getTextTarget(const GrPaint& paint);
578 void flushText();
579 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000580 void resetStats();
581 const GrGpuStats& getStats() const;
582 void printStats() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000583
584private:
585 // used to keep track of when we need to flush the draw buffer
586 enum DrawCategory {
587 kBuffered_DrawCategory, // last draw was inserted in draw buffer
588 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
589 kText_DrawCategory // text context was last to draw
590 };
591 DrawCategory fLastDrawCategory;
592
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000593 GrGpu* fGpu;
594 GrResourceCache* fTextureCache;
595 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000596
597 GrPathRenderer* fCustomPathRenderer;
598 GrDefaultPathRenderer fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599
600 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
601 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
602 GrInOrderDrawBuffer* fDrawBuffer;
603
bsalomon@google.com205d4602011-04-25 12:43:45 +0000604 GrIndexBuffer* fAAFillRectIndexBuffer;
605 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000606 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000607
bsalomon@google.com27847de2011-02-22 20:59:41 +0000608 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000609
bsalomon@google.com205d4602011-04-25 12:43:45 +0000610 void fillAARect(GrDrawTarget* target,
611 const GrPaint& paint,
612 const GrRect& devRect);
613
614 void strokeAARect(GrDrawTarget* target,
615 const GrPaint& paint,
616 const GrRect& devRect,
617 const GrVec& devStrokeSize);
618
619 inline int aaFillRectIndexCount() const;
620 GrIndexBuffer* aaFillRectIndexBuffer();
621
622 inline int aaStrokeRectIndexCount() const;
623 GrIndexBuffer* aaStrokeRectIndexBuffer();
624
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000625 void setupDrawBuffer();
626
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627 void flushDrawBuffer();
628
629 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
630
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
632
633 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000634
bsalomon@google.comee435122011-07-01 14:57:55 +0000635 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000636
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000637 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000638
bsalomon@google.com91958362011-06-13 17:58:13 +0000639 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000640 bool doOffscreenAA(GrDrawTarget* target,
641 const GrPaint& paint,
642 bool isLines) const;
643
bsalomon@google.com91958362011-06-13 17:58:13 +0000644 // attempts to setup offscreen AA. All paint state must be transferred to
645 // target by the time this is called.
646 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000647 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000648 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000649 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000650 OffscreenRecord* record);
651
bsalomon@google.com91958362011-06-13 17:58:13 +0000652 // sets up target to draw coverage to the supersampled render target
653 void setupOffscreenAAPass1(GrDrawTarget* target,
654 const GrIRect& boundRect,
655 int tileX, int tileY,
656 OffscreenRecord* record);
657
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000658 // sets up target to sample coverage of supersampled render target back
659 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000660 void doOffscreenAAPass2(GrDrawTarget* target,
661 const GrPaint& paint,
662 const GrIRect& boundRect,
663 int tileX, int tileY,
664 OffscreenRecord* record);
665
666 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000667 void cleanupOffscreenAA(GrDrawTarget* target,
668 GrPathRenderer* pr,
669 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000670
671 void convolve(GrTexture* texture,
672 const SkRect& rect,
673 float imageIncrement[2],
674 const float* kernel,
675 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000676
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000677 // computes vertex layout bits based on the paint. If paint expresses
678 // a texture for a stage, the stage coords will be bound to postitions
679 // unless hasTexCoords[s]==true in which case stage s's input coords
680 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
681 // for an array where all the values are false.
682 static int PaintStageVertexLayoutBits(
683 const GrPaint& paint,
684 const bool hasTexCoords[GrPaint::kTotalStages]);
685
bsalomon@google.com27847de2011-02-22 20:59:41 +0000686};
687
688/**
689 * Save/restore the view-matrix in the context.
690 */
691class GrAutoMatrix : GrNoncopyable {
692public:
693 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
694 fMatrix = ctx->getMatrix();
695 }
696 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
697 fMatrix = ctx->getMatrix();
698 ctx->setMatrix(matrix);
699 }
700 ~GrAutoMatrix() {
701 fContext->setMatrix(fMatrix);
702 }
703
704private:
705 GrContext* fContext;
706 GrMatrix fMatrix;
707};
708
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000709/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000710 * Gets and locks a scratch texture from a descriptor using
711 * either exact or approximate criteria. Unlocks texture in
712 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000713 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000714class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000715public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000716 GrAutoScratchTexture()
717 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000718 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000719
720 GrAutoScratchTexture(GrContext* context,
721 const GrTextureDesc& desc,
722 GrContext::ScratchTexMatch match =
723 GrContext::kApprox_ScratchTexMatch)
724 : fContext(NULL) {
725 this->set(context, desc, match);
726 }
727
728 ~GrAutoScratchTexture() {
729 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000730 fContext->unlockTexture(fEntry);
731 }
732 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000733
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000734 GrTexture* set(GrContext* context,
735 const GrTextureDesc& desc,
736 GrContext::ScratchTexMatch match =
737 GrContext::kApprox_ScratchTexMatch) {
738 if (NULL != fContext) {
739 fContext->unlockTexture(fEntry);
740 }
741 fContext = context;
742 if (NULL != fContext) {
743 fEntry = fContext->lockScratchTexture(desc, match);
744 GrTexture* ret = fEntry.texture();
745 if (NULL == ret) {
746 fContext = NULL;
747 }
748 return ret;
749 } else {
750 return NULL;
751 }
752 }
753
754 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000756 GrContext* fContext;
757 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000758};
759
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760#endif
761
762#include "GrContext_impl.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000763