blob: af44aa7c510087e39c9989b983d3b657dc1f4aca [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
8#ifndef GrContext_DEFINED
9#define GrContext_DEFINED
10
robertphillips@google.coma2d71482012-08-01 20:08:47 +000011#include "GrClipData.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "GrColor.h"
bsalomon@google.com288d9542012-10-17 12:53:54 +000013#include "GrPaint.h"
bsalomon@google.com45a15f52012-12-10 19:10:17 +000014#include "GrPathRendererChain.h"
rmistry@google.comfbfcd562012-08-23 18:09:54 +000015#include "GrRenderTarget.h"
bsalomon@google.com288d9542012-10-17 12:53:54 +000016#include "GrTexture.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000017#include "SkMatrix.h"
egdaniele61c4112014-06-12 10:24:21 -070018#include "SkPathEffect.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000021class GrAARectRenderer;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000022class GrAutoScratchTexture;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000023class GrCacheable;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000024class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000025class GrDrawTarget;
bsalomon@google.coma469c282012-10-24 18:28:34 +000026class GrEffect;
bsalomon@google.com27847de2011-02-22 20:59:41 +000027class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000028class GrGpu;
egdanielbbcb38d2014-06-19 10:19:29 -070029class GrGpuTraceMarker;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000030class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000031class GrIndexBufferAllocPool;
32class GrInOrderDrawBuffer;
robertphillips@google.come930a072014-04-03 00:34:27 +000033class GrLayerCache;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000034class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000035class GrPath;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000036class GrPathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000037class GrResourceEntry;
38class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000039class GrStencilBuffer;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000040class GrTestTarget;
kkinnunenc6cb56f2014-06-24 00:12:27 -070041class GrTextContext;
bsalomon@google.com288d9542012-10-17 12:53:54 +000042class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000043class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000044class GrVertexBufferAllocPool;
egdanield58a0ba2014-06-11 10:30:05 -070045class GrStrokeInfo;
robertphillips@google.com72176b22012-05-23 13:19:12 +000046class GrSoftwarePathRenderer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000047class SkStrokeRec;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000048
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000049class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000050public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000051 SK_DECLARE_INST_COUNT(GrContext)
52
bsalomon@google.com27847de2011-02-22 20:59:41 +000053 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000054 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000055 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000056 static GrContext* Create(GrBackend, GrBackendContext);
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.com27847de2011-02-22 20:59:41 +000058 virtual ~GrContext();
59
60 /**
61 * The GrContext normally assumes that no outsider is setting state
62 * within the underlying 3D API's context/device/whatever. This call informs
63 * the context that the state was modified and it should resend. Shouldn't
64 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000065 * The flag bits, state, is dpendent on which backend is used by the
66 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000067 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000068 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000069
bsalomon@google.com8fe72472011-03-30 21:26:44 +000070 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000071 * Callback function to allow classes to cleanup on GrContext destruction.
72 * The 'info' field is filled in with the 'info' passed to addCleanUp.
73 */
74 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
75
76 /**
77 * Add a function to be called from within GrContext's destructor.
78 * This gives classes a chance to free resources held on a per context basis.
79 * The 'info' parameter will be stored and passed to the callback function.
80 */
81 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
82 CleanUpData* entry = fCleanUpData.push();
83
84 entry->fFunc = cleanUp;
85 entry->fInfo = info;
86 }
87
88 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +000089 * Abandons all GPU resources, assumes 3D API state is unknown. Call this
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 * if you have lost the associated GPU context, and thus internal texture,
91 * buffer, etc. references/IDs are now invalid. Should be called even when
92 * GrContext is no longer going to be used for two reasons:
93 * 1) ~GrContext will not try to free the objects in the 3D API.
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000094 * 2) If you've created GrGpuObjects that outlive the GrContext they will
95 * be marked as invalid (GrGpuObjects::isValid()) and won't attempt to
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096 * free their underlying resource in the 3D API.
97 * Content drawn since the last GrContext::flush() may be lost.
98 */
99 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100
101 /**
junov@google.com53a55842011-06-08 22:55:10 +0000102 * Similar to contextLost, but makes no attempt to reset state.
103 * Use this method when GrContext destruction is pending, but
104 * the graphics context is destroyed first.
105 */
106 void contextDestroyed();
107
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000108 ///////////////////////////////////////////////////////////////////////////
109 // Resource Cache
110
111 /**
112 * Return the current GPU resource cache limits.
113 *
114 * @param maxResources If non-null, returns maximum number of resources that
115 * can be held in the cache.
116 * @param maxResourceBytes If non-null, returns maximum number of bytes of
117 * video memory that can be held in the cache.
118 */
119 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
120 SK_ATTR_DEPRECATED("This function has been renamed to getResourceCacheLimits().")
121 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
122 this->getResourceCacheLimits(maxTextures, maxTextureBytes);
123 }
124
125 /**
126 * Gets the current GPU resource cache usage.
127 *
128 * @param resourceCount If non-null, returns the number of resources that are held in the
129 * cache.
130 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
131 * in the cache.
132 */
133 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
134
135 SK_ATTR_DEPRECATED("Use getResourceCacheUsage().")
136 size_t getGpuTextureCacheBytes() const {
137 size_t bytes;
138 this->getResourceCacheUsage(NULL, &bytes);
139 return bytes;
140 }
141
142 SK_ATTR_DEPRECATED("Use getResourceCacheUsage().")
143 int getGpuTextureCacheResourceCount() const {
144 int count;
145 this->getResourceCacheUsage(&count, NULL);
146 return count;
147 }
148
149 /**
150 * Specify the GPU resource cache limits. If the current cache exceeds either
151 * of these, it will be purged (LRU) to keep the cache within these limits.
152 *
153 * @param maxResources The maximum number of resources that can be held in
154 * the cache.
155 * @param maxResourceBytes The maximum number of bytes of video memory
156 * that can be held in the cache.
157 */
158 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
159 SK_ATTR_DEPRECATED("This function has been renamed to setResourceCacheLimits().")
160 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
161 this->setResourceCacheLimits(maxTextures, maxTextureBytes);
162 }
163
junov@google.com53a55842011-06-08 22:55:10 +0000164 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000165 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000166 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000167 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000168 void freeGpuResources();
169
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000170 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000171 * This method should be called whenever a GrResource is unreffed or
172 * switched from exclusive to non-exclusive. This
173 * gives the resource cache a chance to discard unneeded resources.
174 * Note: this entry point will be removed once totally ref-driven
175 * cache maintenance is implemented.
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000176 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000177 void purgeCache();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000178
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000179 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000180 * Purge all the unlocked resources from the cache.
181 * This entry point is mainly meant for timing texture uploads
182 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000183 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000184 void purgeAllUnlockedResources();
185
186 /**
187 * Stores a custom resource in the cache, based on the specified key.
188 */
189 void addResourceToCache(const GrResourceKey&, GrCacheable*);
190
191 /**
192 * Finds a resource in the cache, based on the specified key. This is intended for use in
193 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
194 * caller must balance with a call to unref().
195 */
196 GrCacheable* findAndRefCachedResource(const GrResourceKey&);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000197
kkinnunenc6cb56f2014-06-24 00:12:27 -0700198 /**
199 * Creates a new text rendering context that is optimal for the
200 * render target and the context. Caller assumes the ownership
201 * of the returned object. The returned object must be deleted
202 * before the context is destroyed.
203 */
204 GrTextContext* createTextContext(GrRenderTarget*,
205 const SkDeviceProperties&,
206 bool enableDistanceFieldFonts);
207
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000208 ///////////////////////////////////////////////////////////////////////////
209 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +0000210
211 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000212 * Creates a new entry, based on the specified key and texture and returns it. The caller owns a
213 * ref on the returned texture which must be balanced by a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000214 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000215 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000216 * the cache entry used. (e.g. different versions may exist
217 * for different wrap modes on GPUs with limited NPOT
218 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000219 * @param desc Description of the texture properties.
krajcevski9c0e6292014-06-02 07:38:14 -0700220 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000221 * @param srcData Pointer to the pixel values.
222 * @param rowBytes The number of bytes between rows of the texture. Zero
krajcevski9c0e6292014-06-02 07:38:14 -0700223 * implies tightly packed rows. For compressed pixel configs, this
224 * field is ignored.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000225 * @param cacheKey (optional) If non-NULL, we'll write the cache key we used to cacheKey.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000226 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000227 GrTexture* createTexture(const GrTextureParams* params,
228 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000229 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000230 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000231 size_t rowBytes,
232 GrResourceKey* cacheKey = NULL);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000233 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000234 * Search for an entry based on key and dimensions. If found, ref it and return it. The return
235 * value will be NULL if not found. The caller must balance with a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000236 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000237 * @param desc Description of the texture properties.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000238 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000239 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000240 * the cache entry used. (e.g. different versions may exist
241 * for different wrap modes on GPUs with limited NPOT
242 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243 */
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000244 GrTexture* findAndRefTexture(const GrTextureDesc& desc,
245 const GrCacheID& cacheID,
246 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000247 /**
248 * Determines whether a texture is in the cache. If the texture is found it
249 * will not be locked or returned. This call does not affect the priority of
250 * the texture for deletion.
251 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000252 bool isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000253 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000254 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255
256 /**
257 * Enum that determines how closely a returned scratch texture must match
258 * a provided GrTextureDesc.
259 */
260 enum ScratchTexMatch {
261 /**
262 * Finds a texture that exactly matches the descriptor.
263 */
264 kExact_ScratchTexMatch,
265 /**
266 * Finds a texture that approximately matches the descriptor. Will be
267 * at least as large in width and height as desc specifies. If desc
268 * specifies that texture is a render target then result will be a
269 * render target. If desc specifies a render target and doesn't set the
270 * no stencil flag then result will have a stencil. Format and aa level
271 * will always match.
272 */
273 kApprox_ScratchTexMatch
274 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000275
276 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000277 * Returns a texture matching the desc. It's contents are unknown. Subsequent
278 * requests with the same descriptor are not guaranteed to return the same
279 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000280 * unlocked. Call must be balanced with an unlockTexture() call. The caller
281 * owns a ref on the returned texture and must balance with a call to unref.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000282 *
283 * Textures created by createAndLockTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000285 * unextended GLES2). Tiling a NPOT texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000286 * such an API will create gaps in the tiling pattern. This includes clamp
287 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000288 */
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000289 GrTexture* lockAndRefScratchTexture(const GrTextureDesc&, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000290
291 /**
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000292 * When done with an entry, call unlockScratchTexture(entry) on it, which returns
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000293 * it to the cache, where it may be purged. This does not unref the texture.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000294 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000295 void unlockScratchTexture(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000296
297 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298 * Creates a texture that is outside the cache. Does not count against
299 * cache's budget.
300 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000301 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000302 void* srcData,
303 size_t rowBytes);
304
305 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000306 * Returns true if the specified use of an indexed texture is supported.
307 * Support may depend upon whether the texture params indicate that the
308 * texture will be tiled. Passing NULL for the texture params indicates
309 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000311 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000312 int width,
313 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314
315 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000316 * Return the max width or height of a texture supported by the current GPU.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000317 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000318 int getMaxTextureSize() const;
319
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000320 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000321 * Temporarily override the true max texture size. Note: an override
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000322 * larger then the true max texture size will have no effect.
323 * This entry point is mainly meant for testing texture size dependent
324 * features and is only available if defined outside of Skia (see
325 * bleed GM.
326 */
327 void setMaxTextureSizeOverride(int maxTextureSizeOverride);
328
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329 ///////////////////////////////////////////////////////////////////////////
330 // Render targets
331
332 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000333 * Sets the render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000334 * @param target the render target to set.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000335 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000336 void setRenderTarget(GrRenderTarget* target) {
337 fRenderTarget.reset(SkSafeRef(target));
338 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000339
340 /**
341 * Gets the current render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000342 * @return the currently bound render target.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000343 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000344 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
345 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000346
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000347 /**
348 * Can the provided configuration act as a color render target?
349 */
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000350 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000351
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000352 /**
353 * Return the max width or height of a render target supported by the
354 * current GPU.
355 */
356 int getMaxRenderTargetSize() const;
357
358 /**
359 * Returns the max sample count for a render target. It will be 0 if MSAA
360 * is not supported.
361 */
362 int getMaxSampleCount() const;
363
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000364 /**
365 * Returns the recommended sample count for a render target when using this
366 * context.
367 *
368 * @param config the configuration of the render target.
369 * @param dpi the display density in dots per inch.
370 *
371 * @return sample count that should be perform well and have good enough
372 * rendering quality for the display. Alternatively returns 0 if
373 * MSAA is not supported or recommended to be used by default.
374 */
375 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
376
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000377 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000378 // Backend Surfaces
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000379
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000380 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000381 * Wraps an existing texture with a GrTexture object.
382 *
383 * OpenGL: if the object is a texture Gr may change its GL texture params
384 * when it is drawn.
385 *
386 * @param desc description of the object to create.
387 *
388 * @return GrTexture object or NULL on failure.
389 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000390 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000391
392 /**
393 * Wraps an existing render target with a GrRenderTarget object. It is
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000394 * similar to wrapBackendTexture but can be used to draw into surfaces
bsalomon@google.come269f212011-11-07 13:29:52 +0000395 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
396 * the client will resolve to a texture).
397 *
398 * @param desc description of the object to create.
399 *
400 * @return GrTexture object or NULL on failure.
401 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000402 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000403
bsalomon@google.com27847de2011-02-22 20:59:41 +0000404 ///////////////////////////////////////////////////////////////////////////
405 // Matrix state
406
407 /**
408 * Gets the current transformation matrix.
409 * @return the current matrix.
410 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000411 const SkMatrix& getMatrix() const { return fViewMatrix; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000412
413 /**
414 * Sets the transformation matrix.
415 * @param m the matrix to set.
416 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000417 void setMatrix(const SkMatrix& m) { fViewMatrix = m; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418
419 /**
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000420 * Sets the current transformation matrix to identity.
421 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000422 void setIdentityMatrix() { fViewMatrix.reset(); }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000423
424 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 * Concats the current matrix. The passed matrix is applied before the
426 * current matrix.
427 * @param m the matrix to concat.
428 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000429 void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430
431
432 ///////////////////////////////////////////////////////////////////////////
433 // Clip state
434 /**
435 * Gets the current clip.
436 * @return the current clip.
437 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000438 const GrClipData* getClip() const { return fClip; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000439
440 /**
441 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000442 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000444 void setClip(const GrClipData* clipData) { fClip = clipData; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000445
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446 ///////////////////////////////////////////////////////////////////////////
447 // Draws
448
449 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000450 * Clear the entire or rect of the render target, ignoring any clips.
451 * @param rect the rect to clear or the whole thing if rect is NULL.
452 * @param color the color to clear to.
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000453 * @param canIgnoreRect allows partial clears to be converted to whole
454 * clears on platforms for which that is cheap
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000455 * @param target if non-NULL, the render target to clear otherwise clear
456 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000457 */
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000458 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000459 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460
461 /**
462 * Draw everywhere (respecting the clip) with the paint.
463 */
464 void drawPaint(const GrPaint& paint);
465
466 /**
467 * Draw the rect using a paint.
468 * @param paint describes how to color pixels.
egdanield58a0ba2014-06-11 10:30:05 -0700469 * @param strokeInfo the stroke information (width, join, cap), and.
470 * the dash information (intervals, count, phase).
471 * If strokeInfo == NULL, then the rect is filled.
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000472 * Otherwise, if stroke width == 0, then the stroke
skia.committer@gmail.com26144182013-11-07 07:02:19 +0000473 * is always a single pixel thick, else the rect is
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000474 * mitered/beveled stroked based on stroke width.
egdanield58a0ba2014-06-11 10:30:05 -0700475 * If the stroke is dashed the rect is sent to drawPath.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000476 * @param matrix Optional matrix applied to the rect. Applied before
477 * context's matrix or the paint's matrix.
478 * The rects coords are used to access the paint (through texture matrix)
479 */
480 void drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000481 const SkRect&,
egdanield58a0ba2014-06-11 10:30:05 -0700482 const GrStrokeInfo* strokeInfo = NULL,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000483 const SkMatrix* matrix = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000484
485 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000486 * Maps a rect of local coordinates onto the a rect of destination
487 * coordinates. Each rect can optionally be transformed. The localRect
bsalomon@google.com27847de2011-02-22 20:59:41 +0000488 * is stretched over the dstRect. The dstRect is transformed by the
bsalomon@google.comc7818882013-03-20 19:19:53 +0000489 * context's matrix. Additional optional matrices for both rects can be
490 * provided by parameters.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000491 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000492 * @param paint describes how to color pixels.
493 * @param dstRect the destination rect to draw.
494 * @param localRect rect of local coordinates to be mapped onto dstRect
495 * @param dstMatrix Optional matrix to transform dstRect. Applied before context's matrix.
496 * @param localMatrix Optional matrix to transform localRect.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000497 */
498 void drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000499 const SkRect& dstRect,
500 const SkRect& localRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000501 const SkMatrix* dstMatrix = NULL,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000502 const SkMatrix* localMatrix = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000503
504 /**
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000505 * Draw a roundrect using a paint.
506 *
507 * @param paint describes how to color pixels.
508 * @param rrect the roundrect to draw
egdanield58a0ba2014-06-11 10:30:05 -0700509 * @param strokeInfo the stroke information (width, join, cap) and
510 * the dash information (intervals, count, phase).
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000511 */
egdanield58a0ba2014-06-11 10:30:05 -0700512 void drawRRect(const GrPaint& paint, const SkRRect& rrect, const GrStrokeInfo& strokeInfo);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000513
514 /**
515 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
516 * Does not support stroking. The result is undefined if outer does not contain
517 * inner.
518 *
519 * @param paint describes how to color pixels.
520 * @param outer the outer roundrect
521 * @param inner the inner roundrect
522 */
523 void drawDRRect(const GrPaint& paint, const SkRRect& outer, const SkRRect& inner);
524
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000525
526 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000527 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000528 *
529 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000530 * @param path the path to draw
egdanield58a0ba2014-06-11 10:30:05 -0700531 * @param strokeInfo the stroke information (width, join, cap) and
532 * the dash information (intervals, count, phase).
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533 */
egdanield58a0ba2014-06-11 10:30:05 -0700534 void drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo);
reed@google.com07f3ee12011-05-16 17:21:57 +0000535
bsalomon@google.com27847de2011-02-22 20:59:41 +0000536 /**
537 * Draws vertices with a paint.
538 *
539 * @param paint describes how to color pixels.
540 * @param primitiveType primitives type to draw.
541 * @param vertexCount number of vertices.
542 * @param positions array of vertex positions, required.
543 * @param texCoords optional array of texture coordinates used
544 * to access the paint.
545 * @param colors optional array of per-vertex colors, supercedes
546 * the paint's color field.
547 * @param indices optional array of indices. If NULL vertices
548 * are drawn non-indexed.
549 * @param indexCount if indices is non-null then this is the
550 * number of indices.
551 */
552 void drawVertices(const GrPaint& paint,
553 GrPrimitiveType primitiveType,
554 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000555 const SkPoint positions[],
556 const SkPoint texs[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 const GrColor colors[],
558 const uint16_t indices[],
559 int indexCount);
560
bsalomon@google.com93c96602012-04-27 13:05:21 +0000561 /**
562 * Draws an oval.
563 *
564 * @param paint describes how to color pixels.
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000565 * @param oval the bounding rect of the oval.
egdanield58a0ba2014-06-11 10:30:05 -0700566 * @param strokeInfo the stroke information (width, join, cap) and
567 * the dash information (intervals, count, phase).
bsalomon@google.com93c96602012-04-27 13:05:21 +0000568 */
569 void drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000570 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700571 const GrStrokeInfo& strokeInfo);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000572
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573 ///////////////////////////////////////////////////////////////////////////
574 // Misc.
575
576 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000577 * Flags that affect flush() behavior.
578 */
579 enum FlushBits {
580 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000581 * A client may reach a point where it has partially rendered a frame
582 * through a GrContext that it knows the user will never see. This flag
583 * causes the flush to skip submission of deferred content to the 3D API
584 * during the flush.
585 */
586 kDiscard_FlushBit = 0x2,
587 };
588
589 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590 * Call to ensure all drawing to the context has been issued to the
591 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000592 * @param flagsBitfield flags that control the flushing behavior. See
593 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000595 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000596
bsalomon@google.com0342a852012-08-20 19:22:38 +0000597 /**
598 * These flags can be used with the read/write pixels functions below.
599 */
600 enum PixelOpsFlags {
601 /** The GrContext will not be flushed. This means that the read or write may occur before
602 previous draws have executed. */
603 kDontFlush_PixelOpsFlag = 0x1,
604 /** The src for write or dst read is unpremultiplied. This is only respected if both the
605 config src and dst configs are an RGBA/BGRA 8888 format. */
606 kUnpremul_PixelOpsFlag = 0x2,
607 };
608
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000610 * Reads a rectangle of pixels from a render target.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000611 * @param target the render target to read from. NULL means the current render target.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000612 * @param left left edge of the rectangle to read (inclusive)
613 * @param top top edge of the rectangle to read (inclusive)
614 * @param width width of rectangle to read in pixels.
615 * @param height height of rectangle to read in pixels.
616 * @param config the pixel config of the destination buffer
617 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000618 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
619 * packed.
620 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000621 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000622 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
623 * pixel config or because no render target is currently set and NULL was passed for
624 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000625 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000626 bool readRenderTargetPixels(GrRenderTarget* target,
627 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000628 GrPixelConfig config, void* buffer,
629 size_t rowBytes = 0,
630 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000631
632 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000633 * Copy the src pixels [buffer, row bytes, pixel config] into a render target at the specified
bsalomon@google.com0342a852012-08-20 19:22:38 +0000634 * rectangle.
635 * @param target the render target to write into. NULL means the current render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000636 * @param left left edge of the rectangle to write (inclusive)
637 * @param top top edge of the rectangle to write (inclusive)
638 * @param width width of rectangle to write in pixels.
639 * @param height height of rectangle to write in pixels.
640 * @param config the pixel config of the source buffer
641 * @param buffer memory to read the rectangle from.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000642 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000643 * packed.
644 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000645 *
646 * @return true if the write succeeded, false if not. The write can fail because of an
647 * unsupported combination of target and pixel configs.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000648 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000649 bool writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000650 int left, int top, int width, int height,
651 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000652 size_t rowBytes = 0,
653 uint32_t pixelOpsFlags = 0);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000654
655 /**
656 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000657 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000658 * @param left left edge of the rectangle to read (inclusive)
659 * @param top top edge of the rectangle to read (inclusive)
660 * @param width width of rectangle to read in pixels.
661 * @param height height of rectangle to read in pixels.
662 * @param config the pixel config of the destination buffer
663 * @param buffer memory to read the rectangle into.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000664 * @param rowBytes number of bytes between consecutive rows. Zero means rows are tightly
bsalomon@google.com0342a852012-08-20 19:22:38 +0000665 * packed.
666 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000667 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000668 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
669 * pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000670 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000671 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000672 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000673 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000674 size_t rowBytes = 0,
675 uint32_t pixelOpsFlags = 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676
677 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000678 * Writes a rectangle of pixels to a texture.
679 * @param texture the render target to read from.
680 * @param left left edge of the rectangle to write (inclusive)
681 * @param top top edge of the rectangle to write (inclusive)
682 * @param width width of rectangle to write in pixels.
683 * @param height height of rectangle to write in pixels.
684 * @param config the pixel config of the source buffer
685 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000686 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000687 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000688 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000689 * @return true if the write succeeded, false if not. The write can fail because of an
690 * unsupported combination of texture and pixel configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000692 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000693 int left, int top, int width, int height,
694 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000695 size_t rowBytes,
696 uint32_t pixelOpsFlags = 0);
697
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000698 /**
scroggo@google.coma2a31922012-12-07 19:14:45 +0000699 * Copies a rectangle of texels from src to dst. The size of dst is the size of the rectangle
700 * copied and topLeft is the position of the rect in src. The rectangle is clipped to src's
701 * bounds.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000702 * @param src the texture to copy from.
703 * @param dst the render target to copy to.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000704 * @param topLeft the point in src that will be copied to the top-left of dst. If NULL,
705 * (0, 0) will be used.
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000706 */
scroggo@google.coma2a31922012-12-07 19:14:45 +0000707 void copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft = NULL);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000708
709 /**
710 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000711 * down-sampled to the associated GrTexture (accessible via
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000712 * GrRenderTarget::asTexture()). Any pending draws to the render target will
713 * be executed before the resolve.
714 *
715 * This is only necessary when a client wants to access the object directly
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000716 * using the backend API directly. GrContext will detect when it must
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000717 * perform a resolve to a GrTexture used as the source of a draw or before
718 * reading pixels back from a GrTexture or GrRenderTarget.
719 */
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000720 void resolveRenderTarget(GrRenderTarget*);
721
722 /**
723 * Provides a perfomance hint that the render target's contents are allowed
724 * to become undefined.
725 */
726 void discardRenderTarget(GrRenderTarget*);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000727
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000728#ifdef SK_DEVELOPER
729 void dumpFontCache() const;
730#endif
731
bsalomon@google.com27847de2011-02-22 20:59:41 +0000732 ///////////////////////////////////////////////////////////////////////////
733 // Helpers
734
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000735 class AutoRenderTarget : public ::SkNoncopyable {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000736 public:
737 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000738 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000739 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000740 context->setRenderTarget(target);
741 fContext = context;
742 }
743 AutoRenderTarget(GrContext* context) {
744 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000745 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000746 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000747 }
748 ~AutoRenderTarget() {
robertphillips@google.comccb39502012-10-01 18:25:13 +0000749 if (NULL != fContext) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000750 fContext->setRenderTarget(fPrevTarget);
751 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000752 SkSafeUnref(fPrevTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753 }
754 private:
755 GrContext* fContext;
756 GrRenderTarget* fPrevTarget;
757 };
758
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000759 /**
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000760 * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
761 * for a coordinate system change. Here is an example of how the paint param can be used:
762 *
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000763 * A GrPaint is setup with GrEffects. The stages will have access to the pre-matrix source
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000764 * geometry positions when the draw is executed. Later on a decision is made to transform the
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000765 * geometry to device space on the CPU. The effects now need to know that the space in which
766 * the geometry will be specified has changed.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000767 *
768 * Note that when restore is called (or in the destructor) the context's matrix will be
769 * restored. However, the paint will not be restored. The caller must make a copy of the
770 * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
771 * initialized.
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000772 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000773 class AutoMatrix : public ::SkNoncopyable {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000774 public:
775 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000776
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000777 ~AutoMatrix() { this->restore(); }
778
779 /**
780 * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
781 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000782 void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000783 SkASSERT(NULL != context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000784
785 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000786
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000787 fContext = context;
788 fMatrix = context->getMatrix();
789 this->preConcat(preConcat, paint);
790 }
791
792 /**
793 * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
794 * update a paint but the matrix cannot be inverted.
795 */
796 bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000797 SkASSERT(NULL != context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000798
799 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000800
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000801 if (NULL != paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000802 if (!paint->localCoordChangeInverse(context->getMatrix())) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000803 return false;
804 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000805 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000806 fMatrix = context->getMatrix();
807 fContext = context;
808 context->setIdentityMatrix();
809 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000810 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000811
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000812 /**
813 * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
814 * required to update a paint but the matrix cannot be inverted.
815 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000816 bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000817 if (NULL != paint) {
818 if (!this->setIdentity(context, paint)) {
819 return false;
820 }
821 this->preConcat(newMatrix, paint);
822 } else {
823 this->restore();
824 fContext = context;
825 fMatrix = context->getMatrix();
826 context->setMatrix(newMatrix);
827 }
828 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000829 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000830
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000831 /**
832 * If this has been initialized then the context's matrix will be further updated by
833 * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
834 * The paint is assumed to be relative to the context's matrix at the time this call is
835 * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
836 * performs an incremental update of the paint.
837 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000838 void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000839 if (NULL != paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000840 paint->localCoordChange(preConcat);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000841 }
842 fContext->concatMatrix(preConcat);
843 }
844
845 /**
846 * Returns false if never initialized or the inverse matrix was required to update a paint
847 * but the matrix could not be inverted.
848 */
849 bool succeeded() const { return NULL != fContext; }
850
851 /**
852 * If this has been initialized then the context's original matrix is restored.
853 */
854 void restore() {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000855 if (NULL != fContext) {
856 fContext->setMatrix(fMatrix);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000857 fContext = NULL;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000858 }
859 }
860
861 private:
862 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000863 SkMatrix fMatrix;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000864 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000866 class AutoClip : public ::SkNoncopyable {
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000867 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000868 // This enum exists to require a caller of the constructor to acknowledge that the clip will
869 // initially be wide open. It also could be extended if there are other desirable initial
870 // clip states.
871 enum InitialClip {
872 kWideOpen_InitialClip,
873 };
874
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000875 AutoClip(GrContext* context, InitialClip initialState)
robertphillips@google.comccb39502012-10-01 18:25:13 +0000876 : fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000877 SkASSERT(kWideOpen_InitialClip == initialState);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000878 fNewClipData.fClipStack = &fNewClipStack;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000879
880 fOldClip = context->getClip();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000881 context->setClip(&fNewClipData);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000882 }
883
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000884 AutoClip(GrContext* context, const SkRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000885 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000886 , fNewClipStack(newClipRect) {
887 fNewClipData.fClipStack = &fNewClipStack;
888
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000889 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000890 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000891 }
892
893 ~AutoClip() {
894 if (NULL != fContext) {
895 fContext->setClip(fOldClip);
896 }
897 }
898 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000899 GrContext* fContext;
900 const GrClipData* fOldClip;
901
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000902 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000903 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000904 };
905
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000906 class AutoWideOpenIdentityDraw {
907 public:
908 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
909 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000910 , fAutoRT(ctx, rt) {
911 fAutoMatrix.setIdentity(ctx);
912 // should never fail with no paint param.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000913 SkASSERT(fAutoMatrix.succeeded());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000914 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000915
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000916 private:
917 AutoClip fAutoClip;
918 AutoRenderTarget fAutoRT;
919 AutoMatrix fAutoMatrix;
920 };
921
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 ///////////////////////////////////////////////////////////////////////////
923 // Functions intended for internal use only.
924 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000925 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926 GrFontCache* getFontCache() { return fFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000927 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000928 GrDrawTarget* getTextTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 const GrIndexBuffer* getQuadIndexBuffer() const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000930 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000931
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000932 // Called by tests that draw directly to the context via GrDrawTarget
933 void getTestTarget(GrTestTarget*);
934
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000935 // Functions for managing gpu trace markers
936 bool isGpuTracingEnabled() const { return fGpuTracingEnabled; }
937 void enableGpuTracing() { fGpuTracingEnabled = true; }
938 void disableGpuTracing() { fGpuTracingEnabled = false; }
939
egdanielbbcb38d2014-06-19 10:19:29 -0700940 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
941 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
942
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000943 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000944 * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
945 * called to check the cache for a SB that matches an RT's criteria.
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000946 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000947 void addStencilBuffer(GrStencilBuffer* sb);
948 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000950 GrPathRenderer* getPathRenderer(
951 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000952 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000953 const GrDrawTarget* target,
954 bool allowSW,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000955 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000956 GrPathRendererChain::StencilSupport* stencilSupport = NULL);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000957
robertphillips@google.com59552022012-08-31 13:07:37 +0000958#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000959 void printCacheStats() const;
960#endif
961
bsalomon@google.com27847de2011-02-22 20:59:41 +0000962private:
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000963 // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
964 enum BufferedDraw {
965 kYes_BufferedDraw,
966 kNo_BufferedDraw,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967 };
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000968 BufferedDraw fLastDrawWasBuffered;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000969
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000970 GrGpu* fGpu;
971 SkMatrix fViewMatrix;
972 SkAutoTUnref<GrRenderTarget> fRenderTarget;
973 const GrClipData* fClip; // TODO: make this ref counted
974 GrDrawState* fDrawState;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000975
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000976 GrResourceCache* fResourceCache;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000977 GrFontCache* fFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000978 SkAutoTDelete<GrLayerCache> fLayerCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000979
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000980 GrPathRendererChain* fPathRendererChain;
981 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000983 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
984 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
985 GrInOrderDrawBuffer* fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000986
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000987 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
988 bool fFlushToReduceCacheSize;
989
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000990 GrAARectRenderer* fAARectRenderer;
991 GrOvalRenderer* fOvalRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000992
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000993 bool fDidTestPMConversions;
994 int fPMToUPMConversion;
995 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000996
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000997 struct CleanUpData {
998 PFCleanUpFunc fFunc;
999 void* fInfo;
1000 };
1001
robertphillips@google.com44a91dc2013-07-25 15:32:06 +00001002 SkTDArray<CleanUpData> fCleanUpData;
1003
skia.committer@gmail.com956b3102013-07-26 07:00:58 +00001004 int fMaxTextureSizeOverride;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +00001005
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001006 bool fGpuTracingEnabled;
1007
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001008 GrContext(); // init must be called after the constructor.
1009 bool init(GrBackend, GrBackendContext);
bsalomon@google.com205d4602011-04-25 12:43:45 +00001010
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001011 void setupDrawBuffer();
1012
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001013 class AutoRestoreEffects;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001014 class AutoCheckFlush;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001015 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
1016 /// draw state is left unmodified.
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001017 GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw, AutoRestoreEffects*, AutoCheckFlush*);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001018
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +00001019 void internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001020 const GrStrokeInfo& stroke);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001021
robertphillips@google.com3319f332012-08-13 18:00:36 +00001022 GrTexture* createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001023 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +00001024 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +00001025 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +00001026 bool filter);
robertphillips@google.com3319f332012-08-13 18:00:36 +00001027
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001028 // Needed so GrTexture's returnToCache helper function can call
1029 // addExistingTextureToCache
1030 friend class GrTexture;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001031 friend class GrStencilAndCoverPathRenderer;
kkinnunenc6cb56f2014-06-24 00:12:27 -07001032 friend class GrStencilAndCoverTextContext;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001033
1034 // Add an existing texture to the texture cache. This is intended solely
1035 // for use with textures released from an GrAutoScratchTexture.
1036 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +00001037
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +00001038 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +00001039 * These functions create premul <-> unpremul effects if it is possible to generate a pair
1040 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
1041 * return NULL.
1042 */
1043 const GrEffectRef* createPMToUPMEffect(GrTexture* texture,
1044 bool swapRAndB,
1045 const SkMatrix& matrix);
1046 const GrEffectRef* createUPMToPMEffect(GrTexture* texture,
1047 bool swapRAndB,
1048 const SkMatrix& matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001049
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +00001050 /**
1051 * This callback allows the resource cache to callback into the GrContext
1052 * when the cache is still overbudget after a purge.
1053 */
1054 static bool OverbudgetCB(void* data);
1055
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001056 /** Creates a new gpu path, based on the specified path and stroke and returns it.
1057 * The caller owns a ref on the returned path which must be balanced by a call to unref.
1058 *
1059 * @param skPath the path geometry.
1060 * @param stroke the path stroke.
1061 * @return a new path or NULL if the operation is not supported by the backend.
1062 */
1063 GrPath* createPath(const SkPath& skPath, const SkStrokeRec& stroke);
1064
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +00001065 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001066};
1067
1068/**
bsalomon@google.com1e269b52012-10-15 14:25:31 +00001069 * Gets and locks a scratch texture from a descriptor using either exact or approximate criteria.
1070 * Unlocks texture in the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001071 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +00001072class GrAutoScratchTexture : public ::SkNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001073public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001074 GrAutoScratchTexture()
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001075 : fContext(NULL)
1076 , fTexture(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001077 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001078
1079 GrAutoScratchTexture(GrContext* context,
1080 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001081 GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch)
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001082 : fContext(NULL)
1083 , fTexture(NULL) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001084 this->set(context, desc, match);
1085 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001086
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001087 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +00001088 this->reset();
1089 }
1090
1091 void reset() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001092 if (NULL != fContext && NULL != fTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +00001093 fContext->unlockScratchTexture(fTexture);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001094 fTexture->unref();
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001095 fTexture = NULL;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001096 }
1097 }
bsalomon@google.com84223112011-07-14 14:45:44 +00001098
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001099 /*
1100 * When detaching a texture we do not unlock it in the texture cache but
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001101 * we do set the returnToCache flag. In this way the texture remains
1102 * "locked" in the texture cache until it is freed and recycled in
1103 * GrTexture::internal_dispose. In reality, the texture has been removed
1104 * from the cache (because this is in AutoScratchTexture) and by not
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001105 * calling unlockScratchTexture we simply don't re-add it. It will be
1106 * reattached in GrTexture::internal_dispose.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001107 *
1108 * Note that the caller is assumed to accept and manage the ref to the
1109 * returned texture.
1110 */
1111 GrTexture* detach() {
commit-bot@chromium.org3f4ea262013-05-11 13:21:43 +00001112 if (NULL == fTexture) {
1113 return NULL;
1114 }
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001115 GrTexture* texture = fTexture;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001116 fTexture = NULL;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001117
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001118 // This GrAutoScratchTexture has a ref from lockAndRefScratchTexture, which we give up now.
1119 // The cache also has a ref which we are lending to the caller of detach(). When the caller
1120 // lets go of the ref and the ref count goes to 0 internal_dispose will see this flag is
1121 // set and re-ref the texture, thereby restoring the cache's ref.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001122 SkASSERT(texture->getRefCnt() > 1);
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +00001123 texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001124 texture->unref();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001125 SkASSERT(NULL != texture->getCacheEntry());
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001126
1127 return texture;
robertphillips@google.com15c0fea2012-06-22 12:41:43 +00001128 }
1129
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001130 GrTexture* set(GrContext* context,
1131 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001132 GrContext::ScratchTexMatch match = GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +00001133 this->reset();
1134
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001135 fContext = context;
1136 if (NULL != fContext) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001137 fTexture = fContext->lockAndRefScratchTexture(desc, match);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001138 if (NULL == fTexture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001139 fContext = NULL;
1140 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001141 return fTexture;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001142 } else {
1143 return NULL;
1144 }
1145 }
1146
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001147 GrTexture* texture() { return fTexture; }
1148
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001149private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001150 GrContext* fContext;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001151 GrTexture* fTexture;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +00001152};
1153
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154#endif