blob: 876852825cefb3a2681845c49fb132cab75b221e [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;
robertphillips@google.com72176b22012-05-23 13:19:12 +000035class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000036
bsalomon@google.com91826102011-03-21 19:51:57 +000037class GR_API GrContext : public GrRefCnt {
bsalomon@google.com27847de2011-02-22 20:59:41 +000038public:
39 /**
40 * Creates a GrContext from within a 3D context.
41 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000042 static GrContext* Create(GrEngine engine,
43 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000044
bsalomon@google.com27847de2011-02-22 20:59:41 +000045 virtual ~GrContext();
46
47 /**
48 * The GrContext normally assumes that no outsider is setting state
49 * within the underlying 3D API's context/device/whatever. This call informs
50 * the context that the state was modified and it should resend. Shouldn't
51 * be called frequently for good performance.
52 */
53 void resetContext();
54
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 /**
56 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
57 * if you have lost the associated GPU context, and thus internal texture,
58 * buffer, etc. references/IDs are now invalid. Should be called even when
59 * GrContext is no longer going to be used for two reasons:
60 * 1) ~GrContext will not try to free the objects in the 3D API.
61 * 2) If you've created GrResources that outlive the GrContext they will
62 * be marked as invalid (GrResource::isValid()) and won't attempt to
63 * free their underlying resource in the 3D API.
64 * Content drawn since the last GrContext::flush() may be lost.
65 */
66 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000067
68 /**
junov@google.com53a55842011-06-08 22:55:10 +000069 * Similar to contextLost, but makes no attempt to reset state.
70 * Use this method when GrContext destruction is pending, but
71 * the graphics context is destroyed first.
72 */
73 void contextDestroyed();
74
75 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 * Frees gpu created by the context. Can be called to reduce GPU memory
77 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 void freeGpuResources();
80
twiz@google.com05e70242012-01-27 19:12:00 +000081 /**
82 * Returns the number of bytes of GPU memory hosted by the texture cache.
83 */
84 size_t getGpuTextureCacheBytes() const;
85
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 ///////////////////////////////////////////////////////////////////////////
87 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000088
89 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000090 * Token that refers to an entry in the texture cache. Returned by
91 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000092 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000093 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000094 public:
95 TextureCacheEntry() : fEntry(NULL) {}
96 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
97 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
98 fEntry = e.fEntry;
99 return *this;
100 }
101 GrTexture* texture() const;
102 void reset() { fEntry = NULL; }
103 private:
104 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
105 void set(GrResourceEntry* entry) { fEntry = entry; }
106 GrResourceEntry* cacheEntry() { return fEntry; }
107 GrResourceEntry* fEntry;
108 friend class GrContext;
109 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000111 /**
112 * Key generated by client. Should be a unique key on the texture data.
113 * Does not need to consider that width and height of the texture. Two
114 * textures with the same TextureKey but different bounds will not collide.
115 */
116 typedef uint64_t TextureKey;
117
118 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000119 * Create a new entry, based on the specified key and texture, and return
120 * its "locked" entry. Must call be balanced with an unlockTexture() call.
121 *
122 * @param key A client-generated key that identifies the contents
123 * of the texture. Respecified to findAndLockTexture
124 * for subsequent uses of the texture.
125 * @param sampler The sampler state used to draw a texture may be used
126 * to determine how to store the pixel data in the texture
127 * cache. (e.g. different versions may exist for different
128 * wrap modes on GPUs with limited or no NPOT texture
129 * support). Only the wrap and filter fields are used. NULL
130 * implies clamp wrap modes and nearest filtering.
131 * @param desc Description of the texture properties.
132 * @param srcData Pointer to the pixel values.
133 * @param rowBytes The number of bytes between rows of the texture. Zero
134 * implies tightly packed rows.
135 */
136 TextureCacheEntry createAndLockTexture(TextureKey key,
137 const GrSamplerState* sampler,
138 const GrTextureDesc& desc,
139 void* srcData, size_t rowBytes);
140
141 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000142 * Search for an entry based on key and dimensions. If found, "lock" it and
143 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000144 * Must be balanced with an unlockTexture() call.
145 *
146 * @param key A client-generated key that identifies the contents
147 * of the texture.
148 * @param width The width of the texture in pixels as specifed in
149 * the GrTextureDesc originally passed to
150 * createAndLockTexture
151 * @param width The height of the texture in pixels as specifed in
152 * the GrTextureDesc originally passed to
153 * createAndLockTexture
154 * @param sampler The sampler state used to draw a texture may be used
155 * to determine the cache entry used. (e.g. different
156 * versions may exist for different wrap modes on GPUs with
157 * limited or no NPOT texture support). Only the wrap and
158 * filter fields are used. NULL implies clamp wrap modes
159 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000160 */
161 TextureCacheEntry findAndLockTexture(TextureKey key,
162 int width,
163 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000164 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000165 /**
166 * Determines whether a texture is in the cache. If the texture is found it
167 * will not be locked or returned. This call does not affect the priority of
168 * the texture for deletion.
169 */
170 bool isTextureInCache(TextureKey key,
171 int width,
172 int height,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000173 const GrSamplerState*) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000174
175 /**
176 * Enum that determines how closely a returned scratch texture must match
177 * a provided GrTextureDesc.
178 */
179 enum ScratchTexMatch {
180 /**
181 * Finds a texture that exactly matches the descriptor.
182 */
183 kExact_ScratchTexMatch,
184 /**
185 * Finds a texture that approximately matches the descriptor. Will be
186 * at least as large in width and height as desc specifies. If desc
187 * specifies that texture is a render target then result will be a
188 * render target. If desc specifies a render target and doesn't set the
189 * no stencil flag then result will have a stencil. Format and aa level
190 * will always match.
191 */
192 kApprox_ScratchTexMatch
193 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194
195 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000196 * Returns a texture matching the desc. It's contents are unknown. Subsequent
197 * requests with the same descriptor are not guaranteed to return the same
198 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000199 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000200 *
201 * Textures created by createAndLockTexture() hide the complications of
202 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000203 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000204 * such an API will create gaps in the tiling pattern. This includes clamp
205 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000206 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000207 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc, ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000208
209 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000210 * When done with an entry, call unlockTexture(entry) on it, which returns
211 * it to the cache, where it may be purged.
212 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000213 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214
215 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216 * Creates a texture that is outside the cache. Does not count against
217 * cache's budget.
218 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000219 GrTexture* createUncachedTexture(const GrTextureDesc&,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220 void* srcData,
221 size_t rowBytes);
222
223 /**
224 * Returns true if the specified use of an indexed texture is supported.
225 */
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000226 bool supportsIndex8PixelConfig(const GrSamplerState*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000227 int width,
228 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229
230 /**
231 * Return the current texture cache limits.
232 *
233 * @param maxTextures If non-null, returns maximum number of textures that
234 * can be held in the cache.
235 * @param maxTextureBytes If non-null, returns maximum number of bytes of
236 * texture memory that can be held in the cache.
237 */
238 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
239
240 /**
241 * Specify the texture cache limits. If the current cache exceeds either
242 * of these, it will be purged (LRU) to keep the cache within these limits.
243 *
244 * @param maxTextures The maximum number of textures that can be held in
245 * the cache.
246 * @param maxTextureBytes The maximum number of bytes of texture memory
247 * that can be held in the cache.
248 */
249 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
250
251 /**
252 * Return the max width or height of a texture supported by the current gpu
253 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000254 int getMaxTextureSize() const;
255
256 /**
257 * Return the max width or height of a render target supported by the
258 * current gpu
259 */
260 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000261
262 ///////////////////////////////////////////////////////////////////////////
263 // Render targets
264
265 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000266 * Sets the render target.
267 * @param target the render target to set. (should not be NULL.)
268 */
269 void setRenderTarget(GrRenderTarget* target);
270
271 /**
272 * Gets the current render target.
273 * @return the currently bound render target. Should never be NULL.
274 */
275 const GrRenderTarget* getRenderTarget() const;
276 GrRenderTarget* getRenderTarget();
277
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000278 /**
279 * Can the provided configuration act as a color render target?
280 */
281 bool isConfigRenderable(GrPixelConfig config) const;
282
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000283 ///////////////////////////////////////////////////////////////////////////
284 // Platform Surfaces
285
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000286 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000287 * Wraps an existing texture with a GrTexture object.
288 *
289 * OpenGL: if the object is a texture Gr may change its GL texture params
290 * when it is drawn.
291 *
292 * @param desc description of the object to create.
293 *
294 * @return GrTexture object or NULL on failure.
295 */
296 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
297
298 /**
299 * Wraps an existing render target with a GrRenderTarget object. It is
300 * similar to createPlatformTexture but can be used to draw into surfaces
301 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
302 * the client will resolve to a texture).
303 *
304 * @param desc description of the object to create.
305 *
306 * @return GrTexture object or NULL on failure.
307 */
308 GrRenderTarget* createPlatformRenderTarget(
309 const GrPlatformRenderTargetDesc& desc);
310
bsalomon@google.com27847de2011-02-22 20:59:41 +0000311 ///////////////////////////////////////////////////////////////////////////
312 // Matrix state
313
314 /**
315 * Gets the current transformation matrix.
316 * @return the current matrix.
317 */
318 const GrMatrix& getMatrix() const;
319
320 /**
321 * Sets the transformation matrix.
322 * @param m the matrix to set.
323 */
324 void setMatrix(const GrMatrix& m);
325
326 /**
327 * Concats the current matrix. The passed matrix is applied before the
328 * current matrix.
329 * @param m the matrix to concat.
330 */
331 void concatMatrix(const GrMatrix& m) const;
332
333
334 ///////////////////////////////////////////////////////////////////////////
335 // Clip state
336 /**
337 * Gets the current clip.
338 * @return the current clip.
339 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000340 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000341
342 /**
343 * Sets the clip.
344 * @param clip the clip to set.
345 */
346 void setClip(const GrClip& clip);
347
348 /**
349 * Convenience method for setting the clip to a rect.
350 * @param rect the rect to set as the new clip.
351 */
352 void setClip(const GrIRect& rect);
353
354 ///////////////////////////////////////////////////////////////////////////
355 // Draws
356
357 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000358 * Clear the entire or rect of the render target, ignoring any clips.
359 * @param rect the rect to clear or the whole thing if rect is NULL.
360 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000362 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000363
364 /**
365 * Draw everywhere (respecting the clip) with the paint.
366 */
367 void drawPaint(const GrPaint& paint);
368
369 /**
370 * Draw the rect using a paint.
371 * @param paint describes how to color pixels.
372 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
373 * the rect is mitered stroked based on strokeWidth. If
374 * strokeWidth == 0, then the stroke is always a single
375 * pixel thick.
376 * @param matrix Optional matrix applied to the rect. Applied before
377 * context's matrix or the paint's matrix.
378 * The rects coords are used to access the paint (through texture matrix)
379 */
380 void drawRect(const GrPaint& paint,
381 const GrRect&,
382 GrScalar strokeWidth = -1,
383 const GrMatrix* matrix = NULL);
384
385 /**
386 * Maps a rect of paint coordinates onto the a rect of destination
387 * coordinates. Each rect can optionally be transformed. The srcRect
388 * is stretched over the dstRect. The dstRect is transformed by the
389 * context's matrix and the srcRect is transformed by the paint's matrix.
390 * Additional optional matrices can be provided by parameters.
391 *
392 * @param paint describes how to color pixels.
393 * @param dstRect the destination rect to draw.
394 * @param srcRect rect of paint coordinates to be mapped onto dstRect
395 * @param dstMatrix Optional matrix to transform dstRect. Applied before
396 * context's matrix.
397 * @param srcMatrix Optional matrix to transform srcRect Applied before
398 * paint's matrix.
399 */
400 void drawRectToRect(const GrPaint& paint,
401 const GrRect& dstRect,
402 const GrRect& srcRect,
403 const GrMatrix* dstMatrix = NULL,
404 const GrMatrix* srcMatrix = NULL);
405
406 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000407 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408 *
409 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000410 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411 * @param fill the path filling rule to use.
412 * @param translate optional additional translation applied to the
413 * path.
414 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000415 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000416 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000417
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 /**
419 * Draws vertices with a paint.
420 *
421 * @param paint describes how to color pixels.
422 * @param primitiveType primitives type to draw.
423 * @param vertexCount number of vertices.
424 * @param positions array of vertex positions, required.
425 * @param texCoords optional array of texture coordinates used
426 * to access the paint.
427 * @param colors optional array of per-vertex colors, supercedes
428 * the paint's color field.
429 * @param indices optional array of indices. If NULL vertices
430 * are drawn non-indexed.
431 * @param indexCount if indices is non-null then this is the
432 * number of indices.
433 */
434 void drawVertices(const GrPaint& paint,
435 GrPrimitiveType primitiveType,
436 int vertexCount,
437 const GrPoint positions[],
438 const GrPoint texs[],
439 const GrColor colors[],
440 const uint16_t indices[],
441 int indexCount);
442
bsalomon@google.com93c96602012-04-27 13:05:21 +0000443 /**
444 * Draws an oval.
445 *
446 * @param paint describes how to color pixels.
447 * @param rect the bounding rect of the oval.
448 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
449 * the rect is stroked based on strokeWidth. If
450 * strokeWidth == 0, then the stroke is always a single
451 * pixel thick.
452 */
453 void drawOval(const GrPaint& paint,
454 const GrRect& rect,
455 SkScalar strokeWidth);
456
bsalomon@google.com27847de2011-02-22 20:59:41 +0000457 ///////////////////////////////////////////////////////////////////////////
458 // Misc.
459
460 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000461 * Flags that affect flush() behavior.
462 */
463 enum FlushBits {
464 /**
465 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
466 * it can be rendered to directly. However, Gr lazily sets state. Simply
467 * calling setRenderTarget() followed by flush() without flags may not
468 * bind the render target. This flag forces the context to bind the last
469 * set render target in the 3D API.
470 */
471 kForceCurrentRenderTarget_FlushBit = 0x1,
472 /**
473 * A client may reach a point where it has partially rendered a frame
474 * through a GrContext that it knows the user will never see. This flag
475 * causes the flush to skip submission of deferred content to the 3D API
476 * during the flush.
477 */
478 kDiscard_FlushBit = 0x2,
479 };
480
481 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000482 * Call to ensure all drawing to the context has been issued to the
483 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000484 * @param flagsBitfield flags that control the flushing behavior. See
485 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000486 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000487 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000488
bsalomon@google.com27847de2011-02-22 20:59:41 +0000489 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000490 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000491 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000492 * current render target.
493 * @param left left edge of the rectangle to read (inclusive)
494 * @param top top edge of the rectangle to read (inclusive)
495 * @param width width of rectangle to read in pixels.
496 * @param height height of rectangle to read in pixels.
497 * @param config the pixel config of the destination buffer
498 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000499 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000500 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000501 *
502 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000503 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000504 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000505 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000506 bool readRenderTargetPixels(GrRenderTarget* target,
507 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000508 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000509 size_t rowBytes) {
510 return this->internalReadRenderTargetPixels(target, left, top,
511 width, height,
512 config, buffer,
513 rowBytes, 0);
514 }
515
516 /**
517 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
518 * at the specified rectangle.
519 * @param target the render target to write into. NULL means the
520 * current render target.
521 * @param left left edge of the rectangle to write (inclusive)
522 * @param top top edge of the rectangle to write (inclusive)
523 * @param width width of rectangle to write in pixels.
524 * @param height height of rectangle to write in pixels.
525 * @param config the pixel config of the source buffer
526 * @param buffer memory to read the rectangle from.
527 * @param rowBytes number of bytes bewtween consecutive rows. Zero
528 * means rows are tightly packed.
529 */
530 void writeRenderTargetPixels(GrRenderTarget* target,
531 int left, int top, int width, int height,
532 GrPixelConfig config, const void* buffer,
533 size_t rowBytes) {
534 this->internalWriteRenderTargetPixels(target, left, top, width, height,
535 config, buffer, rowBytes, 0);
536 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000537
538 /**
539 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000540 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000541 * @param left left edge of the rectangle to read (inclusive)
542 * @param top top edge of the rectangle to read (inclusive)
543 * @param width width of rectangle to read in pixels.
544 * @param height height of rectangle to read in pixels.
545 * @param config the pixel config of the destination buffer
546 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 * @param rowBytes number of bytes bewtween consecutive rows. Zero
548 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549 *
550 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000551 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000552 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000553 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000554 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000555 GrPixelConfig config, void* buffer,
556 size_t rowBytes) {
557 return this->internalReadTexturePixels(texture, left, top,
558 width, height,
559 config, buffer, rowBytes, 0);
560 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561
562 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000563 * Writes a rectangle of pixels to a texture.
564 * @param texture the render target to read from.
565 * @param left left edge of the rectangle to write (inclusive)
566 * @param top top edge of the rectangle to write (inclusive)
567 * @param width width of rectangle to write in pixels.
568 * @param height height of rectangle to write in pixels.
569 * @param config the pixel config of the source buffer
570 * @param buffer memory to read pixels from
571 * @param rowBytes number of bytes bewtween consecutive rows. Zero
572 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000574 void writeTexturePixels(GrTexture* texture,
575 int left, int top, int width, int height,
576 GrPixelConfig config, const void* buffer,
577 size_t rowBytes) {
578 this->internalWriteTexturePixels(texture, left, top, width, height,
579 config, buffer, rowBytes, 0);
580 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000581 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000582 * Copies all texels from one texture to another.
583 * @param src the texture to copy from.
584 * @param dst the render target to copy to.
585 */
586 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000587
588 /**
589 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
590 * downsampled to the associated GrTexture (accessible via
591 * GrRenderTarget::asTexture()). Any pending draws to the render target will
592 * be executed before the resolve.
593 *
594 * This is only necessary when a client wants to access the object directly
595 * using the underlying graphics API. GrContext will detect when it must
596 * perform a resolve to a GrTexture used as the source of a draw or before
597 * reading pixels back from a GrTexture or GrRenderTarget.
598 */
599 void resolveRenderTarget(GrRenderTarget* target);
600
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000601 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000602 * Applies a 2D Gaussian blur to a given texture.
603 * @param srcTexture The source texture to be blurred.
604 * @param temp1 A scratch texture. Must not be NULL.
605 * @param temp2 A scratch texture. May be NULL, in which case
606 * srcTexture is overwritten with intermediate
607 * results.
608 * @param rect The destination rectangle.
609 * @param sigmaX The blur's standard deviation in X.
610 * @param sigmaY The blur's standard deviation in Y.
611 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000612 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000613 GrTexture* gaussianBlur(GrTexture* srcTexture,
614 GrAutoScratchTexture* temp1,
615 GrAutoScratchTexture* temp2,
616 const SkRect& rect,
617 float sigmaX, float sigmaY);
618
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000619 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000620 * This enum is used with the function below, applyMorphology.
621 */
622 enum MorphologyType {
623 kErode_MorphologyType,
624 kDilate_MorphologyType,
625 };
626
627 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000628 * Applies a 2D morphology to a given texture.
629 * @param srcTexture The source texture to be blurred.
630 * @param rect The destination rectangle.
631 * @param temp1 A scratch texture. Must not be NULL.
632 * @param temp2 A scratch texture. Must not be NULL.
633 * @param filter The morphology filter. Must be kDilate_Filter or
634 * kErode_Filter.
635 * @param radius The morphology radius in X and Y. The filter is
636 * applied to a fWidth by fHeight rectangle of
637 * pixels.
638 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000639 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000640 GrTexture* applyMorphology(GrTexture* srcTexture,
641 const GrRect& rect,
642 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000643 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000644 SkISize radius);
645
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646 ///////////////////////////////////////////////////////////////////////////
647 // Helpers
648
649 class AutoRenderTarget : ::GrNoncopyable {
650 public:
651 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
652 fContext = NULL;
653 fPrevTarget = context->getRenderTarget();
654 if (fPrevTarget != target) {
655 context->setRenderTarget(target);
656 fContext = context;
657 }
658 }
659 ~AutoRenderTarget() {
660 if (fContext) {
661 fContext->setRenderTarget(fPrevTarget);
662 }
663 }
664 private:
665 GrContext* fContext;
666 GrRenderTarget* fPrevTarget;
667 };
668
669
670 ///////////////////////////////////////////////////////////////////////////
671 // Functions intended for internal use only.
672 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000673 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674 GrFontCache* getFontCache() { return fFontCache; }
675 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000677 void resetStats();
678 const GrGpuStats& getStats() const;
679 void printStats() const;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000680 /**
681 * Stencil buffers add themselves to the cache using
682 * addAndLockStencilBuffer. When a SB's RT-attachment count
683 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
684 * eligible for purging. findStencilBuffer is called to check the cache for
685 * a SB that matching an RT's criteria. If a match is found that has been
686 * unlocked (its attachment count has reached 0) then it will be relocked.
687 */
688 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
689 void unlockStencilBuffer(GrResourceEntry* sbEntry);
690 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000692 /*
693 * postClipPush acts as a hint to this and lower-level classes (e.g.,
694 * GrGpu) that the clip stack has changed.
695 */
696 virtual void postClipPush();
697
698 /*
699 * preClipPop acts as a hint that the clip stack has been restored to an
700 * earlier state.
701 */
702 virtual void preClipPop();
703
robertphillips@google.com2c756812012-05-22 20:28:23 +0000704 GrPathRenderer* getPathRenderer(const SkPath& path,
705 GrPathFill fill,
706 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000707 bool antiAlias,
708 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000709
bsalomon@google.com27847de2011-02-22 20:59:41 +0000710private:
711 // used to keep track of when we need to flush the draw buffer
712 enum DrawCategory {
713 kBuffered_DrawCategory, // last draw was inserted in draw buffer
714 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000715 };
716 DrawCategory fLastDrawCategory;
717
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000718 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000719 GrDrawState* fDrawState;
720
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000721 GrResourceCache* fTextureCache;
722 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000723
bsalomon@google.com30085192011-08-19 15:42:31 +0000724 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000725 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000726
727 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
728 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
729 GrInOrderDrawBuffer* fDrawBuffer;
730
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 GrIndexBuffer* fAAFillRectIndexBuffer;
732 GrIndexBuffer* fAAStrokeRectIndexBuffer;
733
bsalomon@google.com27847de2011-02-22 20:59:41 +0000734 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000735
bsalomon@google.com205d4602011-04-25 12:43:45 +0000736 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000737 const GrRect& devRect,
738 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739
740 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000741 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 const GrVec& devStrokeSize,
743 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000744
745 inline int aaFillRectIndexCount() const;
746 GrIndexBuffer* aaFillRectIndexBuffer();
747
748 inline int aaStrokeRectIndexCount() const;
749 GrIndexBuffer* aaStrokeRectIndexBuffer();
750
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000751 void setupDrawBuffer();
752
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753 void flushDrawBuffer();
754
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000755 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000756
bsalomon@google.com27847de2011-02-22 20:59:41 +0000757 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
758
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000759 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000760 GrPathFill fill, const GrPoint* translate);
761
bsalomon@google.com6f379512011-11-16 20:36:03 +0000762 /**
763 * Flags to the internal read/write pixels funcs
764 */
765 enum PixelOpsFlags {
766 kDontFlush_PixelOpsFlag = 0x1,
767 };
768
769 bool internalReadRenderTargetPixels(GrRenderTarget* target,
770 int left, int top,
771 int width, int height,
772 GrPixelConfig config, void* buffer,
773 size_t rowBytes, uint32_t flags);
774
775 void internalWriteRenderTargetPixels(GrRenderTarget* target,
776 int left, int top,
777 int width, int height,
778 GrPixelConfig, const void* buffer,
779 size_t rowBytes, uint32_t flags);
780
781 bool internalReadTexturePixels(GrTexture* texture,
782 int left, int top,
783 int width, int height,
784 GrPixelConfig config, void* buffer,
785 size_t rowBytes, uint32_t flags);
786
787 void internalWriteTexturePixels(GrTexture* texture,
788 int left, int top,
789 int width, int height,
790 GrPixelConfig config, const void* buffer,
791 size_t rowBytes, uint32_t flags);
792 // needed for access to internalWriteTexturePixels. TODO: make GrContext
793 // be a facade for an internal class. Then functions that are public on the
794 // internal class would have only be callable in src/gpu. The facade would
795 // only have to functions necessary for clients.
796 friend class GrAtlas;
797
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000798 // computes vertex layout bits based on the paint. If paint expresses
799 // a texture for a stage, the stage coords will be bound to postitions
800 // unless hasTexCoords[s]==true in which case stage s's input coords
801 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
802 // for an array where all the values are false.
803 static int PaintStageVertexLayoutBits(
804 const GrPaint& paint,
805 const bool hasTexCoords[GrPaint::kTotalStages]);
806
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807};
808
809/**
810 * Save/restore the view-matrix in the context.
811 */
812class GrAutoMatrix : GrNoncopyable {
813public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000814 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000815 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
816 fMatrix = ctx->getMatrix();
817 }
818 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
819 fMatrix = ctx->getMatrix();
820 ctx->setMatrix(matrix);
821 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000822 void set(GrContext* ctx) {
823 if (NULL != fContext) {
824 fContext->setMatrix(fMatrix);
825 }
826 fMatrix = ctx->getMatrix();
827 fContext = ctx;
828 }
829 void set(GrContext* ctx, const GrMatrix& matrix) {
830 if (NULL != fContext) {
831 fContext->setMatrix(fMatrix);
832 }
833 fMatrix = ctx->getMatrix();
834 ctx->setMatrix(matrix);
835 fContext = ctx;
836 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000837 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000838 if (NULL != fContext) {
839 fContext->setMatrix(fMatrix);
840 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 }
842
843private:
844 GrContext* fContext;
845 GrMatrix fMatrix;
846};
847
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000848/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849 * Gets and locks a scratch texture from a descriptor using
850 * either exact or approximate criteria. Unlocks texture in
851 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000852 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000853class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000854public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000855 GrAutoScratchTexture()
856 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000857 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000858
859 GrAutoScratchTexture(GrContext* context,
860 const GrTextureDesc& desc,
861 GrContext::ScratchTexMatch match =
862 GrContext::kApprox_ScratchTexMatch)
863 : fContext(NULL) {
864 this->set(context, desc, match);
865 }
866
867 ~GrAutoScratchTexture() {
868 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000869 fContext->unlockTexture(fEntry);
870 }
871 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000872
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000873 GrTexture* set(GrContext* context,
874 const GrTextureDesc& desc,
875 GrContext::ScratchTexMatch match =
876 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000877 if (NULL != fContext) {
878 fContext->unlockTexture(fEntry);
879 fEntry.reset();
880 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000881 fContext = context;
882 if (NULL != fContext) {
883 fEntry = fContext->lockScratchTexture(desc, match);
884 GrTexture* ret = fEntry.texture();
885 if (NULL == ret) {
886 fContext = NULL;
887 }
888 return ret;
889 } else {
890 return NULL;
891 }
892 }
893
894 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000895private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000896 GrContext* fContext;
897 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000898};
899
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900#endif