blob: 2f05458c443ba28f2517d75d1edc98d19a92ae7c [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:
reed@google.comfa35e3d2012-06-26 20:16:17 +000039 SK_DECLARE_INST_COUNT(GrContext)
40
bsalomon@google.com27847de2011-02-22 20:59:41 +000041 /**
42 * Creates a GrContext from within a 3D context.
43 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000044 static GrContext* Create(GrEngine engine,
45 GrPlatform3DContext context3D);
bsalomon@google.com27847de2011-02-22 20:59:41 +000046
bsalomon@google.comc0af3172012-06-15 14:10:09 +000047 /**
48 * Returns the number of GrContext instances for the current thread.
49 */
50 static int GetThreadInstanceCount();
51
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 virtual ~GrContext();
53
54 /**
55 * The GrContext normally assumes that no outsider is setting state
56 * within the underlying 3D API's context/device/whatever. This call informs
57 * the context that the state was modified and it should resend. Shouldn't
58 * be called frequently for good performance.
59 */
60 void resetContext();
61
bsalomon@google.com8fe72472011-03-30 21:26:44 +000062 /**
63 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
64 * if you have lost the associated GPU context, and thus internal texture,
65 * buffer, etc. references/IDs are now invalid. Should be called even when
66 * GrContext is no longer going to be used for two reasons:
67 * 1) ~GrContext will not try to free the objects in the 3D API.
68 * 2) If you've created GrResources that outlive the GrContext they will
69 * be marked as invalid (GrResource::isValid()) and won't attempt to
70 * free their underlying resource in the 3D API.
71 * Content drawn since the last GrContext::flush() may be lost.
72 */
73 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000074
75 /**
junov@google.com53a55842011-06-08 22:55:10 +000076 * Similar to contextLost, but makes no attempt to reset state.
77 * Use this method when GrContext destruction is pending, but
78 * the graphics context is destroyed first.
79 */
80 void contextDestroyed();
81
82 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 * Frees gpu created by the context. Can be called to reduce GPU memory
84 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000085 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 void freeGpuResources();
87
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000088 /**
89 * Returns the number of bytes of GPU memory hosted by the texture cache.
90 */
91 size_t getGpuTextureCacheBytes() const;
92
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 ///////////////////////////////////////////////////////////////////////////
94 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000095
96 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000097 * Token that refers to an entry in the texture cache. Returned by
98 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +0000100 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000101 public:
102 TextureCacheEntry() : fEntry(NULL) {}
103 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
104 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
105 fEntry = e.fEntry;
106 return *this;
107 }
108 GrTexture* texture() const;
109 void reset() { fEntry = NULL; }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000110 GrResourceEntry* cacheEntry() { return fEntry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000111 private:
112 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
113 void set(GrResourceEntry* entry) { fEntry = entry; }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000114 GrResourceEntry* fEntry;
115 friend class GrContext;
116 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000117
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000118 /**
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 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000122 * @param params The tex params used to draw a texture may help determine
123 * the cache entry used. (e.g. different versions may exist
124 * for different wrap modes on GPUs with limited NPOT
125 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000126 * @param desc Description of the texture properties.
127 * @param srcData Pointer to the pixel values.
128 * @param rowBytes The number of bytes between rows of the texture. Zero
129 * implies tightly packed rows.
130 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000131 TextureCacheEntry createAndLockTexture(const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000132 const GrTextureDesc& desc,
133 void* srcData, size_t rowBytes);
134
135 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000136 * Search for an entry based on key and dimensions. If found, "lock" it and
137 * return it. The entry's texture() function will return NULL if not found.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000138 * Must be balanced with an unlockTexture() call.
139 *
bsalomon@google.comb8670992012-07-25 21:27:09 +0000140 * @param desc Description of the texture properties.
141 * @param params The tex params used to draw a texture may help determine
142 * the cache entry used. (e.g. different versions may exist
143 * for different wrap modes on GPUs with limited NPOT
144 * texture support). NULL implies clamp wrap modes.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000145 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000146 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000147 const GrTextureParams* params);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000148 /**
149 * Determines whether a texture is in the cache. If the texture is found it
150 * will not be locked or returned. This call does not affect the priority of
151 * the texture for deletion.
152 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000153 bool isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000154 const GrTextureParams* params) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000155
156 /**
157 * Enum that determines how closely a returned scratch texture must match
158 * a provided GrTextureDesc.
159 */
160 enum ScratchTexMatch {
161 /**
162 * Finds a texture that exactly matches the descriptor.
163 */
164 kExact_ScratchTexMatch,
165 /**
166 * Finds a texture that approximately matches the descriptor. Will be
167 * at least as large in width and height as desc specifies. If desc
168 * specifies that texture is a render target then result will be a
169 * render target. If desc specifies a render target and doesn't set the
170 * no stencil flag then result will have a stencil. Format and aa level
171 * will always match.
172 */
173 kApprox_ScratchTexMatch
174 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000175
176 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000177 * Returns a texture matching the desc. It's contents are unknown. Subsequent
178 * requests with the same descriptor are not guaranteed to return the same
179 * texture. The same texture is guaranteed not be returned again until it is
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000180 * unlocked. Call must be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000181 *
182 * Textures created by createAndLockTexture() hide the complications of
183 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000184 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000185 * such an API will create gaps in the tiling pattern. This includes clamp
186 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000187 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000188 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
189 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000190
191 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000192 * When done with an entry, call unlockTexture(entry) on it, which returns
193 * it to the cache, where it may be purged.
194 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000196
197 /**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000198 * Free any data associated with the provided entry in the texture cache
199 */
200 void freeEntry(TextureCacheEntry entry);
201
202 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000203 * Creates a texture that is outside the cache. Does not count against
204 * cache's budget.
205 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000206 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207 void* srcData,
208 size_t rowBytes);
209
210 /**
bsalomon@google.comb8670992012-07-25 21:27:09 +0000211 * Returns true if the specified use of an indexed texture is supported.
212 * Support may depend upon whether the texture params indicate that the
213 * texture will be tiled. Passing NULL for the texture params indicates
214 * clamp mode.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000215 */
bsalomon@google.comb8670992012-07-25 21:27:09 +0000216 bool supportsIndex8PixelConfig(const GrTextureParams*,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000217 int width,
218 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219
220 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000221 * Return the current texture cache limits.
222 *
223 * @param maxTextures If non-null, returns maximum number of textures that
224 * can be held in the cache.
225 * @param maxTextureBytes If non-null, returns maximum number of bytes of
226 * texture memory that can be held in the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000227 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000228 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229
230 /**
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000231 * Specify the texture cache limits. If the current cache exceeds either
232 * of these, it will be purged (LRU) to keep the cache within these limits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000233 *
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000234 * @param maxTextures The maximum number of textures that can be held in
235 * the cache.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000236 * @param maxTextureBytes The maximum number of bytes of texture memory
237 * that can be held in the cache.
238 */
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000239 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000240
241 /**
242 * Return the max width or height of a texture supported by the current gpu
243 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000244 int getMaxTextureSize() const;
245
246 /**
247 * Return the max width or height of a render target supported by the
248 * current gpu
249 */
250 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000251
252 ///////////////////////////////////////////////////////////////////////////
253 // Render targets
254
255 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000256 * Sets the render target.
257 * @param target the render target to set. (should not be NULL.)
258 */
259 void setRenderTarget(GrRenderTarget* target);
260
261 /**
262 * Gets the current render target.
263 * @return the currently bound render target. Should never be NULL.
264 */
265 const GrRenderTarget* getRenderTarget() const;
266 GrRenderTarget* getRenderTarget();
267
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000268 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
269
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000270 /**
271 * Can the provided configuration act as a color render target?
272 */
273 bool isConfigRenderable(GrPixelConfig config) const;
274
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000275 ///////////////////////////////////////////////////////////////////////////
276 // Platform Surfaces
277
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000278 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000279 * Wraps an existing texture with a GrTexture object.
280 *
281 * OpenGL: if the object is a texture Gr may change its GL texture params
282 * when it is drawn.
283 *
284 * @param desc description of the object to create.
285 *
286 * @return GrTexture object or NULL on failure.
287 */
288 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
289
290 /**
291 * Wraps an existing render target with a GrRenderTarget object. It is
292 * similar to createPlatformTexture but can be used to draw into surfaces
293 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
294 * the client will resolve to a texture).
295 *
296 * @param desc description of the object to create.
297 *
298 * @return GrTexture object or NULL on failure.
299 */
300 GrRenderTarget* createPlatformRenderTarget(
301 const GrPlatformRenderTargetDesc& desc);
302
bsalomon@google.com27847de2011-02-22 20:59:41 +0000303 ///////////////////////////////////////////////////////////////////////////
304 // Matrix state
305
306 /**
307 * Gets the current transformation matrix.
308 * @return the current matrix.
309 */
310 const GrMatrix& getMatrix() const;
311
312 /**
313 * Sets the transformation matrix.
314 * @param m the matrix to set.
315 */
316 void setMatrix(const GrMatrix& m);
317
318 /**
319 * Concats the current matrix. The passed matrix is applied before the
320 * current matrix.
321 * @param m the matrix to concat.
322 */
323 void concatMatrix(const GrMatrix& m) const;
324
325
326 ///////////////////////////////////////////////////////////////////////////
327 // Clip state
328 /**
329 * Gets the current clip.
330 * @return the current clip.
331 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000332 const GrClipData* getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000333
334 /**
335 * Sets the clip.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000336 * @param clipData the clip to set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000337 */
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000338 void setClip(const GrClipData* clipData);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000339
bsalomon@google.com27847de2011-02-22 20:59:41 +0000340 ///////////////////////////////////////////////////////////////////////////
341 // Draws
342
343 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000344 * Clear the entire or rect of the render target, ignoring any clips.
345 * @param rect the rect to clear or the whole thing if rect is NULL.
346 * @param color the color to clear to.
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000347 * @param target if non-NULL, the render target to clear otherwise clear
348 * the current render target
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349 */
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000350 void clear(const GrIRect* rect, GrColor color,
351 GrRenderTarget* target = NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000352
353 /**
354 * Draw everywhere (respecting the clip) with the paint.
355 */
356 void drawPaint(const GrPaint& paint);
357
358 /**
359 * Draw the rect using a paint.
360 * @param paint describes how to color pixels.
361 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
362 * the rect is mitered stroked based on strokeWidth. If
363 * strokeWidth == 0, then the stroke is always a single
364 * pixel thick.
365 * @param matrix Optional matrix applied to the rect. Applied before
366 * context's matrix or the paint's matrix.
367 * The rects coords are used to access the paint (through texture matrix)
368 */
369 void drawRect(const GrPaint& paint,
370 const GrRect&,
371 GrScalar strokeWidth = -1,
372 const GrMatrix* matrix = NULL);
373
374 /**
375 * Maps a rect of paint coordinates onto the a rect of destination
376 * coordinates. Each rect can optionally be transformed. The srcRect
377 * is stretched over the dstRect. The dstRect is transformed by the
378 * context's matrix and the srcRect is transformed by the paint's matrix.
379 * Additional optional matrices can be provided by parameters.
380 *
381 * @param paint describes how to color pixels.
382 * @param dstRect the destination rect to draw.
383 * @param srcRect rect of paint coordinates to be mapped onto dstRect
384 * @param dstMatrix Optional matrix to transform dstRect. Applied before
385 * context's matrix.
386 * @param srcMatrix Optional matrix to transform srcRect Applied before
387 * paint's matrix.
388 */
389 void drawRectToRect(const GrPaint& paint,
390 const GrRect& dstRect,
391 const GrRect& srcRect,
392 const GrMatrix* dstMatrix = NULL,
393 const GrMatrix* srcMatrix = NULL);
394
395 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000396 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 *
398 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000399 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400 * @param fill the path filling rule to use.
401 * @param translate optional additional translation applied to the
402 * path.
403 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000404 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000405 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000406
bsalomon@google.com27847de2011-02-22 20:59:41 +0000407 /**
408 * Draws vertices with a paint.
409 *
410 * @param paint describes how to color pixels.
411 * @param primitiveType primitives type to draw.
412 * @param vertexCount number of vertices.
413 * @param positions array of vertex positions, required.
414 * @param texCoords optional array of texture coordinates used
415 * to access the paint.
416 * @param colors optional array of per-vertex colors, supercedes
417 * the paint's color field.
418 * @param indices optional array of indices. If NULL vertices
419 * are drawn non-indexed.
420 * @param indexCount if indices is non-null then this is the
421 * number of indices.
422 */
423 void drawVertices(const GrPaint& paint,
424 GrPrimitiveType primitiveType,
425 int vertexCount,
426 const GrPoint positions[],
427 const GrPoint texs[],
428 const GrColor colors[],
429 const uint16_t indices[],
430 int indexCount);
431
bsalomon@google.com93c96602012-04-27 13:05:21 +0000432 /**
433 * Draws an oval.
434 *
435 * @param paint describes how to color pixels.
436 * @param rect the bounding rect of the oval.
437 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
438 * the rect is stroked based on strokeWidth. If
439 * strokeWidth == 0, then the stroke is always a single
440 * pixel thick.
441 */
442 void drawOval(const GrPaint& paint,
443 const GrRect& rect,
444 SkScalar strokeWidth);
445
bsalomon@google.com27847de2011-02-22 20:59:41 +0000446 ///////////////////////////////////////////////////////////////////////////
447 // Misc.
448
449 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450 * Flags that affect flush() behavior.
451 */
452 enum FlushBits {
453 /**
454 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
455 * it can be rendered to directly. However, Gr lazily sets state. Simply
456 * calling setRenderTarget() followed by flush() without flags may not
457 * bind the render target. This flag forces the context to bind the last
458 * set render target in the 3D API.
459 */
460 kForceCurrentRenderTarget_FlushBit = 0x1,
461 /**
462 * A client may reach a point where it has partially rendered a frame
463 * through a GrContext that it knows the user will never see. This flag
464 * causes the flush to skip submission of deferred content to the 3D API
465 * during the flush.
466 */
467 kDiscard_FlushBit = 0x2,
468 };
469
470 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000471 * Call to ensure all drawing to the context has been issued to the
472 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000473 * @param flagsBitfield flags that control the flushing behavior. See
474 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000475 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000476 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000477
bsalomon@google.com27847de2011-02-22 20:59:41 +0000478 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000479 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000480 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481 * current render target.
482 * @param left left edge of the rectangle to read (inclusive)
483 * @param top top edge of the rectangle to read (inclusive)
484 * @param width width of rectangle to read in pixels.
485 * @param height height of rectangle to read in pixels.
486 * @param config the pixel config of the destination buffer
487 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000488 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000489 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000490 *
491 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000492 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000493 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000494 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000495 bool readRenderTargetPixels(GrRenderTarget* target,
496 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000497 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000498 size_t rowBytes) {
499 return this->internalReadRenderTargetPixels(target, left, top,
500 width, height,
501 config, buffer,
502 rowBytes, 0);
503 }
504
505 /**
506 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
507 * at the specified rectangle.
508 * @param target the render target to write into. NULL means the
509 * current render target.
510 * @param left left edge of the rectangle to write (inclusive)
511 * @param top top edge of the rectangle to write (inclusive)
512 * @param width width of rectangle to write in pixels.
513 * @param height height of rectangle to write in pixels.
514 * @param config the pixel config of the source buffer
515 * @param buffer memory to read the rectangle from.
516 * @param rowBytes number of bytes bewtween consecutive rows. Zero
517 * means rows are tightly packed.
518 */
519 void writeRenderTargetPixels(GrRenderTarget* target,
520 int left, int top, int width, int height,
521 GrPixelConfig config, const void* buffer,
522 size_t rowBytes) {
523 this->internalWriteRenderTargetPixels(target, left, top, width, height,
524 config, buffer, rowBytes, 0);
525 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000526
527 /**
528 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000529 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000530 * @param left left edge of the rectangle to read (inclusive)
531 * @param top top edge of the rectangle to read (inclusive)
532 * @param width width of rectangle to read in pixels.
533 * @param height height of rectangle to read in pixels.
534 * @param config the pixel config of the destination buffer
535 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000536 * @param rowBytes number of bytes bewtween consecutive rows. Zero
537 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000538 *
539 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000540 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000541 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000542 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544 GrPixelConfig config, void* buffer,
545 size_t rowBytes) {
546 return this->internalReadTexturePixels(texture, left, top,
547 width, height,
548 config, buffer, rowBytes, 0);
549 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550
551 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000552 * Writes a rectangle of pixels to a texture.
553 * @param texture the render target to read from.
554 * @param left left edge of the rectangle to write (inclusive)
555 * @param top top edge of the rectangle to write (inclusive)
556 * @param width width of rectangle to write in pixels.
557 * @param height height of rectangle to write in pixels.
558 * @param config the pixel config of the source buffer
559 * @param buffer memory to read pixels from
560 * @param rowBytes number of bytes bewtween consecutive rows. Zero
561 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000562 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000563 void writeTexturePixels(GrTexture* texture,
564 int left, int top, int width, int height,
565 GrPixelConfig config, const void* buffer,
566 size_t rowBytes) {
567 this->internalWriteTexturePixels(texture, left, top, width, height,
568 config, buffer, rowBytes, 0);
569 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000570 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000571 * Copies all texels from one texture to another.
572 * @param src the texture to copy from.
573 * @param dst the render target to copy to.
574 */
575 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000576
577 /**
578 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
579 * downsampled to the associated GrTexture (accessible via
580 * GrRenderTarget::asTexture()). Any pending draws to the render target will
581 * be executed before the resolve.
582 *
583 * This is only necessary when a client wants to access the object directly
584 * using the underlying graphics API. GrContext will detect when it must
585 * perform a resolve to a GrTexture used as the source of a draw or before
586 * reading pixels back from a GrTexture or GrRenderTarget.
587 */
588 void resolveRenderTarget(GrRenderTarget* target);
589
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000590 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000591 * Applies a 2D Gaussian blur to a given texture.
592 * @param srcTexture The source texture to be blurred.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000593 * @param canClobberSrc If true, srcTexture may be overwritten, and
594 * may be returned as the result.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000595 * @param rect The destination rectangle.
596 * @param sigmaX The blur's standard deviation in X.
597 * @param sigmaY The blur's standard deviation in Y.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000598 * @return the blurred texture, which may be srcTexture ref'ed, or a
599 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000600 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000601 GrTexture* gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000602 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000603 const SkRect& rect,
604 float sigmaX, float sigmaY);
605
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000606 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000607 * This enum is used with the function below, applyMorphology.
608 */
609 enum MorphologyType {
610 kErode_MorphologyType,
611 kDilate_MorphologyType,
612 };
613
614 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000615 * Applies a 2D morphology to a given texture.
616 * @param srcTexture The source texture to be blurred.
617 * @param rect The destination rectangle.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000618 * @param filter The morphology filter. Must be kDilate_Filter or
619 * kErode_Filter.
620 * @param radius The morphology radius in X and Y. The filter is
621 * applied to a fWidth by fHeight rectangle of
622 * pixels.
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000623 * @return the morphed texture, which may be srcTexture ref'ed, or a
624 * new texture. It is the caller's responsibility to unref this texture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000625 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000626 GrTexture* applyMorphology(GrTexture* srcTexture,
627 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000628 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000629 SkISize radius);
630
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631 ///////////////////////////////////////////////////////////////////////////
632 // Helpers
633
634 class AutoRenderTarget : ::GrNoncopyable {
635 public:
636 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
637 fContext = NULL;
638 fPrevTarget = context->getRenderTarget();
639 if (fPrevTarget != target) {
640 context->setRenderTarget(target);
641 fContext = context;
642 }
643 }
644 ~AutoRenderTarget() {
645 if (fContext) {
646 fContext->setRenderTarget(fPrevTarget);
647 }
648 }
649 private:
650 GrContext* fContext;
651 GrRenderTarget* fPrevTarget;
652 };
653
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000654 /**
655 * Save/restore the view-matrix in the context.
656 */
657 class AutoMatrix : GrNoncopyable {
658 public:
659 AutoMatrix() : fContext(NULL) {}
660 AutoMatrix(GrContext* ctx) : fContext(ctx) {
661 fMatrix = ctx->getMatrix();
662 }
663 AutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
664 fMatrix = ctx->getMatrix();
665 ctx->setMatrix(matrix);
666 }
667 void set(GrContext* ctx) {
668 if (NULL != fContext) {
669 fContext->setMatrix(fMatrix);
670 }
671 fMatrix = ctx->getMatrix();
672 fContext = ctx;
673 }
674 void set(GrContext* ctx, const GrMatrix& matrix) {
675 if (NULL != fContext) {
676 fContext->setMatrix(fMatrix);
677 }
678 fMatrix = ctx->getMatrix();
679 ctx->setMatrix(matrix);
680 fContext = ctx;
681 }
682 ~AutoMatrix() {
683 if (NULL != fContext) {
684 fContext->setMatrix(fMatrix);
685 }
686 }
687
688 private:
689 GrContext* fContext;
690 GrMatrix fMatrix;
691 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000693 class AutoClip : GrNoncopyable {
694 public:
695 AutoClip(GrContext* context, const GrRect& newClipRect)
696 : fContext(context)
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000697 , fNewClipStack(newClipRect) {
698 fNewClipData.fClipStack = &fNewClipStack;
699
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000700 fOldClip = fContext->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000701 fContext->setClip(&fNewClipData);
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000702 }
703
704 ~AutoClip() {
705 if (NULL != fContext) {
706 fContext->setClip(fOldClip);
707 }
708 }
709 private:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000710 GrContext* fContext;
711 const GrClipData* fOldClip;
712
713 GrClip fNewClipStack;
714 GrClipData fNewClipData;
robertphillips@google.com56c79b12012-07-11 20:57:46 +0000715 };
716
bsalomon@google.com27847de2011-02-22 20:59:41 +0000717 ///////////////////////////////////////////////////////////////////////////
718 // Functions intended for internal use only.
719 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000720 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721 GrFontCache* getFontCache() { return fFontCache; }
722 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000723 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000724
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000725 /**
726 * Stencil buffers add themselves to the cache using
727 * addAndLockStencilBuffer. When a SB's RT-attachment count
728 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
729 * eligible for purging. findStencilBuffer is called to check the cache for
730 * a SB that matching an RT's criteria. If a match is found that has been
731 * unlocked (its attachment count has reached 0) then it will be relocked.
732 */
733 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
734 void unlockStencilBuffer(GrResourceEntry* sbEntry);
735 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000736
robertphillips@google.com2c756812012-05-22 20:28:23 +0000737 GrPathRenderer* getPathRenderer(const SkPath& path,
738 GrPathFill fill,
739 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000740 bool antiAlias,
741 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000742
bsalomon@google.com27847de2011-02-22 20:59:41 +0000743private:
744 // used to keep track of when we need to flush the draw buffer
745 enum DrawCategory {
746 kBuffered_DrawCategory, // last draw was inserted in draw buffer
747 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748 };
749 DrawCategory fLastDrawCategory;
750
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000751 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000752 GrDrawState* fDrawState;
753
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000754 GrResourceCache* fTextureCache;
755 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000756
bsalomon@google.com30085192011-08-19 15:42:31 +0000757 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000758 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000759
760 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
761 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
762 GrInOrderDrawBuffer* fDrawBuffer;
763
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000764 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000765
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000766 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000768 void setupDrawBuffer();
769
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770 void flushDrawBuffer();
771
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000772 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773
bsalomon@google.com27847de2011-02-22 20:59:41 +0000774 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
775
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000776 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000777 GrPathFill fill, const GrPoint* translate);
778
bsalomon@google.com6f379512011-11-16 20:36:03 +0000779 /**
780 * Flags to the internal read/write pixels funcs
781 */
782 enum PixelOpsFlags {
783 kDontFlush_PixelOpsFlag = 0x1,
784 };
785
786 bool internalReadRenderTargetPixels(GrRenderTarget* target,
787 int left, int top,
788 int width, int height,
789 GrPixelConfig config, void* buffer,
790 size_t rowBytes, uint32_t flags);
791
792 void internalWriteRenderTargetPixels(GrRenderTarget* target,
793 int left, int top,
794 int width, int height,
795 GrPixelConfig, const void* buffer,
796 size_t rowBytes, uint32_t flags);
797
798 bool internalReadTexturePixels(GrTexture* texture,
799 int left, int top,
800 int width, int height,
801 GrPixelConfig config, void* buffer,
802 size_t rowBytes, uint32_t flags);
803
804 void internalWriteTexturePixels(GrTexture* texture,
805 int left, int top,
806 int width, int height,
807 GrPixelConfig config, const void* buffer,
808 size_t rowBytes, uint32_t flags);
809 // needed for access to internalWriteTexturePixels. TODO: make GrContext
810 // be a facade for an internal class. Then functions that are public on the
811 // internal class would have only be callable in src/gpu. The facade would
812 // only have to functions necessary for clients.
813 friend class GrAtlas;
814
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000815 // Needed so GrTexture's returnToCache helper function can call
816 // addExistingTextureToCache
817 friend class GrTexture;
818
819 // Add an existing texture to the texture cache. This is intended solely
820 // for use with textures released from an GrAutoScratchTexture.
821 void addExistingTextureToCache(GrTexture* texture);
reed@google.comfa35e3d2012-06-26 20:16:17 +0000822
823 typedef GrRefCnt INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000824};
825
826/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000827 * Gets and locks a scratch texture from a descriptor using
828 * either exact or approximate criteria. Unlocks texture in
829 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000830 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000831class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000833 GrAutoScratchTexture()
834 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000836
837 GrAutoScratchTexture(GrContext* context,
838 const GrTextureDesc& desc,
839 GrContext::ScratchTexMatch match =
840 GrContext::kApprox_ScratchTexMatch)
841 : fContext(NULL) {
842 this->set(context, desc, match);
843 }
844
845 ~GrAutoScratchTexture() {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000846 this->reset();
847 }
848
849 void reset() {
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000850 if (NULL != fContext && NULL != fEntry.cacheEntry()) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000851 fContext->unlockTexture(fEntry);
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000852 fEntry.reset();
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000853 }
854 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000855
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000856 /*
857 * When detaching a texture we do not unlock it in the texture cache but
858 * we do set the returnToCache flag. In this way the texture remains
859 * "locked" in the texture cache until it is freed and recycled in
860 * GrTexture::internal_dispose. In reality, the texture has been removed
861 * from the cache (because this is in AutoScratchTexture) and by not
862 * calling unlockTexture we simply don't re-add it. It will be reattached
863 * in GrTexture::internal_dispose.
864 *
865 * Note that the caller is assumed to accept and manage the ref to the
866 * returned texture.
867 */
868 GrTexture* detach() {
869 GrTexture* temp = this->texture();
870
871 GrAssert(1 == temp->getRefCnt());
872
873 // freeEntry will remove the texture cache's ref
874 temp->ref();
875 fContext->freeEntry(fEntry);
876 fEntry.reset();
877
878 temp->setFlag((GrTextureFlags) GrTexture::kReturnToCache_FlagBit);
879 GrAssert(1 == temp->getRefCnt());
880 return temp;
881 }
882
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000883 GrTexture* set(GrContext* context,
884 const GrTextureDesc& desc,
885 GrContext::ScratchTexMatch match =
886 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000887 this->reset();
888
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000889 fContext = context;
890 if (NULL != fContext) {
891 fEntry = fContext->lockScratchTexture(desc, match);
892 GrTexture* ret = fEntry.texture();
893 if (NULL == ret) {
894 fContext = NULL;
895 }
896 return ret;
897 } else {
898 return NULL;
899 }
900 }
901
902 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000903private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000904 GrContext* fContext;
905 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000906};
907
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908#endif