blob: 33a3abf41db7a00d8833c9b3d76a637096ce3bfb [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;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000024class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000025class GrIndexBufferAllocPool;
26class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000027class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000028class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029class GrResourceEntry;
30class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000031class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000032class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000033class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000034class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035
bsalomon@google.com91826102011-03-21 19:51:57 +000036class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000037public:
38 /**
39 * Creates a GrContext from within a 3D context.
40 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000041 static GrContext* Create(GrEngine engine,
42 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000043
bsalomon@google.com27847de2011-02-22 20:59:41 +000044 virtual ~GrContext();
45
46 /**
47 * The GrContext normally assumes that no outsider is setting state
48 * within the underlying 3D API's context/device/whatever. This call informs
49 * the context that the state was modified and it should resend. Shouldn't
50 * be called frequently for good performance.
51 */
52 void resetContext();
53
bsalomon@google.com8fe72472011-03-30 21:26:44 +000054 /**
55 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
56 * if you have lost the associated GPU context, and thus internal texture,
57 * buffer, etc. references/IDs are now invalid. Should be called even when
58 * GrContext is no longer going to be used for two reasons:
59 * 1) ~GrContext will not try to free the objects in the 3D API.
60 * 2) If you've created GrResources that outlive the GrContext they will
61 * be marked as invalid (GrResource::isValid()) and won't attempt to
62 * free their underlying resource in the 3D API.
63 * Content drawn since the last GrContext::flush() may be lost.
64 */
65 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000066
67 /**
junov@google.com53a55842011-06-08 22:55:10 +000068 * Similar to contextLost, but makes no attempt to reset state.
69 * Use this method when GrContext destruction is pending, but
70 * the graphics context is destroyed first.
71 */
72 void contextDestroyed();
73
74 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 * Frees gpu created by the context. Can be called to reduce GPU memory
76 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000077 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 void freeGpuResources();
79
twiz@google.com05e70242012-01-27 19:12:00 +000080 /**
81 * Returns the number of bytes of GPU memory hosted by the texture cache.
82 */
83 size_t getGpuTextureCacheBytes() const;
84
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085 ///////////////////////////////////////////////////////////////////////////
86 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000087
88 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089 * Token that refers to an entry in the texture cache. Returned by
90 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000091 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000092 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000093 public:
94 TextureCacheEntry() : fEntry(NULL) {}
95 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
96 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
97 fEntry = e.fEntry;
98 return *this;
99 }
100 GrTexture* texture() const;
101 void reset() { fEntry = NULL; }
102 private:
103 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
104 void set(GrResourceEntry* entry) { fEntry = entry; }
105 GrResourceEntry* cacheEntry() { return fEntry; }
106 GrResourceEntry* fEntry;
107 friend class GrContext;
108 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000109
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000110 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000111 * Create a new entry, based on the specified key and texture, and return
112 * its "locked" entry. Must call be balanced with an unlockTexture() call.
113 *
114 * @param key A client-generated key that identifies the contents
115 * of the texture. Respecified to findAndLockTexture
116 * for subsequent uses of the texture.
117 * @param sampler The sampler state used to draw a texture may be used
118 * to determine how to store the pixel data in the texture
119 * cache. (e.g. different versions may exist for different
120 * wrap modes on GPUs with limited or no NPOT texture
121 * support). Only the wrap and filter fields are used. NULL
122 * implies clamp wrap modes and nearest filtering.
123 * @param desc Description of the texture properties.
124 * @param srcData Pointer to the pixel values.
125 * @param rowBytes The number of bytes between rows of the texture. Zero
126 * implies tightly packed rows.
127 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000128 TextureCacheEntry createAndLockTexture(GrTexture::TextureKey key,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000129 const GrSamplerState* sampler,
130 const GrTextureDesc& desc,
131 void* srcData, size_t rowBytes);
132
133 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000134 * Search for an entry based on key and dimensions. If found, "lock" it and
135 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000136 * Must be balanced with an unlockTexture() call.
137 *
138 * @param key A client-generated key that identifies the contents
139 * of the texture.
140 * @param width The width of the texture in pixels as specifed in
141 * the GrTextureDesc originally passed to
142 * createAndLockTexture
143 * @param width The height of the texture in pixels as specifed in
144 * the GrTextureDesc originally passed to
145 * createAndLockTexture
146 * @param sampler The sampler state used to draw a texture may be used
147 * to determine the cache entry used. (e.g. different
148 * versions may exist for different wrap modes on GPUs with
149 * limited or no NPOT texture support). Only the wrap and
150 * filter fields are used. NULL implies clamp wrap modes
151 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000153 TextureCacheEntry findAndLockTexture(GrTexture::TextureKey key,
154 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000155 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000156 /**
157 * Determines whether a texture is in the cache. If the texture is found it
158 * will not be locked or returned. This call does not affect the priority of
159 * the texture for deletion.
160 */
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000161 bool isTextureInCache(GrTexture::TextureKey key,
162 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000163 const GrSamplerState*) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000164
165 /**
166 * Enum that determines how closely a returned scratch texture must match
167 * a provided GrTextureDesc.
168 */
169 enum ScratchTexMatch {
170 /**
171 * Finds a texture that exactly matches the descriptor.
172 */
173 kExact_ScratchTexMatch,
174 /**
175 * Finds a texture that approximately matches the descriptor. Will be
176 * at least as large in width and height as desc specifies. If desc
177 * specifies that texture is a render target then result will be a
178 * render target. If desc specifies a render target and doesn't set the
179 * no stencil flag then result will have a stencil. Format and aa level
180 * will always match.
181 */
182 kApprox_ScratchTexMatch
183 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000184
185 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000186 * Returns a texture matching the desc. It's contents are unknown. Subsequent
187 * requests with the same descriptor are not guaranteed to return the same
188 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000189 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000190 *
191 * Textures created by createAndLockTexture() hide the complications of
192 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000193 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000194 * such an API will create gaps in the tiling pattern. This includes clamp
195 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000196 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000198
199 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000200 * When done with an entry, call unlockTexture(entry) on it, which returns
201 * it to the cache, where it may be purged.
202 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000203 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000204
205 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000206 * Creates a texture that is outside the cache. Does not count against
207 * cache's budget.
208 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000209 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000210 void* srcData,
211 size_t rowBytes);
212
213 /**
214 * Returns true if the specified use of an indexed texture is supported.
215 */
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000216 bool supportsIndex8PixelConfig(const GrSamplerState*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000217 int width,
218 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219
220 /**
221 * Return the current texture cache limits.
222 *
223 * @param maxTextures If non-null, returns maximum number of textures that
224 * can be held in the cache.
225 * @param maxTextureBytes If non-null, returns maximum number of bytes of
226 * texture memory that can be held in the cache.
227 */
228 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
229
230 /**
231 * Specify the texture cache limits. If the current cache exceeds either
232 * of these, it will be purged (LRU) to keep the cache within these limits.
233 *
234 * @param maxTextures The maximum number of textures that can be held in
235 * the cache.
236 * @param maxTextureBytes The maximum number of bytes of texture memory
237 * that can be held in the cache.
238 */
239 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
240
241 /**
242 * Return the max width or height of a texture supported by the current gpu
243 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000244 int getMaxTextureSize() const;
245
246 /**
247 * Return the max width or height of a render target supported by the
248 * current gpu
249 */
250 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000251
252 ///////////////////////////////////////////////////////////////////////////
253 // Render targets
254
255 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000256 * Sets the render target.
257 * @param target the render target to set. (should not be NULL.)
258 */
259 void setRenderTarget(GrRenderTarget* target);
260
261 /**
262 * Gets the current render target.
263 * @return the currently bound render target. Should never be NULL.
264 */
265 const GrRenderTarget* getRenderTarget() const;
266 GrRenderTarget* getRenderTarget();
267
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000268 /**
269 * Can the provided configuration act as a color render target?
270 */
271 bool isConfigRenderable(GrPixelConfig config) const;
272
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000273 ///////////////////////////////////////////////////////////////////////////
274 // Platform Surfaces
275
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000276 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000277 * Wraps an existing texture with a GrTexture object.
278 *
279 * OpenGL: if the object is a texture Gr may change its GL texture params
280 * when it is drawn.
281 *
282 * @param desc description of the object to create.
283 *
284 * @return GrTexture object or NULL on failure.
285 */
286 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
287
288 /**
289 * Wraps an existing render target with a GrRenderTarget object. It is
290 * similar to createPlatformTexture but can be used to draw into surfaces
291 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
292 * the client will resolve to a texture).
293 *
294 * @param desc description of the object to create.
295 *
296 * @return GrTexture object or NULL on failure.
297 */
298 GrRenderTarget* createPlatformRenderTarget(
299 const GrPlatformRenderTargetDesc& desc);
300
bsalomon@google.com27847de2011-02-22 20:59:41 +0000301 ///////////////////////////////////////////////////////////////////////////
302 // Matrix state
303
304 /**
305 * Gets the current transformation matrix.
306 * @return the current matrix.
307 */
308 const GrMatrix& getMatrix() const;
309
310 /**
311 * Sets the transformation matrix.
312 * @param m the matrix to set.
313 */
314 void setMatrix(const GrMatrix& m);
315
316 /**
317 * Concats the current matrix. The passed matrix is applied before the
318 * current matrix.
319 * @param m the matrix to concat.
320 */
321 void concatMatrix(const GrMatrix& m) const;
322
323
324 ///////////////////////////////////////////////////////////////////////////
325 // Clip state
326 /**
327 * Gets the current clip.
328 * @return the current clip.
329 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000330 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000331
332 /**
333 * Sets the clip.
334 * @param clip the clip to set.
335 */
336 void setClip(const GrClip& clip);
337
338 /**
339 * Convenience method for setting the clip to a rect.
340 * @param rect the rect to set as the new clip.
341 */
342 void setClip(const GrIRect& rect);
343
344 ///////////////////////////////////////////////////////////////////////////
345 // Draws
346
347 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000348 * Clear the entire or rect of the render target, ignoring any clips.
349 * @param rect the rect to clear or the whole thing if rect is NULL.
350 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000352 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353
354 /**
355 * Draw everywhere (respecting the clip) with the paint.
356 */
357 void drawPaint(const GrPaint& paint);
358
359 /**
360 * Draw the rect using a paint.
361 * @param paint describes how to color pixels.
362 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
363 * the rect is mitered stroked based on strokeWidth. If
364 * strokeWidth == 0, then the stroke is always a single
365 * pixel thick.
366 * @param matrix Optional matrix applied to the rect. Applied before
367 * context's matrix or the paint's matrix.
368 * The rects coords are used to access the paint (through texture matrix)
369 */
370 void drawRect(const GrPaint& paint,
371 const GrRect&,
372 GrScalar strokeWidth = -1,
373 const GrMatrix* matrix = NULL);
374
375 /**
376 * Maps a rect of paint coordinates onto the a rect of destination
377 * coordinates. Each rect can optionally be transformed. The srcRect
378 * is stretched over the dstRect. The dstRect is transformed by the
379 * context's matrix and the srcRect is transformed by the paint's matrix.
380 * Additional optional matrices can be provided by parameters.
381 *
382 * @param paint describes how to color pixels.
383 * @param dstRect the destination rect to draw.
384 * @param srcRect rect of paint coordinates to be mapped onto dstRect
385 * @param dstMatrix Optional matrix to transform dstRect. Applied before
386 * context's matrix.
387 * @param srcMatrix Optional matrix to transform srcRect Applied before
388 * paint's matrix.
389 */
390 void drawRectToRect(const GrPaint& paint,
391 const GrRect& dstRect,
392 const GrRect& srcRect,
393 const GrMatrix* dstMatrix = NULL,
394 const GrMatrix* srcMatrix = NULL);
395
396 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000397 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000398 *
399 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000400 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 * @param fill the path filling rule to use.
402 * @param translate optional additional translation applied to the
403 * path.
404 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000405 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000406 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000407
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408 /**
409 * Draws vertices with a paint.
410 *
411 * @param paint describes how to color pixels.
412 * @param primitiveType primitives type to draw.
413 * @param vertexCount number of vertices.
414 * @param positions array of vertex positions, required.
415 * @param texCoords optional array of texture coordinates used
416 * to access the paint.
417 * @param colors optional array of per-vertex colors, supercedes
418 * the paint's color field.
419 * @param indices optional array of indices. If NULL vertices
420 * are drawn non-indexed.
421 * @param indexCount if indices is non-null then this is the
422 * number of indices.
423 */
424 void drawVertices(const GrPaint& paint,
425 GrPrimitiveType primitiveType,
426 int vertexCount,
427 const GrPoint positions[],
428 const GrPoint texs[],
429 const GrColor colors[],
430 const uint16_t indices[],
431 int indexCount);
432
bsalomon@google.com93c96602012-04-27 13:05:21 +0000433 /**
434 * Draws an oval.
435 *
436 * @param paint describes how to color pixels.
437 * @param rect the bounding rect of the oval.
438 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
439 * the rect is stroked based on strokeWidth. If
440 * strokeWidth == 0, then the stroke is always a single
441 * pixel thick.
442 */
443 void drawOval(const GrPaint& paint,
444 const GrRect& rect,
445 SkScalar strokeWidth);
446
bsalomon@google.com27847de2011-02-22 20:59:41 +0000447 ///////////////////////////////////////////////////////////////////////////
448 // Misc.
449
450 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000451 * Flags that affect flush() behavior.
452 */
453 enum FlushBits {
454 /**
455 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
456 * it can be rendered to directly. However, Gr lazily sets state. Simply
457 * calling setRenderTarget() followed by flush() without flags may not
458 * bind the render target. This flag forces the context to bind the last
459 * set render target in the 3D API.
460 */
461 kForceCurrentRenderTarget_FlushBit = 0x1,
462 /**
463 * A client may reach a point where it has partially rendered a frame
464 * through a GrContext that it knows the user will never see. This flag
465 * causes the flush to skip submission of deferred content to the 3D API
466 * during the flush.
467 */
468 kDiscard_FlushBit = 0x2,
469 };
470
471 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000472 * Call to ensure all drawing to the context has been issued to the
473 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000474 * @param flagsBitfield flags that control the flushing behavior. See
475 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000476 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000477 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478
bsalomon@google.com27847de2011-02-22 20:59:41 +0000479 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000480 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000481 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000482 * current render target.
483 * @param left left edge of the rectangle to read (inclusive)
484 * @param top top edge of the rectangle to read (inclusive)
485 * @param width width of rectangle to read in pixels.
486 * @param height height of rectangle to read in pixels.
487 * @param config the pixel config of the destination buffer
488 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000489 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000490 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000491 *
492 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000493 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000494 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000495 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000496 bool readRenderTargetPixels(GrRenderTarget* target,
497 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000498 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000499 size_t rowBytes) {
500 return this->internalReadRenderTargetPixels(target, left, top,
501 width, height,
502 config, buffer,
503 rowBytes, 0);
504 }
505
506 /**
507 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
508 * at the specified rectangle.
509 * @param target the render target to write into. NULL means the
510 * current render target.
511 * @param left left edge of the rectangle to write (inclusive)
512 * @param top top edge of the rectangle to write (inclusive)
513 * @param width width of rectangle to write in pixels.
514 * @param height height of rectangle to write in pixels.
515 * @param config the pixel config of the source buffer
516 * @param buffer memory to read the rectangle from.
517 * @param rowBytes number of bytes bewtween consecutive rows. Zero
518 * means rows are tightly packed.
519 */
520 void writeRenderTargetPixels(GrRenderTarget* target,
521 int left, int top, int width, int height,
522 GrPixelConfig config, const void* buffer,
523 size_t rowBytes) {
524 this->internalWriteRenderTargetPixels(target, left, top, width, height,
525 config, buffer, rowBytes, 0);
526 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000527
528 /**
529 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000530 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000531 * @param left left edge of the rectangle to read (inclusive)
532 * @param top top edge of the rectangle to read (inclusive)
533 * @param width width of rectangle to read in pixels.
534 * @param height height of rectangle to read in pixels.
535 * @param config the pixel config of the destination buffer
536 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000537 * @param rowBytes number of bytes bewtween consecutive rows. Zero
538 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000539 *
540 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000541 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000542 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000544 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000545 GrPixelConfig config, void* buffer,
546 size_t rowBytes) {
547 return this->internalReadTexturePixels(texture, left, top,
548 width, height,
549 config, buffer, rowBytes, 0);
550 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551
552 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000553 * Writes a rectangle of pixels to a texture.
554 * @param texture the render target to read from.
555 * @param left left edge of the rectangle to write (inclusive)
556 * @param top top edge of the rectangle to write (inclusive)
557 * @param width width of rectangle to write in pixels.
558 * @param height height of rectangle to write in pixels.
559 * @param config the pixel config of the source buffer
560 * @param buffer memory to read pixels from
561 * @param rowBytes number of bytes bewtween consecutive rows. Zero
562 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000564 void writeTexturePixels(GrTexture* texture,
565 int left, int top, int width, int height,
566 GrPixelConfig config, const void* buffer,
567 size_t rowBytes) {
568 this->internalWriteTexturePixels(texture, left, top, width, height,
569 config, buffer, rowBytes, 0);
570 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000571 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000572 * Copies all texels from one texture to another.
573 * @param src the texture to copy from.
574 * @param dst the render target to copy to.
575 */
576 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000577
578 /**
579 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
580 * downsampled to the associated GrTexture (accessible via
581 * GrRenderTarget::asTexture()). Any pending draws to the render target will
582 * be executed before the resolve.
583 *
584 * This is only necessary when a client wants to access the object directly
585 * using the underlying graphics API. GrContext will detect when it must
586 * perform a resolve to a GrTexture used as the source of a draw or before
587 * reading pixels back from a GrTexture or GrRenderTarget.
588 */
589 void resolveRenderTarget(GrRenderTarget* target);
590
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000591 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000592 * Applies a 2D Gaussian blur to a given texture.
593 * @param srcTexture The source texture to be blurred.
594 * @param temp1 A scratch texture. Must not be NULL.
595 * @param temp2 A scratch texture. May be NULL, in which case
596 * srcTexture is overwritten with intermediate
597 * results.
598 * @param rect The destination rectangle.
599 * @param sigmaX The blur's standard deviation in X.
600 * @param sigmaY The blur's standard deviation in Y.
601 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000602 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000603 GrTexture* gaussianBlur(GrTexture* srcTexture,
604 GrAutoScratchTexture* temp1,
605 GrAutoScratchTexture* temp2,
606 const SkRect& rect,
607 float sigmaX, float sigmaY);
608
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000609 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000610 * This enum is used with the function below, applyMorphology.
611 */
612 enum MorphologyType {
613 kErode_MorphologyType,
614 kDilate_MorphologyType,
615 };
616
617 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000618 * Applies a 2D morphology to a given texture.
619 * @param srcTexture The source texture to be blurred.
620 * @param rect The destination rectangle.
621 * @param temp1 A scratch texture. Must not be NULL.
622 * @param temp2 A scratch texture. Must not be NULL.
623 * @param filter The morphology filter. Must be kDilate_Filter or
624 * kErode_Filter.
625 * @param radius The morphology radius in X and Y. The filter is
626 * applied to a fWidth by fHeight rectangle of
627 * pixels.
628 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000629 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000630 GrTexture* applyMorphology(GrTexture* srcTexture,
631 const GrRect& rect,
632 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000633 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000634 SkISize radius);
635
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636 ///////////////////////////////////////////////////////////////////////////
637 // Helpers
638
639 class AutoRenderTarget : ::GrNoncopyable {
640 public:
641 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
642 fContext = NULL;
643 fPrevTarget = context->getRenderTarget();
644 if (fPrevTarget != target) {
645 context->setRenderTarget(target);
646 fContext = context;
647 }
648 }
649 ~AutoRenderTarget() {
650 if (fContext) {
651 fContext->setRenderTarget(fPrevTarget);
652 }
653 }
654 private:
655 GrContext* fContext;
656 GrRenderTarget* fPrevTarget;
657 };
658
659
660 ///////////////////////////////////////////////////////////////////////////
661 // Functions intended for internal use only.
662 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000663 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000664 GrFontCache* getFontCache() { return fFontCache; }
665 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000666 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000667
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000668 /**
669 * Stencil buffers add themselves to the cache using
670 * addAndLockStencilBuffer. When a SB's RT-attachment count
671 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
672 * eligible for purging. findStencilBuffer is called to check the cache for
673 * a SB that matching an RT's criteria. If a match is found that has been
674 * unlocked (its attachment count has reached 0) then it will be relocked.
675 */
676 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
677 void unlockStencilBuffer(GrResourceEntry* sbEntry);
678 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000680 /*
681 * postClipPush acts as a hint to this and lower-level classes (e.g.,
682 * GrGpu) that the clip stack has changed.
683 */
684 virtual void postClipPush();
685
686 /*
687 * preClipPop acts as a hint that the clip stack has been restored to an
688 * earlier state.
689 */
690 virtual void preClipPop();
691
robertphillips@google.com2c756812012-05-22 20:28:23 +0000692 GrPathRenderer* getPathRenderer(const SkPath& path,
693 GrPathFill fill,
694 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000695 bool antiAlias,
696 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000697
bsalomon@google.com27847de2011-02-22 20:59:41 +0000698private:
699 // used to keep track of when we need to flush the draw buffer
700 enum DrawCategory {
701 kBuffered_DrawCategory, // last draw was inserted in draw buffer
702 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000703 };
704 DrawCategory fLastDrawCategory;
705
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000706 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000707 GrDrawState* fDrawState;
708
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000709 GrResourceCache* fTextureCache;
710 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000711
bsalomon@google.com30085192011-08-19 15:42:31 +0000712 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000713 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000714
715 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
716 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
717 GrInOrderDrawBuffer* fDrawBuffer;
718
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 GrIndexBuffer* fAAFillRectIndexBuffer;
720 GrIndexBuffer* fAAStrokeRectIndexBuffer;
721
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000723
bsalomon@google.com205d4602011-04-25 12:43:45 +0000724 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000725 const GrRect& devRect,
726 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000727
728 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000730 const GrVec& devStrokeSize,
731 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000732
733 inline int aaFillRectIndexCount() const;
734 GrIndexBuffer* aaFillRectIndexBuffer();
735
736 inline int aaStrokeRectIndexCount() const;
737 GrIndexBuffer* aaStrokeRectIndexBuffer();
738
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000739 void setupDrawBuffer();
740
bsalomon@google.com27847de2011-02-22 20:59:41 +0000741 void flushDrawBuffer();
742
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000743 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
746
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000747 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000748 GrPathFill fill, const GrPoint* translate);
749
bsalomon@google.com6f379512011-11-16 20:36:03 +0000750 /**
751 * Flags to the internal read/write pixels funcs
752 */
753 enum PixelOpsFlags {
754 kDontFlush_PixelOpsFlag = 0x1,
755 };
756
757 bool internalReadRenderTargetPixels(GrRenderTarget* target,
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 internalWriteRenderTargetPixels(GrRenderTarget* target,
764 int left, int top,
765 int width, int height,
766 GrPixelConfig, const void* buffer,
767 size_t rowBytes, uint32_t flags);
768
769 bool internalReadTexturePixels(GrTexture* texture,
770 int left, int top,
771 int width, int height,
772 GrPixelConfig config, void* buffer,
773 size_t rowBytes, uint32_t flags);
774
775 void internalWriteTexturePixels(GrTexture* texture,
776 int left, int top,
777 int width, int height,
778 GrPixelConfig config, const void* buffer,
779 size_t rowBytes, uint32_t flags);
780 // needed for access to internalWriteTexturePixels. TODO: make GrContext
781 // be a facade for an internal class. Then functions that are public on the
782 // internal class would have only be callable in src/gpu. The facade would
783 // only have to functions necessary for clients.
784 friend class GrAtlas;
785
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000786 // computes vertex layout bits based on the paint. If paint expresses
787 // a texture for a stage, the stage coords will be bound to postitions
788 // unless hasTexCoords[s]==true in which case stage s's input coords
789 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
790 // for an array where all the values are false.
791 static int PaintStageVertexLayoutBits(
792 const GrPaint& paint,
793 const bool hasTexCoords[GrPaint::kTotalStages]);
794
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795};
796
797/**
798 * Save/restore the view-matrix in the context.
799 */
800class GrAutoMatrix : GrNoncopyable {
801public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000802 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
804 fMatrix = ctx->getMatrix();
805 }
806 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
807 fMatrix = ctx->getMatrix();
808 ctx->setMatrix(matrix);
809 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000810 void set(GrContext* ctx) {
811 if (NULL != fContext) {
812 fContext->setMatrix(fMatrix);
813 }
814 fMatrix = ctx->getMatrix();
815 fContext = ctx;
816 }
817 void set(GrContext* ctx, const GrMatrix& matrix) {
818 if (NULL != fContext) {
819 fContext->setMatrix(fMatrix);
820 }
821 fMatrix = ctx->getMatrix();
822 ctx->setMatrix(matrix);
823 fContext = ctx;
824 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000826 if (NULL != fContext) {
827 fContext->setMatrix(fMatrix);
828 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829 }
830
831private:
832 GrContext* fContext;
833 GrMatrix fMatrix;
834};
835
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000836/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000837 * Gets and locks a scratch texture from a descriptor using
838 * either exact or approximate criteria. Unlocks texture in
839 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000841class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000842public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000843 GrAutoScratchTexture()
844 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000846
847 GrAutoScratchTexture(GrContext* context,
848 const GrTextureDesc& desc,
849 GrContext::ScratchTexMatch match =
850 GrContext::kApprox_ScratchTexMatch)
851 : fContext(NULL) {
852 this->set(context, desc, match);
853 }
854
855 ~GrAutoScratchTexture() {
856 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000857 fContext->unlockTexture(fEntry);
858 }
859 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000860
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000861 GrTexture* set(GrContext* context,
862 const GrTextureDesc& desc,
863 GrContext::ScratchTexMatch match =
864 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000865 if (NULL != fContext) {
866 fContext->unlockTexture(fEntry);
867 fEntry.reset();
868 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000869 fContext = context;
870 if (NULL != fContext) {
871 fEntry = fContext->lockScratchTexture(desc, match);
872 GrTexture* ret = fEntry.texture();
873 if (NULL == ret) {
874 fContext = NULL;
875 }
876 return ret;
877 } else {
878 return NULL;
879 }
880 }
881
882 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000883private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000884 GrContext* fContext;
885 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000886};
887
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888#endif