blob: ae0e5652bd8a203654715877e5ca18fda51e4f91 [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.com583a1e32011-08-17 13:42:46 +000020class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000021class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000022class GrGpu;
23struct GrGpuStats;
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;
34
bsalomon@google.com91826102011-03-21 19:51:57 +000035class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000036public:
37 /**
38 * Creates a GrContext from within a 3D context.
39 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000040 static GrContext* Create(GrEngine engine,
41 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000042
bsalomon@google.com27847de2011-02-22 20:59:41 +000043 virtual ~GrContext();
44
45 /**
46 * The GrContext normally assumes that no outsider is setting state
47 * within the underlying 3D API's context/device/whatever. This call informs
48 * the context that the state was modified and it should resend. Shouldn't
49 * be called frequently for good performance.
50 */
51 void resetContext();
52
bsalomon@google.com8fe72472011-03-30 21:26:44 +000053 /**
54 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
55 * if you have lost the associated GPU context, and thus internal texture,
56 * buffer, etc. references/IDs are now invalid. Should be called even when
57 * GrContext is no longer going to be used for two reasons:
58 * 1) ~GrContext will not try to free the objects in the 3D API.
59 * 2) If you've created GrResources that outlive the GrContext they will
60 * be marked as invalid (GrResource::isValid()) and won't attempt to
61 * free their underlying resource in the 3D API.
62 * Content drawn since the last GrContext::flush() may be lost.
63 */
64 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000065
66 /**
junov@google.com53a55842011-06-08 22:55:10 +000067 * Similar to contextLost, but makes no attempt to reset state.
68 * Use this method when GrContext destruction is pending, but
69 * the graphics context is destroyed first.
70 */
71 void contextDestroyed();
72
73 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000074 * Frees gpu created by the context. Can be called to reduce GPU memory
75 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000076 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000077 void freeGpuResources();
78
twiz@google.com05e70242012-01-27 19:12:00 +000079 /**
80 * Returns the number of bytes of GPU memory hosted by the texture cache.
81 */
82 size_t getGpuTextureCacheBytes() const;
83
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 ///////////////////////////////////////////////////////////////////////////
85 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000086
87 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000088 * Token that refers to an entry in the texture cache. Returned by
89 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000090 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000091 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000092 public:
93 TextureCacheEntry() : fEntry(NULL) {}
94 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
95 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
96 fEntry = e.fEntry;
97 return *this;
98 }
99 GrTexture* texture() const;
100 void reset() { fEntry = NULL; }
101 private:
102 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
103 void set(GrResourceEntry* entry) { fEntry = entry; }
104 GrResourceEntry* cacheEntry() { return fEntry; }
105 GrResourceEntry* fEntry;
106 friend class GrContext;
107 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000108
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000109 /**
110 * Key generated by client. Should be a unique key on the texture data.
111 * Does not need to consider that width and height of the texture. Two
112 * textures with the same TextureKey but different bounds will not collide.
113 */
114 typedef uint64_t TextureKey;
115
116 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000117 * Create a new entry, based on the specified key and texture, and return
118 * its "locked" entry. Must call be balanced with an unlockTexture() call.
119 *
120 * @param key A client-generated key that identifies the contents
121 * of the texture. Respecified to findAndLockTexture
122 * for subsequent uses of the texture.
123 * @param sampler The sampler state used to draw a texture may be used
124 * to determine how to store the pixel data in the texture
125 * cache. (e.g. different versions may exist for different
126 * wrap modes on GPUs with limited or no NPOT texture
127 * support). Only the wrap and filter fields are used. NULL
128 * implies clamp wrap modes and nearest filtering.
129 * @param desc Description of the texture properties.
130 * @param srcData Pointer to the pixel values.
131 * @param rowBytes The number of bytes between rows of the texture. Zero
132 * implies tightly packed rows.
133 */
134 TextureCacheEntry createAndLockTexture(TextureKey key,
135 const GrSamplerState* sampler,
136 const GrTextureDesc& desc,
137 void* srcData, size_t rowBytes);
138
139 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000140 * Search for an entry based on key and dimensions. If found, "lock" it and
141 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000142 * Must be balanced with an unlockTexture() call.
143 *
144 * @param key A client-generated key that identifies the contents
145 * of the texture.
146 * @param width The width of the texture in pixels as specifed in
147 * the GrTextureDesc originally passed to
148 * createAndLockTexture
149 * @param width The height of the texture in pixels as specifed in
150 * the GrTextureDesc originally passed to
151 * createAndLockTexture
152 * @param sampler The sampler state used to draw a texture may be used
153 * to determine the cache entry used. (e.g. different
154 * versions may exist for different wrap modes on GPUs with
155 * limited or no NPOT texture support). Only the wrap and
156 * filter fields are used. NULL implies clamp wrap modes
157 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000158 */
159 TextureCacheEntry findAndLockTexture(TextureKey key,
160 int width,
161 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000162 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000163 /**
164 * Determines whether a texture is in the cache. If the texture is found it
165 * will not be locked or returned. This call does not affect the priority of
166 * the texture for deletion.
167 */
168 bool isTextureInCache(TextureKey key,
169 int width,
170 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000171 const GrSamplerState*) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000172
173 /**
174 * Enum that determines how closely a returned scratch texture must match
175 * a provided GrTextureDesc.
176 */
177 enum ScratchTexMatch {
178 /**
179 * Finds a texture that exactly matches the descriptor.
180 */
181 kExact_ScratchTexMatch,
182 /**
183 * Finds a texture that approximately matches the descriptor. Will be
184 * at least as large in width and height as desc specifies. If desc
185 * specifies that texture is a render target then result will be a
186 * render target. If desc specifies a render target and doesn't set the
187 * no stencil flag then result will have a stencil. Format and aa level
188 * will always match.
189 */
190 kApprox_ScratchTexMatch
191 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000192
193 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000194 * Returns a texture matching the desc. It's contents are unknown. Subsequent
195 * requests with the same descriptor are not guaranteed to return the same
196 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000198 *
199 * Textures created by createAndLockTexture() hide the complications of
200 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000201 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000202 * such an API will create gaps in the tiling pattern. This includes clamp
203 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000204 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000205 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000206
207 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000208 * When done with an entry, call unlockTexture(entry) on it, which returns
209 * it to the cache, where it may be purged.
210 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000211 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000212
213 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214 * Creates a texture that is outside the cache. Does not count against
215 * cache's budget.
216 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000217 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218 void* srcData,
219 size_t rowBytes);
220
221 /**
222 * Returns true if the specified use of an indexed texture is supported.
223 */
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000224 bool supportsIndex8PixelConfig(const GrSamplerState*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000225 int width,
226 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000227
228 /**
229 * Return the current texture cache limits.
230 *
231 * @param maxTextures If non-null, returns maximum number of textures that
232 * can be held in the cache.
233 * @param maxTextureBytes If non-null, returns maximum number of bytes of
234 * texture memory that can be held in the cache.
235 */
236 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
237
238 /**
239 * Specify the texture cache limits. If the current cache exceeds either
240 * of these, it will be purged (LRU) to keep the cache within these limits.
241 *
242 * @param maxTextures The maximum number of textures that can be held in
243 * the cache.
244 * @param maxTextureBytes The maximum number of bytes of texture memory
245 * that can be held in the cache.
246 */
247 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
248
249 /**
250 * Return the max width or height of a texture supported by the current gpu
251 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000252 int getMaxTextureSize() const;
253
254 /**
255 * Return the max width or height of a render target supported by the
256 * current gpu
257 */
258 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000259
260 ///////////////////////////////////////////////////////////////////////////
261 // Render targets
262
263 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000264 * Sets the render target.
265 * @param target the render target to set. (should not be NULL.)
266 */
267 void setRenderTarget(GrRenderTarget* target);
268
269 /**
270 * Gets the current render target.
271 * @return the currently bound render target. Should never be NULL.
272 */
273 const GrRenderTarget* getRenderTarget() const;
274 GrRenderTarget* getRenderTarget();
275
276 ///////////////////////////////////////////////////////////////////////////
277 // Platform Surfaces
278
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000279 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000280 * Wraps an existing texture with a GrTexture object.
281 *
282 * OpenGL: if the object is a texture Gr may change its GL texture params
283 * when it is drawn.
284 *
285 * @param desc description of the object to create.
286 *
287 * @return GrTexture object or NULL on failure.
288 */
289 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
290
291 /**
292 * Wraps an existing render target with a GrRenderTarget object. It is
293 * similar to createPlatformTexture but can be used to draw into surfaces
294 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
295 * the client will resolve to a texture).
296 *
297 * @param desc description of the object to create.
298 *
299 * @return GrTexture object or NULL on failure.
300 */
301 GrRenderTarget* createPlatformRenderTarget(
302 const GrPlatformRenderTargetDesc& desc);
303
bsalomon@google.com27847de2011-02-22 20:59:41 +0000304 ///////////////////////////////////////////////////////////////////////////
305 // Matrix state
306
307 /**
308 * Gets the current transformation matrix.
309 * @return the current matrix.
310 */
311 const GrMatrix& getMatrix() const;
312
313 /**
314 * Sets the transformation matrix.
315 * @param m the matrix to set.
316 */
317 void setMatrix(const GrMatrix& m);
318
319 /**
320 * Concats the current matrix. The passed matrix is applied before the
321 * current matrix.
322 * @param m the matrix to concat.
323 */
324 void concatMatrix(const GrMatrix& m) const;
325
326
327 ///////////////////////////////////////////////////////////////////////////
328 // Clip state
329 /**
330 * Gets the current clip.
331 * @return the current clip.
332 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000333 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000334
335 /**
336 * Sets the clip.
337 * @param clip the clip to set.
338 */
339 void setClip(const GrClip& clip);
340
341 /**
342 * Convenience method for setting the clip to a rect.
343 * @param rect the rect to set as the new clip.
344 */
345 void setClip(const GrIRect& rect);
346
347 ///////////////////////////////////////////////////////////////////////////
348 // Draws
349
350 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000351 * Clear the entire or rect of the render target, ignoring any clips.
352 * @param rect the rect to clear or the whole thing if rect is NULL.
353 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000354 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000355 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356
357 /**
358 * Draw everywhere (respecting the clip) with the paint.
359 */
360 void drawPaint(const GrPaint& paint);
361
362 /**
363 * Draw the rect using a paint.
364 * @param paint describes how to color pixels.
365 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
366 * the rect is mitered stroked based on strokeWidth. If
367 * strokeWidth == 0, then the stroke is always a single
368 * pixel thick.
369 * @param matrix Optional matrix applied to the rect. Applied before
370 * context's matrix or the paint's matrix.
371 * The rects coords are used to access the paint (through texture matrix)
372 */
373 void drawRect(const GrPaint& paint,
374 const GrRect&,
375 GrScalar strokeWidth = -1,
376 const GrMatrix* matrix = NULL);
377
378 /**
379 * Maps a rect of paint coordinates onto the a rect of destination
380 * coordinates. Each rect can optionally be transformed. The srcRect
381 * is stretched over the dstRect. The dstRect is transformed by the
382 * context's matrix and the srcRect is transformed by the paint's matrix.
383 * Additional optional matrices can be provided by parameters.
384 *
385 * @param paint describes how to color pixels.
386 * @param dstRect the destination rect to draw.
387 * @param srcRect rect of paint coordinates to be mapped onto dstRect
388 * @param dstMatrix Optional matrix to transform dstRect. Applied before
389 * context's matrix.
390 * @param srcMatrix Optional matrix to transform srcRect Applied before
391 * paint's matrix.
392 */
393 void drawRectToRect(const GrPaint& paint,
394 const GrRect& dstRect,
395 const GrRect& srcRect,
396 const GrMatrix* dstMatrix = NULL,
397 const GrMatrix* srcMatrix = NULL);
398
399 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000400 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 *
402 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000403 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000404 * @param fill the path filling rule to use.
405 * @param translate optional additional translation applied to the
406 * path.
407 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000408 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000409 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000410
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411 /**
412 * Draws vertices with a paint.
413 *
414 * @param paint describes how to color pixels.
415 * @param primitiveType primitives type to draw.
416 * @param vertexCount number of vertices.
417 * @param positions array of vertex positions, required.
418 * @param texCoords optional array of texture coordinates used
419 * to access the paint.
420 * @param colors optional array of per-vertex colors, supercedes
421 * the paint's color field.
422 * @param indices optional array of indices. If NULL vertices
423 * are drawn non-indexed.
424 * @param indexCount if indices is non-null then this is the
425 * number of indices.
426 */
427 void drawVertices(const GrPaint& paint,
428 GrPrimitiveType primitiveType,
429 int vertexCount,
430 const GrPoint positions[],
431 const GrPoint texs[],
432 const GrColor colors[],
433 const uint16_t indices[],
434 int indexCount);
435
bsalomon@google.com27847de2011-02-22 20:59:41 +0000436 ///////////////////////////////////////////////////////////////////////////
437 // Misc.
438
439 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000440 * Flags that affect flush() behavior.
441 */
442 enum FlushBits {
443 /**
444 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
445 * it can be rendered to directly. However, Gr lazily sets state. Simply
446 * calling setRenderTarget() followed by flush() without flags may not
447 * bind the render target. This flag forces the context to bind the last
448 * set render target in the 3D API.
449 */
450 kForceCurrentRenderTarget_FlushBit = 0x1,
451 /**
452 * A client may reach a point where it has partially rendered a frame
453 * through a GrContext that it knows the user will never see. This flag
454 * causes the flush to skip submission of deferred content to the 3D API
455 * during the flush.
456 */
457 kDiscard_FlushBit = 0x2,
458 };
459
460 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000461 * Call to ensure all drawing to the context has been issued to the
462 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000463 * @param flagsBitfield flags that control the flushing behavior. See
464 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000465 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000466 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000467
bsalomon@google.com27847de2011-02-22 20:59:41 +0000468 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000469 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000470 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000471 * current render target.
472 * @param left left edge of the rectangle to read (inclusive)
473 * @param top top edge of the rectangle to read (inclusive)
474 * @param width width of rectangle to read in pixels.
475 * @param height height of rectangle to read in pixels.
476 * @param config the pixel config of the destination buffer
477 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000479 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000480 *
481 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000482 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000483 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000484 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000485 bool readRenderTargetPixels(GrRenderTarget* target,
486 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000487 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000488 size_t rowBytes) {
489 return this->internalReadRenderTargetPixels(target, left, top,
490 width, height,
491 config, buffer,
492 rowBytes, 0);
493 }
494
495 /**
496 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
497 * at the specified rectangle.
498 * @param target the render target to write into. NULL means the
499 * current render target.
500 * @param left left edge of the rectangle to write (inclusive)
501 * @param top top edge of the rectangle to write (inclusive)
502 * @param width width of rectangle to write in pixels.
503 * @param height height of rectangle to write in pixels.
504 * @param config the pixel config of the source buffer
505 * @param buffer memory to read the rectangle from.
506 * @param rowBytes number of bytes bewtween consecutive rows. Zero
507 * means rows are tightly packed.
508 */
509 void writeRenderTargetPixels(GrRenderTarget* target,
510 int left, int top, int width, int height,
511 GrPixelConfig config, const void* buffer,
512 size_t rowBytes) {
513 this->internalWriteRenderTargetPixels(target, left, top, width, height,
514 config, buffer, rowBytes, 0);
515 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000516
517 /**
518 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000519 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000520 * @param left left edge of the rectangle to read (inclusive)
521 * @param top top edge of the rectangle to read (inclusive)
522 * @param width width of rectangle to read in pixels.
523 * @param height height of rectangle to read in pixels.
524 * @param config the pixel config of the destination buffer
525 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000526 * @param rowBytes number of bytes bewtween consecutive rows. Zero
527 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000528 *
529 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000530 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000531 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000532 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000533 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 GrPixelConfig config, void* buffer,
535 size_t rowBytes) {
536 return this->internalReadTexturePixels(texture, left, top,
537 width, height,
538 config, buffer, rowBytes, 0);
539 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540
541 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 * Writes a rectangle of pixels to a texture.
543 * @param texture the render target to read from.
544 * @param left left edge of the rectangle to write (inclusive)
545 * @param top top edge of the rectangle to write (inclusive)
546 * @param width width of rectangle to write in pixels.
547 * @param height height of rectangle to write in pixels.
548 * @param config the pixel config of the source buffer
549 * @param buffer memory to read pixels from
550 * @param rowBytes number of bytes bewtween consecutive rows. Zero
551 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000553 void writeTexturePixels(GrTexture* texture,
554 int left, int top, int width, int height,
555 GrPixelConfig config, const void* buffer,
556 size_t rowBytes) {
557 this->internalWriteTexturePixels(texture, left, top, width, height,
558 config, buffer, rowBytes, 0);
559 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000560 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000561 * Copies all texels from one texture to another.
562 * @param src the texture to copy from.
563 * @param dst the render target to copy to.
564 */
565 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000566
567 /**
568 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
569 * downsampled to the associated GrTexture (accessible via
570 * GrRenderTarget::asTexture()). Any pending draws to the render target will
571 * be executed before the resolve.
572 *
573 * This is only necessary when a client wants to access the object directly
574 * using the underlying graphics API. GrContext will detect when it must
575 * perform a resolve to a GrTexture used as the source of a draw or before
576 * reading pixels back from a GrTexture or GrRenderTarget.
577 */
578 void resolveRenderTarget(GrRenderTarget* target);
579
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000580 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000581 * Applies a 2D Gaussian blur to a given texture.
582 * @param srcTexture The source texture to be blurred.
583 * @param temp1 A scratch texture. Must not be NULL.
584 * @param temp2 A scratch texture. May be NULL, in which case
585 * srcTexture is overwritten with intermediate
586 * results.
587 * @param rect The destination rectangle.
588 * @param sigmaX The blur's standard deviation in X.
589 * @param sigmaY The blur's standard deviation in Y.
590 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000591 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000592 GrTexture* gaussianBlur(GrTexture* srcTexture,
593 GrAutoScratchTexture* temp1,
594 GrAutoScratchTexture* temp2,
595 const SkRect& rect,
596 float sigmaX, float sigmaY);
597
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000598 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000599 * Applies a 2D morphology to a given texture.
600 * @param srcTexture The source texture to be blurred.
601 * @param rect The destination rectangle.
602 * @param temp1 A scratch texture. Must not be NULL.
603 * @param temp2 A scratch texture. Must not be NULL.
604 * @param filter The morphology filter. Must be kDilate_Filter or
605 * kErode_Filter.
606 * @param radius The morphology radius in X and Y. The filter is
607 * applied to a fWidth by fHeight rectangle of
608 * pixels.
609 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000610 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000611 GrTexture* applyMorphology(GrTexture* srcTexture,
612 const GrRect& rect,
613 GrTexture* temp1, GrTexture* temp2,
614 GrSamplerState::Filter filter,
615 SkISize radius);
616
bsalomon@google.com27847de2011-02-22 20:59:41 +0000617 ///////////////////////////////////////////////////////////////////////////
618 // Helpers
619
620 class AutoRenderTarget : ::GrNoncopyable {
621 public:
622 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
623 fContext = NULL;
624 fPrevTarget = context->getRenderTarget();
625 if (fPrevTarget != target) {
626 context->setRenderTarget(target);
627 fContext = context;
628 }
629 }
630 ~AutoRenderTarget() {
631 if (fContext) {
632 fContext->setRenderTarget(fPrevTarget);
633 }
634 }
635 private:
636 GrContext* fContext;
637 GrRenderTarget* fPrevTarget;
638 };
639
640
641 ///////////////////////////////////////////////////////////////////////////
642 // Functions intended for internal use only.
643 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000644 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000645 GrFontCache* getFontCache() { return fFontCache; }
646 GrDrawTarget* getTextTarget(const GrPaint& paint);
647 void flushText();
648 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000649 void resetStats();
650 const GrGpuStats& getStats() const;
651 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000652 /**
653 * Stencil buffers add themselves to the cache using
654 * addAndLockStencilBuffer. When a SB's RT-attachment count
655 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
656 * eligible for purging. findStencilBuffer is called to check the cache for
657 * a SB that matching an RT's criteria. If a match is found that has been
658 * unlocked (its attachment count has reached 0) then it will be relocked.
659 */
660 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
661 void unlockStencilBuffer(GrResourceEntry* sbEntry);
662 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000663
664private:
665 // used to keep track of when we need to flush the draw buffer
666 enum DrawCategory {
667 kBuffered_DrawCategory, // last draw was inserted in draw buffer
668 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
669 kText_DrawCategory // text context was last to draw
670 };
671 DrawCategory fLastDrawCategory;
672
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000673 GrGpu* fGpu;
674 GrResourceCache* fTextureCache;
675 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000676
bsalomon@google.com30085192011-08-19 15:42:31 +0000677 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000678
679 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
680 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
681 GrInOrderDrawBuffer* fDrawBuffer;
682
bsalomon@google.com205d4602011-04-25 12:43:45 +0000683 GrIndexBuffer* fAAFillRectIndexBuffer;
684 GrIndexBuffer* fAAStrokeRectIndexBuffer;
685
bsalomon@google.com27847de2011-02-22 20:59:41 +0000686 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000687
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000689 const GrRect& devRect,
690 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000691
692 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000694 const GrVec& devStrokeSize,
695 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696
697 inline int aaFillRectIndexCount() const;
698 GrIndexBuffer* aaFillRectIndexBuffer();
699
700 inline int aaStrokeRectIndexCount() const;
701 GrIndexBuffer* aaStrokeRectIndexBuffer();
702
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000703 void setupDrawBuffer();
704
bsalomon@google.com27847de2011-02-22 20:59:41 +0000705 void flushDrawBuffer();
706
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000707 void setPaint(const GrPaint& paint, GrDrawTarget* target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000708
bsalomon@google.com27847de2011-02-22 20:59:41 +0000709 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
710
bsalomon@google.com289533a2011-10-27 12:34:25 +0000711 GrPathRenderer* getPathRenderer(const GrPath& path,
712 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000713 const GrDrawTarget* target,
bsalomon@google.com289533a2011-10-27 12:34:25 +0000714 bool antiAlias);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000715
bsalomon@google.com6f379512011-11-16 20:36:03 +0000716 /**
717 * Flags to the internal read/write pixels funcs
718 */
719 enum PixelOpsFlags {
720 kDontFlush_PixelOpsFlag = 0x1,
721 };
722
723 bool internalReadRenderTargetPixels(GrRenderTarget* target,
724 int left, int top,
725 int width, int height,
726 GrPixelConfig config, void* buffer,
727 size_t rowBytes, uint32_t flags);
728
729 void internalWriteRenderTargetPixels(GrRenderTarget* target,
730 int left, int top,
731 int width, int height,
732 GrPixelConfig, const void* buffer,
733 size_t rowBytes, uint32_t flags);
734
735 bool internalReadTexturePixels(GrTexture* texture,
736 int left, int top,
737 int width, int height,
738 GrPixelConfig config, void* buffer,
739 size_t rowBytes, uint32_t flags);
740
741 void internalWriteTexturePixels(GrTexture* texture,
742 int left, int top,
743 int width, int height,
744 GrPixelConfig config, const void* buffer,
745 size_t rowBytes, uint32_t flags);
746 // needed for access to internalWriteTexturePixels. TODO: make GrContext
747 // be a facade for an internal class. Then functions that are public on the
748 // internal class would have only be callable in src/gpu. The facade would
749 // only have to functions necessary for clients.
750 friend class GrAtlas;
751
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000752 // computes vertex layout bits based on the paint. If paint expresses
753 // a texture for a stage, the stage coords will be bound to postitions
754 // unless hasTexCoords[s]==true in which case stage s's input coords
755 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
756 // for an array where all the values are false.
757 static int PaintStageVertexLayoutBits(
758 const GrPaint& paint,
759 const bool hasTexCoords[GrPaint::kTotalStages]);
760
bsalomon@google.com27847de2011-02-22 20:59:41 +0000761};
762
763/**
764 * Save/restore the view-matrix in the context.
765 */
766class GrAutoMatrix : GrNoncopyable {
767public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000768 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000769 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
770 fMatrix = ctx->getMatrix();
771 }
772 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
773 fMatrix = ctx->getMatrix();
774 ctx->setMatrix(matrix);
775 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000776 void set(GrContext* ctx) {
777 if (NULL != fContext) {
778 fContext->setMatrix(fMatrix);
779 }
780 fMatrix = ctx->getMatrix();
781 fContext = ctx;
782 }
783 void set(GrContext* ctx, const GrMatrix& matrix) {
784 if (NULL != fContext) {
785 fContext->setMatrix(fMatrix);
786 }
787 fMatrix = ctx->getMatrix();
788 ctx->setMatrix(matrix);
789 fContext = ctx;
790 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000792 if (NULL != fContext) {
793 fContext->setMatrix(fMatrix);
794 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 }
796
797private:
798 GrContext* fContext;
799 GrMatrix fMatrix;
800};
801
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000802/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000803 * Gets and locks a scratch texture from a descriptor using
804 * either exact or approximate criteria. Unlocks texture in
805 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000806 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000807class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000808public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000809 GrAutoScratchTexture()
810 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000811 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000812
813 GrAutoScratchTexture(GrContext* context,
814 const GrTextureDesc& desc,
815 GrContext::ScratchTexMatch match =
816 GrContext::kApprox_ScratchTexMatch)
817 : fContext(NULL) {
818 this->set(context, desc, match);
819 }
820
821 ~GrAutoScratchTexture() {
822 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000823 fContext->unlockTexture(fEntry);
824 }
825 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000826
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000827 GrTexture* set(GrContext* context,
828 const GrTextureDesc& desc,
829 GrContext::ScratchTexMatch match =
830 GrContext::kApprox_ScratchTexMatch) {
831 if (NULL != fContext) {
832 fContext->unlockTexture(fEntry);
833 }
834 fContext = context;
835 if (NULL != fContext) {
836 fEntry = fContext->lockScratchTexture(desc, match);
837 GrTexture* ret = fEntry.texture();
838 if (NULL == ret) {
839 fContext = NULL;
840 }
841 return ret;
842 } else {
843 return NULL;
844 }
845 }
846
847 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000848private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849 GrContext* fContext;
850 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000851};
852
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853#endif