blob: e05c48a8c40f1896ab90b03dfe0a64d54c05910c [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 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000414 void drawPath(const GrPaint& paint, const SkPath& 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.com93c96602012-04-27 13:05:21 +0000442 /**
443 * Draws an oval.
444 *
445 * @param paint describes how to color pixels.
446 * @param rect the bounding rect of the oval.
447 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
448 * the rect is stroked based on strokeWidth. If
449 * strokeWidth == 0, then the stroke is always a single
450 * pixel thick.
451 */
452 void drawOval(const GrPaint& paint,
453 const GrRect& rect,
454 SkScalar strokeWidth);
455
bsalomon@google.com27847de2011-02-22 20:59:41 +0000456 ///////////////////////////////////////////////////////////////////////////
457 // Misc.
458
459 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000460 * Flags that affect flush() behavior.
461 */
462 enum FlushBits {
463 /**
464 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
465 * it can be rendered to directly. However, Gr lazily sets state. Simply
466 * calling setRenderTarget() followed by flush() without flags may not
467 * bind the render target. This flag forces the context to bind the last
468 * set render target in the 3D API.
469 */
470 kForceCurrentRenderTarget_FlushBit = 0x1,
471 /**
472 * A client may reach a point where it has partially rendered a frame
473 * through a GrContext that it knows the user will never see. This flag
474 * causes the flush to skip submission of deferred content to the 3D API
475 * during the flush.
476 */
477 kDiscard_FlushBit = 0x2,
478 };
479
480 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000481 * Call to ensure all drawing to the context has been issued to the
482 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000483 * @param flagsBitfield flags that control the flushing behavior. See
484 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000485 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000486 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000487
bsalomon@google.com27847de2011-02-22 20:59:41 +0000488 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000489 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000490 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000491 * current render target.
492 * @param left left edge of the rectangle to read (inclusive)
493 * @param top top edge of the rectangle to read (inclusive)
494 * @param width width of rectangle to read in pixels.
495 * @param height height of rectangle to read in pixels.
496 * @param config the pixel config of the destination buffer
497 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000499 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000500 *
501 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000502 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000503 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000504 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000505 bool readRenderTargetPixels(GrRenderTarget* target,
506 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000507 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000508 size_t rowBytes) {
509 return this->internalReadRenderTargetPixels(target, left, top,
510 width, height,
511 config, buffer,
512 rowBytes, 0);
513 }
514
515 /**
516 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
517 * at the specified rectangle.
518 * @param target the render target to write into. NULL means the
519 * current render target.
520 * @param left left edge of the rectangle to write (inclusive)
521 * @param top top edge of the rectangle to write (inclusive)
522 * @param width width of rectangle to write in pixels.
523 * @param height height of rectangle to write in pixels.
524 * @param config the pixel config of the source buffer
525 * @param buffer memory to read the rectangle from.
526 * @param rowBytes number of bytes bewtween consecutive rows. Zero
527 * means rows are tightly packed.
528 */
529 void writeRenderTargetPixels(GrRenderTarget* target,
530 int left, int top, int width, int height,
531 GrPixelConfig config, const void* buffer,
532 size_t rowBytes) {
533 this->internalWriteRenderTargetPixels(target, left, top, width, height,
534 config, buffer, rowBytes, 0);
535 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000536
537 /**
538 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000540 * @param left left edge of the rectangle to read (inclusive)
541 * @param top top edge of the rectangle to read (inclusive)
542 * @param width width of rectangle to read in pixels.
543 * @param height height of rectangle to read in pixels.
544 * @param config the pixel config of the destination buffer
545 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000546 * @param rowBytes number of bytes bewtween consecutive rows. Zero
547 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000548 *
549 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000550 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000551 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000552 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000553 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000554 GrPixelConfig config, void* buffer,
555 size_t rowBytes) {
556 return this->internalReadTexturePixels(texture, left, top,
557 width, height,
558 config, buffer, rowBytes, 0);
559 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560
561 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000562 * Writes a rectangle of pixels to a texture.
563 * @param texture the render target to read from.
564 * @param left left edge of the rectangle to write (inclusive)
565 * @param top top edge of the rectangle to write (inclusive)
566 * @param width width of rectangle to write in pixels.
567 * @param height height of rectangle to write in pixels.
568 * @param config the pixel config of the source buffer
569 * @param buffer memory to read pixels from
570 * @param rowBytes number of bytes bewtween consecutive rows. Zero
571 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000572 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000573 void writeTexturePixels(GrTexture* texture,
574 int left, int top, int width, int height,
575 GrPixelConfig config, const void* buffer,
576 size_t rowBytes) {
577 this->internalWriteTexturePixels(texture, left, top, width, height,
578 config, buffer, rowBytes, 0);
579 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000580 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000581 * Copies all texels from one texture to another.
582 * @param src the texture to copy from.
583 * @param dst the render target to copy to.
584 */
585 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000586
587 /**
588 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
589 * downsampled to the associated GrTexture (accessible via
590 * GrRenderTarget::asTexture()). Any pending draws to the render target will
591 * be executed before the resolve.
592 *
593 * This is only necessary when a client wants to access the object directly
594 * using the underlying graphics API. GrContext will detect when it must
595 * perform a resolve to a GrTexture used as the source of a draw or before
596 * reading pixels back from a GrTexture or GrRenderTarget.
597 */
598 void resolveRenderTarget(GrRenderTarget* target);
599
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000600 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000601 * Applies a 2D Gaussian blur to a given texture.
602 * @param srcTexture The source texture to be blurred.
603 * @param temp1 A scratch texture. Must not be NULL.
604 * @param temp2 A scratch texture. May be NULL, in which case
605 * srcTexture is overwritten with intermediate
606 * results.
607 * @param rect The destination rectangle.
608 * @param sigmaX The blur's standard deviation in X.
609 * @param sigmaY The blur's standard deviation in Y.
610 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000611 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000612 GrTexture* gaussianBlur(GrTexture* srcTexture,
613 GrAutoScratchTexture* temp1,
614 GrAutoScratchTexture* temp2,
615 const SkRect& rect,
616 float sigmaX, float sigmaY);
617
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000618 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000619 * Applies a 2D morphology to a given texture.
620 * @param srcTexture The source texture to be blurred.
621 * @param rect The destination rectangle.
622 * @param temp1 A scratch texture. Must not be NULL.
623 * @param temp2 A scratch texture. Must not be NULL.
624 * @param filter The morphology filter. Must be kDilate_Filter or
625 * kErode_Filter.
626 * @param radius The morphology radius in X and Y. The filter is
627 * applied to a fWidth by fHeight rectangle of
628 * pixels.
629 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000630 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000631 GrTexture* applyMorphology(GrTexture* srcTexture,
632 const GrRect& rect,
633 GrTexture* temp1, GrTexture* temp2,
634 GrSamplerState::Filter filter,
635 SkISize radius);
636
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637 ///////////////////////////////////////////////////////////////////////////
638 // Helpers
639
640 class AutoRenderTarget : ::GrNoncopyable {
641 public:
642 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
643 fContext = NULL;
644 fPrevTarget = context->getRenderTarget();
645 if (fPrevTarget != target) {
646 context->setRenderTarget(target);
647 fContext = context;
648 }
649 }
650 ~AutoRenderTarget() {
651 if (fContext) {
652 fContext->setRenderTarget(fPrevTarget);
653 }
654 }
655 private:
656 GrContext* fContext;
657 GrRenderTarget* fPrevTarget;
658 };
659
660
661 ///////////////////////////////////////////////////////////////////////////
662 // Functions intended for internal use only.
663 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000664 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665 GrFontCache* getFontCache() { return fFontCache; }
666 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000667 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000668 void resetStats();
669 const GrGpuStats& getStats() const;
670 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000671 /**
672 * Stencil buffers add themselves to the cache using
673 * addAndLockStencilBuffer. When a SB's RT-attachment count
674 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
675 * eligible for purging. findStencilBuffer is called to check the cache for
676 * a SB that matching an RT's criteria. If a match is found that has been
677 * unlocked (its attachment count has reached 0) then it will be relocked.
678 */
679 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
680 void unlockStencilBuffer(GrResourceEntry* sbEntry);
681 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000682
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000683 /*
684 * postClipPush acts as a hint to this and lower-level classes (e.g.,
685 * GrGpu) that the clip stack has changed.
686 */
687 virtual void postClipPush();
688
689 /*
690 * preClipPop acts as a hint that the clip stack has been restored to an
691 * earlier state.
692 */
693 virtual void preClipPop();
694
robertphillips@google.com2c756812012-05-22 20:28:23 +0000695 GrPathRenderer* getPathRenderer(const SkPath& path,
696 GrPathFill fill,
697 const GrDrawTarget* target,
698 bool antiAlias);
699
bsalomon@google.com27847de2011-02-22 20:59:41 +0000700private:
701 // used to keep track of when we need to flush the draw buffer
702 enum DrawCategory {
703 kBuffered_DrawCategory, // last draw was inserted in draw buffer
704 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000705 };
706 DrawCategory fLastDrawCategory;
707
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000708 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000709 GrDrawState* fDrawState;
710
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000711 GrResourceCache* fTextureCache;
712 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000713
bsalomon@google.com30085192011-08-19 15:42:31 +0000714 GrPathRendererChain* fPathRendererChain;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000715
716 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
717 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
718 GrInOrderDrawBuffer* fDrawBuffer;
719
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720 GrIndexBuffer* fAAFillRectIndexBuffer;
721 GrIndexBuffer* fAAStrokeRectIndexBuffer;
722
bsalomon@google.com27847de2011-02-22 20:59:41 +0000723 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000724
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000726 const GrRect& devRect,
727 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000728
729 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000731 const GrVec& devStrokeSize,
732 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000733
734 inline int aaFillRectIndexCount() const;
735 GrIndexBuffer* aaFillRectIndexBuffer();
736
737 inline int aaStrokeRectIndexCount() const;
738 GrIndexBuffer* aaStrokeRectIndexBuffer();
739
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000740 void setupDrawBuffer();
741
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742 void flushDrawBuffer();
743
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000744 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
bsalomon@google.com27847de2011-02-22 20:59:41 +0000746 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
747
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000748 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000749 GrPathFill fill, const GrPoint* translate);
750
bsalomon@google.com6f379512011-11-16 20:36:03 +0000751 /**
752 * Flags to the internal read/write pixels funcs
753 */
754 enum PixelOpsFlags {
755 kDontFlush_PixelOpsFlag = 0x1,
756 };
757
758 bool internalReadRenderTargetPixels(GrRenderTarget* target,
759 int left, int top,
760 int width, int height,
761 GrPixelConfig config, void* buffer,
762 size_t rowBytes, uint32_t flags);
763
764 void internalWriteRenderTargetPixels(GrRenderTarget* target,
765 int left, int top,
766 int width, int height,
767 GrPixelConfig, const void* buffer,
768 size_t rowBytes, uint32_t flags);
769
770 bool internalReadTexturePixels(GrTexture* texture,
771 int left, int top,
772 int width, int height,
773 GrPixelConfig config, void* buffer,
774 size_t rowBytes, uint32_t flags);
775
776 void internalWriteTexturePixels(GrTexture* texture,
777 int left, int top,
778 int width, int height,
779 GrPixelConfig config, const void* buffer,
780 size_t rowBytes, uint32_t flags);
781 // needed for access to internalWriteTexturePixels. TODO: make GrContext
782 // be a facade for an internal class. Then functions that are public on the
783 // internal class would have only be callable in src/gpu. The facade would
784 // only have to functions necessary for clients.
785 friend class GrAtlas;
786
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000787 // computes vertex layout bits based on the paint. If paint expresses
788 // a texture for a stage, the stage coords will be bound to postitions
789 // unless hasTexCoords[s]==true in which case stage s's input coords
790 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
791 // for an array where all the values are false.
792 static int PaintStageVertexLayoutBits(
793 const GrPaint& paint,
794 const bool hasTexCoords[GrPaint::kTotalStages]);
795
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796};
797
798/**
799 * Save/restore the view-matrix in the context.
800 */
801class GrAutoMatrix : GrNoncopyable {
802public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000803 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
805 fMatrix = ctx->getMatrix();
806 }
807 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
808 fMatrix = ctx->getMatrix();
809 ctx->setMatrix(matrix);
810 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000811 void set(GrContext* ctx) {
812 if (NULL != fContext) {
813 fContext->setMatrix(fMatrix);
814 }
815 fMatrix = ctx->getMatrix();
816 fContext = ctx;
817 }
818 void set(GrContext* ctx, const GrMatrix& matrix) {
819 if (NULL != fContext) {
820 fContext->setMatrix(fMatrix);
821 }
822 fMatrix = ctx->getMatrix();
823 ctx->setMatrix(matrix);
824 fContext = ctx;
825 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000827 if (NULL != fContext) {
828 fContext->setMatrix(fMatrix);
829 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830 }
831
832private:
833 GrContext* fContext;
834 GrMatrix fMatrix;
835};
836
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000837/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000838 * Gets and locks a scratch texture from a descriptor using
839 * either exact or approximate criteria. Unlocks texture in
840 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000841 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000842class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000844 GrAutoScratchTexture()
845 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000846 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000847
848 GrAutoScratchTexture(GrContext* context,
849 const GrTextureDesc& desc,
850 GrContext::ScratchTexMatch match =
851 GrContext::kApprox_ScratchTexMatch)
852 : fContext(NULL) {
853 this->set(context, desc, match);
854 }
855
856 ~GrAutoScratchTexture() {
857 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000858 fContext->unlockTexture(fEntry);
859 }
860 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000861
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000862 GrTexture* set(GrContext* context,
863 const GrTextureDesc& desc,
864 GrContext::ScratchTexMatch match =
865 GrContext::kApprox_ScratchTexMatch) {
866 if (NULL != fContext) {
867 fContext->unlockTexture(fEntry);
robertphillips@google.comf105b102012-05-14 12:18:26 +0000868 fEntry.reset();
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000869 }
870 fContext = context;
871 if (NULL != fContext) {
872 fEntry = fContext->lockScratchTexture(desc, match);
873 GrTexture* ret = fEntry.texture();
874 if (NULL == ret) {
875 fContext = NULL;
876 }
877 return ret;
878 } else {
879 return NULL;
880 }
881 }
882
883 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000884private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000885 GrContext* fContext;
886 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000887};
888
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889#endif