blob: 0cd5116cffce17cf28b06dc922b6a52f153e7f2b [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
twiz@google.com05e70242012-01-27 19:12:00 +000086 /**
87 * Returns the number of bytes of GPU memory hosted by the texture cache.
88 */
89 size_t getGpuTextureCacheBytes() const;
90
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 ///////////////////////////////////////////////////////////////////////////
92 // Textures
bsalomon@google.com27847de2011-02-22 20:59:41 +000093
94 /**
bsalomon@google.com50398bf2011-07-26 20:45:30 +000095 * Token that refers to an entry in the texture cache. Returned by
96 * functions that lock textures. Passed to unlockTexture.
bsalomon@google.com27847de2011-02-22 20:59:41 +000097 */
senorblanco@chromium.orgdfad3832012-02-09 16:07:08 +000098 class SK_API TextureCacheEntry {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000099 public:
100 TextureCacheEntry() : fEntry(NULL) {}
101 TextureCacheEntry(const TextureCacheEntry& e) : fEntry(e.fEntry) {}
102 TextureCacheEntry& operator= (const TextureCacheEntry& e) {
103 fEntry = e.fEntry;
104 return *this;
105 }
106 GrTexture* texture() const;
107 void reset() { fEntry = NULL; }
108 private:
109 explicit TextureCacheEntry(GrResourceEntry* entry) { fEntry = entry; }
110 void set(GrResourceEntry* entry) { fEntry = entry; }
111 GrResourceEntry* cacheEntry() { return fEntry; }
112 GrResourceEntry* fEntry;
113 friend class GrContext;
114 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000115
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000116 /**
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000117 * Create a new entry, based on the specified key and texture, and return
118 * its "locked" entry. Must call be balanced with an unlockTexture() call.
119 *
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000120 * @param sampler The sampler state used to draw a texture may be used
121 * to determine how to store the pixel data in the texture
122 * cache. (e.g. different versions may exist for different
123 * wrap modes on GPUs with limited or no NPOT texture
124 * support). Only the wrap and filter fields are used. NULL
125 * implies clamp wrap modes and nearest filtering.
126 * @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 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000131 TextureCacheEntry createAndLockTexture(const GrSamplerState* sampler,
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 *
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000140 * @param desc Description of the texture properties.
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000141 * @param sampler The sampler state used to draw a texture may be used
142 * to determine the cache entry used. (e.g. different
143 * versions may exist for different wrap modes on GPUs with
144 * limited or no NPOT texture support). Only the wrap and
145 * filter fields are used. NULL implies clamp wrap modes
146 * and nearest filtering.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000147 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000148 TextureCacheEntry findAndLockTexture(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000149 const GrSamplerState* sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000150 /**
151 * Determines whether a texture is in the cache. If the texture is found it
152 * will not be locked or returned. This call does not affect the priority of
153 * the texture for deletion.
154 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000155 bool isTextureInCache(const GrTextureDesc& desc,
156 const GrSamplerState* sampler) const;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000157
158 /**
159 * Enum that determines how closely a returned scratch texture must match
160 * a provided GrTextureDesc.
161 */
162 enum ScratchTexMatch {
163 /**
164 * Finds a texture that exactly matches the descriptor.
165 */
166 kExact_ScratchTexMatch,
167 /**
168 * Finds a texture that approximately matches the descriptor. Will be
169 * at least as large in width and height as desc specifies. If desc
170 * specifies that texture is a render target then result will be a
171 * render target. If desc specifies a render target and doesn't set the
172 * no stencil flag then result will have a stencil. Format and aa level
173 * will always match.
174 */
175 kApprox_ScratchTexMatch
176 };
bsalomon@google.com27847de2011-02-22 20:59:41 +0000177
178 /**
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000179 * Returns a texture matching the desc. It's contents are unknown. Subsequent
180 * requests with the same descriptor are not guaranteed to return the same
181 * texture. The same texture is guaranteed not be returned again until it is
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000182 * unlocked. Must call be balanced with an unlockTexture() call.
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000183 *
184 * Textures created by createAndLockTexture() hide the complications of
185 * tiling non-power-of-two textures on APIs that don't support this (e.g.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000186 * unextended GLES2). Tiling a npot texture created by lockScratchTexture on
bsalomon@google.coma39f4042011-04-26 13:18:16 +0000187 * such an API will create gaps in the tiling pattern. This includes clamp
188 * mode. (This may be addressed in a future update.)
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000189 */
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000190 TextureCacheEntry lockScratchTexture(const GrTextureDesc& desc,
191 ScratchTexMatch match);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000192
193 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194 * When done with an entry, call unlockTexture(entry) on it, which returns
195 * it to the cache, where it may be purged.
196 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000197 void unlockTexture(TextureCacheEntry entry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000198
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 /**
215 * Return the current texture cache limits.
216 *
217 * @param maxTextures If non-null, returns maximum number of textures that
218 * can be held in the cache.
219 * @param maxTextureBytes If non-null, returns maximum number of bytes of
220 * texture memory that can be held in the cache.
221 */
222 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
223
224 /**
225 * Specify the texture cache limits. If the current cache exceeds either
226 * of these, it will be purged (LRU) to keep the cache within these limits.
227 *
228 * @param maxTextures The maximum number of textures that can be held in
229 * the cache.
230 * @param maxTextureBytes The maximum number of bytes of texture memory
231 * that can be held in the cache.
232 */
233 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
234
235 /**
236 * Return the max width or height of a texture supported by the current gpu
237 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000238 int getMaxTextureSize() const;
239
240 /**
241 * Return the max width or height of a render target supported by the
242 * current gpu
243 */
244 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000245
246 ///////////////////////////////////////////////////////////////////////////
247 // Render targets
248
249 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000250 * Sets the render target.
251 * @param target the render target to set. (should not be NULL.)
252 */
253 void setRenderTarget(GrRenderTarget* target);
254
255 /**
256 * Gets the current render target.
257 * @return the currently bound render target. Should never be NULL.
258 */
259 const GrRenderTarget* getRenderTarget() const;
260 GrRenderTarget* getRenderTarget();
261
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000262 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
263
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000264 /**
265 * Can the provided configuration act as a color render target?
266 */
267 bool isConfigRenderable(GrPixelConfig config) const;
268
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000269 ///////////////////////////////////////////////////////////////////////////
270 // Platform Surfaces
271
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000272 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000273 * Wraps an existing texture with a GrTexture object.
274 *
275 * OpenGL: if the object is a texture Gr may change its GL texture params
276 * when it is drawn.
277 *
278 * @param desc description of the object to create.
279 *
280 * @return GrTexture object or NULL on failure.
281 */
282 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
283
284 /**
285 * Wraps an existing render target with a GrRenderTarget object. It is
286 * similar to createPlatformTexture but can be used to draw into surfaces
287 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
288 * the client will resolve to a texture).
289 *
290 * @param desc description of the object to create.
291 *
292 * @return GrTexture object or NULL on failure.
293 */
294 GrRenderTarget* createPlatformRenderTarget(
295 const GrPlatformRenderTargetDesc& desc);
296
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297 ///////////////////////////////////////////////////////////////////////////
298 // Matrix state
299
300 /**
301 * Gets the current transformation matrix.
302 * @return the current matrix.
303 */
304 const GrMatrix& getMatrix() const;
305
306 /**
307 * Sets the transformation matrix.
308 * @param m the matrix to set.
309 */
310 void setMatrix(const GrMatrix& m);
311
312 /**
313 * Concats the current matrix. The passed matrix is applied before the
314 * current matrix.
315 * @param m the matrix to concat.
316 */
317 void concatMatrix(const GrMatrix& m) const;
318
319
320 ///////////////////////////////////////////////////////////////////////////
321 // Clip state
322 /**
323 * Gets the current clip.
324 * @return the current clip.
325 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000326 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000327
328 /**
329 * Sets the clip.
330 * @param clip the clip to set.
331 */
332 void setClip(const GrClip& clip);
333
334 /**
335 * Convenience method for setting the clip to a rect.
336 * @param rect the rect to set as the new clip.
337 */
338 void setClip(const GrIRect& rect);
339
340 ///////////////////////////////////////////////////////////////////////////
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.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000347 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000348 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349
350 /**
351 * Draw everywhere (respecting the clip) with the paint.
352 */
353 void drawPaint(const GrPaint& paint);
354
355 /**
356 * Draw the rect using a paint.
357 * @param paint describes how to color pixels.
358 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
359 * the rect is mitered stroked based on strokeWidth. If
360 * strokeWidth == 0, then the stroke is always a single
361 * pixel thick.
362 * @param matrix Optional matrix applied to the rect. Applied before
363 * context's matrix or the paint's matrix.
364 * The rects coords are used to access the paint (through texture matrix)
365 */
366 void drawRect(const GrPaint& paint,
367 const GrRect&,
368 GrScalar strokeWidth = -1,
369 const GrMatrix* matrix = NULL);
370
371 /**
372 * Maps a rect of paint coordinates onto the a rect of destination
373 * coordinates. Each rect can optionally be transformed. The srcRect
374 * is stretched over the dstRect. The dstRect is transformed by the
375 * context's matrix and the srcRect is transformed by the paint's matrix.
376 * Additional optional matrices can be provided by parameters.
377 *
378 * @param paint describes how to color pixels.
379 * @param dstRect the destination rect to draw.
380 * @param srcRect rect of paint coordinates to be mapped onto dstRect
381 * @param dstMatrix Optional matrix to transform dstRect. Applied before
382 * context's matrix.
383 * @param srcMatrix Optional matrix to transform srcRect Applied before
384 * paint's matrix.
385 */
386 void drawRectToRect(const GrPaint& paint,
387 const GrRect& dstRect,
388 const GrRect& srcRect,
389 const GrMatrix* dstMatrix = NULL,
390 const GrMatrix* srcMatrix = NULL);
391
392 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000393 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394 *
395 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000396 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 * @param fill the path filling rule to use.
398 * @param translate optional additional translation applied to the
399 * path.
400 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000401 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000402 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000403
bsalomon@google.com27847de2011-02-22 20:59:41 +0000404 /**
405 * Draws vertices with a paint.
406 *
407 * @param paint describes how to color pixels.
408 * @param primitiveType primitives type to draw.
409 * @param vertexCount number of vertices.
410 * @param positions array of vertex positions, required.
411 * @param texCoords optional array of texture coordinates used
412 * to access the paint.
413 * @param colors optional array of per-vertex colors, supercedes
414 * the paint's color field.
415 * @param indices optional array of indices. If NULL vertices
416 * are drawn non-indexed.
417 * @param indexCount if indices is non-null then this is the
418 * number of indices.
419 */
420 void drawVertices(const GrPaint& paint,
421 GrPrimitiveType primitiveType,
422 int vertexCount,
423 const GrPoint positions[],
424 const GrPoint texs[],
425 const GrColor colors[],
426 const uint16_t indices[],
427 int indexCount);
428
bsalomon@google.com93c96602012-04-27 13:05:21 +0000429 /**
430 * Draws an oval.
431 *
432 * @param paint describes how to color pixels.
433 * @param rect the bounding rect of the oval.
434 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
435 * the rect is stroked based on strokeWidth. If
436 * strokeWidth == 0, then the stroke is always a single
437 * pixel thick.
438 */
439 void drawOval(const GrPaint& paint,
440 const GrRect& rect,
441 SkScalar strokeWidth);
442
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 ///////////////////////////////////////////////////////////////////////////
444 // Misc.
445
446 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000447 * Flags that affect flush() behavior.
448 */
449 enum FlushBits {
450 /**
451 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
452 * it can be rendered to directly. However, Gr lazily sets state. Simply
453 * calling setRenderTarget() followed by flush() without flags may not
454 * bind the render target. This flag forces the context to bind the last
455 * set render target in the 3D API.
456 */
457 kForceCurrentRenderTarget_FlushBit = 0x1,
458 /**
459 * A client may reach a point where it has partially rendered a frame
460 * through a GrContext that it knows the user will never see. This flag
461 * causes the flush to skip submission of deferred content to the 3D API
462 * during the flush.
463 */
464 kDiscard_FlushBit = 0x2,
465 };
466
467 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000468 * Call to ensure all drawing to the context has been issued to the
469 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000470 * @param flagsBitfield flags that control the flushing behavior. See
471 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000472 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000473 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000474
bsalomon@google.com27847de2011-02-22 20:59:41 +0000475 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000476 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000477 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000478 * current render target.
479 * @param left left edge of the rectangle to read (inclusive)
480 * @param top top edge of the rectangle to read (inclusive)
481 * @param width width of rectangle to read in pixels.
482 * @param height height of rectangle to read in pixels.
483 * @param config the pixel config of the destination buffer
484 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000485 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000486 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000487 *
488 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000489 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000490 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000491 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000492 bool readRenderTargetPixels(GrRenderTarget* target,
493 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000494 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000495 size_t rowBytes) {
496 return this->internalReadRenderTargetPixels(target, left, top,
497 width, height,
498 config, buffer,
499 rowBytes, 0);
500 }
501
502 /**
503 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
504 * at the specified rectangle.
505 * @param target the render target to write into. NULL means the
506 * current render target.
507 * @param left left edge of the rectangle to write (inclusive)
508 * @param top top edge of the rectangle to write (inclusive)
509 * @param width width of rectangle to write in pixels.
510 * @param height height of rectangle to write in pixels.
511 * @param config the pixel config of the source buffer
512 * @param buffer memory to read the rectangle from.
513 * @param rowBytes number of bytes bewtween consecutive rows. Zero
514 * means rows are tightly packed.
515 */
516 void writeRenderTargetPixels(GrRenderTarget* target,
517 int left, int top, int width, int height,
518 GrPixelConfig config, const void* buffer,
519 size_t rowBytes) {
520 this->internalWriteRenderTargetPixels(target, left, top, width, height,
521 config, buffer, rowBytes, 0);
522 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523
524 /**
525 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000526 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000527 * @param left left edge of the rectangle to read (inclusive)
528 * @param top top edge of the rectangle to read (inclusive)
529 * @param width width of rectangle to read in pixels.
530 * @param height height of rectangle to read in pixels.
531 * @param config the pixel config of the destination buffer
532 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000533 * @param rowBytes number of bytes bewtween consecutive rows. Zero
534 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000535 *
536 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000537 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000538 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000539 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000540 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000541 GrPixelConfig config, void* buffer,
542 size_t rowBytes) {
543 return this->internalReadTexturePixels(texture, left, top,
544 width, height,
545 config, buffer, rowBytes, 0);
546 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547
548 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000549 * Writes a rectangle of pixels to a texture.
550 * @param texture the render target to read from.
551 * @param left left edge of the rectangle to write (inclusive)
552 * @param top top edge of the rectangle to write (inclusive)
553 * @param width width of rectangle to write in pixels.
554 * @param height height of rectangle to write in pixels.
555 * @param config the pixel config of the source buffer
556 * @param buffer memory to read pixels from
557 * @param rowBytes number of bytes bewtween consecutive rows. Zero
558 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000560 void writeTexturePixels(GrTexture* texture,
561 int left, int top, int width, int height,
562 GrPixelConfig config, const void* buffer,
563 size_t rowBytes) {
564 this->internalWriteTexturePixels(texture, left, top, width, height,
565 config, buffer, rowBytes, 0);
566 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000567 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000568 * Copies all texels from one texture to another.
569 * @param src the texture to copy from.
570 * @param dst the render target to copy to.
571 */
572 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000573
574 /**
575 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
576 * downsampled to the associated GrTexture (accessible via
577 * GrRenderTarget::asTexture()). Any pending draws to the render target will
578 * be executed before the resolve.
579 *
580 * This is only necessary when a client wants to access the object directly
581 * using the underlying graphics API. GrContext will detect when it must
582 * perform a resolve to a GrTexture used as the source of a draw or before
583 * reading pixels back from a GrTexture or GrRenderTarget.
584 */
585 void resolveRenderTarget(GrRenderTarget* target);
586
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000587 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000588 * Applies a 2D Gaussian blur to a given texture.
589 * @param srcTexture The source texture to be blurred.
590 * @param temp1 A scratch texture. Must not be NULL.
591 * @param temp2 A scratch texture. May be NULL, in which case
592 * srcTexture is overwritten with intermediate
593 * results.
594 * @param rect The destination rectangle.
595 * @param sigmaX The blur's standard deviation in X.
596 * @param sigmaY The blur's standard deviation in Y.
597 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000598 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000599 GrTexture* gaussianBlur(GrTexture* srcTexture,
600 GrAutoScratchTexture* temp1,
601 GrAutoScratchTexture* temp2,
602 const SkRect& rect,
603 float sigmaX, float sigmaY);
604
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000605 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000606 * This enum is used with the function below, applyMorphology.
607 */
608 enum MorphologyType {
609 kErode_MorphologyType,
610 kDilate_MorphologyType,
611 };
612
613 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000614 * Applies a 2D morphology to a given texture.
615 * @param srcTexture The source texture to be blurred.
616 * @param rect The destination rectangle.
617 * @param temp1 A scratch texture. Must not be NULL.
618 * @param temp2 A scratch texture. Must not be NULL.
619 * @param filter The morphology filter. Must be kDilate_Filter or
620 * kErode_Filter.
621 * @param radius The morphology radius in X and Y. The filter is
622 * applied to a fWidth by fHeight rectangle of
623 * pixels.
624 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
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,
628 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000629 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000630 SkISize radius);
631
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 ///////////////////////////////////////////////////////////////////////////
633 // Helpers
634
635 class AutoRenderTarget : ::GrNoncopyable {
636 public:
637 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
638 fContext = NULL;
639 fPrevTarget = context->getRenderTarget();
640 if (fPrevTarget != target) {
641 context->setRenderTarget(target);
642 fContext = context;
643 }
644 }
645 ~AutoRenderTarget() {
646 if (fContext) {
647 fContext->setRenderTarget(fPrevTarget);
648 }
649 }
650 private:
651 GrContext* fContext;
652 GrRenderTarget* fPrevTarget;
653 };
654
655
656 ///////////////////////////////////////////////////////////////////////////
657 // Functions intended for internal use only.
658 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000659 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000660 GrFontCache* getFontCache() { return fFontCache; }
661 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000662 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000663
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000664 /**
665 * Stencil buffers add themselves to the cache using
666 * addAndLockStencilBuffer. When a SB's RT-attachment count
667 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
668 * eligible for purging. findStencilBuffer is called to check the cache for
669 * a SB that matching an RT's criteria. If a match is found that has been
670 * unlocked (its attachment count has reached 0) then it will be relocked.
671 */
672 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
673 void unlockStencilBuffer(GrResourceEntry* sbEntry);
674 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000675
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000676 /*
677 * postClipPush acts as a hint to this and lower-level classes (e.g.,
678 * GrGpu) that the clip stack has changed.
679 */
680 virtual void postClipPush();
681
682 /*
683 * preClipPop acts as a hint that the clip stack has been restored to an
684 * earlier state.
685 */
686 virtual void preClipPop();
687
robertphillips@google.com2c756812012-05-22 20:28:23 +0000688 GrPathRenderer* getPathRenderer(const SkPath& path,
689 GrPathFill fill,
690 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000691 bool antiAlias,
692 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000693
bsalomon@google.com27847de2011-02-22 20:59:41 +0000694private:
695 // used to keep track of when we need to flush the draw buffer
696 enum DrawCategory {
697 kBuffered_DrawCategory, // last draw was inserted in draw buffer
698 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000699 };
700 DrawCategory fLastDrawCategory;
701
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000702 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000703 GrDrawState* fDrawState;
704
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000705 GrResourceCache* fTextureCache;
706 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000707
bsalomon@google.com30085192011-08-19 15:42:31 +0000708 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000709 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000710
711 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
712 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
713 GrInOrderDrawBuffer* fDrawBuffer;
714
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000715 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000716
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000717 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000719 void setupDrawBuffer();
720
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721 void flushDrawBuffer();
722
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000723 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000724
bsalomon@google.com27847de2011-02-22 20:59:41 +0000725 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
726
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000727 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000728 GrPathFill fill, const GrPoint* translate);
729
bsalomon@google.com6f379512011-11-16 20:36:03 +0000730 /**
731 * Flags to the internal read/write pixels funcs
732 */
733 enum PixelOpsFlags {
734 kDontFlush_PixelOpsFlag = 0x1,
735 };
736
737 bool internalReadRenderTargetPixels(GrRenderTarget* target,
738 int left, int top,
739 int width, int height,
740 GrPixelConfig config, void* buffer,
741 size_t rowBytes, uint32_t flags);
742
743 void internalWriteRenderTargetPixels(GrRenderTarget* target,
744 int left, int top,
745 int width, int height,
746 GrPixelConfig, const void* buffer,
747 size_t rowBytes, uint32_t flags);
748
749 bool internalReadTexturePixels(GrTexture* texture,
750 int left, int top,
751 int width, int height,
752 GrPixelConfig config, void* buffer,
753 size_t rowBytes, uint32_t flags);
754
755 void internalWriteTexturePixels(GrTexture* texture,
756 int left, int top,
757 int width, int height,
758 GrPixelConfig config, const void* buffer,
759 size_t rowBytes, uint32_t flags);
760 // needed for access to internalWriteTexturePixels. TODO: make GrContext
761 // be a facade for an internal class. Then functions that are public on the
762 // internal class would have only be callable in src/gpu. The facade would
763 // only have to functions necessary for clients.
764 friend class GrAtlas;
765
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000766 // computes vertex layout bits based on the paint. If paint expresses
767 // a texture for a stage, the stage coords will be bound to postitions
768 // unless hasTexCoords[s]==true in which case stage s's input coords
769 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
770 // for an array where all the values are false.
771 static int PaintStageVertexLayoutBits(
772 const GrPaint& paint,
773 const bool hasTexCoords[GrPaint::kTotalStages]);
774
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775};
776
777/**
778 * Save/restore the view-matrix in the context.
779 */
780class GrAutoMatrix : GrNoncopyable {
781public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000782 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
784 fMatrix = ctx->getMatrix();
785 }
786 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
787 fMatrix = ctx->getMatrix();
788 ctx->setMatrix(matrix);
789 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000790 void set(GrContext* ctx) {
791 if (NULL != fContext) {
792 fContext->setMatrix(fMatrix);
793 }
794 fMatrix = ctx->getMatrix();
795 fContext = ctx;
796 }
797 void set(GrContext* ctx, const GrMatrix& matrix) {
798 if (NULL != fContext) {
799 fContext->setMatrix(fMatrix);
800 }
801 fMatrix = ctx->getMatrix();
802 ctx->setMatrix(matrix);
803 fContext = ctx;
804 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000806 if (NULL != fContext) {
807 fContext->setMatrix(fMatrix);
808 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000809 }
810
811private:
812 GrContext* fContext;
813 GrMatrix fMatrix;
814};
815
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000816/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000817 * Gets and locks a scratch texture from a descriptor using
818 * either exact or approximate criteria. Unlocks texture in
819 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000821class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000822public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000823 GrAutoScratchTexture()
824 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000825 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000826
827 GrAutoScratchTexture(GrContext* context,
828 const GrTextureDesc& desc,
829 GrContext::ScratchTexMatch match =
830 GrContext::kApprox_ScratchTexMatch)
831 : fContext(NULL) {
832 this->set(context, desc, match);
833 }
834
835 ~GrAutoScratchTexture() {
836 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000837 fContext->unlockTexture(fEntry);
838 }
839 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000840
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000841 GrTexture* set(GrContext* context,
842 const GrTextureDesc& desc,
843 GrContext::ScratchTexMatch match =
844 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000845 if (NULL != fContext) {
846 fContext->unlockTexture(fEntry);
847 fEntry.reset();
848 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000849 fContext = context;
850 if (NULL != fContext) {
851 fEntry = fContext->lockScratchTexture(desc, match);
852 GrTexture* ret = fEntry.texture();
853 if (NULL == ret) {
854 fContext = NULL;
855 }
856 return ret;
857 } else {
858 return NULL;
859 }
860 }
861
862 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000863private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000864 GrContext* fContext;
865 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000866};
867
bsalomon@google.com27847de2011-02-22 20:59:41 +0000868#endif