blob: c6dd2d75631af38fdef00e2a933c144e11b90680 [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;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000022class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000023class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000024class GrFontCache;
joshualittb0a8a372014-09-23 09:50:21 -070025class GrFragmentProcessor;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000026class GrGpu;
egdanielbbcb38d2014-06-19 10:19:29 -070027class GrGpuTraceMarker;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000029class GrIndexBufferAllocPool;
30class GrInOrderDrawBuffer;
robertphillips@google.come930a072014-04-03 00:34:27 +000031class GrLayerCache;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000032class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000033class GrPath;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000034class GrPathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035class GrResourceEntry;
bsalomonc8dc1f72014-08-21 13:02:13 -070036class GrResourceCache2;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000037class GrTestTarget;
kkinnunenc6cb56f2014-06-24 00:12:27 -070038class GrTextContext;
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;
egdanield58a0ba2014-06-11 10:30:05 -070042class GrStrokeInfo;
robertphillips@google.com72176b22012-05-23 13:19:12 +000043class GrSoftwarePathRenderer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000044class SkStrokeRec;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000045
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000046class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000047public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000048 SK_DECLARE_INST_COUNT(GrContext)
49
krajcevski9c6d4d72014-08-12 07:26:25 -070050 struct Options {
51 Options() : fDrawPathToCompressedTexture(false) { }
52
53 // EXPERIMENTAL
54 // May be removed in the future, or may become standard depending
55 // on the outcomes of a variety of internal tests.
56 bool fDrawPathToCompressedTexture;
57 };
58
bsalomon@google.com27847de2011-02-22 20:59:41 +000059 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000060 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000061 */
krajcevski9c6d4d72014-08-12 07:26:25 -070062 static GrContext* Create(GrBackend, GrBackendContext, const Options* opts = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +000063
bsalomon33435572014-11-05 14:47:41 -080064 /**
65 * Only defined in test apps.
66 */
67 static GrContext* CreateMockContext();
68
bsalomon@google.com27847de2011-02-22 20:59:41 +000069 virtual ~GrContext();
70
71 /**
72 * The GrContext normally assumes that no outsider is setting state
73 * within the underlying 3D API's context/device/whatever. This call informs
74 * the context that the state was modified and it should resend. Shouldn't
75 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000076 * The flag bits, state, is dpendent on which backend is used by the
77 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000079 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000080
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000082 * Callback function to allow classes to cleanup on GrContext destruction.
83 * The 'info' field is filled in with the 'info' passed to addCleanUp.
84 */
85 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
86
87 /**
88 * Add a function to be called from within GrContext's destructor.
89 * This gives classes a chance to free resources held on a per context basis.
90 * The 'info' parameter will be stored and passed to the callback function.
91 */
92 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
93 CleanUpData* entry = fCleanUpData.push();
94
95 entry->fFunc = cleanUp;
96 entry->fInfo = info;
97 }
98
99 /**
bsalomon2354f842014-07-28 13:48:36 -0700100 * Abandons all GPU resources and assumes the underlying backend 3D API
101 * context is not longer usable. Call this if you have lost the associated
102 * GPU context, and thus internal texture, buffer, etc. references/IDs are
103 * now invalid. Should be called even when GrContext is no longer going to
104 * be used for two reasons:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000105 * 1) ~GrContext will not try to free the objects in the 3D API.
bsalomon2354f842014-07-28 13:48:36 -0700106 * 2) Any GrGpuResources created by this GrContext that outlive
107 * will be marked as invalid (GrGpuResource::wasDestroyed()) and
108 * when they're destroyed no 3D API calls will be made.
109 * Content drawn since the last GrContext::flush() may be lost. After this
110 * function is called the only valid action on the GrContext or
111 * GrGpuResources it created is to destroy them.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112 */
bsalomon2354f842014-07-28 13:48:36 -0700113 void abandonContext();
114 void contextDestroyed() { this->abandonContext(); } // legacy alias
junov@google.com53a55842011-06-08 22:55:10 +0000115
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000116 ///////////////////////////////////////////////////////////////////////////
117 // Resource Cache
118
119 /**
120 * Return the current GPU resource cache limits.
121 *
122 * @param maxResources If non-null, returns maximum number of resources that
123 * can be held in the cache.
124 * @param maxResourceBytes If non-null, returns maximum number of bytes of
125 * video memory that can be held in the cache.
126 */
127 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000128
129 /**
130 * Gets the current GPU resource cache usage.
131 *
132 * @param resourceCount If non-null, returns the number of resources that are held in the
133 * cache.
134 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
135 * in the cache.
136 */
137 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
138
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000139 /**
140 * Specify the GPU resource cache limits. If the current cache exceeds either
141 * of these, it will be purged (LRU) to keep the cache within these limits.
142 *
143 * @param maxResources The maximum number of resources that can be held in
144 * the cache.
145 * @param maxResourceBytes The maximum number of bytes of video memory
146 * that can be held in the cache.
147 */
148 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000149
junov@google.com53a55842011-06-08 22:55:10 +0000150 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000151 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000152 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000153 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154 void freeGpuResources();
155
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000156 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000157 * This method should be called whenever a GrResource is unreffed or
158 * switched from exclusive to non-exclusive. This
159 * gives the resource cache a chance to discard unneeded resources.
160 * Note: this entry point will be removed once totally ref-driven
161 * cache maintenance is implemented.
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000162 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000163 void purgeCache();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000164
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000165 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000166 * Purge all the unlocked resources from the cache.
167 * This entry point is mainly meant for timing texture uploads
168 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000169 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000170 void purgeAllUnlockedResources();
171
172 /**
173 * Stores a custom resource in the cache, based on the specified key.
174 */
bsalomon6d3fe022014-07-25 08:35:45 -0700175 void addResourceToCache(const GrResourceKey&, GrGpuResource*);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000176
177 /**
178 * Finds a resource in the cache, based on the specified key. This is intended for use in
179 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
180 * caller must balance with a call to unref().
181 */
bsalomon6d3fe022014-07-25 08:35:45 -0700182 GrGpuResource* findAndRefCachedResource(const GrResourceKey&);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000183
kkinnunenc6cb56f2014-06-24 00:12:27 -0700184 /**
185 * Creates a new text rendering context that is optimal for the
186 * render target and the context. Caller assumes the ownership
187 * of the returned object. The returned object must be deleted
188 * before the context is destroyed.
189 */
190 GrTextContext* createTextContext(GrRenderTarget*,
191 const SkDeviceProperties&,
192 bool enableDistanceFieldFonts);
193
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000194 ///////////////////////////////////////////////////////////////////////////
195 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +0000196
197 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000198 * Creates a new entry, based on the specified key and texture and returns it. The caller owns a
199 * ref on the returned texture which must be balanced by a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000200 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000201 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000202 * the cache entry used. (e.g. different versions may exist
203 * for different wrap modes on GPUs with limited NPOT
204 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000205 * @param desc Description of the texture properties.
krajcevski9c0e6292014-06-02 07:38:14 -0700206 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000207 * @param srcData Pointer to the pixel values.
208 * @param rowBytes The number of bytes between rows of the texture. Zero
krajcevski9c0e6292014-06-02 07:38:14 -0700209 * implies tightly packed rows. For compressed pixel configs, this
210 * field is ignored.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000211 * @param cacheKey (optional) If non-NULL, we'll write the cache key we used to cacheKey.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000212 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000213 GrTexture* createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700214 const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000215 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000216 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000217 size_t rowBytes,
218 GrResourceKey* cacheKey = NULL);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000219 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000220 * Search for an entry based on key and dimensions. If found, ref it and return it. The return
221 * value will be NULL if not found. The caller must balance with a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000222 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000223 * @param desc Description of the texture properties.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000224 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000225 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000226 * the cache entry used. (e.g. different versions may exist
227 * for different wrap modes on GPUs with limited NPOT
228 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000229 */
bsalomonf2703d82014-10-28 14:33:06 -0700230 GrTexture* findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000231 const GrCacheID& cacheID,
232 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000233 /**
234 * Determines whether a texture is in the cache. If the texture is found it
235 * will not be locked or returned. This call does not affect the priority of
236 * the texture for deletion.
237 */
bsalomonf2703d82014-10-28 14:33:06 -0700238 bool isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000239 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000240 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000241
242 /**
243 * Enum that determines how closely a returned scratch texture must match
bsalomonf2703d82014-10-28 14:33:06 -0700244 * a provided GrSurfaceDesc.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000245 */
246 enum ScratchTexMatch {
247 /**
248 * Finds a texture that exactly matches the descriptor.
249 */
250 kExact_ScratchTexMatch,
251 /**
252 * Finds a texture that approximately matches the descriptor. Will be
253 * at least as large in width and height as desc specifies. If desc
254 * specifies that texture is a render target then result will be a
255 * render target. If desc specifies a render target and doesn't set the
256 * no stencil flag then result will have a stencil. Format and aa level
257 * will always match.
258 */
259 kApprox_ScratchTexMatch
260 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000261
262 /**
bsalomone3059732014-10-14 11:47:22 -0700263 * Returns a texture matching the desc. It's contents are unknown. The caller
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000264 * owns a ref on the returned texture and must balance with a call to unref.
bsalomone3059732014-10-14 11:47:22 -0700265 * It is guaranteed that the same texture will not be returned in subsequent
266 * calls until all refs to the texture are dropped.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000267 *
bsalomone3059732014-10-14 11:47:22 -0700268 * Textures created by createTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000269 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomone3059732014-10-14 11:47:22 -0700270 * unextended GLES2). NPOT scratch textures are not tilable on such APIs.
bsalomonbcf0a522014-10-08 08:40:09 -0700271 *
272 * internalFlag is a temporary workaround until changes in the internal
273 * architecture are complete. Use the default value.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000274 */
bsalomonf2703d82014-10-28 14:33:06 -0700275 GrTexture* refScratchTexture(const GrSurfaceDesc&, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700276 bool internalFlag = false);
robertphillipsdbe60742014-09-30 06:54:17 -0700277
278 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000279 * Creates a texture that is outside the cache. Does not count against
280 * cache's budget.
bsalomone3059732014-10-14 11:47:22 -0700281 *
282 * Textures created by createTexture() hide the complications of
283 * tiling non-power-of-two textures on APIs that don't support this (e.g.
284 * unextended GLES2). NPOT uncached textures are not tilable on such APIs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000285 */
bsalomonf2703d82014-10-28 14:33:06 -0700286 GrTexture* createUncachedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000287 void* srcData,
288 size_t rowBytes);
289
290 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000291 * Returns true if the specified use of an indexed texture is supported.
292 * Support may depend upon whether the texture params indicate that the
293 * texture will be tiled. Passing NULL for the texture params indicates
294 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000296 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000297 int width,
298 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000299
300 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000301 * Return the max width or height of a texture supported by the current GPU.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000302 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000303 int getMaxTextureSize() const;
304
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000305 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000306 * Temporarily override the true max texture size. Note: an override
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000307 * larger then the true max texture size will have no effect.
308 * This entry point is mainly meant for testing texture size dependent
309 * features and is only available if defined outside of Skia (see
310 * bleed GM.
311 */
312 void setMaxTextureSizeOverride(int maxTextureSizeOverride);
313
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314 ///////////////////////////////////////////////////////////////////////////
315 // Render targets
316
317 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000318 * Sets the render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000319 * @param target the render target to set.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000320 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000321 void setRenderTarget(GrRenderTarget* target) {
322 fRenderTarget.reset(SkSafeRef(target));
323 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000324
325 /**
326 * Gets the current render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000327 * @return the currently bound render target.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000328 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000329 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
330 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000331
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000332 /**
333 * Can the provided configuration act as a color render target?
334 */
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000335 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000336
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000337 /**
338 * Return the max width or height of a render target supported by the
339 * current GPU.
340 */
341 int getMaxRenderTargetSize() const;
342
343 /**
344 * Returns the max sample count for a render target. It will be 0 if MSAA
345 * is not supported.
346 */
347 int getMaxSampleCount() const;
348
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000349 /**
350 * Returns the recommended sample count for a render target when using this
351 * context.
352 *
353 * @param config the configuration of the render target.
354 * @param dpi the display density in dots per inch.
355 *
356 * @return sample count that should be perform well and have good enough
357 * rendering quality for the display. Alternatively returns 0 if
358 * MSAA is not supported or recommended to be used by default.
359 */
360 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
361
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000362 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000363 // Backend Surfaces
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000364
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000365 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000366 * Wraps an existing texture with a GrTexture object.
367 *
368 * OpenGL: if the object is a texture Gr may change its GL texture params
369 * when it is drawn.
370 *
371 * @param desc description of the object to create.
372 *
373 * @return GrTexture object or NULL on failure.
374 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000375 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000376
377 /**
378 * Wraps an existing render target with a GrRenderTarget object. It is
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000379 * similar to wrapBackendTexture but can be used to draw into surfaces
bsalomon@google.come269f212011-11-07 13:29:52 +0000380 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
381 * the client will resolve to a texture).
382 *
383 * @param desc description of the object to create.
384 *
385 * @return GrTexture object or NULL on failure.
386 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000387 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000388
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389 ///////////////////////////////////////////////////////////////////////////
390 // Matrix state
391
392 /**
393 * Gets the current transformation matrix.
394 * @return the current matrix.
395 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000396 const SkMatrix& getMatrix() const { return fViewMatrix; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397
398 /**
399 * Sets the transformation matrix.
400 * @param m the matrix to set.
401 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000402 void setMatrix(const SkMatrix& m) { fViewMatrix = m; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403
404 /**
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000405 * Sets the current transformation matrix to identity.
406 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000407 void setIdentityMatrix() { fViewMatrix.reset(); }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000408
409 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000410 * Concats the current matrix. The passed matrix is applied before the
411 * current matrix.
412 * @param m the matrix to concat.
413 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000414 void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415
416
417 ///////////////////////////////////////////////////////////////////////////
418 // Clip state
419 /**
420 * Gets the current clip.
421 * @return the current clip.
422 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000423 const GrClipData* getClip() const { return fClip; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000424
425 /**
426 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000427 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000428 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000429 void setClip(const GrClipData* clipData) { fClip = clipData; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430
bsalomon@google.com27847de2011-02-22 20:59:41 +0000431 ///////////////////////////////////////////////////////////////////////////
432 // Draws
433
434 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000435 * Clear the entire or rect of the render target, ignoring any clips.
436 * @param rect the rect to clear or the whole thing if rect is NULL.
437 * @param color the color to clear to.
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000438 * @param canIgnoreRect allows partial clears to be converted to whole
439 * clears on platforms for which that is cheap
bsalomon89c62982014-11-03 12:08:42 -0800440 * @param target The render target to clear.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000441 */
bsalomon89c62982014-11-03 12:08:42 -0800442 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect, GrRenderTarget* target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443
444 /**
445 * Draw everywhere (respecting the clip) with the paint.
446 */
447 void drawPaint(const GrPaint& paint);
448
449 /**
450 * Draw the rect using a paint.
451 * @param paint describes how to color pixels.
egdanield58a0ba2014-06-11 10:30:05 -0700452 * @param strokeInfo the stroke information (width, join, cap), and.
453 * the dash information (intervals, count, phase).
454 * If strokeInfo == NULL, then the rect is filled.
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000455 * Otherwise, if stroke width == 0, then the stroke
skia.committer@gmail.com26144182013-11-07 07:02:19 +0000456 * is always a single pixel thick, else the rect is
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000457 * mitered/beveled stroked based on stroke width.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000458 * The rects coords are used to access the paint (through texture matrix)
459 */
460 void drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000461 const SkRect&,
bsalomon01c8da12014-08-04 09:21:30 -0700462 const GrStrokeInfo* strokeInfo = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000463
464 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000465 * Maps a rect of local coordinates onto the a rect of destination
bsalomon01c8da12014-08-04 09:21:30 -0700466 * coordinates. The localRect is stretched over the dstRect. The dstRect is
467 * transformed by the context's matrix. An additional optional matrix can be
468 * provided to transform the local rect.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000469 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000470 * @param paint describes how to color pixels.
471 * @param dstRect the destination rect to draw.
472 * @param localRect rect of local coordinates to be mapped onto dstRect
bsalomon@google.comc7818882013-03-20 19:19:53 +0000473 * @param localMatrix Optional matrix to transform localRect.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474 */
475 void drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000476 const SkRect& dstRect,
477 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000478 const SkMatrix* localMatrix = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000479
480 /**
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000481 * Draw a roundrect using a paint.
482 *
483 * @param paint describes how to color pixels.
484 * @param rrect the roundrect to draw
egdanield58a0ba2014-06-11 10:30:05 -0700485 * @param strokeInfo the stroke information (width, join, cap) and
486 * the dash information (intervals, count, phase).
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000487 */
egdanield58a0ba2014-06-11 10:30:05 -0700488 void drawRRect(const GrPaint& paint, const SkRRect& rrect, const GrStrokeInfo& strokeInfo);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000489
490 /**
491 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
492 * Does not support stroking. The result is undefined if outer does not contain
493 * inner.
494 *
495 * @param paint describes how to color pixels.
496 * @param outer the outer roundrect
497 * @param inner the inner roundrect
498 */
499 void drawDRRect(const GrPaint& paint, const SkRRect& outer, const SkRRect& inner);
500
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000501
502 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000503 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000504 *
505 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000506 * @param path the path to draw
egdanield58a0ba2014-06-11 10:30:05 -0700507 * @param strokeInfo the stroke information (width, join, cap) and
508 * the dash information (intervals, count, phase).
bsalomon@google.com27847de2011-02-22 20:59:41 +0000509 */
egdanield58a0ba2014-06-11 10:30:05 -0700510 void drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo);
reed@google.com07f3ee12011-05-16 17:21:57 +0000511
bsalomon@google.com27847de2011-02-22 20:59:41 +0000512 /**
513 * Draws vertices with a paint.
514 *
515 * @param paint describes how to color pixels.
516 * @param primitiveType primitives type to draw.
517 * @param vertexCount number of vertices.
518 * @param positions array of vertex positions, required.
519 * @param texCoords optional array of texture coordinates used
520 * to access the paint.
521 * @param colors optional array of per-vertex colors, supercedes
522 * the paint's color field.
523 * @param indices optional array of indices. If NULL vertices
524 * are drawn non-indexed.
525 * @param indexCount if indices is non-null then this is the
526 * number of indices.
527 */
528 void drawVertices(const GrPaint& paint,
529 GrPrimitiveType primitiveType,
530 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000531 const SkPoint positions[],
532 const SkPoint texs[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533 const GrColor colors[],
534 const uint16_t indices[],
535 int indexCount);
536
bsalomon@google.com93c96602012-04-27 13:05:21 +0000537 /**
538 * Draws an oval.
539 *
540 * @param paint describes how to color pixels.
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000541 * @param oval the bounding rect of the oval.
egdanield58a0ba2014-06-11 10:30:05 -0700542 * @param strokeInfo the stroke information (width, join, cap) and
543 * the dash information (intervals, count, phase).
bsalomon@google.com93c96602012-04-27 13:05:21 +0000544 */
545 void drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000546 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700547 const GrStrokeInfo& strokeInfo);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000548
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549 ///////////////////////////////////////////////////////////////////////////
550 // Misc.
551
552 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000553 * Flags that affect flush() behavior.
554 */
555 enum FlushBits {
556 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000557 * A client may reach a point where it has partially rendered a frame
558 * through a GrContext that it knows the user will never see. This flag
559 * causes the flush to skip submission of deferred content to the 3D API
560 * during the flush.
561 */
562 kDiscard_FlushBit = 0x2,
563 };
564
565 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566 * Call to ensure all drawing to the context has been issued to the
567 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000568 * @param flagsBitfield flags that control the flushing behavior. See
569 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000571 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000572
bsalomon@google.com0342a852012-08-20 19:22:38 +0000573 /**
574 * These flags can be used with the read/write pixels functions below.
575 */
576 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700577 /** The GrContext will not be flushed before the surface read or write. This means that
578 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000579 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700580 /** Any surface writes should be flushed to the backend 3D API after the surface operation
581 is complete */
582 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000583 /** The src for write or dst read is unpremultiplied. This is only respected if both the
584 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700585 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000586 };
587
bsalomon@google.com27847de2011-02-22 20:59:41 +0000588 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000589 * Reads a rectangle of pixels from a render target.
bsalomon89c62982014-11-03 12:08:42 -0800590 * @param target the render target to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000591 * @param left left edge of the rectangle to read (inclusive)
592 * @param top top edge of the rectangle to read (inclusive)
593 * @param width width of rectangle to read in pixels.
594 * @param height height of rectangle to read in pixels.
595 * @param config the pixel config of the destination buffer
596 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000597 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
598 * packed.
599 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000600 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000601 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
602 * pixel config or because no render target is currently set and NULL was passed for
603 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000605 bool readRenderTargetPixels(GrRenderTarget* target,
606 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000607 GrPixelConfig config, void* buffer,
608 size_t rowBytes = 0,
609 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000610
611 /**
bsalomon81beccc2014-10-13 12:32:55 -0700612 * Writes a rectangle of pixels to a surface.
613 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000614 * @param left left edge of the rectangle to write (inclusive)
615 * @param top top edge of the rectangle to write (inclusive)
616 * @param width width of rectangle to write in pixels.
617 * @param height height of rectangle to write in pixels.
618 * @param config the pixel config of the source buffer
619 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000620 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000621 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000622 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000623 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700624 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000625 */
bsalomon81beccc2014-10-13 12:32:55 -0700626 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000627 int left, int top, int width, int height,
628 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000629 size_t rowBytes,
630 uint32_t pixelOpsFlags = 0);
631
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000632 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700633 * Copies a rectangle of texels from src to dst.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000634 * bounds.
bsalomonf80bfed2014-10-07 05:56:02 -0700635 * @param dst the surface to copy to.
636 * @param src the surface to copy from.
637 * @param srcRect the rectangle of the src that should be copied.
638 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
639 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000640 */
bsalomonf80bfed2014-10-07 05:56:02 -0700641 void copySurface(GrSurface* dst,
642 GrSurface* src,
643 const SkIRect& srcRect,
644 const SkIPoint& dstPoint,
645 uint32_t pixelOpsFlags = 0);
646
647 /** Helper that copies the whole surface but fails when the two surfaces are not identically
648 sized. */
649 bool copySurface(GrSurface* dst, GrSurface* src) {
650 if (NULL == dst || NULL == src || dst->width() != src->width() ||
651 dst->height() != src->height()) {
652 return false;
653 }
654 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
655 SkIPoint::Make(0,0));
656 return true;
657 }
658
659 /**
660 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
661 */
662 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000663
664 /**
bsalomon87a94eb2014-11-03 14:28:32 -0800665 * Equivalent to flushSurfaceWrites but also performs MSAA resolve if necessary. This call is
666 * used to make the surface contents available to be read in the backend 3D API, usually for a
667 * compositing step external to Skia.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000668 *
bsalomon87a94eb2014-11-03 14:28:32 -0800669 * It is not necessary to call this before reading the render target via Skia/GrContext.
670 * GrContext will detect when it must perform a resolve before reading pixels back from the
671 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000672 */
bsalomon87a94eb2014-11-03 14:28:32 -0800673 void prepareSurfaceForExternalRead(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000674
675 /**
676 * Provides a perfomance hint that the render target's contents are allowed
677 * to become undefined.
678 */
679 void discardRenderTarget(GrRenderTarget*);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000680
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000681#ifdef SK_DEVELOPER
682 void dumpFontCache() const;
683#endif
684
bsalomon@google.com27847de2011-02-22 20:59:41 +0000685 ///////////////////////////////////////////////////////////////////////////
686 // Helpers
687
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000688 class AutoRenderTarget : public ::SkNoncopyable {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000689 public:
690 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000692 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000693 context->setRenderTarget(target);
694 fContext = context;
695 }
696 AutoRenderTarget(GrContext* context) {
697 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000698 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000699 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000700 }
701 ~AutoRenderTarget() {
bsalomon49f085d2014-09-05 13:34:00 -0700702 if (fContext) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000703 fContext->setRenderTarget(fPrevTarget);
704 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000705 SkSafeUnref(fPrevTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000706 }
707 private:
708 GrContext* fContext;
709 GrRenderTarget* fPrevTarget;
710 };
711
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000712 /**
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000713 * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
714 * for a coordinate system change. Here is an example of how the paint param can be used:
715 *
joshualittb0a8a372014-09-23 09:50:21 -0700716 * A GrPaint is setup with GrProcessors. The stages will have access to the pre-matrix source
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000717 * geometry positions when the draw is executed. Later on a decision is made to transform the
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000718 * geometry to device space on the CPU. The effects now need to know that the space in which
719 * the geometry will be specified has changed.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000720 *
721 * Note that when restore is called (or in the destructor) the context's matrix will be
722 * restored. However, the paint will not be restored. The caller must make a copy of the
723 * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
724 * initialized.
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000725 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000726 class AutoMatrix : public ::SkNoncopyable {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000727 public:
728 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000729
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000730 ~AutoMatrix() { this->restore(); }
731
732 /**
733 * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
734 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000735 void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700736 SkASSERT(context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000737
738 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000739
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000740 fContext = context;
741 fMatrix = context->getMatrix();
742 this->preConcat(preConcat, paint);
743 }
744
745 /**
746 * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
747 * update a paint but the matrix cannot be inverted.
748 */
749 bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700750 SkASSERT(context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000751
752 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000753
bsalomon49f085d2014-09-05 13:34:00 -0700754 if (paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000755 if (!paint->localCoordChangeInverse(context->getMatrix())) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000756 return false;
757 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000758 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000759 fMatrix = context->getMatrix();
760 fContext = context;
761 context->setIdentityMatrix();
762 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000763 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000764
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000765 /**
766 * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
767 * required to update a paint but the matrix cannot be inverted.
768 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000769 bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700770 if (paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000771 if (!this->setIdentity(context, paint)) {
772 return false;
773 }
774 this->preConcat(newMatrix, paint);
775 } else {
776 this->restore();
777 fContext = context;
778 fMatrix = context->getMatrix();
779 context->setMatrix(newMatrix);
780 }
781 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000782 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000783
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000784 /**
785 * If this has been initialized then the context's matrix will be further updated by
786 * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
787 * The paint is assumed to be relative to the context's matrix at the time this call is
788 * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
789 * performs an incremental update of the paint.
790 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000791 void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700792 if (paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000793 paint->localCoordChange(preConcat);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000794 }
795 fContext->concatMatrix(preConcat);
796 }
797
798 /**
799 * Returns false if never initialized or the inverse matrix was required to update a paint
800 * but the matrix could not be inverted.
801 */
bsalomon49f085d2014-09-05 13:34:00 -0700802 bool succeeded() const { return SkToBool(fContext); }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000803
804 /**
805 * If this has been initialized then the context's original matrix is restored.
806 */
807 void restore() {
bsalomon49f085d2014-09-05 13:34:00 -0700808 if (fContext) {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000809 fContext->setMatrix(fMatrix);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000810 fContext = NULL;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000811 }
812 }
813
814 private:
815 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000816 SkMatrix fMatrix;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000817 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000819 class AutoClip : public ::SkNoncopyable {
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000820 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000821 // This enum exists to require a caller of the constructor to acknowledge that the clip will
822 // initially be wide open. It also could be extended if there are other desirable initial
823 // clip states.
824 enum InitialClip {
825 kWideOpen_InitialClip,
826 };
827
djsollenc87dd2c2014-11-14 11:11:46 -0800828 AutoClip(GrContext* context, InitialClip SkDEBUGCODE(initialState))
robertphillips@google.comccb39502012-10-01 18:25:13 +0000829 : fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000830 SkASSERT(kWideOpen_InitialClip == initialState);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000831 fNewClipData.fClipStack = &fNewClipStack;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000832
833 fOldClip = context->getClip();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000834 context->setClip(&fNewClipData);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000835 }
836
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000837 AutoClip(GrContext* context, const SkRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000838 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000839 , fNewClipStack(newClipRect) {
840 fNewClipData.fClipStack = &fNewClipStack;
841
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000842 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000843 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000844 }
845
846 ~AutoClip() {
bsalomon49f085d2014-09-05 13:34:00 -0700847 if (fContext) {
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000848 fContext->setClip(fOldClip);
849 }
850 }
851 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000852 GrContext* fContext;
853 const GrClipData* fOldClip;
854
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000855 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000856 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000857 };
858
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000859 class AutoWideOpenIdentityDraw {
860 public:
861 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
862 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000863 , fAutoRT(ctx, rt) {
864 fAutoMatrix.setIdentity(ctx);
865 // should never fail with no paint param.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000866 SkASSERT(fAutoMatrix.succeeded());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000867 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000868
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000869 private:
870 AutoClip fAutoClip;
871 AutoRenderTarget fAutoRT;
872 AutoMatrix fAutoMatrix;
873 };
874
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875 ///////////////////////////////////////////////////////////////////////////
876 // Functions intended for internal use only.
877 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000878 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879 GrFontCache* getFontCache() { return fFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000880 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000881 GrDrawTarget* getTextTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882 const GrIndexBuffer* getQuadIndexBuffer() const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000883 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
bsalomonc8dc1f72014-08-21 13:02:13 -0700884 GrResourceCache2* getResourceCache2() { return fResourceCache2; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000885
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000886 // Called by tests that draw directly to the context via GrDrawTarget
887 void getTestTarget(GrTestTarget*);
888
egdanielbbcb38d2014-06-19 10:19:29 -0700889 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
890 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
891
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000892 GrPathRenderer* getPathRenderer(
joshualitt9853cce2014-11-17 14:22:48 -0800893 const GrDrawTarget* target,
894 const GrDrawState*,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000895 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000896 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000897 bool allowSW,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000898 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000899 GrPathRendererChain::StencilSupport* stencilSupport = NULL);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000900
krajcevski9c6d4d72014-08-12 07:26:25 -0700901 /**
902 * This returns a copy of the the GrContext::Options that was passed to the
903 * constructor of this class.
904 */
905 const Options& getOptions() const { return fOptions; }
906
robertphillips@google.com59552022012-08-31 13:07:37 +0000907#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000908 void printCacheStats() const;
909#endif
910
robertphillips754f4e92014-09-18 13:52:08 -0700911 class GPUStats {
912 public:
913#if GR_GPU_STATS
914 GPUStats() { this->reset(); }
915
916 void reset() { fRenderTargetBinds = 0; fShaderCompilations = 0; }
917
918 int renderTargetBinds() const { return fRenderTargetBinds; }
919 void incRenderTargetBinds() { fRenderTargetBinds++; }
920 int shaderCompilations() const { return fShaderCompilations; }
921 void incShaderCompilations() { fShaderCompilations++; }
922 private:
923 int fRenderTargetBinds;
924 int fShaderCompilations;
925#else
926 void incRenderTargetBinds() {}
927 void incShaderCompilations() {}
928#endif
929 };
930
931#if GR_GPU_STATS
932 const GPUStats* gpuStats() const;
933#endif
934
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935private:
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000936 GrGpu* fGpu;
937 SkMatrix fViewMatrix;
938 SkAutoTUnref<GrRenderTarget> fRenderTarget;
939 const GrClipData* fClip; // TODO: make this ref counted
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000940
bsalomonc8dc1f72014-08-21 13:02:13 -0700941 GrResourceCache2* fResourceCache2;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000942 GrFontCache* fFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000943 SkAutoTDelete<GrLayerCache> fLayerCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000944
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000945 GrPathRendererChain* fPathRendererChain;
946 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000948 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
949 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
950 GrInOrderDrawBuffer* fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951
bsalomonf21dab92014-11-13 13:33:28 -0800952 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
953 bool fFlushToReduceCacheSize;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000954 GrAARectRenderer* fAARectRenderer;
955 GrOvalRenderer* fOvalRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000956
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000957 bool fDidTestPMConversions;
958 int fPMToUPMConversion;
959 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000960
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000961 struct CleanUpData {
962 PFCleanUpFunc fFunc;
963 void* fInfo;
964 };
965
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000966 SkTDArray<CleanUpData> fCleanUpData;
967
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000968 int fMaxTextureSizeOverride;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000969
krajcevski9c6d4d72014-08-12 07:26:25 -0700970 const Options fOptions;
971
972 GrContext(const Options&); // init must be called after the constructor.
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000973 bool init(GrBackend, GrBackendContext);
bsalomon33435572014-11-05 14:47:41 -0800974 void initMockContext();
975 void initCommon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000976
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000977 void setupDrawBuffer();
978
bsalomonf21dab92014-11-13 13:33:28 -0800979 class AutoCheckFlush;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000980 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
981 /// draw state is left unmodified.
joshualitt9853cce2014-11-17 14:22:48 -0800982 GrDrawTarget* prepareToDraw(GrDrawState* ds, const GrPaint* paint, const AutoCheckFlush*);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000983
joshualitt9853cce2014-11-17 14:22:48 -0800984 void internalDrawPath(GrDrawTarget*,
985 GrDrawState*,
986 bool useAA,
987 const SkPath&,
988 const GrStrokeInfo&);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000989
bsalomonf2703d82014-10-28 14:33:06 -0700990 GrTexture* createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000991 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000992 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000993 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000994 bool filter);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000995
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000996 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000997 * These functions create premul <-> unpremul effects if it is possible to generate a pair
998 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
999 * return NULL.
1000 */
joshualittb0a8a372014-09-23 09:50:21 -07001001 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
1002 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001003
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +00001004 /**
1005 * This callback allows the resource cache to callback into the GrContext
bsalomon71cb0c22014-11-14 12:10:14 -08001006 * when the cache is still over budget after a purge.
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +00001007 */
bsalomon71cb0c22014-11-14 12:10:14 -08001008 static void OverBudgetCB(void* data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +00001009
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +00001010 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001011};
1012
bsalomon@google.com27847de2011-02-22 20:59:41 +00001013#endif