blob: a9316957cbf68978b7be98a3d3b861413362f70c [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();
44
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 /**
118 * Returns true if the specified use of an indexed texture is supported.
119 */
120 bool supportsIndex8PixelConfig(const GrSamplerState&, int width, int height);
121
122 ///////////////////////////////////////////////////////////////////////////
123
124 GrRenderTarget* currentRenderTarget() const;
125 void getViewMatrix(GrMatrix* m) const;
126 const GrClip& getClip() const { return fGpu->getClip(); }
127
128 void setRenderTarget(GrRenderTarget* target);
129 void setDefaultRenderTargetSize(uint32_t width, uint32_t height);
130 GrRenderTarget* defaultRenderTarget() { return fGpu->defaultRenderTarget(); }
131
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000132 void setTexture(int stage, GrTexture* texture);
133 void setSamplerState(int stage, const GrSamplerState&);
134 void setTextureMatrix(int stage, const GrMatrix& m);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135
136 void setAntiAlias(bool);
137 void setDither(bool);
138 void setAlpha(uint8_t alpha);
139 void setColor(GrColor color);
140 void setPointSize(float size);
141 void setBlendFunc(GrGpu::BlendCoeff srcCoef, GrGpu::BlendCoeff dstCoef);
142 void setViewMatrix(const GrMatrix& m);
143 void setClip(const GrClip&);
144
145 /**
146 * Erase the entire render target, ignoring any clips/scissors.
147 */
148 void eraseColor(GrColor color);
149
150 /**
151 * Draw everywhere (respecting the clip) with the current color.
152 */
153 void drawFull(bool useTexture);
154
155 /**
156 * Draw the rect, respecting the current texture if useTexture is true.
157 * If strokeWidth < 0, then the rect is filled, else the rect is stroked
158 * based on strokeWidth. If strokeWidth == 0, then the stroke is always
159 * a single pixel thick.
160 */
161 void drawRect(const GrRect&, bool useTexture, GrScalar strokeWidth);
162
163 void fillRect(const GrRect& rect, bool useTexture) {
164 this->drawRect(rect, useTexture, -1);
165 }
166
167 /**
168 * Path filling rules
169 */
170 enum PathFills {
171 kWinding_PathFill,
172 kEvenOdd_PathFill,
173 kInverseWinding_PathFill,
174 kInverseEvenOdd_PathFill,
175 kHairLine_PathFill,
176
177 kPathFillCount
178 };
179
180 /**
181 * Tessellates and draws a path.
182 *
183 * @param path the path to draw
184 * @param paint the paint to set before drawing
185 * @param useTexture if true the path vertices will also be used as
186 * texture coorindates referencing last texture passed
187 * to setTexture.
188 */
189 void drawPath(GrPathIter* path,
190 PathFills fill,
191 bool useTexture,
192 const GrPoint* translate = NULL);
193
194 /**
195 * Call to ensure all drawing to the context has been issued to the
196 * underlying 3D API.
197 * if flushRenderTarget is true then after the call the last
198 * rendertarget set will be current in the underlying 3D API, otherwise
199 * it may not be. It is useful to set if the caller plans to use the 3D
200 * context outside of Ganesh to render into the current RT.
201 */
202 void flush(bool flushRenderTarget);
203
204 /**
205 * Return true on success, i.e. if we could copy the specified range of
206 * pixels from the current render-target into the buffer, converting into
207 * the specified pixel-config.
208 */
209 bool readPixels(int left, int top, int width, int height,
210 GrTexture::PixelConfig, void* buffer);
211
212 /**
213 * Copy the src pixels [buffer, stride, pixelconfig] into the current
214 * render-target at the specified rectangle.
215 */
216 void writePixels(int left, int top, int width, int height,
217 GrTexture::PixelConfig, const void* buffer, size_t stride);
218
219 /* -------------------------------------------------------
220 * Mimicking the GrGpu interface for now
221 * TODO: define appropriate higher-level API for context
222 */
223
224 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
225
226 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
227
228 bool reserveAndLockGeometry(GrVertexLayout vertexLayout,
229 uint32_t vertexCount,
230 uint32_t indexCount,
231 void** vertices,
232 void** indices);
233
234 void drawIndexed(GrGpu::PrimitiveType type,
235 uint32_t startVertex,
236 uint32_t startIndex,
237 uint32_t vertexCount,
238 uint32_t indexCount);
239
240 void drawNonIndexed(GrGpu::PrimitiveType type,
241 uint32_t startVertex,
242 uint32_t vertexCount);
243
244 void setVertexSourceToArray(const void* array,
245 GrVertexLayout vertexLayout);
246 void setIndexSourceToArray(const void* array);
247 void setVertexSourceToBuffer(GrVertexBuffer* buffer,
248 GrVertexLayout vertexLayout);
249 void setIndexSourceToBuffer(GrIndexBuffer* buffer);
250
251 void releaseReservedGeometry();
252
253 void resetStats();
254
255 const GrGpu::Stats& getStats() const;
256
257 void printStats() const;
258
259 class AutoRenderTarget : ::GrNoncopyable {
260 public:
261 AutoRenderTarget(GrContext* context, GrRenderTarget* target) {
262 fContext = NULL;
263 fPrevTarget = context->currentRenderTarget();
264 if (fPrevTarget != target) {
265 context->setRenderTarget(target);
266 fContext = context;
267 }
268 }
269 ~AutoRenderTarget() {
270 if (fContext) {
271 fContext->setRenderTarget(fPrevTarget);
272 }
273 }
274 private:
275 GrContext* fContext;
276 GrRenderTarget* fPrevTarget;
277 };
278
279 /* -------------------------------------------------------
280 */
281
282 // Intended only to be used within Ganesh:
283 GrGpu* getGpu() { return fGpu; }
284 GrFontCache* getFontCache() { return fFontCache; }
285 GrDrawTarget* getTextTarget();
286 void flushText();
287
288 const GrIndexBuffer* quadIndexBuffer() const;
289 int maxQuadsInIndexBuffer() const;
290
291private:
292 GrGpu* fGpu;
293 GrTextureCache* fTextureCache;
294 GrFontCache* fFontCache;
295
296 GrVertexBufferAllocPool fVBAllocPool;
297 GrInOrderDrawBuffer fTextDrawBuffer;
298
299 GrContext(GrGpu* gpu);
300 bool finalizeTextureKey(GrTextureKey*, const GrSamplerState&) const;
301
302 void drawClipIntoStencil();
303};
304
305/**
306 * Save/restore the view-matrix in the context.
307 */
308class GrAutoViewMatrix : GrNoncopyable {
309public:
310 GrAutoViewMatrix(GrContext* ctx) : fContext(ctx) {
311 ctx->getViewMatrix(&fMatrix);
312 }
313 GrAutoViewMatrix(GrContext* ctx, const GrMatrix& matrix) : fContext(ctx) {
314 ctx->getViewMatrix(&fMatrix);
315 ctx->setViewMatrix(matrix);
316 }
317 ~GrAutoViewMatrix() {
318 fContext->setViewMatrix(fMatrix);
319 }
320
321private:
322 GrContext* fContext;
323 GrMatrix fMatrix;
324};
325
326#endif
reed@google.com873cb1e2010-12-23 15:00:45 +0000327