blob: 93c012c33bd858574aba99e2fd34474e01677265 [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"
bsalomon@google.com5782d712011-01-21 21:03:59 +000023#include "GrPaint.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25class GrFontCache;
26class GrPathIter;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027class GrVertexBufferAllocPool;
28class GrIndexBufferAllocPool;
29class GrInOrderDrawBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000030
reed@google.comac10a2d2010-12-22 21:39:39 +000031class GrContext : public GrRefCnt {
32public:
33 /**
34 * Creates a GrContext from within a 3D context.
35 */
36 static GrContext* Create(GrGpu::Engine engine,
37 GrGpu::Platform3DContext context3D);
38
reed@google.com873cb1e2010-12-23 15:00:45 +000039 /**
40 * Helper to create a opengl-shader based context
41 */
42 static GrContext* CreateGLShaderContext();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044 virtual ~GrContext();
45
46 /**
47 * The GrContext normally assumes that no outsider is setting state
48 * within the underlying 3D API's context/device/whatever. This call informs
49 * the context that the state was modified and it should resend. Shouldn't
50 * be called frequently for good performance.
51 */
52 void resetContext();
53
bsalomon@google.com5782d712011-01-21 21:03:59 +000054 ///////////////////////////////////////////////////////////////////////////
55 // Textures
56
reed@google.comac10a2d2010-12-22 21:39:39 +000057 /**
58 * Abandons all textures. Call this if you have lost the associated GPU
59 * context, and thus internal texture references/IDs are now invalid.
60 */
61 void abandonAllTextures();
62
63 /**
64 * Search for an entry with the same Key. If found, "lock" it and return it.
65 * If not found, return null.
66 */
67 GrTextureEntry* findAndLockTexture(GrTextureKey*,
68 const GrSamplerState&);
69
70
71 /**
72 * Create a new entry, based on the specified key and texture, and return
73 * its "locked" entry.
74 *
75 * Ownership of the texture is transferred to the Entry, which will unref()
76 * it when we are purged or deleted.
77 */
78 GrTextureEntry* createAndLockTexture(GrTextureKey* key,
79 const GrSamplerState&,
80 const GrGpu::TextureDesc&,
81 void* srcData, size_t rowBytes);
82
83 /**
84 * When done with an entry, call unlockTexture(entry) on it, which returns
85 * it to the cache, where it may be purged.
86 */
87 void unlockTexture(GrTextureEntry* entry);
88
89 /**
90 * Removes an texture from the cache. This prevents the texture from
91 * being found by a subsequent findAndLockTexture() until it is
92 * reattached. The entry still counts against the cache's budget and should
93 * be reattached when exclusive access is no longer needed.
94 */
95 void detachCachedTexture(GrTextureEntry*);
96
97 /**
98 * Reattaches a texture to the cache and unlocks it. Allows it to be found
99 * by a subsequent findAndLock or be purged (provided its lock count is
100 * now 0.)
101 */
102 void reattachAndUnlockCachedTexture(GrTextureEntry*);
103
104 /**
105 * Creates a texture that is outside the cache. Does not count against
106 * cache's budget.
107 */
108 GrTexture* createUncachedTexture(const GrGpu::TextureDesc&,
109 void* srcData,
110 size_t rowBytes);
111
112 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000113 * Returns true if the specified use of an indexed texture is supported.
114 */
115 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
116
117 /**
118 * Return the current texture cache limits.
119 *
120 * @param maxTextures If non-null, returns maximum number of textures that
121 * can be held in the cache.
122 * @param maxTextureBytes If non-null, returns maximum number of bytes of
123 * texture memory that can be held in the cache.
124 */
125 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
126
127 /**
128 * Specify the texture cache limits. If the current cache exceeds either
129 * of these, it will be purged (LRU) to keep the cache within these limits.
130 *
131 * @param maxTextures The maximum number of textures that can be held in
132 * the cache.
133 * @param maxTextureBytes The maximum number of bytes of texture memory
134 * that can be held in the cache.
135 */
136 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
137
reed@google.com02a7e6c2011-01-28 21:21:49 +0000138 /**
139 * Return the max width or height of a texture supported by the current gpu
140 */
141 int getMaxTextureDimension();
142
bsalomon@google.com5782d712011-01-21 21:03:59 +0000143 ///////////////////////////////////////////////////////////////////////////
144 // Render targets
145
146 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 * Wraps an externally-created rendertarget in a GrRenderTarget.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000148 * @param platformRenderTarget 3D API-specific render target identifier
149 * e.g. in GL platforamRenderTarget is an FBO
150 * id.
151 * @param stencilBits the number of stencil bits that the render
152 * target has.
153 * @param width width of the render target.
154 * @param height height of the render target.
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 */
156 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000157 int stencilBits,
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 int width, int height);
159
160 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000161 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
162 * viewport state from the underlying 3D API and wraps it in a
163 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
164 * underlying object in its destructor and it is up to caller to guarantee
165 * that it remains valid while the GrRenderTarget is used.
166 *
167 * @return the newly created GrRenderTarget
168 */
169 GrRenderTarget* createRenderTargetFrom3DApiState() {
170 return fGpu->createRenderTargetFrom3DApiState();
171 }
172
173 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000174 * Sets the render target.
175 * @param target the render target to set. (should not be NULL.)
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 void setRenderTarget(GrRenderTarget* target);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.com5782d712011-01-21 21:03:59 +0000179 /**
180 * Gets the current render target.
181 * @return the currently bound render target. Should never be NULL.
182 */
183 const GrRenderTarget* getRenderTarget() const;
184 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.com5782d712011-01-21 21:03:59 +0000186 ///////////////////////////////////////////////////////////////////////////
187 // Matrix state
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
189 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000190 * Gets the current transformation matrix.
191 * @return the current matrix.
192 */
193 const GrMatrix& getMatrix() const;
194
195 /**
196 * Sets the transformation matrix.
197 * @param m the matrix to set.
198 */
199 void setMatrix(const GrMatrix& m);
200
201 /**
202 * Concats the current matrix. The passed matrix is applied before the
203 * current matrix.
204 * @param m the matrix to concat.
205 */
206 void concatMatrix(const GrMatrix& m) const;
207
208
209 ///////////////////////////////////////////////////////////////////////////
210 // Clip state
211 /**
212 * Gets the current clip.
213 * @return the current clip.
214 */
215 const GrClip& getClip() const { return fGpu->getClip(); }
216
217 /**
218 * Sets the clip.
219 * @param clip the clip to set.
220 */
221 void setClip(const GrClip& clip);
222
223 /**
224 * Convenience method for setting the clip to a rect.
225 * @param rect the rect to set as the new clip.
226 */
227 void setClip(const GrIRect& rect);
228
229 ///////////////////////////////////////////////////////////////////////////
230 // Draws
231
232 /**
233 * Erase the entire render target, ignoring any clips
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 */
235 void eraseColor(GrColor color);
236
237 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000238 * Draw everywhere (respecting the clip) with the paint.
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000240 void drawPaint(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
242 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243 * Draw the rect using a paint.
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000244 * @param paint describes how to color pixels.
245 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
246 * the rect is mitered stroked based on strokeWidth. If
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000247 * strokeWidth == 0, then the stroke is always a single
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000248 * pixel thick.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000249 * @param matrix Optional matrix applied to the rect. Applied before
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000250 * context's matrix or the paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000251 * The rects coords are used to access the paint (through texture matrix)
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000253 void drawRect(const GrPaint& paint,
254 const GrRect&,
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000255 GrScalar strokeWidth = -1,
256 const GrMatrix* matrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000257
bsalomon@google.com5782d712011-01-21 21:03:59 +0000258 /**
259 * Maps a rect of paint coordinates onto the a rect of destination
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000260 * coordinates. Each rect can optionally be transformed. The srcRect
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000261 * is stretched over the dstRect. The dstRect is transformed by the
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000262 * context's matrix and the srcRect is transformed by the paint's matrix.
263 * Additional optional matrices can be provided by parameters.
264 *
265 * @param paint describes how to color pixels.
266 * @param dstRect the destination rect to draw.
267 * @param srcRect rect of paint coordinates to be mapped onto dstRect
268 * @param dstMatrix Optional matrix to transform dstRect. Applied before
269 * context's matrix.
270 * @param srcMatrix Optional matrix to transform srcRect Applied before
271 * paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000272 */
273 void drawRectToRect(const GrPaint& paint,
274 const GrRect& dstRect,
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000275 const GrRect& srcRect,
276 const GrMatrix* dstMatrix = NULL,
277 const GrMatrix* srcMatrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
279 /**
280 * Path filling rules
281 */
282 enum PathFills {
283 kWinding_PathFill,
284 kEvenOdd_PathFill,
285 kInverseWinding_PathFill,
286 kInverseEvenOdd_PathFill,
287 kHairLine_PathFill,
288
289 kPathFillCount
290 };
291
292 /**
293 * Tessellates and draws a path.
294 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000295 * @param paint describes how to color pixels.
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 * @param path the path to draw
bsalomon@google.com5782d712011-01-21 21:03:59 +0000297 * @param fill the path filling rule to use.
298 * @param translate optional additional translation applied to the
299 * path.
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000301 void drawPath(const GrPaint& paint,
302 GrPathIter* path,
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 PathFills fill,
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 const GrPoint* translate = NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000305 /**
306 * Draws vertices with a paint.
307 *
308 * @param paint describes how to color pixels.
309 * @param primitiveType primitives type to draw.
310 * @param vertexCount number of vertices.
311 * @param positions array of vertex positions, required.
312 * @param texCoords optional array of texture coordinates used
313 * to access the paint.
314 * @param colors optional array of per-vertex colors, supercedes
315 * the paint's color field.
316 * @param indices optional array of indices. If NULL vertices
317 * are drawn non-indexed.
318 * @param indexCount if indices is non-null then this is the
319 * number of indices.
320 */
321 void drawVertices(const GrPaint& paint,
322 GrDrawTarget::PrimitiveType primitiveType,
323 int vertexCount,
324 const GrPoint positions[],
325 const GrPoint texs[],
326 const GrColor colors[],
327 const uint16_t indices[],
328 int indexCount);
329
330 /**
331 * Similar to drawVertices but caller provides objects that convert to Gr
332 * types. The count of vertices is given by posSrc.
333 *
334 * @param paint describes how to color pixels.
335 * @param primitiveType primitives type to draw.
336 * @param posSrc Source of vertex positions. Must implement
337 * int count() const;
338 * void writeValue(int i, GrPoint* point) const;
339 * count returns the total number of vertices and
340 * writeValue writes a vertex position to point.
341 * @param texSrc optional, pass NULL to not use explicit tex
342 * coords. If present provides tex coords with
343 * method:
344 * void writeValue(int i, GrPoint* point) const;
345 * @param texSrc optional, pass NULL to not use per-vertex colors
346 * If present provides colors with method:
347 * void writeValue(int i, GrColor* point) const;
348 * @param indices optional, pass NULL for non-indexed drawing. If
349 * present supplies indices for indexed drawing
350 * with following methods:
351 * int count() const;
352 * void writeValue(int i, uint16_t* point) const;
353 * count returns the number of indices and
354 * writeValue supplies each index.
355 */
356 template <typename POS_SRC,
357 typename TEX_SRC,
358 typename COL_SRC,
359 typename IDX_SRC>
360 void drawCustomVertices(const GrPaint& paint,
361 GrDrawTarget::PrimitiveType primitiveType,
362 const POS_SRC& posSrc,
363 const TEX_SRC* texCoordSrc,
364 const COL_SRC* colorSrc,
365 const IDX_SRC* idxSrc);
366 /**
367 * To avoid the problem of having to create a typename for NULL parameters,
368 * these reduced versions of drawCustomVertices are provided.
369 */
370 template <typename POS_SRC>
371 void drawCustomVertices(const GrPaint& paint,
372 GrDrawTarget::PrimitiveType primitiveType,
373 const POS_SRC& posSrc);
374 template <typename POS_SRC, typename TEX_SRC>
375 void drawCustomVertices(const GrPaint& paint,
376 GrDrawTarget::PrimitiveType primitiveType,
377 const POS_SRC& posSrc,
378 const TEX_SRC* texCoordSrc);
379 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
380 void drawCustomVertices(const GrPaint& paint,
381 GrDrawTarget::PrimitiveType primitiveType,
382 const POS_SRC& posSrc,
383 const TEX_SRC* texCoordSrc,
384 const COL_SRC* colorSrc);
385
386
387 ///////////////////////////////////////////////////////////////////////////
388 // Misc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
390 /**
391 * Call to ensure all drawing to the context has been issued to the
392 * underlying 3D API.
393 * if flushRenderTarget is true then after the call the last
394 * rendertarget set will be current in the underlying 3D API, otherwise
395 * it may not be. It is useful to set if the caller plans to use the 3D
396 * context outside of Ganesh to render into the current RT.
397 */
398 void flush(bool flushRenderTarget);
399
400 /**
401 * Return true on success, i.e. if we could copy the specified range of
402 * pixels from the current render-target into the buffer, converting into
403 * the specified pixel-config.
404 */
405 bool readPixels(int left, int top, int width, int height,
406 GrTexture::PixelConfig, void* buffer);
407
408 /**
409 * Copy the src pixels [buffer, stride, pixelconfig] into the current
410 * render-target at the specified rectangle.
411 */
412 void writePixels(int left, int top, int width, int height,
413 GrTexture::PixelConfig, const void* buffer, size_t stride);
414
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416 ///////////////////////////////////////////////////////////////////////////
417 // Statistics
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
419 void resetStats();
420
421 const GrGpu::Stats& getStats() const;
422
423 void printStats() const;
424
bsalomon@google.com5782d712011-01-21 21:03:59 +0000425 ///////////////////////////////////////////////////////////////////////////
426 // Helpers
427
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 class AutoRenderTarget : ::GrNoncopyable {
429 public:
430 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
431 fContext = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 fPrevTarget = context->getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 if (fPrevTarget != target) {
434 context->setRenderTarget(target);
435 fContext = context;
436 }
437 }
438 ~AutoRenderTarget() {
439 if (fContext) {
440 fContext->setRenderTarget(fPrevTarget);
441 }
442 }
443 private:
444 GrContext* fContext;
445 GrRenderTarget* fPrevTarget;
446 };
447
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448
reed@google.com01804b42011-01-18 21:50:41 +0000449 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450 // Functions intended for internal use only.
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 GrGpu* getGpu() { return fGpu; }
452 GrFontCache* getFontCache() { return fFontCache; }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453 GrDrawTarget* getTextTarget(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 void flushText();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000455 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000456
457private:
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000458 // used to keep track of when we need to flush the draw buffer
459 enum DrawCategory {
460 kBuffered_DrawCategory, // last draw was inserted in draw buffer
461 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
462 kText_DrawCategory // text context was last to draw
463 };
464 DrawCategory fLastDrawCategory;
465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 GrGpu* fGpu;
467 GrTextureCache* fTextureCache;
468 GrFontCache* fFontCache;
469
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000470 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
471 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
472 GrInOrderDrawBuffer* fDrawBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
474 GrContext(GrGpu* gpu);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000475 void flushDrawBuffer();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476
477 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
478
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000480
481 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
483 void drawClipIntoStencil();
484};
485
486/**
487 * Save/restore the view-matrix in the context.
488 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489class GrAutoMatrix : GrNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000490public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
492 fMatrix = ctx->getMatrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
495 fMatrix = ctx->getMatrix();
496 ctx->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 ~GrAutoMatrix() {
499 fContext->setMatrix(fMatrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 }
501
502private:
503 GrContext* fContext;
504 GrMatrix fMatrix;
505};
506
507#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000508
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509#include "GrContext_impl.h"