blob: 75fb1f4eaa61f87038d71eea47fb68500a586a79 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
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 GrGpu_DEFINED
18#define GrGpu_DEFINED
19
20#include "GrRect.h"
21#include "GrRefCnt.h"
22#include "GrDrawTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com1c13c962011-02-14 16:51:21 +000025class GrVertexBufferAllocPool;
26class GrIndexBufferAllocPool;
bsalomon@google.comd302f142011-03-03 13:54:13 +000027class GrPathRenderer;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29class GrGpu : public GrDrawTarget {
30
31public:
32 /**
33 * Possible 3D APIs that may be used by Ganesh.
34 */
35 enum Engine {
36 kOpenGL_Shaders_Engine,
37 kOpenGL_Fixed_Engine,
38 kDirect3D9_Engine
39 };
40
41 /**
42 * Platform specific 3D context.
43 * For
44 * kOpenGL_Shaders_Engine use NULL
45 * kOpenGL_Fixed_Engine use NULL
46 * kDirect3D9_Engine use an IDirect3DDevice9*
47 */
48 typedef void* Platform3DContext;
49
50 /**
51 * Create an instance of GrGpu that matches the specified Engine backend.
52 * If the requested engine is not supported (at compile-time or run-time)
53 * this returns NULL.
54 */
55 static GrGpu* Create(Engine, Platform3DContext context3D);
56
57 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000058 * Used to control the level of antialiasing available for a rendertarget.
59 * Anti-alias quality levels depend on the underlying API/GPU capabilities.
60 */
61 enum AALevels {
62 kNone_AALevel, //<! No antialiasing available.
63 kLow_AALevel, //<! Low quality antialiased rendering. Actual
64 // interpretation is platform-dependent.
65 kMed_AALevel, //<! Medium quality antialiased rendering. Actual
66 // interpretation is platform-dependent.
67 kHigh_AALevel, //<! High quality antialiased rendering. Actual
68 // interpretation is platform-dependent.
69 };
70
71
72 /**
73 * Optional bitfield flags that can be passed to createTexture.
74 */
75 enum TextureFlags {
76 kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be
77 // rendered to by calling
78 // GrGpu::setRenderTarget() with
79 // GrTexture::asRenderTarget().
80 kNoPathRendering_TextureFlag = 0x2, //<! If the texture is used as a
81 // rendertarget but paths will not
82 // be rendered to it.
83 kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify
84 // this texture after creation
85 };
86
87 enum {
88 /**
89 * For Index8 pixel config, the colortable must be 256 entries
90 */
91 kColorTableSize = 256 * sizeof(GrColor)
92 };
93 /**
94 * Describes a texture to be created.
95 */
96 struct TextureDesc {
97 uint32_t fFlags; //!< bitfield of TextureFlags
98 GrGpu::AALevels fAALevel;//!< The level of antialiasing available
99 // for a rendertarget texture. Only
100 // flags contains
101 // kRenderTarget_TextureFlag.
102 uint32_t fWidth; //!< Width of the texture
103 uint32_t fHeight; //!< Height of the texture
104 GrTexture::PixelConfig fFormat; //!< Format of source data of the
105 // texture. Not guaraunteed to be the
106 // same as internal format used by
107 // 3D API.
108 };
109
110 /**
111 * Gpu usage statistics.
112 */
113 struct Stats {
114 uint32_t fVertexCnt; //<! Number of vertices drawn
115 uint32_t fIndexCnt; //<! Number of indices drawn
116 uint32_t fDrawCnt; //<! Number of draws
117
118 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
119
120 /*
121 * Number of times the texture is set in 3D API
122 */
123 uint32_t fTextureChngCnt;
124 /*
125 * Number of times the render target is set in 3D API
126 */
127 uint32_t fRenderTargetChngCnt;
128 /*
129 * Number of textures created (includes textures that are rendertargets).
130 */
131 uint32_t fTextureCreateCnt;
132 /*
133 * Number of rendertargets created.
134 */
135 uint32_t fRenderTargetCreateCnt;
136 };
137
138 ////////////////////////////////////////////////////////////////////////////
139
140 GrGpu();
141 virtual ~GrGpu();
142
143 /**
144 * The GrGpu object normally assumes that no outsider is setting state
145 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146 * the GrGpu that the state was modified and it shouldn't make assumptions
147 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000149 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
151 void unimpl(const char[]);
152
153 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000154 * Creates a texture object. If desc width or height is not a power of
155 * two but underlying API requires a power of two texture then srcData
156 * will be embedded in a power of two texture. The extra width and height
157 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 *
bsalomon@google.com1da07462011-03-10 14:51:57 +0000159 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
160 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
161 * on the render target until its releaseRenderTarget() is called or it is
162 * destroyed.
163 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 * @param desc describes the texture to be created.
165 * @param srcData texel data to load texture. Begins with full-size
166 * palette data for paletted textures. Contains width*
167 * height texels. If NULL texture data is uninitialized.
168 *
169 * @return The texture object if successful, otherwise NULL.
170 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000171 GrTexture* createTexture(const TextureDesc& desc,
172 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 /**
174 * Wraps an externally-created rendertarget in a GrRenderTarget.
175 * @param platformRenderTarget handle to the the render target in the
176 * underlying 3D API. Interpretation depends on
177 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000178 * @param stencilBits number of stencil bits the target has
reed@google.comac10a2d2010-12-22 21:39:39 +0000179 * @param width width of the render target
180 * @param height height of the render target
181 */
182 virtual GrRenderTarget* createPlatformRenderTarget(
183 intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000184 int stencilBits,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000185 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000186
187 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000188 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
189 * viewport state from the underlying 3D API and wraps it in a
190 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
191 * underlying object in its destructor and it is up to caller to guarantee
192 * that it remains valid while the GrRenderTarget is used.
193 *
194 * @return the newly created GrRenderTarget
195 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000197
198 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 * Creates a vertex buffer.
200 *
201 * @param size size in bytes of the vertex buffer
202 * @param dynamic hints whether the data will be frequently changed
203 * by either GrVertexBuffer::lock or
204 * GrVertexBuffer::updateData.
205 *
206 * @return The vertex buffer if successful, otherwise NULL.
207 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000209
210 /**
211 * Creates an index buffer.
212 *
213 * @param size size in bytes of the index buffer
214 * @param dynamic hints whether the data will be frequently changed
215 * by either GrIndexBuffer::lock or
216 * GrIndexBuffer::updateData.
217 *
218 * @return The index buffer if successful, otherwise NULL.
219 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 * Erase the entire render target, ignoring any clips/scissors.
224 *
225 * This is issued to the GPU driver immediately.
226 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227 void eraseColor(GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
229 /**
230 * Are 8 bit paletted textures supported.
231 *
232 * @return true if 8bit palette textures are supported, false otherwise
233 */
234 bool supports8BitPalette() const { return f8bitPaletteSupport; }
235
236 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000237 * returns true if two sided stenciling is supported. If false then only
238 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 * @return true if only a single stencil pass is needed.
240 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000241 bool supportsTwoSidedStencil() const
242 { return fTwoSidedStencilSupport; }
243
244 /**
245 * returns true if stencil wrap is supported. If false then
246 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
247 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
248 * @return true if stencil wrap ops are supported.
249 */
250 bool supportsStencilWrapOps() const
251 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000252
253 /**
254 * Checks whether locking vertex and index buffers is supported.
255 *
256 * @return true if locking is supported.
257 */
258 bool supportsBufferLocking() const { return fBufferLockSupport; }
259
260 /**
261 * Gets the minimum width of a render target. If a texture/rt is created
262 * with a width less than this size the GrGpu object will clamp it to this
263 * value.
264 */
265 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
266
267 /**
268 * Gets the minimum width of a render target. If a texture/rt is created
269 * with a height less than this size the GrGpu object will clamp it to this
270 * value.
271 */
272 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
273
274 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000275 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000277 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000279 bool npotTextureSupport() const { return fNPOTTextureSupport; }
280
281 /**
282 * Returns true if NPOT textures can be repeat/mirror tiled.
283 *
284 * @return true if NPOT textures can be tiled
285 */
286 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
287
288 /**
289 * Returns true if a NPOT texture can be a rendertarget
290 *
291 * @return the true if NPOT texture/rendertarget can be created.
292 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000294
reed@google.com02a7e6c2011-01-28 21:21:49 +0000295 int maxTextureDimension() const { return fMaxTextureDimension; }
296
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000298 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000299 int startVertex,
300 int startIndex,
301 int vertexCount,
302 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.comffca4002011-02-22 20:34:01 +0000304 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000305 int startVertex,
306 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
308 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000310 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
311 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000313 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000315 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
317 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000318 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000319 * (1,1), (0,1)].
320 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000321 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000322 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000323
324 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000325 * Ensures that the current render target is actually set in the
326 * underlying 3D API. Used when client wants to use 3D API to directly
327 * render to the RT.
328 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000329 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000330
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000331 /**
332 * Reads a rectangle of pixels from the current render target.
333 * @param left left edge of the rectangle to read (inclusive)
334 * @param top top edge of the rectangle to read (inclusive)
335 * @param width width of rectangle to read in pixels.
336 * @param height height of rectangle to read in pixels.
337 * @param buffer memory to read the rectangle into.
338 *
339 * @return true if the read succeeded, false if not. The read can fail
340 * because of a unsupported pixel config or because no render
341 * target is currently set.
342 */
343 bool readPixels(int left, int top, int width, int height,
344 GrTexture::PixelConfig, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
346
347 const Stats& getStats() const;
348 void resetStats();
349 void printStats() const;
350
351protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000352 enum PrivateStateBits {
353 kFirstBit = (kLastPublicStateBit << 1),
354
355 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
356 // stencil bits used for
357 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000358 };
359
360 /**
361 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
362 */
363 struct ClipState {
364 bool fClipInStencil;
365 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 } fClipState;
367
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000368 // GrDrawTarget override
369 virtual void clipWillBeSet(const GrClip& newClip);
370
371 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000372 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 // Functions used to map clip-respecting stencil tests into normal
375 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000376 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000377 GrStencilFunc func);
378 static void ConvertStencilFuncAndMask(GrStencilFunc func,
379 bool clipInStencil,
380 unsigned int clipBit,
381 unsigned int userBits,
382 unsigned int* ref,
383 unsigned int* mask);
384
385 // stencil settings to clip drawing when stencil clipping is in effect
386 // and the client isn't using the stencil test.
387 static const GrStencilSettings gClipStencilSettings;
388
reed@google.comac10a2d2010-12-22 21:39:39 +0000389 // defaults to false, subclass can set true to support palleted textures
390 bool f8bitPaletteSupport;
391
bsalomon@google.com0748f212011-02-01 22:56:16 +0000392 // set by subclass
393 bool fNPOTTextureSupport;
394 bool fNPOTTextureTileSupport;
395 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000396 bool fTwoSidedStencilSupport;
397 bool fStencilWrapOpsSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000398
399 // set by subclass to true if index and vertex buffers can be locked, false
400 // otherwise.
401 bool fBufferLockSupport;
402
403 // set by subclass
404 int fMinRenderTargetWidth;
405 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000406 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000408 Stats fStats;
409
410 const GrVertexBuffer* fCurrPoolVertexBuffer;
411 int fCurrPoolStartVertex;
412
413 const GrIndexBuffer* fCurrPoolIndexBuffer;
414 int fCurrPoolStartIndex;
415
416 // GrDrawTarget overrides
417 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
418 void** vertices,
419 void** indices);
420 virtual void releaseGeometryHelper();
421
422 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
423 int vertexCount);
424
425 virtual void setIndexSourceToArrayHelper(const void* indexArray,
426 int indexCount);
427 // Helpers for setting up geometry state
428 void finalizeReservedVertices();
429 void finalizeReservedIndices();
430
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000431 // overridden by API-specific derived class to handle re-emitting 3D API
432 // preample and dirtying state cache.
433 virtual void resetContext() = 0;
434
435 // overridden by API-specific derived class to create objects.
436 virtual GrTexture* createTextureHelper(const TextureDesc& desc,
437 const void* srcData,
438 size_t rowBytes) = 0;
439 virtual GrRenderTarget* createPlatformRenderTargetHelper(
440 intptr_t platformRenderTarget,
441 int stencilBits,
442 int width, int height) = 0;
443 virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0;
444 virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size,
445 bool dynamic) = 0;
446 virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size,
447 bool dynamic) = 0;
448
449 // overridden by API-specific derivated class to perform the erase.
450 virtual void eraseColorHelper(GrColor color) = 0;
451
452 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000453 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 uint32_t startVertex,
455 uint32_t startIndex,
456 uint32_t vertexCount,
457 uint32_t indexCount) = 0;
458
bsalomon@google.comffca4002011-02-22 20:34:01 +0000459 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 uint32_t vertexCount,
461 uint32_t numVertices) = 0;
462
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000463 // overridden by API-specific derived class to perform flush
464 virtual void forceRenderTargetFlushHelper() = 0;
465
466 // overridden by API-specific derived class to perform the read pixels.
467 virtual bool readPixelsHelper(int left, int top, int width, int height,
468 GrTexture::PixelConfig, void* buffer) = 0;
469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000471 // indexed geometry. The subclass may adjust the startVertex and/or
472 // startIndex since it may have already accounted for these in the setup.
473 virtual void setupGeometry(int* startVertex,
474 int* startIndex,
475 int vertexCount,
476 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
478
479 // The GrGpu typically records the clients requested state and then flushes
480 // deltas from previous state at draw time. This function does the
481 // API-specific flush of the state
482 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000483 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
485 // Sets the scissor rect, or disables if rect is NULL.
486 virtual void flushScissor(const GrIRect* rect) = 0;
487
488 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +0000489 virtual void eraseStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
reed@google.comac10a2d2010-12-22 21:39:39 +0000491private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000492 // readies the pools to provide vertex/index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000493 void prepareVertexPool();
494 void prepareIndexPool();
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000495
bsalomon@google.comd302f142011-03-03 13:54:13 +0000496 GrPathRenderer* getPathRenderer();
497
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000498 void handleDirtyContext() {
499 if (fContextIsDirty) {
500 this->resetContext();
501 fContextIsDirty = false;
502 }
503 }
504
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000505 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000507 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000509 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
510 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000512 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
513 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000514
515 GrPathRenderer* fPathRenderer;
516
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000517 bool fContextIsDirty;
518
bsalomon@google.comd302f142011-03-03 13:54:13 +0000519 // when in an internal draw these indicate whether the pools are in use
520 // by one of the outer draws. If false then it is safe to reset the
521 // pool.
522 bool fVertexPoolInUse;
523 bool fIndexPoolInUse;
524
525 // used to save and restore state when the GrGpu needs
526 // to make its geometry pools available internally
527 class AutoInternalDrawGeomRestore {
528 public:
529 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
530 fGpu = gpu;
531
532 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
533 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
534
535 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
536 (kBuffer_GeometrySrcType !=
537 gpu->fGeometrySrc.fVertexSrc);
538 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
539 (kBuffer_GeometrySrcType !=
540 gpu->fGeometrySrc.fIndexSrc);;
541
542 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
543 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
544 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
545 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
546
547 fSavedReservedGeometry = gpu->fReservedGeometry;
548 gpu->fReservedGeometry.fLocked = false;
549 }
550 ~AutoInternalDrawGeomRestore() {
551 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
552 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
553 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
554 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
555 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
556 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
557 fGpu->fReservedGeometry = fSavedReservedGeometry;
558 }
559 private:
560 AutoGeometrySrcRestore fAgsr;
561 GrGpu* fGpu;
562 const GrVertexBuffer* fSavedPoolVertexBuffer;
563 int fSavedPoolStartVertex;
564 const GrIndexBuffer* fSavedPoolIndexBuffer;
565 int fSavedPoolStartIndex;
566 bool fVertexPoolWasInUse;
567 bool fIndexPoolWasInUse;
568 ReservedGeometry fSavedReservedGeometry;
569 };
570
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000571 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572};
573
574#endif