blob: b6fef3d80c3baca0d156776b9d3ebeb44137fbef [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
joshualitt44701df2015-02-23 14:44:57 -080011#include "GrClip.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"
bsalomond309e7a2015-04-30 14:18:54 -070016#include "GrTextureProvider.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;
joshualitt7c3a2f82015-03-31 13:32:05 -070022class GrBatchFontCache;
bsalomon682c2692015-05-22 14:01:46 -070023struct GrContextOptions;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000024class GrDrawTarget;
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;
robertphillips@google.come930a072014-04-03 00:34:27 +000029class GrLayerCache;
commit-bot@chromium.org81312832013-03-22 18:34:09 +000030class GrOvalRenderer;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000031class GrPath;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000032class GrPathRenderer;
egdaniel8dd688b2015-01-22 10:16:09 -080033class GrPipelineBuilder;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000034class GrResourceEntry;
bsalomon0ea80f42015-02-11 10:49:59 -080035class GrResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -070036class GrResourceProvider;
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000037class GrTestTarget;
joshualittb7133be2015-04-08 09:08:31 -070038class GrTextBlobCache;
kkinnunenc6cb56f2014-06-24 00:12:27 -070039class GrTextContext;
bsalomon@google.com288d9542012-10-17 12:53:54 +000040class GrTextureParams;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000041class GrVertexBuffer;
egdanield58a0ba2014-06-11 10:30:05 -070042class GrStrokeInfo;
robertphillips@google.com72176b22012-05-23 13:19:12 +000043class GrSoftwarePathRenderer;
joshualitt6e8cd962015-03-20 10:30:14 -070044class SkGpuDevice;
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
bsalomon@google.com27847de2011-02-22 20:59:41 +000050 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000051 * Creates a GrContext for a backend context.
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 */
bsalomon682c2692015-05-22 14:01:46 -070053 static GrContext* Create(GrBackend, GrBackendContext, const GrContextOptions& options);
54 static GrContext* Create(GrBackend, GrBackendContext);
bsalomon@google.com27847de2011-02-22 20:59:41 +000055
bsalomon33435572014-11-05 14:47:41 -080056 /**
57 * Only defined in test apps.
58 */
59 static GrContext* CreateMockContext();
60
bsalomon@google.com27847de2011-02-22 20:59:41 +000061 virtual ~GrContext();
62
63 /**
64 * The GrContext normally assumes that no outsider is setting state
65 * within the underlying 3D API's context/device/whatever. This call informs
66 * the context that the state was modified and it should resend. Shouldn't
67 * be called frequently for good performance.
bsalomon@google.com0a208a12013-06-28 18:57:35 +000068 * The flag bits, state, is dpendent on which backend is used by the
69 * context, either GL or D3D (possible in future).
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000071 void resetContext(uint32_t state = kAll_GrBackendState);
bsalomon@google.com27847de2011-02-22 20:59:41 +000072
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073 /**
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000074 * Callback function to allow classes to cleanup on GrContext destruction.
75 * The 'info' field is filled in with the 'info' passed to addCleanUp.
76 */
77 typedef void (*PFCleanUpFunc)(const GrContext* context, void* info);
78
79 /**
80 * Add a function to be called from within GrContext's destructor.
81 * This gives classes a chance to free resources held on a per context basis.
82 * The 'info' parameter will be stored and passed to the callback function.
83 */
84 void addCleanUp(PFCleanUpFunc cleanUp, void* info) {
85 CleanUpData* entry = fCleanUpData.push();
86
87 entry->fFunc = cleanUp;
88 entry->fInfo = info;
89 }
90
91 /**
bsalomon2354f842014-07-28 13:48:36 -070092 * Abandons all GPU resources and assumes the underlying backend 3D API
93 * context is not longer usable. Call this if you have lost the associated
94 * GPU context, and thus internal texture, buffer, etc. references/IDs are
95 * now invalid. Should be called even when GrContext is no longer going to
96 * be used for two reasons:
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097 * 1) ~GrContext will not try to free the objects in the 3D API.
bsalomon2354f842014-07-28 13:48:36 -070098 * 2) Any GrGpuResources created by this GrContext that outlive
99 * will be marked as invalid (GrGpuResource::wasDestroyed()) and
100 * when they're destroyed no 3D API calls will be made.
101 * Content drawn since the last GrContext::flush() may be lost. After this
102 * function is called the only valid action on the GrContext or
103 * GrGpuResources it created is to destroy them.
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104 */
bsalomon2354f842014-07-28 13:48:36 -0700105 void abandonContext();
junov@google.com53a55842011-06-08 22:55:10 +0000106
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000107 ///////////////////////////////////////////////////////////////////////////
108 // Resource Cache
109
110 /**
111 * Return the current GPU resource cache limits.
112 *
113 * @param maxResources If non-null, returns maximum number of resources that
114 * can be held in the cache.
115 * @param maxResourceBytes If non-null, returns maximum number of bytes of
116 * video memory that can be held in the cache.
117 */
118 void getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000119
120 /**
121 * Gets the current GPU resource cache usage.
122 *
123 * @param resourceCount If non-null, returns the number of resources that are held in the
124 * cache.
125 * @param maxResourceBytes If non-null, returns the total number of bytes of video memory held
126 * in the cache.
127 */
128 void getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const;
129
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000130 /**
131 * Specify the GPU resource cache limits. If the current cache exceeds either
132 * of these, it will be purged (LRU) to keep the cache within these limits.
133 *
134 * @param maxResources The maximum number of resources that can be held in
135 * the cache.
136 * @param maxResourceBytes The maximum number of bytes of video memory
137 * that can be held in the cache.
138 */
139 void setResourceCacheLimits(int maxResources, size_t maxResourceBytes);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000140
bsalomond309e7a2015-04-30 14:18:54 -0700141 GrTextureProvider* textureProvider() { return fTextureProvider; }
142 const GrTextureProvider* textureProvider() const { return fTextureProvider; }
143
junov@google.com53a55842011-06-08 22:55:10 +0000144 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000145 * Frees GPU created by the context. Can be called to reduce GPU memory
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148 void freeGpuResources();
149
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000150 /**
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000151 * Purge all the unlocked resources from the cache.
152 * This entry point is mainly meant for timing texture uploads
153 * and is not defined in normal builds of Skia.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000154 */
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000155 void purgeAllUnlockedResources();
156
bsalomond309e7a2015-04-30 14:18:54 -0700157 //////////////////////////////////////////////////////////////////////////
158 /// Texture and Render Target Queries
robertphillipsdbe60742014-09-30 06:54:17 -0700159
160 /**
bsalomond2a6f4e2015-02-04 10:55:54 -0800161 * Can the provided configuration act as a texture?
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162 */
bsalomond2a6f4e2015-02-04 10:55:54 -0800163 bool isConfigTexturable(GrPixelConfig) const;
164
165 /**
166 * Can non-power-of-two textures be used with tile modes other than clamp?
167 */
168 bool npotTextureTileSupport() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000169
170 /**
bsalomon@google.com1e269b52012-10-15 14:25:31 +0000171 * Return the max width or height of a texture supported by the current GPU.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000172 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000173 int getMaxTextureSize() const;
174
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000175 /**
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000176 * Temporarily override the true max texture size. Note: an override
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000177 * larger then the true max texture size will have no effect.
178 * This entry point is mainly meant for testing texture size dependent
179 * features and is only available if defined outside of Skia (see
180 * bleed GM.
181 */
182 void setMaxTextureSizeOverride(int maxTextureSizeOverride);
183
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000184 /**
185 * Can the provided configuration act as a color render target?
186 */
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000187 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const;
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000188
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000189 /**
190 * Return the max width or height of a render target supported by the
191 * current GPU.
192 */
193 int getMaxRenderTargetSize() const;
194
195 /**
196 * Returns the max sample count for a render target. It will be 0 if MSAA
197 * is not supported.
198 */
199 int getMaxSampleCount() const;
200
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000201 /**
202 * Returns the recommended sample count for a render target when using this
203 * context.
204 *
205 * @param config the configuration of the render target.
206 * @param dpi the display density in dots per inch.
207 *
208 * @return sample count that should be perform well and have good enough
209 * rendering quality for the display. Alternatively returns 0 if
210 * MSAA is not supported or recommended to be used by default.
211 */
212 int getRecommendedSampleCount(GrPixelConfig config, SkScalar dpi) const;
213
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000214 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000215 // Draws
216
217 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000218 * Clear the entire or rect of the render target, ignoring any clips.
219 * @param rect the rect to clear or the whole thing if rect is NULL.
220 * @param color the color to clear to.
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000221 * @param canIgnoreRect allows partial clears to be converted to whole
222 * clears on platforms for which that is cheap
bsalomon89c62982014-11-03 12:08:42 -0800223 * @param target The render target to clear.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000224 */
bsalomon89c62982014-11-03 12:08:42 -0800225 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect, GrRenderTarget* target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226
227 /**
228 * Draw everywhere (respecting the clip) with the paint.
229 */
joshualitt570d2f82015-02-25 13:19:48 -0800230 void drawPaint(GrRenderTarget*, const GrClip&, const GrPaint&, const SkMatrix& viewMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000231
232 /**
233 * Draw the rect using a paint.
234 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800235 * @param viewMatrix transformation matrix
egdanield58a0ba2014-06-11 10:30:05 -0700236 * @param strokeInfo the stroke information (width, join, cap), and.
237 * the dash information (intervals, count, phase).
238 * If strokeInfo == NULL, then the rect is filled.
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000239 * Otherwise, if stroke width == 0, then the stroke
skia.committer@gmail.com26144182013-11-07 07:02:19 +0000240 * is always a single pixel thick, else the rect is
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000241 * mitered/beveled stroked based on stroke width.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000242 * The rects coords are used to access the paint (through texture matrix)
243 */
joshualitt25d9c152015-02-18 12:29:52 -0800244 void drawRect(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800245 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800246 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800247 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000248 const SkRect&,
bsalomon01c8da12014-08-04 09:21:30 -0700249 const GrStrokeInfo* strokeInfo = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250
251 /**
joshualitt16b27892014-12-18 07:47:16 -0800252 * Maps a rectangle of shader coordinates to a rectangle and draws that rectangle
bsalomon@google.com27847de2011-02-22 20:59:41 +0000253 *
bsalomon@google.comc7818882013-03-20 19:19:53 +0000254 * @param paint describes how to color pixels.
joshualitt16b27892014-12-18 07:47:16 -0800255 * @param viewMatrix transformation matrix which applies to rectToDraw
256 * @param rectToDraw the rectangle to draw
257 * @param localRect the rectangle of shader coordinates applied to rectToDraw
258 * @param localMatrix an optional matrix to transform the shader coordinates before applying
259 * to rectToDraw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000260 */
joshualitt25d9c152015-02-18 12:29:52 -0800261 void drawNonAARectToRect(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800262 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800263 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800264 const SkMatrix& viewMatrix,
265 const SkRect& rectToDraw,
266 const SkRect& localRect,
267 const SkMatrix* localMatrix = NULL);
268
269 /**
270 * Draws a non-AA rect with paint and a localMatrix
271 */
joshualitt25d9c152015-02-18 12:29:52 -0800272 void drawNonAARectWithLocalMatrix(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800273 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800274 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800275 const SkMatrix& viewMatrix,
276 const SkRect& rect,
277 const SkMatrix& localMatrix) {
joshualitt570d2f82015-02-25 13:19:48 -0800278 this->drawNonAARectToRect(rt, clip, paint, viewMatrix, rect, rect, &localMatrix);
joshualitt16b27892014-12-18 07:47:16 -0800279 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000280
281 /**
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000282 * Draw a roundrect using a paint.
283 *
284 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800285 * @param viewMatrix transformation matrix
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000286 * @param rrect the roundrect to draw
egdanield58a0ba2014-06-11 10:30:05 -0700287 * @param strokeInfo the stroke information (width, join, cap) and
288 * the dash information (intervals, count, phase).
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000289 */
joshualitt25d9c152015-02-18 12:29:52 -0800290 void drawRRect(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800291 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800292 const GrPaint&,
293 const SkMatrix& viewMatrix,
294 const SkRRect& rrect,
joshualitt5531d512014-12-17 15:50:11 -0800295 const GrStrokeInfo&);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000296
297 /**
298 * Shortcut for drawing an SkPath consisting of nested rrects using a paint.
299 * Does not support stroking. The result is undefined if outer does not contain
300 * inner.
301 *
302 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800303 * @param viewMatrix transformation matrix
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000304 * @param outer the outer roundrect
305 * @param inner the inner roundrect
306 */
joshualitt25d9c152015-02-18 12:29:52 -0800307 void drawDRRect(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800308 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800309 const GrPaint&,
310 const SkMatrix& viewMatrix,
311 const SkRRect& outer,
joshualitt5531d512014-12-17 15:50:11 -0800312 const SkRRect& inner);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000313
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000314
315 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000316 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000317 *
318 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800319 * @param viewMatrix transformation matrix
reed@google.com07f3ee12011-05-16 17:21:57 +0000320 * @param path the path to draw
egdanield58a0ba2014-06-11 10:30:05 -0700321 * @param strokeInfo the stroke information (width, join, cap) and
322 * the dash information (intervals, count, phase).
bsalomon@google.com27847de2011-02-22 20:59:41 +0000323 */
joshualitt25d9c152015-02-18 12:29:52 -0800324 void drawPath(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800325 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800326 const GrPaint&,
327 const SkMatrix& viewMatrix,
328 const SkPath&,
329 const GrStrokeInfo&);
reed@google.com07f3ee12011-05-16 17:21:57 +0000330
bsalomon@google.com27847de2011-02-22 20:59:41 +0000331 /**
332 * Draws vertices with a paint.
333 *
334 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800335 * @param viewMatrix transformation matrix
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336 * @param primitiveType primitives type to draw.
337 * @param vertexCount number of vertices.
338 * @param positions array of vertex positions, required.
339 * @param texCoords optional array of texture coordinates used
340 * to access the paint.
341 * @param colors optional array of per-vertex colors, supercedes
342 * the paint's color field.
343 * @param indices optional array of indices. If NULL vertices
344 * are drawn non-indexed.
345 * @param indexCount if indices is non-null then this is the
346 * number of indices.
347 */
joshualitt25d9c152015-02-18 12:29:52 -0800348 void drawVertices(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800349 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800350 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800351 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000352 GrPrimitiveType primitiveType,
353 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000354 const SkPoint positions[],
355 const SkPoint texs[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356 const GrColor colors[],
357 const uint16_t indices[],
358 int indexCount);
359
bsalomon@google.com93c96602012-04-27 13:05:21 +0000360 /**
361 * Draws an oval.
362 *
363 * @param paint describes how to color pixels.
joshualitt5531d512014-12-17 15:50:11 -0800364 * @param viewMatrix transformation matrix
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000365 * @param oval the bounding rect of the oval.
egdanield58a0ba2014-06-11 10:30:05 -0700366 * @param strokeInfo the stroke information (width, join, cap) and
367 * the dash information (intervals, count, phase).
bsalomon@google.com93c96602012-04-27 13:05:21 +0000368 */
joshualitt25d9c152015-02-18 12:29:52 -0800369 void drawOval(GrRenderTarget*,
joshualitt570d2f82015-02-25 13:19:48 -0800370 const GrClip&,
joshualitt25d9c152015-02-18 12:29:52 -0800371 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800372 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000373 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -0700374 const GrStrokeInfo& strokeInfo);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000375
bsalomon@google.com27847de2011-02-22 20:59:41 +0000376 ///////////////////////////////////////////////////////////////////////////
377 // Misc.
378
379 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000380 * Flags that affect flush() behavior.
381 */
382 enum FlushBits {
383 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000384 * A client may reach a point where it has partially rendered a frame
385 * through a GrContext that it knows the user will never see. This flag
386 * causes the flush to skip submission of deferred content to the 3D API
387 * during the flush.
388 */
389 kDiscard_FlushBit = 0x2,
390 };
391
392 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000393 * Call to ensure all drawing to the context has been issued to the
394 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000395 * @param flagsBitfield flags that control the flushing behavior. See
396 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000398 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000399
bsalomon@google.com0342a852012-08-20 19:22:38 +0000400 /**
401 * These flags can be used with the read/write pixels functions below.
402 */
403 enum PixelOpsFlags {
bsalomonf80bfed2014-10-07 05:56:02 -0700404 /** The GrContext will not be flushed before the surface read or write. This means that
405 the read or write may occur before previous draws have executed. */
bsalomon@google.com0342a852012-08-20 19:22:38 +0000406 kDontFlush_PixelOpsFlag = 0x1,
bsalomonf80bfed2014-10-07 05:56:02 -0700407 /** Any surface writes should be flushed to the backend 3D API after the surface operation
408 is complete */
409 kFlushWrites_PixelOp = 0x2,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000410 /** The src for write or dst read is unpremultiplied. This is only respected if both the
411 config src and dst configs are an RGBA/BGRA 8888 format. */
bsalomonf80bfed2014-10-07 05:56:02 -0700412 kUnpremul_PixelOpsFlag = 0x4,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000413 };
414
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000416 * Reads a rectangle of pixels from a render target.
bsalomon89c62982014-11-03 12:08:42 -0800417 * @param target the render target to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000418 * @param left left edge of the rectangle to read (inclusive)
419 * @param top top edge of the rectangle to read (inclusive)
420 * @param width width of rectangle to read in pixels.
421 * @param height height of rectangle to read in pixels.
422 * @param config the pixel config of the destination buffer
423 * @param buffer memory to read the rectangle into.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000424 * @param rowBytes number of bytes bewtween consecutive rows. Zero means rows are tightly
425 * packed.
426 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000427 *
bsalomon@google.com0342a852012-08-20 19:22:38 +0000428 * @return true if the read succeeded, false if not. The read can fail because of an unsupported
429 * pixel config or because no render target is currently set and NULL was passed for
430 * target.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000431 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000432 bool readRenderTargetPixels(GrRenderTarget* target,
433 int left, int top, int width, int height,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000434 GrPixelConfig config, void* buffer,
435 size_t rowBytes = 0,
436 uint32_t pixelOpsFlags = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000437
438 /**
bsalomon81beccc2014-10-13 12:32:55 -0700439 * Writes a rectangle of pixels to a surface.
440 * @param surface the surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000441 * @param left left edge of the rectangle to write (inclusive)
442 * @param top top edge of the rectangle to write (inclusive)
443 * @param width width of rectangle to write in pixels.
444 * @param height height of rectangle to write in pixels.
445 * @param config the pixel config of the source buffer
446 * @param buffer memory to read pixels from
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000447 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000448 * means rows are tightly packed.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000449 * @param pixelOpsFlags see PixelOpsFlags enum above.
bsalomon@google.com9c680582013-02-06 18:17:50 +0000450 * @return true if the write succeeded, false if not. The write can fail because of an
bsalomon81beccc2014-10-13 12:32:55 -0700451 * unsupported combination of surface and src configs.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000452 */
bsalomon81beccc2014-10-13 12:32:55 -0700453 bool writeSurfacePixels(GrSurface* surface,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000454 int left, int top, int width, int height,
455 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000456 size_t rowBytes,
457 uint32_t pixelOpsFlags = 0);
458
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000459 /**
bsalomonf80bfed2014-10-07 05:56:02 -0700460 * Copies a rectangle of texels from src to dst.
scroggo@google.coma2a31922012-12-07 19:14:45 +0000461 * bounds.
bsalomonf80bfed2014-10-07 05:56:02 -0700462 * @param dst the surface to copy to.
463 * @param src the surface to copy from.
464 * @param srcRect the rectangle of the src that should be copied.
465 * @param dstPoint the translation applied when writing the srcRect's pixels to the dst.
466 * @param pixelOpsFlags see PixelOpsFlags enum above. (kUnpremul_PixelOpsFlag is not allowed).
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000467 */
bsalomonf80bfed2014-10-07 05:56:02 -0700468 void copySurface(GrSurface* dst,
469 GrSurface* src,
470 const SkIRect& srcRect,
471 const SkIPoint& dstPoint,
472 uint32_t pixelOpsFlags = 0);
473
474 /** Helper that copies the whole surface but fails when the two surfaces are not identically
475 sized. */
476 bool copySurface(GrSurface* dst, GrSurface* src) {
477 if (NULL == dst || NULL == src || dst->width() != src->width() ||
478 dst->height() != src->height()) {
479 return false;
480 }
481 this->copySurface(dst, src, SkIRect::MakeWH(dst->width(), dst->height()),
482 SkIPoint::Make(0,0));
483 return true;
484 }
485
486 /**
487 * After this returns any pending writes to the surface will have been issued to the backend 3D API.
488 */
489 void flushSurfaceWrites(GrSurface* surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000490
491 /**
bsalomon87a94eb2014-11-03 14:28:32 -0800492 * Equivalent to flushSurfaceWrites but also performs MSAA resolve if necessary. This call is
493 * used to make the surface contents available to be read in the backend 3D API, usually for a
494 * compositing step external to Skia.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000495 *
bsalomon87a94eb2014-11-03 14:28:32 -0800496 * It is not necessary to call this before reading the render target via Skia/GrContext.
497 * GrContext will detect when it must perform a resolve before reading pixels back from the
498 * surface or using it as a texture.
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000499 */
bsalomon87a94eb2014-11-03 14:28:32 -0800500 void prepareSurfaceForExternalRead(GrSurface*);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000501
502 /**
503 * Provides a perfomance hint that the render target's contents are allowed
504 * to become undefined.
505 */
506 void discardRenderTarget(GrRenderTarget*);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000507
joshualitt0acd0d32015-05-07 08:23:19 -0700508 /**
509 * An ID associated with this context, guaranteed to be unique.
510 */
511 uint32_t uniqueID() { return fUniqueID; }
512
bsalomon@google.com27847de2011-02-22 20:59:41 +0000513 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514 // Functions intended for internal use only.
515 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000516 const GrGpu* getGpu() const { return fGpu; }
joshualitt7c3a2f82015-03-31 13:32:05 -0700517 GrBatchFontCache* getBatchFontCache() { return fBatchFontCache; }
robertphillips@google.come930a072014-04-03 00:34:27 +0000518 GrLayerCache* getLayerCache() { return fLayerCache.get(); }
joshualittb7133be2015-04-08 09:08:31 -0700519 GrTextBlobCache* getTextBlobCache() { return fTextBlobCache; }
bsalomon@google.com21c10c52013-06-13 17:44:07 +0000520 GrDrawTarget* getTextTarget();
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000521 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
bsalomond309e7a2015-04-30 14:18:54 -0700522 GrResourceProvider* resourceProvider() { return fResourceProvider; }
523 const GrResourceProvider* resourceProvider() const { return fResourceProvider; }
bsalomon0ea80f42015-02-11 10:49:59 -0800524 GrResourceCache* getResourceCache() { return fResourceCache; }
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000525
commit-bot@chromium.org78a10782013-08-21 19:27:48 +0000526 // Called by tests that draw directly to the context via GrDrawTarget
527 void getTestTarget(GrTestTarget*);
528
egdanielbbcb38d2014-06-19 10:19:29 -0700529 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
530 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
531
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000532 GrPathRenderer* getPathRenderer(
joshualitt9853cce2014-11-17 14:22:48 -0800533 const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800534 const GrPipelineBuilder*,
joshualitt8059eb92014-12-29 15:10:07 -0800535 const SkMatrix& viewMatrix,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000536 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700537 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000538 bool allowSW,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000539 GrPathRendererChain::DrawType drawType = GrPathRendererChain::kColor_DrawType,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000540 GrPathRendererChain::StencilSupport* stencilSupport = NULL);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000541
mtkleinb9eb4ac2015-02-02 18:26:03 -0800542 /** Prints cache stats to the string if GR_CACHE_STATS == 1. */
543 void dumpCacheStats(SkString*) const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000544 void printCacheStats() const;
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +0000545
mtkleinb9eb4ac2015-02-02 18:26:03 -0800546 /** Prints GPU stats to the string if GR_GPU_STATS == 1. */
547 void dumpGpuStats(SkString*) const;
548 void printGpuStats() const;
robertphillips754f4e92014-09-18 13:52:08 -0700549
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550private:
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000551 GrGpu* fGpu;
bsalomon0ea80f42015-02-11 10:49:59 -0800552 GrResourceCache* fResourceCache;
bsalomond309e7a2015-04-30 14:18:54 -0700553 // this union exists because the inheritance of GrTextureProvider->GrResourceProvider
554 // is in a private header.
555 union {
556 GrResourceProvider* fResourceProvider;
557 GrTextureProvider* fTextureProvider;
558 };
559
joshualitt7c3a2f82015-03-31 13:32:05 -0700560 GrBatchFontCache* fBatchFontCache;
robertphillips@google.come930a072014-04-03 00:34:27 +0000561 SkAutoTDelete<GrLayerCache> fLayerCache;
joshualittb7133be2015-04-08 09:08:31 -0700562 SkAutoTDelete<GrTextBlobCache> fTextBlobCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000563
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000564 GrPathRendererChain* fPathRendererChain;
565 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566
bsalomona73239a2015-04-28 13:35:17 -0700567 GrDrawTarget* fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568
bsalomonf21dab92014-11-13 13:33:28 -0800569 // Set by OverbudgetCB() to request that GrContext flush before exiting a draw.
570 bool fFlushToReduceCacheSize;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000571 GrAARectRenderer* fAARectRenderer;
572 GrOvalRenderer* fOvalRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000573
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000574 bool fDidTestPMConversions;
575 int fPMToUPMConversion;
576 int fUPMToPMConversion;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000577
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000578 struct CleanUpData {
579 PFCleanUpFunc fFunc;
580 void* fInfo;
581 };
582
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000583 SkTDArray<CleanUpData> fCleanUpData;
584
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000585 int fMaxTextureSizeOverride;
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000586
joshualitt0acd0d32015-05-07 08:23:19 -0700587 const uint32_t fUniqueID;
krajcevski9c6d4d72014-08-12 07:26:25 -0700588
bsalomon682c2692015-05-22 14:01:46 -0700589 GrContext(); // init must be called after the constructor.
590 bool init(GrBackend, GrBackendContext, const GrContextOptions& options);
bsalomon33435572014-11-05 14:47:41 -0800591 void initMockContext();
592 void initCommon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000593
bsalomonf21dab92014-11-13 13:33:28 -0800594 class AutoCheckFlush;
joshualitt570d2f82015-02-25 13:19:48 -0800595 // Sets the paint and returns the target to draw into.
596 GrDrawTarget* prepareToDraw(GrPipelineBuilder*,
597 GrRenderTarget* rt,
598 const GrClip&,
599 const GrPaint* paint,
joshualitt25d9c152015-02-18 12:29:52 -0800600 const AutoCheckFlush*);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601
joshualitt570d2f82015-02-25 13:19:48 -0800602 // A simpler version of the above which just returns the draw target. Clip is *NOT* set
603 GrDrawTarget* prepareToDraw();
604
joshualitt9853cce2014-11-17 14:22:48 -0800605 void internalDrawPath(GrDrawTarget*,
egdaniel8dd688b2015-01-22 10:16:09 -0800606 GrPipelineBuilder*,
joshualitt5531d512014-12-17 15:50:11 -0800607 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -0800608 GrColor,
joshualitt9853cce2014-11-17 14:22:48 -0800609 bool useAA,
610 const SkPath&,
611 const GrStrokeInfo&);
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000612
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000613 /**
joshualitt6e8cd962015-03-20 10:30:14 -0700614 * Creates a new text rendering context that is optimal for the
615 * render target and the context. Caller assumes the ownership
616 * of the returned object. The returned object must be deleted
617 * before the context is destroyed.
618 * TODO we can possibly bury this behind context, but we need to be able to use the
619 * drawText_asPaths logic on SkGpuDevice
620 */
621 GrTextContext* createTextContext(GrRenderTarget*,
622 SkGpuDevice*,
623 const SkDeviceProperties&,
624 bool enableDistanceFieldFonts);
625
626
627 /**
bsalomon@google.comadc65362013-01-28 14:26:09 +0000628 * These functions create premul <-> unpremul effects if it is possible to generate a pair
629 * of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
630 * return NULL.
631 */
joshualittb0a8a372014-09-23 09:50:21 -0700632 const GrFragmentProcessor* createPMToUPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
633 const GrFragmentProcessor* createUPMToPMEffect(GrTexture*, bool swapRAndB, const SkMatrix&);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000634
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000635 /**
636 * This callback allows the resource cache to callback into the GrContext
bsalomon71cb0c22014-11-14 12:10:14 -0800637 * when the cache is still over budget after a purge.
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000638 */
bsalomon71cb0c22014-11-14 12:10:14 -0800639 static void OverBudgetCB(void* data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000640
joshualitt0db6dfa2015-04-10 07:01:30 -0700641 /**
642 * A callback similar to the above for use by the TextBlobCache
643 * TODO move textblob draw calls below context so we can use the call above.
644 */
645 static void TextBlobCacheOverBudgetCB(void* data);
646
joshualitt6e8cd962015-03-20 10:30:14 -0700647 // TODO see note on createTextContext
648 friend class SkGpuDevice;
649
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000650 typedef SkRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000651};
652
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653#endif