blob: c7a87da07f2a6124f87bd05fb28493e15fccb31a [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17#ifndef GrContext_DEFINED
18#define GrContext_DEFINED
19
20#include "GrClip.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021#include "GrPaint.h"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000022#include "GrPathRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000023
24class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000025class GrGpu;
26struct GrGpuStats;
bsalomon@google.com27847de2011-02-22 20:59:41 +000027class GrIndexBufferAllocPool;
28class GrInOrderDrawBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029class GrResourceEntry;
30class GrResourceCache;
31class GrVertexBufferAllocPool;
32
bsalomon@google.com27847de2011-02-22 20:59:41 +000033
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
400 /**
401 * Similar to drawVertices but caller provides objects that convert to Gr
402 * types. The count of vertices is given by posSrc.
403 *
404 * @param paint describes how to color pixels.
405 * @param primitiveType primitives type to draw.
406 * @param posSrc Source of vertex positions. Must implement
407 * int count() const;
408 * void writeValue(int i, GrPoint* point) const;
409 * count returns the total number of vertices and
410 * writeValue writes a vertex position to point.
411 * @param texSrc optional, pass NULL to not use explicit tex
412 * coords. If present provides tex coords with
413 * method:
414 * void writeValue(int i, GrPoint* point) const;
415 * @param texSrc optional, pass NULL to not use per-vertex colors
416 * If present provides colors with method:
417 * void writeValue(int i, GrColor* point) const;
418 * @param indices optional, pass NULL for non-indexed drawing. If
419 * present supplies indices for indexed drawing
420 * with following methods:
421 * int count() const;
422 * void writeValue(int i, uint16_t* point) const;
423 * count returns the number of indices and
424 * writeValue supplies each index.
425 */
426 template <typename POS_SRC,
427 typename TEX_SRC,
428 typename COL_SRC,
429 typename IDX_SRC>
430 void drawCustomVertices(const GrPaint& paint,
431 GrPrimitiveType primitiveType,
432 const POS_SRC& posSrc,
433 const TEX_SRC* texCoordSrc,
434 const COL_SRC* colorSrc,
435 const IDX_SRC* idxSrc);
436 /**
437 * To avoid the problem of having to create a typename for NULL parameters,
438 * these reduced versions of drawCustomVertices are provided.
439 */
440 template <typename POS_SRC>
441 void drawCustomVertices(const GrPaint& paint,
442 GrPrimitiveType primitiveType,
443 const POS_SRC& posSrc);
444 template <typename POS_SRC, typename TEX_SRC>
445 void drawCustomVertices(const GrPaint& paint,
446 GrPrimitiveType primitiveType,
447 const POS_SRC& posSrc,
448 const TEX_SRC* texCoordSrc);
449 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
450 void drawCustomVertices(const GrPaint& paint,
451 GrPrimitiveType primitiveType,
452 const POS_SRC& posSrc,
453 const TEX_SRC* texCoordSrc,
454 const COL_SRC* colorSrc);
455
bsalomon@google.com27847de2011-02-22 20:59:41 +0000456 ///////////////////////////////////////////////////////////////////////////
457 // Misc.
458
459 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000460 * Flags that affect flush() behavior.
461 */
462 enum FlushBits {
463 /**
464 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
465 * it can be rendered to directly. However, Gr lazily sets state. Simply
466 * calling setRenderTarget() followed by flush() without flags may not
467 * bind the render target. This flag forces the context to bind the last
468 * set render target in the 3D API.
469 */
470 kForceCurrentRenderTarget_FlushBit = 0x1,
471 /**
472 * A client may reach a point where it has partially rendered a frame
473 * through a GrContext that it knows the user will never see. This flag
474 * causes the flush to skip submission of deferred content to the 3D API
475 * during the flush.
476 */
477 kDiscard_FlushBit = 0x2,
478 };
479
480 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000481 * Call to ensure all drawing to the context has been issued to the
482 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000483 * @param flagsBitfield flags that control the flushing behavior. See
484 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000486 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000487
bsalomon@google.com27847de2011-02-22 20:59:41 +0000488 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000489 * Reads a rectangle of pixels from a render target.
490 * @param renderTarget the render target to read from. NULL means the
491 * current render target.
492 * @param left left edge of the rectangle to read (inclusive)
493 * @param top top edge of the rectangle to read (inclusive)
494 * @param width width of rectangle to read in pixels.
495 * @param height height of rectangle to read in pixels.
496 * @param config the pixel config of the destination buffer
497 * @param buffer memory to read the rectangle into.
498 *
499 * @return true if the read succeeded, false if not. The read can fail
500 * because of a unsupported pixel config or because no render
501 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000502 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000503 bool readRenderTargetPixels(GrRenderTarget* target,
504 int left, int top, int width, int height,
505 GrPixelConfig config, void* buffer);
506
507 /**
508 * Reads a rectangle of pixels from a texture.
509 * @param texture the render target to read from.
510 * @param left left edge of the rectangle to read (inclusive)
511 * @param top top edge of the rectangle to read (inclusive)
512 * @param width width of rectangle to read in pixels.
513 * @param height height of rectangle to read in pixels.
514 * @param config the pixel config of the destination buffer
515 * @param buffer memory to read the rectangle into.
516 *
517 * @return true if the read succeeded, false if not. The read can fail
518 * because of a unsupported pixel config.
519 */
520 bool readTexturePixels(GrTexture* target,
521 int left, int top, int width, int height,
522 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523
524 /**
525 * Copy the src pixels [buffer, stride, pixelconfig] into the current
526 * render-target at the specified rectangle.
527 */
528 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000529 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000531 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000532 * Applies a 1D convolution kernel in the X direction to a rectangle of
533 * pixels from a given texture.
534 * @param texture the texture to read from
535 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000536 * @param kernel the convolution kernel (kernelWidth elements)
537 * @param kernelWidth the width of the convolution kernel
538 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000539 void convolveInX(GrTexture* texture,
540 const SkRect& rect,
541 const float* kernel,
542 int kernelWidth);
543 /**
544 * Applies a 1D convolution kernel in the Y direction to a rectangle of
545 * pixels from a given texture.
546 * direction.
547 * @param texture the texture to read from
548 * @param rect the destination rectangle
549 * @param kernel the convolution kernel (kernelWidth elements)
550 * @param kernelWidth the width of the convolution kernel
551 */
552 void convolveInY(GrTexture* texture,
553 const SkRect& rect,
554 const float* kernel,
555 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556 ///////////////////////////////////////////////////////////////////////////
557 // Helpers
558
559 class AutoRenderTarget : ::GrNoncopyable {
560 public:
561 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
562 fContext = NULL;
563 fPrevTarget = context->getRenderTarget();
564 if (fPrevTarget != target) {
565 context->setRenderTarget(target);
566 fContext = context;
567 }
568 }
569 ~AutoRenderTarget() {
570 if (fContext) {
571 fContext->setRenderTarget(fPrevTarget);
572 }
573 }
574 private:
575 GrContext* fContext;
576 GrRenderTarget* fPrevTarget;
577 };
578
579
580 ///////////////////////////////////////////////////////////////////////////
581 // Functions intended for internal use only.
582 GrGpu* getGpu() { return fGpu; }
583 GrFontCache* getFontCache() { return fFontCache; }
584 GrDrawTarget* getTextTarget(const GrPaint& paint);
585 void flushText();
586 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000587 void resetStats();
588 const GrGpuStats& getStats() const;
589 void printStats() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590
591private:
592 // used to keep track of when we need to flush the draw buffer
593 enum DrawCategory {
594 kBuffered_DrawCategory, // last draw was inserted in draw buffer
595 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
596 kText_DrawCategory // text context was last to draw
597 };
598 DrawCategory fLastDrawCategory;
599
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000600 GrGpu* fGpu;
601 GrResourceCache* fTextureCache;
602 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000603
604 GrPathRenderer* fCustomPathRenderer;
605 GrDefaultPathRenderer fDefaultPathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000606
607 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
608 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
609 GrInOrderDrawBuffer* fDrawBuffer;
610
bsalomon@google.com205d4602011-04-25 12:43:45 +0000611 GrIndexBuffer* fAAFillRectIndexBuffer;
612 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000613 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000614
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000616
bsalomon@google.com205d4602011-04-25 12:43:45 +0000617 void fillAARect(GrDrawTarget* target,
618 const GrPaint& paint,
619 const GrRect& devRect);
620
621 void strokeAARect(GrDrawTarget* target,
622 const GrPaint& paint,
623 const GrRect& devRect,
624 const GrVec& devStrokeSize);
625
626 inline int aaFillRectIndexCount() const;
627 GrIndexBuffer* aaFillRectIndexBuffer();
628
629 inline int aaStrokeRectIndexCount() const;
630 GrIndexBuffer* aaStrokeRectIndexBuffer();
631
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000632 void setupDrawBuffer();
633
bsalomon@google.com27847de2011-02-22 20:59:41 +0000634 void flushDrawBuffer();
635
636 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
637
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
639
640 void drawClipIntoStencil();
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000641
bsalomon@google.comee435122011-07-01 14:57:55 +0000642 GrPathRenderer* getPathRenderer(const GrPath&, GrPathFill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000643
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000644 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000645
bsalomon@google.com91958362011-06-13 17:58:13 +0000646 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000647 bool doOffscreenAA(GrDrawTarget* target,
648 const GrPaint& paint,
649 bool isLines) const;
650
bsalomon@google.com91958362011-06-13 17:58:13 +0000651 // attempts to setup offscreen AA. All paint state must be transferred to
652 // target by the time this is called.
653 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000654 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000655 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000656 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000657 OffscreenRecord* record);
658
bsalomon@google.com91958362011-06-13 17:58:13 +0000659 // sets up target to draw coverage to the supersampled render target
660 void setupOffscreenAAPass1(GrDrawTarget* target,
661 const GrIRect& boundRect,
662 int tileX, int tileY,
663 OffscreenRecord* record);
664
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000665 // sets up target to sample coverage of supersampled render target back
666 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000667 void doOffscreenAAPass2(GrDrawTarget* target,
668 const GrPaint& paint,
669 const GrIRect& boundRect,
670 int tileX, int tileY,
671 OffscreenRecord* record);
672
673 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000674 void cleanupOffscreenAA(GrDrawTarget* target,
675 GrPathRenderer* pr,
676 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000677
678 void convolve(GrTexture* texture,
679 const SkRect& rect,
680 float imageIncrement[2],
681 const float* kernel,
682 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000683
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000684 // computes vertex layout bits based on the paint. If paint expresses
685 // a texture for a stage, the stage coords will be bound to postitions
686 // unless hasTexCoords[s]==true in which case stage s's input coords
687 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
688 // for an array where all the values are false.
689 static int PaintStageVertexLayoutBits(
690 const GrPaint& paint,
691 const bool hasTexCoords[GrPaint::kTotalStages]);
692
bsalomon@google.com27847de2011-02-22 20:59:41 +0000693};
694
695/**
696 * Save/restore the view-matrix in the context.
697 */
698class GrAutoMatrix : GrNoncopyable {
699public:
700 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
701 fMatrix = ctx->getMatrix();
702 }
703 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
704 fMatrix = ctx->getMatrix();
705 ctx->setMatrix(matrix);
706 }
707 ~GrAutoMatrix() {
708 fContext->setMatrix(fMatrix);
709 }
710
711private:
712 GrContext* fContext;
713 GrMatrix fMatrix;
714};
715
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000716/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000717 * Gets and locks a scratch texture from a descriptor using
718 * either exact or approximate criteria. Unlocks texture in
719 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000720 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000721class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000722public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000723 GrAutoScratchTexture()
724 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000725 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000726
727 GrAutoScratchTexture(GrContext* context,
728 const GrTextureDesc& desc,
729 GrContext::ScratchTexMatch match =
730 GrContext::kApprox_ScratchTexMatch)
731 : fContext(NULL) {
732 this->set(context, desc, match);
733 }
734
735 ~GrAutoScratchTexture() {
736 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000737 fContext->unlockTexture(fEntry);
738 }
739 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000740
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000741 GrTexture* set(GrContext* context,
742 const GrTextureDesc& desc,
743 GrContext::ScratchTexMatch match =
744 GrContext::kApprox_ScratchTexMatch) {
745 if (NULL != fContext) {
746 fContext->unlockTexture(fEntry);
747 }
748 fContext = context;
749 if (NULL != fContext) {
750 fEntry = fContext->lockScratchTexture(desc, match);
751 GrTexture* ret = fEntry.texture();
752 if (NULL == ret) {
753 fContext = NULL;
754 }
755 return ret;
756 } else {
757 return NULL;
758 }
759 }
760
761 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000762private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000763 GrContext* fContext;
764 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000765};
766
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767#endif
768
769#include "GrContext_impl.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000770