blob: 6b9c3f3009908cbeb2c5fabb10ec117f7f16547e [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.
148 * e.g. in GL platforamRenderTarget is an FBO id.
149 */
150 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
151 int width, int height);
152
153 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000154 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
155 * viewport state from the underlying 3D API and wraps it in a
156 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
157 * underlying object in its destructor and it is up to caller to guarantee
158 * that it remains valid while the GrRenderTarget is used.
159 *
160 * @return the newly created GrRenderTarget
161 */
162 GrRenderTarget* createRenderTargetFrom3DApiState() {
163 return fGpu->createRenderTargetFrom3DApiState();
164 }
165
166 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000167 * Sets the render target.
168 * @param target the render target to set. (should not be NULL.)
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 */
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 void setRenderTarget(GrRenderTarget* target);
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
bsalomon@google.com5782d712011-01-21 21:03:59 +0000172 /**
173 * Gets the current render target.
174 * @return the currently bound render target. Should never be NULL.
175 */
176 const GrRenderTarget* getRenderTarget() const;
177 GrRenderTarget* getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.com5782d712011-01-21 21:03:59 +0000179 ///////////////////////////////////////////////////////////////////////////
180 // Matrix state
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
182 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000183 * Gets the current transformation matrix.
184 * @return the current matrix.
185 */
186 const GrMatrix& getMatrix() const;
187
188 /**
189 * Sets the transformation matrix.
190 * @param m the matrix to set.
191 */
192 void setMatrix(const GrMatrix& m);
193
194 /**
195 * Concats the current matrix. The passed matrix is applied before the
196 * current matrix.
197 * @param m the matrix to concat.
198 */
199 void concatMatrix(const GrMatrix& m) const;
200
201
202 ///////////////////////////////////////////////////////////////////////////
203 // Clip state
204 /**
205 * Gets the current clip.
206 * @return the current clip.
207 */
208 const GrClip& getClip() const { return fGpu->getClip(); }
209
210 /**
211 * Sets the clip.
212 * @param clip the clip to set.
213 */
214 void setClip(const GrClip& clip);
215
216 /**
217 * Convenience method for setting the clip to a rect.
218 * @param rect the rect to set as the new clip.
219 */
220 void setClip(const GrIRect& rect);
221
222 ///////////////////////////////////////////////////////////////////////////
223 // Draws
224
225 /**
226 * Erase the entire render target, ignoring any clips
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 */
228 void eraseColor(GrColor color);
229
230 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000231 * Draw everywhere (respecting the clip) with the paint.
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000233 void drawPaint(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
235 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000236 * Draw the rect using a paint.
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000237 * @param paint describes how to color pixels.
238 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
239 * the rect is mitered stroked based on strokeWidth. If
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000240 * strokeWidth == 0, then the stroke is always a single
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000241 * pixel thick.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000242 * @param matrix Optional matrix applied to the rect. Applied before
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000243 * context's matrix or the paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000244 * The rects coords are used to access the paint (through texture matrix)
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000246 void drawRect(const GrPaint& paint,
247 const GrRect&,
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000248 GrScalar strokeWidth = -1,
249 const GrMatrix* matrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
bsalomon@google.com5782d712011-01-21 21:03:59 +0000251 /**
252 * Maps a rect of paint coordinates onto the a rect of destination
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000253 * coordinates. Each rect can optionally be transformed. The srcRect
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000254 * is stretched over the dstRect. The dstRect is transformed by the
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000255 * context's matrix and the srcRect is transformed by the paint's matrix.
256 * Additional optional matrices can be provided by parameters.
257 *
258 * @param paint describes how to color pixels.
259 * @param dstRect the destination rect to draw.
260 * @param srcRect rect of paint coordinates to be mapped onto dstRect
261 * @param dstMatrix Optional matrix to transform dstRect. Applied before
262 * context's matrix.
263 * @param srcMatrix Optional matrix to transform srcRect Applied before
264 * paint's matrix.
bsalomon@google.com5782d712011-01-21 21:03:59 +0000265 */
266 void drawRectToRect(const GrPaint& paint,
267 const GrRect& dstRect,
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000268 const GrRect& srcRect,
269 const GrMatrix* dstMatrix = NULL,
270 const GrMatrix* srcMatrix = NULL);
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
272 /**
273 * Path filling rules
274 */
275 enum PathFills {
276 kWinding_PathFill,
277 kEvenOdd_PathFill,
278 kInverseWinding_PathFill,
279 kInverseEvenOdd_PathFill,
280 kHairLine_PathFill,
281
282 kPathFillCount
283 };
284
285 /**
286 * Tessellates and draws a path.
287 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000288 * @param paint describes how to color pixels.
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 * @param path the path to draw
bsalomon@google.com5782d712011-01-21 21:03:59 +0000290 * @param fill the path filling rule to use.
291 * @param translate optional additional translation applied to the
292 * path.
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000294 void drawPath(const GrPaint& paint,
295 GrPathIter* path,
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 PathFills fill,
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 const GrPoint* translate = NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000298 /**
299 * Draws vertices with a paint.
300 *
301 * @param paint describes how to color pixels.
302 * @param primitiveType primitives type to draw.
303 * @param vertexCount number of vertices.
304 * @param positions array of vertex positions, required.
305 * @param texCoords optional array of texture coordinates used
306 * to access the paint.
307 * @param colors optional array of per-vertex colors, supercedes
308 * the paint's color field.
309 * @param indices optional array of indices. If NULL vertices
310 * are drawn non-indexed.
311 * @param indexCount if indices is non-null then this is the
312 * number of indices.
313 */
314 void drawVertices(const GrPaint& paint,
315 GrDrawTarget::PrimitiveType primitiveType,
316 int vertexCount,
317 const GrPoint positions[],
318 const GrPoint texs[],
319 const GrColor colors[],
320 const uint16_t indices[],
321 int indexCount);
322
323 /**
324 * Similar to drawVertices but caller provides objects that convert to Gr
325 * types. The count of vertices is given by posSrc.
326 *
327 * @param paint describes how to color pixels.
328 * @param primitiveType primitives type to draw.
329 * @param posSrc Source of vertex positions. Must implement
330 * int count() const;
331 * void writeValue(int i, GrPoint* point) const;
332 * count returns the total number of vertices and
333 * writeValue writes a vertex position to point.
334 * @param texSrc optional, pass NULL to not use explicit tex
335 * coords. If present provides tex coords with
336 * method:
337 * void writeValue(int i, GrPoint* point) const;
338 * @param texSrc optional, pass NULL to not use per-vertex colors
339 * If present provides colors with method:
340 * void writeValue(int i, GrColor* point) const;
341 * @param indices optional, pass NULL for non-indexed drawing. If
342 * present supplies indices for indexed drawing
343 * with following methods:
344 * int count() const;
345 * void writeValue(int i, uint16_t* point) const;
346 * count returns the number of indices and
347 * writeValue supplies each index.
348 */
349 template <typename POS_SRC,
350 typename TEX_SRC,
351 typename COL_SRC,
352 typename IDX_SRC>
353 void drawCustomVertices(const GrPaint& paint,
354 GrDrawTarget::PrimitiveType primitiveType,
355 const POS_SRC& posSrc,
356 const TEX_SRC* texCoordSrc,
357 const COL_SRC* colorSrc,
358 const IDX_SRC* idxSrc);
359 /**
360 * To avoid the problem of having to create a typename for NULL parameters,
361 * these reduced versions of drawCustomVertices are provided.
362 */
363 template <typename POS_SRC>
364 void drawCustomVertices(const GrPaint& paint,
365 GrDrawTarget::PrimitiveType primitiveType,
366 const POS_SRC& posSrc);
367 template <typename POS_SRC, typename TEX_SRC>
368 void drawCustomVertices(const GrPaint& paint,
369 GrDrawTarget::PrimitiveType primitiveType,
370 const POS_SRC& posSrc,
371 const TEX_SRC* texCoordSrc);
372 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
373 void drawCustomVertices(const GrPaint& paint,
374 GrDrawTarget::PrimitiveType primitiveType,
375 const POS_SRC& posSrc,
376 const TEX_SRC* texCoordSrc,
377 const COL_SRC* colorSrc);
378
379
380 ///////////////////////////////////////////////////////////////////////////
381 // Misc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000382
383 /**
384 * Call to ensure all drawing to the context has been issued to the
385 * underlying 3D API.
386 * if flushRenderTarget is true then after the call the last
387 * rendertarget set will be current in the underlying 3D API, otherwise
388 * it may not be. It is useful to set if the caller plans to use the 3D
389 * context outside of Ganesh to render into the current RT.
390 */
391 void flush(bool flushRenderTarget);
392
393 /**
394 * Return true on success, i.e. if we could copy the specified range of
395 * pixels from the current render-target into the buffer, converting into
396 * the specified pixel-config.
397 */
398 bool readPixels(int left, int top, int width, int height,
399 GrTexture::PixelConfig, void* buffer);
400
401 /**
402 * Copy the src pixels [buffer, stride, pixelconfig] into the current
403 * render-target at the specified rectangle.
404 */
405 void writePixels(int left, int top, int width, int height,
406 GrTexture::PixelConfig, const void* buffer, size_t stride);
407
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
bsalomon@google.com5782d712011-01-21 21:03:59 +0000409 ///////////////////////////////////////////////////////////////////////////
410 // Statistics
reed@google.comac10a2d2010-12-22 21:39:39 +0000411
412 void resetStats();
413
414 const GrGpu::Stats& getStats() const;
415
416 void printStats() const;
417
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418 ///////////////////////////////////////////////////////////////////////////
419 // Helpers
420
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 class AutoRenderTarget : ::GrNoncopyable {
422 public:
423 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
424 fContext = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000425 fPrevTarget = context->getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 if (fPrevTarget != target) {
427 context->setRenderTarget(target);
428 fContext = context;
429 }
430 }
431 ~AutoRenderTarget() {
432 if (fContext) {
433 fContext->setRenderTarget(fPrevTarget);
434 }
435 }
436 private:
437 GrContext* fContext;
438 GrRenderTarget* fPrevTarget;
439 };
440
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
reed@google.com01804b42011-01-18 21:50:41 +0000442 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443 // Functions intended for internal use only.
reed@google.comac10a2d2010-12-22 21:39:39 +0000444 GrGpu* getGpu() { return fGpu; }
445 GrFontCache* getFontCache() { return fFontCache; }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446 GrDrawTarget* getTextTarget(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 void flushText();
448
449 const GrIndexBuffer* quadIndexBuffer() const;
450 int maxQuadsInIndexBuffer() const;
451
452private:
453 GrGpu* fGpu;
454 GrTextureCache* fTextureCache;
455 GrFontCache* fFontCache;
456
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000457 GrVertexBufferAllocPool* fTextVBAllocPool;
458 GrIndexBufferAllocPool* fTextIBAllocPool;
459 GrInOrderDrawBuffer* fTextDrawBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
461 GrContext(GrGpu* gpu);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462
463 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
464
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 void prepareToDraw(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
468 void drawClipIntoStencil();
469};
470
471/**
472 * Save/restore the view-matrix in the context.
473 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474class GrAutoMatrix : GrNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000475public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
477 fMatrix = ctx->getMatrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000479 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
480 fMatrix = ctx->getMatrix();
481 ctx->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483 ~GrAutoMatrix() {
484 fContext->setMatrix(fMatrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 }
486
487private:
488 GrContext* fContext;
489 GrMatrix fMatrix;
490};
491
492#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000493
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494#include "GrContext_impl.h"