blob: 905fe50411f4e5f03461da5d15ad59698c308f71 [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"
22#include "GrSamplerState.h"
23#include "GrTextureCache.h"
24#include "GrInOrderDrawBuffer.h"
25#include "GrVertexBufferAllocPool.h"
26
27class GrFontCache;
28class GrPathIter;
29
30//TODO: move GrGpu enums/nested types here
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
reed@google.com873cb1e2010-12-23 15:00:45 +000040 /**
41 * Helper to create a opengl-shader based context
42 */
43 static GrContext* CreateGLShaderContext();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045 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 * Abandons all textures. Call this if you have lost the associated GPU
57 * context, and thus internal texture references/IDs are now invalid.
58 */
59 void abandonAllTextures();
60
61 /**
62 * Search for an entry with the same Key. If found, "lock" it and return it.
63 * If not found, return null.
64 */
65 GrTextureEntry* findAndLockTexture(GrTextureKey*,
66 const GrSamplerState&);
67
68
69 /**
70 * Create a new entry, based on the specified key and texture, and return
71 * its "locked" entry.
72 *
73 * Ownership of the texture is transferred to the Entry, which will unref()
74 * it when we are purged or deleted.
75 */
76 GrTextureEntry* createAndLockTexture(GrTextureKey* key,
77 const GrSamplerState&,
78 const GrGpu::TextureDesc&,
79 void* srcData, size_t rowBytes);
80
81 /**
82 * When done with an entry, call unlockTexture(entry) on it, which returns
83 * it to the cache, where it may be purged.
84 */
85 void unlockTexture(GrTextureEntry* entry);
86
87 /**
88 * Removes an texture from the cache. This prevents the texture from
89 * being found by a subsequent findAndLockTexture() until it is
90 * reattached. The entry still counts against the cache's budget and should
91 * be reattached when exclusive access is no longer needed.
92 */
93 void detachCachedTexture(GrTextureEntry*);
94
95 /**
96 * Reattaches a texture to the cache and unlocks it. Allows it to be found
97 * by a subsequent findAndLock or be purged (provided its lock count is
98 * now 0.)
99 */
100 void reattachAndUnlockCachedTexture(GrTextureEntry*);
101
102 /**
103 * Creates a texture that is outside the cache. Does not count against
104 * cache's budget.
105 */
106 GrTexture* createUncachedTexture(const GrGpu::TextureDesc&,
107 void* srcData,
108 size_t rowBytes);
109
110 /**
111 * Wraps an externally-created rendertarget in a GrRenderTarget.
112 * e.g. in GL platforamRenderTarget is an FBO id.
113 */
114 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
115 int width, int height);
116
117 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000118 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
119 * viewport state from the underlying 3D API and wraps it in a
120 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
121 * underlying object in its destructor and it is up to caller to guarantee
122 * that it remains valid while the GrRenderTarget is used.
123 *
124 * @return the newly created GrRenderTarget
125 */
126 GrRenderTarget* createRenderTargetFrom3DApiState() {
127 return fGpu->createRenderTargetFrom3DApiState();
128 }
129
130 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000131 * Returns true if the specified use of an indexed texture is supported.
132 */
133 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
134
135 ///////////////////////////////////////////////////////////////////////////
136
137 GrRenderTarget* currentRenderTarget() const;
138 void getViewMatrix(GrMatrix* m) const;
139 const GrClip& getClip() const { return fGpu->getClip(); }
140
141 void setRenderTarget(GrRenderTarget* target);
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000143 void setTexture(int stage, GrTexture* texture);
144 void setSamplerState(int stage, const GrSamplerState&);
145 void setTextureMatrix(int stage, const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000146
147 void setAntiAlias(bool);
148 void setDither(bool);
149 void setAlpha(uint8_t alpha);
150 void setColor(GrColor color);
151 void setPointSize(float size);
152 void setBlendFunc(GrGpu::BlendCoeff srcCoef, GrGpu::BlendCoeff dstCoef);
153 void setViewMatrix(const GrMatrix& m);
154 void setClip(const GrClip&);
155
156 /**
157 * Erase the entire render target, ignoring any clips/scissors.
158 */
159 void eraseColor(GrColor color);
160
161 /**
162 * Draw everywhere (respecting the clip) with the current color.
163 */
164 void drawFull(bool useTexture);
165
166 /**
167 * Draw the rect, respecting the current texture if useTexture is true.
168 * If strokeWidth < 0, then the rect is filled, else the rect is stroked
169 * based on strokeWidth. If strokeWidth == 0, then the stroke is always
170 * a single pixel thick.
171 */
172 void drawRect(const GrRect&, bool useTexture, GrScalar strokeWidth);
173
174 void fillRect(const GrRect& rect, bool useTexture) {
175 this->drawRect(rect, useTexture, -1);
176 }
177
178 /**
179 * Path filling rules
180 */
181 enum PathFills {
182 kWinding_PathFill,
183 kEvenOdd_PathFill,
184 kInverseWinding_PathFill,
185 kInverseEvenOdd_PathFill,
186 kHairLine_PathFill,
187
188 kPathFillCount
189 };
190
191 /**
192 * Tessellates and draws a path.
193 *
194 * @param path the path to draw
195 * @param paint the paint to set before drawing
196 * @param useTexture if true the path vertices will also be used as
197 * texture coorindates referencing last texture passed
198 * to setTexture.
199 */
200 void drawPath(GrPathIter* path,
201 PathFills fill,
202 bool useTexture,
203 const GrPoint* translate = NULL);
204
205 /**
206 * Call to ensure all drawing to the context has been issued to the
207 * underlying 3D API.
208 * if flushRenderTarget is true then after the call the last
209 * rendertarget set will be current in the underlying 3D API, otherwise
210 * it may not be. It is useful to set if the caller plans to use the 3D
211 * context outside of Ganesh to render into the current RT.
212 */
213 void flush(bool flushRenderTarget);
214
215 /**
216 * Return true on success, i.e. if we could copy the specified range of
217 * pixels from the current render-target into the buffer, converting into
218 * the specified pixel-config.
219 */
220 bool readPixels(int left, int top, int width, int height,
221 GrTexture::PixelConfig, void* buffer);
222
223 /**
224 * Copy the src pixels [buffer, stride, pixelconfig] into the current
225 * render-target at the specified rectangle.
226 */
227 void writePixels(int left, int top, int width, int height,
228 GrTexture::PixelConfig, const void* buffer, size_t stride);
229
230 /* -------------------------------------------------------
231 * Mimicking the GrGpu interface for now
232 * TODO: define appropriate higher-level API for context
233 */
234
235 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
236
237 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
238
239 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
240 uint32_t vertexCount,
241 uint32_t indexCount,
242 void** vertices,
243 void** indices);
244
245 void drawIndexed(GrGpu::PrimitiveType type,
246 uint32_t startVertex,
247 uint32_t startIndex,
248 uint32_t vertexCount,
249 uint32_t indexCount);
250
251 void drawNonIndexed(GrGpu::PrimitiveType type,
252 uint32_t startVertex,
253 uint32_t vertexCount);
254
255 void setVertexSourceToArray(const void* array,
256 GrVertexLayout vertexLayout);
257 void setIndexSourceToArray(const void* array);
258 void setVertexSourceToBuffer(GrVertexBuffer* buffer,
259 GrVertexLayout vertexLayout);
260 void setIndexSourceToBuffer(GrIndexBuffer* buffer);
261
262 void releaseReservedGeometry();
263
264 void resetStats();
265
266 const GrGpu::Stats& getStats() const;
267
268 void printStats() const;
269
270 class AutoRenderTarget : ::GrNoncopyable {
271 public:
272 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
273 fContext = NULL;
274 fPrevTarget = context->currentRenderTarget();
275 if (fPrevTarget != target) {
276 context->setRenderTarget(target);
277 fContext = context;
278 }
279 }
280 ~AutoRenderTarget() {
281 if (fContext) {
282 fContext->setRenderTarget(fPrevTarget);
283 }
284 }
285 private:
286 GrContext* fContext;
287 GrRenderTarget* fPrevTarget;
288 };
289
290 /* -------------------------------------------------------
291 */
292
293 // Intended only to be used within Ganesh:
294 GrGpu* getGpu() { return fGpu; }
295 GrFontCache* getFontCache() { return fFontCache; }
296 GrDrawTarget* getTextTarget();
297 void flushText();
298
299 const GrIndexBuffer* quadIndexBuffer() const;
300 int maxQuadsInIndexBuffer() const;
301
302private:
303 GrGpu* fGpu;
304 GrTextureCache* fTextureCache;
305 GrFontCache* fFontCache;
306
307 GrVertexBufferAllocPool fVBAllocPool;
308 GrInOrderDrawBuffer fTextDrawBuffer;
309
310 GrContext(GrGpu* gpu);
311 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
312
313 void drawClipIntoStencil();
314};
315
316/**
317 * Save/restore the view-matrix in the context.
318 */
319class GrAutoViewMatrix : GrNoncopyable {
320public:
321 GrAutoViewMatrix(GrContext* ctx) : fContext(ctx) {
322 ctx->getViewMatrix(&fMatrix);
323 }
324 GrAutoViewMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
325 ctx->getViewMatrix(&fMatrix);
326 ctx->setViewMatrix(matrix);
327 }
328 ~GrAutoViewMatrix() {
329 fContext->setViewMatrix(fMatrix);
330 }
331
332private:
333 GrContext* fContext;
334 GrMatrix fMatrix;
335};
336
337#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000338