blob: e07cf42288d97dde024f3fb2334e896f8f4e9f89 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrContext_DEFINED
11#define GrContext_DEFINED
12
13#include "GrClip.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000014#include "GrPaint.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000015// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
16// remove.
17#include "GrRenderTarget.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018
bsalomon@google.com583a1e32011-08-17 13:42:46 +000019class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000020class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000021class GrGpu;
22struct GrGpuStats;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000023class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000024class GrIndexBufferAllocPool;
25class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000026class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000027class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000028class GrResourceEntry;
29class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000030class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000031class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000032class GrVertexBufferAllocPool;
33
bsalomon@google.com91826102011-03-21 19:51:57 +000034class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000035public:
36 /**
37 * Creates a GrContext from within a 3D context.
38 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000039 static GrContext* Create(GrEngine engine,
40 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000041
42 /**
43 * Helper to create a opengl-shader based context
44 */
45 static GrContext* CreateGLShaderContext();
46
47 virtual ~GrContext();
48
49 /**
50 * The GrContext normally assumes that no outsider is setting state
51 * within the underlying 3D API's context/device/whatever. This call informs
52 * the context that the state was modified and it should resend. Shouldn't
53 * be called frequently for good performance.
54 */
55 void resetContext();
56
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057 /**
58 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
59 * if you have lost the associated GPU context, and thus internal texture,
60 * buffer, etc. references/IDs are now invalid. Should be called even when
61 * GrContext is no longer going to be used for two reasons:
62 * 1) ~GrContext will not try to free the objects in the 3D API.
63 * 2) If you've created GrResources that outlive the GrContext they will
64 * be marked as invalid (GrResource::isValid()) and won't attempt to
65 * free their underlying resource in the 3D API.
66 * Content drawn since the last GrContext::flush() may be lost.
67 */
68 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000069
70 /**
junov@google.com53a55842011-06-08 22:55:10 +000071 * Similar to contextLost, but makes no attempt to reset state.
72 * Use this method when GrContext destruction is pending, but
73 * the graphics context is destroyed first.
74 */
75 void contextDestroyed();
76
77 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 * Frees gpu created by the context. Can be called to reduce GPU memory
79 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000080 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 void freeGpuResources();
82
83 ///////////////////////////////////////////////////////////////////////////
84 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000085
86 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000087 * Token that refers to an entry in the texture cache. Returned by
88 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000089 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 class TextureCacheEntry {
91 public:
92 TextureCacheEntry() : fEntry(NULL) {}
93 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
94 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
95 fEntry = e.fEntry;
96 return *this;
97 }
98 GrTexture* texture() const;
99 void reset() { fEntry = NULL; }
100 private:
101 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
102 void set(GrResourceEntry* entry) { fEntry = entry; }
103 GrResourceEntry* cacheEntry() { return fEntry; }
104 GrResourceEntry* fEntry;
105 friend class GrContext;
106 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000107
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000108 /**
109 * Key generated by client. Should be a unique key on the texture data.
110 * Does not need to consider that width and height of the texture. Two
111 * textures with the same TextureKey but different bounds will not collide.
112 */
113 typedef uint64_t TextureKey;
114
115 /**
116 * Search for an entry based on key and dimensions. If found, "lock" it and
117 * return it. The entry's texture() function will return NULL if not found.
118 * Must call be balanced with an unlockTexture() call.
119 */
120 TextureCacheEntry findAndLockTexture(TextureKey key,
121 int width,
122 int height,
123 const GrSamplerState&);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000124
125 /**
126 * Create a new entry, based on the specified key and texture, and return
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000127 * its "locked" entry. Must call be balanced with an unlockTexture() call.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000128 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000129 TextureCacheEntry createAndLockTexture(TextureKey key,
130 const GrSamplerState&,
131 const GrTextureDesc&,
132 void* srcData, size_t rowBytes);
133
134 /**
135 * Enum that determines how closely a returned scratch texture must match
136 * a provided GrTextureDesc.
137 */
138 enum ScratchTexMatch {
139 /**
140 * Finds a texture that exactly matches the descriptor.
141 */
142 kExact_ScratchTexMatch,
143 /**
144 * Finds a texture that approximately matches the descriptor. Will be
145 * at least as large in width and height as desc specifies. If desc
146 * specifies that texture is a render target then result will be a
147 * render target. If desc specifies a render target and doesn't set the
148 * no stencil flag then result will have a stencil. Format and aa level
149 * will always match.
150 */
151 kApprox_ScratchTexMatch
152 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000153
154 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000155 * Returns a texture matching the desc. It's contents are unknown. Subsequent
156 * requests with the same descriptor are not guaranteed to return the same
157 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000158 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000159 *
160 * Textures created by createAndLockTexture() hide the complications of
161 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000162 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000163 * such an API will create gaps in the tiling pattern. This includes clamp
164 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000166 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000167
168 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000169 * When done with an entry, call unlockTexture(entry) on it, which returns
170 * it to the cache, where it may be purged.
171 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000173
174 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000175 * Creates a texture that is outside the cache. Does not count against
176 * cache's budget.
177 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000178 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000179 void* srcData,
180 size_t rowBytes);
181
182 /**
183 * Returns true if the specified use of an indexed texture is supported.
184 */
185 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
186
187 /**
188 * Return the current texture cache limits.
189 *
190 * @param maxTextures If non-null, returns maximum number of textures that
191 * can be held in the cache.
192 * @param maxTextureBytes If non-null, returns maximum number of bytes of
193 * texture memory that can be held in the cache.
194 */
195 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
196
197 /**
198 * Specify the texture cache limits. If the current cache exceeds either
199 * of these, it will be purged (LRU) to keep the cache within these limits.
200 *
201 * @param maxTextures The maximum number of textures that can be held in
202 * the cache.
203 * @param maxTextureBytes The maximum number of bytes of texture memory
204 * that can be held in the cache.
205 */
206 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
207
208 /**
209 * Return the max width or height of a texture supported by the current gpu
210 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000211 int getMaxTextureSize() const;
212
213 /**
214 * Return the max width or height of a render target supported by the
215 * current gpu
216 */
217 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218
219 ///////////////////////////////////////////////////////////////////////////
220 // Render targets
221
222 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000223 * Sets the render target.
224 * @param target the render target to set. (should not be NULL.)
225 */
226 void setRenderTarget(GrRenderTarget* target);
227
228 /**
229 * Gets the current render target.
230 * @return the currently bound render target. Should never be NULL.
231 */
232 const GrRenderTarget* getRenderTarget() const;
233 GrRenderTarget* getRenderTarget();
234
235 ///////////////////////////////////////////////////////////////////////////
236 // Platform Surfaces
237
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000238 /**
239 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
240 * the type of object returned. If kIsTexture is set the returned object
241 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
242 * are set the render target object is accessible by
243 * GrTexture::asRenderTarget().
244 *
245 * GL: if the object is a texture Gr may change its GL texture parameters
246 * when it is drawn.
247 *
248 * @param desc description of the object to create.
249 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
250 * on failure.
251 */
252 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253
bsalomon@google.com27847de2011-02-22 20:59:41 +0000254 ///////////////////////////////////////////////////////////////////////////
255 // Matrix state
256
257 /**
258 * Gets the current transformation matrix.
259 * @return the current matrix.
260 */
261 const GrMatrix& getMatrix() const;
262
263 /**
264 * Sets the transformation matrix.
265 * @param m the matrix to set.
266 */
267 void setMatrix(const GrMatrix& m);
268
269 /**
270 * Concats the current matrix. The passed matrix is applied before the
271 * current matrix.
272 * @param m the matrix to concat.
273 */
274 void concatMatrix(const GrMatrix& m) const;
275
276
277 ///////////////////////////////////////////////////////////////////////////
278 // Clip state
279 /**
280 * Gets the current clip.
281 * @return the current clip.
282 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000283 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000284
285 /**
286 * Sets the clip.
287 * @param clip the clip to set.
288 */
289 void setClip(const GrClip& clip);
290
291 /**
292 * Convenience method for setting the clip to a rect.
293 * @param rect the rect to set as the new clip.
294 */
295 void setClip(const GrIRect& rect);
296
297 ///////////////////////////////////////////////////////////////////////////
298 // Draws
299
300 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000301 * Clear the entire or rect of the render target, ignoring any clips.
302 * @param rect the rect to clear or the whole thing if rect is NULL.
303 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000304 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000305 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000306
307 /**
308 * Draw everywhere (respecting the clip) with the paint.
309 */
310 void drawPaint(const GrPaint& paint);
311
312 /**
313 * Draw the rect using a paint.
314 * @param paint describes how to color pixels.
315 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
316 * the rect is mitered stroked based on strokeWidth. If
317 * strokeWidth == 0, then the stroke is always a single
318 * pixel thick.
319 * @param matrix Optional matrix applied to the rect. Applied before
320 * context's matrix or the paint's matrix.
321 * The rects coords are used to access the paint (through texture matrix)
322 */
323 void drawRect(const GrPaint& paint,
324 const GrRect&,
325 GrScalar strokeWidth = -1,
326 const GrMatrix* matrix = NULL);
327
328 /**
329 * Maps a rect of paint coordinates onto the a rect of destination
330 * coordinates. Each rect can optionally be transformed. The srcRect
331 * is stretched over the dstRect. The dstRect is transformed by the
332 * context's matrix and the srcRect is transformed by the paint's matrix.
333 * Additional optional matrices can be provided by parameters.
334 *
335 * @param paint describes how to color pixels.
336 * @param dstRect the destination rect to draw.
337 * @param srcRect rect of paint coordinates to be mapped onto dstRect
338 * @param dstMatrix Optional matrix to transform dstRect. Applied before
339 * context's matrix.
340 * @param srcMatrix Optional matrix to transform srcRect Applied before
341 * paint's matrix.
342 */
343 void drawRectToRect(const GrPaint& paint,
344 const GrRect& dstRect,
345 const GrRect& srcRect,
346 const GrMatrix* dstMatrix = NULL,
347 const GrMatrix* srcMatrix = NULL);
348
349 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351 *
352 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000353 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000354 * @param fill the path filling rule to use.
355 * @param translate optional additional translation applied to the
356 * path.
357 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000358 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000360
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361 /**
362 * Draws vertices with a paint.
363 *
364 * @param paint describes how to color pixels.
365 * @param primitiveType primitives type to draw.
366 * @param vertexCount number of vertices.
367 * @param positions array of vertex positions, required.
368 * @param texCoords optional array of texture coordinates used
369 * to access the paint.
370 * @param colors optional array of per-vertex colors, supercedes
371 * the paint's color field.
372 * @param indices optional array of indices. If NULL vertices
373 * are drawn non-indexed.
374 * @param indexCount if indices is non-null then this is the
375 * number of indices.
376 */
377 void drawVertices(const GrPaint& paint,
378 GrPrimitiveType primitiveType,
379 int vertexCount,
380 const GrPoint positions[],
381 const GrPoint texs[],
382 const GrColor colors[],
383 const uint16_t indices[],
384 int indexCount);
385
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386 ///////////////////////////////////////////////////////////////////////////
387 // Misc.
388
389 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000390 * Flags that affect flush() behavior.
391 */
392 enum FlushBits {
393 /**
394 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
395 * it can be rendered to directly. However, Gr lazily sets state. Simply
396 * calling setRenderTarget() followed by flush() without flags may not
397 * bind the render target. This flag forces the context to bind the last
398 * set render target in the 3D API.
399 */
400 kForceCurrentRenderTarget_FlushBit = 0x1,
401 /**
402 * A client may reach a point where it has partially rendered a frame
403 * through a GrContext that it knows the user will never see. This flag
404 * causes the flush to skip submission of deferred content to the 3D API
405 * during the flush.
406 */
407 kDiscard_FlushBit = 0x2,
408 };
409
410 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411 * Call to ensure all drawing to the context has been issued to the
412 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000413 * @param flagsBitfield flags that control the flushing behavior. See
414 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000416 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000417
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000419 * Reads a rectangle of pixels from a render target.
420 * @param renderTarget the render target to read from. NULL means the
421 * current render target.
422 * @param left left edge of the rectangle to read (inclusive)
423 * @param top top edge of the rectangle to read (inclusive)
424 * @param width width of rectangle to read in pixels.
425 * @param height height of rectangle to read in pixels.
426 * @param config the pixel config of the destination buffer
427 * @param buffer memory to read the rectangle into.
428 *
429 * @return true if the read succeeded, false if not. The read can fail
430 * because of a unsupported pixel config or because no render
431 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000433 bool readRenderTargetPixels(GrRenderTarget* target,
434 int left, int top, int width, int height,
435 GrPixelConfig config, void* buffer);
436
437 /**
438 * Reads a rectangle of pixels from a texture.
439 * @param texture the render target to read from.
440 * @param left left edge of the rectangle to read (inclusive)
441 * @param top top edge of the rectangle to read (inclusive)
442 * @param width width of rectangle to read in pixels.
443 * @param height height of rectangle to read in pixels.
444 * @param config the pixel config of the destination buffer
445 * @param buffer memory to read the rectangle into.
446 *
447 * @return true if the read succeeded, false if not. The read can fail
448 * because of a unsupported pixel config.
449 */
450 bool readTexturePixels(GrTexture* target,
451 int left, int top, int width, int height,
452 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000453
454 /**
455 * Copy the src pixels [buffer, stride, pixelconfig] into the current
456 * render-target at the specified rectangle.
457 */
458 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000459 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000461 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000462 * Applies a 1D convolution kernel in the X direction to a rectangle of
463 * pixels from a given texture.
464 * @param texture the texture to read from
465 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000466 * @param kernel the convolution kernel (kernelWidth elements)
467 * @param kernelWidth the width of the convolution kernel
468 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000469 void convolveInX(GrTexture* texture,
470 const SkRect& rect,
471 const float* kernel,
472 int kernelWidth);
473 /**
474 * Applies a 1D convolution kernel in the Y direction to a rectangle of
475 * pixels from a given texture.
476 * direction.
477 * @param texture the texture to read from
478 * @param rect the destination rectangle
479 * @param kernel the convolution kernel (kernelWidth elements)
480 * @param kernelWidth the width of the convolution kernel
481 */
482 void convolveInY(GrTexture* texture,
483 const SkRect& rect,
484 const float* kernel,
485 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000486 ///////////////////////////////////////////////////////////////////////////
487 // Helpers
488
489 class AutoRenderTarget : ::GrNoncopyable {
490 public:
491 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
492 fContext = NULL;
493 fPrevTarget = context->getRenderTarget();
494 if (fPrevTarget != target) {
495 context->setRenderTarget(target);
496 fContext = context;
497 }
498 }
499 ~AutoRenderTarget() {
500 if (fContext) {
501 fContext->setRenderTarget(fPrevTarget);
502 }
503 }
504 private:
505 GrContext* fContext;
506 GrRenderTarget* fPrevTarget;
507 };
508
509
510 ///////////////////////////////////////////////////////////////////////////
511 // Functions intended for internal use only.
512 GrGpu* getGpu() { return fGpu; }
513 GrFontCache* getFontCache() { return fFontCache; }
514 GrDrawTarget* getTextTarget(const GrPaint& paint);
515 void flushText();
516 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000517 void resetStats();
518 const GrGpuStats& getStats() const;
519 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000520 /**
521 * Stencil buffers add themselves to the cache using
522 * addAndLockStencilBuffer. When a SB's RT-attachment count
523 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
524 * eligible for purging. findStencilBuffer is called to check the cache for
525 * a SB that matching an RT's criteria. If a match is found that has been
526 * unlocked (its attachment count has reached 0) then it will be relocked.
527 */
528 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
529 void unlockStencilBuffer(GrResourceEntry* sbEntry);
530 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531
532private:
533 // used to keep track of when we need to flush the draw buffer
534 enum DrawCategory {
535 kBuffered_DrawCategory, // last draw was inserted in draw buffer
536 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
537 kText_DrawCategory // text context was last to draw
538 };
539 DrawCategory fLastDrawCategory;
540
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000541 GrGpu* fGpu;
542 GrResourceCache* fTextureCache;
543 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000544
bsalomon@google.com30085192011-08-19 15:42:31 +0000545 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546
547 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
548 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
549 GrInOrderDrawBuffer* fDrawBuffer;
550
bsalomon@google.com205d4602011-04-25 12:43:45 +0000551 GrIndexBuffer* fAAFillRectIndexBuffer;
552 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000553 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000554
bsalomon@google.com27847de2011-02-22 20:59:41 +0000555 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000556
bsalomon@google.com205d4602011-04-25 12:43:45 +0000557 void fillAARect(GrDrawTarget* target,
558 const GrPaint& paint,
559 const GrRect& devRect);
560
561 void strokeAARect(GrDrawTarget* target,
562 const GrPaint& paint,
563 const GrRect& devRect,
564 const GrVec& devStrokeSize);
565
566 inline int aaFillRectIndexCount() const;
567 GrIndexBuffer* aaFillRectIndexBuffer();
568
569 inline int aaStrokeRectIndexCount() const;
570 GrIndexBuffer* aaStrokeRectIndexBuffer();
571
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000572 void setupDrawBuffer();
573
bsalomon@google.com27847de2011-02-22 20:59:41 +0000574 void flushDrawBuffer();
575
576 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
577
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
579
bsalomon@google.com30085192011-08-19 15:42:31 +0000580 GrPathRenderer* getPathRenderer(const GrDrawTarget* target,
581 const GrPath& path,
582 GrPathFill fill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000583
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000584 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000585
bsalomon@google.com91958362011-06-13 17:58:13 +0000586 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000587 bool doOffscreenAA(GrDrawTarget* target,
588 const GrPaint& paint,
bsalomon@google.com471d4712011-08-23 15:45:25 +0000589 bool isHairLines) const;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000590
bsalomon@google.com91958362011-06-13 17:58:13 +0000591 // attempts to setup offscreen AA. All paint state must be transferred to
592 // target by the time this is called.
593 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000594 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000595 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000596 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000597 OffscreenRecord* record);
598
bsalomon@google.com91958362011-06-13 17:58:13 +0000599 // sets up target to draw coverage to the supersampled render target
600 void setupOffscreenAAPass1(GrDrawTarget* target,
601 const GrIRect& boundRect,
602 int tileX, int tileY,
603 OffscreenRecord* record);
604
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000605 // sets up target to sample coverage of supersampled render target back
606 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000607 void doOffscreenAAPass2(GrDrawTarget* target,
608 const GrPaint& paint,
609 const GrIRect& boundRect,
610 int tileX, int tileY,
611 OffscreenRecord* record);
612
613 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000614 void cleanupOffscreenAA(GrDrawTarget* target,
615 GrPathRenderer* pr,
616 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000617
618 void convolve(GrTexture* texture,
619 const SkRect& rect,
620 float imageIncrement[2],
621 const float* kernel,
622 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000623
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000624 // computes vertex layout bits based on the paint. If paint expresses
625 // a texture for a stage, the stage coords will be bound to postitions
626 // unless hasTexCoords[s]==true in which case stage s's input coords
627 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
628 // for an array where all the values are false.
629 static int PaintStageVertexLayoutBits(
630 const GrPaint& paint,
631 const bool hasTexCoords[GrPaint::kTotalStages]);
632
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633};
634
635/**
636 * Save/restore the view-matrix in the context.
637 */
638class GrAutoMatrix : GrNoncopyable {
639public:
640 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
641 fMatrix = ctx->getMatrix();
642 }
643 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
644 fMatrix = ctx->getMatrix();
645 ctx->setMatrix(matrix);
646 }
647 ~GrAutoMatrix() {
648 fContext->setMatrix(fMatrix);
649 }
650
651private:
652 GrContext* fContext;
653 GrMatrix fMatrix;
654};
655
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000656/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000657 * Gets and locks a scratch texture from a descriptor using
658 * either exact or approximate criteria. Unlocks texture in
659 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000660 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000661class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000662public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000663 GrAutoScratchTexture()
664 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000665 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000666
667 GrAutoScratchTexture(GrContext* context,
668 const GrTextureDesc& desc,
669 GrContext::ScratchTexMatch match =
670 GrContext::kApprox_ScratchTexMatch)
671 : fContext(NULL) {
672 this->set(context, desc, match);
673 }
674
675 ~GrAutoScratchTexture() {
676 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000677 fContext->unlockTexture(fEntry);
678 }
679 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000680
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000681 GrTexture* set(GrContext* context,
682 const GrTextureDesc& desc,
683 GrContext::ScratchTexMatch match =
684 GrContext::kApprox_ScratchTexMatch) {
685 if (NULL != fContext) {
686 fContext->unlockTexture(fEntry);
687 }
688 fContext = context;
689 if (NULL != fContext) {
690 fEntry = fContext->lockScratchTexture(desc, match);
691 GrTexture* ret = fEntry.texture();
692 if (NULL == ret) {
693 fContext = NULL;
694 }
695 return ret;
696 } else {
697 return NULL;
698 }
699 }
700
701 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000702private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000703 GrContext* fContext;
704 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000705};
706
bsalomon@google.com27847de2011-02-22 20:59:41 +0000707#endif
708