blob: adceb778c5841345b8482abd6030204ad7e85178 [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"
bsalomon@google.comc287a892011-08-19 14:49:36 +000015// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
16// remove.
17#include "GrRenderTarget.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000019class GrAutoScratchTexture;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000020class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000021class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000022class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000023class GrGpu;
24struct GrGpuStats;
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.com27847de2011-02-22 20:59:41 +000045 virtual ~GrContext();
46
47 /**
48 * The GrContext normally assumes that no outsider is setting state
49 * within the underlying 3D API's context/device/whatever. This call informs
50 * the context that the state was modified and it should resend. Shouldn't
51 * be called frequently for good performance.
52 */
53 void resetContext();
54
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 /**
56 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
57 * if you have lost the associated GPU context, and thus internal texture,
58 * buffer, etc. references/IDs are now invalid. Should be called even when
59 * GrContext is no longer going to be used for two reasons:
60 * 1) ~GrContext will not try to free the objects in the 3D API.
61 * 2) If you've created GrResources that outlive the GrContext they will
62 * be marked as invalid (GrResource::isValid()) and won't attempt to
63 * free their underlying resource in the 3D API.
64 * Content drawn since the last GrContext::flush() may be lost.
65 */
66 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000067
68 /**
junov@google.com53a55842011-06-08 22:55:10 +000069 * Similar to contextLost, but makes no attempt to reset state.
70 * Use this method when GrContext destruction is pending, but
71 * the graphics context is destroyed first.
72 */
73 void contextDestroyed();
74
75 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 * Frees gpu created by the context. Can be called to reduce GPU memory
77 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 void freeGpuResources();
80
twiz@google.com05e70242012-01-27 19:12:00 +000081 /**
82 * Returns the number of bytes of GPU memory hosted by the texture cache.
83 */
84 size_t getGpuTextureCacheBytes() const;
85
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 ///////////////////////////////////////////////////////////////////////////
87 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000088
89 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 * Token that refers to an entry in the texture cache. Returned by
91 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000092 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000093 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000094 public:
95 TextureCacheEntry() : fEntry(NULL) {}
96 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
97 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
98 fEntry = e.fEntry;
99 return *this;
100 }
101 GrTexture* texture() const;
102 void reset() { fEntry = NULL; }
103 private:
104 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
105 void set(GrResourceEntry* entry) { fEntry = entry; }
106 GrResourceEntry* cacheEntry() { return fEntry; }
107 GrResourceEntry* fEntry;
108 friend class GrContext;
109 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000111 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000112 * Create a new entry, based on the specified key and texture, and return
113 * its "locked" entry. Must call be balanced with an unlockTexture() call.
114 *
115 * @param key A client-generated key that identifies the contents
116 * of the texture. Respecified to findAndLockTexture
117 * for subsequent uses of the texture.
118 * @param sampler The sampler state used to draw a texture may be used
119 * to determine how to store the pixel data in the texture
120 * cache. (e.g. different versions may exist for different
121 * wrap modes on GPUs with limited or no NPOT texture
122 * support). Only the wrap and filter fields are used. NULL
123 * implies clamp wrap modes and nearest filtering.
124 * @param desc Description of the texture properties.
125 * @param srcData Pointer to the pixel values.
126 * @param rowBytes The number of bytes between rows of the texture. Zero
127 * implies tightly packed rows.
128 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000129 TextureCacheEntry createAndLockTexture(GrTexture::TextureKey key,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000130 const GrSamplerState* sampler,
131 const GrTextureDesc& desc,
132 void* srcData, size_t rowBytes);
133
134 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000135 * Search for an entry based on key and dimensions. If found, "lock" it and
136 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000137 * Must be balanced with an unlockTexture() call.
138 *
139 * @param key A client-generated key that identifies the contents
140 * of the texture.
141 * @param width The width of the texture in pixels as specifed in
142 * the GrTextureDesc originally passed to
143 * createAndLockTexture
144 * @param width The height of the texture in pixels as specifed in
145 * the GrTextureDesc originally passed to
146 * createAndLockTexture
147 * @param sampler The sampler state used to draw a texture may be used
148 * to determine the cache entry used. (e.g. different
149 * versions may exist for different wrap modes on GPUs with
150 * limited or no NPOT texture support). Only the wrap and
151 * filter fields are used. NULL implies clamp wrap modes
152 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000153 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000154 TextureCacheEntry findAndLockTexture(GrTexture::TextureKey key,
155 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000156 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000157 /**
158 * Determines whether a texture is in the cache. If the texture is found it
159 * will not be locked or returned. This call does not affect the priority of
160 * the texture for deletion.
161 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000162 bool isTextureInCache(GrTexture::TextureKey key,
163 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000164 const GrSamplerState*) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000165
166 /**
167 * Enum that determines how closely a returned scratch texture must match
168 * a provided GrTextureDesc.
169 */
170 enum ScratchTexMatch {
171 /**
172 * Finds a texture that exactly matches the descriptor.
173 */
174 kExact_ScratchTexMatch,
175 /**
176 * Finds a texture that approximately matches the descriptor. Will be
177 * at least as large in width and height as desc specifies. If desc
178 * specifies that texture is a render target then result will be a
179 * render target. If desc specifies a render target and doesn't set the
180 * no stencil flag then result will have a stencil. Format and aa level
181 * will always match.
182 */
183 kApprox_ScratchTexMatch
184 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000185
186 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000187 * Returns a texture matching the desc. It's contents are unknown. Subsequent
188 * requests with the same descriptor are not guaranteed to return the same
189 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000190 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000191 *
192 * Textures created by createAndLockTexture() hide the complications of
193 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000195 * such an API will create gaps in the tiling pattern. This includes clamp
196 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000197 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000199
200 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000201 * When done with an entry, call unlockTexture(entry) on it, which returns
202 * it to the cache, where it may be purged.
203 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000204 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000205
206 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207 * Creates a texture that is outside the cache. Does not count against
208 * cache's budget.
209 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000210 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211 void* srcData,
212 size_t rowBytes);
213
214 /**
215 * Returns true if the specified use of an indexed texture is supported.
216 */
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000217 bool supportsIndex8PixelConfig(const GrSamplerState*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000218 int width,
219 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220
221 /**
222 * Return the current texture cache limits.
223 *
224 * @param maxTextures If non-null, returns maximum number of textures that
225 * can be held in the cache.
226 * @param maxTextureBytes If non-null, returns maximum number of bytes of
227 * texture memory that can be held in the cache.
228 */
229 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
230
231 /**
232 * Specify the texture cache limits. If the current cache exceeds either
233 * of these, it will be purged (LRU) to keep the cache within these limits.
234 *
235 * @param maxTextures The maximum number of textures that can be held in
236 * the cache.
237 * @param maxTextureBytes The maximum number of bytes of texture memory
238 * that can be held in the cache.
239 */
240 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
241
242 /**
243 * Return the max width or height of a texture supported by the current gpu
244 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000245 int getMaxTextureSize() const;
246
247 /**
248 * Return the max width or height of a render target supported by the
249 * current gpu
250 */
251 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000252
253 ///////////////////////////////////////////////////////////////////////////
254 // Render targets
255
256 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000257 * Sets the render target.
258 * @param target the render target to set. (should not be NULL.)
259 */
260 void setRenderTarget(GrRenderTarget* target);
261
262 /**
263 * Gets the current render target.
264 * @return the currently bound render target. Should never be NULL.
265 */
266 const GrRenderTarget* getRenderTarget() const;
267 GrRenderTarget* getRenderTarget();
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.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000352 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000353 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000354
355 /**
356 * Draw everywhere (respecting the clip) with the paint.
357 */
358 void drawPaint(const GrPaint& paint);
359
360 /**
361 * Draw the rect using a paint.
362 * @param paint describes how to color pixels.
363 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
364 * the rect is mitered stroked based on strokeWidth. If
365 * strokeWidth == 0, then the stroke is always a single
366 * pixel thick.
367 * @param matrix Optional matrix applied to the rect. Applied before
368 * context's matrix or the paint's matrix.
369 * The rects coords are used to access the paint (through texture matrix)
370 */
371 void drawRect(const GrPaint& paint,
372 const GrRect&,
373 GrScalar strokeWidth = -1,
374 const GrMatrix* matrix = NULL);
375
376 /**
377 * Maps a rect of paint coordinates onto the a rect of destination
378 * coordinates. Each rect can optionally be transformed. The srcRect
379 * is stretched over the dstRect. The dstRect is transformed by the
380 * context's matrix and the srcRect is transformed by the paint's matrix.
381 * Additional optional matrices can be provided by parameters.
382 *
383 * @param paint describes how to color pixels.
384 * @param dstRect the destination rect to draw.
385 * @param srcRect rect of paint coordinates to be mapped onto dstRect
386 * @param dstMatrix Optional matrix to transform dstRect. Applied before
387 * context's matrix.
388 * @param srcMatrix Optional matrix to transform srcRect Applied before
389 * paint's matrix.
390 */
391 void drawRectToRect(const GrPaint& paint,
392 const GrRect& dstRect,
393 const GrRect& srcRect,
394 const GrMatrix* dstMatrix = NULL,
395 const GrMatrix* srcMatrix = NULL);
396
397 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000398 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 *
400 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000401 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 * @param fill the path filling rule to use.
403 * @param translate optional additional translation applied to the
404 * path.
405 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000406 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000407 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000408
bsalomon@google.com27847de2011-02-22 20:59:41 +0000409 /**
410 * Draws vertices with a paint.
411 *
412 * @param paint describes how to color pixels.
413 * @param primitiveType primitives type to draw.
414 * @param vertexCount number of vertices.
415 * @param positions array of vertex positions, required.
416 * @param texCoords optional array of texture coordinates used
417 * to access the paint.
418 * @param colors optional array of per-vertex colors, supercedes
419 * the paint's color field.
420 * @param indices optional array of indices. If NULL vertices
421 * are drawn non-indexed.
422 * @param indexCount if indices is non-null then this is the
423 * number of indices.
424 */
425 void drawVertices(const GrPaint& paint,
426 GrPrimitiveType primitiveType,
427 int vertexCount,
428 const GrPoint positions[],
429 const GrPoint texs[],
430 const GrColor colors[],
431 const uint16_t indices[],
432 int indexCount);
433
bsalomon@google.com93c96602012-04-27 13:05:21 +0000434 /**
435 * Draws an oval.
436 *
437 * @param paint describes how to color pixels.
438 * @param rect the bounding rect of the oval.
439 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
440 * the rect is stroked based on strokeWidth. If
441 * strokeWidth == 0, then the stroke is always a single
442 * pixel thick.
443 */
444 void drawOval(const GrPaint& paint,
445 const GrRect& rect,
446 SkScalar strokeWidth);
447
bsalomon@google.com27847de2011-02-22 20:59:41 +0000448 ///////////////////////////////////////////////////////////////////////////
449 // Misc.
450
451 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452 * Flags that affect flush() behavior.
453 */
454 enum FlushBits {
455 /**
456 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
457 * it can be rendered to directly. However, Gr lazily sets state. Simply
458 * calling setRenderTarget() followed by flush() without flags may not
459 * bind the render target. This flag forces the context to bind the last
460 * set render target in the 3D API.
461 */
462 kForceCurrentRenderTarget_FlushBit = 0x1,
463 /**
464 * A client may reach a point where it has partially rendered a frame
465 * through a GrContext that it knows the user will never see. This flag
466 * causes the flush to skip submission of deferred content to the 3D API
467 * during the flush.
468 */
469 kDiscard_FlushBit = 0x2,
470 };
471
472 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000473 * Call to ensure all drawing to the context has been issued to the
474 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000475 * @param flagsBitfield flags that control the flushing behavior. See
476 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000477 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000478 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000479
bsalomon@google.com27847de2011-02-22 20:59:41 +0000480 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000482 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000483 * current render target.
484 * @param left left edge of the rectangle to read (inclusive)
485 * @param top top edge of the rectangle to read (inclusive)
486 * @param width width of rectangle to read in pixels.
487 * @param height height of rectangle to read in pixels.
488 * @param config the pixel config of the destination buffer
489 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000490 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000491 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000492 *
493 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000494 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000495 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497 bool readRenderTargetPixels(GrRenderTarget* target,
498 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000499 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000500 size_t rowBytes) {
501 return this->internalReadRenderTargetPixels(target, left, top,
502 width, height,
503 config, buffer,
504 rowBytes, 0);
505 }
506
507 /**
508 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
509 * at the specified rectangle.
510 * @param target the render target to write into. NULL means the
511 * current render target.
512 * @param left left edge of the rectangle to write (inclusive)
513 * @param top top edge of the rectangle to write (inclusive)
514 * @param width width of rectangle to write in pixels.
515 * @param height height of rectangle to write in pixels.
516 * @param config the pixel config of the source buffer
517 * @param buffer memory to read the rectangle from.
518 * @param rowBytes number of bytes bewtween consecutive rows. Zero
519 * means rows are tightly packed.
520 */
521 void writeRenderTargetPixels(GrRenderTarget* target,
522 int left, int top, int width, int height,
523 GrPixelConfig config, const void* buffer,
524 size_t rowBytes) {
525 this->internalWriteRenderTargetPixels(target, left, top, width, height,
526 config, buffer, rowBytes, 0);
527 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000528
529 /**
530 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000532 * @param left left edge of the rectangle to read (inclusive)
533 * @param top top edge of the rectangle to read (inclusive)
534 * @param width width of rectangle to read in pixels.
535 * @param height height of rectangle to read in pixels.
536 * @param config the pixel config of the destination buffer
537 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000538 * @param rowBytes number of bytes bewtween consecutive rows. Zero
539 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000540 *
541 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000545 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000546 GrPixelConfig config, void* buffer,
547 size_t rowBytes) {
548 return this->internalReadTexturePixels(texture, left, top,
549 width, height,
550 config, buffer, rowBytes, 0);
551 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552
553 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554 * Writes a rectangle of pixels to a texture.
555 * @param texture the render target to read from.
556 * @param left left edge of the rectangle to write (inclusive)
557 * @param top top edge of the rectangle to write (inclusive)
558 * @param width width of rectangle to write in pixels.
559 * @param height height of rectangle to write in pixels.
560 * @param config the pixel config of the source buffer
561 * @param buffer memory to read pixels from
562 * @param rowBytes number of bytes bewtween consecutive rows. Zero
563 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000564 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000565 void writeTexturePixels(GrTexture* texture,
566 int left, int top, int width, int height,
567 GrPixelConfig config, const void* buffer,
568 size_t rowBytes) {
569 this->internalWriteTexturePixels(texture, left, top, width, height,
570 config, buffer, rowBytes, 0);
571 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000572 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000573 * Copies all texels from one texture to another.
574 * @param src the texture to copy from.
575 * @param dst the render target to copy to.
576 */
577 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000578
579 /**
580 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
581 * downsampled to the associated GrTexture (accessible via
582 * GrRenderTarget::asTexture()). Any pending draws to the render target will
583 * be executed before the resolve.
584 *
585 * This is only necessary when a client wants to access the object directly
586 * using the underlying graphics API. GrContext will detect when it must
587 * perform a resolve to a GrTexture used as the source of a draw or before
588 * reading pixels back from a GrTexture or GrRenderTarget.
589 */
590 void resolveRenderTarget(GrRenderTarget* target);
591
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000592 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000593 * Applies a 2D Gaussian blur to a given texture.
594 * @param srcTexture The source texture to be blurred.
595 * @param temp1 A scratch texture. Must not be NULL.
596 * @param temp2 A scratch texture. May be NULL, in which case
597 * srcTexture is overwritten with intermediate
598 * results.
599 * @param rect The destination rectangle.
600 * @param sigmaX The blur's standard deviation in X.
601 * @param sigmaY The blur's standard deviation in Y.
602 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000603 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000604 GrTexture* gaussianBlur(GrTexture* srcTexture,
605 GrAutoScratchTexture* temp1,
606 GrAutoScratchTexture* temp2,
607 const SkRect& rect,
608 float sigmaX, float sigmaY);
609
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000610 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000611 * This enum is used with the function below, applyMorphology.
612 */
613 enum MorphologyType {
614 kErode_MorphologyType,
615 kDilate_MorphologyType,
616 };
617
618 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000619 * Applies a 2D morphology to a given texture.
620 * @param srcTexture The source texture to be blurred.
621 * @param rect The destination rectangle.
622 * @param temp1 A scratch texture. Must not be NULL.
623 * @param temp2 A scratch texture. Must not be NULL.
624 * @param filter The morphology filter. Must be kDilate_Filter or
625 * kErode_Filter.
626 * @param radius The morphology radius in X and Y. The filter is
627 * applied to a fWidth by fHeight rectangle of
628 * pixels.
629 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000630 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000631 GrTexture* applyMorphology(GrTexture* srcTexture,
632 const GrRect& rect,
633 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000634 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000635 SkISize radius);
636
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637 ///////////////////////////////////////////////////////////////////////////
638 // Helpers
639
640 class AutoRenderTarget : ::GrNoncopyable {
641 public:
642 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
643 fContext = NULL;
644 fPrevTarget = context->getRenderTarget();
645 if (fPrevTarget != target) {
646 context->setRenderTarget(target);
647 fContext = context;
648 }
649 }
650 ~AutoRenderTarget() {
651 if (fContext) {
652 fContext->setRenderTarget(fPrevTarget);
653 }
654 }
655 private:
656 GrContext* fContext;
657 GrRenderTarget* fPrevTarget;
658 };
659
660
661 ///////////////////////////////////////////////////////////////////////////
662 // Functions intended for internal use only.
663 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000664 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665 GrFontCache* getFontCache() { return fFontCache; }
666 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000667 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000668 void resetStats();
669 const GrGpuStats& getStats() const;
670 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000671 /**
672 * Stencil buffers add themselves to the cache using
673 * addAndLockStencilBuffer. When a SB's RT-attachment count
674 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
675 * eligible for purging. findStencilBuffer is called to check the cache for
676 * a SB that matching an RT's criteria. If a match is found that has been
677 * unlocked (its attachment count has reached 0) then it will be relocked.
678 */
679 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
680 void unlockStencilBuffer(GrResourceEntry* sbEntry);
681 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000682
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000683 /*
684 * postClipPush acts as a hint to this and lower-level classes (e.g.,
685 * GrGpu) that the clip stack has changed.
686 */
687 virtual void postClipPush();
688
689 /*
690 * preClipPop acts as a hint that the clip stack has been restored to an
691 * earlier state.
692 */
693 virtual void preClipPop();
694
robertphillips@google.com2c756812012-05-22 20:28:23 +0000695 GrPathRenderer* getPathRenderer(const SkPath& path,
696 GrPathFill fill,
697 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000698 bool antiAlias,
699 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000700
bsalomon@google.com27847de2011-02-22 20:59:41 +0000701private:
702 // used to keep track of when we need to flush the draw buffer
703 enum DrawCategory {
704 kBuffered_DrawCategory, // last draw was inserted in draw buffer
705 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000706 };
707 DrawCategory fLastDrawCategory;
708
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000709 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000710 GrDrawState* fDrawState;
711
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000712 GrResourceCache* fTextureCache;
713 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000714
bsalomon@google.com30085192011-08-19 15:42:31 +0000715 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000716 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000717
718 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
719 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
720 GrInOrderDrawBuffer* fDrawBuffer;
721
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 GrIndexBuffer* fAAFillRectIndexBuffer;
723 GrIndexBuffer* fAAStrokeRectIndexBuffer;
724
bsalomon@google.com27847de2011-02-22 20:59:41 +0000725 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000726
bsalomon@google.com205d4602011-04-25 12:43:45 +0000727 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000728 const GrRect& devRect,
729 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730
731 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000732 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000733 const GrVec& devStrokeSize,
734 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735
736 inline int aaFillRectIndexCount() const;
737 GrIndexBuffer* aaFillRectIndexBuffer();
738
739 inline int aaStrokeRectIndexCount() const;
740 GrIndexBuffer* aaStrokeRectIndexBuffer();
741
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000742 void setupDrawBuffer();
743
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744 void flushDrawBuffer();
745
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000746 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000747
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
749
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000750 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000751 GrPathFill fill, const GrPoint* translate);
752
bsalomon@google.com6f379512011-11-16 20:36:03 +0000753 /**
754 * Flags to the internal read/write pixels funcs
755 */
756 enum PixelOpsFlags {
757 kDontFlush_PixelOpsFlag = 0x1,
758 };
759
760 bool internalReadRenderTargetPixels(GrRenderTarget* target,
761 int left, int top,
762 int width, int height,
763 GrPixelConfig config, void* buffer,
764 size_t rowBytes, uint32_t flags);
765
766 void internalWriteRenderTargetPixels(GrRenderTarget* target,
767 int left, int top,
768 int width, int height,
769 GrPixelConfig, const void* buffer,
770 size_t rowBytes, uint32_t flags);
771
772 bool internalReadTexturePixels(GrTexture* texture,
773 int left, int top,
774 int width, int height,
775 GrPixelConfig config, void* buffer,
776 size_t rowBytes, uint32_t flags);
777
778 void internalWriteTexturePixels(GrTexture* texture,
779 int left, int top,
780 int width, int height,
781 GrPixelConfig config, const void* buffer,
782 size_t rowBytes, uint32_t flags);
783 // needed for access to internalWriteTexturePixels. TODO: make GrContext
784 // be a facade for an internal class. Then functions that are public on the
785 // internal class would have only be callable in src/gpu. The facade would
786 // only have to functions necessary for clients.
787 friend class GrAtlas;
788
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000789 // computes vertex layout bits based on the paint. If paint expresses
790 // a texture for a stage, the stage coords will be bound to postitions
791 // unless hasTexCoords[s]==true in which case stage s's input coords
792 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
793 // for an array where all the values are false.
794 static int PaintStageVertexLayoutBits(
795 const GrPaint& paint,
796 const bool hasTexCoords[GrPaint::kTotalStages]);
797
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798};
799
800/**
801 * Save/restore the view-matrix in the context.
802 */
803class GrAutoMatrix : GrNoncopyable {
804public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000805 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
807 fMatrix = ctx->getMatrix();
808 }
809 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
810 fMatrix = ctx->getMatrix();
811 ctx->setMatrix(matrix);
812 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000813 void set(GrContext* ctx) {
814 if (NULL != fContext) {
815 fContext->setMatrix(fMatrix);
816 }
817 fMatrix = ctx->getMatrix();
818 fContext = ctx;
819 }
820 void set(GrContext* ctx, const GrMatrix& matrix) {
821 if (NULL != fContext) {
822 fContext->setMatrix(fMatrix);
823 }
824 fMatrix = ctx->getMatrix();
825 ctx->setMatrix(matrix);
826 fContext = ctx;
827 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000829 if (NULL != fContext) {
830 fContext->setMatrix(fMatrix);
831 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000832 }
833
834private:
835 GrContext* fContext;
836 GrMatrix fMatrix;
837};
838
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000839/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000840 * Gets and locks a scratch texture from a descriptor using
841 * either exact or approximate criteria. Unlocks texture in
842 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000844class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000846 GrAutoScratchTexture()
847 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000848 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849
850 GrAutoScratchTexture(GrContext* context,
851 const GrTextureDesc& desc,
852 GrContext::ScratchTexMatch match =
853 GrContext::kApprox_ScratchTexMatch)
854 : fContext(NULL) {
855 this->set(context, desc, match);
856 }
857
858 ~GrAutoScratchTexture() {
859 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000860 fContext->unlockTexture(fEntry);
861 }
862 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000863
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000864 GrTexture* set(GrContext* context,
865 const GrTextureDesc& desc,
866 GrContext::ScratchTexMatch match =
867 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000868 if (NULL != fContext) {
869 fContext->unlockTexture(fEntry);
870 fEntry.reset();
871 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000872 fContext = context;
873 if (NULL != fContext) {
874 fEntry = fContext->lockScratchTexture(desc, match);
875 GrTexture* ret = fEntry.texture();
876 if (NULL == ret) {
877 fContext = NULL;
878 }
879 return ret;
880 } else {
881 return NULL;
882 }
883 }
884
885 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000886private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000887 GrContext* fContext;
888 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000889};
890
bsalomon@google.com27847de2011-02-22 20:59:41 +0000891#endif