blob: 6a6b0841d1e69b867fc616b709821be0f4a20f96 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrContext_DEFINED
11#define GrContext_DEFINED
12
13#include "GrClip.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000014#include "GrPaint.h"
robertphillips@google.comf6747b02012-06-12 00:32:28 +000015#include "GrAARectRenderer.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000016// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
17// remove.
18#include "GrRenderTarget.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000020class GrAutoScratchTexture;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000021class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000022class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000023class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000024class GrGpu;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000025class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000026class GrIndexBufferAllocPool;
27class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000028class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000029class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000030class GrResourceEntry;
31class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000032class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000033class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000034class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000035class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000036
bsalomon@google.com91826102011-03-21 19:51:57 +000037class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000038public:
39 /**
40 * Creates a GrContext from within a 3D context.
41 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000042 static GrContext* Create(GrEngine engine,
43 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000044
bsalomon@google.comc0af3172012-06-15 14:10:09 +000045 /**
46 * Returns the number of GrContext instances for the current thread.
47 */
48 static int GetThreadInstanceCount();
49
bsalomon@google.com27847de2011-02-22 20:59:41 +000050 virtual ~GrContext();
51
52 /**
53 * The GrContext normally assumes that no outsider is setting state
54 * within the underlying 3D API's context/device/whatever. This call informs
55 * the context that the state was modified and it should resend. Shouldn't
56 * be called frequently for good performance.
57 */
58 void resetContext();
59
bsalomon@google.com8fe72472011-03-30 21:26:44 +000060 /**
61 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
62 * if you have lost the associated GPU context, and thus internal texture,
63 * buffer, etc. references/IDs are now invalid. Should be called even when
64 * GrContext is no longer going to be used for two reasons:
65 * 1) ~GrContext will not try to free the objects in the 3D API.
66 * 2) If you've created GrResources that outlive the GrContext they will
67 * be marked as invalid (GrResource::isValid()) and won't attempt to
68 * free their underlying resource in the 3D API.
69 * Content drawn since the last GrContext::flush() may be lost.
70 */
71 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000072
73 /**
junov@google.com53a55842011-06-08 22:55:10 +000074 * Similar to contextLost, but makes no attempt to reset state.
75 * Use this method when GrContext destruction is pending, but
76 * the graphics context is destroyed first.
77 */
78 void contextDestroyed();
79
80 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 * Frees gpu created by the context. Can be called to reduce GPU memory
82 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000083 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 void freeGpuResources();
85
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000086 /**
87 * Returns the number of bytes of GPU memory hosted by the texture cache.
88 */
89 size_t getGpuTextureCacheBytes() const;
90
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 ///////////////////////////////////////////////////////////////////////////
92 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000093
94 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000095 * Token that refers to an entry in the texture cache. Returned by
96 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000097 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000098 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000099 public:
100 TextureCacheEntry() : fEntry(NULL) {}
101 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
102 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
103 fEntry = e.fEntry;
104 return *this;
105 }
106 GrTexture* texture() const;
107 void reset() { fEntry = NULL; }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000108 GrResourceEntry* cacheEntry() { return fEntry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000109 private:
110 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
111 void set(GrResourceEntry* entry) { fEntry = entry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000112 GrResourceEntry* fEntry;
113 friend class GrContext;
114 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000115
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000116 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000117 * Create a new entry, based on the specified key and texture, and return
118 * its "locked" entry. Must call be balanced with an unlockTexture() call.
119 *
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000120 * @param sampler The sampler state used to draw a texture may be used
121 * to determine how to store the pixel data in the texture
122 * cache. (e.g. different versions may exist for different
123 * wrap modes on GPUs with limited or no NPOT texture
124 * support). Only the wrap and filter fields are used. NULL
125 * implies clamp wrap modes and nearest filtering.
126 * @param desc Description of the texture properties.
127 * @param srcData Pointer to the pixel values.
128 * @param rowBytes The number of bytes between rows of the texture. Zero
129 * implies tightly packed rows.
130 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000131 TextureCacheEntry createAndLockTexture(const GrSamplerState* sampler,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000132 const GrTextureDesc& desc,
133 void* srcData, size_t rowBytes);
134
135 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000136 * Search for an entry based on key and dimensions. If found, "lock" it and
137 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000138 * Must be balanced with an unlockTexture() call.
139 *
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000140 * @param desc Description of the texture properties.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000141 * @param sampler The sampler state used to draw a texture may be used
142 * to determine the cache entry used. (e.g. different
143 * versions may exist for different wrap modes on GPUs with
144 * limited or no NPOT texture support). Only the wrap and
145 * filter fields are used. NULL implies clamp wrap modes
146 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000147 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000148 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000149 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000150 /**
151 * Determines whether a texture is in the cache. If the texture is found it
152 * will not be locked or returned. This call does not affect the priority of
153 * the texture for deletion.
154 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000155 bool isTextureInCache(const GrTextureDesc& desc,
156 const GrSamplerState* sampler) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000157
158 /**
159 * Enum that determines how closely a returned scratch texture must match
160 * a provided GrTextureDesc.
161 */
162 enum ScratchTexMatch {
163 /**
164 * Finds a texture that exactly matches the descriptor.
165 */
166 kExact_ScratchTexMatch,
167 /**
168 * Finds a texture that approximately matches the descriptor. Will be
169 * at least as large in width and height as desc specifies. If desc
170 * specifies that texture is a render target then result will be a
171 * render target. If desc specifies a render target and doesn't set the
172 * no stencil flag then result will have a stencil. Format and aa level
173 * will always match.
174 */
175 kApprox_ScratchTexMatch
176 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177
178 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000179 * Returns a texture matching the desc. It's contents are unknown. Subsequent
180 * requests with the same descriptor are not guaranteed to return the same
181 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000182 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000183 *
184 * Textures created by createAndLockTexture() hide the complications of
185 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000186 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000187 * such an API will create gaps in the tiling pattern. This includes clamp
188 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000189 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000190 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
191 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000192
193 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194 * When done with an entry, call unlockTexture(entry) on it, which returns
195 * it to the cache, where it may be purged.
196 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198
199 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000200 * Free any data associated with the provided entry in the texture cache
201 */
202 void freeEntry(TextureCacheEntry entry);
203
204 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000205 * Creates a texture that is outside the cache. Does not count against
206 * cache's budget.
207 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000208 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000209 void* srcData,
210 size_t rowBytes);
211
212 /**
213 * Returns true if the specified use of an indexed texture is supported.
214 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000215 bool supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000216 int width,
217 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218
219 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000220 * Return the current texture cache limits.
221 *
222 * @param maxTextures If non-null, returns maximum number of textures that
223 * can be held in the cache.
224 * @param maxTextureBytes If non-null, returns maximum number of bytes of
225 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000227 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228
229 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000230 * Specify the texture cache limits. If the current cache exceeds either
231 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000233 * @param maxTextures The maximum number of textures that can be held in
234 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000235 * @param maxTextureBytes The maximum number of bytes of texture memory
236 * that can be held in the cache.
237 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000238 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239
240 /**
241 * Return the max width or height of a texture supported by the current gpu
242 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000243 int getMaxTextureSize() const;
244
245 /**
246 * Return the max width or height of a render target supported by the
247 * current gpu
248 */
249 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250
251 ///////////////////////////////////////////////////////////////////////////
252 // Render targets
253
254 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000255 * Sets the render target.
256 * @param target the render target to set. (should not be NULL.)
257 */
258 void setRenderTarget(GrRenderTarget* target);
259
260 /**
261 * Gets the current render target.
262 * @return the currently bound render target. Should never be NULL.
263 */
264 const GrRenderTarget* getRenderTarget() const;
265 GrRenderTarget* getRenderTarget();
266
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000267 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
268
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000269 /**
270 * Can the provided configuration act as a color render target?
271 */
272 bool isConfigRenderable(GrPixelConfig config) const;
273
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000274 ///////////////////////////////////////////////////////////////////////////
275 // Platform Surfaces
276
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000277 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000278 * Wraps an existing texture with a GrTexture object.
279 *
280 * OpenGL: if the object is a texture Gr may change its GL texture params
281 * when it is drawn.
282 *
283 * @param desc description of the object to create.
284 *
285 * @return GrTexture object or NULL on failure.
286 */
287 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
288
289 /**
290 * Wraps an existing render target with a GrRenderTarget object. It is
291 * similar to createPlatformTexture but can be used to draw into surfaces
292 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
293 * the client will resolve to a texture).
294 *
295 * @param desc description of the object to create.
296 *
297 * @return GrTexture object or NULL on failure.
298 */
299 GrRenderTarget* createPlatformRenderTarget(
300 const GrPlatformRenderTargetDesc& desc);
301
bsalomon@google.com27847de2011-02-22 20:59:41 +0000302 ///////////////////////////////////////////////////////////////////////////
303 // Matrix state
304
305 /**
306 * Gets the current transformation matrix.
307 * @return the current matrix.
308 */
309 const GrMatrix& getMatrix() const;
310
311 /**
312 * Sets the transformation matrix.
313 * @param m the matrix to set.
314 */
315 void setMatrix(const GrMatrix& m);
316
317 /**
318 * Concats the current matrix. The passed matrix is applied before the
319 * current matrix.
320 * @param m the matrix to concat.
321 */
322 void concatMatrix(const GrMatrix& m) const;
323
324
325 ///////////////////////////////////////////////////////////////////////////
326 // Clip state
327 /**
328 * Gets the current clip.
329 * @return the current clip.
330 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000331 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000332
333 /**
334 * Sets the clip.
335 * @param clip the clip to set.
336 */
337 void setClip(const GrClip& clip);
338
339 /**
340 * Convenience method for setting the clip to a rect.
341 * @param rect the rect to set as the new clip.
342 */
343 void setClip(const GrIRect& rect);
344
345 ///////////////////////////////////////////////////////////////////////////
346 // Draws
347
348 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000349 * Clear the entire or rect of the render target, ignoring any clips.
350 * @param rect the rect to clear or the whole thing if rect is NULL.
351 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000352 * @param target if non-NULL, the render target to clear otherwise clear
353 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000354 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000355 void clear(const GrIRect* rect, GrColor color,
356 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
358 /**
359 * Draw everywhere (respecting the clip) with the paint.
360 */
361 void drawPaint(const GrPaint& paint);
362
363 /**
364 * Draw the rect using a paint.
365 * @param paint describes how to color pixels.
366 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
367 * the rect is mitered stroked based on strokeWidth. If
368 * strokeWidth == 0, then the stroke is always a single
369 * pixel thick.
370 * @param matrix Optional matrix applied to the rect. Applied before
371 * context's matrix or the paint's matrix.
372 * The rects coords are used to access the paint (through texture matrix)
373 */
374 void drawRect(const GrPaint& paint,
375 const GrRect&,
376 GrScalar strokeWidth = -1,
377 const GrMatrix* matrix = NULL);
378
379 /**
380 * Maps a rect of paint coordinates onto the a rect of destination
381 * coordinates. Each rect can optionally be transformed. The srcRect
382 * is stretched over the dstRect. The dstRect is transformed by the
383 * context's matrix and the srcRect is transformed by the paint's matrix.
384 * Additional optional matrices can be provided by parameters.
385 *
386 * @param paint describes how to color pixels.
387 * @param dstRect the destination rect to draw.
388 * @param srcRect rect of paint coordinates to be mapped onto dstRect
389 * @param dstMatrix Optional matrix to transform dstRect. Applied before
390 * context's matrix.
391 * @param srcMatrix Optional matrix to transform srcRect Applied before
392 * paint's matrix.
393 */
394 void drawRectToRect(const GrPaint& paint,
395 const GrRect& dstRect,
396 const GrRect& srcRect,
397 const GrMatrix* dstMatrix = NULL,
398 const GrMatrix* srcMatrix = NULL);
399
400 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000401 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 *
403 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000404 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000405 * @param fill the path filling rule to use.
406 * @param translate optional additional translation applied to the
407 * path.
408 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000409 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000410 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000411
bsalomon@google.com27847de2011-02-22 20:59:41 +0000412 /**
413 * Draws vertices with a paint.
414 *
415 * @param paint describes how to color pixels.
416 * @param primitiveType primitives type to draw.
417 * @param vertexCount number of vertices.
418 * @param positions array of vertex positions, required.
419 * @param texCoords optional array of texture coordinates used
420 * to access the paint.
421 * @param colors optional array of per-vertex colors, supercedes
422 * the paint's color field.
423 * @param indices optional array of indices. If NULL vertices
424 * are drawn non-indexed.
425 * @param indexCount if indices is non-null then this is the
426 * number of indices.
427 */
428 void drawVertices(const GrPaint& paint,
429 GrPrimitiveType primitiveType,
430 int vertexCount,
431 const GrPoint positions[],
432 const GrPoint texs[],
433 const GrColor colors[],
434 const uint16_t indices[],
435 int indexCount);
436
bsalomon@google.com93c96602012-04-27 13:05:21 +0000437 /**
438 * Draws an oval.
439 *
440 * @param paint describes how to color pixels.
441 * @param rect the bounding rect of the oval.
442 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
443 * the rect is stroked based on strokeWidth. If
444 * strokeWidth == 0, then the stroke is always a single
445 * pixel thick.
446 */
447 void drawOval(const GrPaint& paint,
448 const GrRect& rect,
449 SkScalar strokeWidth);
450
bsalomon@google.com27847de2011-02-22 20:59:41 +0000451 ///////////////////////////////////////////////////////////////////////////
452 // Misc.
453
454 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000455 * Flags that affect flush() behavior.
456 */
457 enum FlushBits {
458 /**
459 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
460 * it can be rendered to directly. However, Gr lazily sets state. Simply
461 * calling setRenderTarget() followed by flush() without flags may not
462 * bind the render target. This flag forces the context to bind the last
463 * set render target in the 3D API.
464 */
465 kForceCurrentRenderTarget_FlushBit = 0x1,
466 /**
467 * A client may reach a point where it has partially rendered a frame
468 * through a GrContext that it knows the user will never see. This flag
469 * causes the flush to skip submission of deferred content to the 3D API
470 * during the flush.
471 */
472 kDiscard_FlushBit = 0x2,
473 };
474
475 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000476 * Call to ensure all drawing to the context has been issued to the
477 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000478 * @param flagsBitfield flags that control the flushing behavior. See
479 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000480 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000481 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000482
bsalomon@google.com27847de2011-02-22 20:59:41 +0000483 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000484 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000485 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000486 * current render target.
487 * @param left left edge of the rectangle to read (inclusive)
488 * @param top top edge of the rectangle to read (inclusive)
489 * @param width width of rectangle to read in pixels.
490 * @param height height of rectangle to read in pixels.
491 * @param config the pixel config of the destination buffer
492 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000493 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000494 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000495 *
496 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000497 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000498 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000499 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000500 bool readRenderTargetPixels(GrRenderTarget* target,
501 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000502 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000503 size_t rowBytes) {
504 return this->internalReadRenderTargetPixels(target, left, top,
505 width, height,
506 config, buffer,
507 rowBytes, 0);
508 }
509
510 /**
511 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
512 * at the specified rectangle.
513 * @param target the render target to write into. NULL means the
514 * current render target.
515 * @param left left edge of the rectangle to write (inclusive)
516 * @param top top edge of the rectangle to write (inclusive)
517 * @param width width of rectangle to write in pixels.
518 * @param height height of rectangle to write in pixels.
519 * @param config the pixel config of the source buffer
520 * @param buffer memory to read the rectangle from.
521 * @param rowBytes number of bytes bewtween consecutive rows. Zero
522 * means rows are tightly packed.
523 */
524 void writeRenderTargetPixels(GrRenderTarget* target,
525 int left, int top, int width, int height,
526 GrPixelConfig config, const void* buffer,
527 size_t rowBytes) {
528 this->internalWriteRenderTargetPixels(target, left, top, width, height,
529 config, buffer, rowBytes, 0);
530 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000531
532 /**
533 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000535 * @param left left edge of the rectangle to read (inclusive)
536 * @param top top edge of the rectangle to read (inclusive)
537 * @param width width of rectangle to read in pixels.
538 * @param height height of rectangle to read in pixels.
539 * @param config the pixel config of the destination buffer
540 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000541 * @param rowBytes number of bytes bewtween consecutive rows. Zero
542 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543 *
544 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000545 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000546 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000548 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000549 GrPixelConfig config, void* buffer,
550 size_t rowBytes) {
551 return this->internalReadTexturePixels(texture, left, top,
552 width, height,
553 config, buffer, rowBytes, 0);
554 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000555
556 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000557 * Writes a rectangle of pixels to a texture.
558 * @param texture the render target to read from.
559 * @param left left edge of the rectangle to write (inclusive)
560 * @param top top edge of the rectangle to write (inclusive)
561 * @param width width of rectangle to write in pixels.
562 * @param height height of rectangle to write in pixels.
563 * @param config the pixel config of the source buffer
564 * @param buffer memory to read pixels from
565 * @param rowBytes number of bytes bewtween consecutive rows. Zero
566 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000568 void writeTexturePixels(GrTexture* texture,
569 int left, int top, int width, int height,
570 GrPixelConfig config, const void* buffer,
571 size_t rowBytes) {
572 this->internalWriteTexturePixels(texture, left, top, width, height,
573 config, buffer, rowBytes, 0);
574 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000575 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000576 * Copies all texels from one texture to another.
577 * @param src the texture to copy from.
578 * @param dst the render target to copy to.
579 */
580 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000581
582 /**
583 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
584 * downsampled to the associated GrTexture (accessible via
585 * GrRenderTarget::asTexture()). Any pending draws to the render target will
586 * be executed before the resolve.
587 *
588 * This is only necessary when a client wants to access the object directly
589 * using the underlying graphics API. GrContext will detect when it must
590 * perform a resolve to a GrTexture used as the source of a draw or before
591 * reading pixels back from a GrTexture or GrRenderTarget.
592 */
593 void resolveRenderTarget(GrRenderTarget* target);
594
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000595 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000596 * Applies a 2D Gaussian blur to a given texture.
597 * @param srcTexture The source texture to be blurred.
598 * @param temp1 A scratch texture. Must not be NULL.
599 * @param temp2 A scratch texture. May be NULL, in which case
600 * srcTexture is overwritten with intermediate
601 * results.
602 * @param rect The destination rectangle.
603 * @param sigmaX The blur's standard deviation in X.
604 * @param sigmaY The blur's standard deviation in Y.
605 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000606 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000607 GrTexture* gaussianBlur(GrTexture* srcTexture,
608 GrAutoScratchTexture* temp1,
609 GrAutoScratchTexture* temp2,
610 const SkRect& rect,
611 float sigmaX, float sigmaY);
612
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000613 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000614 * This enum is used with the function below, applyMorphology.
615 */
616 enum MorphologyType {
617 kErode_MorphologyType,
618 kDilate_MorphologyType,
619 };
620
621 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000622 * Applies a 2D morphology to a given texture.
623 * @param srcTexture The source texture to be blurred.
624 * @param rect The destination rectangle.
625 * @param temp1 A scratch texture. Must not be NULL.
626 * @param temp2 A scratch texture. Must not be NULL.
627 * @param filter The morphology filter. Must be kDilate_Filter or
628 * kErode_Filter.
629 * @param radius The morphology radius in X and Y. The filter is
630 * applied to a fWidth by fHeight rectangle of
631 * pixels.
632 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000633 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000634 GrTexture* applyMorphology(GrTexture* srcTexture,
635 const GrRect& rect,
636 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000637 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000638 SkISize radius);
639
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640 ///////////////////////////////////////////////////////////////////////////
641 // Helpers
642
643 class AutoRenderTarget : ::GrNoncopyable {
644 public:
645 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
646 fContext = NULL;
647 fPrevTarget = context->getRenderTarget();
648 if (fPrevTarget != target) {
649 context->setRenderTarget(target);
650 fContext = context;
651 }
652 }
653 ~AutoRenderTarget() {
654 if (fContext) {
655 fContext->setRenderTarget(fPrevTarget);
656 }
657 }
658 private:
659 GrContext* fContext;
660 GrRenderTarget* fPrevTarget;
661 };
662
663
664 ///////////////////////////////////////////////////////////////////////////
665 // Functions intended for internal use only.
666 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000667 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000668 GrFontCache* getFontCache() { return fFontCache; }
669 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000671
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000672 /**
673 * Stencil buffers add themselves to the cache using
674 * addAndLockStencilBuffer. When a SB's RT-attachment count
675 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
676 * eligible for purging. findStencilBuffer is called to check the cache for
677 * a SB that matching an RT's criteria. If a match is found that has been
678 * unlocked (its attachment count has reached 0) then it will be relocked.
679 */
680 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
681 void unlockStencilBuffer(GrResourceEntry* sbEntry);
682 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000683
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000684 /*
685 * postClipPush acts as a hint to this and lower-level classes (e.g.,
686 * GrGpu) that the clip stack has changed.
687 */
688 virtual void postClipPush();
689
690 /*
691 * preClipPop acts as a hint that the clip stack has been restored to an
692 * earlier state.
693 */
694 virtual void preClipPop();
695
robertphillips@google.com2c756812012-05-22 20:28:23 +0000696 GrPathRenderer* getPathRenderer(const SkPath& path,
697 GrPathFill fill,
698 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000699 bool antiAlias,
700 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000701
bsalomon@google.com27847de2011-02-22 20:59:41 +0000702private:
703 // used to keep track of when we need to flush the draw buffer
704 enum DrawCategory {
705 kBuffered_DrawCategory, // last draw was inserted in draw buffer
706 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000707 };
708 DrawCategory fLastDrawCategory;
709
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000710 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000711 GrDrawState* fDrawState;
712
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000713 GrResourceCache* fTextureCache;
714 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000715
bsalomon@google.com30085192011-08-19 15:42:31 +0000716 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000717 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000718
719 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
720 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
721 GrInOrderDrawBuffer* fDrawBuffer;
722
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000723 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000724
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000725 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000727 void setupDrawBuffer();
728
bsalomon@google.com27847de2011-02-22 20:59:41 +0000729 void flushDrawBuffer();
730
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000731 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000732
bsalomon@google.com27847de2011-02-22 20:59:41 +0000733 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
734
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000735 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000736 GrPathFill fill, const GrPoint* translate);
737
bsalomon@google.com6f379512011-11-16 20:36:03 +0000738 /**
739 * Flags to the internal read/write pixels funcs
740 */
741 enum PixelOpsFlags {
742 kDontFlush_PixelOpsFlag = 0x1,
743 };
744
745 bool internalReadRenderTargetPixels(GrRenderTarget* target,
746 int left, int top,
747 int width, int height,
748 GrPixelConfig config, void* buffer,
749 size_t rowBytes, uint32_t flags);
750
751 void internalWriteRenderTargetPixels(GrRenderTarget* target,
752 int left, int top,
753 int width, int height,
754 GrPixelConfig, const void* buffer,
755 size_t rowBytes, uint32_t flags);
756
757 bool internalReadTexturePixels(GrTexture* texture,
758 int left, int top,
759 int width, int height,
760 GrPixelConfig config, void* buffer,
761 size_t rowBytes, uint32_t flags);
762
763 void internalWriteTexturePixels(GrTexture* texture,
764 int left, int top,
765 int width, int height,
766 GrPixelConfig config, const void* buffer,
767 size_t rowBytes, uint32_t flags);
768 // needed for access to internalWriteTexturePixels. TODO: make GrContext
769 // be a facade for an internal class. Then functions that are public on the
770 // internal class would have only be callable in src/gpu. The facade would
771 // only have to functions necessary for clients.
772 friend class GrAtlas;
773
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000774 // computes vertex layout bits based on the paint. If paint expresses
775 // a texture for a stage, the stage coords will be bound to postitions
776 // unless hasTexCoords[s]==true in which case stage s's input coords
777 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
778 // for an array where all the values are false.
779 static int PaintStageVertexLayoutBits(
780 const GrPaint& paint,
781 const bool hasTexCoords[GrPaint::kTotalStages]);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000782
783 // Needed so GrTexture's returnToCache helper function can call
784 // addExistingTextureToCache
785 friend class GrTexture;
786
787 // Add an existing texture to the texture cache. This is intended solely
788 // for use with textures released from an GrAutoScratchTexture.
789 void addExistingTextureToCache(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790};
791
792/**
793 * Save/restore the view-matrix in the context.
794 */
795class GrAutoMatrix : GrNoncopyable {
796public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000797 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
799 fMatrix = ctx->getMatrix();
800 }
801 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
802 fMatrix = ctx->getMatrix();
803 ctx->setMatrix(matrix);
804 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000805 void set(GrContext* ctx) {
806 if (NULL != fContext) {
807 fContext->setMatrix(fMatrix);
808 }
809 fMatrix = ctx->getMatrix();
810 fContext = ctx;
811 }
812 void set(GrContext* ctx, const GrMatrix& matrix) {
813 if (NULL != fContext) {
814 fContext->setMatrix(fMatrix);
815 }
816 fMatrix = ctx->getMatrix();
817 ctx->setMatrix(matrix);
818 fContext = ctx;
819 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000821 if (NULL != fContext) {
822 fContext->setMatrix(fMatrix);
823 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000824 }
825
826private:
827 GrContext* fContext;
828 GrMatrix fMatrix;
829};
830
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000831/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000832 * Gets and locks a scratch texture from a descriptor using
833 * either exact or approximate criteria. Unlocks texture in
834 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000836class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000837public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000838 GrAutoScratchTexture()
839 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000841
842 GrAutoScratchTexture(GrContext* context,
843 const GrTextureDesc& desc,
844 GrContext::ScratchTexMatch match =
845 GrContext::kApprox_ScratchTexMatch)
846 : fContext(NULL) {
847 this->set(context, desc, match);
848 }
849
850 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000851 this->reset();
852 }
853
854 void reset() {
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000855 if (NULL != fContext && NULL != fEntry.cacheEntry()) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000856 fContext->unlockTexture(fEntry);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000857 fEntry.reset();
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000858 }
859 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000860
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000861 /*
862 * When detaching a texture we do not unlock it in the texture cache but
863 * we do set the returnToCache flag. In this way the texture remains
864 * "locked" in the texture cache until it is freed and recycled in
865 * GrTexture::internal_dispose. In reality, the texture has been removed
866 * from the cache (because this is in AutoScratchTexture) and by not
867 * calling unlockTexture we simply don't re-add it. It will be reattached
868 * in GrTexture::internal_dispose.
869 *
870 * Note that the caller is assumed to accept and manage the ref to the
871 * returned texture.
872 */
873 GrTexture* detach() {
874 GrTexture* temp = this->texture();
875
876 GrAssert(1 == temp->getRefCnt());
877
878 // freeEntry will remove the texture cache's ref
879 temp->ref();
880 fContext->freeEntry(fEntry);
881 fEntry.reset();
882
883 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
884 GrAssert(1 == temp->getRefCnt());
885 return temp;
886 }
887
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000888 GrTexture* set(GrContext* context,
889 const GrTextureDesc& desc,
890 GrContext::ScratchTexMatch match =
891 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000892 this->reset();
893
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000894 fContext = context;
895 if (NULL != fContext) {
896 fEntry = fContext->lockScratchTexture(desc, match);
897 GrTexture* ret = fEntry.texture();
898 if (NULL == ret) {
899 fContext = NULL;
900 }
901 return ret;
902 } else {
903 return NULL;
904 }
905 }
906
907 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000908private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000909 GrContext* fContext;
910 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000911};
912
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913#endif