blob: 08a04fbd93cb6fb631fd9293ab13e49e39581f5c [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;
36class GrResourceCache;
bsalomonc8dc1f72014-08-21 13:02:13 -070037class GrResourceCache2;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000038class GrStencilBuffer;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000039class GrTestTarget;
kkinnunenc6cb56f2014-06-24 00:12:27 -070040class GrTextContext;
bsalomon@google.com288d9542012-10-17 12:53:54 +000041class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000042class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000043class GrVertexBufferAllocPool;
egdanield58a0ba2014-06-11 10:30:05 -070044class GrStrokeInfo;
robertphillips@google.com72176b22012-05-23 13:19:12 +000045class GrSoftwarePathRenderer;
sugoi@google.com5f74cf82012-12-17 21:16:45 +000046class SkStrokeRec;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000047
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000048class SK_API GrContext : public SkRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000049public:
reed@google.comfa35e3d2012-06-26 20:16:17 +000050 SK_DECLARE_INST_COUNT(GrContext)
51
krajcevski9c6d4d72014-08-12 07:26:25 -070052 struct Options {
53 Options() : fDrawPathToCompressedTexture(false) { }
54
55 // EXPERIMENTAL
56 // May be removed in the future, or may become standard depending
57 // on the outcomes of a variety of internal tests.
58 bool fDrawPathToCompressedTexture;
59 };
60
bsalomon@google.com27847de2011-02-22 20:59:41 +000061 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000062 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 */
krajcevski9c6d4d72014-08-12 07:26:25 -070064 static GrContext* Create(GrBackend, GrBackendContext, const Options* opts = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +000065
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 virtual ~GrContext();
67
68 /**
69 * The GrContext normally assumes that no outsider is setting state
70 * within the underlying 3D API's context/device/whatever. This call informs
71 * the context that the state was modified and it should resend. Shouldn't
72 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000073 * The flag bits, state, is dpendent on which backend is used by the
74 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000075 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000076 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000077
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000079 * Callback function to allow classes to cleanup on GrContext destruction.
80 * The 'info' field is filled in with the 'info' passed to addCleanUp.
81 */
82 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
83
84 /**
85 * Add a function to be called from within GrContext's destructor.
86 * This gives classes a chance to free resources held on a per context basis.
87 * The 'info' parameter will be stored and passed to the callback function.
88 */
89 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
90 CleanUpData* entry = fCleanUpData.push();
91
92 entry->fFunc = cleanUp;
93 entry->fInfo = info;
94 }
95
96 /**
bsalomon2354f842014-07-28 13:48:36 -070097 * Abandons all GPU resources and assumes the underlying backend 3D API
98 * context is not longer usable. Call this if you have lost the associated
99 * GPU context, and thus internal texture, buffer, etc. references/IDs are
100 * now invalid. Should be called even when GrContext is no longer going to
101 * be used for two reasons:
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102 * 1) ~GrContext will not try to free the objects in the 3D API.
bsalomon2354f842014-07-28 13:48:36 -0700103 * 2) Any GrGpuResources created by this GrContext that outlive
104 * will be marked as invalid (GrGpuResource::wasDestroyed()) and
105 * when they're destroyed no 3D API calls will be made.
106 * Content drawn since the last GrContext::flush() may be lost. After this
107 * function is called the only valid action on the GrContext or
108 * GrGpuResources it created is to destroy them.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109 */
bsalomon2354f842014-07-28 13:48:36 -0700110 void abandonContext();
111 void contextDestroyed() { this->abandonContext(); } // legacy alias
junov@google.com53a55842011-06-08 22:55:10 +0000112
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000113 ///////////////////////////////////////////////////////////////////////////
114 // Resource Cache
115
116 /**
117 * Return the current GPU resource cache limits.
118 *
119 * @param maxResources If non-null, returns maximum number of resources that
120 * can be held in the cache.
121 * @param maxResourceBytes If non-null, returns maximum number of bytes of
122 * video memory that can be held in the cache.
123 */
124 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000125
126 /**
127 * Gets the current GPU resource cache usage.
128 *
129 * @param resourceCount If non-null, returns the number of resources that are held in the
130 * cache.
131 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
132 * in the cache.
133 */
134 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
135
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000136 /**
137 * Specify the GPU resource cache limits. If the current cache exceeds either
138 * of these, it will be purged (LRU) to keep the cache within these limits.
139 *
140 * @param maxResources The maximum number of resources that can be held in
141 * the cache.
142 * @param maxResourceBytes The maximum number of bytes of video memory
143 * that can be held in the cache.
144 */
145 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000146
junov@google.com53a55842011-06-08 22:55:10 +0000147 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000148 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000149 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000150 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000151 void freeGpuResources();
152
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000153 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000154 * This method should be called whenever a GrResource is unreffed or
155 * switched from exclusive to non-exclusive. This
156 * gives the resource cache a chance to discard unneeded resources.
157 * Note: this entry point will be removed once totally ref-driven
158 * cache maintenance is implemented.
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000159 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000160 void purgeCache();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000161
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000162 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000163 * Purge all the unlocked resources from the cache.
164 * This entry point is mainly meant for timing texture uploads
165 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000166 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000167 void purgeAllUnlockedResources();
168
169 /**
170 * Stores a custom resource in the cache, based on the specified key.
171 */
bsalomon6d3fe022014-07-25 08:35:45 -0700172 void addResourceToCache(const GrResourceKey&, GrGpuResource*);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000173
174 /**
175 * Finds a resource in the cache, based on the specified key. This is intended for use in
176 * conjunction with addResourceToCache(). The return value will be NULL if not found. The
177 * caller must balance with a call to unref().
178 */
bsalomon6d3fe022014-07-25 08:35:45 -0700179 GrGpuResource* findAndRefCachedResource(const GrResourceKey&);
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000180
kkinnunenc6cb56f2014-06-24 00:12:27 -0700181 /**
182 * Creates a new text rendering context that is optimal for the
183 * render target and the context. Caller assumes the ownership
184 * of the returned object. The returned object must be deleted
185 * before the context is destroyed.
186 */
187 GrTextContext* createTextContext(GrRenderTarget*,
188 const SkDeviceProperties&,
189 bool enableDistanceFieldFonts);
190
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191 ///////////////////////////////////////////////////////////////////////////
192 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193
194 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000195 * Creates a new entry, based on the specified key and texture and returns it. The caller owns a
196 * ref on the returned texture which must be balanced by a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000197 *
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000198 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000199 * the cache entry used. (e.g. different versions may exist
200 * for different wrap modes on GPUs with limited NPOT
201 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000202 * @param desc Description of the texture properties.
krajcevski9c0e6292014-06-02 07:38:14 -0700203 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000204 * @param srcData Pointer to the pixel values.
205 * @param rowBytes The number of bytes between rows of the texture. Zero
krajcevski9c0e6292014-06-02 07:38:14 -0700206 * implies tightly packed rows. For compressed pixel configs, this
207 * field is ignored.
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000208 * @param cacheKey (optional) If non-NULL, we'll write the cache key we used to cacheKey.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000209 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000210 GrTexture* createTexture(const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700211 const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000212 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000213 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000214 size_t rowBytes,
215 GrResourceKey* cacheKey = NULL);
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000216 /**
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000217 * Search for an entry based on key and dimensions. If found, ref it and return it. The return
218 * value will be NULL if not found. The caller must balance with a call to unref.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000219 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000220 * @param desc Description of the texture properties.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000221 * @param cacheID Cache-specific properties (e.g., texture gen ID)
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000222 * @param params The texture params used to draw a texture may help determine
bsalomon@google.comb8670992012-07-25 21:27:09 +0000223 * the cache entry used. (e.g. different versions may exist
224 * for different wrap modes on GPUs with limited NPOT
225 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000226 */
bsalomonf2703d82014-10-28 14:33:06 -0700227 GrTexture* findAndRefTexture(const GrSurfaceDesc& desc,
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000228 const GrCacheID& cacheID,
229 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000230 /**
231 * Determines whether a texture is in the cache. If the texture is found it
232 * will not be locked or returned. This call does not affect the priority of
233 * the texture for deletion.
234 */
bsalomonf2703d82014-10-28 14:33:06 -0700235 bool isTextureInCache(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000236 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000237 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000238
239 /**
240 * Enum that determines how closely a returned scratch texture must match
bsalomonf2703d82014-10-28 14:33:06 -0700241 * a provided GrSurfaceDesc.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000242 */
243 enum ScratchTexMatch {
244 /**
245 * Finds a texture that exactly matches the descriptor.
246 */
247 kExact_ScratchTexMatch,
248 /**
249 * Finds a texture that approximately matches the descriptor. Will be
250 * at least as large in width and height as desc specifies. If desc
251 * specifies that texture is a render target then result will be a
252 * render target. If desc specifies a render target and doesn't set the
253 * no stencil flag then result will have a stencil. Format and aa level
254 * will always match.
255 */
256 kApprox_ScratchTexMatch
257 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000258
259 /**
bsalomone3059732014-10-14 11:47:22 -0700260 * Returns a texture matching the desc. It's contents are unknown. The caller
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000261 * owns a ref on the returned texture and must balance with a call to unref.
bsalomone3059732014-10-14 11:47:22 -0700262 * It is guaranteed that the same texture will not be returned in subsequent
263 * calls until all refs to the texture are dropped.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000264 *
bsalomone3059732014-10-14 11:47:22 -0700265 * Textures created by createTexture() hide the complications of
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000266 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomone3059732014-10-14 11:47:22 -0700267 * unextended GLES2). NPOT scratch textures are not tilable on such APIs.
bsalomonbcf0a522014-10-08 08:40:09 -0700268 *
269 * internalFlag is a temporary workaround until changes in the internal
270 * architecture are complete. Use the default value.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000271 */
bsalomonf2703d82014-10-28 14:33:06 -0700272 GrTexture* refScratchTexture(const GrSurfaceDesc&, ScratchTexMatch match,
bsalomone3059732014-10-14 11:47:22 -0700273 bool internalFlag = false);
robertphillipsdbe60742014-09-30 06:54:17 -0700274
275 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000276 * Creates a texture that is outside the cache. Does not count against
277 * cache's budget.
bsalomone3059732014-10-14 11:47:22 -0700278 *
279 * Textures created by createTexture() hide the complications of
280 * tiling non-power-of-two textures on APIs that don't support this (e.g.
281 * unextended GLES2). NPOT uncached textures are not tilable on such APIs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282 */
bsalomonf2703d82014-10-28 14:33:06 -0700283 GrTexture* createUncachedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000284 void* srcData,
285 size_t rowBytes);
286
287 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000288 * Returns true if the specified use of an indexed texture is supported.
289 * Support may depend upon whether the texture params indicate that the
290 * texture will be tiled. Passing NULL for the texture params indicates
291 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000292 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000293 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000294 int width,
295 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000296
297 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000298 * Return the max width or height of a texture supported by the current GPU.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000299 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000300 int getMaxTextureSize() const;
301
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000302 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000303 * Temporarily override the true max texture size. Note: an override
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000304 * larger then the true max texture size will have no effect.
305 * This entry point is mainly meant for testing texture size dependent
306 * features and is only available if defined outside of Skia (see
307 * bleed GM.
308 */
309 void setMaxTextureSizeOverride(int maxTextureSizeOverride);
310
bsalomon@google.com27847de2011-02-22 20:59:41 +0000311 ///////////////////////////////////////////////////////////////////////////
312 // Render targets
313
314 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000315 * Sets the render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000316 * @param target the render target to set.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000317 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000318 void setRenderTarget(GrRenderTarget* target) {
319 fRenderTarget.reset(SkSafeRef(target));
320 }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000321
322 /**
323 * Gets the current render target.
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000324 * @return the currently bound render target.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000325 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000326 const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
327 GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000328
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000329 /**
330 * Can the provided configuration act as a color render target?
331 */
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000332 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000333
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000334 /**
335 * Return the max width or height of a render target supported by the
336 * current GPU.
337 */
338 int getMaxRenderTargetSize() const;
339
340 /**
341 * Returns the max sample count for a render target. It will be 0 if MSAA
342 * is not supported.
343 */
344 int getMaxSampleCount() const;
345
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000346 /**
347 * Returns the recommended sample count for a render target when using this
348 * context.
349 *
350 * @param config the configuration of the render target.
351 * @param dpi the display density in dots per inch.
352 *
353 * @return sample count that should be perform well and have good enough
354 * rendering quality for the display. Alternatively returns 0 if
355 * MSAA is not supported or recommended to be used by default.
356 */
357 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
358
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000359 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000360 // Backend Surfaces
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000361
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000362 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000363 * Wraps an existing texture with a GrTexture object.
364 *
365 * OpenGL: if the object is a texture Gr may change its GL texture params
366 * when it is drawn.
367 *
368 * @param desc description of the object to create.
369 *
370 * @return GrTexture object or NULL on failure.
371 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000372 GrTexture* wrapBackendTexture(const GrBackendTextureDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000373
374 /**
375 * Wraps an existing render target with a GrRenderTarget object. It is
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000376 * similar to wrapBackendTexture but can be used to draw into surfaces
bsalomon@google.come269f212011-11-07 13:29:52 +0000377 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
378 * the client will resolve to a texture).
379 *
380 * @param desc description of the object to create.
381 *
382 * @return GrTexture object or NULL on failure.
383 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000384 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000385
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386 ///////////////////////////////////////////////////////////////////////////
387 // Matrix state
388
389 /**
390 * Gets the current transformation matrix.
391 * @return the current matrix.
392 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000393 const SkMatrix& getMatrix() const { return fViewMatrix; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394
395 /**
396 * Sets the transformation matrix.
397 * @param m the matrix to set.
398 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000399 void setMatrix(const SkMatrix& m) { fViewMatrix = m; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400
401 /**
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000402 * Sets the current transformation matrix to identity.
403 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000404 void setIdentityMatrix() { fViewMatrix.reset(); }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000405
406 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000407 * Concats the current matrix. The passed matrix is applied before the
408 * current matrix.
409 * @param m the matrix to concat.
410 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000411 void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000412
413
414 ///////////////////////////////////////////////////////////////////////////
415 // Clip state
416 /**
417 * Gets the current clip.
418 * @return the current clip.
419 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000420 const GrClipData* getClip() const { return fClip; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000421
422 /**
423 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000424 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 */
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000426 void setClip(const GrClipData* clipData) { fClip = clipData; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427
bsalomon@google.com27847de2011-02-22 20:59:41 +0000428 ///////////////////////////////////////////////////////////////////////////
429 // Draws
430
431 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000432 * Clear the entire or rect of the render target, ignoring any clips.
433 * @param rect the rect to clear or the whole thing if rect is NULL.
434 * @param color the color to clear to.
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000435 * @param canIgnoreRect allows partial clears to be converted to whole
436 * clears on platforms for which that is cheap
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000437 * @param target if non-NULL, the render target to clear otherwise clear
438 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000439 */
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000440 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000441 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442
443 /**
444 * Draw everywhere (respecting the clip) with the paint.
445 */
446 void drawPaint(const GrPaint& paint);
447
448 /**
449 * Draw the rect using a paint.
450 * @param paint describes how to color pixels.
egdanield58a0ba2014-06-11 10:30:05 -0700451 * @param strokeInfo the stroke information (width, join, cap), and.
452 * the dash information (intervals, count, phase).
453 * If strokeInfo == NULL, then the rect is filled.
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000454 * Otherwise, if stroke width == 0, then the stroke
skia.committer@gmail.com26144182013-11-07 07:02:19 +0000455 * is always a single pixel thick, else the rect is
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000456 * mitered/beveled stroked based on stroke width.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000457 * The rects coords are used to access the paint (through texture matrix)
458 */
459 void drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000460 const SkRect&,
bsalomon01c8da12014-08-04 09:21:30 -0700461 const GrStrokeInfo* strokeInfo = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000462
463 /**
bsalomon@google.comc7818882013-03-20 19:19:53 +0000464 * Maps a rect of local coordinates onto the a rect of destination
bsalomon01c8da12014-08-04 09:21:30 -0700465 * coordinates. The localRect is stretched over the dstRect. The dstRect is
466 * transformed by the context's matrix. An additional optional matrix can be
467 * provided to transform the local rect.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000468 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000469 * @param paint describes how to color pixels.
470 * @param dstRect the destination rect to draw.
471 * @param localRect rect of local coordinates to be mapped onto dstRect
bsalomon@google.comc7818882013-03-20 19:19:53 +0000472 * @param localMatrix Optional matrix to transform localRect.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000473 */
474 void drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000475 const SkRect& dstRect,
476 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000477 const SkMatrix* localMatrix = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000478
479 /**
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000480 * Draw a roundrect using a paint.
481 *
482 * @param paint describes how to color pixels.
483 * @param rrect the roundrect to draw
egdanield58a0ba2014-06-11 10:30:05 -0700484 * @param strokeInfo the stroke information (width, join, cap) and
485 * the dash information (intervals, count, phase).
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000486 */
egdanield58a0ba2014-06-11 10:30:05 -0700487 void drawRRect(const GrPaint& paint, const SkRRect& rrect, const GrStrokeInfo& strokeInfo);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000488
489 /**
490 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
491 * Does not support stroking. The result is undefined if outer does not contain
492 * inner.
493 *
494 * @param paint describes how to color pixels.
495 * @param outer the outer roundrect
496 * @param inner the inner roundrect
497 */
498 void drawDRRect(const GrPaint& paint, const SkRRect& outer, const SkRRect& inner);
499
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000500
501 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000502 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000503 *
504 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000505 * @param path the path to draw
egdanield58a0ba2014-06-11 10:30:05 -0700506 * @param strokeInfo the stroke information (width, join, cap) and
507 * the dash information (intervals, count, phase).
bsalomon@google.com27847de2011-02-22 20:59:41 +0000508 */
egdanield58a0ba2014-06-11 10:30:05 -0700509 void drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo);
reed@google.com07f3ee12011-05-16 17:21:57 +0000510
bsalomon@google.com27847de2011-02-22 20:59:41 +0000511 /**
512 * Draws vertices with a paint.
513 *
514 * @param paint describes how to color pixels.
515 * @param primitiveType primitives type to draw.
516 * @param vertexCount number of vertices.
517 * @param positions array of vertex positions, required.
518 * @param texCoords optional array of texture coordinates used
519 * to access the paint.
520 * @param colors optional array of per-vertex colors, supercedes
521 * the paint's color field.
522 * @param indices optional array of indices. If NULL vertices
523 * are drawn non-indexed.
524 * @param indexCount if indices is non-null then this is the
525 * number of indices.
526 */
527 void drawVertices(const GrPaint& paint,
528 GrPrimitiveType primitiveType,
529 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000530 const SkPoint positions[],
531 const SkPoint texs[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532 const GrColor colors[],
533 const uint16_t indices[],
534 int indexCount);
535
bsalomon@google.com93c96602012-04-27 13:05:21 +0000536 /**
537 * Draws an oval.
538 *
539 * @param paint describes how to color pixels.
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000540 * @param oval the bounding rect of the oval.
egdanield58a0ba2014-06-11 10:30:05 -0700541 * @param strokeInfo the stroke information (width, join, cap) and
542 * the dash information (intervals, count, phase).
bsalomon@google.com93c96602012-04-27 13:05:21 +0000543 */
544 void drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000545 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700546 const GrStrokeInfo& strokeInfo);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000547
bsalomon@google.com27847de2011-02-22 20:59:41 +0000548 ///////////////////////////////////////////////////////////////////////////
549 // Misc.
550
551 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000552 * Flags that affect flush() behavior.
553 */
554 enum FlushBits {
555 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000556 * A client may reach a point where it has partially rendered a frame
557 * through a GrContext that it knows the user will never see. This flag
558 * causes the flush to skip submission of deferred content to the 3D API
559 * during the flush.
560 */
561 kDiscard_FlushBit = 0x2,
562 };
563
564 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565 * Call to ensure all drawing to the context has been issued to the
566 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000567 * @param flagsBitfield flags that control the flushing behavior. See
568 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000570 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000571
bsalomon@google.com0342a852012-08-20 19:22:38 +0000572 /**
573 * These flags can be used with the read/write pixels functions below.
574 */
575 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700576 /** The GrContext will not be flushed before the surface read or write. This means that
577 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000578 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700579 /** Any surface writes should be flushed to the backend 3D API after the surface operation
580 is complete */
581 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000582 /** The src for write or dst read is unpremultiplied. This is only respected if both the
583 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700584 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000585 };
586
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000588 * Reads a rectangle of pixels from a render target.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000589 * @param target the render target to read from. NULL means the current render target.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000590 * @param left left edge of the rectangle to read (inclusive)
591 * @param top top edge of the rectangle to read (inclusive)
592 * @param width width of rectangle to read in pixels.
593 * @param height height of rectangle to read in pixels.
594 * @param config the pixel config of the destination buffer
595 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000596 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
597 * packed.
598 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000599 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000600 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
601 * pixel config or because no render target is currently set and NULL was passed for
602 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000604 bool readRenderTargetPixels(GrRenderTarget* target,
605 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000606 GrPixelConfig config, void* buffer,
607 size_t rowBytes = 0,
608 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000609
610 /**
bsalomon81beccc2014-10-13 12:32:55 -0700611 * Writes a rectangle of pixels to a surface.
612 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000613 * @param left left edge of the rectangle to write (inclusive)
614 * @param top top edge of the rectangle to write (inclusive)
615 * @param width width of rectangle to write in pixels.
616 * @param height height of rectangle to write in pixels.
617 * @param config the pixel config of the source buffer
618 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000619 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000620 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000621 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000622 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700623 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000624 */
bsalomon81beccc2014-10-13 12:32:55 -0700625 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000626 int left, int top, int width, int height,
627 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000628 size_t rowBytes,
629 uint32_t pixelOpsFlags = 0);
630
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000631 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700632 * Copies a rectangle of texels from src to dst.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000633 * bounds.
bsalomonf80bfed2014-10-07 05:56:02 -0700634 * @param dst the surface to copy to.
635 * @param src the surface to copy from.
636 * @param srcRect the rectangle of the src that should be copied.
637 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
638 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000639 */
bsalomonf80bfed2014-10-07 05:56:02 -0700640 void copySurface(GrSurface* dst,
641 GrSurface* src,
642 const SkIRect& srcRect,
643 const SkIPoint& dstPoint,
644 uint32_t pixelOpsFlags = 0);
645
646 /** Helper that copies the whole surface but fails when the two surfaces are not identically
647 sized. */
648 bool copySurface(GrSurface* dst, GrSurface* src) {
649 if (NULL == dst || NULL == src || dst->width() != src->width() ||
650 dst->height() != src->height()) {
651 return false;
652 }
653 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
654 SkIPoint::Make(0,0));
655 return true;
656 }
657
658 /**
659 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
660 */
661 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000662
663 /**
664 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000665 * down-sampled to the associated GrTexture (accessible via
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000666 * GrRenderTarget::asTexture()). Any pending draws to the render target will
667 * be executed before the resolve.
668 *
669 * This is only necessary when a client wants to access the object directly
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000670 * using the backend API directly. GrContext will detect when it must
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000671 * perform a resolve to a GrTexture used as the source of a draw or before
672 * reading pixels back from a GrTexture or GrRenderTarget.
673 */
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000674 void resolveRenderTarget(GrRenderTarget*);
675
676 /**
677 * Provides a perfomance hint that the render target's contents are allowed
678 * to become undefined.
679 */
680 void discardRenderTarget(GrRenderTarget*);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000681
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000682#ifdef SK_DEVELOPER
683 void dumpFontCache() const;
684#endif
685
bsalomon@google.com27847de2011-02-22 20:59:41 +0000686 ///////////////////////////////////////////////////////////////////////////
687 // Helpers
688
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000689 class AutoRenderTarget : public ::SkNoncopyable {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000690 public:
691 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000693 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000694 context->setRenderTarget(target);
695 fContext = context;
696 }
697 AutoRenderTarget(GrContext* context) {
698 fPrevTarget = context->getRenderTarget();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000699 SkSafeRef(fPrevTarget);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000700 fContext = context;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000701 }
702 ~AutoRenderTarget() {
bsalomon49f085d2014-09-05 13:34:00 -0700703 if (fContext) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000704 fContext->setRenderTarget(fPrevTarget);
705 }
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000706 SkSafeUnref(fPrevTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000707 }
708 private:
709 GrContext* fContext;
710 GrRenderTarget* fPrevTarget;
711 };
712
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000713 /**
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000714 * Save/restore the view-matrix in the context. It can optionally adjust a paint to account
715 * for a coordinate system change. Here is an example of how the paint param can be used:
716 *
joshualittb0a8a372014-09-23 09:50:21 -0700717 * A GrPaint is setup with GrProcessors. The stages will have access to the pre-matrix source
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000718 * geometry positions when the draw is executed. Later on a decision is made to transform the
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000719 * geometry to device space on the CPU. The effects now need to know that the space in which
720 * the geometry will be specified has changed.
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000721 *
722 * Note that when restore is called (or in the destructor) the context's matrix will be
723 * restored. However, the paint will not be restored. The caller must make a copy of the
724 * paint if necessary. Hint: use SkTCopyOnFirstWrite if the AutoMatrix is conditionally
725 * initialized.
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000726 */
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000727 class AutoMatrix : public ::SkNoncopyable {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000728 public:
729 AutoMatrix() : fContext(NULL) {}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000730
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000731 ~AutoMatrix() { this->restore(); }
732
733 /**
734 * Initializes by pre-concat'ing the context's current matrix with the preConcat param.
735 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000736 void setPreConcat(GrContext* context, const SkMatrix& preConcat, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700737 SkASSERT(context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000738
739 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000740
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000741 fContext = context;
742 fMatrix = context->getMatrix();
743 this->preConcat(preConcat, paint);
744 }
745
746 /**
747 * Sets the context's matrix to identity. Returns false if the inverse matrix is required to
748 * update a paint but the matrix cannot be inverted.
749 */
750 bool setIdentity(GrContext* context, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700751 SkASSERT(context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000752
753 this->restore();
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000754
bsalomon49f085d2014-09-05 13:34:00 -0700755 if (paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000756 if (!paint->localCoordChangeInverse(context->getMatrix())) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000757 return false;
758 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000759 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000760 fMatrix = context->getMatrix();
761 fContext = context;
762 context->setIdentityMatrix();
763 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000764 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000765
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000766 /**
767 * Replaces the context's matrix with a new matrix. Returns false if the inverse matrix is
768 * required to update a paint but the matrix cannot be inverted.
769 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000770 bool set(GrContext* context, const SkMatrix& newMatrix, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700771 if (paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000772 if (!this->setIdentity(context, paint)) {
773 return false;
774 }
775 this->preConcat(newMatrix, paint);
776 } else {
777 this->restore();
778 fContext = context;
779 fMatrix = context->getMatrix();
780 context->setMatrix(newMatrix);
781 }
782 return true;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000783 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000784
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000785 /**
786 * If this has been initialized then the context's matrix will be further updated by
787 * pre-concat'ing the preConcat param. The matrix that will be restored remains unchanged.
788 * The paint is assumed to be relative to the context's matrix at the time this call is
789 * made, not the matrix at the time AutoMatrix was first initialized. In other words, this
790 * performs an incremental update of the paint.
791 */
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000792 void preConcat(const SkMatrix& preConcat, GrPaint* paint = NULL) {
bsalomon49f085d2014-09-05 13:34:00 -0700793 if (paint) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000794 paint->localCoordChange(preConcat);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000795 }
796 fContext->concatMatrix(preConcat);
797 }
798
799 /**
800 * Returns false if never initialized or the inverse matrix was required to update a paint
801 * but the matrix could not be inverted.
802 */
bsalomon49f085d2014-09-05 13:34:00 -0700803 bool succeeded() const { return SkToBool(fContext); }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000804
805 /**
806 * If this has been initialized then the context's original matrix is restored.
807 */
808 void restore() {
bsalomon49f085d2014-09-05 13:34:00 -0700809 if (fContext) {
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000810 fContext->setMatrix(fMatrix);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000811 fContext = NULL;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000812 }
813 }
814
815 private:
816 GrContext* fContext;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000817 SkMatrix fMatrix;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000818 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000820 class AutoClip : public ::SkNoncopyable {
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000821 public:
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000822 // This enum exists to require a caller of the constructor to acknowledge that the clip will
823 // initially be wide open. It also could be extended if there are other desirable initial
824 // clip states.
825 enum InitialClip {
826 kWideOpen_InitialClip,
827 };
828
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000829 AutoClip(GrContext* context, InitialClip initialState)
robertphillips@google.comccb39502012-10-01 18:25:13 +0000830 : fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000831 SkASSERT(kWideOpen_InitialClip == initialState);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000832 fNewClipData.fClipStack = &fNewClipStack;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000833
834 fOldClip = context->getClip();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000835 context->setClip(&fNewClipData);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000836 }
837
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000838 AutoClip(GrContext* context, const SkRect& newClipRect)
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000839 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000840 , fNewClipStack(newClipRect) {
841 fNewClipData.fClipStack = &fNewClipStack;
842
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000843 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000844 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000845 }
846
847 ~AutoClip() {
bsalomon49f085d2014-09-05 13:34:00 -0700848 if (fContext) {
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000849 fContext->setClip(fOldClip);
850 }
851 }
852 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000853 GrContext* fContext;
854 const GrClipData* fOldClip;
855
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000856 SkClipStack fNewClipStack;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000857 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000858 };
859
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000860 class AutoWideOpenIdentityDraw {
861 public:
862 AutoWideOpenIdentityDraw(GrContext* ctx, GrRenderTarget* rt)
863 : fAutoClip(ctx, AutoClip::kWideOpen_InitialClip)
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000864 , fAutoRT(ctx, rt) {
865 fAutoMatrix.setIdentity(ctx);
866 // should never fail with no paint param.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000867 SkASSERT(fAutoMatrix.succeeded());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000868 }
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000869
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000870 private:
871 AutoClip fAutoClip;
872 AutoRenderTarget fAutoRT;
873 AutoMatrix fAutoMatrix;
874 };
875
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876 ///////////////////////////////////////////////////////////////////////////
877 // Functions intended for internal use only.
878 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000879 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880 GrFontCache* getFontCache() { return fFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000881 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000882 GrDrawTarget* getTextTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883 const GrIndexBuffer* getQuadIndexBuffer() const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000884 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
bsalomonbcf0a522014-10-08 08:40:09 -0700885 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomonc8dc1f72014-08-21 13:02:13 -0700886 GrResourceCache2* getResourceCache2() { return fResourceCache2; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000887
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000888 // Called by tests that draw directly to the context via GrDrawTarget
889 void getTestTarget(GrTestTarget*);
890
egdanielbbcb38d2014-06-19 10:19:29 -0700891 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
892 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
893
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000894 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000895 * Stencil buffers add themselves to the cache using addStencilBuffer. findStencilBuffer is
896 * called to check the cache for a SB that matches an RT's criteria.
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000897 */
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000898 void addStencilBuffer(GrStencilBuffer* sb);
899 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000901 GrPathRenderer* getPathRenderer(
902 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000903 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000904 const GrDrawTarget* target,
905 bool allowSW,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000906 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000907 GrPathRendererChain::StencilSupport* stencilSupport = NULL);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000908
krajcevski9c6d4d72014-08-12 07:26:25 -0700909 /**
910 * This returns a copy of the the GrContext::Options that was passed to the
911 * constructor of this class.
912 */
913 const Options& getOptions() const { return fOptions; }
914
robertphillips@google.com59552022012-08-31 13:07:37 +0000915#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000916 void printCacheStats() const;
917#endif
918
robertphillips754f4e92014-09-18 13:52:08 -0700919 class GPUStats {
920 public:
921#if GR_GPU_STATS
922 GPUStats() { this->reset(); }
923
924 void reset() { fRenderTargetBinds = 0; fShaderCompilations = 0; }
925
926 int renderTargetBinds() const { return fRenderTargetBinds; }
927 void incRenderTargetBinds() { fRenderTargetBinds++; }
928 int shaderCompilations() const { return fShaderCompilations; }
929 void incShaderCompilations() { fShaderCompilations++; }
930 private:
931 int fRenderTargetBinds;
932 int fShaderCompilations;
933#else
934 void incRenderTargetBinds() {}
935 void incShaderCompilations() {}
936#endif
937 };
938
939#if GR_GPU_STATS
940 const GPUStats* gpuStats() const;
941#endif
942
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943private:
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000944 // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
945 enum BufferedDraw {
946 kYes_BufferedDraw,
947 kNo_BufferedDraw,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948 };
bsalomon@google.com1d4edd32012-08-16 18:36:06 +0000949 BufferedDraw fLastDrawWasBuffered;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000951 GrGpu* fGpu;
952 SkMatrix fViewMatrix;
953 SkAutoTUnref<GrRenderTarget> fRenderTarget;
954 const GrClipData* fClip; // TODO: make this ref counted
955 GrDrawState* fDrawState;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000956
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000957 GrResourceCache* fResourceCache;
bsalomonc8dc1f72014-08-21 13:02:13 -0700958 GrResourceCache2* fResourceCache2;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000959 GrFontCache* fFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000960 SkAutoTDelete<GrLayerCache> fLayerCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000961
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000962 GrPathRendererChain* fPathRendererChain;
963 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000964
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000965 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
966 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
967 GrInOrderDrawBuffer* fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000969 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
970 bool fFlushToReduceCacheSize;
971
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000972 GrAARectRenderer* fAARectRenderer;
973 GrOvalRenderer* fOvalRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000974
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000975 bool fDidTestPMConversions;
976 int fPMToUPMConversion;
977 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000978
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000979 struct CleanUpData {
980 PFCleanUpFunc fFunc;
981 void* fInfo;
982 };
983
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000984 SkTDArray<CleanUpData> fCleanUpData;
985
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000986 int fMaxTextureSizeOverride;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000987
krajcevski9c6d4d72014-08-12 07:26:25 -0700988 const Options fOptions;
989
990 GrContext(const Options&); // init must be called after the constructor.
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000991 bool init(GrBackend, GrBackendContext);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000992
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000993 void setupDrawBuffer();
994
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000995 class AutoRestoreEffects;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000996 class AutoCheckFlush;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000997 /// Sets the paint and returns the target to draw into. The paint can be NULL in which case the
998 /// draw state is left unmodified.
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000999 GrDrawTarget* prepareToDraw(const GrPaint*, BufferedDraw, AutoRestoreEffects*, AutoCheckFlush*);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001000
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +00001001 void internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001002 const GrStrokeInfo& stroke);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001003
bsalomonf2703d82014-10-28 14:33:06 -07001004 GrTexture* createResizedTexture(const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001005 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +00001006 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +00001007 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +00001008 bool filter);
robertphillips@google.com3319f332012-08-13 18:00:36 +00001009
bsalomonf2703d82014-10-28 14:33:06 -07001010 GrTexture* createNewScratchTexture(const GrSurfaceDesc& desc);
reed@google.comfa35e3d2012-06-26 20:16:17 +00001011
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +00001012 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +00001013 * These functions create premul <-> unpremul effects if it is possible to generate a pair
1014 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
1015 * return NULL.
1016 */
joshualittb0a8a372014-09-23 09:50:21 -07001017 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
1018 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001019
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +00001020 /**
1021 * This callback allows the resource cache to callback into the GrContext
1022 * when the cache is still overbudget after a purge.
1023 */
1024 static bool OverbudgetCB(void* data);
1025
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +00001026 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001027};
1028
bsalomon@google.com27847de2011-02-22 20:59:41 +00001029#endif