blob: c4f5e4b7bc1e732667da91306b85be8865141950 [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"
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +000024#include "GrPathRenderer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com1c13c962011-02-14 16:51:21 +000026class GrVertexBufferAllocPool;
27class GrIndexBufferAllocPool;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028class GrResource;
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30class GrGpu : public GrDrawTarget {
31
32public:
33 /**
34 * Possible 3D APIs that may be used by Ganesh.
35 */
36 enum Engine {
37 kOpenGL_Shaders_Engine,
38 kOpenGL_Fixed_Engine,
39 kDirect3D9_Engine
40 };
41
42 /**
43 * Platform specific 3D context.
44 * For
45 * kOpenGL_Shaders_Engine use NULL
46 * kOpenGL_Fixed_Engine use NULL
47 * kDirect3D9_Engine use an IDirect3DDevice9*
48 */
49 typedef void* Platform3DContext;
50
51 /**
52 * Create an instance of GrGpu that matches the specified Engine backend.
53 * If the requested engine is not supported (at compile-time or run-time)
54 * this returns NULL.
55 */
56 static GrGpu* Create(Engine, Platform3DContext context3D);
57
58 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000059 * Used to control the level of antialiasing available for a rendertarget.
60 * Anti-alias quality levels depend on the underlying API/GPU capabilities.
61 */
62 enum AALevels {
63 kNone_AALevel, //<! No antialiasing available.
64 kLow_AALevel, //<! Low quality antialiased rendering. Actual
65 // interpretation is platform-dependent.
66 kMed_AALevel, //<! Medium quality antialiased rendering. Actual
67 // interpretation is platform-dependent.
68 kHigh_AALevel, //<! High quality antialiased rendering. Actual
69 // interpretation is platform-dependent.
70 };
71
72
73 /**
74 * Optional bitfield flags that can be passed to createTexture.
75 */
76 enum TextureFlags {
77 kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be
78 // rendered to by calling
79 // GrGpu::setRenderTarget() with
80 // GrTexture::asRenderTarget().
bsalomon@google.comf6a7c112011-03-24 16:14:10 +000081 kNoStencil_TextureFlag = 0x2, //<! If the texture is used as a
82 // rendertarget but a stencil
83 // buffer is not required. Stencil
84 // may be required for clipping and
85 // path rendering.
reed@google.comac10a2d2010-12-22 21:39:39 +000086 kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify
87 // this texture after creation
88 };
89
90 enum {
91 /**
92 * For Index8 pixel config, the colortable must be 256 entries
93 */
94 kColorTableSize = 256 * sizeof(GrColor)
95 };
96 /**
97 * Describes a texture to be created.
98 */
99 struct TextureDesc {
100 uint32_t fFlags; //!< bitfield of TextureFlags
101 GrGpu::AALevels fAALevel;//!< The level of antialiasing available
102 // for a rendertarget texture. Only
103 // flags contains
104 // kRenderTarget_TextureFlag.
105 uint32_t fWidth; //!< Width of the texture
106 uint32_t fHeight; //!< Height of the texture
107 GrTexture::PixelConfig fFormat; //!< Format of source data of the
108 // texture. Not guaraunteed to be the
109 // same as internal format used by
110 // 3D API.
111 };
112
113 /**
114 * Gpu usage statistics.
115 */
116 struct Stats {
117 uint32_t fVertexCnt; //<! Number of vertices drawn
118 uint32_t fIndexCnt; //<! Number of indices drawn
119 uint32_t fDrawCnt; //<! Number of draws
120
121 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
122
123 /*
124 * Number of times the texture is set in 3D API
125 */
126 uint32_t fTextureChngCnt;
127 /*
128 * Number of times the render target is set in 3D API
129 */
130 uint32_t fRenderTargetChngCnt;
131 /*
132 * Number of textures created (includes textures that are rendertargets).
133 */
134 uint32_t fTextureCreateCnt;
135 /*
136 * Number of rendertargets created.
137 */
138 uint32_t fRenderTargetCreateCnt;
139 };
140
141 ////////////////////////////////////////////////////////////////////////////
142
143 GrGpu();
144 virtual ~GrGpu();
145
146 /**
147 * The GrGpu object normally assumes that no outsider is setting state
148 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000149 * the GrGpu that the state was modified and it shouldn't make assumptions
150 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
154 void unimpl(const char[]);
155
156 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000157 * Creates a texture object. If desc width or height is not a power of
158 * two but underlying API requires a power of two texture then srcData
159 * will be embedded in a power of two texture. The extra width and height
160 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000162 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000163 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
164 * on the render target until its releaseRenderTarget() is called or it is
165 * destroyed.
166 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 * @param desc describes the texture to be created.
168 * @param srcData texel data to load texture. Begins with full-size
169 * palette data for paletted textures. Contains width*
170 * height texels. If NULL texture data is uninitialized.
171 *
172 * @return The texture object if successful, otherwise NULL.
173 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000174 GrTexture* createTexture(const TextureDesc& desc,
175 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 /**
177 * Wraps an externally-created rendertarget in a GrRenderTarget.
178 * @param platformRenderTarget handle to the the render target in the
179 * underlying 3D API. Interpretation depends on
180 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000181 * @param stencilBits number of stencil bits the target has
reed@google.comac10a2d2010-12-22 21:39:39 +0000182 * @param width width of the render target
183 * @param height height of the render target
184 */
185 virtual GrRenderTarget* createPlatformRenderTarget(
186 intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000187 int stencilBits,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000188 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000189
190 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000191 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
192 * viewport state from the underlying 3D API and wraps it in a
193 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
194 * underlying object in its destructor and it is up to caller to guarantee
195 * that it remains valid while the GrRenderTarget is used.
196 *
197 * @return the newly created GrRenderTarget
198 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000199 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000200
201 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 * Creates a vertex buffer.
203 *
204 * @param size size in bytes of the vertex buffer
205 * @param dynamic hints whether the data will be frequently changed
206 * by either GrVertexBuffer::lock or
207 * GrVertexBuffer::updateData.
208 *
209 * @return The vertex buffer if successful, otherwise NULL.
210 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
213 /**
214 * Creates an index buffer.
215 *
216 * @param size size in bytes of the index buffer
217 * @param dynamic hints whether the data will be frequently changed
218 * by either GrIndexBuffer::lock or
219 * GrIndexBuffer::updateData.
220 *
221 * @return The index buffer if successful, otherwise NULL.
222 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
225 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 * Erase the entire render target, ignoring any clips/scissors.
227 *
228 * This is issued to the GPU driver immediately.
229 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230 void eraseColor(GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 /**
233 * Are 8 bit paletted textures supported.
234 *
235 * @return true if 8bit palette textures are supported, false otherwise
236 */
237 bool supports8BitPalette() const { return f8bitPaletteSupport; }
238
239 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000240 * returns true if two sided stenciling is supported. If false then only
241 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 * @return true if only a single stencil pass is needed.
243 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000244 bool supportsTwoSidedStencil() const
245 { return fTwoSidedStencilSupport; }
246
247 /**
248 * returns true if stencil wrap is supported. If false then
249 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
250 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
251 * @return true if stencil wrap ops are supported.
252 */
253 bool supportsStencilWrapOps() const
254 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
256 /**
257 * Checks whether locking vertex and index buffers is supported.
258 *
259 * @return true if locking is supported.
260 */
261 bool supportsBufferLocking() const { return fBufferLockSupport; }
262
263 /**
264 * Gets the minimum width of a render target. If a texture/rt is created
265 * with a width less than this size the GrGpu object will clamp it to this
266 * value.
267 */
268 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
269
270 /**
271 * Gets the minimum width of a render target. If a texture/rt is created
272 * with a height less than this size the GrGpu object will clamp it to this
273 * value.
274 */
275 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
276
277 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000278 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000280 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000282 bool npotTextureSupport() const { return fNPOTTextureSupport; }
283
284 /**
285 * Returns true if NPOT textures can be repeat/mirror tiled.
286 *
287 * @return true if NPOT textures can be tiled
288 */
289 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
290
291 /**
292 * Returns true if a NPOT texture can be a rendertarget
293 *
294 * @return the true if NPOT texture/rendertarget can be created.
295 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000297
reed@google.com02a7e6c2011-01-28 21:21:49 +0000298 int maxTextureDimension() const { return fMaxTextureDimension; }
299
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000301 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302 int startVertex,
303 int startIndex,
304 int vertexCount,
305 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000306
bsalomon@google.comffca4002011-02-22 20:34:01 +0000307 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000308 int startVertex,
309 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000310
311 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000312 * Installs a path renderer that will be used to draw paths that are
313 * part of the clip.
314 */
315 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
316 GrSafeAssign(fClientPathRenderer, pathRenderer);
317 }
318
319 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000321 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
322 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000324 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000325 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000326 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
328 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000330 * (1,1), (0,1)].
331 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000332 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000334
335 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000336 * Ensures that the current render target is actually set in the
337 * underlying 3D API. Used when client wants to use 3D API to directly
338 * render to the RT.
339 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000340 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000341
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000342 /**
343 * Reads a rectangle of pixels from the current render target.
344 * @param left left edge of the rectangle to read (inclusive)
345 * @param top top edge of the rectangle to read (inclusive)
346 * @param width width of rectangle to read in pixels.
347 * @param height height of rectangle to read in pixels.
348 * @param buffer memory to read the rectangle into.
349 *
350 * @return true if the read succeeded, false if not. The read can fail
351 * because of a unsupported pixel config or because no render
352 * target is currently set.
353 */
354 bool readPixels(int left, int top, int width, int height,
355 GrTexture::PixelConfig, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000356
reed@google.comac10a2d2010-12-22 21:39:39 +0000357 const Stats& getStats() const;
358 void resetStats();
359 void printStats() const;
360
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000361 /**
362 * Called to tell Gpu object that all GrResources have been lost and should
363 * be abandoned.
364 */
365 void abandonResources();
366
367 /**
368 * Called to tell Gpu object to release all GrResources.
369 */
370 void releaseResources();
371
372 /**
373 * Add resource to list of resources. Should only be called by GrResource.
374 * @param resource the resource to add.
375 */
376 void insertResource(GrResource* resource);
377
378 /**
379 * Remove resource from list of resources. Should only be called by
380 * GrResource.
381 * @param resource the resource to remove.
382 */
383 void removeResource(GrResource* resource);
384
reed@google.comac10a2d2010-12-22 21:39:39 +0000385protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000386 enum PrivateStateBits {
387 kFirstBit = (kLastPublicStateBit << 1),
388
389 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
390 // stencil bits used for
391 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 };
393
394 /**
395 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
396 */
397 struct ClipState {
398 bool fClipInStencil;
399 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000400 } fClipState;
401
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000402 // GrDrawTarget override
403 virtual void clipWillBeSet(const GrClip& newClip);
404
405 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000406 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
bsalomon@google.comd302f142011-03-03 13:54:13 +0000408 // Functions used to map clip-respecting stencil tests into normal
409 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000410 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411 GrStencilFunc func);
412 static void ConvertStencilFuncAndMask(GrStencilFunc func,
413 bool clipInStencil,
414 unsigned int clipBit,
415 unsigned int userBits,
416 unsigned int* ref,
417 unsigned int* mask);
418
419 // stencil settings to clip drawing when stencil clipping is in effect
420 // and the client isn't using the stencil test.
421 static const GrStencilSettings gClipStencilSettings;
422
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 // defaults to false, subclass can set true to support palleted textures
424 bool f8bitPaletteSupport;
425
bsalomon@google.com0748f212011-02-01 22:56:16 +0000426 // set by subclass
427 bool fNPOTTextureSupport;
428 bool fNPOTTextureTileSupport;
429 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000430 bool fTwoSidedStencilSupport;
431 bool fStencilWrapOpsSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000432
433 // set by subclass to true if index and vertex buffers can be locked, false
434 // otherwise.
435 bool fBufferLockSupport;
436
437 // set by subclass
438 int fMinRenderTargetWidth;
439 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000440 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000442 Stats fStats;
443
444 const GrVertexBuffer* fCurrPoolVertexBuffer;
445 int fCurrPoolStartVertex;
446
447 const GrIndexBuffer* fCurrPoolIndexBuffer;
448 int fCurrPoolStartIndex;
449
450 // GrDrawTarget overrides
451 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
452 void** vertices,
453 void** indices);
454 virtual void releaseGeometryHelper();
455
456 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
457 int vertexCount);
458
459 virtual void setIndexSourceToArrayHelper(const void* indexArray,
460 int indexCount);
461 // Helpers for setting up geometry state
462 void finalizeReservedVertices();
463 void finalizeReservedIndices();
464
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000465 // overridden by API-specific derived class to handle re-emitting 3D API
466 // preample and dirtying state cache.
467 virtual void resetContext() = 0;
468
469 // overridden by API-specific derived class to create objects.
470 virtual GrTexture* createTextureHelper(const TextureDesc& desc,
471 const void* srcData,
472 size_t rowBytes) = 0;
473 virtual GrRenderTarget* createPlatformRenderTargetHelper(
474 intptr_t platformRenderTarget,
475 int stencilBits,
476 int width, int height) = 0;
477 virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0;
478 virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size,
479 bool dynamic) = 0;
480 virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size,
481 bool dynamic) = 0;
482
483 // overridden by API-specific derivated class to perform the erase.
484 virtual void eraseColorHelper(GrColor color) = 0;
485
486 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000487 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 uint32_t startVertex,
489 uint32_t startIndex,
490 uint32_t vertexCount,
491 uint32_t indexCount) = 0;
492
bsalomon@google.comffca4002011-02-22 20:34:01 +0000493 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 uint32_t vertexCount,
495 uint32_t numVertices) = 0;
496
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000497 // overridden by API-specific derived class to perform flush
498 virtual void forceRenderTargetFlushHelper() = 0;
499
500 // overridden by API-specific derived class to perform the read pixels.
501 virtual bool readPixelsHelper(int left, int top, int width, int height,
502 GrTexture::PixelConfig, void* buffer) = 0;
503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000505 // indexed geometry. The subclass may adjust the startVertex and/or
506 // startIndex since it may have already accounted for these in the setup.
507 virtual void setupGeometry(int* startVertex,
508 int* startIndex,
509 int vertexCount,
510 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511
512
513 // The GrGpu typically records the clients requested state and then flushes
514 // deltas from previous state at draw time. This function does the
515 // API-specific flush of the state
516 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000517 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
519 // Sets the scissor rect, or disables if rect is NULL.
520 virtual void flushScissor(const GrIRect* rect) = 0;
521
522 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +0000523 virtual void eraseStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
reed@google.comac10a2d2010-12-22 21:39:39 +0000525private:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000526 // readies the pools to provide vertex/index data.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000527 void prepareVertexPool();
528 void prepareIndexPool();
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000529
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000530 // determines the path renderer used to draw a clip path element.
531 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
532 GrPathFill fill);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000533
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000534 void handleDirtyContext() {
535 if (fContextIsDirty) {
536 this->resetContext();
537 fContextIsDirty = false;
538 }
539 }
540
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000541 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000542
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000543 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000545 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
546 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000548 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
549 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000550
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000551 GrDefaultPathRenderer* fDefaultPathRenderer;
552 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000553
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000554 bool fContextIsDirty;
555
bsalomon@google.comd302f142011-03-03 13:54:13 +0000556 // when in an internal draw these indicate whether the pools are in use
557 // by one of the outer draws. If false then it is safe to reset the
558 // pool.
559 bool fVertexPoolInUse;
560 bool fIndexPoolInUse;
561
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000562 GrResource* fResourceHead;
563
bsalomon@google.comd302f142011-03-03 13:54:13 +0000564 // used to save and restore state when the GrGpu needs
565 // to make its geometry pools available internally
566 class AutoInternalDrawGeomRestore {
567 public:
568 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
569 fGpu = gpu;
570
571 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
572 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
573
574 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
575 (kBuffer_GeometrySrcType !=
576 gpu->fGeometrySrc.fVertexSrc);
577 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
578 (kBuffer_GeometrySrcType !=
579 gpu->fGeometrySrc.fIndexSrc);;
580
581 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
582 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
583 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
584 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
585
586 fSavedReservedGeometry = gpu->fReservedGeometry;
587 gpu->fReservedGeometry.fLocked = false;
588 }
589 ~AutoInternalDrawGeomRestore() {
590 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
591 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
592 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
593 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
594 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
595 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
596 fGpu->fReservedGeometry = fSavedReservedGeometry;
597 }
598 private:
599 AutoGeometrySrcRestore fAgsr;
600 GrGpu* fGpu;
601 const GrVertexBuffer* fSavedPoolVertexBuffer;
602 int fSavedPoolStartVertex;
603 const GrIndexBuffer* fSavedPoolIndexBuffer;
604 int fSavedPoolStartIndex;
605 bool fVertexPoolWasInUse;
606 bool fIndexPoolWasInUse;
607 ReservedGeometry fSavedReservedGeometry;
608 };
609
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000610 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000611};
612
613#endif