blob: 4fd60266d22d3cbd585492861eae05471d8766d3 [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.com288d9542012-10-17 12:53:54 +000013#include "GrColor.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.com288d9542012-10-17 12:53:54 +000016#include "GrMatrix.h"
17#include "GrPaint.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000018// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
19// remove.
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020#include "GrRenderTarget.h"
bsalomon@google.com288d9542012-10-17 12:53:54 +000021#include "GrRefCnt.h"
22#include "GrTexture.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000023
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000024class GrAutoScratchTexture;
robertphillips@google.coma9b06232012-08-30 11:06:31 +000025class GrCacheKey;
bsalomon@google.com288d9542012-10-17 12:53:54 +000026class GrCustomStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000027class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000029class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000030class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000031class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000032class GrIndexBufferAllocPool;
33class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000034class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000035class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000036class GrResourceEntry;
37class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000038class GrStencilBuffer;
bsalomon@google.com288d9542012-10-17 12:53:54 +000039class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000040class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000041class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000042class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000043
bsalomon@google.com91826102011-03-21 19:51:57 +000044class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000045public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000046 SK_DECLARE_INST_COUNT(GrContext)
47
bsalomon@google.com27847de2011-02-22 20:59:41 +000048 /**
49 * Creates a GrContext from within a 3D context.
50 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000051 static GrContext* Create(GrEngine engine,
52 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000053
bsalomon@google.comc0af3172012-06-15 14:10:09 +000054 /**
55 * Returns the number of GrContext instances for the current thread.
56 */
57 static int GetThreadInstanceCount();
58
bsalomon@google.com27847de2011-02-22 20:59:41 +000059 virtual ~GrContext();
60
61 /**
62 * The GrContext normally assumes that no outsider is setting state
63 * within the underlying 3D API's context/device/whatever. This call informs
64 * the context that the state was modified and it should resend. Shouldn't
65 * be called frequently for good performance.
66 */
67 void resetContext();
68
bsalomon@google.com8fe72472011-03-30 21:26:44 +000069 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000070 * Callback function to allow classes to cleanup on GrContext destruction.
71 * The 'info' field is filled in with the 'info' passed to addCleanUp.
72 */
73 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
74
75 /**
76 * Add a function to be called from within GrContext's destructor.
77 * This gives classes a chance to free resources held on a per context basis.
78 * The 'info' parameter will be stored and passed to the callback function.
79 */
80 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
81 CleanUpData* entry = fCleanUpData.push();
82
83 entry->fFunc = cleanUp;
84 entry->fInfo = info;
85 }
86
87 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +000088 * Abandons all GPU resources, assumes 3D API state is unknown. Call this
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 * if you have lost the associated GPU context, and thus internal texture,
90 * buffer, etc. references/IDs are now invalid. Should be called even when
91 * GrContext is no longer going to be used for two reasons:
92 * 1) ~GrContext will not try to free the objects in the 3D API.
93 * 2) If you've created GrResources that outlive the GrContext they will
94 * be marked as invalid (GrResource::isValid()) and won't attempt to
95 * free their underlying resource in the 3D API.
96 * Content drawn since the last GrContext::flush() may be lost.
97 */
98 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000099
100 /**
junov@google.com53a55842011-06-08 22:55:10 +0000101 * Similar to contextLost, but makes no attempt to reset state.
102 * Use this method when GrContext destruction is pending, but
103 * the graphics context is destroyed first.
104 */
105 void contextDestroyed();
106
107 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000108 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111 void freeGpuResources();
112
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000113 /**
114 * Returns the number of bytes of GPU memory hosted by the texture cache.
115 */
116 size_t getGpuTextureCacheBytes() const;
117
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118 ///////////////////////////////////////////////////////////////////////////
119 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +0000120
121 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000122 * Create a new entry, based on the specified key and texture, and return
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000123 * a "locked" texture. Must call be balanced with an unlockTexture() call.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000124 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000125 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000126 * the cache entry used. (e.g. different versions may exist
127 * for different wrap modes on GPUs with limited NPOT
128 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000129 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000130 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000131 * @param srcData Pointer to the pixel values.
132 * @param rowBytes The number of bytes between rows of the texture. Zero
133 * implies tightly packed rows.
134 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000135 GrTexture* createTexture(const GrTextureParams* params,
136 const GrTextureDesc& desc,
137 const GrCacheData& cacheData,
138 void* srcData, size_t rowBytes);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000139
140 /**
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000141 * Look for a texture that matches 'key' in the cache. If not found,
142 * return NULL.
143 */
144 GrTexture* findTexture(const GrCacheKey& key);
145
146 /**
skia.committer@gmail.comd6ce0db2012-09-10 02:01:22 +0000147 * Search for an entry based on key and dimensions. If found,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000148 * return it. The return value will be NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000149 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000150 * @param desc Description of the texture properties.
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000151 * @param cacheData Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000152 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000153 * the cache entry used. (e.g. different versions may exist
154 * for different wrap modes on GPUs with limited NPOT
155 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000156 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000157 GrTexture* findTexture(const GrTextureDesc& desc,
158 const GrCacheData& cacheData,
159 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000160 /**
161 * Determines whether a texture is in the cache. If the texture is found it
162 * will not be locked or returned. This call does not affect the priority of
163 * the texture for deletion.
164 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000165 bool isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000166 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000167 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000168
169 /**
170 * Enum that determines how closely a returned scratch texture must match
171 * a provided GrTextureDesc.
172 */
173 enum ScratchTexMatch {
174 /**
175 * Finds a texture that exactly matches the descriptor.
176 */
177 kExact_ScratchTexMatch,
178 /**
179 * Finds a texture that approximately matches the descriptor. Will be
180 * at least as large in width and height as desc specifies. If desc
181 * specifies that texture is a render target then result will be a
182 * render target. If desc specifies a render target and doesn't set the
183 * no stencil flag then result will have a stencil. Format and aa level
184 * will always match.
185 */
186 kApprox_ScratchTexMatch
187 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000188
189 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000190 * Returns a texture matching the desc. It's contents are unknown. Subsequent
191 * requests with the same descriptor are not guaranteed to return the same
192 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000193 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000194 *
195 * Textures created by createAndLockTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000197 * unextended GLES2). Tiling a NPOT texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000198 * such an API will create gaps in the tiling pattern. This includes clamp
199 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000200 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000201 GrTexture* lockScratchTexture(const GrTextureDesc& desc,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000202 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000203
204 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000205 * When done with an entry, call unlockTexture(entry) on it, which returns
206 * it to the cache, where it may be purged.
207 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000208 void unlockScratchTexture(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000209
210 /**
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000211 * This method should be called whenever a GrTexture is unreffed or
212 * switched from exclusive to non-exclusive. This
213 * gives the resource cache a chance to discard unneeded textures.
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000214 * Note: this entry point will be removed once totally ref-driven
215 * cache maintenance is implemented
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000216 */
217 void purgeCache();
218
219 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220 * Creates a texture that is outside the cache. Does not count against
221 * cache's budget.
222 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000223 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000224 void* srcData,
225 size_t rowBytes);
226
227 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000228 * Returns true if the specified use of an indexed texture is supported.
229 * Support may depend upon whether the texture params indicate that the
230 * texture will be tiled. Passing NULL for the texture params indicates
231 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000233 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000234 int width,
235 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000236
237 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000238 * Return the current texture cache limits.
239 *
240 * @param maxTextures If non-null, returns maximum number of textures that
241 * can be held in the cache.
242 * @param maxTextureBytes If non-null, returns maximum number of bytes of
243 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000244 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000245 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000246
247 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000248 * Specify the texture cache limits. If the current cache exceeds either
249 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000251 * @param maxTextures The maximum number of textures that can be held in
252 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000253 * @param maxTextureBytes The maximum number of bytes of texture memory
254 * that can be held in the cache.
255 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000256 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000257
258 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000259 * Return the max width or height of a texture supported by the current GPU.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000260 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000261 int getMaxTextureSize() const;
262
263 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000264 * Return the max width or height of a render target supported by the
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000265 * current GPU.
bsalomon@google.com91958362011-06-13 17:58:13 +0000266 */
267 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000268
269 ///////////////////////////////////////////////////////////////////////////
270 // Render targets
271
272 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000273 * Sets the render target.
274 * @param target the render target to set. (should not be NULL.)
275 */
276 void setRenderTarget(GrRenderTarget* target);
277
278 /**
279 * Gets the current render target.
280 * @return the currently bound render target. Should never be NULL.
281 */
282 const GrRenderTarget* getRenderTarget() const;
283 GrRenderTarget* getRenderTarget();
284
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000285 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
286
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000287 /**
288 * Can the provided configuration act as a color render target?
289 */
290 bool isConfigRenderable(GrPixelConfig config) const;
291
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000292 ///////////////////////////////////////////////////////////////////////////
293 // Platform Surfaces
294
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000295 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000296 * Wraps an existing texture with a GrTexture object.
297 *
298 * OpenGL: if the object is a texture Gr may change its GL texture params
299 * when it is drawn.
300 *
301 * @param desc description of the object to create.
302 *
303 * @return GrTexture object or NULL on failure.
304 */
305 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
306
307 /**
308 * Wraps an existing render target with a GrRenderTarget object. It is
309 * similar to createPlatformTexture but can be used to draw into surfaces
310 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
311 * the client will resolve to a texture).
312 *
313 * @param desc description of the object to create.
314 *
315 * @return GrTexture object or NULL on failure.
316 */
317 GrRenderTarget* createPlatformRenderTarget(
318 const GrPlatformRenderTargetDesc& desc);
319
bsalomon@google.com27847de2011-02-22 20:59:41 +0000320 ///////////////////////////////////////////////////////////////////////////
321 // Matrix state
322
323 /**
324 * Gets the current transformation matrix.
325 * @return the current matrix.
326 */
327 const GrMatrix& getMatrix() const;
328
329 /**
330 * Sets the transformation matrix.
331 * @param m the matrix to set.
332 */
333 void setMatrix(const GrMatrix& m);
334
335 /**
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000336 * Sets the current transformation matrix to identity.
337 */
338 void setIdentityMatrix();
339
340 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000341 * Concats the current matrix. The passed matrix is applied before the
342 * current matrix.
343 * @param m the matrix to concat.
344 */
345 void concatMatrix(const GrMatrix& m) const;
346
347
348 ///////////////////////////////////////////////////////////////////////////
349 // Clip state
350 /**
351 * Gets the current clip.
352 * @return the current clip.
353 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000354 const GrClipData* getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355
356 /**
357 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000358 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000359 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000360 void setClip(const GrClipData* clipData);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361
bsalomon@google.com27847de2011-02-22 20:59:41 +0000362 ///////////////////////////////////////////////////////////////////////////
363 // Draws
364
365 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000366 * Clear the entire or rect of the render target, ignoring any clips.
367 * @param rect the rect to clear or the whole thing if rect is NULL.
368 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000369 * @param target if non-NULL, the render target to clear otherwise clear
370 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000371 */
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000372 void clear(const GrIRect* rect, GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000373 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374
375 /**
376 * Draw everywhere (respecting the clip) with the paint.
377 */
378 void drawPaint(const GrPaint& paint);
379
380 /**
381 * Draw the rect using a paint.
382 * @param paint describes how to color pixels.
383 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
384 * the rect is mitered stroked based on strokeWidth. If
385 * strokeWidth == 0, then the stroke is always a single
386 * pixel thick.
387 * @param matrix Optional matrix applied to the rect. Applied before
388 * context's matrix or the paint's matrix.
389 * The rects coords are used to access the paint (through texture matrix)
390 */
391 void drawRect(const GrPaint& paint,
392 const GrRect&,
393 GrScalar strokeWidth = -1,
394 const GrMatrix* matrix = NULL);
395
396 /**
397 * Maps a rect of paint coordinates onto the a rect of destination
398 * coordinates. Each rect can optionally be transformed. The srcRect
399 * is stretched over the dstRect. The dstRect is transformed by the
400 * context's matrix and the srcRect is transformed by the paint's matrix.
401 * Additional optional matrices can be provided by parameters.
402 *
403 * @param paint describes how to color pixels.
404 * @param dstRect the destination rect to draw.
405 * @param srcRect rect of paint coordinates to be mapped onto dstRect
406 * @param dstMatrix Optional matrix to transform dstRect. Applied before
407 * context's matrix.
408 * @param srcMatrix Optional matrix to transform srcRect Applied before
409 * paint's matrix.
410 */
411 void drawRectToRect(const GrPaint& paint,
412 const GrRect& dstRect,
413 const GrRect& srcRect,
414 const GrMatrix* dstMatrix = NULL,
415 const GrMatrix* srcMatrix = NULL);
416
417 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000418 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 *
420 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000421 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000422 * @param fill the path filling rule to use.
423 * @param translate optional additional translation applied to the
424 * path.
425 */
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000426 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000427
bsalomon@google.com27847de2011-02-22 20:59:41 +0000428 /**
429 * Draws vertices with a paint.
430 *
431 * @param paint describes how to color pixels.
432 * @param primitiveType primitives type to draw.
433 * @param vertexCount number of vertices.
434 * @param positions array of vertex positions, required.
435 * @param texCoords optional array of texture coordinates used
436 * to access the paint.
437 * @param colors optional array of per-vertex colors, supercedes
438 * the paint's color field.
439 * @param indices optional array of indices. If NULL vertices
440 * are drawn non-indexed.
441 * @param indexCount if indices is non-null then this is the
442 * number of indices.
443 */
444 void drawVertices(const GrPaint& paint,
445 GrPrimitiveType primitiveType,
446 int vertexCount,
447 const GrPoint positions[],
448 const GrPoint texs[],
449 const GrColor colors[],
450 const uint16_t indices[],
451 int indexCount);
452
bsalomon@google.com93c96602012-04-27 13:05:21 +0000453 /**
454 * Draws an oval.
455 *
456 * @param paint describes how to color pixels.
457 * @param rect the bounding rect of the oval.
458 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
459 * the rect is stroked based on strokeWidth. If
460 * strokeWidth == 0, then the stroke is always a single
461 * pixel thick.
462 */
463 void drawOval(const GrPaint& paint,
464 const GrRect& rect,
465 SkScalar strokeWidth);
466
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467 ///////////////////////////////////////////////////////////////////////////
468 // Misc.
469
470 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000471 * Flags that affect flush() behavior.
472 */
473 enum FlushBits {
474 /**
475 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
476 * it can be rendered to directly. However, Gr lazily sets state. Simply
477 * calling setRenderTarget() followed by flush() without flags may not
478 * bind the render target. This flag forces the context to bind the last
479 * set render target in the 3D API.
480 */
481 kForceCurrentRenderTarget_FlushBit = 0x1,
482 /**
483 * A client may reach a point where it has partially rendered a frame
484 * through a GrContext that it knows the user will never see. This flag
485 * causes the flush to skip submission of deferred content to the 3D API
486 * during the flush.
487 */
488 kDiscard_FlushBit = 0x2,
489 };
490
491 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000492 * Call to ensure all drawing to the context has been issued to the
493 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000494 * @param flagsBitfield flags that control the flushing behavior. See
495 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000497 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498
bsalomon@google.com0342a852012-08-20 19:22:38 +0000499 /**
500 * These flags can be used with the read/write pixels functions below.
501 */
502 enum PixelOpsFlags {
503 /** The GrContext will not be flushed. This means that the read or write may occur before
504 previous draws have executed. */
505 kDontFlush_PixelOpsFlag = 0x1,
506 /** The src for write or dst read is unpremultiplied. This is only respected if both the
507 config src and dst configs are an RGBA/BGRA 8888 format. */
508 kUnpremul_PixelOpsFlag = 0x2,
509 };
510
bsalomon@google.com27847de2011-02-22 20:59:41 +0000511 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000512 * Reads a rectangle of pixels from a render target.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000513 * @param target the render target to read from. NULL means the current render target.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000514 * @param left left edge of the rectangle to read (inclusive)
515 * @param top top edge of the rectangle to read (inclusive)
516 * @param width width of rectangle to read in pixels.
517 * @param height height of rectangle to read in pixels.
518 * @param config the pixel config of the destination buffer
519 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000520 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
521 * packed.
522 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000524 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
525 * pixel config or because no render target is currently set and NULL was passed for
526 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000527 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000528 bool readRenderTargetPixels(GrRenderTarget* target,
529 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000530 GrPixelConfig config, void* buffer,
531 size_t rowBytes = 0,
532 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533
534 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000535 * Copy the src pixels [buffer, row bytes, pixel config] into a render target at the specified
bsalomon@google.com0342a852012-08-20 19:22:38 +0000536 * rectangle.
537 * @param target the render target to write into. NULL means the current render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000538 * @param left left edge of the rectangle to write (inclusive)
539 * @param top top edge of the rectangle to write (inclusive)
540 * @param width width of rectangle to write in pixels.
541 * @param height height of rectangle to write in pixels.
542 * @param config the pixel config of the source buffer
543 * @param buffer memory to read the rectangle from.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000544 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000545 * packed.
546 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 */
548 void writeRenderTargetPixels(GrRenderTarget* target,
549 int left, int top, int width, int height,
550 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000551 size_t rowBytes = 0,
552 uint32_t pixelOpsFlags = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000553
554 /**
555 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000556 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000557 * @param left left edge of the rectangle to read (inclusive)
558 * @param top top edge of the rectangle to read (inclusive)
559 * @param width width of rectangle to read in pixels.
560 * @param height height of rectangle to read in pixels.
561 * @param config the pixel config of the destination buffer
562 * @param buffer memory to read the rectangle into.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000563 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000564 * packed.
565 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000566 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000567 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
568 * pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000569 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000570 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000571 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000572 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000573 size_t rowBytes = 0,
574 uint32_t pixelOpsFlags = 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575
576 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000577 * Writes a rectangle of pixels to a texture.
578 * @param texture the render target to read from.
579 * @param left left edge of the rectangle to write (inclusive)
580 * @param top top edge of the rectangle to write (inclusive)
581 * @param width width of rectangle to write in pixels.
582 * @param height height of rectangle to write in pixels.
583 * @param config the pixel config of the source buffer
584 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000585 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000586 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000587 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000588 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000589 void writeTexturePixels(GrTexture* texture,
590 int left, int top, int width, int height,
591 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000592 size_t rowBytes,
593 uint32_t pixelOpsFlags = 0);
594
595
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000596 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000597 * Copies all texels from one texture to another.
598 * @param src the texture to copy from.
599 * @param dst the render target to copy to.
600 */
601 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000602
603 /**
604 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000605 * down-sampled to the associated GrTexture (accessible via
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000606 * GrRenderTarget::asTexture()). Any pending draws to the render target will
607 * be executed before the resolve.
608 *
609 * This is only necessary when a client wants to access the object directly
610 * using the underlying graphics API. GrContext will detect when it must
611 * perform a resolve to a GrTexture used as the source of a draw or before
612 * reading pixels back from a GrTexture or GrRenderTarget.
613 */
614 void resolveRenderTarget(GrRenderTarget* target);
615
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000616 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000617 * Applies a 2D Gaussian blur to a given texture.
618 * @param srcTexture The source texture to be blurred.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000619 * @param canClobberSrc If true, srcTexture may be overwritten, and
620 * may be returned as the result.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000621 * @param rect The destination rectangle.
622 * @param sigmaX The blur's standard deviation in X.
623 * @param sigmaY The blur's standard deviation in Y.
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000624 * @return the blurred texture, which may be srcTexture reffed, or a
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000625 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000626 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000627 GrTexture* gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000628 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000629 const SkRect& rect,
630 float sigmaX, float sigmaY);
631
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000632 /**
bsalomon@google.com82aa7482012-08-13 14:22:17 +0000633 * Zooms a subset of the texture to a larger size with a nice edge.
634 * The inner rectangle is a simple scaling of the texture by a factor of
635 * |zoom|. The outer |inset| pixels transition from the background texture
636 * to the zoomed coordinate system at a rate of
637 * (distance_to_edge / inset) ^2, producing a rounded lens effect.
638 * @param srcTexture The source texture to be zoomed.
639 * @param dstRect The destination rectangle.
640 * @param srcRect The source rectangle. Must be smaller than
641 * dstRect
642 * @param inset Number of pixels to blend along the edges.
643 * @return the zoomed texture, which is dstTexture.
644 */
645 GrTexture* zoom(GrTexture* srcTexture,
646 const SkRect& dstRect, const SkRect& srcRect, float inset);
647
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648 ///////////////////////////////////////////////////////////////////////////
649 // Helpers
650
robertphillips@google.comccb39502012-10-01 18:25:13 +0000651 class AutoRenderTarget : public ::GrNoncopyable {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000652 public:
653 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000654 fPrevTarget = context->getRenderTarget();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000655 GrSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000656 context->setRenderTarget(target);
657 fContext = context;
658 }
659 AutoRenderTarget(GrContext* context) {
660 fPrevTarget = context->getRenderTarget();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000661 GrSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000662 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000663 }
664 ~AutoRenderTarget() {
robertphillips@google.comccb39502012-10-01 18:25:13 +0000665 if (NULL != fContext) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000666 fContext->setRenderTarget(fPrevTarget);
667 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000668 GrSafeUnref(fPrevTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000669 }
670 private:
671 GrContext* fContext;
672 GrRenderTarget* fPrevTarget;
673 };
674
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000675 /**
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000676 * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
677 * for a coordinate system change. Here is an example of how the paint param can be used:
678 *
679 * A GrPaint is setup with custom stages. The stages will have access to the pre-matrix source
680 * geometry positions when the draw is executed. Later on a decision is made to transform the
681 * geometry to device space on the CPU. The custom stages now need to know that the space in
682 * which the geometry will be specified has changed.
683 *
684 * Note that when restore is called (or in the destructor) the context's matrix will be
685 * restored. However, the paint will not be restored. The caller must make a copy of the
686 * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
687 * initialized.
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000688 */
689 class AutoMatrix : GrNoncopyable {
690 public:
691 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000692
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000693 ~AutoMatrix() { this->restore(); }
694
695 /**
696 * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
697 */
698 void setPreConcat(GrContext* context, const GrMatrix& preConcat, GrPaint* paint = NULL) {
699 GrAssert(NULL != context);
700
701 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000702
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000703 fContext = context;
704 fMatrix = context->getMatrix();
705 this->preConcat(preConcat, paint);
706 }
707
708 /**
709 * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
710 * update a paint but the matrix cannot be inverted.
711 */
712 bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
713 GrAssert(NULL != context);
714
715 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000716
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000717 if (NULL != paint) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000718 if (!paint->sourceCoordChangeByInverse(context->getMatrix())) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000719 return false;
720 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000721 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000722 fMatrix = context->getMatrix();
723 fContext = context;
724 context->setIdentityMatrix();
725 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000726 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000727
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000728 /**
729 * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
730 * required to update a paint but the matrix cannot be inverted.
731 */
732 bool set(GrContext* context, const GrMatrix& newMatrix, GrPaint* paint = NULL) {
733 if (NULL != paint) {
734 if (!this->setIdentity(context, paint)) {
735 return false;
736 }
737 this->preConcat(newMatrix, paint);
738 } else {
739 this->restore();
740 fContext = context;
741 fMatrix = context->getMatrix();
742 context->setMatrix(newMatrix);
743 }
744 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000745 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000746
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000747 /**
748 * If this has been initialized then the context's matrix will be further updated by
749 * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
750 * The paint is assumed to be relative to the context's matrix at the time this call is
751 * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
752 * performs an incremental update of the paint.
753 */
754 void preConcat(const GrMatrix& preConcat, GrPaint* paint = NULL) {
755 if (NULL != paint) {
bsalomon@google.com288d9542012-10-17 12:53:54 +0000756 paint->sourceCoordChange(preConcat);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000757 }
758 fContext->concatMatrix(preConcat);
759 }
760
761 /**
762 * Returns false if never initialized or the inverse matrix was required to update a paint
763 * but the matrix could not be inverted.
764 */
765 bool succeeded() const { return NULL != fContext; }
766
767 /**
768 * If this has been initialized then the context's original matrix is restored.
769 */
770 void restore() {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000771 if (NULL != fContext) {
772 fContext->setMatrix(fMatrix);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000773 fContext = NULL;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000774 }
775 }
776
777 private:
778 GrContext* fContext;
779 GrMatrix fMatrix;
780 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000782 class AutoClip : GrNoncopyable {
783 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000784 // This enum exists to require a caller of the constructor to acknowledge that the clip will
785 // initially be wide open. It also could be extended if there are other desirable initial
786 // clip states.
787 enum InitialClip {
788 kWideOpen_InitialClip,
789 };
790
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000791 AutoClip(GrContext* context, InitialClip initialState)
robertphillips@google.comccb39502012-10-01 18:25:13 +0000792 : fContext(context) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000793 GrAssert(kWideOpen_InitialClip == initialState);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000794 fNewClipData.fClipStack = &fNewClipStack;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000795
796 fOldClip = context->getClip();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000797 context->setClip(&fNewClipData);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000798 }
799
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000800 AutoClip(GrContext* context, const GrRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000801 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000802 , fNewClipStack(newClipRect) {
803 fNewClipData.fClipStack = &fNewClipStack;
804
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000805 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000806 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000807 }
808
809 ~AutoClip() {
810 if (NULL != fContext) {
811 fContext->setClip(fOldClip);
812 }
813 }
814 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000815 GrContext* fContext;
816 const GrClipData* fOldClip;
817
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000818 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000819 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000820 };
821
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000822 class AutoWideOpenIdentityDraw {
823 public:
824 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
825 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000826 , fAutoRT(ctx, rt) {
827 fAutoMatrix.setIdentity(ctx);
828 // should never fail with no paint param.
829 GrAssert(fAutoMatrix.succeeded());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000830 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000831
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000832 private:
833 AutoClip fAutoClip;
834 AutoRenderTarget fAutoRT;
835 AutoMatrix fAutoMatrix;
836 };
837
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 ///////////////////////////////////////////////////////////////////////////
839 // Functions intended for internal use only.
840 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000841 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842 GrFontCache* getFontCache() { return fFontCache; }
843 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000844 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000845
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000846 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000847 * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
848 * called to check the cache for a SB that matches an RT's criteria.
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000849 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000850 void addStencilBuffer(GrStencilBuffer* sb);
851 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000852
robertphillips@google.com2c756812012-05-22 20:28:23 +0000853 GrPathRenderer* getPathRenderer(const SkPath& path,
854 GrPathFill fill,
855 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000856 bool antiAlias,
857 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000858
robertphillips@google.com59552022012-08-31 13:07:37 +0000859#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000860 void printCacheStats() const;
861#endif
862
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863private:
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000864 // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
865 enum BufferedDraw {
866 kYes_BufferedDraw,
867 kNo_BufferedDraw,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000868 };
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000869 BufferedDraw fLastDrawWasBuffered;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000871 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000872 GrDrawState* fDrawState;
873
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000874 GrResourceCache* fTextureCache;
875 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000876
bsalomon@google.com30085192011-08-19 15:42:31 +0000877 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000878 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879
880 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
881 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
882 GrInOrderDrawBuffer* fDrawBuffer;
883
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000884 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000885
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000886 bool fDidTestPMConversions;
887 int fPMToUPMConversion;
888 int fUPMToPMConversion;
889
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000890 struct CleanUpData {
891 PFCleanUpFunc fFunc;
892 void* fInfo;
893 };
894
895 SkTDArray<CleanUpData> fCleanUpData;
896
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000897 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000898
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000899 void setupDrawBuffer();
900
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 void flushDrawBuffer();
902
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000903 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
904 /// draw state is left unmodified.
905 GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000907 void internalDrawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000908
robertphillips@google.com3319f332012-08-13 18:00:36 +0000909 GrTexture* createResizedTexture(const GrTextureDesc& desc,
910 const GrCacheData& cacheData,
911 void* srcData,
912 size_t rowBytes,
913 bool needsFiltering);
914
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000915 // Needed so GrTexture's returnToCache helper function can call
916 // addExistingTextureToCache
917 friend class GrTexture;
918
919 // Add an existing texture to the texture cache. This is intended solely
920 // for use with textures released from an GrAutoScratchTexture.
921 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +0000922
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000923 GrCustomStage* createPMToUPMEffect(GrTexture* texture, bool swapRAndB);
924 GrCustomStage* createUPMToPMEffect(GrTexture* texture, bool swapRAndB);
925
reed@google.comfa35e3d2012-06-26 20:16:17 +0000926 typedef GrRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927};
928
929/**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000930 * Gets and locks a scratch texture from a descriptor using either exact or approximate criteria.
931 * Unlocks texture in the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000932 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000934public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000935 GrAutoScratchTexture()
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000936 : fContext(NULL)
937 , fTexture(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000938 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000939
940 GrAutoScratchTexture(GrContext* context,
941 const GrTextureDesc& desc,
942 GrContext::ScratchTexMatch match =
943 GrContext::kApprox_ScratchTexMatch)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000944 : fContext(NULL)
945 , fTexture(NULL) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000946 this->set(context, desc, match);
947 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000948
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000949 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000950 this->reset();
951 }
952
953 void reset() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000954 if (NULL != fContext && NULL != fTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000955 fContext->unlockScratchTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000956 fTexture = NULL;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000957 }
958 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000959
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000960 /*
961 * When detaching a texture we do not unlock it in the texture cache but
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000962 * we do set the returnToCache flag. In this way the texture remains
963 * "locked" in the texture cache until it is freed and recycled in
964 * GrTexture::internal_dispose. In reality, the texture has been removed
965 * from the cache (because this is in AutoScratchTexture) and by not
966 * calling unlockTexture we simply don't re-add it. It will be reattached
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000967 * in GrTexture::internal_dispose.
968 *
969 * Note that the caller is assumed to accept and manage the ref to the
970 * returned texture.
971 */
972 GrTexture* detach() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000973 GrTexture* temp = fTexture;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000974
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000975 // Conceptually the texture's cache entry loses its ref to the
976 // texture while the caller of this method gets a ref.
977 GrAssert(NULL != temp->getCacheEntry());
978
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000979 fTexture = NULL;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000980
981 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000982 return temp;
983 }
984
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000985 GrTexture* set(GrContext* context,
986 const GrTextureDesc& desc,
987 GrContext::ScratchTexMatch match =
988 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000989 this->reset();
990
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000991 fContext = context;
992 if (NULL != fContext) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000993 fTexture = fContext->lockScratchTexture(desc, match);
994 if (NULL == fTexture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000995 fContext = NULL;
996 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000997 return fTexture;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000998 } else {
999 return NULL;
1000 }
1001 }
1002
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001003 GrTexture* texture() { return fTexture; }
1004
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001005private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001006 GrContext* fContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001007 GrTexture* fTexture;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001008};
1009
bsalomon@google.com27847de2011-02-22 20:59:41 +00001010#endif