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