blob: 1966f221b065a39a24fdcf7d0aa4c020bdd7820b [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 /**
bsalomon@google.com398109c2011-04-14 18:40:27 +0000183 * Clear the entire render target, ignoring any clips/scissors.
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 *
185 * This is issued to the GPU driver immediately.
186 */
bsalomon@google.com398109c2011-04-14 18:40:27 +0000187 void clear(GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
189 /**
190 * Are 8 bit paletted textures supported.
191 *
192 * @return true if 8bit palette textures are supported, false otherwise
193 */
194 bool supports8BitPalette() const { return f8bitPaletteSupport; }
195
196 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000197 * returns true if two sided stenciling is supported. If false then only
198 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 * @return true if only a single stencil pass is needed.
200 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000201 bool supportsTwoSidedStencil() const
202 { return fTwoSidedStencilSupport; }
203
204 /**
205 * returns true if stencil wrap is supported. If false then
206 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
207 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
208 * @return true if stencil wrap ops are supported.
209 */
210 bool supportsStencilWrapOps() const
211 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000212
213 /**
214 * Checks whether locking vertex and index buffers is supported.
215 *
216 * @return true if locking is supported.
217 */
218 bool supportsBufferLocking() const { return fBufferLockSupport; }
219
220 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000221 * Does the 3D API support anti-aliased lines. If so then line primitive
222 * types will use this functionality when the AA state flag is set.
223 */
224 bool supportsAALines() const { return fAALineSupport; }
225
226 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 * Gets the minimum width of a render target. If a texture/rt is created
228 * with a width less than this size the GrGpu object will clamp it to this
229 * value.
230 */
231 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
232
233 /**
234 * Gets the minimum width of a render target. If a texture/rt is created
235 * with a height less than this size the GrGpu object will clamp it to this
236 * value.
237 */
238 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
239
240 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000241 * Returns 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 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000245 bool npotTextureSupport() const { return fNPOTTextureSupport; }
246
247 /**
248 * Returns true if NPOT textures can be repeat/mirror tiled.
249 *
250 * @return true if NPOT textures can be tiled
251 */
252 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
253
254 /**
255 * Returns true if a NPOT texture can be a rendertarget
256 *
257 * @return the true if NPOT texture/rendertarget can be created.
258 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000259 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
reed@google.com02a7e6c2011-01-28 21:21:49 +0000261 int maxTextureDimension() const { return fMaxTextureDimension; }
262
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000264 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000265 int startVertex,
266 int startIndex,
267 int vertexCount,
268 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.comffca4002011-02-22 20:34:01 +0000270 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000271 int startVertex,
272 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
274 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000275 * Installs a path renderer that will be used to draw paths that are
276 * part of the clip.
277 */
278 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
279 GrSafeAssign(fClientPathRenderer, pathRenderer);
280 }
281
282 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000284 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
285 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000287 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000289 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000290
291 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000292 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000293 * (1,1), (0,1)].
294 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000295 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000296 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000297
298 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 * Ensures that the current render target is actually set in the
300 * underlying 3D API. Used when client wants to use 3D API to directly
301 * render to the RT.
302 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000303 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000304
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000305 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000306 * Reads a rectangle of pixels from a render target.
307 * @param renderTarget the render target to read from. NULL means the
308 * current render target.
309 * @param left left edge of the rectangle to read (inclusive)
310 * @param top top edge of the rectangle to read (inclusive)
311 * @param width width of rectangle to read in pixels.
312 * @param height height of rectangle to read in pixels.
313 * @param config the pixel config of the destination buffer
314 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000315 *
316 * @return true if the read succeeded, false if not. The read can fail
317 * because of a unsupported pixel config or because no render
318 * target is currently set.
319 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000320 bool readPixels(GrRenderTarget* renderTarget,
321 int left, int top, int width, int height,
322 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 const Stats& getStats() const;
325 void resetStats();
326 void printStats() const;
327
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000328 /**
329 * Called to tell Gpu object that all GrResources have been lost and should
330 * be abandoned.
331 */
332 void abandonResources();
333
334 /**
335 * Called to tell Gpu object to release all GrResources.
336 */
337 void releaseResources();
338
339 /**
340 * Add resource to list of resources. Should only be called by GrResource.
341 * @param resource the resource to add.
342 */
343 void insertResource(GrResource* resource);
344
345 /**
346 * Remove resource from list of resources. Should only be called by
347 * GrResource.
348 * @param resource the resource to remove.
349 */
350 void removeResource(GrResource* resource);
351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000353 enum PrivateStateBits {
354 kFirstBit = (kLastPublicStateBit << 1),
355
356 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
357 // stencil bits used for
358 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 };
360
361 /**
362 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
363 */
364 struct ClipState {
365 bool fClipInStencil;
366 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 } fClipState;
368
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000369 // GrDrawTarget override
370 virtual void clipWillBeSet(const GrClip& newClip);
371
372 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000373 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
bsalomon@google.comd302f142011-03-03 13:54:13 +0000375 // Functions used to map clip-respecting stencil tests into normal
376 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000377 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 GrStencilFunc func);
379 static void ConvertStencilFuncAndMask(GrStencilFunc func,
380 bool clipInStencil,
381 unsigned int clipBit,
382 unsigned int userBits,
383 unsigned int* ref,
384 unsigned int* mask);
385
386 // stencil settings to clip drawing when stencil clipping is in effect
387 // and the client isn't using the stencil test.
388 static const GrStencilSettings gClipStencilSettings;
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 // defaults to false, subclass can set true to support palleted textures
391 bool f8bitPaletteSupport;
392
bsalomon@google.com0748f212011-02-01 22:56:16 +0000393 // set by subclass
394 bool fNPOTTextureSupport;
395 bool fNPOTTextureTileSupport;
396 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000397 bool fTwoSidedStencilSupport;
398 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000399 bool fAALineSupport;
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.com398109c2011-04-14 18:40:27 +0000453 // overridden by API-specific derivated class to perform the clear.
454 virtual void onClear(GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000455
456 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000457 virtual void onDrawIndexed(GrPrimitiveType type,
458 uint32_t startVertex,
459 uint32_t startIndex,
460 uint32_t vertexCount,
461 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000463 virtual void onDrawNonIndexed(GrPrimitiveType type,
464 uint32_t vertexCount,
465 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000467 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000468 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469
470 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000471 virtual bool onReadPixels(GrRenderTarget* target,
472 int left, int top, int width, int height,
473 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000474
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000476 // indexed geometry. The subclass may adjust the startVertex and/or
477 // startIndex since it may have already accounted for these in the setup.
478 virtual void setupGeometry(int* startVertex,
479 int* startIndex,
480 int vertexCount,
481 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
483
484 // The GrGpu typically records the clients requested state and then flushes
485 // deltas from previous state at draw time. This function does the
486 // API-specific flush of the state
487 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000488 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000489
490 // Sets the scissor rect, or disables if rect is NULL.
491 virtual void flushScissor(const GrIRect* rect) = 0;
492
493 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000494 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
reed@google.comac10a2d2010-12-22 21:39:39 +0000496private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000498
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000499 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000501 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000503 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
504 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000506 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
507 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000508
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000509 GrDefaultPathRenderer* fDefaultPathRenderer;
510 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000511
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000512 bool fContextIsDirty;
513
bsalomon@google.comd302f142011-03-03 13:54:13 +0000514 // when in an internal draw these indicate whether the pools are in use
515 // by one of the outer draws. If false then it is safe to reset the
516 // pool.
517 bool fVertexPoolInUse;
518 bool fIndexPoolInUse;
519
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000520 GrResource* fResourceHead;
521
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000522 // readies the pools to provide vertex/index data.
523 void prepareVertexPool();
524 void prepareIndexPool();
525
526 // determines the path renderer used to draw a clip path element.
527 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
528 GrPathFill fill);
529
530 void handleDirtyContext() {
531 if (fContextIsDirty) {
532 this->resetContext();
533 fContextIsDirty = false;
534 }
535 }
536
bsalomon@google.comd302f142011-03-03 13:54:13 +0000537 // used to save and restore state when the GrGpu needs
538 // to make its geometry pools available internally
539 class AutoInternalDrawGeomRestore {
540 public:
541 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
542 fGpu = gpu;
543
544 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
545 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
546
547 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
548 (kBuffer_GeometrySrcType !=
549 gpu->fGeometrySrc.fVertexSrc);
550 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
551 (kBuffer_GeometrySrcType !=
552 gpu->fGeometrySrc.fIndexSrc);;
553
554 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
555 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
556 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
557 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
558
559 fSavedReservedGeometry = gpu->fReservedGeometry;
560 gpu->fReservedGeometry.fLocked = false;
561 }
562 ~AutoInternalDrawGeomRestore() {
563 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
564 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
565 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
566 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
567 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
568 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
569 fGpu->fReservedGeometry = fSavedReservedGeometry;
570 }
571 private:
572 AutoGeometrySrcRestore fAgsr;
573 GrGpu* fGpu;
574 const GrVertexBuffer* fSavedPoolVertexBuffer;
575 int fSavedPoolStartVertex;
576 const GrIndexBuffer* fSavedPoolIndexBuffer;
577 int fSavedPoolStartIndex;
578 bool fVertexPoolWasInUse;
579 bool fIndexPoolWasInUse;
580 ReservedGeometry fSavedReservedGeometry;
581 };
582
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000583 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000584};
585
586#endif