blob: 53c6cb73af6b84e62b5c4e99a6c86716ba994555 [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
robertphillips@google.com59552022012-08-31 13:07:37 +000013#include "GrConfig.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000014#include "GrPaint.h"
robertphillips@google.comf6747b02012-06-12 00:32:28 +000015#include "GrAARectRenderer.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000016#include "GrClipData.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000017// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
18// remove.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000019#include "GrRenderTarget.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000020#include "SkClipStack.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000021
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000022class GrAutoScratchTexture;
robertphillips@google.coma9b06232012-08-30 11:06:31 +000023class GrCacheKey;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000024class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000025class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000026class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000027class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000029class GrIndexBufferAllocPool;
30class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000031class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000032class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000033class GrResourceEntry;
34class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000035class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000036class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000038class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000039
bsalomon@google.com91826102011-03-21 19:51:57 +000040class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000041public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000042 SK_DECLARE_INST_COUNT(GrContext)
43
bsalomon@google.com27847de2011-02-22 20:59:41 +000044 /**
45 * Creates a GrContext from within a 3D context.
46 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000047 static GrContext* Create(GrEngine engine,
48 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000049
bsalomon@google.comc0af3172012-06-15 14:10:09 +000050 /**
51 * Returns the number of GrContext instances for the current thread.
52 */
53 static int GetThreadInstanceCount();
54
bsalomon@google.com27847de2011-02-22 20:59:41 +000055 virtual ~GrContext();
56
57 /**
58 * The GrContext normally assumes that no outsider is setting state
59 * within the underlying 3D API's context/device/whatever. This call informs
60 * the context that the state was modified and it should resend. Shouldn't
61 * be called frequently for good performance.
62 */
63 void resetContext();
64
bsalomon@google.com8fe72472011-03-30 21:26:44 +000065 /**
66 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
67 * if you have lost the associated GPU context, and thus internal texture,
68 * buffer, etc. references/IDs are now invalid. Should be called even when
69 * GrContext is no longer going to be used for two reasons:
70 * 1) ~GrContext will not try to free the objects in the 3D API.
71 * 2) If you've created GrResources that outlive the GrContext they will
72 * be marked as invalid (GrResource::isValid()) and won't attempt to
73 * free their underlying resource in the 3D API.
74 * Content drawn since the last GrContext::flush() may be lost.
75 */
76 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000077
78 /**
junov@google.com53a55842011-06-08 22:55:10 +000079 * Similar to contextLost, but makes no attempt to reset state.
80 * Use this method when GrContext destruction is pending, but
81 * the graphics context is destroyed first.
82 */
83 void contextDestroyed();
84
85 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 * Frees gpu created by the context. Can be called to reduce GPU memory
87 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000088 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 void freeGpuResources();
90
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000091 /**
92 * Returns the number of bytes of GPU memory hosted by the texture cache.
93 */
94 size_t getGpuTextureCacheBytes() const;
95
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096 ///////////////////////////////////////////////////////////////////////////
97 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000098
99 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000100 * Create a new entry, based on the specified key and texture, and return
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000101 * a "locked" texture. Must call be balanced with an unlockTexture() call.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000102 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000103 * @param params The tex params used to draw a texture may help determine
104 * the cache entry used. (e.g. different versions may exist
105 * for different wrap modes on GPUs with limited NPOT
106 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000107 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000108 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000109 * @param srcData Pointer to the pixel values.
110 * @param rowBytes The number of bytes between rows of the texture. Zero
111 * implies tightly packed rows.
112 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000113 GrTexture* createTexture(const GrTextureParams* params,
114 const GrTextureDesc& desc,
115 const GrCacheData& cacheData,
116 void* srcData, size_t rowBytes);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000117
118 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000119 * Look for a texture that matches 'key' in the cache. If not found,
120 * return NULL.
121 */
122 GrTexture* findTexture(const GrCacheKey& key);
123
124 /**
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000125 * Search for an entry based on key and dimensions. If found,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000126 * return it. The return value will be NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000127 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000128 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000129 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.comb8670992012-07-25 21:27:09 +0000130 * @param params The tex params used to draw a texture may help determine
131 * the cache entry used. (e.g. different versions may exist
132 * for different wrap modes on GPUs with limited NPOT
133 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000134 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000135 GrTexture* findTexture(const GrTextureDesc& desc,
136 const GrCacheData& cacheData,
137 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000138 /**
139 * Determines whether a texture is in the cache. If the texture is found it
140 * will not be locked or returned. This call does not affect the priority of
141 * the texture for deletion.
142 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000143 bool isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000144 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000145 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000146
147 /**
148 * Enum that determines how closely a returned scratch texture must match
149 * a provided GrTextureDesc.
150 */
151 enum ScratchTexMatch {
152 /**
153 * Finds a texture that exactly matches the descriptor.
154 */
155 kExact_ScratchTexMatch,
156 /**
157 * Finds a texture that approximately matches the descriptor. Will be
158 * at least as large in width and height as desc specifies. If desc
159 * specifies that texture is a render target then result will be a
160 * render target. If desc specifies a render target and doesn't set the
161 * no stencil flag then result will have a stencil. Format and aa level
162 * will always match.
163 */
164 kApprox_ScratchTexMatch
165 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000166
167 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168 * Returns a texture matching the desc. It's contents are unknown. Subsequent
169 * requests with the same descriptor are not guaranteed to return the same
170 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000171 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000172 *
173 * Textures created by createAndLockTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000174 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000176 * such an API will create gaps in the tiling pattern. This includes clamp
177 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000178 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000179 GrTexture* lockScratchTexture(const GrTextureDesc& desc,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000180 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000181
182 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000183 * When done with an entry, call unlockTexture(entry) on it, which returns
184 * it to the cache, where it may be purged.
185 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000186 void unlockScratchTexture(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000187
188 /**
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000189 * This method should be called whenever a GrTexture is unreffed or
190 * switched from exclusive to non-exclusive. This
191 * gives the resource cache a chance to discard unneeded textures.
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000192 * Note: this entry point will be removed once totally ref-driven
193 * cache maintenance is implemented
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000194 */
195 void purgeCache();
196
197 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198 * Creates a texture that is outside the cache. Does not count against
199 * cache's budget.
200 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000201 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000202 void* srcData,
203 size_t rowBytes);
204
205 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000206 * Returns true if the specified use of an indexed texture is supported.
207 * Support may depend upon whether the texture params indicate that the
208 * texture will be tiled. Passing NULL for the texture params indicates
209 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000210 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000211 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000212 int width,
213 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214
215 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000216 * Return the current texture cache limits.
217 *
218 * @param maxTextures If non-null, returns maximum number of textures that
219 * can be held in the cache.
220 * @param maxTextureBytes If non-null, returns maximum number of bytes of
221 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000222 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000223 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000224
225 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000226 * Specify the texture cache limits. If the current cache exceeds either
227 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000229 * @param maxTextures The maximum number of textures that can be held in
230 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000231 * @param maxTextureBytes The maximum number of bytes of texture memory
232 * that can be held in the cache.
233 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000234 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000235
236 /**
237 * Return the max width or height of a texture supported by the current gpu
238 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000239 int getMaxTextureSize() const;
240
241 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000242 * Return the max width or height of a render target supported by the
bsalomon@google.com91958362011-06-13 17:58:13 +0000243 * current gpu
244 */
245 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246
247 ///////////////////////////////////////////////////////////////////////////
248 // Render targets
249
250 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000251 * Sets the render target.
252 * @param target the render target to set. (should not be NULL.)
253 */
254 void setRenderTarget(GrRenderTarget* target);
255
256 /**
257 * Gets the current render target.
258 * @return the currently bound render target. Should never be NULL.
259 */
260 const GrRenderTarget* getRenderTarget() const;
261 GrRenderTarget* getRenderTarget();
262
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000263 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
264
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000265 /**
266 * Can the provided configuration act as a color render target?
267 */
268 bool isConfigRenderable(GrPixelConfig config) const;
269
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000270 ///////////////////////////////////////////////////////////////////////////
271 // Platform Surfaces
272
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000273 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000274 * Wraps an existing texture with a GrTexture object.
275 *
276 * OpenGL: if the object is a texture Gr may change its GL texture params
277 * when it is drawn.
278 *
279 * @param desc description of the object to create.
280 *
281 * @return GrTexture object or NULL on failure.
282 */
283 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
284
285 /**
286 * Wraps an existing render target with a GrRenderTarget object. It is
287 * similar to createPlatformTexture but can be used to draw into surfaces
288 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
289 * the client will resolve to a texture).
290 *
291 * @param desc description of the object to create.
292 *
293 * @return GrTexture object or NULL on failure.
294 */
295 GrRenderTarget* createPlatformRenderTarget(
296 const GrPlatformRenderTargetDesc& desc);
297
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298 ///////////////////////////////////////////////////////////////////////////
299 // Matrix state
300
301 /**
302 * Gets the current transformation matrix.
303 * @return the current matrix.
304 */
305 const GrMatrix& getMatrix() const;
306
307 /**
308 * Sets the transformation matrix.
309 * @param m the matrix to set.
310 */
311 void setMatrix(const GrMatrix& m);
312
313 /**
314 * Concats the current matrix. The passed matrix is applied before the
315 * current matrix.
316 * @param m the matrix to concat.
317 */
318 void concatMatrix(const GrMatrix& m) const;
319
320
321 ///////////////////////////////////////////////////////////////////////////
322 // Clip state
323 /**
324 * Gets the current clip.
325 * @return the current clip.
326 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000327 const GrClipData* getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000328
329 /**
330 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000331 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000333 void setClip(const GrClipData* clipData);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000334
bsalomon@google.com27847de2011-02-22 20:59:41 +0000335 ///////////////////////////////////////////////////////////////////////////
336 // Draws
337
338 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000339 * Clear the entire or rect of the render target, ignoring any clips.
340 * @param rect the rect to clear or the whole thing if rect is NULL.
341 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000342 * @param target if non-NULL, the render target to clear otherwise clear
343 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000344 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000345 void clear(const GrIRect* rect, GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000346 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000347
348 /**
349 * Draw everywhere (respecting the clip) with the paint.
350 */
351 void drawPaint(const GrPaint& paint);
352
353 /**
354 * Draw the rect using a paint.
355 * @param paint describes how to color pixels.
356 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
357 * the rect is mitered stroked based on strokeWidth. If
358 * strokeWidth == 0, then the stroke is always a single
359 * pixel thick.
360 * @param matrix Optional matrix applied to the rect. Applied before
361 * context's matrix or the paint's matrix.
362 * The rects coords are used to access the paint (through texture matrix)
363 */
364 void drawRect(const GrPaint& paint,
365 const GrRect&,
366 GrScalar strokeWidth = -1,
367 const GrMatrix* matrix = NULL);
368
369 /**
370 * Maps a rect of paint coordinates onto the a rect of destination
371 * coordinates. Each rect can optionally be transformed. The srcRect
372 * is stretched over the dstRect. The dstRect is transformed by the
373 * context's matrix and the srcRect is transformed by the paint's matrix.
374 * Additional optional matrices can be provided by parameters.
375 *
376 * @param paint describes how to color pixels.
377 * @param dstRect the destination rect to draw.
378 * @param srcRect rect of paint coordinates to be mapped onto dstRect
379 * @param dstMatrix Optional matrix to transform dstRect. Applied before
380 * context's matrix.
381 * @param srcMatrix Optional matrix to transform srcRect Applied before
382 * paint's matrix.
383 */
384 void drawRectToRect(const GrPaint& paint,
385 const GrRect& dstRect,
386 const GrRect& srcRect,
387 const GrMatrix* dstMatrix = NULL,
388 const GrMatrix* srcMatrix = NULL);
389
390 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 *
393 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000394 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000395 * @param fill the path filling rule to use.
396 * @param translate optional additional translation applied to the
397 * path.
398 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000399 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000400 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000401
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 /**
403 * Draws vertices with a paint.
404 *
405 * @param paint describes how to color pixels.
406 * @param primitiveType primitives type to draw.
407 * @param vertexCount number of vertices.
408 * @param positions array of vertex positions, required.
409 * @param texCoords optional array of texture coordinates used
410 * to access the paint.
411 * @param colors optional array of per-vertex colors, supercedes
412 * the paint's color field.
413 * @param indices optional array of indices. If NULL vertices
414 * are drawn non-indexed.
415 * @param indexCount if indices is non-null then this is the
416 * number of indices.
417 */
418 void drawVertices(const GrPaint& paint,
419 GrPrimitiveType primitiveType,
420 int vertexCount,
421 const GrPoint positions[],
422 const GrPoint texs[],
423 const GrColor colors[],
424 const uint16_t indices[],
425 int indexCount);
426
bsalomon@google.com93c96602012-04-27 13:05:21 +0000427 /**
428 * Draws an oval.
429 *
430 * @param paint describes how to color pixels.
431 * @param rect the bounding rect of the oval.
432 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
433 * the rect is stroked based on strokeWidth. If
434 * strokeWidth == 0, then the stroke is always a single
435 * pixel thick.
436 */
437 void drawOval(const GrPaint& paint,
438 const GrRect& rect,
439 SkScalar strokeWidth);
440
bsalomon@google.com27847de2011-02-22 20:59:41 +0000441 ///////////////////////////////////////////////////////////////////////////
442 // Misc.
443
444 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000445 * Flags that affect flush() behavior.
446 */
447 enum FlushBits {
448 /**
449 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
450 * it can be rendered to directly. However, Gr lazily sets state. Simply
451 * calling setRenderTarget() followed by flush() without flags may not
452 * bind the render target. This flag forces the context to bind the last
453 * set render target in the 3D API.
454 */
455 kForceCurrentRenderTarget_FlushBit = 0x1,
456 /**
457 * A client may reach a point where it has partially rendered a frame
458 * through a GrContext that it knows the user will never see. This flag
459 * causes the flush to skip submission of deferred content to the 3D API
460 * during the flush.
461 */
462 kDiscard_FlushBit = 0x2,
463 };
464
465 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000466 * Call to ensure all drawing to the context has been issued to the
467 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000468 * @param flagsBitfield flags that control the flushing behavior. See
469 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000470 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000471 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000472
bsalomon@google.com0342a852012-08-20 19:22:38 +0000473 /**
474 * These flags can be used with the read/write pixels functions below.
475 */
476 enum PixelOpsFlags {
477 /** The GrContext will not be flushed. This means that the read or write may occur before
478 previous draws have executed. */
479 kDontFlush_PixelOpsFlag = 0x1,
480 /** The src for write or dst read is unpremultiplied. This is only respected if both the
481 config src and dst configs are an RGBA/BGRA 8888 format. */
482 kUnpremul_PixelOpsFlag = 0x2,
483 };
484
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000486 * Reads a rectangle of pixels from a render target.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000487 * @param target the render target to read from. NULL means the current render target.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000488 * @param left left edge of the rectangle to read (inclusive)
489 * @param top top edge of the rectangle to read (inclusive)
490 * @param width width of rectangle to read in pixels.
491 * @param height height of rectangle to read in pixels.
492 * @param config the pixel config of the destination buffer
493 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000494 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
495 * packed.
496 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000498 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
499 * pixel config or because no render target is currently set and NULL was passed for
500 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000501 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000502 bool readRenderTargetPixels(GrRenderTarget* target,
503 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000504 GrPixelConfig config, void* buffer,
505 size_t rowBytes = 0,
506 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000507
508 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000509 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target at the specified
510 * rectangle.
511 * @param target the render target to write into. NULL means the current render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000512 * @param left left edge of the rectangle to write (inclusive)
513 * @param top top edge of the rectangle to write (inclusive)
514 * @param width width of rectangle to write in pixels.
515 * @param height height of rectangle to write in pixels.
516 * @param config the pixel config of the source buffer
517 * @param buffer memory to read the rectangle from.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000518 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
519 * packed.
520 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000521 */
522 void writeRenderTargetPixels(GrRenderTarget* target,
523 int left, int top, int width, int height,
524 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000525 size_t rowBytes = 0,
526 uint32_t pixelOpsFlags = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000527
528 /**
529 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000530 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000531 * @param left left edge of the rectangle to read (inclusive)
532 * @param top top edge of the rectangle to read (inclusive)
533 * @param width width of rectangle to read in pixels.
534 * @param height height of rectangle to read in pixels.
535 * @param config the pixel config of the destination buffer
536 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000537 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
538 * packed.
539 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000540 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000541 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
542 * pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000545 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000546 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000547 size_t rowBytes = 0,
548 uint32_t pixelOpsFlags = 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549
550 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000551 * Writes a rectangle of pixels to a texture.
552 * @param texture the render target to read from.
553 * @param left left edge of the rectangle to write (inclusive)
554 * @param top top edge of the rectangle to write (inclusive)
555 * @param width width of rectangle to write in pixels.
556 * @param height height of rectangle to write in pixels.
557 * @param config the pixel config of the source buffer
558 * @param buffer memory to read pixels from
559 * @param rowBytes number of bytes bewtween consecutive rows. Zero
560 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000561 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000562 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000563 void writeTexturePixels(GrTexture* texture,
564 int left, int top, int width, int height,
565 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000566 size_t rowBytes,
567 uint32_t pixelOpsFlags = 0);
568
569
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000570 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000571 * Copies all texels from one texture to another.
572 * @param src the texture to copy from.
573 * @param dst the render target to copy to.
574 */
575 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000576
577 /**
578 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
579 * downsampled to the associated GrTexture (accessible via
580 * GrRenderTarget::asTexture()). Any pending draws to the render target will
581 * be executed before the resolve.
582 *
583 * This is only necessary when a client wants to access the object directly
584 * using the underlying graphics API. GrContext will detect when it must
585 * perform a resolve to a GrTexture used as the source of a draw or before
586 * reading pixels back from a GrTexture or GrRenderTarget.
587 */
588 void resolveRenderTarget(GrRenderTarget* target);
589
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000590 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000591 * Applies a 2D Gaussian blur to a given texture.
592 * @param srcTexture The source texture to be blurred.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000593 * @param canClobberSrc If true, srcTexture may be overwritten, and
594 * may be returned as the result.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000595 * @param rect The destination rectangle.
596 * @param sigmaX The blur's standard deviation in X.
597 * @param sigmaY The blur's standard deviation in Y.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000598 * @return the blurred texture, which may be srcTexture ref'ed, or a
599 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000600 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000601 GrTexture* gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000602 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000603 const SkRect& rect,
604 float sigmaX, float sigmaY);
605
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000606 /**
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000607 * Zooms a subset of the texture to a larger size with a nice edge.
608 * The inner rectangle is a simple scaling of the texture by a factor of
609 * |zoom|. The outer |inset| pixels transition from the background texture
610 * to the zoomed coordinate system at a rate of
611 * (distance_to_edge / inset) ^2, producing a rounded lens effect.
612 * @param srcTexture The source texture to be zoomed.
613 * @param dstRect The destination rectangle.
614 * @param srcRect The source rectangle. Must be smaller than
615 * dstRect
616 * @param inset Number of pixels to blend along the edges.
617 * @return the zoomed texture, which is dstTexture.
618 */
619 GrTexture* zoom(GrTexture* srcTexture,
620 const SkRect& dstRect, const SkRect& srcRect, float inset);
621
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622 ///////////////////////////////////////////////////////////////////////////
623 // Helpers
624
625 class AutoRenderTarget : ::GrNoncopyable {
626 public:
627 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628 fPrevTarget = context->getRenderTarget();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000629 context->setRenderTarget(target);
630 fContext = context;
631 }
632 AutoRenderTarget(GrContext* context) {
633 fPrevTarget = context->getRenderTarget();
634 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635 }
636 ~AutoRenderTarget() {
637 if (fContext) {
638 fContext->setRenderTarget(fPrevTarget);
639 }
640 }
641 private:
642 GrContext* fContext;
643 GrRenderTarget* fPrevTarget;
644 };
645
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000646 /**
647 * Save/restore the view-matrix in the context.
648 */
649 class AutoMatrix : GrNoncopyable {
650 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000651 enum InitialMatrix {
652 kPreserve_InitialMatrix,
653 kIdentity_InitialMatrix,
654 };
655
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000656 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000657
658 AutoMatrix(GrContext* ctx, InitialMatrix initialState) : fContext(ctx) {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000659 fMatrix = ctx->getMatrix();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000660 switch (initialState) {
661 case kPreserve_InitialMatrix:
662 break;
663 case kIdentity_InitialMatrix:
664 ctx->setMatrix(GrMatrix::I());
665 break;
666 default:
667 GrCrash("Unexpected initial matrix state");
668 }
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000669 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000670
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000671 AutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
672 fMatrix = ctx->getMatrix();
673 ctx->setMatrix(matrix);
674 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000675
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000676 void set(GrContext* ctx) {
677 if (NULL != fContext) {
678 fContext->setMatrix(fMatrix);
679 }
680 fMatrix = ctx->getMatrix();
681 fContext = ctx;
682 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000683
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000684 void set(GrContext* ctx, const GrMatrix& matrix) {
685 if (NULL != fContext) {
686 fContext->setMatrix(fMatrix);
687 }
688 fMatrix = ctx->getMatrix();
689 ctx->setMatrix(matrix);
690 fContext = ctx;
691 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000692
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000693 ~AutoMatrix() {
694 if (NULL != fContext) {
695 fContext->setMatrix(fMatrix);
696 }
697 }
698
699 private:
700 GrContext* fContext;
701 GrMatrix fMatrix;
702 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000703
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000704 class AutoClip : GrNoncopyable {
705 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000706 // This enum exists to require a caller of the constructor to acknowledge that the clip will
707 // initially be wide open. It also could be extended if there are other desirable initial
708 // clip states.
709 enum InitialClip {
710 kWideOpen_InitialClip,
711 };
712
713 AutoClip(GrContext* context, InitialClip initialState) {
714 GrAssert(kWideOpen_InitialClip == initialState);
715 fOldClip = context->getClip();
716 fNewClipData.fClipStack = &fNewClipStack;
717 context->setClip(&fNewClipData);
718 fContext = context;
719 }
720
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000721 AutoClip(GrContext* context, const GrRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000722 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000723 , fNewClipStack(newClipRect) {
724 fNewClipData.fClipStack = &fNewClipStack;
725
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000726 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000727 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000728 }
729
730 ~AutoClip() {
731 if (NULL != fContext) {
732 fContext->setClip(fOldClip);
733 }
734 }
735 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000736 GrContext* fContext;
737 const GrClipData* fOldClip;
738
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000739 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000740 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000741 };
742
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000743 class AutoWideOpenIdentityDraw {
744 public:
745 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
746 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
747 , fAutoRT(ctx, rt)
748 , fAutoMatrix(ctx, AutoMatrix::kIdentity_InitialMatrix) {
749 }
750 private:
751 AutoClip fAutoClip;
752 AutoRenderTarget fAutoRT;
753 AutoMatrix fAutoMatrix;
754 };
755
bsalomon@google.com27847de2011-02-22 20:59:41 +0000756 ///////////////////////////////////////////////////////////////////////////
757 // Functions intended for internal use only.
758 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000759 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760 GrFontCache* getFontCache() { return fFontCache; }
761 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000763
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000764 /**
765 * Stencil buffers add themselves to the cache using
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000766 * addStencilBuffer. findStencilBuffer is called to check the
skia.committer@gmail.com6c778162012-09-06 02:01:13 +0000767 * cache for a SB that matches an RT's criteria.
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000768 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000769 void addStencilBuffer(GrStencilBuffer* sb);
770 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000771
robertphillips@google.com2c756812012-05-22 20:28:23 +0000772 GrPathRenderer* getPathRenderer(const SkPath& path,
773 GrPathFill fill,
774 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000775 bool antiAlias,
776 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000777
robertphillips@google.com59552022012-08-31 13:07:37 +0000778#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000779 void printCacheStats() const;
780#endif
781
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782private:
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000783 // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
784 enum BufferedDraw {
785 kYes_BufferedDraw,
786 kNo_BufferedDraw,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000787 };
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000788 BufferedDraw fLastDrawWasBuffered;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000790 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000791 GrDrawState* fDrawState;
792
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000793 GrResourceCache* fTextureCache;
794 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000795
bsalomon@google.com30085192011-08-19 15:42:31 +0000796 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000797 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798
799 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
800 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
801 GrInOrderDrawBuffer* fDrawBuffer;
802
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000803 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000804
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000805 bool fDidTestPMConversions;
806 int fPMToUPMConversion;
807 int fUPMToPMConversion;
808
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000809 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000810
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000811 void setupDrawBuffer();
812
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813 void flushDrawBuffer();
814
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000815 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000817 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
818 /// draw state is left unmodified.
819 GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000821 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000822 GrPathFill fill, const GrPoint* translate);
823
robertphillips@google.com3319f332012-08-13 18:00:36 +0000824 GrTexture* createResizedTexture(const GrTextureDesc& desc,
825 const GrCacheData& cacheData,
826 void* srcData,
827 size_t rowBytes,
828 bool needsFiltering);
829
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000830 // Needed so GrTexture's returnToCache helper function can call
831 // addExistingTextureToCache
832 friend class GrTexture;
833
834 // Add an existing texture to the texture cache. This is intended solely
835 // for use with textures released from an GrAutoScratchTexture.
836 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +0000837
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000838 GrCustomStage* createPMToUPMEffect(GrTexture* texture, bool swapRAndB);
839 GrCustomStage* createUPMToPMEffect(GrTexture* texture, bool swapRAndB);
840
reed@google.comfa35e3d2012-06-26 20:16:17 +0000841 typedef GrRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842};
843
844/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000845 * Gets and locks a scratch texture from a descriptor using
846 * either exact or approximate criteria. Unlocks texture in
847 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000848 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000850public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000851 GrAutoScratchTexture()
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000852 : fContext(NULL)
853 , fTexture(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000854 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000855
856 GrAutoScratchTexture(GrContext* context,
857 const GrTextureDesc& desc,
858 GrContext::ScratchTexMatch match =
859 GrContext::kApprox_ScratchTexMatch)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000860 : fContext(NULL)
861 , fTexture(NULL) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000862 this->set(context, desc, match);
863 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000864
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000865 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000866 this->reset();
867 }
868
869 void reset() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000870 if (NULL != fContext && NULL != fTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000871 fContext->unlockScratchTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000872 fTexture = NULL;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000873 }
874 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000875
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000876 /*
877 * When detaching a texture we do not unlock it in the texture cache but
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000878 * we do set the returnToCache flag. In this way the texture remains
879 * "locked" in the texture cache until it is freed and recycled in
880 * GrTexture::internal_dispose. In reality, the texture has been removed
881 * from the cache (because this is in AutoScratchTexture) and by not
882 * calling unlockTexture we simply don't re-add it. It will be reattached
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000883 * in GrTexture::internal_dispose.
884 *
885 * Note that the caller is assumed to accept and manage the ref to the
886 * returned texture.
887 */
888 GrTexture* detach() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000889 GrTexture* temp = fTexture;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000890
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000891 // Conceptually the texture's cache entry loses its ref to the
892 // texture while the caller of this method gets a ref.
893 GrAssert(NULL != temp->getCacheEntry());
894
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000895 fTexture = NULL;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000896
897 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000898 return temp;
899 }
900
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000901 GrTexture* set(GrContext* context,
902 const GrTextureDesc& desc,
903 GrContext::ScratchTexMatch match =
904 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000905 this->reset();
906
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000907 fContext = context;
908 if (NULL != fContext) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000909 fTexture = fContext->lockScratchTexture(desc, match);
910 if (NULL == fTexture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000911 fContext = NULL;
912 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000913 return fTexture;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000914 } else {
915 return NULL;
916 }
917 }
918
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000919 GrTexture* texture() { return fTexture; }
920
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000921private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000922 GrContext* fContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000923 GrTexture* fTexture;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000924};
925
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926#endif