blob: 3158a03a15af17b31c97bf0c1096422d123bc4ea [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 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000066 * Callback function to allow classes to cleanup on GrContext destruction.
67 * The 'info' field is filled in with the 'info' passed to addCleanUp.
68 */
69 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
70
71 /**
72 * Add a function to be called from within GrContext's destructor.
73 * This gives classes a chance to free resources held on a per context basis.
74 * The 'info' parameter will be stored and passed to the callback function.
75 */
76 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
77 CleanUpData* entry = fCleanUpData.push();
78
79 entry->fFunc = cleanUp;
80 entry->fInfo = info;
81 }
82
83 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
85 * if you have lost the associated GPU context, and thus internal texture,
86 * buffer, etc. references/IDs are now invalid. Should be called even when
87 * GrContext is no longer going to be used for two reasons:
88 * 1) ~GrContext will not try to free the objects in the 3D API.
89 * 2) If you've created GrResources that outlive the GrContext they will
90 * be marked as invalid (GrResource::isValid()) and won't attempt to
91 * free their underlying resource in the 3D API.
92 * Content drawn since the last GrContext::flush() may be lost.
93 */
94 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000095
96 /**
junov@google.com53a55842011-06-08 22:55:10 +000097 * Similar to contextLost, but makes no attempt to reset state.
98 * Use this method when GrContext destruction is pending, but
99 * the graphics context is destroyed first.
100 */
101 void contextDestroyed();
102
103 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 * Frees gpu created by the context. Can be called to reduce GPU memory
105 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000106 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 void freeGpuResources();
108
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000109 /**
110 * Returns the number of bytes of GPU memory hosted by the texture cache.
111 */
112 size_t getGpuTextureCacheBytes() const;
113
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114 ///////////////////////////////////////////////////////////////////////////
115 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +0000116
117 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000118 * Create a new entry, based on the specified key and texture, and return
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000119 * a "locked" texture. Must call be balanced with an unlockTexture() call.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000120 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000121 * @param params The tex params used to draw a texture may help determine
122 * the cache entry used. (e.g. different versions may exist
123 * for different wrap modes on GPUs with limited NPOT
124 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000125 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000126 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000127 * @param srcData Pointer to the pixel values.
128 * @param rowBytes The number of bytes between rows of the texture. Zero
129 * implies tightly packed rows.
130 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000131 GrTexture* createTexture(const GrTextureParams* params,
132 const GrTextureDesc& desc,
133 const GrCacheData& cacheData,
134 void* srcData, size_t rowBytes);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000135
136 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000137 * Look for a texture that matches 'key' in the cache. If not found,
138 * return NULL.
139 */
140 GrTexture* findTexture(const GrCacheKey& key);
141
142 /**
skia.committer@gmail.comd6ce0db2012-09-10 02:01:22 +0000143 * Search for an entry based on key and dimensions. If found,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000144 * return it. The return value will be NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000145 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000146 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000147 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.comb8670992012-07-25 21:27:09 +0000148 * @param params The tex params used to draw a texture may help determine
149 * the cache entry used. (e.g. different versions may exist
150 * for different wrap modes on GPUs with limited NPOT
151 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000153 GrTexture* findTexture(const GrTextureDesc& desc,
154 const GrCacheData& cacheData,
155 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000156 /**
157 * Determines whether a texture is in the cache. If the texture is found it
158 * will not be locked or returned. This call does not affect the priority of
159 * the texture for deletion.
160 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000161 bool isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000162 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000163 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000164
165 /**
166 * Enum that determines how closely a returned scratch texture must match
167 * a provided GrTextureDesc.
168 */
169 enum ScratchTexMatch {
170 /**
171 * Finds a texture that exactly matches the descriptor.
172 */
173 kExact_ScratchTexMatch,
174 /**
175 * Finds a texture that approximately matches the descriptor. Will be
176 * at least as large in width and height as desc specifies. If desc
177 * specifies that texture is a render target then result will be a
178 * render target. If desc specifies a render target and doesn't set the
179 * no stencil flag then result will have a stencil. Format and aa level
180 * will always match.
181 */
182 kApprox_ScratchTexMatch
183 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000184
185 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000186 * Returns a texture matching the desc. It's contents are unknown. Subsequent
187 * requests with the same descriptor are not guaranteed to return the same
188 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000189 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000190 *
191 * Textures created by createAndLockTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000192 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000193 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000194 * such an API will create gaps in the tiling pattern. This includes clamp
195 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000196 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197 GrTexture* lockScratchTexture(const GrTextureDesc& desc,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000198 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000199
200 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000201 * When done with an entry, call unlockTexture(entry) on it, which returns
202 * it to the cache, where it may be purged.
203 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000204 void unlockScratchTexture(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000205
206 /**
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000207 * This method should be called whenever a GrTexture is unreffed or
208 * switched from exclusive to non-exclusive. This
209 * gives the resource cache a chance to discard unneeded textures.
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000210 * Note: this entry point will be removed once totally ref-driven
211 * cache maintenance is implemented
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000212 */
213 void purgeCache();
214
215 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216 * Creates a texture that is outside the cache. Does not count against
217 * cache's budget.
218 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000219 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220 void* srcData,
221 size_t rowBytes);
222
223 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000224 * Returns true if the specified use of an indexed texture is supported.
225 * Support may depend upon whether the texture params indicate that the
226 * texture will be tiled. Passing NULL for the texture params indicates
227 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000229 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000230 int width,
231 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232
233 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000234 * Return the current texture cache limits.
235 *
236 * @param maxTextures If non-null, returns maximum number of textures that
237 * can be held in the cache.
238 * @param maxTextureBytes If non-null, returns maximum number of bytes of
239 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000240 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000241 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000242
243 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000244 * Specify the texture cache limits. If the current cache exceeds either
245 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000247 * @param maxTextures The maximum number of textures that can be held in
248 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000249 * @param maxTextureBytes The maximum number of bytes of texture memory
250 * that can be held in the cache.
251 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000252 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000253
254 /**
255 * Return the max width or height of a texture supported by the current gpu
256 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000257 int getMaxTextureSize() const;
258
259 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000260 * Return the max width or height of a render target supported by the
bsalomon@google.com91958362011-06-13 17:58:13 +0000261 * current gpu
262 */
263 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264
265 ///////////////////////////////////////////////////////////////////////////
266 // Render targets
267
268 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000269 * Sets the render target.
270 * @param target the render target to set. (should not be NULL.)
271 */
272 void setRenderTarget(GrRenderTarget* target);
273
274 /**
275 * Gets the current render target.
276 * @return the currently bound render target. Should never be NULL.
277 */
278 const GrRenderTarget* getRenderTarget() const;
279 GrRenderTarget* getRenderTarget();
280
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000281 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
282
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000283 /**
284 * Can the provided configuration act as a color render target?
285 */
286 bool isConfigRenderable(GrPixelConfig config) const;
287
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000288 ///////////////////////////////////////////////////////////////////////////
289 // Platform Surfaces
290
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000291 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000292 * Wraps an existing texture with a GrTexture object.
293 *
294 * OpenGL: if the object is a texture Gr may change its GL texture params
295 * when it is drawn.
296 *
297 * @param desc description of the object to create.
298 *
299 * @return GrTexture object or NULL on failure.
300 */
301 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
302
303 /**
304 * Wraps an existing render target with a GrRenderTarget object. It is
305 * similar to createPlatformTexture but can be used to draw into surfaces
306 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
307 * the client will resolve to a texture).
308 *
309 * @param desc description of the object to create.
310 *
311 * @return GrTexture object or NULL on failure.
312 */
313 GrRenderTarget* createPlatformRenderTarget(
314 const GrPlatformRenderTargetDesc& desc);
315
bsalomon@google.com27847de2011-02-22 20:59:41 +0000316 ///////////////////////////////////////////////////////////////////////////
317 // Matrix state
318
319 /**
320 * Gets the current transformation matrix.
321 * @return the current matrix.
322 */
323 const GrMatrix& getMatrix() const;
324
325 /**
326 * Sets the transformation matrix.
327 * @param m the matrix to set.
328 */
329 void setMatrix(const GrMatrix& m);
330
331 /**
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000332 * Sets the current transformation matrix to identity.
333 */
334 void setIdentityMatrix();
335
336 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000337 * Concats the current matrix. The passed matrix is applied before the
338 * current matrix.
339 * @param m the matrix to concat.
340 */
341 void concatMatrix(const GrMatrix& m) const;
342
343
344 ///////////////////////////////////////////////////////////////////////////
345 // Clip state
346 /**
347 * Gets the current clip.
348 * @return the current clip.
349 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000350 const GrClipData* getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351
352 /**
353 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000354 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000356 void setClip(const GrClipData* clipData);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
bsalomon@google.com27847de2011-02-22 20:59:41 +0000358 ///////////////////////////////////////////////////////////////////////////
359 // Draws
360
361 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000362 * Clear the entire or rect of the render target, ignoring any clips.
363 * @param rect the rect to clear or the whole thing if rect is NULL.
364 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000365 * @param target if non-NULL, the render target to clear otherwise clear
366 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000367 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000368 void clear(const GrIRect* rect, GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000369 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000370
371 /**
372 * Draw everywhere (respecting the clip) with the paint.
373 */
374 void drawPaint(const GrPaint& paint);
375
376 /**
377 * Draw the rect using a paint.
378 * @param paint describes how to color pixels.
379 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
380 * the rect is mitered stroked based on strokeWidth. If
381 * strokeWidth == 0, then the stroke is always a single
382 * pixel thick.
383 * @param matrix Optional matrix applied to the rect. Applied before
384 * context's matrix or the paint's matrix.
385 * The rects coords are used to access the paint (through texture matrix)
386 */
387 void drawRect(const GrPaint& paint,
388 const GrRect&,
389 GrScalar strokeWidth = -1,
390 const GrMatrix* matrix = NULL);
391
392 /**
393 * Maps a rect of paint coordinates onto the a rect of destination
394 * coordinates. Each rect can optionally be transformed. The srcRect
395 * is stretched over the dstRect. The dstRect is transformed by the
396 * context's matrix and the srcRect is transformed by the paint's matrix.
397 * Additional optional matrices can be provided by parameters.
398 *
399 * @param paint describes how to color pixels.
400 * @param dstRect the destination rect to draw.
401 * @param srcRect rect of paint coordinates to be mapped onto dstRect
402 * @param dstMatrix Optional matrix to transform dstRect. Applied before
403 * context's matrix.
404 * @param srcMatrix Optional matrix to transform srcRect Applied before
405 * paint's matrix.
406 */
407 void drawRectToRect(const GrPaint& paint,
408 const GrRect& dstRect,
409 const GrRect& srcRect,
410 const GrMatrix* dstMatrix = NULL,
411 const GrMatrix* srcMatrix = NULL);
412
413 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000414 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 *
416 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000417 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 * @param fill the path filling rule to use.
419 * @param translate optional additional translation applied to the
420 * path.
421 */
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000422 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000423
bsalomon@google.com27847de2011-02-22 20:59:41 +0000424 /**
425 * Draws vertices with a paint.
426 *
427 * @param paint describes how to color pixels.
428 * @param primitiveType primitives type to draw.
429 * @param vertexCount number of vertices.
430 * @param positions array of vertex positions, required.
431 * @param texCoords optional array of texture coordinates used
432 * to access the paint.
433 * @param colors optional array of per-vertex colors, supercedes
434 * the paint's color field.
435 * @param indices optional array of indices. If NULL vertices
436 * are drawn non-indexed.
437 * @param indexCount if indices is non-null then this is the
438 * number of indices.
439 */
440 void drawVertices(const GrPaint& paint,
441 GrPrimitiveType primitiveType,
442 int vertexCount,
443 const GrPoint positions[],
444 const GrPoint texs[],
445 const GrColor colors[],
446 const uint16_t indices[],
447 int indexCount);
448
bsalomon@google.com93c96602012-04-27 13:05:21 +0000449 /**
450 * Draws an oval.
451 *
452 * @param paint describes how to color pixels.
453 * @param rect the bounding rect of the oval.
454 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
455 * the rect is stroked based on strokeWidth. If
456 * strokeWidth == 0, then the stroke is always a single
457 * pixel thick.
458 */
459 void drawOval(const GrPaint& paint,
460 const GrRect& rect,
461 SkScalar strokeWidth);
462
bsalomon@google.com27847de2011-02-22 20:59:41 +0000463 ///////////////////////////////////////////////////////////////////////////
464 // Misc.
465
466 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000467 * Flags that affect flush() behavior.
468 */
469 enum FlushBits {
470 /**
471 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
472 * it can be rendered to directly. However, Gr lazily sets state. Simply
473 * calling setRenderTarget() followed by flush() without flags may not
474 * bind the render target. This flag forces the context to bind the last
475 * set render target in the 3D API.
476 */
477 kForceCurrentRenderTarget_FlushBit = 0x1,
478 /**
479 * A client may reach a point where it has partially rendered a frame
480 * through a GrContext that it knows the user will never see. This flag
481 * causes the flush to skip submission of deferred content to the 3D API
482 * during the flush.
483 */
484 kDiscard_FlushBit = 0x2,
485 };
486
487 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000488 * Call to ensure all drawing to the context has been issued to the
489 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000490 * @param flagsBitfield flags that control the flushing behavior. See
491 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000492 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000493 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000494
bsalomon@google.com0342a852012-08-20 19:22:38 +0000495 /**
496 * These flags can be used with the read/write pixels functions below.
497 */
498 enum PixelOpsFlags {
499 /** The GrContext will not be flushed. This means that the read or write may occur before
500 previous draws have executed. */
501 kDontFlush_PixelOpsFlag = 0x1,
502 /** The src for write or dst read is unpremultiplied. This is only respected if both the
503 config src and dst configs are an RGBA/BGRA 8888 format. */
504 kUnpremul_PixelOpsFlag = 0x2,
505 };
506
bsalomon@google.com27847de2011-02-22 20:59:41 +0000507 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000508 * Reads a rectangle of pixels from a render target.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000509 * @param target the render target to read from. NULL means the current render target.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000510 * @param left left edge of the rectangle to read (inclusive)
511 * @param top top edge of the rectangle to read (inclusive)
512 * @param width width of rectangle to read in pixels.
513 * @param height height of rectangle to read in pixels.
514 * @param config the pixel config of the destination buffer
515 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000516 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
517 * packed.
518 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000519 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000520 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
521 * pixel config or because no render target is currently set and NULL was passed for
522 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000524 bool readRenderTargetPixels(GrRenderTarget* target,
525 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000526 GrPixelConfig config, void* buffer,
527 size_t rowBytes = 0,
528 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529
530 /**
bsalomon@google.com0342a852012-08-20 19:22:38 +0000531 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target at the specified
532 * rectangle.
533 * @param target the render target to write into. NULL means the current render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 * @param left left edge of the rectangle to write (inclusive)
535 * @param top top edge of the rectangle to write (inclusive)
536 * @param width width of rectangle to write in pixels.
537 * @param height height of rectangle to write in pixels.
538 * @param config the pixel config of the source buffer
539 * @param buffer memory to read the rectangle from.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000540 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000541 * packed.
542 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 */
544 void writeRenderTargetPixels(GrRenderTarget* target,
545 int left, int top, int width, int height,
546 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000547 size_t rowBytes = 0,
548 uint32_t pixelOpsFlags = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549
550 /**
551 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000552 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000553 * @param left left edge of the rectangle to read (inclusive)
554 * @param top top edge of the rectangle to read (inclusive)
555 * @param width width of rectangle to read in pixels.
556 * @param height height of rectangle to read in pixels.
557 * @param config the pixel config of the destination buffer
558 * @param buffer memory to read the rectangle into.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000559 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000560 * packed.
561 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000562 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000563 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
564 * pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000565 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000566 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000567 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000568 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000569 size_t rowBytes = 0,
570 uint32_t pixelOpsFlags = 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571
572 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000573 * Writes a rectangle of pixels to a texture.
574 * @param texture the render target to read from.
575 * @param left left edge of the rectangle to write (inclusive)
576 * @param top top edge of the rectangle to write (inclusive)
577 * @param width width of rectangle to write in pixels.
578 * @param height height of rectangle to write in pixels.
579 * @param config the pixel config of the source buffer
580 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000581 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000582 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000583 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000585 void writeTexturePixels(GrTexture* texture,
586 int left, int top, int width, int height,
587 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000588 size_t rowBytes,
589 uint32_t pixelOpsFlags = 0);
590
591
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000592 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000593 * Copies all texels from one texture to another.
594 * @param src the texture to copy from.
595 * @param dst the render target to copy to.
596 */
597 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000598
599 /**
600 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
601 * downsampled to the associated GrTexture (accessible via
602 * GrRenderTarget::asTexture()). Any pending draws to the render target will
603 * be executed before the resolve.
604 *
605 * This is only necessary when a client wants to access the object directly
606 * using the underlying graphics API. GrContext will detect when it must
607 * perform a resolve to a GrTexture used as the source of a draw or before
608 * reading pixels back from a GrTexture or GrRenderTarget.
609 */
610 void resolveRenderTarget(GrRenderTarget* target);
611
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000612 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000613 * Applies a 2D Gaussian blur to a given texture.
614 * @param srcTexture The source texture to be blurred.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000615 * @param canClobberSrc If true, srcTexture may be overwritten, and
616 * may be returned as the result.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000617 * @param rect The destination rectangle.
618 * @param sigmaX The blur's standard deviation in X.
619 * @param sigmaY The blur's standard deviation in Y.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000620 * @return the blurred texture, which may be srcTexture ref'ed, or a
621 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000622 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000623 GrTexture* gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000624 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000625 const SkRect& rect,
626 float sigmaX, float sigmaY);
627
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000628 /**
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000629 * Zooms a subset of the texture to a larger size with a nice edge.
630 * The inner rectangle is a simple scaling of the texture by a factor of
631 * |zoom|. The outer |inset| pixels transition from the background texture
632 * to the zoomed coordinate system at a rate of
633 * (distance_to_edge / inset) ^2, producing a rounded lens effect.
634 * @param srcTexture The source texture to be zoomed.
635 * @param dstRect The destination rectangle.
636 * @param srcRect The source rectangle. Must be smaller than
637 * dstRect
638 * @param inset Number of pixels to blend along the edges.
639 * @return the zoomed texture, which is dstTexture.
640 */
641 GrTexture* zoom(GrTexture* srcTexture,
642 const SkRect& dstRect, const SkRect& srcRect, float inset);
643
bsalomon@google.com27847de2011-02-22 20:59:41 +0000644 ///////////////////////////////////////////////////////////////////////////
645 // Helpers
646
robertphillips@google.comccb39502012-10-01 18:25:13 +0000647 class AutoRenderTarget : public ::GrNoncopyable {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648 public:
649 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650 fPrevTarget = context->getRenderTarget();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000651 GrSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000652 context->setRenderTarget(target);
653 fContext = context;
654 }
655 AutoRenderTarget(GrContext* context) {
656 fPrevTarget = context->getRenderTarget();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000657 GrSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000658 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659 }
660 ~AutoRenderTarget() {
robertphillips@google.comccb39502012-10-01 18:25:13 +0000661 if (NULL != fContext) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000662 fContext->setRenderTarget(fPrevTarget);
663 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000664 GrSafeUnref(fPrevTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665 }
666 private:
667 GrContext* fContext;
668 GrRenderTarget* fPrevTarget;
669 };
670
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000671 /**
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000672 * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
673 * for a coordinate system change. Here is an example of how the paint param can be used:
674 *
675 * A GrPaint is setup with custom stages. The stages will have access to the pre-matrix source
676 * geometry positions when the draw is executed. Later on a decision is made to transform the
677 * geometry to device space on the CPU. The custom stages now need to know that the space in
678 * which the geometry will be specified has changed.
679 *
680 * Note that when restore is called (or in the destructor) the context's matrix will be
681 * restored. However, the paint will not be restored. The caller must make a copy of the
682 * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
683 * initialized.
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000684 */
685 class AutoMatrix : GrNoncopyable {
686 public:
687 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000688
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000689 ~AutoMatrix() { this->restore(); }
690
691 /**
692 * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
693 */
694 void setPreConcat(GrContext* context, const GrMatrix& preConcat, GrPaint* paint = NULL) {
695 GrAssert(NULL != context);
696
697 this->restore();
698
699 fContext = context;
700 fMatrix = context->getMatrix();
701 this->preConcat(preConcat, paint);
702 }
703
704 /**
705 * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
706 * update a paint but the matrix cannot be inverted.
707 */
708 bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
709 GrAssert(NULL != context);
710
711 this->restore();
712
713 if (NULL != paint) {
714 if (!paint->preConcatSamplerMatricesWithInverse(context->getMatrix())) {
715 return false;
716 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000717 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000718 fMatrix = context->getMatrix();
719 fContext = context;
720 context->setIdentityMatrix();
721 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000722 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000723
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000724 /**
725 * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
726 * required to update a paint but the matrix cannot be inverted.
727 */
728 bool set(GrContext* context, const GrMatrix& newMatrix, GrPaint* paint = NULL) {
729 if (NULL != paint) {
730 if (!this->setIdentity(context, paint)) {
731 return false;
732 }
733 this->preConcat(newMatrix, paint);
734 } else {
735 this->restore();
736 fContext = context;
737 fMatrix = context->getMatrix();
738 context->setMatrix(newMatrix);
739 }
740 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000741 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000742
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000743 /**
744 * If this has been initialized then the context's matrix will be further updated by
745 * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
746 * The paint is assumed to be relative to the context's matrix at the time this call is
747 * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
748 * performs an incremental update of the paint.
749 */
750 void preConcat(const GrMatrix& preConcat, GrPaint* paint = NULL) {
751 if (NULL != paint) {
752 paint->preConcatSamplerMatrices(preConcat);
753 }
754 fContext->concatMatrix(preConcat);
755 }
756
757 /**
758 * Returns false if never initialized or the inverse matrix was required to update a paint
759 * but the matrix could not be inverted.
760 */
761 bool succeeded() const { return NULL != fContext; }
762
763 /**
764 * If this has been initialized then the context's original matrix is restored.
765 */
766 void restore() {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000767 if (NULL != fContext) {
768 fContext->setMatrix(fMatrix);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000769 fContext = NULL;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000770 }
771 }
772
773 private:
774 GrContext* fContext;
775 GrMatrix fMatrix;
776 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000777
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000778 class AutoClip : GrNoncopyable {
779 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000780 // This enum exists to require a caller of the constructor to acknowledge that the clip will
781 // initially be wide open. It also could be extended if there are other desirable initial
782 // clip states.
783 enum InitialClip {
784 kWideOpen_InitialClip,
785 };
786
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000787 AutoClip(GrContext* context, InitialClip initialState)
robertphillips@google.comccb39502012-10-01 18:25:13 +0000788 : fContext(context) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000789 GrAssert(kWideOpen_InitialClip == initialState);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000790 fNewClipData.fClipStack = &fNewClipStack;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000791
792 fOldClip = context->getClip();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000793 context->setClip(&fNewClipData);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000794 }
795
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000796 AutoClip(GrContext* context, const GrRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000797 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000798 , fNewClipStack(newClipRect) {
799 fNewClipData.fClipStack = &fNewClipStack;
800
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000801 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000802 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000803 }
804
805 ~AutoClip() {
806 if (NULL != fContext) {
807 fContext->setClip(fOldClip);
808 }
809 }
810 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000811 GrContext* fContext;
812 const GrClipData* fOldClip;
813
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000814 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000815 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000816 };
817
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000818 class AutoWideOpenIdentityDraw {
819 public:
820 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
821 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000822 , fAutoRT(ctx, rt) {
823 fAutoMatrix.setIdentity(ctx);
824 // should never fail with no paint param.
825 GrAssert(fAutoMatrix.succeeded());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000826 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000827
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000828 private:
829 AutoClip fAutoClip;
830 AutoRenderTarget fAutoRT;
831 AutoMatrix fAutoMatrix;
832 };
833
bsalomon@google.com27847de2011-02-22 20:59:41 +0000834 ///////////////////////////////////////////////////////////////////////////
835 // Functions intended for internal use only.
836 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000837 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 GrFontCache* getFontCache() { return fFontCache; }
839 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000841
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000842 /**
843 * Stencil buffers add themselves to the cache using
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000844 * addStencilBuffer. findStencilBuffer is called to check the
skia.committer@gmail.com6c778162012-09-06 02:01:13 +0000845 * cache for a SB that matches an RT's criteria.
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000846 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000847 void addStencilBuffer(GrStencilBuffer* sb);
848 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849
robertphillips@google.com2c756812012-05-22 20:28:23 +0000850 GrPathRenderer* getPathRenderer(const SkPath& path,
851 GrPathFill fill,
852 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000853 bool antiAlias,
854 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000855
robertphillips@google.com59552022012-08-31 13:07:37 +0000856#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000857 void printCacheStats() const;
858#endif
859
bsalomon@google.com27847de2011-02-22 20:59:41 +0000860private:
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000861 // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
862 enum BufferedDraw {
863 kYes_BufferedDraw,
864 kNo_BufferedDraw,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 };
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000866 BufferedDraw fLastDrawWasBuffered;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000868 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000869 GrDrawState* fDrawState;
870
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000871 GrResourceCache* fTextureCache;
872 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000873
bsalomon@google.com30085192011-08-19 15:42:31 +0000874 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000875 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876
877 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
878 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
879 GrInOrderDrawBuffer* fDrawBuffer;
880
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000881 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000882
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000883 bool fDidTestPMConversions;
884 int fPMToUPMConversion;
885 int fUPMToPMConversion;
886
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000887 struct CleanUpData {
888 PFCleanUpFunc fFunc;
889 void* fInfo;
890 };
891
892 SkTDArray<CleanUpData> fCleanUpData;
893
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000894 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000895
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000896 void setupDrawBuffer();
897
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898 void flushDrawBuffer();
899
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000900 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
901 /// draw state is left unmodified.
902 GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000904 void internalDrawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000905
robertphillips@google.com3319f332012-08-13 18:00:36 +0000906 GrTexture* createResizedTexture(const GrTextureDesc& desc,
907 const GrCacheData& cacheData,
908 void* srcData,
909 size_t rowBytes,
910 bool needsFiltering);
911
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000912 // Needed so GrTexture's returnToCache helper function can call
913 // addExistingTextureToCache
914 friend class GrTexture;
915
916 // Add an existing texture to the texture cache. This is intended solely
917 // for use with textures released from an GrAutoScratchTexture.
918 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +0000919
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000920 GrCustomStage* createPMToUPMEffect(GrTexture* texture, bool swapRAndB);
921 GrCustomStage* createUPMToPMEffect(GrTexture* texture, bool swapRAndB);
922
reed@google.comfa35e3d2012-06-26 20:16:17 +0000923 typedef GrRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924};
925
926/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000927 * Gets and locks a scratch texture from a descriptor using
928 * either exact or approximate criteria. Unlocks texture in
929 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000930 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000931class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000932public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933 GrAutoScratchTexture()
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000934 : fContext(NULL)
935 , fTexture(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000936 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000937
938 GrAutoScratchTexture(GrContext* context,
939 const GrTextureDesc& desc,
940 GrContext::ScratchTexMatch match =
941 GrContext::kApprox_ScratchTexMatch)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000942 : fContext(NULL)
943 , fTexture(NULL) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000944 this->set(context, desc, match);
945 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000946
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000947 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000948 this->reset();
949 }
950
951 void reset() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000952 if (NULL != fContext && NULL != fTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000953 fContext->unlockScratchTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000954 fTexture = NULL;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000955 }
956 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000957
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000958 /*
959 * When detaching a texture we do not unlock it in the texture cache but
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000960 * we do set the returnToCache flag. In this way the texture remains
961 * "locked" in the texture cache until it is freed and recycled in
962 * GrTexture::internal_dispose. In reality, the texture has been removed
963 * from the cache (because this is in AutoScratchTexture) and by not
964 * calling unlockTexture we simply don't re-add it. It will be reattached
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000965 * in GrTexture::internal_dispose.
966 *
967 * Note that the caller is assumed to accept and manage the ref to the
968 * returned texture.
969 */
970 GrTexture* detach() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000971 GrTexture* temp = fTexture;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000972
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000973 // Conceptually the texture's cache entry loses its ref to the
974 // texture while the caller of this method gets a ref.
975 GrAssert(NULL != temp->getCacheEntry());
976
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000977 fTexture = NULL;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000978
979 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000980 return temp;
981 }
982
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000983 GrTexture* set(GrContext* context,
984 const GrTextureDesc& desc,
985 GrContext::ScratchTexMatch match =
986 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000987 this->reset();
988
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000989 fContext = context;
990 if (NULL != fContext) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000991 fTexture = fContext->lockScratchTexture(desc, match);
992 if (NULL == fTexture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000993 fContext = NULL;
994 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000995 return fTexture;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000996 } else {
997 return NULL;
998 }
999 }
1000
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001001 GrTexture* texture() { return fTexture; }
1002
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001003private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001004 GrContext* fContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001005 GrTexture* fTexture;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001006};
1007
bsalomon@google.com27847de2011-02-22 20:59:41 +00001008#endif