blob: 46e80e0c4f3391e793c7446d0ebfb7654d29ea68 [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"
robertphillips@google.comf6747b02012-06-12 00:32:28 +000015#include "GrAARectRenderer.h"
bsalomon@google.comc287a892011-08-19 14:49:36 +000016// not strictly needed but requires WK change in LayerTextureUpdaterCanvas to
17// remove.
18#include "GrRenderTarget.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000020class GrAutoScratchTexture;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000021class GrDrawState;
bsalomon@google.com583a1e32011-08-17 13:42:46 +000022class GrDrawTarget;
bsalomon@google.com27847de2011-02-22 20:59:41 +000023class GrFontCache;
bsalomon@google.com05ef5102011-05-02 21:14:59 +000024class GrGpu;
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.comc0af3172012-06-15 14:10:09 +000045 /**
46 * Returns the number of GrContext instances for the current thread.
47 */
48 static int GetThreadInstanceCount();
49
bsalomon@google.com27847de2011-02-22 20:59:41 +000050 virtual ~GrContext();
51
52 /**
53 * The GrContext normally assumes that no outsider is setting state
54 * within the underlying 3D API's context/device/whatever. This call informs
55 * the context that the state was modified and it should resend. Shouldn't
56 * be called frequently for good performance.
57 */
58 void resetContext();
59
bsalomon@google.com8fe72472011-03-30 21:26:44 +000060 /**
61 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
62 * if you have lost the associated GPU context, and thus internal texture,
63 * buffer, etc. references/IDs are now invalid. Should be called even when
64 * GrContext is no longer going to be used for two reasons:
65 * 1) ~GrContext will not try to free the objects in the 3D API.
66 * 2) If you've created GrResources that outlive the GrContext they will
67 * be marked as invalid (GrResource::isValid()) and won't attempt to
68 * free their underlying resource in the 3D API.
69 * Content drawn since the last GrContext::flush() may be lost.
70 */
71 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000072
73 /**
junov@google.com53a55842011-06-08 22:55:10 +000074 * Similar to contextLost, but makes no attempt to reset state.
75 * Use this method when GrContext destruction is pending, but
76 * the graphics context is destroyed first.
77 */
78 void contextDestroyed();
79
80 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 * Frees gpu created by the context. Can be called to reduce GPU memory
82 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000083 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 void freeGpuResources();
85
86 ///////////////////////////////////////////////////////////////////////////
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; }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000103 GrResourceEntry* cacheEntry() { return fEntry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000104 private:
105 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
106 void set(GrResourceEntry* entry) { fEntry = entry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000107 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 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000112 * Create a new entry, based on the specified key and texture, and return
113 * its "locked" entry. Must call be balanced with an unlockTexture() call.
114 *
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000115 * @param sampler The sampler state used to draw a texture may be used
116 * to determine how to store the pixel data in the texture
117 * cache. (e.g. different versions may exist for different
118 * wrap modes on GPUs with limited or no NPOT texture
119 * support). Only the wrap and filter fields are used. NULL
120 * implies clamp wrap modes and nearest filtering.
121 * @param desc Description of the texture properties.
122 * @param srcData Pointer to the pixel values.
123 * @param rowBytes The number of bytes between rows of the texture. Zero
124 * implies tightly packed rows.
125 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000126 TextureCacheEntry createAndLockTexture(const GrSamplerState* sampler,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000127 const GrTextureDesc& desc,
128 void* srcData, size_t rowBytes);
129
130 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000131 * Search for an entry based on key and dimensions. If found, "lock" it and
132 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000133 * Must be balanced with an unlockTexture() call.
134 *
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000135 * @param desc Description of the texture properties.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000136 * @param sampler The sampler state used to draw a texture may be used
137 * to determine the cache entry used. (e.g. different
138 * versions may exist for different wrap modes on GPUs with
139 * limited or no NPOT texture support). Only the wrap and
140 * filter fields are used. NULL implies clamp wrap modes
141 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000142 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000143 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000144 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000145 /**
146 * Determines whether a texture is in the cache. If the texture is found it
147 * will not be locked or returned. This call does not affect the priority of
148 * the texture for deletion.
149 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000150 bool isTextureInCache(const GrTextureDesc& desc,
151 const GrSamplerState* sampler) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000152
153 /**
154 * Enum that determines how closely a returned scratch texture must match
155 * a provided GrTextureDesc.
156 */
157 enum ScratchTexMatch {
158 /**
159 * Finds a texture that exactly matches the descriptor.
160 */
161 kExact_ScratchTexMatch,
162 /**
163 * Finds a texture that approximately matches the descriptor. Will be
164 * at least as large in width and height as desc specifies. If desc
165 * specifies that texture is a render target then result will be a
166 * render target. If desc specifies a render target and doesn't set the
167 * no stencil flag then result will have a stencil. Format and aa level
168 * will always match.
169 */
170 kApprox_ScratchTexMatch
171 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000172
173 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000174 * Returns a texture matching the desc. It's contents are unknown. Subsequent
175 * requests with the same descriptor are not guaranteed to return the same
176 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000177 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000178 *
179 * Textures created by createAndLockTexture() hide the complications of
180 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000181 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000182 * such an API will create gaps in the tiling pattern. This includes clamp
183 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000184 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000185 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
186 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000187
188 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000189 * When done with an entry, call unlockTexture(entry) on it, which returns
190 * it to the cache, where it may be purged.
191 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000192 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193
194 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000195 * Free any data associated with the provided entry in the texture cache
196 */
197 void freeEntry(TextureCacheEntry entry);
198
199 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000200 * Creates a texture that is outside the cache. Does not count against
201 * cache's budget.
202 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000203 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000204 void* srcData,
205 size_t rowBytes);
206
207 /**
208 * Returns true if the specified use of an indexed texture is supported.
209 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000210 bool supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000211 int width,
212 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213
214 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000215 * Return the current texture cache budget in bytes.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000217 size_t getTextureCacheBudget() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218
219 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000220 * Specify the texture cache budget. If the current cache size exceeds the
221 * budget it will immediately be purged to be within the budget.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000222 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000223 * @param maxTextureBytes The maximum number of bytes of texture memory
224 * that can be held in the cache.
225 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000226 void setTextureCacheBudget(size_t maxTextureBytes);
227 // DEPRECATED, this will be deleted soon.
228 void setTextureCacheLimits(int ignored, size_t maxTextureBytes) {
229 this->setTextureCacheBudget(maxTextureBytes);
230 }
231
232 /**
233 * Returns the current number of bytes of GPU memory hosted by the texture
234 * cache.
235 */
236 size_t getGpuTextureCacheBytes() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000237
238 /**
239 * Return the max width or height of a texture supported by the current gpu
240 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000241 int getMaxTextureSize() const;
242
243 /**
244 * Return the max width or height of a render target supported by the
245 * current gpu
246 */
247 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248
249 ///////////////////////////////////////////////////////////////////////////
250 // Render targets
251
252 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000253 * Sets the render target.
254 * @param target the render target to set. (should not be NULL.)
255 */
256 void setRenderTarget(GrRenderTarget* target);
257
258 /**
259 * Gets the current render target.
260 * @return the currently bound render target. Should never be NULL.
261 */
262 const GrRenderTarget* getRenderTarget() const;
263 GrRenderTarget* getRenderTarget();
264
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000265 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
266
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000267 /**
268 * Can the provided configuration act as a color render target?
269 */
270 bool isConfigRenderable(GrPixelConfig config) const;
271
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000272 ///////////////////////////////////////////////////////////////////////////
273 // Platform Surfaces
274
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000275 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000276 * Wraps an existing texture with a GrTexture object.
277 *
278 * OpenGL: if the object is a texture Gr may change its GL texture params
279 * when it is drawn.
280 *
281 * @param desc description of the object to create.
282 *
283 * @return GrTexture object or NULL on failure.
284 */
285 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
286
287 /**
288 * Wraps an existing render target with a GrRenderTarget object. It is
289 * similar to createPlatformTexture but can be used to draw into surfaces
290 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
291 * the client will resolve to a texture).
292 *
293 * @param desc description of the object to create.
294 *
295 * @return GrTexture object or NULL on failure.
296 */
297 GrRenderTarget* createPlatformRenderTarget(
298 const GrPlatformRenderTargetDesc& desc);
299
bsalomon@google.com27847de2011-02-22 20:59:41 +0000300 ///////////////////////////////////////////////////////////////////////////
301 // Matrix state
302
303 /**
304 * Gets the current transformation matrix.
305 * @return the current matrix.
306 */
307 const GrMatrix& getMatrix() const;
308
309 /**
310 * Sets the transformation matrix.
311 * @param m the matrix to set.
312 */
313 void setMatrix(const GrMatrix& m);
314
315 /**
316 * Concats the current matrix. The passed matrix is applied before the
317 * current matrix.
318 * @param m the matrix to concat.
319 */
320 void concatMatrix(const GrMatrix& m) const;
321
322
323 ///////////////////////////////////////////////////////////////////////////
324 // Clip state
325 /**
326 * Gets the current clip.
327 * @return the current clip.
328 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000329 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330
331 /**
332 * Sets the clip.
333 * @param clip the clip to set.
334 */
335 void setClip(const GrClip& clip);
336
337 /**
338 * Convenience method for setting the clip to a rect.
339 * @param rect the rect to set as the new clip.
340 */
341 void setClip(const GrIRect& rect);
342
343 ///////////////////////////////////////////////////////////////////////////
344 // Draws
345
346 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000347 * Clear the entire or rect of the render target, ignoring any clips.
348 * @param rect the rect to clear or the whole thing if rect is NULL.
349 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000350 * @param target if non-NULL, the render target to clear otherwise clear
351 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000352 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000353 void clear(const GrIRect* rect, GrColor color,
354 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355
356 /**
357 * Draw everywhere (respecting the clip) with the paint.
358 */
359 void drawPaint(const GrPaint& paint);
360
361 /**
362 * Draw the rect using a paint.
363 * @param paint describes how to color pixels.
364 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
365 * the rect is mitered stroked based on strokeWidth. If
366 * strokeWidth == 0, then the stroke is always a single
367 * pixel thick.
368 * @param matrix Optional matrix applied to the rect. Applied before
369 * context's matrix or the paint's matrix.
370 * The rects coords are used to access the paint (through texture matrix)
371 */
372 void drawRect(const GrPaint& paint,
373 const GrRect&,
374 GrScalar strokeWidth = -1,
375 const GrMatrix* matrix = NULL);
376
377 /**
378 * Maps a rect of paint coordinates onto the a rect of destination
379 * coordinates. Each rect can optionally be transformed. The srcRect
380 * is stretched over the dstRect. The dstRect is transformed by the
381 * context's matrix and the srcRect is transformed by the paint's matrix.
382 * Additional optional matrices can be provided by parameters.
383 *
384 * @param paint describes how to color pixels.
385 * @param dstRect the destination rect to draw.
386 * @param srcRect rect of paint coordinates to be mapped onto dstRect
387 * @param dstMatrix Optional matrix to transform dstRect. Applied before
388 * context's matrix.
389 * @param srcMatrix Optional matrix to transform srcRect Applied before
390 * paint's matrix.
391 */
392 void drawRectToRect(const GrPaint& paint,
393 const GrRect& dstRect,
394 const GrRect& srcRect,
395 const GrMatrix* dstMatrix = NULL,
396 const GrMatrix* srcMatrix = NULL);
397
398 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000399 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400 *
401 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000402 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403 * @param fill the path filling rule to use.
404 * @param translate optional additional translation applied to the
405 * path.
406 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000407 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000408 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000409
bsalomon@google.com27847de2011-02-22 20:59:41 +0000410 /**
411 * Draws vertices with a paint.
412 *
413 * @param paint describes how to color pixels.
414 * @param primitiveType primitives type to draw.
415 * @param vertexCount number of vertices.
416 * @param positions array of vertex positions, required.
417 * @param texCoords optional array of texture coordinates used
418 * to access the paint.
419 * @param colors optional array of per-vertex colors, supercedes
420 * the paint's color field.
421 * @param indices optional array of indices. If NULL vertices
422 * are drawn non-indexed.
423 * @param indexCount if indices is non-null then this is the
424 * number of indices.
425 */
426 void drawVertices(const GrPaint& paint,
427 GrPrimitiveType primitiveType,
428 int vertexCount,
429 const GrPoint positions[],
430 const GrPoint texs[],
431 const GrColor colors[],
432 const uint16_t indices[],
433 int indexCount);
434
bsalomon@google.com93c96602012-04-27 13:05:21 +0000435 /**
436 * Draws an oval.
437 *
438 * @param paint describes how to color pixels.
439 * @param rect the bounding rect of the oval.
440 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
441 * the rect is stroked based on strokeWidth. If
442 * strokeWidth == 0, then the stroke is always a single
443 * pixel thick.
444 */
445 void drawOval(const GrPaint& paint,
446 const GrRect& rect,
447 SkScalar strokeWidth);
448
bsalomon@google.com27847de2011-02-22 20:59:41 +0000449 ///////////////////////////////////////////////////////////////////////////
450 // Misc.
451
452 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000453 * Flags that affect flush() behavior.
454 */
455 enum FlushBits {
456 /**
457 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
458 * it can be rendered to directly. However, Gr lazily sets state. Simply
459 * calling setRenderTarget() followed by flush() without flags may not
460 * bind the render target. This flag forces the context to bind the last
461 * set render target in the 3D API.
462 */
463 kForceCurrentRenderTarget_FlushBit = 0x1,
464 /**
465 * A client may reach a point where it has partially rendered a frame
466 * through a GrContext that it knows the user will never see. This flag
467 * causes the flush to skip submission of deferred content to the 3D API
468 * during the flush.
469 */
470 kDiscard_FlushBit = 0x2,
471 };
472
473 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000474 * Call to ensure all drawing to the context has been issued to the
475 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000476 * @param flagsBitfield flags that control the flushing behavior. See
477 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000478 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000479 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000480
bsalomon@google.com27847de2011-02-22 20:59:41 +0000481 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000482 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000484 * current render target.
485 * @param left left edge of the rectangle to read (inclusive)
486 * @param top top edge of the rectangle to read (inclusive)
487 * @param width width of rectangle to read in pixels.
488 * @param height height of rectangle to read in pixels.
489 * @param config the pixel config of the destination buffer
490 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000491 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000492 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000493 *
494 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000495 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000496 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000497 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000498 bool readRenderTargetPixels(GrRenderTarget* target,
499 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000500 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000501 size_t rowBytes) {
502 return this->internalReadRenderTargetPixels(target, left, top,
503 width, height,
504 config, buffer,
505 rowBytes, 0);
506 }
507
508 /**
509 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
510 * at the specified rectangle.
511 * @param target the render target to write into. NULL means the
512 * current render target.
513 * @param left left edge of the rectangle to write (inclusive)
514 * @param top top edge of the rectangle to write (inclusive)
515 * @param width width of rectangle to write in pixels.
516 * @param height height of rectangle to write in pixels.
517 * @param config the pixel config of the source buffer
518 * @param buffer memory to read the rectangle from.
519 * @param rowBytes number of bytes bewtween consecutive rows. Zero
520 * means rows are tightly packed.
521 */
522 void writeRenderTargetPixels(GrRenderTarget* target,
523 int left, int top, int width, int height,
524 GrPixelConfig config, const void* buffer,
525 size_t rowBytes) {
526 this->internalWriteRenderTargetPixels(target, left, top, width, height,
527 config, buffer, rowBytes, 0);
528 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000529
530 /**
531 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000532 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000533 * @param left left edge of the rectangle to read (inclusive)
534 * @param top top edge of the rectangle to read (inclusive)
535 * @param width width of rectangle to read in pixels.
536 * @param height height of rectangle to read in pixels.
537 * @param config the pixel config of the destination buffer
538 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 * @param rowBytes number of bytes bewtween consecutive rows. Zero
540 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000541 *
542 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000543 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000544 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000545 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000546 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 GrPixelConfig config, void* buffer,
548 size_t rowBytes) {
549 return this->internalReadTexturePixels(texture, left, top,
550 width, height,
551 config, buffer, rowBytes, 0);
552 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553
554 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000555 * Writes a rectangle of pixels to a texture.
556 * @param texture the render target to read from.
557 * @param left left edge of the rectangle to write (inclusive)
558 * @param top top edge of the rectangle to write (inclusive)
559 * @param width width of rectangle to write in pixels.
560 * @param height height of rectangle to write in pixels.
561 * @param config the pixel config of the source buffer
562 * @param buffer memory to read pixels from
563 * @param rowBytes number of bytes bewtween consecutive rows. Zero
564 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000566 void writeTexturePixels(GrTexture* texture,
567 int left, int top, int width, int height,
568 GrPixelConfig config, const void* buffer,
569 size_t rowBytes) {
570 this->internalWriteTexturePixels(texture, left, top, width, height,
571 config, buffer, rowBytes, 0);
572 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000573 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000574 * Copies all texels from one texture to another.
575 * @param src the texture to copy from.
576 * @param dst the render target to copy to.
577 */
578 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000579
580 /**
581 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
582 * downsampled to the associated GrTexture (accessible via
583 * GrRenderTarget::asTexture()). Any pending draws to the render target will
584 * be executed before the resolve.
585 *
586 * This is only necessary when a client wants to access the object directly
587 * using the underlying graphics API. GrContext will detect when it must
588 * perform a resolve to a GrTexture used as the source of a draw or before
589 * reading pixels back from a GrTexture or GrRenderTarget.
590 */
591 void resolveRenderTarget(GrRenderTarget* target);
592
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000593 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000594 * Applies a 2D Gaussian blur to a given texture.
595 * @param srcTexture The source texture to be blurred.
596 * @param temp1 A scratch texture. Must not be NULL.
597 * @param temp2 A scratch texture. May be NULL, in which case
598 * srcTexture is overwritten with intermediate
599 * results.
600 * @param rect The destination rectangle.
601 * @param sigmaX The blur's standard deviation in X.
602 * @param sigmaY The blur's standard deviation in Y.
603 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000604 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000605 GrTexture* gaussianBlur(GrTexture* srcTexture,
606 GrAutoScratchTexture* temp1,
607 GrAutoScratchTexture* temp2,
608 const SkRect& rect,
609 float sigmaX, float sigmaY);
610
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000611 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000612 * This enum is used with the function below, applyMorphology.
613 */
614 enum MorphologyType {
615 kErode_MorphologyType,
616 kDilate_MorphologyType,
617 };
618
619 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000620 * Applies a 2D morphology to a given texture.
621 * @param srcTexture The source texture to be blurred.
622 * @param rect The destination rectangle.
623 * @param temp1 A scratch texture. Must not be NULL.
624 * @param temp2 A scratch texture. Must not be NULL.
625 * @param filter The morphology filter. Must be kDilate_Filter or
626 * kErode_Filter.
627 * @param radius The morphology radius in X and Y. The filter is
628 * applied to a fWidth by fHeight rectangle of
629 * pixels.
630 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000631 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000632 GrTexture* applyMorphology(GrTexture* srcTexture,
633 const GrRect& rect,
634 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000635 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000636 SkISize radius);
637
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638 ///////////////////////////////////////////////////////////////////////////
639 // Helpers
640
641 class AutoRenderTarget : ::GrNoncopyable {
642 public:
643 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
644 fContext = NULL;
645 fPrevTarget = context->getRenderTarget();
646 if (fPrevTarget != target) {
647 context->setRenderTarget(target);
648 fContext = context;
649 }
650 }
651 ~AutoRenderTarget() {
652 if (fContext) {
653 fContext->setRenderTarget(fPrevTarget);
654 }
655 }
656 private:
657 GrContext* fContext;
658 GrRenderTarget* fPrevTarget;
659 };
660
661
662 ///////////////////////////////////////////////////////////////////////////
663 // Functions intended for internal use only.
664 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000665 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000666 GrFontCache* getFontCache() { return fFontCache; }
667 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000668 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000669
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000670 /**
671 * Stencil buffers add themselves to the cache using
672 * addAndLockStencilBuffer. When a SB's RT-attachment count
673 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
674 * eligible for purging. findStencilBuffer is called to check the cache for
675 * a SB that matching an RT's criteria. If a match is found that has been
676 * unlocked (its attachment count has reached 0) then it will be relocked.
677 */
678 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
679 void unlockStencilBuffer(GrResourceEntry* sbEntry);
680 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000681
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000682 /*
683 * postClipPush acts as a hint to this and lower-level classes (e.g.,
684 * GrGpu) that the clip stack has changed.
685 */
686 virtual void postClipPush();
687
688 /*
689 * preClipPop acts as a hint that the clip stack has been restored to an
690 * earlier state.
691 */
692 virtual void preClipPop();
693
robertphillips@google.com2c756812012-05-22 20:28:23 +0000694 GrPathRenderer* getPathRenderer(const SkPath& path,
695 GrPathFill fill,
696 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000697 bool antiAlias,
698 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000699
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;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000715 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000716
717 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
718 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
719 GrInOrderDrawBuffer* fDrawBuffer;
720
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000721 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000722
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000723 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000724
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000725 void setupDrawBuffer();
726
bsalomon@google.com27847de2011-02-22 20:59:41 +0000727 void flushDrawBuffer();
728
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000729 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000730
bsalomon@google.com27847de2011-02-22 20:59:41 +0000731 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
732
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000733 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000734 GrPathFill fill, const GrPoint* translate);
735
bsalomon@google.com6f379512011-11-16 20:36:03 +0000736 /**
737 * Flags to the internal read/write pixels funcs
738 */
739 enum PixelOpsFlags {
740 kDontFlush_PixelOpsFlag = 0x1,
741 };
742
743 bool internalReadRenderTargetPixels(GrRenderTarget* target,
744 int left, int top,
745 int width, int height,
746 GrPixelConfig config, void* buffer,
747 size_t rowBytes, uint32_t flags);
748
749 void internalWriteRenderTargetPixels(GrRenderTarget* target,
750 int left, int top,
751 int width, int height,
752 GrPixelConfig, const void* buffer,
753 size_t rowBytes, uint32_t flags);
754
755 bool internalReadTexturePixels(GrTexture* texture,
756 int left, int top,
757 int width, int height,
758 GrPixelConfig config, void* buffer,
759 size_t rowBytes, uint32_t flags);
760
761 void internalWriteTexturePixels(GrTexture* texture,
762 int left, int top,
763 int width, int height,
764 GrPixelConfig config, const void* buffer,
765 size_t rowBytes, uint32_t flags);
766 // needed for access to internalWriteTexturePixels. TODO: make GrContext
767 // be a facade for an internal class. Then functions that are public on the
768 // internal class would have only be callable in src/gpu. The facade would
769 // only have to functions necessary for clients.
770 friend class GrAtlas;
771
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000772 // computes vertex layout bits based on the paint. If paint expresses
773 // a texture for a stage, the stage coords will be bound to postitions
774 // unless hasTexCoords[s]==true in which case stage s's input coords
775 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
776 // for an array where all the values are false.
777 static int PaintStageVertexLayoutBits(
778 const GrPaint& paint,
779 const bool hasTexCoords[GrPaint::kTotalStages]);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000780
781 // Needed so GrTexture's returnToCache helper function can call
782 // addExistingTextureToCache
783 friend class GrTexture;
784
785 // Add an existing texture to the texture cache. This is intended solely
786 // for use with textures released from an GrAutoScratchTexture.
787 void addExistingTextureToCache(GrTexture* texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788};
789
790/**
791 * Save/restore the view-matrix in the context.
792 */
793class GrAutoMatrix : GrNoncopyable {
794public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000795 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
797 fMatrix = ctx->getMatrix();
798 }
799 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
800 fMatrix = ctx->getMatrix();
801 ctx->setMatrix(matrix);
802 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000803 void set(GrContext* ctx) {
804 if (NULL != fContext) {
805 fContext->setMatrix(fMatrix);
806 }
807 fMatrix = ctx->getMatrix();
808 fContext = ctx;
809 }
810 void set(GrContext* ctx, const GrMatrix& matrix) {
811 if (NULL != fContext) {
812 fContext->setMatrix(fMatrix);
813 }
814 fMatrix = ctx->getMatrix();
815 ctx->setMatrix(matrix);
816 fContext = ctx;
817 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000819 if (NULL != fContext) {
820 fContext->setMatrix(fMatrix);
821 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000822 }
823
824private:
825 GrContext* fContext;
826 GrMatrix fMatrix;
827};
828
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000829/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000830 * Gets and locks a scratch texture from a descriptor using
831 * either exact or approximate criteria. Unlocks texture in
832 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000833 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000834class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000836 GrAutoScratchTexture()
837 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000839
840 GrAutoScratchTexture(GrContext* context,
841 const GrTextureDesc& desc,
842 GrContext::ScratchTexMatch match =
843 GrContext::kApprox_ScratchTexMatch)
844 : fContext(NULL) {
845 this->set(context, desc, match);
846 }
847
848 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000849 this->reset();
850 }
851
852 void reset() {
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000853 if (NULL != fContext && NULL != fEntry.cacheEntry()) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000854 fContext->unlockTexture(fEntry);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000855 fEntry.reset();
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000856 }
857 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000858
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000859 /*
860 * When detaching a texture we do not unlock it in the texture cache but
861 * we do set the returnToCache flag. In this way the texture remains
862 * "locked" in the texture cache until it is freed and recycled in
863 * GrTexture::internal_dispose. In reality, the texture has been removed
864 * from the cache (because this is in AutoScratchTexture) and by not
865 * calling unlockTexture we simply don't re-add it. It will be reattached
866 * in GrTexture::internal_dispose.
867 *
868 * Note that the caller is assumed to accept and manage the ref to the
869 * returned texture.
870 */
871 GrTexture* detach() {
872 GrTexture* temp = this->texture();
873
874 GrAssert(1 == temp->getRefCnt());
875
876 // freeEntry will remove the texture cache's ref
877 temp->ref();
878 fContext->freeEntry(fEntry);
879 fEntry.reset();
880
881 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
882 GrAssert(1 == temp->getRefCnt());
883 return temp;
884 }
885
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000886 GrTexture* set(GrContext* context,
887 const GrTextureDesc& desc,
888 GrContext::ScratchTexMatch match =
889 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000890 this->reset();
891
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000892 fContext = context;
893 if (NULL != fContext) {
894 fEntry = fContext->lockScratchTexture(desc, match);
895 GrTexture* ret = fEntry.texture();
896 if (NULL == ret) {
897 fContext = NULL;
898 }
899 return ret;
900 } else {
901 return NULL;
902 }
903 }
904
905 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000906private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000907 GrContext* fContext;
908 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000909};
910
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911#endif