blob: 8cefa77dec6cf6717e5bd9126acf2cb9cd13add5 [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
bsalomon@google.com27847de2011-02-22 20:59:41 +000013#include "GrPaint.h"
robertphillips@google.comf6747b02012-06-12 00:32:28 +000014#include "GrAARectRenderer.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000015#include "GrClipData.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000016// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
17// remove.
18#include "GrRenderTarget.h"
robertphillips@google.coma2d71482012-08-01 20:08:47 +000019#include "SkClipStack.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000021class GrAutoScratchTexture;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000022class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000023class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000024class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000025class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000026class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000027class GrIndexBufferAllocPool;
28class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000029class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000030class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000031class GrResourceEntry;
32class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000033class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000034class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000036class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037
bsalomon@google.com91826102011-03-21 19:51:57 +000038class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000039public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000040 SK_DECLARE_INST_COUNT(GrContext)
41
bsalomon@google.com27847de2011-02-22 20:59:41 +000042 /**
43 * Creates a GrContext from within a 3D context.
44 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000045 static GrContext* Create(GrEngine engine,
46 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000047
bsalomon@google.comc0af3172012-06-15 14:10:09 +000048 /**
49 * Returns the number of GrContext instances for the current thread.
50 */
51 static int GetThreadInstanceCount();
52
bsalomon@google.com27847de2011-02-22 20:59:41 +000053 virtual ~GrContext();
54
55 /**
56 * The GrContext normally assumes that no outsider is setting state
57 * within the underlying 3D API's context/device/whatever. This call informs
58 * the context that the state was modified and it should resend. Shouldn't
59 * be called frequently for good performance.
60 */
61 void resetContext();
62
bsalomon@google.com8fe72472011-03-30 21:26:44 +000063 /**
64 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
65 * if you have lost the associated GPU context, and thus internal texture,
66 * buffer, etc. references/IDs are now invalid. Should be called even when
67 * GrContext is no longer going to be used for two reasons:
68 * 1) ~GrContext will not try to free the objects in the 3D API.
69 * 2) If you've created GrResources that outlive the GrContext they will
70 * be marked as invalid (GrResource::isValid()) and won't attempt to
71 * free their underlying resource in the 3D API.
72 * Content drawn since the last GrContext::flush() may be lost.
73 */
74 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000075
76 /**
junov@google.com53a55842011-06-08 22:55:10 +000077 * Similar to contextLost, but makes no attempt to reset state.
78 * Use this method when GrContext destruction is pending, but
79 * the graphics context is destroyed first.
80 */
81 void contextDestroyed();
82
83 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 * Frees gpu created by the context. Can be called to reduce GPU memory
85 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000086 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000087 void freeGpuResources();
88
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000089 /**
90 * Returns the number of bytes of GPU memory hosted by the texture cache.
91 */
92 size_t getGpuTextureCacheBytes() const;
93
bsalomon@google.com8fe72472011-03-30 21:26:44 +000094 ///////////////////////////////////////////////////////////////////////////
95 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000096
97 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000098 * Token that refers to an entry in the texture cache. Returned by
99 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +0000101 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000102 public:
103 TextureCacheEntry() : fEntry(NULL) {}
104 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
105 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
106 fEntry = e.fEntry;
107 return *this;
108 }
109 GrTexture* texture() const;
110 void reset() { fEntry = NULL; }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000111 GrResourceEntry* cacheEntry() { return fEntry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000112 private:
113 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
114 void set(GrResourceEntry* entry) { fEntry = entry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000115 GrResourceEntry* fEntry;
116 friend class GrContext;
117 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000118
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000119 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000120 * Create a new entry, based on the specified key and texture, and return
121 * its "locked" entry. Must call be balanced with an unlockTexture() call.
122 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000123 * @param params The tex params used to draw a texture may help determine
124 * the cache entry used. (e.g. different versions may exist
125 * for different wrap modes on GPUs with limited NPOT
126 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000127 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000128 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000129 * @param srcData Pointer to the pixel values.
130 * @param rowBytes The number of bytes between rows of the texture. Zero
131 * implies tightly packed rows.
132 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000133 TextureCacheEntry createAndLockTexture(const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000134 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000135 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000136 void* srcData, size_t rowBytes);
137
138 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000139 * Search for an entry based on key and dimensions. If found, "lock" it and
140 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000141 * Must be balanced with an unlockTexture() call.
142 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000143 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000144 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.comb8670992012-07-25 21:27:09 +0000145 * @param params The tex params used to draw a texture may help determine
146 * the cache entry used. (e.g. different versions may exist
147 * for different wrap modes on GPUs with limited NPOT
148 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000149 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000150 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000151 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000152 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000153 /**
154 * Determines whether a texture is in the cache. If the texture is found it
155 * will not be locked or returned. This call does not affect the priority of
156 * the texture for deletion.
157 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000158 bool isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000159 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000160 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000161
162 /**
163 * Enum that determines how closely a returned scratch texture must match
164 * a provided GrTextureDesc.
165 */
166 enum ScratchTexMatch {
167 /**
168 * Finds a texture that exactly matches the descriptor.
169 */
170 kExact_ScratchTexMatch,
171 /**
172 * Finds a texture that approximately matches the descriptor. Will be
173 * at least as large in width and height as desc specifies. If desc
174 * specifies that texture is a render target then result will be a
175 * render target. If desc specifies a render target and doesn't set the
176 * no stencil flag then result will have a stencil. Format and aa level
177 * will always match.
178 */
179 kApprox_ScratchTexMatch
180 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000181
182 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000183 * Returns a texture matching the desc. It's contents are unknown. Subsequent
184 * requests with the same descriptor are not guaranteed to return the same
185 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000186 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000187 *
188 * Textures created by createAndLockTexture() hide the complications of
189 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000190 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000191 * such an API will create gaps in the tiling pattern. This includes clamp
192 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000193 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000194 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
195 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000196
197 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198 * When done with an entry, call unlockTexture(entry) on it, which returns
199 * it to the cache, where it may be purged.
200 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000201 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000202
203 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000204 * Free any data associated with the provided entry in the texture cache
205 */
206 void freeEntry(TextureCacheEntry entry);
207
208 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000209 * Creates a texture that is outside the cache. Does not count against
210 * cache's budget.
211 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000212 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213 void* srcData,
214 size_t rowBytes);
215
216 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000217 * Returns true if the specified use of an indexed texture is supported.
218 * Support may depend upon whether the texture params indicate that the
219 * texture will be tiled. Passing NULL for the texture params indicates
220 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000221 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000222 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000223 int width,
224 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000225
226 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000227 * Return the current texture cache limits.
228 *
229 * @param maxTextures If non-null, returns maximum number of textures that
230 * can be held in the cache.
231 * @param maxTextureBytes If non-null, returns maximum number of bytes of
232 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000233 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000234 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000235
236 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000237 * Specify the texture cache limits. If the current cache exceeds either
238 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000240 * @param maxTextures The maximum number of textures that can be held in
241 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000242 * @param maxTextureBytes The maximum number of bytes of texture memory
243 * that can be held in the cache.
244 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000245 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246
247 /**
248 * Return the max width or height of a texture supported by the current gpu
249 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000250 int getMaxTextureSize() const;
251
252 /**
253 * Return the max width or height of a render target supported by the
254 * current gpu
255 */
256 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257
258 ///////////////////////////////////////////////////////////////////////////
259 // Render targets
260
261 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000262 * Sets the render target.
263 * @param target the render target to set. (should not be NULL.)
264 */
265 void setRenderTarget(GrRenderTarget* target);
266
267 /**
268 * Gets the current render target.
269 * @return the currently bound render target. Should never be NULL.
270 */
271 const GrRenderTarget* getRenderTarget() const;
272 GrRenderTarget* getRenderTarget();
273
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000274 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
275
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000276 /**
277 * Can the provided configuration act as a color render target?
278 */
279 bool isConfigRenderable(GrPixelConfig config) const;
280
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000281 ///////////////////////////////////////////////////////////////////////////
282 // Platform Surfaces
283
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000284 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000285 * Wraps an existing texture with a GrTexture object.
286 *
287 * OpenGL: if the object is a texture Gr may change its GL texture params
288 * when it is drawn.
289 *
290 * @param desc description of the object to create.
291 *
292 * @return GrTexture object or NULL on failure.
293 */
294 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
295
296 /**
297 * Wraps an existing render target with a GrRenderTarget object. It is
298 * similar to createPlatformTexture but can be used to draw into surfaces
299 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
300 * the client will resolve to a texture).
301 *
302 * @param desc description of the object to create.
303 *
304 * @return GrTexture object or NULL on failure.
305 */
306 GrRenderTarget* createPlatformRenderTarget(
307 const GrPlatformRenderTargetDesc& desc);
308
bsalomon@google.com27847de2011-02-22 20:59:41 +0000309 ///////////////////////////////////////////////////////////////////////////
310 // Matrix state
311
312 /**
313 * Gets the current transformation matrix.
314 * @return the current matrix.
315 */
316 const GrMatrix& getMatrix() const;
317
318 /**
319 * Sets the transformation matrix.
320 * @param m the matrix to set.
321 */
322 void setMatrix(const GrMatrix& m);
323
324 /**
325 * Concats the current matrix. The passed matrix is applied before the
326 * current matrix.
327 * @param m the matrix to concat.
328 */
329 void concatMatrix(const GrMatrix& m) const;
330
331
332 ///////////////////////////////////////////////////////////////////////////
333 // Clip state
334 /**
335 * Gets the current clip.
336 * @return the current clip.
337 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000338 const GrClipData* getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000339
340 /**
341 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000342 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000343 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000344 void setClip(const GrClipData* clipData);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000345
bsalomon@google.com27847de2011-02-22 20:59:41 +0000346 ///////////////////////////////////////////////////////////////////////////
347 // Draws
348
349 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000350 * Clear the entire or rect of the render target, ignoring any clips.
351 * @param rect the rect to clear or the whole thing if rect is NULL.
352 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000353 * @param target if non-NULL, the render target to clear otherwise clear
354 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000356 void clear(const GrIRect* rect, GrColor color,
357 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000358
359 /**
360 * Draw everywhere (respecting the clip) with the paint.
361 */
362 void drawPaint(const GrPaint& paint);
363
364 /**
365 * Draw the rect using a paint.
366 * @param paint describes how to color pixels.
367 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
368 * the rect is mitered stroked based on strokeWidth. If
369 * strokeWidth == 0, then the stroke is always a single
370 * pixel thick.
371 * @param matrix Optional matrix applied to the rect. Applied before
372 * context's matrix or the paint's matrix.
373 * The rects coords are used to access the paint (through texture matrix)
374 */
375 void drawRect(const GrPaint& paint,
376 const GrRect&,
377 GrScalar strokeWidth = -1,
378 const GrMatrix* matrix = NULL);
379
380 /**
381 * Maps a rect of paint coordinates onto the a rect of destination
382 * coordinates. Each rect can optionally be transformed. The srcRect
383 * is stretched over the dstRect. The dstRect is transformed by the
384 * context's matrix and the srcRect is transformed by the paint's matrix.
385 * Additional optional matrices can be provided by parameters.
386 *
387 * @param paint describes how to color pixels.
388 * @param dstRect the destination rect to draw.
389 * @param srcRect rect of paint coordinates to be mapped onto dstRect
390 * @param dstMatrix Optional matrix to transform dstRect. Applied before
391 * context's matrix.
392 * @param srcMatrix Optional matrix to transform srcRect Applied before
393 * paint's matrix.
394 */
395 void drawRectToRect(const GrPaint& paint,
396 const GrRect& dstRect,
397 const GrRect& srcRect,
398 const GrMatrix* dstMatrix = NULL,
399 const GrMatrix* srcMatrix = NULL);
400
401 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000402 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403 *
404 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000405 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000406 * @param fill the path filling rule to use.
407 * @param translate optional additional translation applied to the
408 * path.
409 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000410 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000412
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 /**
414 * Draws vertices with a paint.
415 *
416 * @param paint describes how to color pixels.
417 * @param primitiveType primitives type to draw.
418 * @param vertexCount number of vertices.
419 * @param positions array of vertex positions, required.
420 * @param texCoords optional array of texture coordinates used
421 * to access the paint.
422 * @param colors optional array of per-vertex colors, supercedes
423 * the paint's color field.
424 * @param indices optional array of indices. If NULL vertices
425 * are drawn non-indexed.
426 * @param indexCount if indices is non-null then this is the
427 * number of indices.
428 */
429 void drawVertices(const GrPaint& paint,
430 GrPrimitiveType primitiveType,
431 int vertexCount,
432 const GrPoint positions[],
433 const GrPoint texs[],
434 const GrColor colors[],
435 const uint16_t indices[],
436 int indexCount);
437
bsalomon@google.com93c96602012-04-27 13:05:21 +0000438 /**
439 * Draws an oval.
440 *
441 * @param paint describes how to color pixels.
442 * @param rect the bounding rect of the oval.
443 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
444 * the rect is stroked based on strokeWidth. If
445 * strokeWidth == 0, then the stroke is always a single
446 * pixel thick.
447 */
448 void drawOval(const GrPaint& paint,
449 const GrRect& rect,
450 SkScalar strokeWidth);
451
bsalomon@google.com27847de2011-02-22 20:59:41 +0000452 ///////////////////////////////////////////////////////////////////////////
453 // Misc.
454
455 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000456 * Flags that affect flush() behavior.
457 */
458 enum FlushBits {
459 /**
460 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
461 * it can be rendered to directly. However, Gr lazily sets state. Simply
462 * calling setRenderTarget() followed by flush() without flags may not
463 * bind the render target. This flag forces the context to bind the last
464 * set render target in the 3D API.
465 */
466 kForceCurrentRenderTarget_FlushBit = 0x1,
467 /**
468 * A client may reach a point where it has partially rendered a frame
469 * through a GrContext that it knows the user will never see. This flag
470 * causes the flush to skip submission of deferred content to the 3D API
471 * during the flush.
472 */
473 kDiscard_FlushBit = 0x2,
474 };
475
476 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000477 * Call to ensure all drawing to the context has been issued to the
478 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000479 * @param flagsBitfield flags that control the flushing behavior. See
480 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000481 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000482 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483
bsalomon@google.com27847de2011-02-22 20:59:41 +0000484 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000485 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000486 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000487 * current render target.
488 * @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.com6f379512011-11-16 20:36:03 +0000494 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000495 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000496 *
497 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000499 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000500 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000501 bool readRenderTargetPixels(GrRenderTarget* target,
502 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000503 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000504 size_t rowBytes) {
505 return this->internalReadRenderTargetPixels(target, left, top,
506 width, height,
507 config, buffer,
508 rowBytes, 0);
509 }
510
511 /**
512 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
513 * at the specified rectangle.
514 * @param target the render target to write into. NULL means the
515 * current render target.
516 * @param left left edge of the rectangle to write (inclusive)
517 * @param top top edge of the rectangle to write (inclusive)
518 * @param width width of rectangle to write in pixels.
519 * @param height height of rectangle to write in pixels.
520 * @param config the pixel config of the source buffer
521 * @param buffer memory to read the rectangle from.
522 * @param rowBytes number of bytes bewtween consecutive rows. Zero
523 * means rows are tightly packed.
524 */
525 void writeRenderTargetPixels(GrRenderTarget* target,
526 int left, int top, int width, int height,
527 GrPixelConfig config, const void* buffer,
528 size_t rowBytes) {
529 this->internalWriteRenderTargetPixels(target, left, top, width, height,
530 config, buffer, rowBytes, 0);
531 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000532
533 /**
534 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000535 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000536 * @param left left edge of the rectangle to read (inclusive)
537 * @param top top edge of the rectangle to read (inclusive)
538 * @param width width of rectangle to read in pixels.
539 * @param height height of rectangle to read in pixels.
540 * @param config the pixel config of the destination buffer
541 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 * @param rowBytes number of bytes bewtween consecutive rows. Zero
543 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000544 *
545 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000546 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000547 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000548 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000550 GrPixelConfig config, void* buffer,
551 size_t rowBytes) {
552 return this->internalReadTexturePixels(texture, left, top,
553 width, height,
554 config, buffer, rowBytes, 0);
555 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556
557 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558 * Writes a rectangle of pixels to a texture.
559 * @param texture the render target to read from.
560 * @param left left edge of the rectangle to write (inclusive)
561 * @param top top edge of the rectangle to write (inclusive)
562 * @param width width of rectangle to write in pixels.
563 * @param height height of rectangle to write in pixels.
564 * @param config the pixel config of the source buffer
565 * @param buffer memory to read pixels from
566 * @param rowBytes number of bytes bewtween consecutive rows. Zero
567 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000569 void writeTexturePixels(GrTexture* texture,
570 int left, int top, int width, int height,
571 GrPixelConfig config, const void* buffer,
572 size_t rowBytes) {
573 this->internalWriteTexturePixels(texture, left, top, width, height,
574 config, buffer, rowBytes, 0);
575 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000576 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000577 * Copies all texels from one texture to another.
578 * @param src the texture to copy from.
579 * @param dst the render target to copy to.
580 */
581 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000582
583 /**
584 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
585 * downsampled to the associated GrTexture (accessible via
586 * GrRenderTarget::asTexture()). Any pending draws to the render target will
587 * be executed before the resolve.
588 *
589 * This is only necessary when a client wants to access the object directly
590 * using the underlying graphics API. GrContext will detect when it must
591 * perform a resolve to a GrTexture used as the source of a draw or before
592 * reading pixels back from a GrTexture or GrRenderTarget.
593 */
594 void resolveRenderTarget(GrRenderTarget* target);
595
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000596 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000597 * Applies a 2D Gaussian blur to a given texture.
598 * @param srcTexture The source texture to be blurred.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000599 * @param canClobberSrc If true, srcTexture may be overwritten, and
600 * may be returned as the result.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000601 * @param rect The destination rectangle.
602 * @param sigmaX The blur's standard deviation in X.
603 * @param sigmaY The blur's standard deviation in Y.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000604 * @return the blurred texture, which may be srcTexture ref'ed, or a
605 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000606 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000607 GrTexture* gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000608 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000609 const SkRect& rect,
610 float sigmaX, float sigmaY);
611
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000612 /**
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000613 * Zooms a subset of the texture to a larger size with a nice edge.
614 * The inner rectangle is a simple scaling of the texture by a factor of
615 * |zoom|. The outer |inset| pixels transition from the background texture
616 * to the zoomed coordinate system at a rate of
617 * (distance_to_edge / inset) ^2, producing a rounded lens effect.
618 * @param srcTexture The source texture to be zoomed.
619 * @param dstRect The destination rectangle.
620 * @param srcRect The source rectangle. Must be smaller than
621 * dstRect
622 * @param inset Number of pixels to blend along the edges.
623 * @return the zoomed texture, which is dstTexture.
624 */
625 GrTexture* zoom(GrTexture* srcTexture,
626 const SkRect& dstRect, const SkRect& srcRect, float inset);
627
628
629 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000630 * This enum is used with the function below, applyMorphology.
631 */
632 enum MorphologyType {
633 kErode_MorphologyType,
634 kDilate_MorphologyType,
635 };
636
637 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000638 * Applies a 2D morphology to a given texture.
639 * @param srcTexture The source texture to be blurred.
640 * @param rect The destination rectangle.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000641 * @param filter The morphology filter. Must be kDilate_Filter or
642 * kErode_Filter.
643 * @param radius The morphology radius in X and Y. The filter is
644 * applied to a fWidth by fHeight rectangle of
645 * pixels.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000646 * @return the morphed texture, which may be srcTexture ref'ed, or a
647 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000648 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000649 GrTexture* applyMorphology(GrTexture* srcTexture,
650 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000651 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000652 SkISize radius);
653
bsalomon@google.com27847de2011-02-22 20:59:41 +0000654 ///////////////////////////////////////////////////////////////////////////
655 // Helpers
656
657 class AutoRenderTarget : ::GrNoncopyable {
658 public:
659 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
660 fContext = NULL;
661 fPrevTarget = context->getRenderTarget();
662 if (fPrevTarget != target) {
663 context->setRenderTarget(target);
664 fContext = context;
665 }
666 }
667 ~AutoRenderTarget() {
668 if (fContext) {
669 fContext->setRenderTarget(fPrevTarget);
670 }
671 }
672 private:
673 GrContext* fContext;
674 GrRenderTarget* fPrevTarget;
675 };
676
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000677 /**
678 * Save/restore the view-matrix in the context.
679 */
680 class AutoMatrix : GrNoncopyable {
681 public:
682 AutoMatrix() : fContext(NULL) {}
683 AutoMatrix(GrContext* ctx) : fContext(ctx) {
684 fMatrix = ctx->getMatrix();
685 }
686 AutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
687 fMatrix = ctx->getMatrix();
688 ctx->setMatrix(matrix);
689 }
690 void set(GrContext* ctx) {
691 if (NULL != fContext) {
692 fContext->setMatrix(fMatrix);
693 }
694 fMatrix = ctx->getMatrix();
695 fContext = ctx;
696 }
697 void set(GrContext* ctx, const GrMatrix& matrix) {
698 if (NULL != fContext) {
699 fContext->setMatrix(fMatrix);
700 }
701 fMatrix = ctx->getMatrix();
702 ctx->setMatrix(matrix);
703 fContext = ctx;
704 }
705 ~AutoMatrix() {
706 if (NULL != fContext) {
707 fContext->setMatrix(fMatrix);
708 }
709 }
710
711 private:
712 GrContext* fContext;
713 GrMatrix fMatrix;
714 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000715
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000716 class AutoClip : GrNoncopyable {
717 public:
718 AutoClip(GrContext* context, const GrRect& newClipRect)
719 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000720 , fNewClipStack(newClipRect) {
721 fNewClipData.fClipStack = &fNewClipStack;
722
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000723 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000724 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000725 }
726
727 ~AutoClip() {
728 if (NULL != fContext) {
729 fContext->setClip(fOldClip);
730 }
731 }
732 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000733 GrContext* fContext;
734 const GrClipData* fOldClip;
735
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000736 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000737 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000738 };
739
bsalomon@google.com27847de2011-02-22 20:59:41 +0000740 ///////////////////////////////////////////////////////////////////////////
741 // Functions intended for internal use only.
742 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000743 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744 GrFontCache* getFontCache() { return fFontCache; }
745 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000746 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000747
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000748 /**
749 * Stencil buffers add themselves to the cache using
750 * addAndLockStencilBuffer. When a SB's RT-attachment count
751 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
752 * eligible for purging. findStencilBuffer is called to check the cache for
753 * a SB that matching an RT's criteria. If a match is found that has been
754 * unlocked (its attachment count has reached 0) then it will be relocked.
755 */
756 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
757 void unlockStencilBuffer(GrResourceEntry* sbEntry);
758 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000759
robertphillips@google.com2c756812012-05-22 20:28:23 +0000760 GrPathRenderer* getPathRenderer(const SkPath& path,
761 GrPathFill fill,
762 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000763 bool antiAlias,
764 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000765
bsalomon@google.com27847de2011-02-22 20:59:41 +0000766private:
767 // used to keep track of when we need to flush the draw buffer
768 enum DrawCategory {
769 kBuffered_DrawCategory, // last draw was inserted in draw buffer
770 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000771 };
772 DrawCategory fLastDrawCategory;
773
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000774 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000775 GrDrawState* fDrawState;
776
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000777 GrResourceCache* fTextureCache;
778 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000779
bsalomon@google.com30085192011-08-19 15:42:31 +0000780 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000781 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782
783 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
784 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
785 GrInOrderDrawBuffer* fDrawBuffer;
786
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000787 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000788
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000789 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000790
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000791 void setupDrawBuffer();
792
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 void flushDrawBuffer();
794
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000795 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
798
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000799 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000800 GrPathFill fill, const GrPoint* translate);
801
bsalomon@google.com6f379512011-11-16 20:36:03 +0000802 /**
803 * Flags to the internal read/write pixels funcs
804 */
805 enum PixelOpsFlags {
806 kDontFlush_PixelOpsFlag = 0x1,
807 };
808
robertphillips@google.com3319f332012-08-13 18:00:36 +0000809 GrTexture* createResizedTexture(const GrTextureDesc& desc,
810 const GrCacheData& cacheData,
811 void* srcData,
812 size_t rowBytes,
813 bool needsFiltering);
814
bsalomon@google.com6f379512011-11-16 20:36:03 +0000815 bool internalReadRenderTargetPixels(GrRenderTarget* target,
816 int left, int top,
817 int width, int height,
818 GrPixelConfig config, void* buffer,
819 size_t rowBytes, uint32_t flags);
820
821 void internalWriteRenderTargetPixels(GrRenderTarget* target,
822 int left, int top,
823 int width, int height,
824 GrPixelConfig, const void* buffer,
825 size_t rowBytes, uint32_t flags);
826
827 bool internalReadTexturePixels(GrTexture* texture,
828 int left, int top,
829 int width, int height,
830 GrPixelConfig config, void* buffer,
831 size_t rowBytes, uint32_t flags);
832
833 void internalWriteTexturePixels(GrTexture* texture,
834 int left, int top,
835 int width, int height,
836 GrPixelConfig config, const void* buffer,
837 size_t rowBytes, uint32_t flags);
838 // needed for access to internalWriteTexturePixels. TODO: make GrContext
839 // be a facade for an internal class. Then functions that are public on the
840 // internal class would have only be callable in src/gpu. The facade would
841 // only have to functions necessary for clients.
842 friend class GrAtlas;
843
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000844 // Needed so GrTexture's returnToCache helper function can call
845 // addExistingTextureToCache
846 friend class GrTexture;
847
848 // Add an existing texture to the texture cache. This is intended solely
849 // for use with textures released from an GrAutoScratchTexture.
850 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +0000851
852 typedef GrRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853};
854
855/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000856 * Gets and locks a scratch texture from a descriptor using
857 * either exact or approximate criteria. Unlocks texture in
858 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000859 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000860class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000861public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000862 GrAutoScratchTexture()
863 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000864 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000865
866 GrAutoScratchTexture(GrContext* context,
867 const GrTextureDesc& desc,
868 GrContext::ScratchTexMatch match =
869 GrContext::kApprox_ScratchTexMatch)
870 : fContext(NULL) {
871 this->set(context, desc, match);
872 }
873
874 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000875 this->reset();
876 }
877
878 void reset() {
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000879 if (NULL != fContext && NULL != fEntry.cacheEntry()) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000880 fContext->unlockTexture(fEntry);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000881 fEntry.reset();
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000882 }
883 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000884
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000885 /*
886 * When detaching a texture we do not unlock it in the texture cache but
887 * we do set the returnToCache flag. In this way the texture remains
888 * "locked" in the texture cache until it is freed and recycled in
889 * GrTexture::internal_dispose. In reality, the texture has been removed
890 * from the cache (because this is in AutoScratchTexture) and by not
891 * calling unlockTexture we simply don't re-add it. It will be reattached
892 * in GrTexture::internal_dispose.
893 *
894 * Note that the caller is assumed to accept and manage the ref to the
895 * returned texture.
896 */
897 GrTexture* detach() {
898 GrTexture* temp = this->texture();
899
900 GrAssert(1 == temp->getRefCnt());
901
902 // freeEntry will remove the texture cache's ref
903 temp->ref();
904 fContext->freeEntry(fEntry);
905 fEntry.reset();
906
907 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
908 GrAssert(1 == temp->getRefCnt());
909 return temp;
910 }
911
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000912 GrTexture* set(GrContext* context,
913 const GrTextureDesc& desc,
914 GrContext::ScratchTexMatch match =
915 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000916 this->reset();
917
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000918 fContext = context;
919 if (NULL != fContext) {
920 fEntry = fContext->lockScratchTexture(desc, match);
921 GrTexture* ret = fEntry.texture();
922 if (NULL == ret) {
923 fContext = NULL;
924 }
925 return ret;
926 } else {
927 return NULL;
928 }
929 }
930
931 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000932private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933 GrContext* fContext;
934 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000935};
936
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937#endif