blob: 433c113a04027288a45d1f788f3ffb70c6d4cc64 [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;
35
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 /**
111 * Key generated by client. Should be a unique key on the texture data.
112 * Does not need to consider that width and height of the texture. Two
113 * textures with the same TextureKey but different bounds will not collide.
114 */
115 typedef uint64_t TextureKey;
116
117 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000118 * Create a new entry, based on the specified key and texture, and return
119 * its "locked" entry. Must call be balanced with an unlockTexture() call.
120 *
121 * @param key A client-generated key that identifies the contents
122 * of the texture. Respecified to findAndLockTexture
123 * for subsequent uses of the texture.
124 * @param sampler The sampler state used to draw a texture may be used
125 * to determine how to store the pixel data in the texture
126 * cache. (e.g. different versions may exist for different
127 * wrap modes on GPUs with limited or no NPOT texture
128 * support). Only the wrap and filter fields are used. NULL
129 * implies clamp wrap modes and nearest filtering.
130 * @param desc Description of the texture properties.
131 * @param srcData Pointer to the pixel values.
132 * @param rowBytes The number of bytes between rows of the texture. Zero
133 * implies tightly packed rows.
134 */
135 TextureCacheEntry createAndLockTexture(TextureKey key,
136 const GrSamplerState* sampler,
137 const GrTextureDesc& desc,
138 void* srcData, size_t rowBytes);
139
140 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000141 * Search for an entry based on key and dimensions. If found, "lock" it and
142 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000143 * Must be balanced with an unlockTexture() call.
144 *
145 * @param key A client-generated key that identifies the contents
146 * of the texture.
147 * @param width The width of the texture in pixels as specifed in
148 * the GrTextureDesc originally passed to
149 * createAndLockTexture
150 * @param width The height of the texture in pixels as specifed in
151 * the GrTextureDesc originally passed to
152 * createAndLockTexture
153 * @param sampler The sampler state used to draw a texture may be used
154 * to determine the cache entry used. (e.g. different
155 * versions may exist for different wrap modes on GPUs with
156 * limited or no NPOT texture support). Only the wrap and
157 * filter fields are used. NULL implies clamp wrap modes
158 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000159 */
160 TextureCacheEntry findAndLockTexture(TextureKey key,
161 int width,
162 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000163 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000164 /**
165 * Determines whether a texture is in the cache. If the texture is found it
166 * will not be locked or returned. This call does not affect the priority of
167 * the texture for deletion.
168 */
169 bool isTextureInCache(TextureKey key,
170 int width,
171 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000172 const GrSamplerState*) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000173
174 /**
175 * Enum that determines how closely a returned scratch texture must match
176 * a provided GrTextureDesc.
177 */
178 enum ScratchTexMatch {
179 /**
180 * Finds a texture that exactly matches the descriptor.
181 */
182 kExact_ScratchTexMatch,
183 /**
184 * Finds a texture that approximately matches the descriptor. Will be
185 * at least as large in width and height as desc specifies. If desc
186 * specifies that texture is a render target then result will be a
187 * render target. If desc specifies a render target and doesn't set the
188 * no stencil flag then result will have a stencil. Format and aa level
189 * will always match.
190 */
191 kApprox_ScratchTexMatch
192 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193
194 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000195 * Returns a texture matching the desc. It's contents are unknown. Subsequent
196 * requests with the same descriptor are not guaranteed to return the same
197 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000199 *
200 * Textures created by createAndLockTexture() hide the complications of
201 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000202 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000203 * such an API will create gaps in the tiling pattern. This includes clamp
204 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000205 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000206 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000207
208 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000209 * When done with an entry, call unlockTexture(entry) on it, which returns
210 * it to the cache, where it may be purged.
211 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000212 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213
214 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000215 * Creates a texture that is outside the cache. Does not count against
216 * cache's budget.
217 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000218 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219 void* srcData,
220 size_t rowBytes);
221
222 /**
223 * Returns true if the specified use of an indexed texture is supported.
224 */
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000225 bool supportsIndex8PixelConfig(const GrSamplerState*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000226 int width,
227 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228
229 /**
230 * Return the current texture cache limits.
231 *
232 * @param maxTextures If non-null, returns maximum number of textures that
233 * can be held in the cache.
234 * @param maxTextureBytes If non-null, returns maximum number of bytes of
235 * texture memory that can be held in the cache.
236 */
237 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
238
239 /**
240 * Specify the texture cache limits. If the current cache exceeds either
241 * of these, it will be purged (LRU) to keep the cache within these limits.
242 *
243 * @param maxTextures The maximum number of textures that can be held in
244 * the cache.
245 * @param maxTextureBytes The maximum number of bytes of texture memory
246 * that can be held in the cache.
247 */
248 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
249
250 /**
251 * Return the max width or height of a texture supported by the current gpu
252 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000253 int getMaxTextureSize() const;
254
255 /**
256 * Return the max width or height of a render target supported by the
257 * current gpu
258 */
259 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000260
261 ///////////////////////////////////////////////////////////////////////////
262 // Render targets
263
264 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000265 * Sets the render target.
266 * @param target the render target to set. (should not be NULL.)
267 */
268 void setRenderTarget(GrRenderTarget* target);
269
270 /**
271 * Gets the current render target.
272 * @return the currently bound render target. Should never be NULL.
273 */
274 const GrRenderTarget* getRenderTarget() const;
275 GrRenderTarget* getRenderTarget();
276
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000277 /**
278 * Can the provided configuration act as a color render target?
279 */
280 bool isConfigRenderable(GrPixelConfig config) const;
281
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000282 ///////////////////////////////////////////////////////////////////////////
283 // Platform Surfaces
284
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000285 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000286 * Wraps an existing texture with a GrTexture object.
287 *
288 * OpenGL: if the object is a texture Gr may change its GL texture params
289 * when it is drawn.
290 *
291 * @param desc description of the object to create.
292 *
293 * @return GrTexture object or NULL on failure.
294 */
295 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
296
297 /**
298 * Wraps an existing render target with a GrRenderTarget object. It is
299 * similar to createPlatformTexture but can be used to draw into surfaces
300 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
301 * the client will resolve to a texture).
302 *
303 * @param desc description of the object to create.
304 *
305 * @return GrTexture object or NULL on failure.
306 */
307 GrRenderTarget* createPlatformRenderTarget(
308 const GrPlatformRenderTargetDesc& desc);
309
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310 ///////////////////////////////////////////////////////////////////////////
311 // Matrix state
312
313 /**
314 * Gets the current transformation matrix.
315 * @return the current matrix.
316 */
317 const GrMatrix& getMatrix() const;
318
319 /**
320 * Sets the transformation matrix.
321 * @param m the matrix to set.
322 */
323 void setMatrix(const GrMatrix& m);
324
325 /**
326 * Concats the current matrix. The passed matrix is applied before the
327 * current matrix.
328 * @param m the matrix to concat.
329 */
330 void concatMatrix(const GrMatrix& m) const;
331
332
333 ///////////////////////////////////////////////////////////////////////////
334 // Clip state
335 /**
336 * Gets the current clip.
337 * @return the current clip.
338 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000339 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000340
341 /**
342 * Sets the clip.
343 * @param clip the clip to set.
344 */
345 void setClip(const GrClip& clip);
346
347 /**
348 * Convenience method for setting the clip to a rect.
349 * @param rect the rect to set as the new clip.
350 */
351 void setClip(const GrIRect& rect);
352
353 ///////////////////////////////////////////////////////////////////////////
354 // Draws
355
356 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000357 * Clear the entire or rect of the render target, ignoring any clips.
358 * @param rect the rect to clear or the whole thing if rect is NULL.
359 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000360 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000361 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000362
363 /**
364 * Draw everywhere (respecting the clip) with the paint.
365 */
366 void drawPaint(const GrPaint& paint);
367
368 /**
369 * Draw the rect using a paint.
370 * @param paint describes how to color pixels.
371 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
372 * the rect is mitered stroked based on strokeWidth. If
373 * strokeWidth == 0, then the stroke is always a single
374 * pixel thick.
375 * @param matrix Optional matrix applied to the rect. Applied before
376 * context's matrix or the paint's matrix.
377 * The rects coords are used to access the paint (through texture matrix)
378 */
379 void drawRect(const GrPaint& paint,
380 const GrRect&,
381 GrScalar strokeWidth = -1,
382 const GrMatrix* matrix = NULL);
383
384 /**
385 * Maps a rect of paint coordinates onto the a rect of destination
386 * coordinates. Each rect can optionally be transformed. The srcRect
387 * is stretched over the dstRect. The dstRect is transformed by the
388 * context's matrix and the srcRect is transformed by the paint's matrix.
389 * Additional optional matrices can be provided by parameters.
390 *
391 * @param paint describes how to color pixels.
392 * @param dstRect the destination rect to draw.
393 * @param srcRect rect of paint coordinates to be mapped onto dstRect
394 * @param dstMatrix Optional matrix to transform dstRect. Applied before
395 * context's matrix.
396 * @param srcMatrix Optional matrix to transform srcRect Applied before
397 * paint's matrix.
398 */
399 void drawRectToRect(const GrPaint& paint,
400 const GrRect& dstRect,
401 const GrRect& srcRect,
402 const GrMatrix* dstMatrix = NULL,
403 const GrMatrix* srcMatrix = NULL);
404
405 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000406 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000407 *
408 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000409 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000410 * @param fill the path filling rule to use.
411 * @param translate optional additional translation applied to the
412 * path.
413 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000414 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000415 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000416
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 /**
418 * Draws vertices with a paint.
419 *
420 * @param paint describes how to color pixels.
421 * @param primitiveType primitives type to draw.
422 * @param vertexCount number of vertices.
423 * @param positions array of vertex positions, required.
424 * @param texCoords optional array of texture coordinates used
425 * to access the paint.
426 * @param colors optional array of per-vertex colors, supercedes
427 * the paint's color field.
428 * @param indices optional array of indices. If NULL vertices
429 * are drawn non-indexed.
430 * @param indexCount if indices is non-null then this is the
431 * number of indices.
432 */
433 void drawVertices(const GrPaint& paint,
434 GrPrimitiveType primitiveType,
435 int vertexCount,
436 const GrPoint positions[],
437 const GrPoint texs[],
438 const GrColor colors[],
439 const uint16_t indices[],
440 int indexCount);
441
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 ///////////////////////////////////////////////////////////////////////////
443 // Misc.
444
445 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000446 * Flags that affect flush() behavior.
447 */
448 enum FlushBits {
449 /**
450 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
451 * it can be rendered to directly. However, Gr lazily sets state. Simply
452 * calling setRenderTarget() followed by flush() without flags may not
453 * bind the render target. This flag forces the context to bind the last
454 * set render target in the 3D API.
455 */
456 kForceCurrentRenderTarget_FlushBit = 0x1,
457 /**
458 * A client may reach a point where it has partially rendered a frame
459 * through a GrContext that it knows the user will never see. This flag
460 * causes the flush to skip submission of deferred content to the 3D API
461 * during the flush.
462 */
463 kDiscard_FlushBit = 0x2,
464 };
465
466 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467 * Call to ensure all drawing to the context has been issued to the
468 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469 * @param flagsBitfield flags that control the flushing behavior. See
470 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000471 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000472 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000473
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000475 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000476 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000477 * current render target.
478 * @param left left edge of the rectangle to read (inclusive)
479 * @param top top edge of the rectangle to read (inclusive)
480 * @param width width of rectangle to read in pixels.
481 * @param height height of rectangle to read in pixels.
482 * @param config the pixel config of the destination buffer
483 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000485 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000486 *
487 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000488 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000489 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000490 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000491 bool readRenderTargetPixels(GrRenderTarget* target,
492 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000493 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000494 size_t rowBytes) {
495 return this->internalReadRenderTargetPixels(target, left, top,
496 width, height,
497 config, buffer,
498 rowBytes, 0);
499 }
500
501 /**
502 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
503 * at the specified rectangle.
504 * @param target the render target to write into. NULL means the
505 * current render target.
506 * @param left left edge of the rectangle to write (inclusive)
507 * @param top top edge of the rectangle to write (inclusive)
508 * @param width width of rectangle to write in pixels.
509 * @param height height of rectangle to write in pixels.
510 * @param config the pixel config of the source buffer
511 * @param buffer memory to read the rectangle from.
512 * @param rowBytes number of bytes bewtween consecutive rows. Zero
513 * means rows are tightly packed.
514 */
515 void writeRenderTargetPixels(GrRenderTarget* target,
516 int left, int top, int width, int height,
517 GrPixelConfig config, const void* buffer,
518 size_t rowBytes) {
519 this->internalWriteRenderTargetPixels(target, left, top, width, height,
520 config, buffer, rowBytes, 0);
521 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000522
523 /**
524 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000525 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000526 * @param left left edge of the rectangle to read (inclusive)
527 * @param top top edge of the rectangle to read (inclusive)
528 * @param width width of rectangle to read in pixels.
529 * @param height height of rectangle to read in pixels.
530 * @param config the pixel config of the destination buffer
531 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000532 * @param rowBytes number of bytes bewtween consecutive rows. Zero
533 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000534 *
535 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000536 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000537 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000538 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000539 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000540 GrPixelConfig config, void* buffer,
541 size_t rowBytes) {
542 return this->internalReadTexturePixels(texture, left, top,
543 width, height,
544 config, buffer, rowBytes, 0);
545 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546
547 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000548 * Writes a rectangle of pixels to a texture.
549 * @param texture the render target to read from.
550 * @param left left edge of the rectangle to write (inclusive)
551 * @param top top edge of the rectangle to write (inclusive)
552 * @param width width of rectangle to write in pixels.
553 * @param height height of rectangle to write in pixels.
554 * @param config the pixel config of the source buffer
555 * @param buffer memory to read pixels from
556 * @param rowBytes number of bytes bewtween consecutive rows. Zero
557 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000559 void writeTexturePixels(GrTexture* texture,
560 int left, int top, int width, int height,
561 GrPixelConfig config, const void* buffer,
562 size_t rowBytes) {
563 this->internalWriteTexturePixels(texture, left, top, width, height,
564 config, buffer, rowBytes, 0);
565 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000566 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000567 * Copies all texels from one texture to another.
568 * @param src the texture to copy from.
569 * @param dst the render target to copy to.
570 */
571 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000572
573 /**
574 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
575 * downsampled to the associated GrTexture (accessible via
576 * GrRenderTarget::asTexture()). Any pending draws to the render target will
577 * be executed before the resolve.
578 *
579 * This is only necessary when a client wants to access the object directly
580 * using the underlying graphics API. GrContext will detect when it must
581 * perform a resolve to a GrTexture used as the source of a draw or before
582 * reading pixels back from a GrTexture or GrRenderTarget.
583 */
584 void resolveRenderTarget(GrRenderTarget* target);
585
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000586 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000587 * Applies a 2D Gaussian blur to a given texture.
588 * @param srcTexture The source texture to be blurred.
589 * @param temp1 A scratch texture. Must not be NULL.
590 * @param temp2 A scratch texture. May be NULL, in which case
591 * srcTexture is overwritten with intermediate
592 * results.
593 * @param rect The destination rectangle.
594 * @param sigmaX The blur's standard deviation in X.
595 * @param sigmaY The blur's standard deviation in Y.
596 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000597 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000598 GrTexture* gaussianBlur(GrTexture* srcTexture,
599 GrAutoScratchTexture* temp1,
600 GrAutoScratchTexture* temp2,
601 const SkRect& rect,
602 float sigmaX, float sigmaY);
603
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000604 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000605 * Applies a 2D morphology to a given texture.
606 * @param srcTexture The source texture to be blurred.
607 * @param rect The destination rectangle.
608 * @param temp1 A scratch texture. Must not be NULL.
609 * @param temp2 A scratch texture. Must not be NULL.
610 * @param filter The morphology filter. Must be kDilate_Filter or
611 * kErode_Filter.
612 * @param radius The morphology radius in X and Y. The filter is
613 * applied to a fWidth by fHeight rectangle of
614 * pixels.
615 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000616 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000617 GrTexture* applyMorphology(GrTexture* srcTexture,
618 const GrRect& rect,
619 GrTexture* temp1, GrTexture* temp2,
620 GrSamplerState::Filter filter,
621 SkISize radius);
622
bsalomon@google.com27847de2011-02-22 20:59:41 +0000623 ///////////////////////////////////////////////////////////////////////////
624 // Helpers
625
626 class AutoRenderTarget : ::GrNoncopyable {
627 public:
628 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
629 fContext = NULL;
630 fPrevTarget = context->getRenderTarget();
631 if (fPrevTarget != target) {
632 context->setRenderTarget(target);
633 fContext = context;
634 }
635 }
636 ~AutoRenderTarget() {
637 if (fContext) {
638 fContext->setRenderTarget(fPrevTarget);
639 }
640 }
641 private:
642 GrContext* fContext;
643 GrRenderTarget* fPrevTarget;
644 };
645
646
647 ///////////////////////////////////////////////////////////////////////////
648 // Functions intended for internal use only.
649 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000650 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000651 GrFontCache* getFontCache() { return fFontCache; }
652 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000654 void resetStats();
655 const GrGpuStats& getStats() const;
656 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000657 /**
658 * Stencil buffers add themselves to the cache using
659 * addAndLockStencilBuffer. When a SB's RT-attachment count
660 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
661 * eligible for purging. findStencilBuffer is called to check the cache for
662 * a SB that matching an RT's criteria. If a match is found that has been
663 * unlocked (its attachment count has reached 0) then it will be relocked.
664 */
665 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
666 void unlockStencilBuffer(GrResourceEntry* sbEntry);
667 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000668
669private:
670 // used to keep track of when we need to flush the draw buffer
671 enum DrawCategory {
672 kBuffered_DrawCategory, // last draw was inserted in draw buffer
673 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674 };
675 DrawCategory fLastDrawCategory;
676
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000677 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000678 GrDrawState* fDrawState;
679
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000680 GrResourceCache* fTextureCache;
681 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000682
bsalomon@google.com30085192011-08-19 15:42:31 +0000683 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000684
685 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
686 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
687 GrInOrderDrawBuffer* fDrawBuffer;
688
bsalomon@google.com205d4602011-04-25 12:43:45 +0000689 GrIndexBuffer* fAAFillRectIndexBuffer;
690 GrIndexBuffer* fAAStrokeRectIndexBuffer;
691
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000693
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 const GrRect& devRect,
696 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000697
698 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 const GrVec& devStrokeSize,
701 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702
703 inline int aaFillRectIndexCount() const;
704 GrIndexBuffer* aaFillRectIndexBuffer();
705
706 inline int aaStrokeRectIndexCount() const;
707 GrIndexBuffer* aaStrokeRectIndexBuffer();
708
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000709 void setupDrawBuffer();
710
bsalomon@google.com27847de2011-02-22 20:59:41 +0000711 void flushDrawBuffer();
712
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000713 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000714
bsalomon@google.com27847de2011-02-22 20:59:41 +0000715 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
716
bsalomon@google.com289533a2011-10-27 12:34:25 +0000717 GrPathRenderer* getPathRenderer(const GrPath& path,
718 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000719 const GrDrawTarget* target,
bsalomon@google.com289533a2011-10-27 12:34:25 +0000720 bool antiAlias);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000721
bsalomon@google.com6f379512011-11-16 20:36:03 +0000722 /**
723 * Flags to the internal read/write pixels funcs
724 */
725 enum PixelOpsFlags {
726 kDontFlush_PixelOpsFlag = 0x1,
727 };
728
729 bool internalReadRenderTargetPixels(GrRenderTarget* target,
730 int left, int top,
731 int width, int height,
732 GrPixelConfig config, void* buffer,
733 size_t rowBytes, uint32_t flags);
734
735 void internalWriteRenderTargetPixels(GrRenderTarget* target,
736 int left, int top,
737 int width, int height,
738 GrPixelConfig, const void* buffer,
739 size_t rowBytes, uint32_t flags);
740
741 bool internalReadTexturePixels(GrTexture* texture,
742 int left, int top,
743 int width, int height,
744 GrPixelConfig config, void* buffer,
745 size_t rowBytes, uint32_t flags);
746
747 void internalWriteTexturePixels(GrTexture* texture,
748 int left, int top,
749 int width, int height,
750 GrPixelConfig config, const void* buffer,
751 size_t rowBytes, uint32_t flags);
752 // needed for access to internalWriteTexturePixels. TODO: make GrContext
753 // be a facade for an internal class. Then functions that are public on the
754 // internal class would have only be callable in src/gpu. The facade would
755 // only have to functions necessary for clients.
756 friend class GrAtlas;
757
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000758 // computes vertex layout bits based on the paint. If paint expresses
759 // a texture for a stage, the stage coords will be bound to postitions
760 // unless hasTexCoords[s]==true in which case stage s's input coords
761 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
762 // for an array where all the values are false.
763 static int PaintStageVertexLayoutBits(
764 const GrPaint& paint,
765 const bool hasTexCoords[GrPaint::kTotalStages]);
766
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767};
768
769/**
770 * Save/restore the view-matrix in the context.
771 */
772class GrAutoMatrix : GrNoncopyable {
773public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000774 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
776 fMatrix = ctx->getMatrix();
777 }
778 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
779 fMatrix = ctx->getMatrix();
780 ctx->setMatrix(matrix);
781 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000782 void set(GrContext* ctx) {
783 if (NULL != fContext) {
784 fContext->setMatrix(fMatrix);
785 }
786 fMatrix = ctx->getMatrix();
787 fContext = ctx;
788 }
789 void set(GrContext* ctx, const GrMatrix& matrix) {
790 if (NULL != fContext) {
791 fContext->setMatrix(fMatrix);
792 }
793 fMatrix = ctx->getMatrix();
794 ctx->setMatrix(matrix);
795 fContext = ctx;
796 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000798 if (NULL != fContext) {
799 fContext->setMatrix(fMatrix);
800 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 }
802
803private:
804 GrContext* fContext;
805 GrMatrix fMatrix;
806};
807
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000808/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000809 * Gets and locks a scratch texture from a descriptor using
810 * either exact or approximate criteria. Unlocks texture in
811 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000812 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000813class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000814public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000815 GrAutoScratchTexture()
816 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000817 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000818
819 GrAutoScratchTexture(GrContext* context,
820 const GrTextureDesc& desc,
821 GrContext::ScratchTexMatch match =
822 GrContext::kApprox_ScratchTexMatch)
823 : fContext(NULL) {
824 this->set(context, desc, match);
825 }
826
827 ~GrAutoScratchTexture() {
828 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000829 fContext->unlockTexture(fEntry);
830 }
831 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000832
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000833 GrTexture* set(GrContext* context,
834 const GrTextureDesc& desc,
835 GrContext::ScratchTexMatch match =
836 GrContext::kApprox_ScratchTexMatch) {
837 if (NULL != fContext) {
838 fContext->unlockTexture(fEntry);
839 }
840 fContext = context;
841 if (NULL != fContext) {
842 fEntry = fContext->lockScratchTexture(desc, match);
843 GrTexture* ret = fEntry.texture();
844 if (NULL == ret) {
845 fContext = NULL;
846 }
847 return ret;
848 } else {
849 return NULL;
850 }
851 }
852
853 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000854private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000855 GrContext* fContext;
856 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000857};
858
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859#endif