blob: ee6130b13d83a99ca04743872d4477fc2a6d1de5 [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.com27847de2011-02-22 20:59:41 +000045 virtual ~GrContext();
46
47 /**
48 * The GrContext normally assumes that no outsider is setting state
49 * within the underlying 3D API's context/device/whatever. This call informs
50 * the context that the state was modified and it should resend. Shouldn't
51 * be called frequently for good performance.
52 */
53 void resetContext();
54
bsalomon@google.com8fe72472011-03-30 21:26:44 +000055 /**
56 * Abandons all gpu resources, assumes 3D API state is unknown. Call this
57 * if you have lost the associated GPU context, and thus internal texture,
58 * buffer, etc. references/IDs are now invalid. Should be called even when
59 * GrContext is no longer going to be used for two reasons:
60 * 1) ~GrContext will not try to free the objects in the 3D API.
61 * 2) If you've created GrResources that outlive the GrContext they will
62 * be marked as invalid (GrResource::isValid()) and won't attempt to
63 * free their underlying resource in the 3D API.
64 * Content drawn since the last GrContext::flush() may be lost.
65 */
66 void contextLost();
bsalomon@google.com27847de2011-02-22 20:59:41 +000067
68 /**
junov@google.com53a55842011-06-08 22:55:10 +000069 * Similar to contextLost, but makes no attempt to reset state.
70 * Use this method when GrContext destruction is pending, but
71 * the graphics context is destroyed first.
72 */
73 void contextDestroyed();
74
75 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076 * Frees gpu created by the context. Can be called to reduce GPU memory
77 * pressure.
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 void freeGpuResources();
80
twiz@google.com05e70242012-01-27 19:12:00 +000081 /**
82 * Returns the number of bytes of GPU memory hosted by the texture cache.
83 */
84 size_t getGpuTextureCacheBytes() const;
85
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 ///////////////////////////////////////////////////////////////////////////
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 /**
210 * Return the current texture cache limits.
211 *
212 * @param maxTextures If non-null, returns maximum number of textures that
213 * can be held in the cache.
214 * @param maxTextureBytes If non-null, returns maximum number of bytes of
215 * texture memory that can be held in the cache.
216 */
217 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
218
219 /**
220 * Specify the texture cache limits. If the current cache exceeds either
221 * of these, it will be purged (LRU) to keep the cache within these limits.
222 *
223 * @param maxTextures The maximum number of textures that can be held in
224 * the cache.
225 * @param maxTextureBytes The maximum number of bytes of texture memory
226 * that can be held in the cache.
227 */
228 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
229
230 /**
231 * Return the max width or height of a texture supported by the current gpu
232 */
bsalomon@google.com91958362011-06-13 17:58:13 +0000233 int getMaxTextureSize() const;
234
235 /**
236 * Return the max width or height of a render target supported by the
237 * current gpu
238 */
239 int getMaxRenderTargetSize() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000240
241 ///////////////////////////////////////////////////////////////////////////
242 // Render targets
243
244 /**
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000245 * Sets the render target.
246 * @param target the render target to set. (should not be NULL.)
247 */
248 void setRenderTarget(GrRenderTarget* target);
249
250 /**
251 * Gets the current render target.
252 * @return the currently bound render target. Should never be NULL.
253 */
254 const GrRenderTarget* getRenderTarget() const;
255 GrRenderTarget* getRenderTarget();
256
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000257 GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
258
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000259 /**
260 * Can the provided configuration act as a color render target?
261 */
262 bool isConfigRenderable(GrPixelConfig config) const;
263
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000264 ///////////////////////////////////////////////////////////////////////////
265 // Platform Surfaces
266
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000267 /**
bsalomon@google.come269f212011-11-07 13:29:52 +0000268 * Wraps an existing texture with a GrTexture object.
269 *
270 * OpenGL: if the object is a texture Gr may change its GL texture params
271 * when it is drawn.
272 *
273 * @param desc description of the object to create.
274 *
275 * @return GrTexture object or NULL on failure.
276 */
277 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
278
279 /**
280 * Wraps an existing render target with a GrRenderTarget object. It is
281 * similar to createPlatformTexture but can be used to draw into surfaces
282 * that are not also textures (e.g. FBO 0 in OpenGL, or an MSAA buffer that
283 * the client will resolve to a texture).
284 *
285 * @param desc description of the object to create.
286 *
287 * @return GrTexture object or NULL on failure.
288 */
289 GrRenderTarget* createPlatformRenderTarget(
290 const GrPlatformRenderTargetDesc& desc);
291
bsalomon@google.com27847de2011-02-22 20:59:41 +0000292 ///////////////////////////////////////////////////////////////////////////
293 // Matrix state
294
295 /**
296 * Gets the current transformation matrix.
297 * @return the current matrix.
298 */
299 const GrMatrix& getMatrix() const;
300
301 /**
302 * Sets the transformation matrix.
303 * @param m the matrix to set.
304 */
305 void setMatrix(const GrMatrix& m);
306
307 /**
308 * Concats the current matrix. The passed matrix is applied before the
309 * current matrix.
310 * @param m the matrix to concat.
311 */
312 void concatMatrix(const GrMatrix& m) const;
313
314
315 ///////////////////////////////////////////////////////////////////////////
316 // Clip state
317 /**
318 * Gets the current clip.
319 * @return the current clip.
320 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000321 const GrClip& getClip() const;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000322
323 /**
324 * Sets the clip.
325 * @param clip the clip to set.
326 */
327 void setClip(const GrClip& clip);
328
329 /**
330 * Convenience method for setting the clip to a rect.
331 * @param rect the rect to set as the new clip.
332 */
333 void setClip(const GrIRect& rect);
334
335 ///////////////////////////////////////////////////////////////////////////
336 // Draws
337
338 /**
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000339 * Clear the entire or rect of the render target, ignoring any clips.
340 * @param rect the rect to clear or the whole thing if rect is NULL.
341 * @param color the color to clear to.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000342 */
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000343 void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000344
345 /**
346 * Draw everywhere (respecting the clip) with the paint.
347 */
348 void drawPaint(const GrPaint& paint);
349
350 /**
351 * Draw the rect using a paint.
352 * @param paint describes how to color pixels.
353 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
354 * the rect is mitered stroked based on strokeWidth. If
355 * strokeWidth == 0, then the stroke is always a single
356 * pixel thick.
357 * @param matrix Optional matrix applied to the rect. Applied before
358 * context's matrix or the paint's matrix.
359 * The rects coords are used to access the paint (through texture matrix)
360 */
361 void drawRect(const GrPaint& paint,
362 const GrRect&,
363 GrScalar strokeWidth = -1,
364 const GrMatrix* matrix = NULL);
365
366 /**
367 * Maps a rect of paint coordinates onto the a rect of destination
368 * coordinates. Each rect can optionally be transformed. The srcRect
369 * is stretched over the dstRect. The dstRect is transformed by the
370 * context's matrix and the srcRect is transformed by the paint's matrix.
371 * Additional optional matrices can be provided by parameters.
372 *
373 * @param paint describes how to color pixels.
374 * @param dstRect the destination rect to draw.
375 * @param srcRect rect of paint coordinates to be mapped onto dstRect
376 * @param dstMatrix Optional matrix to transform dstRect. Applied before
377 * context's matrix.
378 * @param srcMatrix Optional matrix to transform srcRect Applied before
379 * paint's matrix.
380 */
381 void drawRectToRect(const GrPaint& paint,
382 const GrRect& dstRect,
383 const GrRect& srcRect,
384 const GrMatrix* dstMatrix = NULL,
385 const GrMatrix* srcMatrix = NULL);
386
387 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000388 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389 *
390 * @param paint describes how to color pixels.
reed@google.com07f3ee12011-05-16 17:21:57 +0000391 * @param path the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 * @param fill the path filling rule to use.
393 * @param translate optional additional translation applied to the
394 * path.
395 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000396 void drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000397 const GrPoint* translate = NULL);
reed@google.com07f3ee12011-05-16 17:21:57 +0000398
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 /**
400 * Draws vertices with a paint.
401 *
402 * @param paint describes how to color pixels.
403 * @param primitiveType primitives type to draw.
404 * @param vertexCount number of vertices.
405 * @param positions array of vertex positions, required.
406 * @param texCoords optional array of texture coordinates used
407 * to access the paint.
408 * @param colors optional array of per-vertex colors, supercedes
409 * the paint's color field.
410 * @param indices optional array of indices. If NULL vertices
411 * are drawn non-indexed.
412 * @param indexCount if indices is non-null then this is the
413 * number of indices.
414 */
415 void drawVertices(const GrPaint& paint,
416 GrPrimitiveType primitiveType,
417 int vertexCount,
418 const GrPoint positions[],
419 const GrPoint texs[],
420 const GrColor colors[],
421 const uint16_t indices[],
422 int indexCount);
423
bsalomon@google.com93c96602012-04-27 13:05:21 +0000424 /**
425 * Draws an oval.
426 *
427 * @param paint describes how to color pixels.
428 * @param rect the bounding rect of the oval.
429 * @param strokeWidth if strokeWidth < 0, then the oval is filled, else
430 * the rect is stroked based on strokeWidth. If
431 * strokeWidth == 0, then the stroke is always a single
432 * pixel thick.
433 */
434 void drawOval(const GrPaint& paint,
435 const GrRect& rect,
436 SkScalar strokeWidth);
437
bsalomon@google.com27847de2011-02-22 20:59:41 +0000438 ///////////////////////////////////////////////////////////////////////////
439 // Misc.
440
441 /**
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442 * Flags that affect flush() behavior.
443 */
444 enum FlushBits {
445 /**
446 * A client may want Gr to bind a GrRenderTarget in the 3D API so that
447 * it can be rendered to directly. However, Gr lazily sets state. Simply
448 * calling setRenderTarget() followed by flush() without flags may not
449 * bind the render target. This flag forces the context to bind the last
450 * set render target in the 3D API.
451 */
452 kForceCurrentRenderTarget_FlushBit = 0x1,
453 /**
454 * A client may reach a point where it has partially rendered a frame
455 * through a GrContext that it knows the user will never see. This flag
456 * causes the flush to skip submission of deferred content to the 3D API
457 * during the flush.
458 */
459 kDiscard_FlushBit = 0x2,
460 };
461
462 /**
bsalomon@google.com27847de2011-02-22 20:59:41 +0000463 * Call to ensure all drawing to the context has been issued to the
464 * underlying 3D API.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000465 * @param flagsBitfield flags that control the flushing behavior. See
466 * FlushBits.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467 */
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000468 void flush(int flagsBitfield = 0);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000469
bsalomon@google.com27847de2011-02-22 20:59:41 +0000470 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000471 * Reads a rectangle of pixels from a render target.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000472 * @param target the render target to read from. NULL means the
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000473 * current render target.
474 * @param left left edge of the rectangle to read (inclusive)
475 * @param top top edge of the rectangle to read (inclusive)
476 * @param width width of rectangle to read in pixels.
477 * @param height height of rectangle to read in pixels.
478 * @param config the pixel config of the destination buffer
479 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000480 * @param rowBytes number of bytes bewtween consecutive rows. Zero
bsalomon@google.comc6980972011-11-02 19:57:21 +0000481 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000482 *
483 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000484 * because of an unsupported pixel config or because no render
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000485 * target is currently set.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000486 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000487 bool readRenderTargetPixels(GrRenderTarget* target,
488 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000489 GrPixelConfig config, void* buffer,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000490 size_t rowBytes) {
491 return this->internalReadRenderTargetPixels(target, left, top,
492 width, height,
493 config, buffer,
494 rowBytes, 0);
495 }
496
497 /**
498 * Copy the src pixels [buffer, rowbytes, pixelconfig] into a render target
499 * at the specified rectangle.
500 * @param target the render target to write into. NULL means the
501 * current render target.
502 * @param left left edge of the rectangle to write (inclusive)
503 * @param top top edge of the rectangle to write (inclusive)
504 * @param width width of rectangle to write in pixels.
505 * @param height height of rectangle to write in pixels.
506 * @param config the pixel config of the source buffer
507 * @param buffer memory to read the rectangle from.
508 * @param rowBytes number of bytes bewtween consecutive rows. Zero
509 * means rows are tightly packed.
510 */
511 void writeRenderTargetPixels(GrRenderTarget* target,
512 int left, int top, int width, int height,
513 GrPixelConfig config, const void* buffer,
514 size_t rowBytes) {
515 this->internalWriteRenderTargetPixels(target, left, top, width, height,
516 config, buffer, rowBytes, 0);
517 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000518
519 /**
520 * Reads a rectangle of pixels from a texture.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000521 * @param texture the texture to read from.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000522 * @param left left edge of the rectangle to read (inclusive)
523 * @param top top edge of the rectangle to read (inclusive)
524 * @param width width of rectangle to read in pixels.
525 * @param height height of rectangle to read in pixels.
526 * @param config the pixel config of the destination buffer
527 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000528 * @param rowBytes number of bytes bewtween consecutive rows. Zero
529 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000530 *
531 * @return true if the read succeeded, false if not. The read can fail
bsalomon@google.com6f379512011-11-16 20:36:03 +0000532 * because of an unsupported pixel config.
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000533 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000534 bool readTexturePixels(GrTexture* texture,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000535 int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000536 GrPixelConfig config, void* buffer,
537 size_t rowBytes) {
538 return this->internalReadTexturePixels(texture, left, top,
539 width, height,
540 config, buffer, rowBytes, 0);
541 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542
543 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +0000544 * Writes a rectangle of pixels to a texture.
545 * @param texture the render target to read from.
546 * @param left left edge of the rectangle to write (inclusive)
547 * @param top top edge of the rectangle to write (inclusive)
548 * @param width width of rectangle to write in pixels.
549 * @param height height of rectangle to write in pixels.
550 * @param config the pixel config of the source buffer
551 * @param buffer memory to read pixels from
552 * @param rowBytes number of bytes bewtween consecutive rows. Zero
553 * means rows are tightly packed.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554 */
bsalomon@google.com6f379512011-11-16 20:36:03 +0000555 void writeTexturePixels(GrTexture* texture,
556 int left, int top, int width, int height,
557 GrPixelConfig config, const void* buffer,
558 size_t rowBytes) {
559 this->internalWriteTexturePixels(texture, left, top, width, height,
560 config, buffer, rowBytes, 0);
561 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000562 /**
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000563 * Copies all texels from one texture to another.
564 * @param src the texture to copy from.
565 * @param dst the render target to copy to.
566 */
567 void copyTexture(GrTexture* src, GrRenderTarget* dst);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000568
569 /**
570 * Resolves a render target that has MSAA. The intermediate MSAA buffer is
571 * downsampled to the associated GrTexture (accessible via
572 * GrRenderTarget::asTexture()). Any pending draws to the render target will
573 * be executed before the resolve.
574 *
575 * This is only necessary when a client wants to access the object directly
576 * using the underlying graphics API. GrContext will detect when it must
577 * perform a resolve to a GrTexture used as the source of a draw or before
578 * reading pixels back from a GrTexture or GrRenderTarget.
579 */
580 void resolveRenderTarget(GrRenderTarget* target);
581
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000582 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000583 * Applies a 2D Gaussian blur to a given texture.
584 * @param srcTexture The source texture to be blurred.
585 * @param temp1 A scratch texture. Must not be NULL.
586 * @param temp2 A scratch texture. May be NULL, in which case
587 * srcTexture is overwritten with intermediate
588 * results.
589 * @param rect The destination rectangle.
590 * @param sigmaX The blur's standard deviation in X.
591 * @param sigmaY The blur's standard deviation in Y.
592 * @return the blurred texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000593 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000594 GrTexture* gaussianBlur(GrTexture* srcTexture,
595 GrAutoScratchTexture* temp1,
596 GrAutoScratchTexture* temp2,
597 const SkRect& rect,
598 float sigmaX, float sigmaY);
599
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000600 /**
bsalomon@google.comb505a122012-05-31 18:40:36 +0000601 * This enum is used with the function below, applyMorphology.
602 */
603 enum MorphologyType {
604 kErode_MorphologyType,
605 kDilate_MorphologyType,
606 };
607
608 /**
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000609 * Applies a 2D morphology to a given texture.
610 * @param srcTexture The source texture to be blurred.
611 * @param rect The destination rectangle.
612 * @param temp1 A scratch texture. Must not be NULL.
613 * @param temp2 A scratch texture. Must not be NULL.
614 * @param filter The morphology filter. Must be kDilate_Filter or
615 * kErode_Filter.
616 * @param radius The morphology radius in X and Y. The filter is
617 * applied to a fWidth by fHeight rectangle of
618 * pixels.
619 * @return the morphed texture, which may be temp1, temp2 or srcTexture.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000620 */
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000621 GrTexture* applyMorphology(GrTexture* srcTexture,
622 const GrRect& rect,
623 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000624 MorphologyType type,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000625 SkISize radius);
626
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627 ///////////////////////////////////////////////////////////////////////////
628 // Helpers
629
630 class AutoRenderTarget : ::GrNoncopyable {
631 public:
632 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
633 fContext = NULL;
634 fPrevTarget = context->getRenderTarget();
635 if (fPrevTarget != target) {
636 context->setRenderTarget(target);
637 fContext = context;
638 }
639 }
640 ~AutoRenderTarget() {
641 if (fContext) {
642 fContext->setRenderTarget(fPrevTarget);
643 }
644 }
645 private:
646 GrContext* fContext;
647 GrRenderTarget* fPrevTarget;
648 };
649
650
651 ///////////////////////////////////////////////////////////////////////////
652 // Functions intended for internal use only.
653 GrGpu* getGpu() { return fGpu; }
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000654 const GrGpu* getGpu() const { return fGpu; }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000655 GrFontCache* getFontCache() { return fFontCache; }
656 GrDrawTarget* getTextTarget(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000657 const GrIndexBuffer* getQuadIndexBuffer() const;
bsalomon@google.com9923c2b2012-06-06 18:21:18 +0000658
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000659 /**
660 * Stencil buffers add themselves to the cache using
661 * addAndLockStencilBuffer. When a SB's RT-attachment count
662 * reaches zero the SB unlocks itself using unlockStencilBuffer and is
663 * eligible for purging. findStencilBuffer is called to check the cache for
664 * a SB that matching an RT's criteria. If a match is found that has been
665 * unlocked (its attachment count has reached 0) then it will be relocked.
666 */
667 GrResourceEntry* addAndLockStencilBuffer(GrStencilBuffer* sb);
668 void unlockStencilBuffer(GrResourceEntry* sbEntry);
669 GrStencilBuffer* findStencilBuffer(int width, int height, int sampleCnt);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000671 /*
672 * postClipPush acts as a hint to this and lower-level classes (e.g.,
673 * GrGpu) that the clip stack has changed.
674 */
675 virtual void postClipPush();
676
677 /*
678 * preClipPop acts as a hint that the clip stack has been restored to an
679 * earlier state.
680 */
681 virtual void preClipPop();
682
robertphillips@google.com2c756812012-05-22 20:28:23 +0000683 GrPathRenderer* getPathRenderer(const SkPath& path,
684 GrPathFill fill,
685 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +0000686 bool antiAlias,
687 bool allowSW);
robertphillips@google.com2c756812012-05-22 20:28:23 +0000688
bsalomon@google.com27847de2011-02-22 20:59:41 +0000689private:
690 // used to keep track of when we need to flush the draw buffer
691 enum DrawCategory {
692 kBuffered_DrawCategory, // last draw was inserted in draw buffer
693 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
bsalomon@google.com27847de2011-02-22 20:59:41 +0000694 };
695 DrawCategory fLastDrawCategory;
696
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000697 GrGpu* fGpu;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000698 GrDrawState* fDrawState;
699
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000700 GrResourceCache* fTextureCache;
701 GrFontCache* fFontCache;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000702
bsalomon@google.com30085192011-08-19 15:42:31 +0000703 GrPathRendererChain* fPathRendererChain;
robertphillips@google.com72176b22012-05-23 13:19:12 +0000704 GrSoftwarePathRenderer* fSoftwarePathRenderer;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000705
706 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
707 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
708 GrInOrderDrawBuffer* fDrawBuffer;
709
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000710 GrAARectRenderer* fAARectRenderer;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000711
robertphillips@google.comf69a11b2012-06-15 13:58:07 +0000712 GrContext(GrGpu* gpu);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000714 void setupDrawBuffer();
715
bsalomon@google.com27847de2011-02-22 20:59:41 +0000716 void flushDrawBuffer();
717
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000718 void setPaint(const GrPaint& paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000719
bsalomon@google.com27847de2011-02-22 20:59:41 +0000720 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
721
bsalomon@google.com8d033a12012-04-27 15:52:53 +0000722 void internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +0000723 GrPathFill fill, const GrPoint* translate);
724
bsalomon@google.com6f379512011-11-16 20:36:03 +0000725 /**
726 * Flags to the internal read/write pixels funcs
727 */
728 enum PixelOpsFlags {
729 kDontFlush_PixelOpsFlag = 0x1,
730 };
731
732 bool internalReadRenderTargetPixels(GrRenderTarget* target,
733 int left, int top,
734 int width, int height,
735 GrPixelConfig config, void* buffer,
736 size_t rowBytes, uint32_t flags);
737
738 void internalWriteRenderTargetPixels(GrRenderTarget* target,
739 int left, int top,
740 int width, int height,
741 GrPixelConfig, const void* buffer,
742 size_t rowBytes, uint32_t flags);
743
744 bool internalReadTexturePixels(GrTexture* texture,
745 int left, int top,
746 int width, int height,
747 GrPixelConfig config, void* buffer,
748 size_t rowBytes, uint32_t flags);
749
750 void internalWriteTexturePixels(GrTexture* texture,
751 int left, int top,
752 int width, int height,
753 GrPixelConfig config, const void* buffer,
754 size_t rowBytes, uint32_t flags);
755 // needed for access to internalWriteTexturePixels. TODO: make GrContext
756 // be a facade for an internal class. Then functions that are public on the
757 // internal class would have only be callable in src/gpu. The facade would
758 // only have to functions necessary for clients.
759 friend class GrAtlas;
760
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000761 // computes vertex layout bits based on the paint. If paint expresses
762 // a texture for a stage, the stage coords will be bound to postitions
763 // unless hasTexCoords[s]==true in which case stage s's input coords
764 // are bound to tex coord index s. hasTexCoords == NULL is a shortcut
765 // for an array where all the values are false.
766 static int PaintStageVertexLayoutBits(
767 const GrPaint& paint,
768 const bool hasTexCoords[GrPaint::kTotalStages]);
769
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770};
771
772/**
773 * Save/restore the view-matrix in the context.
774 */
775class GrAutoMatrix : GrNoncopyable {
776public:
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000777 GrAutoMatrix() : fContext(NULL) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
779 fMatrix = ctx->getMatrix();
780 }
781 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
782 fMatrix = ctx->getMatrix();
783 ctx->setMatrix(matrix);
784 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000785 void set(GrContext* ctx) {
786 if (NULL != fContext) {
787 fContext->setMatrix(fMatrix);
788 }
789 fMatrix = ctx->getMatrix();
790 fContext = ctx;
791 }
792 void set(GrContext* ctx, const GrMatrix& matrix) {
793 if (NULL != fContext) {
794 fContext->setMatrix(fMatrix);
795 }
796 fMatrix = ctx->getMatrix();
797 ctx->setMatrix(matrix);
798 fContext = ctx;
799 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 ~GrAutoMatrix() {
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000801 if (NULL != fContext) {
802 fContext->setMatrix(fMatrix);
803 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 }
805
806private:
807 GrContext* fContext;
808 GrMatrix fMatrix;
809};
810
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000811/**
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000812 * Gets and locks a scratch texture from a descriptor using
813 * either exact or approximate criteria. Unlocks texture in
814 * the destructor.
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000815 */
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000816class GrAutoScratchTexture : ::GrNoncopyable {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000817public:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000818 GrAutoScratchTexture()
819 : fContext(NULL) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000821
822 GrAutoScratchTexture(GrContext* context,
823 const GrTextureDesc& desc,
824 GrContext::ScratchTexMatch match =
825 GrContext::kApprox_ScratchTexMatch)
826 : fContext(NULL) {
827 this->set(context, desc, match);
828 }
829
830 ~GrAutoScratchTexture() {
831 if (NULL != fContext) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832 fContext->unlockTexture(fEntry);
833 }
834 }
bsalomon@google.com84223112011-07-14 14:45:44 +0000835
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000836 GrTexture* set(GrContext* context,
837 const GrTextureDesc& desc,
838 GrContext::ScratchTexMatch match =
839 GrContext::kApprox_ScratchTexMatch) {
robertphillips@google.comc077d1e2012-05-28 14:10:15 +0000840 if (NULL != fContext) {
841 fContext->unlockTexture(fEntry);
842 fEntry.reset();
843 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000844 fContext = context;
845 if (NULL != fContext) {
846 fEntry = fContext->lockScratchTexture(desc, match);
847 GrTexture* ret = fEntry.texture();
848 if (NULL == ret) {
849 fContext = NULL;
850 }
851 return ret;
852 } else {
853 return NULL;
854 }
855 }
856
857 GrTexture* texture() { return fEntry.texture(); }
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000858private:
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000859 GrContext* fContext;
860 GrContext::TextureCacheEntry fEntry;
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000861};
862
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863#endif