blob: 7f47c4fdba683b524708709572ca7b0539e472ba [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;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000024class GrIndexBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +000025class GrIndexBufferAllocPool;
26class GrInOrderDrawBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000027class GrPathRenderer;
bsalomon@google.com30085192011-08-19 15:42:31 +000028class GrPathRendererChain;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000029class GrResourceEntry;
30class GrResourceCache;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000031class GrStencilBuffer;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000032class GrVertexBuffer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000033class GrVertexBufferAllocPool;
robertphillips@google.com72176b22012-05-23 13:19:12 +000034class GrSoftwarePathRenderer;
bsalomon@google.com50398bf2011-07-26 20:45:30 +000035
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 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000111 * Create a new entry, based on the specified key and texture, and return
112 * its "locked" entry. Must call be balanced with an unlockTexture() call.
113 *
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000114 * @param sampler The sampler state used to draw a texture may be used
115 * to determine how to store the pixel data in the texture
116 * cache. (e.g. different versions may exist for different
117 * wrap modes on GPUs with limited or no NPOT texture
118 * support). Only the wrap and filter fields are used. NULL
119 * implies clamp wrap modes and nearest filtering.
120 * @param desc Description of the texture properties.
121 * @param srcData Pointer to the pixel values.
122 * @param rowBytes The number of bytes between rows of the texture. Zero
123 * implies tightly packed rows.
124 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000125 TextureCacheEntry createAndLockTexture(const GrSamplerState* sampler,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000126 const GrTextureDesc& desc,
127 void* srcData, size_t rowBytes);
128
129 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000130 * Search for an entry based on key and dimensions. If found, "lock" it and
131 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000132 * Must be balanced with an unlockTexture() call.
133 *
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000134 * @param desc Description of the texture properties.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000135 * @param sampler The sampler state used to draw a texture may be used
136 * to determine the cache entry used. (e.g. different
137 * versions may exist for different wrap modes on GPUs with
138 * limited or no NPOT texture support). Only the wrap and
139 * filter fields are used. NULL implies clamp wrap modes
140 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000141 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000142 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000143 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000144 /**
145 * Determines whether a texture is in the cache. If the texture is found it
146 * will not be locked or returned. This call does not affect the priority of
147 * the texture for deletion.
148 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000149 bool isTextureInCache(const GrTextureDesc& desc,
150 const GrSamplerState* sampler) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000151
152 /**
153 * Enum that determines how closely a returned scratch texture must match
154 * a provided GrTextureDesc.
155 */
156 enum ScratchTexMatch {
157 /**
158 * Finds a texture that exactly matches the descriptor.
159 */
160 kExact_ScratchTexMatch,
161 /**
162 * Finds a texture that approximately matches the descriptor. Will be
163 * at least as large in width and height as desc specifies. If desc
164 * specifies that texture is a render target then result will be a
165 * render target. If desc specifies a render target and doesn't set the
166 * no stencil flag then result will have a stencil. Format and aa level
167 * will always match.
168 */
169 kApprox_ScratchTexMatch
170 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000171
172 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000173 * Returns a texture matching the desc. It's contents are unknown. Subsequent
174 * requests with the same descriptor are not guaranteed to return the same
175 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000176 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000177 *
178 * Textures created by createAndLockTexture() hide the complications of
179 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000180 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000181 * such an API will create gaps in the tiling pattern. This includes clamp
182 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000183 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000184 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
185 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000186
187 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000188 * When done with an entry, call unlockTexture(entry) on it, which returns
189 * it to the cache, where it may be purged.
190 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000191 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000192
193 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194 * Creates a texture that is outside the cache. Does not count against
195 * cache's budget.
196 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000197 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198 void* srcData,
199 size_t rowBytes);
200
201 /**
202 * Returns true if the specified use of an indexed texture is supported.
203 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000204 bool supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000205 int width,
206 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207
208 /**
209 * Return the current texture cache limits.
210 *
211 * @param maxTextures If non-null, returns maximum number of textures that
212 * can be held in the cache.
213 * @param maxTextureBytes If non-null, returns maximum number of bytes of
214 * texture memory that can be held in the cache.
215 */
216 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
217
218 /**
219 * Specify the texture cache limits. If the current cache exceeds either
220 * of these, it will be purged (LRU) to keep the cache within these limits.
221 *
222 * @param maxTextures The maximum number of textures that can be held in
223 * the cache.
224 * @param maxTextureBytes The maximum number of bytes of texture memory
225 * that can be held in the cache.
226 */
227 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
228
229 /**
230 * Return the max width or height of a texture supported by the current gpu
231 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000232 int getMaxTextureSize() const;
233
234 /**
235 * Return the max width or height of a render target supported by the
236 * current gpu
237 */
238 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239
240 ///////////////////////////////////////////////////////////////////////////
241 // Render targets
242
243 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000244 * Sets the render target.
245 * @param target the render target to set. (should not be NULL.)
246 */
247 void setRenderTarget(GrRenderTarget* target);
248
249 /**
250 * Gets the current render target.
251 * @return the currently bound render target. Should never be NULL.
252 */
253 const GrRenderTarget* getRenderTarget() const;
254 GrRenderTarget* getRenderTarget();
255
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000256 /**
257 * Can the provided configuration act as a color render target?
258 */
259 bool isConfigRenderable(GrPixelConfig config) const;
260
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000261 ///////////////////////////////////////////////////////////////////////////
262 // Platform Surfaces
263
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000264 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000265 * Wraps an existing texture with a GrTexture object.
266 *
267 * OpenGL: if the object is a texture Gr may change its GL texture params
268 * when it is drawn.
269 *
270 * @param desc description of the object to create.
271 *
272 * @return GrTexture object or NULL on failure.
273 */
274 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
275
276 /**
277 * Wraps an existing render target with a GrRenderTarget object. It is
278 * similar to createPlatformTexture but can be used to draw into surfaces
279 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
280 * the client will resolve to a texture).
281 *
282 * @param desc description of the object to create.
283 *
284 * @return GrTexture object or NULL on failure.
285 */
286 GrRenderTarget* createPlatformRenderTarget(
287 const GrPlatformRenderTargetDesc& desc);
288
bsalomon@google.com27847de2011-02-22 20:59:41 +0000289 ///////////////////////////////////////////////////////////////////////////
290 // Matrix state
291
292 /**
293 * Gets the current transformation matrix.
294 * @return the current matrix.
295 */
296 const GrMatrix& getMatrix() const;
297
298 /**
299 * Sets the transformation matrix.
300 * @param m the matrix to set.
301 */
302 void setMatrix(const GrMatrix& m);
303
304 /**
305 * Concats the current matrix. The passed matrix is applied before the
306 * current matrix.
307 * @param m the matrix to concat.
308 */
309 void concatMatrix(const GrMatrix& m) const;
310
311
312 ///////////////////////////////////////////////////////////////////////////
313 // Clip state
314 /**
315 * Gets the current clip.
316 * @return the current clip.
317 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000318 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000319
320 /**
321 * Sets the clip.
322 * @param clip the clip to set.
323 */
324 void setClip(const GrClip& clip);
325
326 /**
327 * Convenience method for setting the clip to a rect.
328 * @param rect the rect to set as the new clip.
329 */
330 void setClip(const GrIRect& rect);
331
332 ///////////////////////////////////////////////////////////////////////////
333 // Draws
334
335 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000336 * Clear the entire or rect of the render target, ignoring any clips.
337 * @param rect the rect to clear or the whole thing if rect is NULL.
338 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000339 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000340 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000341
342 /**
343 * Draw everywhere (respecting the clip) with the paint.
344 */
345 void drawPaint(const GrPaint& paint);
346
347 /**
348 * Draw the rect using a paint.
349 * @param paint describes how to color pixels.
350 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
351 * the rect is mitered stroked based on strokeWidth. If
352 * strokeWidth == 0, then the stroke is always a single
353 * pixel thick.
354 * @param matrix Optional matrix applied to the rect. Applied before
355 * context's matrix or the paint's matrix.
356 * The rects coords are used to access the paint (through texture matrix)
357 */
358 void drawRect(const GrPaint& paint,
359 const GrRect&,
360 GrScalar strokeWidth = -1,
361 const GrMatrix* matrix = NULL);
362
363 /**
364 * Maps a rect of paint coordinates onto the a rect of destination
365 * coordinates. Each rect can optionally be transformed. The srcRect
366 * is stretched over the dstRect. The dstRect is transformed by the
367 * context's matrix and the srcRect is transformed by the paint's matrix.
368 * Additional optional matrices can be provided by parameters.
369 *
370 * @param paint describes how to color pixels.
371 * @param dstRect the destination rect to draw.
372 * @param srcRect rect of paint coordinates to be mapped onto dstRect
373 * @param dstMatrix Optional matrix to transform dstRect. Applied before
374 * context's matrix.
375 * @param srcMatrix Optional matrix to transform srcRect Applied before
376 * paint's matrix.
377 */
378 void drawRectToRect(const GrPaint& paint,
379 const GrRect& dstRect,
380 const GrRect& srcRect,
381 const GrMatrix* dstMatrix = NULL,
382 const GrMatrix* srcMatrix = NULL);
383
384 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000385 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386 *
387 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000388 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389 * @param fill the path filling rule to use.
390 * @param translate optional additional translation applied to the
391 * path.
392 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000393 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000394 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000395
bsalomon@google.com27847de2011-02-22 20:59:41 +0000396 /**
397 * Draws vertices with a paint.
398 *
399 * @param paint describes how to color pixels.
400 * @param primitiveType primitives type to draw.
401 * @param vertexCount number of vertices.
402 * @param positions array of vertex positions, required.
403 * @param texCoords optional array of texture coordinates used
404 * to access the paint.
405 * @param colors optional array of per-vertex colors, supercedes
406 * the paint's color field.
407 * @param indices optional array of indices. If NULL vertices
408 * are drawn non-indexed.
409 * @param indexCount if indices is non-null then this is the
410 * number of indices.
411 */
412 void drawVertices(const GrPaint& paint,
413 GrPrimitiveType primitiveType,
414 int vertexCount,
415 const GrPoint positions[],
416 const GrPoint texs[],
417 const GrColor colors[],
418 const uint16_t indices[],
419 int indexCount);
420
bsalomon@google.com93c96602012-04-27 13:05:21 +0000421 /**
422 * Draws an oval.
423 *
424 * @param paint describes how to color pixels.
425 * @param rect the bounding rect of the oval.
426 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
427 * the rect is stroked based on strokeWidth. If
428 * strokeWidth == 0, then the stroke is always a single
429 * pixel thick.
430 */
431 void drawOval(const GrPaint& paint,
432 const GrRect& rect,
433 SkScalar strokeWidth);
434
bsalomon@google.com27847de2011-02-22 20:59:41 +0000435 ///////////////////////////////////////////////////////////////////////////
436 // Misc.
437
438 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000439 * Flags that affect flush() behavior.
440 */
441 enum FlushBits {
442 /**
443 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
444 * it can be rendered to directly. However, Gr lazily sets state. Simply
445 * calling setRenderTarget() followed by flush() without flags may not
446 * bind the render target. This flag forces the context to bind the last
447 * set render target in the 3D API.
448 */
449 kForceCurrentRenderTarget_FlushBit = 0x1,
450 /**
451 * A client may reach a point where it has partially rendered a frame
452 * through a GrContext that it knows the user will never see. This flag
453 * causes the flush to skip submission of deferred content to the 3D API
454 * during the flush.
455 */
456 kDiscard_FlushBit = 0x2,
457 };
458
459 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000460 * Call to ensure all drawing to the context has been issued to the
461 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000462 * @param flagsBitfield flags that control the flushing behavior. See
463 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000464 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000465 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000466
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000468 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000469 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000470 * current render target.
471 * @param left left edge of the rectangle to read (inclusive)
472 * @param top top edge of the rectangle to read (inclusive)
473 * @param width width of rectangle to read in pixels.
474 * @param height height of rectangle to read in pixels.
475 * @param config the pixel config of the destination buffer
476 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000477 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000478 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000479 *
480 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000481 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000482 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000483 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000484 bool readRenderTargetPixels(GrRenderTarget* target,
485 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000486 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000487 size_t rowBytes) {
488 return this->internalReadRenderTargetPixels(target, left, top,
489 width, height,
490 config, buffer,
491 rowBytes, 0);
492 }
493
494 /**
495 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
496 * at the specified rectangle.
497 * @param target the render target to write into. NULL means the
498 * current render target.
499 * @param left left edge of the rectangle to write (inclusive)
500 * @param top top edge of the rectangle to write (inclusive)
501 * @param width width of rectangle to write in pixels.
502 * @param height height of rectangle to write in pixels.
503 * @param config the pixel config of the source buffer
504 * @param buffer memory to read the rectangle from.
505 * @param rowBytes number of bytes bewtween consecutive rows. Zero
506 * means rows are tightly packed.
507 */
508 void writeRenderTargetPixels(GrRenderTarget* target,
509 int left, int top, int width, int height,
510 GrPixelConfig config, const void* buffer,
511 size_t rowBytes) {
512 this->internalWriteRenderTargetPixels(target, left, top, width, height,
513 config, buffer, rowBytes, 0);
514 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000515
516 /**
517 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000518 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000519 * @param left left edge of the rectangle to read (inclusive)
520 * @param top top edge of the rectangle to read (inclusive)
521 * @param width width of rectangle to read in pixels.
522 * @param height height of rectangle to read in pixels.
523 * @param config the pixel config of the destination buffer
524 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000525 * @param rowBytes number of bytes bewtween consecutive rows. Zero
526 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000527 *
528 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000530 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000532 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533 GrPixelConfig config, void* buffer,
534 size_t rowBytes) {
535 return this->internalReadTexturePixels(texture, left, top,
536 width, height,
537 config, buffer, rowBytes, 0);
538 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539
540 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000541 * Writes a rectangle of pixels to a texture.
542 * @param texture the render target to read from.
543 * @param left left edge of the rectangle to write (inclusive)
544 * @param top top edge of the rectangle to write (inclusive)
545 * @param width width of rectangle to write in pixels.
546 * @param height height of rectangle to write in pixels.
547 * @param config the pixel config of the source buffer
548 * @param buffer memory to read pixels from
549 * @param rowBytes number of bytes bewtween consecutive rows. Zero
550 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000552 void writeTexturePixels(GrTexture* texture,
553 int left, int top, int width, int height,
554 GrPixelConfig config, const void* buffer,
555 size_t rowBytes) {
556 this->internalWriteTexturePixels(texture, left, top, width, height,
557 config, buffer, rowBytes, 0);
558 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000559 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000560 * Copies all texels from one texture to another.
561 * @param src the texture to copy from.
562 * @param dst the render target to copy to.
563 */
564 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000565
566 /**
567 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
568 * downsampled to the associated GrTexture (accessible via
569 * GrRenderTarget::asTexture()). Any pending draws to the render target will
570 * be executed before the resolve.
571 *
572 * This is only necessary when a client wants to access the object directly
573 * using the underlying graphics API. GrContext will detect when it must
574 * perform a resolve to a GrTexture used as the source of a draw or before
575 * reading pixels back from a GrTexture or GrRenderTarget.
576 */
577 void resolveRenderTarget(GrRenderTarget* target);
578
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000579 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000580 * Applies a 2D Gaussian blur to a given texture.
581 * @param srcTexture The source texture to be blurred.
582 * @param temp1 A scratch texture. Must not be NULL.
583 * @param temp2 A scratch texture. May be NULL, in which case
584 * srcTexture is overwritten with intermediate
585 * results.
586 * @param rect The destination rectangle.
587 * @param sigmaX The blur's standard deviation in X.
588 * @param sigmaY The blur's standard deviation in Y.
589 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000590 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000591 GrTexture* gaussianBlur(GrTexture* srcTexture,
592 GrAutoScratchTexture* temp1,
593 GrAutoScratchTexture* temp2,
594 const SkRect& rect,
595 float sigmaX, float sigmaY);
596
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000597 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000598 * This enum is used with the function below, applyMorphology.
599 */
600 enum MorphologyType {
601 kErode_MorphologyType,
602 kDilate_MorphologyType,
603 };
604
605 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000606 * Applies a 2D morphology to a given texture.
607 * @param srcTexture The source texture to be blurred.
608 * @param rect The destination rectangle.
609 * @param temp1 A scratch texture. Must not be NULL.
610 * @param temp2 A scratch texture. Must not be NULL.
611 * @param filter The morphology filter. Must be kDilate_Filter or
612 * kErode_Filter.
613 * @param radius The morphology radius in X and Y. The filter is
614 * applied to a fWidth by fHeight rectangle of
615 * pixels.
616 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000617 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000618 GrTexture* applyMorphology(GrTexture* srcTexture,
619 const GrRect& rect,
620 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000621 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000622 SkISize radius);
623
bsalomon@google.com27847de2011-02-22 20:59:41 +0000624 ///////////////////////////////////////////////////////////////////////////
625 // Helpers
626
627 class AutoRenderTarget : ::GrNoncopyable {
628 public:
629 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
630 fContext = NULL;
631 fPrevTarget = context->getRenderTarget();
632 if (fPrevTarget != target) {
633 context->setRenderTarget(target);
634 fContext = context;
635 }
636 }
637 ~AutoRenderTarget() {
638 if (fContext) {
639 fContext->setRenderTarget(fPrevTarget);
640 }
641 }
642 private:
643 GrContext* fContext;
644 GrRenderTarget* fPrevTarget;
645 };
646
647
648 ///////////////////////////////////////////////////////////////////////////
649 // Functions intended for internal use only.
650 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000651 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000652 GrFontCache* getFontCache() { return fFontCache; }
653 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000654 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000655
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000656 /**
657 * Stencil buffers add themselves to the cache using
658 * addAndLockStencilBuffer. When a SB's RT-attachment count
659 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
660 * eligible for purging. findStencilBuffer is called to check the cache for
661 * a SB that matching an RT's criteria. If a match is found that has been
662 * unlocked (its attachment count has reached 0) then it will be relocked.
663 */
664 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
665 void unlockStencilBuffer(GrResourceEntry* sbEntry);
666 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000667
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000668 /*
669 * postClipPush acts as a hint to this and lower-level classes (e.g.,
670 * GrGpu) that the clip stack has changed.
671 */
672 virtual void postClipPush();
673
674 /*
675 * preClipPop acts as a hint that the clip stack has been restored to an
676 * earlier state.
677 */
678 virtual void preClipPop();
679
robertphillips@google.com2c756812012-05-22 20:28:23 +0000680 GrPathRenderer* getPathRenderer(const SkPath& path,
681 GrPathFill fill,
682 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000683 bool antiAlias,
684 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000685
bsalomon@google.com27847de2011-02-22 20:59:41 +0000686private:
687 // used to keep track of when we need to flush the draw buffer
688 enum DrawCategory {
689 kBuffered_DrawCategory, // last draw was inserted in draw buffer
690 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000691 };
692 DrawCategory fLastDrawCategory;
693
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000694 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000695 GrDrawState* fDrawState;
696
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000697 GrResourceCache* fTextureCache;
698 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000699
bsalomon@google.com30085192011-08-19 15:42:31 +0000700 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000701 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000702
703 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
704 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
705 GrInOrderDrawBuffer* fDrawBuffer;
706
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 GrIndexBuffer* fAAFillRectIndexBuffer;
708 GrIndexBuffer* fAAStrokeRectIndexBuffer;
709
bsalomon@google.com27847de2011-02-22 20:59:41 +0000710 GrContext(GrGpu* gpu);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000711
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 void fillAARect(GrDrawTarget* target,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000713 const GrRect& devRect,
714 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000715
716 void strokeAARect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000717 const GrRect& devRect,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000718 const GrVec& devStrokeSize,
719 bool useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720
721 inline int aaFillRectIndexCount() const;
722 GrIndexBuffer* aaFillRectIndexBuffer();
723
724 inline int aaStrokeRectIndexCount() const;
725 GrIndexBuffer* aaStrokeRectIndexBuffer();
726
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000727 void setupDrawBuffer();
728
bsalomon@google.com27847de2011-02-22 20:59:41 +0000729 void flushDrawBuffer();
730
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000731 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000732
bsalomon@google.com27847de2011-02-22 20:59:41 +0000733 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
734
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000735 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000736 GrPathFill fill, const GrPoint* translate);
737
bsalomon@google.com6f379512011-11-16 20:36:03 +0000738 /**
739 * Flags to the internal read/write pixels funcs
740 */
741 enum PixelOpsFlags {
742 kDontFlush_PixelOpsFlag = 0x1,
743 };
744
745 bool internalReadRenderTargetPixels(GrRenderTarget* target,
746 int left, int top,
747 int width, int height,
748 GrPixelConfig config, void* buffer,
749 size_t rowBytes, uint32_t flags);
750
751 void internalWriteRenderTargetPixels(GrRenderTarget* target,
752 int left, int top,
753 int width, int height,
754 GrPixelConfig, const void* buffer,
755 size_t rowBytes, uint32_t flags);
756
757 bool internalReadTexturePixels(GrTexture* texture,
758 int left, int top,
759 int width, int height,
760 GrPixelConfig config, void* buffer,
761 size_t rowBytes, uint32_t flags);
762
763 void internalWriteTexturePixels(GrTexture* texture,
764 int left, int top,
765 int width, int height,
766 GrPixelConfig config, const void* buffer,
767 size_t rowBytes, uint32_t flags);
768 // needed for access to internalWriteTexturePixels. TODO: make GrContext
769 // be a facade for an internal class. Then functions that are public on the
770 // internal class would have only be callable in src/gpu. The facade would
771 // only have to functions necessary for clients.
772 friend class GrAtlas;
773
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000774 // computes vertex layout bits based on the paint. If paint expresses
775 // a texture for a stage, the stage coords will be bound to postitions
776 // unless hasTexCoords[s]==true in which case stage s's input coords
777 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
778 // for an array where all the values are false.
779 static int PaintStageVertexLayoutBits(
780 const GrPaint& paint,
781 const bool hasTexCoords[GrPaint::kTotalStages]);
782
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783};
784
785/**
786 * Save/restore the view-matrix in the context.
787 */
788class GrAutoMatrix : GrNoncopyable {
789public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000790 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
792 fMatrix = ctx->getMatrix();
793 }
794 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
795 fMatrix = ctx->getMatrix();
796 ctx->setMatrix(matrix);
797 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000798 void set(GrContext* ctx) {
799 if (NULL != fContext) {
800 fContext->setMatrix(fMatrix);
801 }
802 fMatrix = ctx->getMatrix();
803 fContext = ctx;
804 }
805 void set(GrContext* ctx, const GrMatrix& matrix) {
806 if (NULL != fContext) {
807 fContext->setMatrix(fMatrix);
808 }
809 fMatrix = ctx->getMatrix();
810 ctx->setMatrix(matrix);
811 fContext = ctx;
812 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000814 if (NULL != fContext) {
815 fContext->setMatrix(fMatrix);
816 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000817 }
818
819private:
820 GrContext* fContext;
821 GrMatrix fMatrix;
822};
823
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000824/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000825 * Gets and locks a scratch texture from a descriptor using
826 * either exact or approximate criteria. Unlocks texture in
827 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000828 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000829class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000830public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000831 GrAutoScratchTexture()
832 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000833 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000834
835 GrAutoScratchTexture(GrContext* context,
836 const GrTextureDesc& desc,
837 GrContext::ScratchTexMatch match =
838 GrContext::kApprox_ScratchTexMatch)
839 : fContext(NULL) {
840 this->set(context, desc, match);
841 }
842
843 ~GrAutoScratchTexture() {
844 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 fContext->unlockTexture(fEntry);
846 }
847 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000848
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849 GrTexture* set(GrContext* context,
850 const GrTextureDesc& desc,
851 GrContext::ScratchTexMatch match =
852 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000853 if (NULL != fContext) {
854 fContext->unlockTexture(fEntry);
855 fEntry.reset();
856 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000857 fContext = context;
858 if (NULL != fContext) {
859 fEntry = fContext->lockScratchTexture(desc, match);
860 GrTexture* ret = fEntry.texture();
861 if (NULL == ret) {
862 fContext = NULL;
863 }
864 return ret;
865 } else {
866 return NULL;
867 }
868 }
869
870 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000871private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000872 GrContext* fContext;
873 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000874};
875
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876#endif