blob: b43375cb482dab28b62b1b678c641ba25823f972 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +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
17#ifndef GrContext_DEFINED
18#define GrContext_DEFINED
19
20#include "GrClip.h"
21#include "GrGpu.h"
22#include "GrTextureCache.h"
23#include "GrPaint.h"
24
25class GrFontCache;
26class GrPathIter;
27class GrVertexBufferAllocPool;
28class GrIndexBufferAllocPool;
29class GrInOrderDrawBuffer;
30class GrPathRenderer;
31
32class GrContext : public GrRefCnt {
33public:
34 /**
35 * Creates a GrContext from within a 3D context.
36 */
37 static GrContext* Create(GrGpu::Engine engine,
38 GrGpu::Platform3DContext context3D);
39
40 /**
41 * Helper to create a opengl-shader based context
42 */
43 static GrContext* CreateGLShaderContext();
44
45 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
55 ///////////////////////////////////////////////////////////////////////////
56 // Textures
57
58 /**
59 * Abandons all textures. Call this if you have lost the associated GPU
60 * context, and thus internal texture references/IDs are now invalid.
61 */
62 void abandonAllTextures();
63
64 /**
65 * Search for an entry with the same Key. If found, "lock" it and return it.
66 * If not found, return null.
67 */
68 GrTextureEntry* findAndLockTexture(GrTextureKey*,
69 const GrSamplerState&);
70
71
72 /**
73 * Create a new entry, based on the specified key and texture, and return
74 * its "locked" entry.
75 *
76 * Ownership of the texture is transferred to the Entry, which will unref()
77 * it when we are purged or deleted.
78 */
79 GrTextureEntry* createAndLockTexture(GrTextureKey* key,
80 const GrSamplerState&,
81 const GrGpu::TextureDesc&,
82 void* srcData, size_t rowBytes);
83
84 /**
85 * When done with an entry, call unlockTexture(entry) on it, which returns
86 * it to the cache, where it may be purged.
87 */
88 void unlockTexture(GrTextureEntry* entry);
89
90 /**
91 * Removes an texture from the cache. This prevents the texture from
92 * being found by a subsequent findAndLockTexture() until it is
93 * reattached. The entry still counts against the cache's budget and should
94 * be reattached when exclusive access is no longer needed.
95 */
96 void detachCachedTexture(GrTextureEntry*);
97
98 /**
99 * Reattaches a texture to the cache and unlocks it. Allows it to be found
100 * by a subsequent findAndLock or be purged (provided its lock count is
101 * now 0.)
102 */
103 void reattachAndUnlockCachedTexture(GrTextureEntry*);
104
105 /**
106 * Creates a texture that is outside the cache. Does not count against
107 * cache's budget.
108 */
109 GrTexture* createUncachedTexture(const GrGpu::TextureDesc&,
110 void* srcData,
111 size_t rowBytes);
112
113 /**
114 * Returns true if the specified use of an indexed texture is supported.
115 */
116 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
117
118 /**
119 * Return the current texture cache limits.
120 *
121 * @param maxTextures If non-null, returns maximum number of textures that
122 * can be held in the cache.
123 * @param maxTextureBytes If non-null, returns maximum number of bytes of
124 * texture memory that can be held in the cache.
125 */
126 void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const;
127
128 /**
129 * Specify the texture cache limits. If the current cache exceeds either
130 * of these, it will be purged (LRU) to keep the cache within these limits.
131 *
132 * @param maxTextures The maximum number of textures that can be held in
133 * the cache.
134 * @param maxTextureBytes The maximum number of bytes of texture memory
135 * that can be held in the cache.
136 */
137 void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes);
138
139 /**
140 * Return the max width or height of a texture supported by the current gpu
141 */
142 int getMaxTextureDimension();
143
144 ///////////////////////////////////////////////////////////////////////////
145 // Render targets
146
147 /**
148 * Wraps an externally-created rendertarget in a GrRenderTarget.
149 * @param platformRenderTarget 3D API-specific render target identifier
150 * e.g. in GL platforamRenderTarget is an FBO
151 * id.
152 * @param stencilBits the number of stencil bits that the render
153 * target has.
154 * @param width width of the render target.
155 * @param height height of the render target.
156 */
157 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
158 int stencilBits,
159 int width, int height);
160
161 /**
162 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
163 * viewport state from the underlying 3D API and wraps it in a
164 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
165 * underlying object in its destructor and it is up to caller to guarantee
166 * that it remains valid while the GrRenderTarget is used.
167 *
168 * @return the newly created GrRenderTarget
169 */
170 GrRenderTarget* createRenderTargetFrom3DApiState() {
171 return fGpu->createRenderTargetFrom3DApiState();
172 }
173
174 /**
175 * Sets the render target.
176 * @param target the render target to set. (should not be NULL.)
177 */
178 void setRenderTarget(GrRenderTarget* target);
179
180 /**
181 * Gets the current render target.
182 * @return the currently bound render target. Should never be NULL.
183 */
184 const GrRenderTarget* getRenderTarget() const;
185 GrRenderTarget* getRenderTarget();
186
187 ///////////////////////////////////////////////////////////////////////////
188 // Matrix state
189
190 /**
191 * Gets the current transformation matrix.
192 * @return the current matrix.
193 */
194 const GrMatrix& getMatrix() const;
195
196 /**
197 * Sets the transformation matrix.
198 * @param m the matrix to set.
199 */
200 void setMatrix(const GrMatrix& m);
201
202 /**
203 * Concats the current matrix. The passed matrix is applied before the
204 * current matrix.
205 * @param m the matrix to concat.
206 */
207 void concatMatrix(const GrMatrix& m) const;
208
209
210 ///////////////////////////////////////////////////////////////////////////
211 // Clip state
212 /**
213 * Gets the current clip.
214 * @return the current clip.
215 */
216 const GrClip& getClip() const { return fGpu->getClip(); }
217
218 /**
219 * Sets the clip.
220 * @param clip the clip to set.
221 */
222 void setClip(const GrClip& clip);
223
224 /**
225 * Convenience method for setting the clip to a rect.
226 * @param rect the rect to set as the new clip.
227 */
228 void setClip(const GrIRect& rect);
229
230 ///////////////////////////////////////////////////////////////////////////
231 // Draws
232
233 /**
234 * Erase the entire render target, ignoring any clips
235 */
236 void eraseColor(GrColor color);
237
238 /**
239 * Draw everywhere (respecting the clip) with the paint.
240 */
241 void drawPaint(const GrPaint& paint);
242
243 /**
244 * Draw the rect using a paint.
245 * @param paint describes how to color pixels.
246 * @param strokeWidth If strokeWidth < 0, then the rect is filled, else
247 * the rect is mitered stroked based on strokeWidth. If
248 * strokeWidth == 0, then the stroke is always a single
249 * pixel thick.
250 * @param matrix Optional matrix applied to the rect. Applied before
251 * context's matrix or the paint's matrix.
252 * The rects coords are used to access the paint (through texture matrix)
253 */
254 void drawRect(const GrPaint& paint,
255 const GrRect&,
256 GrScalar strokeWidth = -1,
257 const GrMatrix* matrix = NULL);
258
259 /**
260 * Maps a rect of paint coordinates onto the a rect of destination
261 * coordinates. Each rect can optionally be transformed. The srcRect
262 * is stretched over the dstRect. The dstRect is transformed by the
263 * context's matrix and the srcRect is transformed by the paint's matrix.
264 * Additional optional matrices can be provided by parameters.
265 *
266 * @param paint describes how to color pixels.
267 * @param dstRect the destination rect to draw.
268 * @param srcRect rect of paint coordinates to be mapped onto dstRect
269 * @param dstMatrix Optional matrix to transform dstRect. Applied before
270 * context's matrix.
271 * @param srcMatrix Optional matrix to transform srcRect Applied before
272 * paint's matrix.
273 */
274 void drawRectToRect(const GrPaint& paint,
275 const GrRect& dstRect,
276 const GrRect& srcRect,
277 const GrMatrix* dstMatrix = NULL,
278 const GrMatrix* srcMatrix = NULL);
279
280 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000281 * Draws a path.
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282 *
283 * @param paint describes how to color pixels.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000284 * @param pathIter the path to draw
bsalomon@google.com27847de2011-02-22 20:59:41 +0000285 * @param fill the path filling rule to use.
286 * @param translate optional additional translation applied to the
287 * path.
288 */
289 void drawPath(const GrPaint& paint,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000290 GrPathIter* pathIter,
291 GrPathFill fill,
292 const GrPoint* translate = NULL);
293 /**
294 * Helper version of drawPath that takes a GrPath
295 */
296 void drawPath(const GrPaint& paint,
297 const GrPath& path,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298 GrPathFill fill,
299 const GrPoint* translate = NULL);
300 /**
301 * Draws vertices with a paint.
302 *
303 * @param paint describes how to color pixels.
304 * @param primitiveType primitives type to draw.
305 * @param vertexCount number of vertices.
306 * @param positions array of vertex positions, required.
307 * @param texCoords optional array of texture coordinates used
308 * to access the paint.
309 * @param colors optional array of per-vertex colors, supercedes
310 * the paint's color field.
311 * @param indices optional array of indices. If NULL vertices
312 * are drawn non-indexed.
313 * @param indexCount if indices is non-null then this is the
314 * number of indices.
315 */
316 void drawVertices(const GrPaint& paint,
317 GrPrimitiveType primitiveType,
318 int vertexCount,
319 const GrPoint positions[],
320 const GrPoint texs[],
321 const GrColor colors[],
322 const uint16_t indices[],
323 int indexCount);
324
325 /**
326 * Similar to drawVertices but caller provides objects that convert to Gr
327 * types. The count of vertices is given by posSrc.
328 *
329 * @param paint describes how to color pixels.
330 * @param primitiveType primitives type to draw.
331 * @param posSrc Source of vertex positions. Must implement
332 * int count() const;
333 * void writeValue(int i, GrPoint* point) const;
334 * count returns the total number of vertices and
335 * writeValue writes a vertex position to point.
336 * @param texSrc optional, pass NULL to not use explicit tex
337 * coords. If present provides tex coords with
338 * method:
339 * void writeValue(int i, GrPoint* point) const;
340 * @param texSrc optional, pass NULL to not use per-vertex colors
341 * If present provides colors with method:
342 * void writeValue(int i, GrColor* point) const;
343 * @param indices optional, pass NULL for non-indexed drawing. If
344 * present supplies indices for indexed drawing
345 * with following methods:
346 * int count() const;
347 * void writeValue(int i, uint16_t* point) const;
348 * count returns the number of indices and
349 * writeValue supplies each index.
350 */
351 template <typename POS_SRC,
352 typename TEX_SRC,
353 typename COL_SRC,
354 typename IDX_SRC>
355 void drawCustomVertices(const GrPaint& paint,
356 GrPrimitiveType primitiveType,
357 const POS_SRC& posSrc,
358 const TEX_SRC* texCoordSrc,
359 const COL_SRC* colorSrc,
360 const IDX_SRC* idxSrc);
361 /**
362 * To avoid the problem of having to create a typename for NULL parameters,
363 * these reduced versions of drawCustomVertices are provided.
364 */
365 template <typename POS_SRC>
366 void drawCustomVertices(const GrPaint& paint,
367 GrPrimitiveType primitiveType,
368 const POS_SRC& posSrc);
369 template <typename POS_SRC, typename TEX_SRC>
370 void drawCustomVertices(const GrPaint& paint,
371 GrPrimitiveType primitiveType,
372 const POS_SRC& posSrc,
373 const TEX_SRC* texCoordSrc);
374 template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
375 void drawCustomVertices(const GrPaint& paint,
376 GrPrimitiveType primitiveType,
377 const POS_SRC& posSrc,
378 const TEX_SRC* texCoordSrc,
379 const COL_SRC* colorSrc);
380
381
382 ///////////////////////////////////////////////////////////////////////////
383 // Misc.
384
385 /**
386 * Call to ensure all drawing to the context has been issued to the
387 * underlying 3D API.
388 * if flushRenderTarget is true then after the call the last
389 * rendertarget set will be current in the underlying 3D API, otherwise
390 * it may not be. It is useful to set if the caller plans to use the 3D
391 * context outside of Ganesh to render into the current RT.
392 */
393 void flush(bool flushRenderTarget);
394
395 /**
396 * Return true on success, i.e. if we could copy the specified range of
397 * pixels from the current render-target into the buffer, converting into
398 * the specified pixel-config.
399 */
400 bool readPixels(int left, int top, int width, int height,
401 GrTexture::PixelConfig, void* buffer);
402
403 /**
404 * Copy the src pixels [buffer, stride, pixelconfig] into the current
405 * render-target at the specified rectangle.
406 */
407 void writePixels(int left, int top, int width, int height,
408 GrTexture::PixelConfig, const void* buffer, size_t stride);
409
410
411 ///////////////////////////////////////////////////////////////////////////
412 // Statistics
413
414 void resetStats();
415
416 const GrGpu::Stats& getStats() const;
417
418 void printStats() const;
419
420 ///////////////////////////////////////////////////////////////////////////
421 // Helpers
422
423 class AutoRenderTarget : ::GrNoncopyable {
424 public:
425 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
426 fContext = NULL;
427 fPrevTarget = context->getRenderTarget();
428 if (fPrevTarget != target) {
429 context->setRenderTarget(target);
430 fContext = context;
431 }
432 }
433 ~AutoRenderTarget() {
434 if (fContext) {
435 fContext->setRenderTarget(fPrevTarget);
436 }
437 }
438 private:
439 GrContext* fContext;
440 GrRenderTarget* fPrevTarget;
441 };
442
443
444 ///////////////////////////////////////////////////////////////////////////
445 // Functions intended for internal use only.
446 GrGpu* getGpu() { return fGpu; }
447 GrFontCache* getFontCache() { return fFontCache; }
448 GrDrawTarget* getTextTarget(const GrPaint& paint);
449 void flushText();
450 const GrIndexBuffer* getQuadIndexBuffer() const;
451
452private:
453 // used to keep track of when we need to flush the draw buffer
454 enum DrawCategory {
455 kBuffered_DrawCategory, // last draw was inserted in draw buffer
456 kUnbuffered_DrawCategory, // last draw was not inserted in the draw buffer
457 kText_DrawCategory // text context was last to draw
458 };
459 DrawCategory fLastDrawCategory;
460
461 GrGpu* fGpu;
462 GrTextureCache* fTextureCache;
463 GrFontCache* fFontCache;
464 GrPathRenderer* fPathRenderer;
465
466 GrVertexBufferAllocPool* fDrawBufferVBAllocPool;
467 GrIndexBufferAllocPool* fDrawBufferIBAllocPool;
468 GrInOrderDrawBuffer* fDrawBuffer;
469
470 GrContext(GrGpu* gpu);
471 void flushDrawBuffer();
472
473 static void SetPaint(const GrPaint& paint, GrDrawTarget* target);
474
475 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
476
477 GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
478
479 void drawClipIntoStencil();
480};
481
482/**
483 * Save/restore the view-matrix in the context.
484 */
485class GrAutoMatrix : GrNoncopyable {
486public:
487 GrAutoMatrix(GrContext* ctx) : fContext(ctx) {
488 fMatrix = ctx->getMatrix();
489 }
490 GrAutoMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
491 fMatrix = ctx->getMatrix();
492 ctx->setMatrix(matrix);
493 }
494 ~GrAutoMatrix() {
495 fContext->setMatrix(fMatrix);
496 }
497
498private:
499 GrContext* fContext;
500 GrMatrix fMatrix;
501};
502
503#endif
504
505#include "GrContext_impl.h"