blob: 9389499ec4130a106238847d9c0460933aa8e2f6 [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; }
103 private:
104 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
105 void set(GrResourceEntry* entry) { fEntry = entry; }
106 GrResourceEntry* cacheEntry() { return fEntry; }
107 GrResourceEntry* fEntry;
108 friend class GrContext;
109 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000111 /**
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
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000177 * unlocked. Must call 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 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000195 * Creates a texture that is outside the cache. Does not count against
196 * cache's budget.
197 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000198 GrTexture* createUncachedTexture(const GrTextureDesc& desc,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000199 void* srcData,
200 size_t rowBytes);
201
202 /**
203 * Returns true if the specified use of an indexed texture is supported.
204 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000205 bool supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000206 int width,
207 int height) const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000208
209 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000210 * Return the current texture cache budget in bytes.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000212 size_t getTextureCacheBudget() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213
214 /**
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000215 * Specify the texture cache budget. If the current cache size exceeds the
216 * budget it will immediately be purged to be within the budget.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000217 *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218 * @param maxTextureBytes The maximum number of bytes of texture memory
219 * that can be held in the cache.
220 */
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000221 void setTextureCacheBudget(size_t maxTextureBytes);
222 // DEPRECATED, this will be deleted soon.
223 void setTextureCacheLimits(int ignored, size_t maxTextureBytes) {
224 this->setTextureCacheBudget(maxTextureBytes);
225 }
226
227 /**
228 * Returns the current number of bytes of GPU memory hosted by the texture
229 * cache.
230 */
231 size_t getGpuTextureCacheBytes() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000232
233 /**
234 * Return the max width or height of a texture supported by the current gpu
235 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000236 int getMaxTextureSize() const;
237
238 /**
239 * Return the max width or height of a render target supported by the
240 * current gpu
241 */
242 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000243
244 ///////////////////////////////////////////////////////////////////////////
245 // Render targets
246
247 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000248 * Sets the render target.
249 * @param target the render target to set. (should not be NULL.)
250 */
251 void setRenderTarget(GrRenderTarget* target);
252
253 /**
254 * Gets the current render target.
255 * @return the currently bound render target. Should never be NULL.
256 */
257 const GrRenderTarget* getRenderTarget() const;
258 GrRenderTarget* getRenderTarget();
259
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000260 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
261
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000262 /**
263 * Can the provided configuration act as a color render target?
264 */
265 bool isConfigRenderable(GrPixelConfig config) const;
266
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000267 ///////////////////////////////////////////////////////////////////////////
268 // Platform Surfaces
269
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000270 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000271 * Wraps an existing texture with a GrTexture object.
272 *
273 * OpenGL: if the object is a texture Gr may change its GL texture params
274 * when it is drawn.
275 *
276 * @param desc description of the object to create.
277 *
278 * @return GrTexture object or NULL on failure.
279 */
280 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
281
282 /**
283 * Wraps an existing render target with a GrRenderTarget object. It is
284 * similar to createPlatformTexture but can be used to draw into surfaces
285 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
286 * the client will resolve to a texture).
287 *
288 * @param desc description of the object to create.
289 *
290 * @return GrTexture object or NULL on failure.
291 */
292 GrRenderTarget* createPlatformRenderTarget(
293 const GrPlatformRenderTargetDesc& desc);
294
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295 ///////////////////////////////////////////////////////////////////////////
296 // Matrix state
297
298 /**
299 * Gets the current transformation matrix.
300 * @return the current matrix.
301 */
302 const GrMatrix& getMatrix() const;
303
304 /**
305 * Sets the transformation matrix.
306 * @param m the matrix to set.
307 */
308 void setMatrix(const GrMatrix& m);
309
310 /**
311 * Concats the current matrix. The passed matrix is applied before the
312 * current matrix.
313 * @param m the matrix to concat.
314 */
315 void concatMatrix(const GrMatrix& m) const;
316
317
318 ///////////////////////////////////////////////////////////////////////////
319 // Clip state
320 /**
321 * Gets the current clip.
322 * @return the current clip.
323 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000324 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000325
326 /**
327 * Sets the clip.
328 * @param clip the clip to set.
329 */
330 void setClip(const GrClip& clip);
331
332 /**
333 * Convenience method for setting the clip to a rect.
334 * @param rect the rect to set as the new clip.
335 */
336 void setClip(const GrIRect& rect);
337
338 ///////////////////////////////////////////////////////////////////////////
339 // Draws
340
341 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000342 * Clear the entire or rect of the render target, ignoring any clips.
343 * @param rect the rect to clear or the whole thing if rect is NULL.
344 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000345 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000346 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000347
348 /**
349 * Draw everywhere (respecting the clip) with the paint.
350 */
351 void drawPaint(const GrPaint& paint);
352
353 /**
354 * Draw the rect using a paint.
355 * @param paint describes how to color pixels.
356 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
357 * the rect is mitered stroked based on strokeWidth. If
358 * strokeWidth == 0, then the stroke is always a single
359 * pixel thick.
360 * @param matrix Optional matrix applied to the rect. Applied before
361 * context's matrix or the paint's matrix.
362 * The rects coords are used to access the paint (through texture matrix)
363 */
364 void drawRect(const GrPaint& paint,
365 const GrRect&,
366 GrScalar strokeWidth = -1,
367 const GrMatrix* matrix = NULL);
368
369 /**
370 * Maps a rect of paint coordinates onto the a rect of destination
371 * coordinates. Each rect can optionally be transformed. The srcRect
372 * is stretched over the dstRect. The dstRect is transformed by the
373 * context's matrix and the srcRect is transformed by the paint's matrix.
374 * Additional optional matrices can be provided by parameters.
375 *
376 * @param paint describes how to color pixels.
377 * @param dstRect the destination rect to draw.
378 * @param srcRect rect of paint coordinates to be mapped onto dstRect
379 * @param dstMatrix Optional matrix to transform dstRect. Applied before
380 * context's matrix.
381 * @param srcMatrix Optional matrix to transform srcRect Applied before
382 * paint's matrix.
383 */
384 void drawRectToRect(const GrPaint& paint,
385 const GrRect& dstRect,
386 const GrRect& srcRect,
387 const GrMatrix* dstMatrix = NULL,
388 const GrMatrix* srcMatrix = NULL);
389
390 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 *
393 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000394 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000395 * @param fill the path filling rule to use.
396 * @param translate optional additional translation applied to the
397 * path.
398 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000399 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000400 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000401
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402 /**
403 * Draws vertices with a paint.
404 *
405 * @param paint describes how to color pixels.
406 * @param primitiveType primitives type to draw.
407 * @param vertexCount number of vertices.
408 * @param positions array of vertex positions, required.
409 * @param texCoords optional array of texture coordinates used
410 * to access the paint.
411 * @param colors optional array of per-vertex colors, supercedes
412 * the paint's color field.
413 * @param indices optional array of indices. If NULL vertices
414 * are drawn non-indexed.
415 * @param indexCount if indices is non-null then this is the
416 * number of indices.
417 */
418 void drawVertices(const GrPaint& paint,
419 GrPrimitiveType primitiveType,
420 int vertexCount,
421 const GrPoint positions[],
422 const GrPoint texs[],
423 const GrColor colors[],
424 const uint16_t indices[],
425 int indexCount);
426
bsalomon@google.com93c96602012-04-27 13:05:21 +0000427 /**
428 * Draws an oval.
429 *
430 * @param paint describes how to color pixels.
431 * @param rect the bounding rect of the oval.
432 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
433 * the rect is stroked based on strokeWidth. If
434 * strokeWidth == 0, then the stroke is always a single
435 * pixel thick.
436 */
437 void drawOval(const GrPaint& paint,
438 const GrRect& rect,
439 SkScalar strokeWidth);
440
bsalomon@google.com27847de2011-02-22 20:59:41 +0000441 ///////////////////////////////////////////////////////////////////////////
442 // Misc.
443
444 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000445 * Flags that affect flush() behavior.
446 */
447 enum FlushBits {
448 /**
449 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
450 * it can be rendered to directly. However, Gr lazily sets state. Simply
451 * calling setRenderTarget() followed by flush() without flags may not
452 * bind the render target. This flag forces the context to bind the last
453 * set render target in the 3D API.
454 */
455 kForceCurrentRenderTarget_FlushBit = 0x1,
456 /**
457 * A client may reach a point where it has partially rendered a frame
458 * through a GrContext that it knows the user will never see. This flag
459 * causes the flush to skip submission of deferred content to the 3D API
460 * during the flush.
461 */
462 kDiscard_FlushBit = 0x2,
463 };
464
465 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000466 * Call to ensure all drawing to the context has been issued to the
467 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000468 * @param flagsBitfield flags that control the flushing behavior. See
469 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000470 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000471 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000472
bsalomon@google.com27847de2011-02-22 20:59:41 +0000473 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000474 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000475 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000476 * current render target.
477 * @param left left edge of the rectangle to read (inclusive)
478 * @param top top edge of the rectangle to read (inclusive)
479 * @param width width of rectangle to read in pixels.
480 * @param height height of rectangle to read in pixels.
481 * @param config the pixel config of the destination buffer
482 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000484 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000485 *
486 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000487 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000488 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000489 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000490 bool readRenderTargetPixels(GrRenderTarget* target,
491 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000492 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000493 size_t rowBytes) {
494 return this->internalReadRenderTargetPixels(target, left, top,
495 width, height,
496 config, buffer,
497 rowBytes, 0);
498 }
499
500 /**
501 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
502 * at the specified rectangle.
503 * @param target the render target to write into. NULL means the
504 * current render target.
505 * @param left left edge of the rectangle to write (inclusive)
506 * @param top top edge of the rectangle to write (inclusive)
507 * @param width width of rectangle to write in pixels.
508 * @param height height of rectangle to write in pixels.
509 * @param config the pixel config of the source buffer
510 * @param buffer memory to read the rectangle from.
511 * @param rowBytes number of bytes bewtween consecutive rows. Zero
512 * means rows are tightly packed.
513 */
514 void writeRenderTargetPixels(GrRenderTarget* target,
515 int left, int top, int width, int height,
516 GrPixelConfig config, const void* buffer,
517 size_t rowBytes) {
518 this->internalWriteRenderTargetPixels(target, left, top, width, height,
519 config, buffer, rowBytes, 0);
520 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000521
522 /**
523 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000524 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000525 * @param left left edge of the rectangle to read (inclusive)
526 * @param top top edge of the rectangle to read (inclusive)
527 * @param width width of rectangle to read in pixels.
528 * @param height height of rectangle to read in pixels.
529 * @param config the pixel config of the destination buffer
530 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000531 * @param rowBytes number of bytes bewtween consecutive rows. Zero
532 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000533 *
534 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000535 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000536 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000537 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000538 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 GrPixelConfig config, void* buffer,
540 size_t rowBytes) {
541 return this->internalReadTexturePixels(texture, left, top,
542 width, height,
543 config, buffer, rowBytes, 0);
544 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545
546 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000547 * Writes a rectangle of pixels to a texture.
548 * @param texture the render target to read from.
549 * @param left left edge of the rectangle to write (inclusive)
550 * @param top top edge of the rectangle to write (inclusive)
551 * @param width width of rectangle to write in pixels.
552 * @param height height of rectangle to write in pixels.
553 * @param config the pixel config of the source buffer
554 * @param buffer memory to read pixels from
555 * @param rowBytes number of bytes bewtween consecutive rows. Zero
556 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000558 void writeTexturePixels(GrTexture* texture,
559 int left, int top, int width, int height,
560 GrPixelConfig config, const void* buffer,
561 size_t rowBytes) {
562 this->internalWriteTexturePixels(texture, left, top, width, height,
563 config, buffer, rowBytes, 0);
564 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000565 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000566 * Copies all texels from one texture to another.
567 * @param src the texture to copy from.
568 * @param dst the render target to copy to.
569 */
570 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000571
572 /**
573 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
574 * downsampled to the associated GrTexture (accessible via
575 * GrRenderTarget::asTexture()). Any pending draws to the render target will
576 * be executed before the resolve.
577 *
578 * This is only necessary when a client wants to access the object directly
579 * using the underlying graphics API. GrContext will detect when it must
580 * perform a resolve to a GrTexture used as the source of a draw or before
581 * reading pixels back from a GrTexture or GrRenderTarget.
582 */
583 void resolveRenderTarget(GrRenderTarget* target);
584
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000585 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000586 * Applies a 2D Gaussian blur to a given texture.
587 * @param srcTexture The source texture to be blurred.
588 * @param temp1 A scratch texture. Must not be NULL.
589 * @param temp2 A scratch texture. May be NULL, in which case
590 * srcTexture is overwritten with intermediate
591 * results.
592 * @param rect The destination rectangle.
593 * @param sigmaX The blur's standard deviation in X.
594 * @param sigmaY The blur's standard deviation in Y.
595 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000596 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000597 GrTexture* gaussianBlur(GrTexture* srcTexture,
598 GrAutoScratchTexture* temp1,
599 GrAutoScratchTexture* temp2,
600 const SkRect& rect,
601 float sigmaX, float sigmaY);
602
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000603 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000604 * This enum is used with the function below, applyMorphology.
605 */
606 enum MorphologyType {
607 kErode_MorphologyType,
608 kDilate_MorphologyType,
609 };
610
611 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000612 * Applies a 2D morphology to a given texture.
613 * @param srcTexture The source texture to be blurred.
614 * @param rect The destination rectangle.
615 * @param temp1 A scratch texture. Must not be NULL.
616 * @param temp2 A scratch texture. Must not be NULL.
617 * @param filter The morphology filter. Must be kDilate_Filter or
618 * kErode_Filter.
619 * @param radius The morphology radius in X and Y. The filter is
620 * applied to a fWidth by fHeight rectangle of
621 * pixels.
622 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000623 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000624 GrTexture* applyMorphology(GrTexture* srcTexture,
625 const GrRect& rect,
626 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000627 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000628 SkISize radius);
629
bsalomon@google.com27847de2011-02-22 20:59:41 +0000630 ///////////////////////////////////////////////////////////////////////////
631 // Helpers
632
633 class AutoRenderTarget : ::GrNoncopyable {
634 public:
635 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
636 fContext = NULL;
637 fPrevTarget = context->getRenderTarget();
638 if (fPrevTarget != target) {
639 context->setRenderTarget(target);
640 fContext = context;
641 }
642 }
643 ~AutoRenderTarget() {
644 if (fContext) {
645 fContext->setRenderTarget(fPrevTarget);
646 }
647 }
648 private:
649 GrContext* fContext;
650 GrRenderTarget* fPrevTarget;
651 };
652
653
654 ///////////////////////////////////////////////////////////////////////////
655 // Functions intended for internal use only.
656 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000657 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000658 GrFontCache* getFontCache() { return fFontCache; }
659 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000660 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000661
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000662 /**
663 * Stencil buffers add themselves to the cache using
664 * addAndLockStencilBuffer. When a SB's RT-attachment count
665 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
666 * eligible for purging. findStencilBuffer is called to check the cache for
667 * a SB that matching an RT's criteria. If a match is found that has been
668 * unlocked (its attachment count has reached 0) then it will be relocked.
669 */
670 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
671 void unlockStencilBuffer(GrResourceEntry* sbEntry);
672 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000673
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000674 /*
675 * postClipPush acts as a hint to this and lower-level classes (e.g.,
676 * GrGpu) that the clip stack has changed.
677 */
678 virtual void postClipPush();
679
680 /*
681 * preClipPop acts as a hint that the clip stack has been restored to an
682 * earlier state.
683 */
684 virtual void preClipPop();
685
robertphillips@google.com2c756812012-05-22 20:28:23 +0000686 GrPathRenderer* getPathRenderer(const SkPath& path,
687 GrPathFill fill,
688 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000689 bool antiAlias,
690 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000691
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692private:
693 // used to keep track of when we need to flush the draw buffer
694 enum DrawCategory {
695 kBuffered_DrawCategory, // last draw was inserted in draw buffer
696 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000697 };
698 DrawCategory fLastDrawCategory;
699
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000700 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000701 GrDrawState* fDrawState;
702
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000703 GrResourceCache* fTextureCache;
704 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000705
bsalomon@google.com30085192011-08-19 15:42:31 +0000706 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000707 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000708
709 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
710 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
711 GrInOrderDrawBuffer* fDrawBuffer;
712
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000713 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000714
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000715 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000717 void setupDrawBuffer();
718
bsalomon@google.com27847de2011-02-22 20:59:41 +0000719 void flushDrawBuffer();
720
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000721 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722
bsalomon@google.com27847de2011-02-22 20:59:41 +0000723 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
724
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000725 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000726 GrPathFill fill, const GrPoint* translate);
727
bsalomon@google.com6f379512011-11-16 20:36:03 +0000728 /**
729 * Flags to the internal read/write pixels funcs
730 */
731 enum PixelOpsFlags {
732 kDontFlush_PixelOpsFlag = 0x1,
733 };
734
735 bool internalReadRenderTargetPixels(GrRenderTarget* target,
736 int left, int top,
737 int width, int height,
738 GrPixelConfig config, void* buffer,
739 size_t rowBytes, uint32_t flags);
740
741 void internalWriteRenderTargetPixels(GrRenderTarget* target,
742 int left, int top,
743 int width, int height,
744 GrPixelConfig, const void* buffer,
745 size_t rowBytes, uint32_t flags);
746
747 bool internalReadTexturePixels(GrTexture* texture,
748 int left, int top,
749 int width, int height,
750 GrPixelConfig config, void* buffer,
751 size_t rowBytes, uint32_t flags);
752
753 void internalWriteTexturePixels(GrTexture* texture,
754 int left, int top,
755 int width, int height,
756 GrPixelConfig config, const void* buffer,
757 size_t rowBytes, uint32_t flags);
758 // needed for access to internalWriteTexturePixels. TODO: make GrContext
759 // be a facade for an internal class. Then functions that are public on the
760 // internal class would have only be callable in src/gpu. The facade would
761 // only have to functions necessary for clients.
762 friend class GrAtlas;
763
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000764 // computes vertex layout bits based on the paint. If paint expresses
765 // a texture for a stage, the stage coords will be bound to postitions
766 // unless hasTexCoords[s]==true in which case stage s's input coords
767 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
768 // for an array where all the values are false.
769 static int PaintStageVertexLayoutBits(
770 const GrPaint& paint,
771 const bool hasTexCoords[GrPaint::kTotalStages]);
772
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773};
774
775/**
776 * Save/restore the view-matrix in the context.
777 */
778class GrAutoMatrix : GrNoncopyable {
779public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000780 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
782 fMatrix = ctx->getMatrix();
783 }
784 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
785 fMatrix = ctx->getMatrix();
786 ctx->setMatrix(matrix);
787 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000788 void set(GrContext* ctx) {
789 if (NULL != fContext) {
790 fContext->setMatrix(fMatrix);
791 }
792 fMatrix = ctx->getMatrix();
793 fContext = ctx;
794 }
795 void set(GrContext* ctx, const GrMatrix& matrix) {
796 if (NULL != fContext) {
797 fContext->setMatrix(fMatrix);
798 }
799 fMatrix = ctx->getMatrix();
800 ctx->setMatrix(matrix);
801 fContext = ctx;
802 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000804 if (NULL != fContext) {
805 fContext->setMatrix(fMatrix);
806 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807 }
808
809private:
810 GrContext* fContext;
811 GrMatrix fMatrix;
812};
813
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000814/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000815 * Gets and locks a scratch texture from a descriptor using
816 * either exact or approximate criteria. Unlocks texture in
817 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000818 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000819class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000821 GrAutoScratchTexture()
822 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000823 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000824
825 GrAutoScratchTexture(GrContext* context,
826 const GrTextureDesc& desc,
827 GrContext::ScratchTexMatch match =
828 GrContext::kApprox_ScratchTexMatch)
829 : fContext(NULL) {
830 this->set(context, desc, match);
831 }
832
833 ~GrAutoScratchTexture() {
834 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835 fContext->unlockTexture(fEntry);
836 }
837 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000838
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000839 GrTexture* set(GrContext* context,
840 const GrTextureDesc& desc,
841 GrContext::ScratchTexMatch match =
842 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000843 if (NULL != fContext) {
844 fContext->unlockTexture(fEntry);
845 fEntry.reset();
846 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000847 fContext = context;
848 if (NULL != fContext) {
849 fEntry = fContext->lockScratchTexture(desc, match);
850 GrTexture* ret = fEntry.texture();
851 if (NULL == ret) {
852 fContext = NULL;
853 }
854 return ret;
855 } else {
856 return NULL;
857 }
858 }
859
860 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000861private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000862 GrContext* fContext;
863 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000864};
865
bsalomon@google.com27847de2011-02-22 20:59:41 +0000866#endif