blob: 3fe1a7b36714e8a71c0ae30d74b6ef3bcb7258c0 [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 */
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000185 bool supportsIndex8PixelConfig(const GrSamplerState&,
186 int width,
187 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000188
189 /**
190 * Return the current texture cache limits.
191 *
192 * @param maxTextures If non-null, returns maximum number of textures that
193 * can be held in the cache.
194 * @param maxTextureBytes If non-null, returns maximum number of bytes of
195 * texture memory that can be held in the cache.
196 */
197 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
198
199 /**
200 * Specify the texture cache limits. If the current cache exceeds either
201 * of these, it will be purged (LRU) to keep the cache within these limits.
202 *
203 * @param maxTextures The maximum number of textures that can be held in
204 * the cache.
205 * @param maxTextureBytes The maximum number of bytes of texture memory
206 * that can be held in the cache.
207 */
208 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
209
210 /**
211 * Return the max width or height of a texture supported by the current gpu
212 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000213 int getMaxTextureSize() const;
214
215 /**
216 * Return the max width or height of a render target supported by the
217 * current gpu
218 */
219 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220
221 ///////////////////////////////////////////////////////////////////////////
222 // Render targets
223
224 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000225 * Sets the render target.
226 * @param target the render target to set. (should not be NULL.)
227 */
228 void setRenderTarget(GrRenderTarget* target);
229
230 /**
231 * Gets the current render target.
232 * @return the currently bound render target. Should never be NULL.
233 */
234 const GrRenderTarget* getRenderTarget() const;
235 GrRenderTarget* getRenderTarget();
236
237 ///////////////////////////////////////////////////////////////////////////
238 // Platform Surfaces
239
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000240 /**
241 * Wraps an existing 3D API surface in a GrObject. desc.fFlags determines
242 * the type of object returned. If kIsTexture is set the returned object
243 * will be a GrTexture*. Otherwise, it will be a GrRenderTarget*. If both
244 * are set the render target object is accessible by
245 * GrTexture::asRenderTarget().
246 *
247 * GL: if the object is a texture Gr may change its GL texture parameters
248 * when it is drawn.
249 *
250 * @param desc description of the object to create.
251 * @return either a GrTexture* or GrRenderTarget* depending on desc. NULL
252 * on failure.
253 */
254 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255
bsalomon@google.com27847de2011-02-22 20:59:41 +0000256 ///////////////////////////////////////////////////////////////////////////
257 // Matrix state
258
259 /**
260 * Gets the current transformation matrix.
261 * @return the current matrix.
262 */
263 const GrMatrix& getMatrix() const;
264
265 /**
266 * Sets the transformation matrix.
267 * @param m the matrix to set.
268 */
269 void setMatrix(const GrMatrix& m);
270
271 /**
272 * Concats the current matrix. The passed matrix is applied before the
273 * current matrix.
274 * @param m the matrix to concat.
275 */
276 void concatMatrix(const GrMatrix& m) const;
277
278
279 ///////////////////////////////////////////////////////////////////////////
280 // Clip state
281 /**
282 * Gets the current clip.
283 * @return the current clip.
284 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000285 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000286
287 /**
288 * Sets the clip.
289 * @param clip the clip to set.
290 */
291 void setClip(const GrClip& clip);
292
293 /**
294 * Convenience method for setting the clip to a rect.
295 * @param rect the rect to set as the new clip.
296 */
297 void setClip(const GrIRect& rect);
298
299 ///////////////////////////////////////////////////////////////////////////
300 // Draws
301
302 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000303 * Clear the entire or rect of the render target, ignoring any clips.
304 * @param rect the rect to clear or the whole thing if rect is NULL.
305 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000306 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000307 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000308
309 /**
310 * Draw everywhere (respecting the clip) with the paint.
311 */
312 void drawPaint(const GrPaint& paint);
313
314 /**
315 * Draw the rect using a paint.
316 * @param paint describes how to color pixels.
317 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
318 * the rect is mitered stroked based on strokeWidth. If
319 * strokeWidth == 0, then the stroke is always a single
320 * pixel thick.
321 * @param matrix Optional matrix applied to the rect. Applied before
322 * context's matrix or the paint's matrix.
323 * The rects coords are used to access the paint (through texture matrix)
324 */
325 void drawRect(const GrPaint& paint,
326 const GrRect&,
327 GrScalar strokeWidth = -1,
328 const GrMatrix* matrix = NULL);
329
330 /**
331 * Maps a rect of paint coordinates onto the a rect of destination
332 * coordinates. Each rect can optionally be transformed. The srcRect
333 * is stretched over the dstRect. The dstRect is transformed by the
334 * context's matrix and the srcRect is transformed by the paint's matrix.
335 * Additional optional matrices can be provided by parameters.
336 *
337 * @param paint describes how to color pixels.
338 * @param dstRect the destination rect to draw.
339 * @param srcRect rect of paint coordinates to be mapped onto dstRect
340 * @param dstMatrix Optional matrix to transform dstRect. Applied before
341 * context's matrix.
342 * @param srcMatrix Optional matrix to transform srcRect Applied before
343 * paint's matrix.
344 */
345 void drawRectToRect(const GrPaint& paint,
346 const GrRect& dstRect,
347 const GrRect& srcRect,
348 const GrMatrix* dstMatrix = NULL,
349 const GrMatrix* srcMatrix = NULL);
350
351 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000352 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353 *
354 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000355 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356 * @param fill the path filling rule to use.
357 * @param translate optional additional translation applied to the
358 * path.
359 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000360 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000362
bsalomon@google.com27847de2011-02-22 20:59:41 +0000363 /**
364 * Draws vertices with a paint.
365 *
366 * @param paint describes how to color pixels.
367 * @param primitiveType primitives type to draw.
368 * @param vertexCount number of vertices.
369 * @param positions array of vertex positions, required.
370 * @param texCoords optional array of texture coordinates used
371 * to access the paint.
372 * @param colors optional array of per-vertex colors, supercedes
373 * the paint's color field.
374 * @param indices optional array of indices. If NULL vertices
375 * are drawn non-indexed.
376 * @param indexCount if indices is non-null then this is the
377 * number of indices.
378 */
379 void drawVertices(const GrPaint& paint,
380 GrPrimitiveType primitiveType,
381 int vertexCount,
382 const GrPoint positions[],
383 const GrPoint texs[],
384 const GrColor colors[],
385 const uint16_t indices[],
386 int indexCount);
387
bsalomon@google.com27847de2011-02-22 20:59:41 +0000388 ///////////////////////////////////////////////////////////////////////////
389 // Misc.
390
391 /**
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000392 * Currently needed by SkGpuDevice. Ideally this shouldn't be exposed.
393 */
394 bool supportsShaders() const;
395
396 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000397 * Flags that affect flush() behavior.
398 */
399 enum FlushBits {
400 /**
401 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
402 * it can be rendered to directly. However, Gr lazily sets state. Simply
403 * calling setRenderTarget() followed by flush() without flags may not
404 * bind the render target. This flag forces the context to bind the last
405 * set render target in the 3D API.
406 */
407 kForceCurrentRenderTarget_FlushBit = 0x1,
408 /**
409 * A client may reach a point where it has partially rendered a frame
410 * through a GrContext that it knows the user will never see. This flag
411 * causes the flush to skip submission of deferred content to the 3D API
412 * during the flush.
413 */
414 kDiscard_FlushBit = 0x2,
415 };
416
417 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 * Call to ensure all drawing to the context has been issued to the
419 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000420 * @param flagsBitfield flags that control the flushing behavior. See
421 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000422 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000423 void flush(int flagsBitfield = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000424
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000426 * Reads a rectangle of pixels from a render target.
427 * @param renderTarget the render target to read from. NULL means the
428 * current render target.
429 * @param left left edge of the rectangle to read (inclusive)
430 * @param top top edge of the rectangle to read (inclusive)
431 * @param width width of rectangle to read in pixels.
432 * @param height height of rectangle to read in pixels.
433 * @param config the pixel config of the destination buffer
434 * @param buffer memory to read the rectangle into.
435 *
436 * @return true if the read succeeded, false if not. The read can fail
437 * because of a unsupported pixel config or because no render
438 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000439 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000440 bool readRenderTargetPixels(GrRenderTarget* target,
441 int left, int top, int width, int height,
442 GrPixelConfig config, void* buffer);
443
444 /**
445 * Reads a rectangle of pixels from a texture.
446 * @param texture the render target to read from.
447 * @param left left edge of the rectangle to read (inclusive)
448 * @param top top edge of the rectangle to read (inclusive)
449 * @param width width of rectangle to read in pixels.
450 * @param height height of rectangle to read in pixels.
451 * @param config the pixel config of the destination buffer
452 * @param buffer memory to read the rectangle into.
453 *
454 * @return true if the read succeeded, false if not. The read can fail
455 * because of a unsupported pixel config.
456 */
457 bool readTexturePixels(GrTexture* target,
458 int left, int top, int width, int height,
459 GrPixelConfig config, void* buffer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460
461 /**
462 * Copy the src pixels [buffer, stride, pixelconfig] into the current
463 * render-target at the specified rectangle.
464 */
465 void writePixels(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000466 GrPixelConfig, const void* buffer, size_t stride);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000468 /**
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000469 * Applies a 1D convolution kernel in the X direction to a rectangle of
470 * pixels from a given texture.
471 * @param texture the texture to read from
472 * @param rect the destination rectangle
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000473 * @param kernel the convolution kernel (kernelWidth elements)
474 * @param kernelWidth the width of the convolution kernel
475 */
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000476 void convolveInX(GrTexture* texture,
477 const SkRect& rect,
478 const float* kernel,
479 int kernelWidth);
480 /**
481 * Applies a 1D convolution kernel in the Y direction to a rectangle of
482 * pixels from a given texture.
483 * direction.
484 * @param texture the texture to read from
485 * @param rect the destination rectangle
486 * @param kernel the convolution kernel (kernelWidth elements)
487 * @param kernelWidth the width of the convolution kernel
488 */
489 void convolveInY(GrTexture* texture,
490 const SkRect& rect,
491 const float* kernel,
492 int kernelWidth);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000493 ///////////////////////////////////////////////////////////////////////////
494 // Helpers
495
496 class AutoRenderTarget : ::GrNoncopyable {
497 public:
498 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
499 fContext = NULL;
500 fPrevTarget = context->getRenderTarget();
501 if (fPrevTarget != target) {
502 context->setRenderTarget(target);
503 fContext = context;
504 }
505 }
506 ~AutoRenderTarget() {
507 if (fContext) {
508 fContext->setRenderTarget(fPrevTarget);
509 }
510 }
511 private:
512 GrContext* fContext;
513 GrRenderTarget* fPrevTarget;
514 };
515
516
517 ///////////////////////////////////////////////////////////////////////////
518 // Functions intended for internal use only.
519 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000520 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521 GrFontCache* getFontCache() { return fFontCache; }
522 GrDrawTarget* getTextTarget(const GrPaint& paint);
523 void flushText();
524 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000525 void resetStats();
526 const GrGpuStats& getStats() const;
527 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000528 /**
529 * Stencil buffers add themselves to the cache using
530 * addAndLockStencilBuffer. When a SB's RT-attachment count
531 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
532 * eligible for purging. findStencilBuffer is called to check the cache for
533 * a SB that matching an RT's criteria. If a match is found that has been
534 * unlocked (its attachment count has reached 0) then it will be relocked.
535 */
536 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
537 void unlockStencilBuffer(GrResourceEntry* sbEntry);
538 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539
540private:
541 // used to keep track of when we need to flush the draw buffer
542 enum DrawCategory {
543 kBuffered_DrawCategory, // last draw was inserted in draw buffer
544 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
545 kText_DrawCategory // text context was last to draw
546 };
547 DrawCategory fLastDrawCategory;
548
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000549 GrGpu* fGpu;
550 GrResourceCache* fTextureCache;
551 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000552
bsalomon@google.com30085192011-08-19 15:42:31 +0000553 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554
555 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
556 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
557 GrInOrderDrawBuffer* fDrawBuffer;
558
bsalomon@google.com205d4602011-04-25 12:43:45 +0000559 GrIndexBuffer* fAAFillRectIndexBuffer;
560 GrIndexBuffer* fAAStrokeRectIndexBuffer;
bsalomon@google.com91958362011-06-13 17:58:13 +0000561 int fMaxOffscreenAASize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000562
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000564
bsalomon@google.com205d4602011-04-25 12:43:45 +0000565 void fillAARect(GrDrawTarget* target,
566 const GrPaint& paint,
567 const GrRect& devRect);
568
569 void strokeAARect(GrDrawTarget* target,
570 const GrPaint& paint,
571 const GrRect& devRect,
572 const GrVec& devStrokeSize);
573
574 inline int aaFillRectIndexCount() const;
575 GrIndexBuffer* aaFillRectIndexBuffer();
576
577 inline int aaStrokeRectIndexCount() const;
578 GrIndexBuffer* aaStrokeRectIndexBuffer();
579
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000580 void setupDrawBuffer();
581
bsalomon@google.com27847de2011-02-22 20:59:41 +0000582 void flushDrawBuffer();
583
584 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
585
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
587
bsalomon@google.com30085192011-08-19 15:42:31 +0000588 GrPathRenderer* getPathRenderer(const GrDrawTarget* target,
589 const GrPath& path,
590 GrPathFill fill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000591
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000592 struct OffscreenRecord;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000593
bsalomon@google.com91958362011-06-13 17:58:13 +0000594 // determines whether offscreen AA should be applied
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000595 bool doOffscreenAA(GrDrawTarget* target,
596 const GrPaint& paint,
bsalomon@google.com471d4712011-08-23 15:45:25 +0000597 bool isHairLines) const;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000598
bsalomon@google.com91958362011-06-13 17:58:13 +0000599 // attempts to setup offscreen AA. All paint state must be transferred to
600 // target by the time this is called.
601 bool prepareForOffscreenAA(GrDrawTarget* target,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000602 bool requireStencil,
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000603 const GrIRect& boundRect,
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000604 GrPathRenderer* pr,
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000605 OffscreenRecord* record);
606
bsalomon@google.com91958362011-06-13 17:58:13 +0000607 // sets up target to draw coverage to the supersampled render target
608 void setupOffscreenAAPass1(GrDrawTarget* target,
609 const GrIRect& boundRect,
610 int tileX, int tileY,
611 OffscreenRecord* record);
612
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000613 // sets up target to sample coverage of supersampled render target back
614 // to the main render target using stage kOffscreenStage.
bsalomon@google.com91958362011-06-13 17:58:13 +0000615 void doOffscreenAAPass2(GrDrawTarget* target,
616 const GrPaint& paint,
617 const GrIRect& boundRect,
618 int tileX, int tileY,
619 OffscreenRecord* record);
620
621 // restored the draw target state and releases offscreen target to cache
tomhudson@google.comd22b6e42011-06-24 15:53:40 +0000622 void cleanupOffscreenAA(GrDrawTarget* target,
623 GrPathRenderer* pr,
624 OffscreenRecord* record);
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000625
626 void convolve(GrTexture* texture,
627 const SkRect& rect,
628 float imageIncrement[2],
629 const float* kernel,
630 int kernelWidth);
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000631
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000632 // computes vertex layout bits based on the paint. If paint expresses
633 // a texture for a stage, the stage coords will be bound to postitions
634 // unless hasTexCoords[s]==true in which case stage s's input coords
635 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
636 // for an array where all the values are false.
637 static int PaintStageVertexLayoutBits(
638 const GrPaint& paint,
639 const bool hasTexCoords[GrPaint::kTotalStages]);
640
bsalomon@google.com27847de2011-02-22 20:59:41 +0000641};
642
643/**
644 * Save/restore the view-matrix in the context.
645 */
646class GrAutoMatrix : GrNoncopyable {
647public:
648 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
649 fMatrix = ctx->getMatrix();
650 }
651 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
652 fMatrix = ctx->getMatrix();
653 ctx->setMatrix(matrix);
654 }
655 ~GrAutoMatrix() {
656 fContext->setMatrix(fMatrix);
657 }
658
659private:
660 GrContext* fContext;
661 GrMatrix fMatrix;
662};
663
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000664/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000665 * Gets and locks a scratch texture from a descriptor using
666 * either exact or approximate criteria. Unlocks texture in
667 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000668 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000669class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000670public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000671 GrAutoScratchTexture()
672 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000673 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000674
675 GrAutoScratchTexture(GrContext* context,
676 const GrTextureDesc& desc,
677 GrContext::ScratchTexMatch match =
678 GrContext::kApprox_ScratchTexMatch)
679 : fContext(NULL) {
680 this->set(context, desc, match);
681 }
682
683 ~GrAutoScratchTexture() {
684 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000685 fContext->unlockTexture(fEntry);
686 }
687 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000688
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000689 GrTexture* set(GrContext* context,
690 const GrTextureDesc& desc,
691 GrContext::ScratchTexMatch match =
692 GrContext::kApprox_ScratchTexMatch) {
693 if (NULL != fContext) {
694 fContext->unlockTexture(fEntry);
695 }
696 fContext = context;
697 if (NULL != fContext) {
698 fEntry = fContext->lockScratchTexture(desc, match);
699 GrTexture* ret = fEntry.texture();
700 if (NULL == ret) {
701 fContext = NULL;
702 }
703 return ret;
704 } else {
705 return NULL;
706 }
707 }
708
709 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000710private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000711 GrContext* fContext;
712 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000713};
714
bsalomon@google.com27847de2011-02-22 20:59:41 +0000715#endif
716