blob: db53b1a859aa297b0d978702c4f3e5aed36588fd [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.
236 * If strokeWidth < 0, then the rect is filled, else the rect is mitered
237 * stroked based on strokeWidth. If strokeWidth == 0, then the stroke is
238 * always a single pixel thick.
239 * The rects coords are used to access the paint (through texture matrix)
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000241 void drawRect(const GrPaint& paint, const GrRect&, GrScalar strokeWidth = -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
bsalomon@google.com5782d712011-01-21 21:03:59 +0000243 /**
244 * Maps a rect of paint coordinates onto the a rect of destination
245 * coordinates. The srcRect is transformed by the paint's matrix and the
246 * dstRect is transformed by the context's matrix.
247 */
248 void drawRectToRect(const GrPaint& paint,
249 const GrRect& dstRect,
250 const GrRect& srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
252 /**
253 * Path filling rules
254 */
255 enum PathFills {
256 kWinding_PathFill,
257 kEvenOdd_PathFill,
258 kInverseWinding_PathFill,
259 kInverseEvenOdd_PathFill,
260 kHairLine_PathFill,
261
262 kPathFillCount
263 };
264
265 /**
266 * Tessellates and draws a path.
267 *
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 * @param paint describes how to color pixels.
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 * @param path the path to draw
bsalomon@google.com5782d712011-01-21 21:03:59 +0000270 * @param fill the path filling rule to use.
271 * @param translate optional additional translation applied to the
272 * path.
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000274 void drawPath(const GrPaint& paint,
275 GrPathIter* path,
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 PathFills fill,
reed@google.comac10a2d2010-12-22 21:39:39 +0000277 const GrPoint* translate = NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000278 /**
279 * Draws vertices with a paint.
280 *
281 * @param paint describes how to color pixels.
282 * @param primitiveType primitives type to draw.
283 * @param vertexCount number of vertices.
284 * @param positions array of vertex positions, required.
285 * @param texCoords optional array of texture coordinates used
286 * to access the paint.
287 * @param colors optional array of per-vertex colors, supercedes
288 * the paint's color field.
289 * @param indices optional array of indices. If NULL vertices
290 * are drawn non-indexed.
291 * @param indexCount if indices is non-null then this is the
292 * number of indices.
293 */
294 void drawVertices(const GrPaint& paint,
295 GrDrawTarget::PrimitiveType primitiveType,
296 int vertexCount,
297 const GrPoint positions[],
298 const GrPoint texs[],
299 const GrColor colors[],
300 const uint16_t indices[],
301 int indexCount);
302
303 /**
304 * Similar to drawVertices but caller provides objects that convert to Gr
305 * types. The count of vertices is given by posSrc.
306 *
307 * @param paint describes how to color pixels.
308 * @param primitiveType primitives type to draw.
309 * @param posSrc Source of vertex positions. Must implement
310 * int count() const;
311 * void writeValue(int i, GrPoint* point) const;
312 * count returns the total number of vertices and
313 * writeValue writes a vertex position to point.
314 * @param texSrc optional, pass NULL to not use explicit tex
315 * coords. If present provides tex coords with
316 * method:
317 * void writeValue(int i, GrPoint* point) const;
318 * @param texSrc optional, pass NULL to not use per-vertex colors
319 * If present provides colors with method:
320 * void writeValue(int i, GrColor* point) const;
321 * @param indices optional, pass NULL for non-indexed drawing. If
322 * present supplies indices for indexed drawing
323 * with following methods:
324 * int count() const;
325 * void writeValue(int i, uint16_t* point) const;
326 * count returns the number of indices and
327 * writeValue supplies each index.
328 */
329 template <typename POS_SRC,
330 typename TEX_SRC,
331 typename COL_SRC,
332 typename IDX_SRC>
333 void drawCustomVertices(const GrPaint& paint,
334 GrDrawTarget::PrimitiveType primitiveType,
335 const POS_SRC& posSrc,
336 const TEX_SRC* texCoordSrc,
337 const COL_SRC* colorSrc,
338 const IDX_SRC* idxSrc);
339 /**
340 * To avoid the problem of having to create a typename for NULL parameters,
341 * these reduced versions of drawCustomVertices are provided.
342 */
343 template <typename POS_SRC>
344 void drawCustomVertices(const GrPaint& paint,
345 GrDrawTarget::PrimitiveType primitiveType,
346 const POS_SRC& posSrc);
347 template <typename POS_SRC, typename TEX_SRC>
348 void drawCustomVertices(const GrPaint& paint,
349 GrDrawTarget::PrimitiveType primitiveType,
350 const POS_SRC& posSrc,
351 const TEX_SRC* texCoordSrc);
352 template <typename POS_SRC, typename TEX_SRC, typename COL_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
359
360 ///////////////////////////////////////////////////////////////////////////
361 // Misc.
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
363 /**
364 * Call to ensure all drawing to the context has been issued to the
365 * underlying 3D API.
366 * if flushRenderTarget is true then after the call the last
367 * rendertarget set will be current in the underlying 3D API, otherwise
368 * it may not be. It is useful to set if the caller plans to use the 3D
369 * context outside of Ganesh to render into the current RT.
370 */
371 void flush(bool flushRenderTarget);
372
373 /**
374 * Return true on success, i.e. if we could copy the specified range of
375 * pixels from the current render-target into the buffer, converting into
376 * the specified pixel-config.
377 */
378 bool readPixels(int left, int top, int width, int height,
379 GrTexture::PixelConfig, void* buffer);
380
381 /**
382 * Copy the src pixels [buffer, stride, pixelconfig] into the current
383 * render-target at the specified rectangle.
384 */
385 void writePixels(int left, int top, int width, int height,
386 GrTexture::PixelConfig, const void* buffer, size_t stride);
387
reed@google.comac10a2d2010-12-22 21:39:39 +0000388
bsalomon@google.com5782d712011-01-21 21:03:59 +0000389 ///////////////////////////////////////////////////////////////////////////
390 // Statistics
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
392 void resetStats();
393
394 const GrGpu::Stats& getStats() const;
395
396 void printStats() const;
397
bsalomon@google.com5782d712011-01-21 21:03:59 +0000398 ///////////////////////////////////////////////////////////////////////////
399 // Helpers
400
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 class AutoRenderTarget : ::GrNoncopyable {
402 public:
403 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
404 fContext = NULL;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405 fPrevTarget = context->getRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 if (fPrevTarget != target) {
407 context->setRenderTarget(target);
408 fContext = context;
409 }
410 }
411 ~AutoRenderTarget() {
412 if (fContext) {
413 fContext->setRenderTarget(fPrevTarget);
414 }
415 }
416 private:
417 GrContext* fContext;
418 GrRenderTarget* fPrevTarget;
419 };
420
bsalomon@google.com5782d712011-01-21 21:03:59 +0000421
reed@google.com01804b42011-01-18 21:50:41 +0000422 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000423 // Functions intended for internal use only.
reed@google.comac10a2d2010-12-22 21:39:39 +0000424 GrGpu* getGpu() { return fGpu; }
425 GrFontCache* getFontCache() { return fFontCache; }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000426 GrDrawTarget* getTextTarget(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 void flushText();
428
429 const GrIndexBuffer* quadIndexBuffer() const;
430 int maxQuadsInIndexBuffer() const;
431
432private:
433 GrGpu* fGpu;
434 GrTextureCache* fTextureCache;
435 GrFontCache* fFontCache;
436
437 GrVertexBufferAllocPool fVBAllocPool;
438 GrInOrderDrawBuffer fTextDrawBuffer;
439
440 GrContext(GrGpu* gpu);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
442 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
443
reed@google.comac10a2d2010-12-22 21:39:39 +0000444 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000445 void prepareToDraw(const GrPaint& paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
447 void drawClipIntoStencil();
448};
449
450/**
451 * Save/restore the view-matrix in the context.
452 */
bsalomon@google.com5782d712011-01-21 21:03:59 +0000453class GrAutoMatrix : GrNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +0000454public:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
456 fMatrix = ctx->getMatrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
459 fMatrix = ctx->getMatrix();
460 ctx->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462 ~GrAutoMatrix() {
463 fContext->setMatrix(fMatrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464 }
465
466private:
467 GrContext* fContext;
468 GrMatrix fMatrix;
469};
470
471#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000472
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473#include "GrContext_impl.h"