blob: bed2d990c4dee16f1b3d4f23057bb43bf339862a [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
277 ///////////////////////////////////////////////////////////////////////////
278 // Platform Surfaces
279
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000280 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000281 * Wraps an existing texture with a GrTexture object.
282 *
283 * OpenGL: if the object is a texture Gr may change its GL texture params
284 * when it is drawn.
285 *
286 * @param desc description of the object to create.
287 *
288 * @return GrTexture object or NULL on failure.
289 */
290 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
291
292 /**
293 * Wraps an existing render target with a GrRenderTarget object. It is
294 * similar to createPlatformTexture but can be used to draw into surfaces
295 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
296 * the client will resolve to a texture).
297 *
298 * @param desc description of the object to create.
299 *
300 * @return GrTexture object or NULL on failure.
301 */
302 GrRenderTarget* createPlatformRenderTarget(
303 const GrPlatformRenderTargetDesc& desc);
304
bsalomon@google.com27847de2011-02-22 20:59:41 +0000305 ///////////////////////////////////////////////////////////////////////////
306 // Matrix state
307
308 /**
309 * Gets the current transformation matrix.
310 * @return the current matrix.
311 */
312 const GrMatrix& getMatrix() const;
313
314 /**
315 * Sets the transformation matrix.
316 * @param m the matrix to set.
317 */
318 void setMatrix(const GrMatrix& m);
319
320 /**
321 * Concats the current matrix. The passed matrix is applied before the
322 * current matrix.
323 * @param m the matrix to concat.
324 */
325 void concatMatrix(const GrMatrix& m) const;
326
327
328 ///////////////////////////////////////////////////////////////////////////
329 // Clip state
330 /**
331 * Gets the current clip.
332 * @return the current clip.
333 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000334 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000335
336 /**
337 * Sets the clip.
338 * @param clip the clip to set.
339 */
340 void setClip(const GrClip& clip);
341
342 /**
343 * Convenience method for setting the clip to a rect.
344 * @param rect the rect to set as the new clip.
345 */
346 void setClip(const GrIRect& rect);
347
348 ///////////////////////////////////////////////////////////////////////////
349 // Draws
350
351 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000352 * Clear the entire or rect of the render target, ignoring any clips.
353 * @param rect the rect to clear or the whole thing if rect is NULL.
354 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000356 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
358 /**
359 * Draw everywhere (respecting the clip) with the paint.
360 */
361 void drawPaint(const GrPaint& paint);
362
363 /**
364 * Draw the rect using a paint.
365 * @param paint describes how to color pixels.
366 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
367 * the rect is mitered stroked based on strokeWidth. If
368 * strokeWidth == 0, then the stroke is always a single
369 * pixel thick.
370 * @param matrix Optional matrix applied to the rect. Applied before
371 * context's matrix or the paint's matrix.
372 * The rects coords are used to access the paint (through texture matrix)
373 */
374 void drawRect(const GrPaint& paint,
375 const GrRect&,
376 GrScalar strokeWidth = -1,
377 const GrMatrix* matrix = NULL);
378
379 /**
380 * Maps a rect of paint coordinates onto the a rect of destination
381 * coordinates. Each rect can optionally be transformed. The srcRect
382 * is stretched over the dstRect. The dstRect is transformed by the
383 * context's matrix and the srcRect is transformed by the paint's matrix.
384 * Additional optional matrices can be provided by parameters.
385 *
386 * @param paint describes how to color pixels.
387 * @param dstRect the destination rect to draw.
388 * @param srcRect rect of paint coordinates to be mapped onto dstRect
389 * @param dstMatrix Optional matrix to transform dstRect. Applied before
390 * context's matrix.
391 * @param srcMatrix Optional matrix to transform srcRect Applied before
392 * paint's matrix.
393 */
394 void drawRectToRect(const GrPaint& paint,
395 const GrRect& dstRect,
396 const GrRect& srcRect,
397 const GrMatrix* dstMatrix = NULL,
398 const GrMatrix* srcMatrix = NULL);
399
400 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000401 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 *
403 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000404 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000405 * @param fill the path filling rule to use.
406 * @param translate optional additional translation applied to the
407 * path.
408 */
reed@google.com07f3ee12011-05-16 17:21:57 +0000409 void drawPath(const GrPaint& paint, const GrPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000410 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000411
bsalomon@google.com27847de2011-02-22 20:59:41 +0000412 /**
413 * Draws vertices with a paint.
414 *
415 * @param paint describes how to color pixels.
416 * @param primitiveType primitives type to draw.
417 * @param vertexCount number of vertices.
418 * @param positions array of vertex positions, required.
419 * @param texCoords optional array of texture coordinates used
420 * to access the paint.
421 * @param colors optional array of per-vertex colors, supercedes
422 * the paint's color field.
423 * @param indices optional array of indices. If NULL vertices
424 * are drawn non-indexed.
425 * @param indexCount if indices is non-null then this is the
426 * number of indices.
427 */
428 void drawVertices(const GrPaint& paint,
429 GrPrimitiveType primitiveType,
430 int vertexCount,
431 const GrPoint positions[],
432 const GrPoint texs[],
433 const GrColor colors[],
434 const uint16_t indices[],
435 int indexCount);
436
bsalomon@google.com27847de2011-02-22 20:59:41 +0000437 ///////////////////////////////////////////////////////////////////////////
438 // Misc.
439
440 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000441 * Flags that affect flush() behavior.
442 */
443 enum FlushBits {
444 /**
445 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
446 * it can be rendered to directly. However, Gr lazily sets state. Simply
447 * calling setRenderTarget() followed by flush() without flags may not
448 * bind the render target. This flag forces the context to bind the last
449 * set render target in the 3D API.
450 */
451 kForceCurrentRenderTarget_FlushBit = 0x1,
452 /**
453 * A client may reach a point where it has partially rendered a frame
454 * through a GrContext that it knows the user will never see. This flag
455 * causes the flush to skip submission of deferred content to the 3D API
456 * during the flush.
457 */
458 kDiscard_FlushBit = 0x2,
459 };
460
461 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000462 * Call to ensure all drawing to the context has been issued to the
463 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000464 * @param flagsBitfield flags that control the flushing behavior. See
465 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000466 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000467 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000468
bsalomon@google.com27847de2011-02-22 20:59:41 +0000469 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000470 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000471 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000472 * current render target.
473 * @param left left edge of the rectangle to read (inclusive)
474 * @param top top edge of the rectangle to read (inclusive)
475 * @param width width of rectangle to read in pixels.
476 * @param height height of rectangle to read in pixels.
477 * @param config the pixel config of the destination buffer
478 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000479 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000480 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481 *
482 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000484 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000486 bool readRenderTargetPixels(GrRenderTarget* target,
487 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000488 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000489 size_t rowBytes) {
490 return this->internalReadRenderTargetPixels(target, left, top,
491 width, height,
492 config, buffer,
493 rowBytes, 0);
494 }
495
496 /**
497 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
498 * at the specified rectangle.
499 * @param target the render target to write into. NULL means the
500 * current render target.
501 * @param left left edge of the rectangle to write (inclusive)
502 * @param top top edge of the rectangle to write (inclusive)
503 * @param width width of rectangle to write in pixels.
504 * @param height height of rectangle to write in pixels.
505 * @param config the pixel config of the source buffer
506 * @param buffer memory to read the rectangle from.
507 * @param rowBytes number of bytes bewtween consecutive rows. Zero
508 * means rows are tightly packed.
509 */
510 void writeRenderTargetPixels(GrRenderTarget* target,
511 int left, int top, int width, int height,
512 GrPixelConfig config, const void* buffer,
513 size_t rowBytes) {
514 this->internalWriteRenderTargetPixels(target, left, top, width, height,
515 config, buffer, rowBytes, 0);
516 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000517
518 /**
519 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000520 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000521 * @param left left edge of the rectangle to read (inclusive)
522 * @param top top edge of the rectangle to read (inclusive)
523 * @param width width of rectangle to read in pixels.
524 * @param height height of rectangle to read in pixels.
525 * @param config the pixel config of the destination buffer
526 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000527 * @param rowBytes number of bytes bewtween consecutive rows. Zero
528 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000529 *
530 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000532 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000534 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000535 GrPixelConfig config, void* buffer,
536 size_t rowBytes) {
537 return this->internalReadTexturePixels(texture, left, top,
538 width, height,
539 config, buffer, rowBytes, 0);
540 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541
542 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 * Writes a rectangle of pixels to a texture.
544 * @param texture the render target to read from.
545 * @param left left edge of the rectangle to write (inclusive)
546 * @param top top edge of the rectangle to write (inclusive)
547 * @param width width of rectangle to write in pixels.
548 * @param height height of rectangle to write in pixels.
549 * @param config the pixel config of the source buffer
550 * @param buffer memory to read pixels from
551 * @param rowBytes number of bytes bewtween consecutive rows. Zero
552 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554 void writeTexturePixels(GrTexture* texture,
555 int left, int top, int width, int height,
556 GrPixelConfig config, const void* buffer,
557 size_t rowBytes) {
558 this->internalWriteTexturePixels(texture, left, top, width, height,
559 config, buffer, rowBytes, 0);
560 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000561 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000562 * Copies all texels from one texture to another.
563 * @param src the texture to copy from.
564 * @param dst the render target to copy to.
565 */
566 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000567
568 /**
569 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
570 * downsampled to the associated GrTexture (accessible via
571 * GrRenderTarget::asTexture()). Any pending draws to the render target will
572 * be executed before the resolve.
573 *
574 * This is only necessary when a client wants to access the object directly
575 * using the underlying graphics API. GrContext will detect when it must
576 * perform a resolve to a GrTexture used as the source of a draw or before
577 * reading pixels back from a GrTexture or GrRenderTarget.
578 */
579 void resolveRenderTarget(GrRenderTarget* target);
580
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000581 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000582 * Applies a 2D Gaussian blur to a given texture.
583 * @param srcTexture The source texture to be blurred.
584 * @param temp1 A scratch texture. Must not be NULL.
585 * @param temp2 A scratch texture. May be NULL, in which case
586 * srcTexture is overwritten with intermediate
587 * results.
588 * @param rect The destination rectangle.
589 * @param sigmaX The blur's standard deviation in X.
590 * @param sigmaY The blur's standard deviation in Y.
591 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000592 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000593 GrTexture* gaussianBlur(GrTexture* srcTexture,
594 GrAutoScratchTexture* temp1,
595 GrAutoScratchTexture* temp2,
596 const SkRect& rect,
597 float sigmaX, float sigmaY);
598
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000599 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000600 * Applies a 2D morphology to a given texture.
601 * @param srcTexture The source texture to be blurred.
602 * @param rect The destination rectangle.
603 * @param temp1 A scratch texture. Must not be NULL.
604 * @param temp2 A scratch texture. Must not be NULL.
605 * @param filter The morphology filter. Must be kDilate_Filter or
606 * kErode_Filter.
607 * @param radius The morphology radius in X and Y. The filter is
608 * applied to a fWidth by fHeight rectangle of
609 * pixels.
610 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000611 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000612 GrTexture* applyMorphology(GrTexture* srcTexture,
613 const GrRect& rect,
614 GrTexture* temp1, GrTexture* temp2,
615 GrSamplerState::Filter filter,
616 SkISize radius);
617
bsalomon@google.com27847de2011-02-22 20:59:41 +0000618 ///////////////////////////////////////////////////////////////////////////
619 // Helpers
620
621 class AutoRenderTarget : ::GrNoncopyable {
622 public:
623 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
624 fContext = NULL;
625 fPrevTarget = context->getRenderTarget();
626 if (fPrevTarget != target) {
627 context->setRenderTarget(target);
628 fContext = context;
629 }
630 }
631 ~AutoRenderTarget() {
632 if (fContext) {
633 fContext->setRenderTarget(fPrevTarget);
634 }
635 }
636 private:
637 GrContext* fContext;
638 GrRenderTarget* fPrevTarget;
639 };
640
641
642 ///////////////////////////////////////////////////////////////////////////
643 // Functions intended for internal use only.
644 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000645 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646 GrFontCache* getFontCache() { return fFontCache; }
647 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000648 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
bsalomon@google.com27847de2011-02-22 20:59:41 +0000669 };
670 DrawCategory fLastDrawCategory;
671
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000672 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000673 GrDrawState* fDrawState;
674
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000675 GrResourceCache* fTextureCache;
676 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000677
bsalomon@google.com30085192011-08-19 15:42:31 +0000678 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679
680 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
681 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
682 GrInOrderDrawBuffer* fDrawBuffer;
683
bsalomon@google.com205d4602011-04-25 12:43:45 +0000684 GrIndexBuffer* fAAFillRectIndexBuffer;
685 GrIndexBuffer* fAAStrokeRectIndexBuffer;
686
bsalomon@google.com27847de2011-02-22 20:59:41 +0000687 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000688
bsalomon@google.com205d4602011-04-25 12:43:45 +0000689 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000690 const GrRect& devRect,
691 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692
693 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 const GrVec& devStrokeSize,
696 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000697
698 inline int aaFillRectIndexCount() const;
699 GrIndexBuffer* aaFillRectIndexBuffer();
700
701 inline int aaStrokeRectIndexCount() const;
702 GrIndexBuffer* aaStrokeRectIndexBuffer();
703
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000704 void setupDrawBuffer();
705
bsalomon@google.com27847de2011-02-22 20:59:41 +0000706 void flushDrawBuffer();
707
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000708 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000709
bsalomon@google.com27847de2011-02-22 20:59:41 +0000710 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
711
bsalomon@google.com289533a2011-10-27 12:34:25 +0000712 GrPathRenderer* getPathRenderer(const GrPath& path,
713 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000714 const GrDrawTarget* target,
bsalomon@google.com289533a2011-10-27 12:34:25 +0000715 bool antiAlias);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000716
bsalomon@google.com6f379512011-11-16 20:36:03 +0000717 /**
718 * Flags to the internal read/write pixels funcs
719 */
720 enum PixelOpsFlags {
721 kDontFlush_PixelOpsFlag = 0x1,
722 };
723
724 bool internalReadRenderTargetPixels(GrRenderTarget* target,
725 int left, int top,
726 int width, int height,
727 GrPixelConfig config, void* buffer,
728 size_t rowBytes, uint32_t flags);
729
730 void internalWriteRenderTargetPixels(GrRenderTarget* target,
731 int left, int top,
732 int width, int height,
733 GrPixelConfig, const void* buffer,
734 size_t rowBytes, uint32_t flags);
735
736 bool internalReadTexturePixels(GrTexture* texture,
737 int left, int top,
738 int width, int height,
739 GrPixelConfig config, void* buffer,
740 size_t rowBytes, uint32_t flags);
741
742 void internalWriteTexturePixels(GrTexture* texture,
743 int left, int top,
744 int width, int height,
745 GrPixelConfig config, const void* buffer,
746 size_t rowBytes, uint32_t flags);
747 // needed for access to internalWriteTexturePixels. TODO: make GrContext
748 // be a facade for an internal class. Then functions that are public on the
749 // internal class would have only be callable in src/gpu. The facade would
750 // only have to functions necessary for clients.
751 friend class GrAtlas;
752
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000753 // computes vertex layout bits based on the paint. If paint expresses
754 // a texture for a stage, the stage coords will be bound to postitions
755 // unless hasTexCoords[s]==true in which case stage s's input coords
756 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
757 // for an array where all the values are false.
758 static int PaintStageVertexLayoutBits(
759 const GrPaint& paint,
760 const bool hasTexCoords[GrPaint::kTotalStages]);
761
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762};
763
764/**
765 * Save/restore the view-matrix in the context.
766 */
767class GrAutoMatrix : GrNoncopyable {
768public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000769 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
771 fMatrix = ctx->getMatrix();
772 }
773 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
774 fMatrix = ctx->getMatrix();
775 ctx->setMatrix(matrix);
776 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000777 void set(GrContext* ctx) {
778 if (NULL != fContext) {
779 fContext->setMatrix(fMatrix);
780 }
781 fMatrix = ctx->getMatrix();
782 fContext = ctx;
783 }
784 void set(GrContext* ctx, const GrMatrix& matrix) {
785 if (NULL != fContext) {
786 fContext->setMatrix(fMatrix);
787 }
788 fMatrix = ctx->getMatrix();
789 ctx->setMatrix(matrix);
790 fContext = ctx;
791 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000792 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000793 if (NULL != fContext) {
794 fContext->setMatrix(fMatrix);
795 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 }
797
798private:
799 GrContext* fContext;
800 GrMatrix fMatrix;
801};
802
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000803/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000804 * Gets and locks a scratch texture from a descriptor using
805 * either exact or approximate criteria. Unlocks texture in
806 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000807 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000808class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000809public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000810 GrAutoScratchTexture()
811 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000812 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000813
814 GrAutoScratchTexture(GrContext* context,
815 const GrTextureDesc& desc,
816 GrContext::ScratchTexMatch match =
817 GrContext::kApprox_ScratchTexMatch)
818 : fContext(NULL) {
819 this->set(context, desc, match);
820 }
821
822 ~GrAutoScratchTexture() {
823 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000824 fContext->unlockTexture(fEntry);
825 }
826 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000827
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000828 GrTexture* set(GrContext* context,
829 const GrTextureDesc& desc,
830 GrContext::ScratchTexMatch match =
831 GrContext::kApprox_ScratchTexMatch) {
832 if (NULL != fContext) {
833 fContext->unlockTexture(fEntry);
834 }
835 fContext = context;
836 if (NULL != fContext) {
837 fEntry = fContext->lockScratchTexture(desc, match);
838 GrTexture* ret = fEntry.texture();
839 if (NULL == ret) {
840 fContext = NULL;
841 }
842 return ret;
843 } else {
844 return NULL;
845 }
846 }
847
848 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000849private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000850 GrContext* fContext;
851 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000852};
853
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854#endif