blob: ccc045e73cba053944d9206f8f402a5d6b9b37d8 [file] [log] [blame]
reed@google.com873cb1e2010-12-23 15:00:45 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#ifndef GrContext_DEFINED
18#define GrContext_DEFINED
19
20#include "GrClip.h"
21#include "GrGpu.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022#include "GrTextureCache.h"
23#include "GrInOrderDrawBuffer.h"
24#include "GrVertexBufferAllocPool.h"
bsalomon@google.com5782d712011-01-21 21:03:59 +000025#include "GrPaint.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27class GrFontCache;
28class GrPathIter;
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030class GrContext : public GrRefCnt {
31public:
32 /**
33 * Creates a GrContext from within a 3D context.
34 */
35 static GrContext* Create(GrGpu::Engine engine,
36 GrGpu::Platform3DContext context3D);
37
reed@google.com873cb1e2010-12-23 15:00:45 +000038 /**
39 * Helper to create a opengl-shader based context
40 */
41 static GrContext* CreateGLShaderContext();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000042
reed@google.comac10a2d2010-12-22 21:39:39 +000043 virtual ~GrContext();
44
45 /**
46 * The GrContext normally assumes that no outsider is setting state
47 * within the underlying 3D API's context/device/whatever. This call informs
48 * the context that the state was modified and it should resend. Shouldn't
49 * be called frequently for good performance.
50 */
51 void resetContext();
52
bsalomon@google.com5782d712011-01-21 21:03:59 +000053 ///////////////////////////////////////////////////////////////////////////
54 // Textures
55
reed@google.comac10a2d2010-12-22 21:39:39 +000056 /**
57 * Abandons all textures. Call this if you have lost the associated GPU
58 * context, and thus internal texture references/IDs are now invalid.
59 */
60 void abandonAllTextures();
61
62 /**
63 * Search for an entry with the same Key. If found, "lock" it and return it.
64 * If not found, return null.
65 */
66 GrTextureEntry* findAndLockTexture(GrTextureKey*,
67 const GrSamplerState&);
68
69
70 /**
71 * Create a new entry, based on the specified key and texture, and return
72 * its "locked" entry.
73 *
74 * Ownership of the texture is transferred to the Entry, which will unref()
75 * it when we are purged or deleted.
76 */
77 GrTextureEntry* createAndLockTexture(GrTextureKey* key,
78 const GrSamplerState&,
79 const GrGpu::TextureDesc&,
80 void* srcData, size_t rowBytes);
81
82 /**
83 * When done with an entry, call unlockTexture(entry) on it, which returns
84 * it to the cache, where it may be purged.
85 */
86 void unlockTexture(GrTextureEntry* entry);
87
88 /**
89 * Removes an texture from the cache. This prevents the texture from
90 * being found by a subsequent findAndLockTexture() until it is
91 * reattached. The entry still counts against the cache's budget and should
92 * be reattached when exclusive access is no longer needed.
93 */
94 void detachCachedTexture(GrTextureEntry*);
95
96 /**
97 * Reattaches a texture to the cache and unlocks it. Allows it to be found
98 * by a subsequent findAndLock or be purged (provided its lock count is
99 * now 0.)
100 */
101 void reattachAndUnlockCachedTexture(GrTextureEntry*);
102
103 /**
104 * Creates a texture that is outside the cache. Does not count against
105 * cache's budget.
106 */
107 GrTexture* createUncachedTexture(const GrGpu::TextureDesc&,
108 void* srcData,
109 size_t rowBytes);
110
111 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000112 * Returns true if the specified use of an indexed texture is supported.
113 */
114 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
115
116 /**
117 * Return the current texture cache limits.
118 *
119 * @param maxTextures If non-null, returns maximum number of textures that
120 * can be held in the cache.
121 * @param maxTextureBytes If non-null, returns maximum number of bytes of
122 * texture memory that can be held in the cache.
123 */
124 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
125
126 /**
127 * Specify the texture cache limits. If the current cache exceeds either
128 * of these, it will be purged (LRU) to keep the cache within these limits.
129 *
130 * @param maxTextures The maximum number of textures that can be held in
131 * the cache.
132 * @param maxTextureBytes The maximum number of bytes of texture memory
133 * that can be held in the cache.
134 */
135 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
136
reed@google.com02a7e6c2011-01-28 21:21:49 +0000137 /**
138 * Return the max width or height of a texture supported by the current gpu
139 */
140 int getMaxTextureDimension();
141
bsalomon@google.com5782d712011-01-21 21:03:59 +0000142 ///////////////////////////////////////////////////////////////////////////
143 // Render targets
144
145 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 * Wraps an externally-created rendertarget in a GrRenderTarget.
147 * e.g. in GL platforamRenderTarget is an FBO id.
148 */
149 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
150 int width, int height);
151
152 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000153 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
154 * viewport state from the underlying 3D API and wraps it in a
155 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
156 * underlying object in its destructor and it is up to caller to guarantee
157 * that it remains valid while the GrRenderTarget is used.
158 *
159 * @return the newly created GrRenderTarget
160 */
161 GrRenderTarget* createRenderTargetFrom3DApiState() {
162 return fGpu->createRenderTargetFrom3DApiState();
163 }
164
165 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000166 * Sets the render target.
167 * @param target the render target to set. (should not be NULL.)
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 void setRenderTarget(GrRenderTarget* target);
reed@google.comac10a2d2010-12-22 21:39:39 +0000170
bsalomon@google.com5782d712011-01-21 21:03:59 +0000171 /**
172 * Gets the current render target.
173 * @return the currently bound render target. Should never be NULL.
174 */
175 const GrRenderTarget* getRenderTarget() const;
176 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
bsalomon@google.com5782d712011-01-21 21:03:59 +0000178 ///////////////////////////////////////////////////////////////////////////
179 // Matrix state
reed@google.comac10a2d2010-12-22 21:39:39 +0000180
181 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182 * Gets the current transformation matrix.
183 * @return the current matrix.
184 */
185 const GrMatrix& getMatrix() const;
186
187 /**
188 * Sets the transformation matrix.
189 * @param m the matrix to set.
190 */
191 void setMatrix(const GrMatrix& m);
192
193 /**
194 * Concats the current matrix. The passed matrix is applied before the
195 * current matrix.
196 * @param m the matrix to concat.
197 */
198 void concatMatrix(const GrMatrix& m) const;
199
200
201 ///////////////////////////////////////////////////////////////////////////
202 // Clip state
203 /**
204 * Gets the current clip.
205 * @return the current clip.
206 */
207 const GrClip& getClip() const { return fGpu->getClip(); }
208
209 /**
210 * Sets the clip.
211 * @param clip the clip to set.
212 */
213 void setClip(const GrClip& clip);
214
215 /**
216 * Convenience method for setting the clip to a rect.
217 * @param rect the rect to set as the new clip.
218 */
219 void setClip(const GrIRect& rect);
220
221 ///////////////////////////////////////////////////////////////////////////
222 // Draws
223
224 /**
225 * Erase the entire render target, ignoring any clips
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 */
227 void eraseColor(GrColor color);
228
229 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000230 * Draw everywhere (respecting the clip) with the paint.
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000232 void drawPaint(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000235 * Draw the rect using a paint.
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000236 * @param paint describes how to color pixels.
237 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
238 * the rect is mitered stroked based on strokeWidth. If
239 * strokeWidth == 0, then the stroke is always a single
240 * pixel thick.
241 * @param matrix Optional matrix applied to the rect. Applied before
242 * context's matrix or the paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243 * The rects coords are used to access the paint (through texture matrix)
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 */
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000245 void drawRect(const GrPaint& paint,
246 const GrRect&,
247 GrScalar strokeWidth = -1,
248 const GrMatrix* matrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
bsalomon@google.com5782d712011-01-21 21:03:59 +0000250 /**
251 * Maps a rect of paint coordinates onto the a rect of destination
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000252 * coordinates. Each rect can optionally be transformed. The srcRect
253 * is stretched over the dstRect. The dstRect is transformed by the
254 * context's matrix and the srcRect is transformed by the paint's matrix.
255 * Additional optional matrices can be provided by parameters.
256 *
257 * @param paint describes how to color pixels.
258 * @param dstRect the destination rect to draw.
259 * @param srcRect rect of paint coordinates to be mapped onto dstRect
260 * @param dstMatrix Optional matrix to transform dstRect. Applied before
261 * context's matrix.
262 * @param srcMatrix Optional matrix to transform srcRect Applied before
263 * paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000264 */
265 void drawRectToRect(const GrPaint& paint,
266 const GrRect& dstRect,
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000267 const GrRect& srcRect,
268 const GrMatrix* dstMatrix = NULL,
269 const GrMatrix* srcMatrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 /**
272 * Path filling rules
273 */
274 enum PathFills {
275 kWinding_PathFill,
276 kEvenOdd_PathFill,
277 kInverseWinding_PathFill,
278 kInverseEvenOdd_PathFill,
279 kHairLine_PathFill,
280
281 kPathFillCount
282 };
283
284 /**
285 * Tessellates and draws a path.
286 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000287 * @param paint describes how to color pixels.
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 * @param path the path to draw
bsalomon@google.com5782d712011-01-21 21:03:59 +0000289 * @param fill the path filling rule to use.
290 * @param translate optional additional translation applied to the
291 * path.
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000293 void drawPath(const GrPaint& paint,
294 GrPathIter* path,
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 PathFills fill,
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 const GrPoint* translate = NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000297 /**
298 * Draws vertices with a paint.
299 *
300 * @param paint describes how to color pixels.
301 * @param primitiveType primitives type to draw.
302 * @param vertexCount number of vertices.
303 * @param positions array of vertex positions, required.
304 * @param texCoords optional array of texture coordinates used
305 * to access the paint.
306 * @param colors optional array of per-vertex colors, supercedes
307 * the paint's color field.
308 * @param indices optional array of indices. If NULL vertices
309 * are drawn non-indexed.
310 * @param indexCount if indices is non-null then this is the
311 * number of indices.
312 */
313 void drawVertices(const GrPaint& paint,
314 GrDrawTarget::PrimitiveType primitiveType,
315 int vertexCount,
316 const GrPoint positions[],
317 const GrPoint texs[],
318 const GrColor colors[],
319 const uint16_t indices[],
320 int indexCount);
321
322 /**
323 * Similar to drawVertices but caller provides objects that convert to Gr
324 * types. The count of vertices is given by posSrc.
325 *
326 * @param paint describes how to color pixels.
327 * @param primitiveType primitives type to draw.
328 * @param posSrc Source of vertex positions. Must implement
329 * int count() const;
330 * void writeValue(int i, GrPoint* point) const;
331 * count returns the total number of vertices and
332 * writeValue writes a vertex position to point.
333 * @param texSrc optional, pass NULL to not use explicit tex
334 * coords. If present provides tex coords with
335 * method:
336 * void writeValue(int i, GrPoint* point) const;
337 * @param texSrc optional, pass NULL to not use per-vertex colors
338 * If present provides colors with method:
339 * void writeValue(int i, GrColor* point) const;
340 * @param indices optional, pass NULL for non-indexed drawing. If
341 * present supplies indices for indexed drawing
342 * with following methods:
343 * int count() const;
344 * void writeValue(int i, uint16_t* point) const;
345 * count returns the number of indices and
346 * writeValue supplies each index.
347 */
348 template <typename POS_SRC,
349 typename TEX_SRC,
350 typename COL_SRC,
351 typename IDX_SRC>
352 void drawCustomVertices(const GrPaint& paint,
353 GrDrawTarget::PrimitiveType primitiveType,
354 const POS_SRC& posSrc,
355 const TEX_SRC* texCoordSrc,
356 const COL_SRC* colorSrc,
357 const IDX_SRC* idxSrc);
358 /**
359 * To avoid the problem of having to create a typename for NULL parameters,
360 * these reduced versions of drawCustomVertices are provided.
361 */
362 template <typename POS_SRC>
363 void drawCustomVertices(const GrPaint& paint,
364 GrDrawTarget::PrimitiveType primitiveType,
365 const POS_SRC& posSrc);
366 template <typename POS_SRC, typename TEX_SRC>
367 void drawCustomVertices(const GrPaint& paint,
368 GrDrawTarget::PrimitiveType primitiveType,
369 const POS_SRC& posSrc,
370 const TEX_SRC* texCoordSrc);
371 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
372 void drawCustomVertices(const GrPaint& paint,
373 GrDrawTarget::PrimitiveType primitiveType,
374 const POS_SRC& posSrc,
375 const TEX_SRC* texCoordSrc,
376 const COL_SRC* colorSrc);
377
378
379 ///////////////////////////////////////////////////////////////////////////
380 // Misc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 /**
383 * Call to ensure all drawing to the context has been issued to the
384 * underlying 3D API.
385 * if flushRenderTarget is true then after the call the last
386 * rendertarget set will be current in the underlying 3D API, otherwise
387 * it may not be. It is useful to set if the caller plans to use the 3D
388 * context outside of Ganesh to render into the current RT.
389 */
390 void flush(bool flushRenderTarget);
391
392 /**
393 * Return true on success, i.e. if we could copy the specified range of
394 * pixels from the current render-target into the buffer, converting into
395 * the specified pixel-config.
396 */
397 bool readPixels(int left, int top, int width, int height,
398 GrTexture::PixelConfig, void* buffer);
399
400 /**
401 * Copy the src pixels [buffer, stride, pixelconfig] into the current
402 * render-target at the specified rectangle.
403 */
404 void writePixels(int left, int top, int width, int height,
405 GrTexture::PixelConfig, const void* buffer, size_t stride);
406
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.com5782d712011-01-21 21:03:59 +0000408 ///////////////////////////////////////////////////////////////////////////
409 // Statistics
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
411 void resetStats();
412
413 const GrGpu::Stats& getStats() const;
414
415 void printStats() const;
416
bsalomon@google.com5782d712011-01-21 21:03:59 +0000417 ///////////////////////////////////////////////////////////////////////////
418 // Helpers
419
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 class AutoRenderTarget : ::GrNoncopyable {
421 public:
422 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
423 fContext = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424 fPrevTarget = context->getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000425 if (fPrevTarget != target) {
426 context->setRenderTarget(target);
427 fContext = context;
428 }
429 }
430 ~AutoRenderTarget() {
431 if (fContext) {
432 fContext->setRenderTarget(fPrevTarget);
433 }
434 }
435 private:
436 GrContext* fContext;
437 GrRenderTarget* fPrevTarget;
438 };
439
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440
reed@google.com01804b42011-01-18 21:50:41 +0000441 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442 // Functions intended for internal use only.
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 GrGpu* getGpu() { return fGpu; }
444 GrFontCache* getFontCache() { return fFontCache; }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000445 GrDrawTarget* getTextTarget(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 void flushText();
447
448 const GrIndexBuffer* quadIndexBuffer() const;
449 int maxQuadsInIndexBuffer() const;
450
451private:
452 GrGpu* fGpu;
453 GrTextureCache* fTextureCache;
454 GrFontCache* fFontCache;
455
456 GrVertexBufferAllocPool fVBAllocPool;
457 GrInOrderDrawBuffer fTextDrawBuffer;
458
459 GrContext(GrGpu* gpu);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460
461 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
462
reed@google.comac10a2d2010-12-22 21:39:39 +0000463 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 void prepareToDraw(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
466 void drawClipIntoStencil();
467};
468
469/**
470 * Save/restore the view-matrix in the context.
471 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472class GrAutoMatrix : GrNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000473public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
475 fMatrix = ctx->getMatrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000477 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
478 fMatrix = ctx->getMatrix();
479 ctx->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 ~GrAutoMatrix() {
482 fContext->setMatrix(fMatrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 }
484
485private:
486 GrContext* fContext;
487 GrMatrix fMatrix;
488};
489
490#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000491
bsalomon@google.com5782d712011-01-21 21:03:59 +0000492#include "GrContext_impl.h"