blob: cbe3e95e3333d43a457025c14edad1392e8b3531 [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
bsalomon@google.com669fdc42011-04-05 17:08:27 +000020#include "GrDrawTarget.h"
21#include "GrPathRenderer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022#include "GrRect.h"
23#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com669fdc42011-04-05 17:08:27 +000026class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027class GrIndexBufferAllocPool;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028class GrResource;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000029class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31class GrGpu : public GrDrawTarget {
32
33public:
34 /**
35 * Possible 3D APIs that may be used by Ganesh.
36 */
37 enum Engine {
38 kOpenGL_Shaders_Engine,
39 kOpenGL_Fixed_Engine,
40 kDirect3D9_Engine
41 };
42
43 /**
44 * Platform specific 3D context.
45 * For
46 * kOpenGL_Shaders_Engine use NULL
47 * kOpenGL_Fixed_Engine use NULL
48 * kDirect3D9_Engine use an IDirect3DDevice9*
49 */
50 typedef void* Platform3DContext;
51
52 /**
53 * Create an instance of GrGpu that matches the specified Engine backend.
54 * If the requested engine is not supported (at compile-time or run-time)
55 * this returns NULL.
56 */
57 static GrGpu* Create(Engine, Platform3DContext context3D);
58
59 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000060 * Used to control the level of antialiasing available for a rendertarget.
61 * Anti-alias quality levels depend on the underlying API/GPU capabilities.
62 */
63 enum AALevels {
64 kNone_AALevel, //<! No antialiasing available.
65 kLow_AALevel, //<! Low quality antialiased rendering. Actual
66 // interpretation is platform-dependent.
67 kMed_AALevel, //<! Medium quality antialiased rendering. Actual
68 // interpretation is platform-dependent.
69 kHigh_AALevel, //<! High quality antialiased rendering. Actual
70 // interpretation is platform-dependent.
71 };
72
73
74 /**
75 * Optional bitfield flags that can be passed to createTexture.
76 */
77 enum TextureFlags {
78 kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be
79 // rendered to by calling
80 // GrGpu::setRenderTarget() with
81 // GrTexture::asRenderTarget().
bsalomon@google.comf6a7c112011-03-24 16:14:10 +000082 kNoStencil_TextureFlag = 0x2, //<! If the texture is used as a
83 // rendertarget but a stencil
84 // buffer is not required. Stencil
85 // may be required for clipping and
86 // path rendering.
reed@google.comac10a2d2010-12-22 21:39:39 +000087 kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify
88 // this texture after creation
89 };
90
91 enum {
92 /**
93 * For Index8 pixel config, the colortable must be 256 entries
94 */
95 kColorTableSize = 256 * sizeof(GrColor)
96 };
97 /**
98 * Describes a texture to be created.
99 */
100 struct TextureDesc {
101 uint32_t fFlags; //!< bitfield of TextureFlags
102 GrGpu::AALevels fAALevel;//!< The level of antialiasing available
103 // for a rendertarget texture. Only
104 // flags contains
105 // kRenderTarget_TextureFlag.
106 uint32_t fWidth; //!< Width of the texture
107 uint32_t fHeight; //!< Height of the texture
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000108 GrPixelConfig fFormat; //!< Format of source data of the
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 // texture. Not guaraunteed to be the
110 // same as internal format used by
111 // 3D API.
112 };
113
114 /**
115 * Gpu usage statistics.
116 */
117 struct Stats {
118 uint32_t fVertexCnt; //<! Number of vertices drawn
119 uint32_t fIndexCnt; //<! Number of indices drawn
120 uint32_t fDrawCnt; //<! Number of draws
121
122 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
123
124 /*
125 * Number of times the texture is set in 3D API
126 */
127 uint32_t fTextureChngCnt;
128 /*
129 * Number of times the render target is set in 3D API
130 */
131 uint32_t fRenderTargetChngCnt;
132 /*
133 * Number of textures created (includes textures that are rendertargets).
134 */
135 uint32_t fTextureCreateCnt;
136 /*
137 * Number of rendertargets created.
138 */
139 uint32_t fRenderTargetCreateCnt;
140 };
141
142 ////////////////////////////////////////////////////////////////////////////
143
144 GrGpu();
145 virtual ~GrGpu();
146
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000147 // The GrContext sets itself as the owner of this Gpu object
148 void setContext(GrContext* context) {
149 GrAssert(NULL == fContext);
150 fContext = context;
151 }
152 GrContext* getContext() { return fContext; }
153 const GrContext* getContext() const { return fContext; }
154
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 /**
156 * The GrGpu object normally assumes that no outsider is setting state
157 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158 * the GrGpu that the state was modified and it shouldn't make assumptions
159 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
163 void unimpl(const char[]);
164
165 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000166 * Creates a texture object. If desc width or height is not a power of
167 * two but underlying API requires a power of two texture then srcData
168 * will be embedded in a power of two texture. The extra width and height
169 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000171 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000172 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
173 * on the render target until its releaseRenderTarget() is called or it is
174 * destroyed.
175 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 * @param desc describes the texture to be created.
177 * @param srcData texel data to load texture. Begins with full-size
178 * palette data for paletted textures. Contains width*
179 * height texels. If NULL texture data is uninitialized.
180 *
181 * @return The texture object if successful, otherwise NULL.
182 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000183 GrTexture* createTexture(const TextureDesc& desc,
184 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 /**
186 * Wraps an externally-created rendertarget in a GrRenderTarget.
187 * @param platformRenderTarget handle to the the render target in the
188 * underlying 3D API. Interpretation depends on
189 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000190 * @param stencilBits number of stencil bits the target has
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 * @param width width of the render target
192 * @param height height of the render target
193 */
194 virtual GrRenderTarget* createPlatformRenderTarget(
195 intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000196 int stencilBits,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000197 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000198
199 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000200 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
201 * viewport state from the underlying 3D API and wraps it in a
202 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
203 * underlying object in its destructor and it is up to caller to guarantee
204 * that it remains valid while the GrRenderTarget is used.
205 *
206 * @return the newly created GrRenderTarget
207 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000209
210 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 * Creates a vertex buffer.
212 *
213 * @param size size in bytes of the vertex buffer
214 * @param dynamic hints whether the data will be frequently changed
215 * by either GrVertexBuffer::lock or
216 * GrVertexBuffer::updateData.
217 *
218 * @return The vertex buffer if successful, otherwise NULL.
219 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
222 /**
223 * Creates an index buffer.
224 *
225 * @param size size in bytes of the index buffer
226 * @param dynamic hints whether the data will be frequently changed
227 * by either GrIndexBuffer::lock or
228 * GrIndexBuffer::updateData.
229 *
230 * @return The index buffer if successful, otherwise NULL.
231 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000232 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
234 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 * Erase the entire render target, ignoring any clips/scissors.
236 *
237 * This is issued to the GPU driver immediately.
238 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239 void eraseColor(GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
241 /**
242 * Are 8 bit paletted textures supported.
243 *
244 * @return true if 8bit palette textures are supported, false otherwise
245 */
246 bool supports8BitPalette() const { return f8bitPaletteSupport; }
247
248 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000249 * returns true if two sided stenciling is supported. If false then only
250 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 * @return true if only a single stencil pass is needed.
252 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000253 bool supportsTwoSidedStencil() const
254 { return fTwoSidedStencilSupport; }
255
256 /**
257 * returns true if stencil wrap is supported. If false then
258 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
259 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
260 * @return true if stencil wrap ops are supported.
261 */
262 bool supportsStencilWrapOps() const
263 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000264
265 /**
266 * Checks whether locking vertex and index buffers is supported.
267 *
268 * @return true if locking is supported.
269 */
270 bool supportsBufferLocking() const { return fBufferLockSupport; }
271
272 /**
273 * Gets the minimum width of a render target. If a texture/rt is created
274 * with a width less than this size the GrGpu object will clamp it to this
275 * value.
276 */
277 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
278
279 /**
280 * Gets the minimum width of a render target. If a texture/rt is created
281 * with a height less than this size the GrGpu object will clamp it to this
282 * value.
283 */
284 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
285
286 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000287 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000289 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000291 bool npotTextureSupport() const { return fNPOTTextureSupport; }
292
293 /**
294 * Returns true if NPOT textures can be repeat/mirror tiled.
295 *
296 * @return true if NPOT textures can be tiled
297 */
298 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
299
300 /**
301 * Returns true if a NPOT texture can be a rendertarget
302 *
303 * @return the true if NPOT texture/rendertarget can be created.
304 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000305 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000306
reed@google.com02a7e6c2011-01-28 21:21:49 +0000307 int maxTextureDimension() const { return fMaxTextureDimension; }
308
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000310 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000311 int startVertex,
312 int startIndex,
313 int vertexCount,
314 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.comffca4002011-02-22 20:34:01 +0000316 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000317 int startVertex,
318 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
320 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000321 * Installs a path renderer that will be used to draw paths that are
322 * part of the clip.
323 */
324 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
325 GrSafeAssign(fClientPathRenderer, pathRenderer);
326 }
327
328 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000330 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
331 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000335 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000336
337 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000339 * (1,1), (0,1)].
340 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000341 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000342 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000343
344 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 * Ensures that the current render target is actually set in the
346 * underlying 3D API. Used when client wants to use 3D API to directly
347 * render to the RT.
348 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000349 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000350
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000351 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000352 * Reads a rectangle of pixels from a render target.
353 * @param renderTarget the render target to read from. NULL means the
354 * current render target.
355 * @param left left edge of the rectangle to read (inclusive)
356 * @param top top edge of the rectangle to read (inclusive)
357 * @param width width of rectangle to read in pixels.
358 * @param height height of rectangle to read in pixels.
359 * @param config the pixel config of the destination buffer
360 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000361 *
362 * @return true if the read succeeded, false if not. The read can fail
363 * because of a unsupported pixel config or because no render
364 * target is currently set.
365 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000366 bool readPixels(GrRenderTarget* renderTarget,
367 int left, int top, int width, int height,
368 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000369
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 const Stats& getStats() const;
371 void resetStats();
372 void printStats() const;
373
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000374 /**
375 * Called to tell Gpu object that all GrResources have been lost and should
376 * be abandoned.
377 */
378 void abandonResources();
379
380 /**
381 * Called to tell Gpu object to release all GrResources.
382 */
383 void releaseResources();
384
385 /**
386 * Add resource to list of resources. Should only be called by GrResource.
387 * @param resource the resource to add.
388 */
389 void insertResource(GrResource* resource);
390
391 /**
392 * Remove resource from list of resources. Should only be called by
393 * GrResource.
394 * @param resource the resource to remove.
395 */
396 void removeResource(GrResource* resource);
397
reed@google.comac10a2d2010-12-22 21:39:39 +0000398protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000399 enum PrivateStateBits {
400 kFirstBit = (kLastPublicStateBit << 1),
401
402 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
403 // stencil bits used for
404 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 };
406
407 /**
408 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
409 */
410 struct ClipState {
411 bool fClipInStencil;
412 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 } fClipState;
414
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000415 // GrDrawTarget override
416 virtual void clipWillBeSet(const GrClip& newClip);
417
418 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000419 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.comd302f142011-03-03 13:54:13 +0000421 // Functions used to map clip-respecting stencil tests into normal
422 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000423 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000424 GrStencilFunc func);
425 static void ConvertStencilFuncAndMask(GrStencilFunc func,
426 bool clipInStencil,
427 unsigned int clipBit,
428 unsigned int userBits,
429 unsigned int* ref,
430 unsigned int* mask);
431
432 // stencil settings to clip drawing when stencil clipping is in effect
433 // and the client isn't using the stencil test.
434 static const GrStencilSettings gClipStencilSettings;
435
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 // defaults to false, subclass can set true to support palleted textures
437 bool f8bitPaletteSupport;
438
bsalomon@google.com0748f212011-02-01 22:56:16 +0000439 // set by subclass
440 bool fNPOTTextureSupport;
441 bool fNPOTTextureTileSupport;
442 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000443 bool fTwoSidedStencilSupport;
444 bool fStencilWrapOpsSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000445
446 // set by subclass to true if index and vertex buffers can be locked, false
447 // otherwise.
448 bool fBufferLockSupport;
449
450 // set by subclass
451 int fMinRenderTargetWidth;
452 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000453 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000454
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455 Stats fStats;
456
457 const GrVertexBuffer* fCurrPoolVertexBuffer;
458 int fCurrPoolStartVertex;
459
460 const GrIndexBuffer* fCurrPoolIndexBuffer;
461 int fCurrPoolStartIndex;
462
463 // GrDrawTarget overrides
464 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
465 void** vertices,
466 void** indices);
467 virtual void releaseGeometryHelper();
468
469 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
470 int vertexCount);
471
472 virtual void setIndexSourceToArrayHelper(const void* indexArray,
473 int indexCount);
474 // Helpers for setting up geometry state
475 void finalizeReservedVertices();
476 void finalizeReservedIndices();
477
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000478 // overridden by API-specific derived class to handle re-emitting 3D API
479 // preample and dirtying state cache.
480 virtual void resetContext() = 0;
481
482 // overridden by API-specific derived class to create objects.
483 virtual GrTexture* createTextureHelper(const TextureDesc& desc,
484 const void* srcData,
485 size_t rowBytes) = 0;
486 virtual GrRenderTarget* createPlatformRenderTargetHelper(
487 intptr_t platformRenderTarget,
488 int stencilBits,
489 int width, int height) = 0;
490 virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper() = 0;
491 virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size,
492 bool dynamic) = 0;
493 virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size,
494 bool dynamic) = 0;
495
496 // overridden by API-specific derivated class to perform the erase.
497 virtual void eraseColorHelper(GrColor color) = 0;
498
499 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000500 virtual void drawIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 uint32_t startVertex,
502 uint32_t startIndex,
503 uint32_t vertexCount,
504 uint32_t indexCount) = 0;
505
bsalomon@google.comffca4002011-02-22 20:34:01 +0000506 virtual void drawNonIndexedHelper(GrPrimitiveType type,
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 uint32_t vertexCount,
508 uint32_t numVertices) = 0;
509
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000510 // overridden by API-specific derived class to perform flush
511 virtual void forceRenderTargetFlushHelper() = 0;
512
513 // overridden by API-specific derived class to perform the read pixels.
514 virtual bool readPixelsHelper(int left, int top, int width, int height,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000515 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000516
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000518 // indexed geometry. The subclass may adjust the startVertex and/or
519 // startIndex since it may have already accounted for these in the setup.
520 virtual void setupGeometry(int* startVertex,
521 int* startIndex,
522 int vertexCount,
523 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000524
525
526 // The GrGpu typically records the clients requested state and then flushes
527 // deltas from previous state at draw time. This function does the
528 // API-specific flush of the state
529 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000530 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
532 // Sets the scissor rect, or disables if rect is NULL.
533 virtual void flushScissor(const GrIRect* rect) = 0;
534
535 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.comd302f142011-03-03 13:54:13 +0000536 virtual void eraseStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
reed@google.comac10a2d2010-12-22 21:39:39 +0000538private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000539 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000540
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.com669fdc42011-04-05 17:08:27 +0000564 // readies the pools to provide vertex/index data.
565 void prepareVertexPool();
566 void prepareIndexPool();
567
568 // determines the path renderer used to draw a clip path element.
569 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
570 GrPathFill fill);
571
572 void handleDirtyContext() {
573 if (fContextIsDirty) {
574 this->resetContext();
575 fContextIsDirty = false;
576 }
577 }
578
bsalomon@google.comd302f142011-03-03 13:54:13 +0000579 // used to save and restore state when the GrGpu needs
580 // to make its geometry pools available internally
581 class AutoInternalDrawGeomRestore {
582 public:
583 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
584 fGpu = gpu;
585
586 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
587 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
588
589 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
590 (kBuffer_GeometrySrcType !=
591 gpu->fGeometrySrc.fVertexSrc);
592 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
593 (kBuffer_GeometrySrcType !=
594 gpu->fGeometrySrc.fIndexSrc);;
595
596 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
597 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
598 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
599 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
600
601 fSavedReservedGeometry = gpu->fReservedGeometry;
602 gpu->fReservedGeometry.fLocked = false;
603 }
604 ~AutoInternalDrawGeomRestore() {
605 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
606 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
607 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
608 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
609 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
610 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
611 fGpu->fReservedGeometry = fSavedReservedGeometry;
612 }
613 private:
614 AutoGeometrySrcRestore fAgsr;
615 GrGpu* fGpu;
616 const GrVertexBuffer* fSavedPoolVertexBuffer;
617 int fSavedPoolStartVertex;
618 const GrIndexBuffer* fSavedPoolIndexBuffer;
619 int fSavedPoolStartIndex;
620 bool fVertexPoolWasInUse;
621 bool fIndexPoolWasInUse;
622 ReservedGeometry fSavedReservedGeometry;
623 };
624
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000625 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000626};
627
628#endif