blob: 16bb6b6933487a3a31706d97d74406b149471a30 [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 * Gpu usage statistics.
61 */
62 struct Stats {
63 uint32_t fVertexCnt; //<! Number of vertices drawn
64 uint32_t fIndexCnt; //<! Number of indices drawn
65 uint32_t fDrawCnt; //<! Number of draws
66
67 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
68
69 /*
70 * Number of times the texture is set in 3D API
71 */
72 uint32_t fTextureChngCnt;
73 /*
74 * Number of times the render target is set in 3D API
75 */
76 uint32_t fRenderTargetChngCnt;
77 /*
78 * Number of textures created (includes textures that are rendertargets).
79 */
80 uint32_t fTextureCreateCnt;
81 /*
82 * Number of rendertargets created.
83 */
84 uint32_t fRenderTargetCreateCnt;
85 };
86
87 ////////////////////////////////////////////////////////////////////////////
88
89 GrGpu();
90 virtual ~GrGpu();
91
bsalomon@google.com669fdc42011-04-05 17:08:27 +000092 // The GrContext sets itself as the owner of this Gpu object
93 void setContext(GrContext* context) {
94 GrAssert(NULL == fContext);
95 fContext = context;
96 }
97 GrContext* getContext() { return fContext; }
98 const GrContext* getContext() const { return fContext; }
99
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 /**
101 * The GrGpu object normally assumes that no outsider is setting state
102 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000103 * the GrGpu that the state was modified and it shouldn't make assumptions
104 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000106 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000107
108 void unimpl(const char[]);
109
110 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000111 * Creates a texture object. If desc width or height is not a power of
112 * two but underlying API requires a power of two texture then srcData
113 * will be embedded in a power of two texture. The extra width and height
114 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000117 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
118 * on the render target until its releaseRenderTarget() is called or it is
119 * destroyed.
120 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 * @param desc describes the texture to be created.
122 * @param srcData texel data to load texture. Begins with full-size
123 * palette data for paletted textures. Contains width*
124 * height texels. If NULL texture data is uninitialized.
125 *
126 * @return The texture object if successful, otherwise NULL.
127 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000128 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000129 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 /**
131 * Wraps an externally-created rendertarget in a GrRenderTarget.
132 * @param platformRenderTarget handle to the the render target in the
133 * underlying 3D API. Interpretation depends on
134 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000135 * @param stencilBits number of stencil bits the target has
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000136 * @param isMultisampled specify whether the RT is multisampled
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 * @param width width of the render target
138 * @param height height of the render target
139 */
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000140 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000141 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000142 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000143 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000145 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
146
reed@google.comac10a2d2010-12-22 21:39:39 +0000147 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000148 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
149 * viewport state from the underlying 3D API and wraps it in a
150 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
151 * underlying object in its destructor and it is up to caller to guarantee
152 * that it remains valid while the GrRenderTarget is used.
153 *
154 * @return the newly created GrRenderTarget
155 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000156 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000157
158 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 * Creates a vertex buffer.
160 *
161 * @param size size in bytes of the vertex buffer
162 * @param dynamic hints whether the data will be frequently changed
163 * by either GrVertexBuffer::lock or
164 * GrVertexBuffer::updateData.
165 *
166 * @return The vertex buffer if successful, otherwise NULL.
167 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000168 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000169
170 /**
171 * Creates an index buffer.
172 *
173 * @param size size in bytes of the index buffer
174 * @param dynamic hints whether the data will be frequently changed
175 * by either GrIndexBuffer::lock or
176 * GrIndexBuffer::updateData.
177 *
178 * @return The index buffer if successful, otherwise NULL.
179 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000180 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
182 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000183 * Are 8 bit paletted textures supported.
184 *
185 * @return true if 8bit palette textures are supported, false otherwise
186 */
187 bool supports8BitPalette() const { return f8bitPaletteSupport; }
188
189 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000190 * returns true if two sided stenciling is supported. If false then only
191 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 * @return true if only a single stencil pass is needed.
193 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000194 bool supportsTwoSidedStencil() const
195 { return fTwoSidedStencilSupport; }
196
197 /**
198 * returns true if stencil wrap is supported. If false then
199 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
200 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
201 * @return true if stencil wrap ops are supported.
202 */
203 bool supportsStencilWrapOps() const
204 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
206 /**
207 * Checks whether locking vertex and index buffers is supported.
208 *
209 * @return true if locking is supported.
210 */
211 bool supportsBufferLocking() const { return fBufferLockSupport; }
212
213 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000214 * Does the 3D API support anti-aliased lines. If so then line primitive
215 * types will use this functionality when the AA state flag is set.
216 */
217 bool supportsAALines() const { return fAALineSupport; }
218
219 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 * Gets the minimum width of a render target. If a texture/rt is created
221 * with a width less than this size the GrGpu object will clamp it to this
222 * value.
223 */
224 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
225
226 /**
227 * Gets the minimum width of a render target. If a texture/rt is created
228 * with a height less than this size the GrGpu object will clamp it to this
229 * value.
230 */
231 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000232
233 /**
234 * Reports whether full scene anti-aliasing is supported.
235 */
236 bool supportsFullsceneAA() const { return fFSAASupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
238 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000239 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000241 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000243 bool npotTextureSupport() const { return fNPOTTextureSupport; }
244
245 /**
246 * Returns true if NPOT textures can be repeat/mirror tiled.
247 *
248 * @return true if NPOT textures can be tiled
249 */
250 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
251
252 /**
253 * Returns true if a NPOT texture can be a rendertarget
254 *
255 * @return the true if NPOT texture/rendertarget can be created.
256 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000257 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000258
reed@google.com02a7e6c2011-01-28 21:21:49 +0000259 int maxTextureDimension() const { return fMaxTextureDimension; }
260
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000262 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000263 int startVertex,
264 int startIndex,
265 int vertexCount,
266 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
bsalomon@google.comffca4002011-02-22 20:34:01 +0000268 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000269 int startVertex,
270 int vertexCount);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000271 virtual void clear(const GrIRect* rect, GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
273 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000274 * Installs a path renderer that will be used to draw paths that are
275 * part of the clip.
276 */
277 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
278 GrSafeAssign(fClientPathRenderer, pathRenderer);
279 }
280
281 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000283 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
284 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000286 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000288 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
290 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000291 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000292 * (1,1), (0,1)].
293 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000294 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000295 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000296
297 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 * Ensures that the current render target is actually set in the
299 * underlying 3D API. Used when client wants to use 3D API to directly
300 * render to the RT.
301 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000302 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000304 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000305 * Reads a rectangle of pixels from a render target.
306 * @param renderTarget the render target to read from. NULL means the
307 * current render target.
308 * @param left left edge of the rectangle to read (inclusive)
309 * @param top top edge of the rectangle to read (inclusive)
310 * @param width width of rectangle to read in pixels.
311 * @param height height of rectangle to read in pixels.
312 * @param config the pixel config of the destination buffer
313 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000314 *
315 * @return true if the read succeeded, false if not. The read can fail
316 * because of a unsupported pixel config or because no render
317 * target is currently set.
318 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000319 bool readPixels(GrRenderTarget* renderTarget,
320 int left, int top, int width, int height,
321 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000322
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 const Stats& getStats() const;
324 void resetStats();
325 void printStats() const;
326
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000327 /**
328 * Called to tell Gpu object that all GrResources have been lost and should
329 * be abandoned.
330 */
331 void abandonResources();
332
333 /**
334 * Called to tell Gpu object to release all GrResources.
335 */
336 void releaseResources();
337
338 /**
339 * Add resource to list of resources. Should only be called by GrResource.
340 * @param resource the resource to add.
341 */
342 void insertResource(GrResource* resource);
343
344 /**
345 * Remove resource from list of resources. Should only be called by
346 * GrResource.
347 * @param resource the resource to remove.
348 */
349 void removeResource(GrResource* resource);
350
reed@google.comac10a2d2010-12-22 21:39:39 +0000351protected:
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;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000398 bool fAALineSupport;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000399 bool fFSAASupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000400
401 // set by subclass to true if index and vertex buffers can be locked, false
402 // otherwise.
403 bool fBufferLockSupport;
404
405 // set by subclass
406 int fMinRenderTargetWidth;
407 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000408 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000410 Stats fStats;
411
412 const GrVertexBuffer* fCurrPoolVertexBuffer;
413 int fCurrPoolStartVertex;
414
415 const GrIndexBuffer* fCurrPoolIndexBuffer;
416 int fCurrPoolStartIndex;
417
418 // GrDrawTarget overrides
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000419 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
420 void** vertices,
421 void** indices);
422 virtual void onReleaseGeometry();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000423
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000424 virtual void onSetVertexSourceToArray(const void* vertexArray,
425 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000426
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000427 virtual void onSetIndexSourceToArray(const void* indexArray,
428 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000429 // Helpers for setting up geometry state
430 void finalizeReservedVertices();
431 void finalizeReservedIndices();
432
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000433 // overridden by API-specific derived class to handle re-emitting 3D API
434 // preample and dirtying state cache.
435 virtual void resetContext() = 0;
436
437 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000438 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000439 const void* srcData,
440 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000441 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000442 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000443 intptr_t platformRenderTarget,
444 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000445 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000446 int width, int height) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000447 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
448 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
449 bool dynamic) = 0;
450 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
451 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000453 // overridden by API-specific derivated class to perform the clear and
454 // clearRect. NULL rect means clear whole target.
455 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000456
457 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000458 virtual void onDrawIndexed(GrPrimitiveType type,
459 uint32_t startVertex,
460 uint32_t startIndex,
461 uint32_t vertexCount,
462 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000464 virtual void onDrawNonIndexed(GrPrimitiveType type,
465 uint32_t vertexCount,
466 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000468 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000469 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000470
471 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000472 virtual bool onReadPixels(GrRenderTarget* target,
473 int left, int top, int width, int height,
474 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000475
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000477 // indexed geometry. The subclass may adjust the startVertex and/or
478 // startIndex since it may have already accounted for these in the setup.
479 virtual void setupGeometry(int* startVertex,
480 int* startIndex,
481 int vertexCount,
482 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
484
485 // The GrGpu typically records the clients requested state and then flushes
486 // deltas from previous state at draw time. This function does the
487 // API-specific flush of the state
488 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000489 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
491 // Sets the scissor rect, or disables if rect is NULL.
492 virtual void flushScissor(const GrIRect* rect) = 0;
493
494 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000495 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
reed@google.comac10a2d2010-12-22 21:39:39 +0000497private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000498 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000499
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000500 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000502 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000504 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
505 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000506
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000507 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
508 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000509
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000510 GrDefaultPathRenderer* fDefaultPathRenderer;
511 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000512
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000513 bool fContextIsDirty;
514
bsalomon@google.comd302f142011-03-03 13:54:13 +0000515 // when in an internal draw these indicate whether the pools are in use
516 // by one of the outer draws. If false then it is safe to reset the
517 // pool.
518 bool fVertexPoolInUse;
519 bool fIndexPoolInUse;
520
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000521 GrResource* fResourceHead;
522
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523 // readies the pools to provide vertex/index data.
524 void prepareVertexPool();
525 void prepareIndexPool();
526
527 // determines the path renderer used to draw a clip path element.
528 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
529 GrPathFill fill);
530
531 void handleDirtyContext() {
532 if (fContextIsDirty) {
533 this->resetContext();
534 fContextIsDirty = false;
535 }
536 }
537
bsalomon@google.comd302f142011-03-03 13:54:13 +0000538 // used to save and restore state when the GrGpu needs
539 // to make its geometry pools available internally
540 class AutoInternalDrawGeomRestore {
541 public:
542 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
543 fGpu = gpu;
544
545 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
546 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
547
548 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
549 (kBuffer_GeometrySrcType !=
550 gpu->fGeometrySrc.fVertexSrc);
551 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
552 (kBuffer_GeometrySrcType !=
553 gpu->fGeometrySrc.fIndexSrc);;
554
555 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
556 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
557 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
558 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
559
560 fSavedReservedGeometry = gpu->fReservedGeometry;
561 gpu->fReservedGeometry.fLocked = false;
562 }
563 ~AutoInternalDrawGeomRestore() {
564 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
565 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
566 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
567 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
568 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
569 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
570 fGpu->fReservedGeometry = fSavedReservedGeometry;
571 }
572 private:
573 AutoGeometrySrcRestore fAgsr;
574 GrGpu* fGpu;
575 const GrVertexBuffer* fSavedPoolVertexBuffer;
576 int fSavedPoolStartVertex;
577 const GrIndexBuffer* fSavedPoolIndexBuffer;
578 int fSavedPoolStartIndex;
579 bool fVertexPoolWasInUse;
580 bool fIndexPoolWasInUse;
581 ReservedGeometry fSavedReservedGeometry;
582 };
583
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000584 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000585};
586
587#endif