blob: 590712bd48801bf7ce1985860e9d13a093d87de8 [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; }
232
233 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000234 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000236 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000238 bool npotTextureSupport() const { return fNPOTTextureSupport; }
239
240 /**
241 * Returns true if NPOT textures can be repeat/mirror tiled.
242 *
243 * @return true if NPOT textures can be tiled
244 */
245 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
246
247 /**
248 * Returns true if a NPOT texture can be a rendertarget
249 *
250 * @return the true if NPOT texture/rendertarget can be created.
251 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000252 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
reed@google.com02a7e6c2011-01-28 21:21:49 +0000254 int maxTextureDimension() const { return fMaxTextureDimension; }
255
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000257 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258 int startVertex,
259 int startIndex,
260 int vertexCount,
261 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
bsalomon@google.comffca4002011-02-22 20:34:01 +0000263 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264 int startVertex,
265 int vertexCount);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000266 virtual void clear(const GrIRect* rect, GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
268 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000269 * Installs a path renderer that will be used to draw paths that are
270 * part of the clip.
271 */
272 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
273 GrSafeAssign(fClientPathRenderer, pathRenderer);
274 }
275
276 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000277 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000278 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
279 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000281 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000283 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
285 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000286 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000287 * (1,1), (0,1)].
288 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000289 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000290 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000291
292 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 * Ensures that the current render target is actually set in the
294 * underlying 3D API. Used when client wants to use 3D API to directly
295 * render to the RT.
296 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000297 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000298
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000299 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000300 * Reads a rectangle of pixels from a render target.
301 * @param renderTarget the render target to read from. NULL means the
302 * current render target.
303 * @param left left edge of the rectangle to read (inclusive)
304 * @param top top edge of the rectangle to read (inclusive)
305 * @param width width of rectangle to read in pixels.
306 * @param height height of rectangle to read in pixels.
307 * @param config the pixel config of the destination buffer
308 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000309 *
310 * @return true if the read succeeded, false if not. The read can fail
311 * because of a unsupported pixel config or because no render
312 * target is currently set.
313 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000314 bool readPixels(GrRenderTarget* renderTarget,
315 int left, int top, int width, int height,
316 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 const Stats& getStats() const;
319 void resetStats();
320 void printStats() const;
321
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000322 /**
323 * Called to tell Gpu object that all GrResources have been lost and should
324 * be abandoned.
325 */
326 void abandonResources();
327
328 /**
329 * Called to tell Gpu object to release all GrResources.
330 */
331 void releaseResources();
332
333 /**
334 * Add resource to list of resources. Should only be called by GrResource.
335 * @param resource the resource to add.
336 */
337 void insertResource(GrResource* resource);
338
339 /**
340 * Remove resource from list of resources. Should only be called by
341 * GrResource.
342 * @param resource the resource to remove.
343 */
344 void removeResource(GrResource* resource);
345
reed@google.comac10a2d2010-12-22 21:39:39 +0000346protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347 enum PrivateStateBits {
348 kFirstBit = (kLastPublicStateBit << 1),
349
350 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
351 // stencil bits used for
352 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 };
354
355 /**
356 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
357 */
358 struct ClipState {
359 bool fClipInStencil;
360 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000361 } fClipState;
362
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000363 // GrDrawTarget override
364 virtual void clipWillBeSet(const GrClip& newClip);
365
366 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000367 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
bsalomon@google.comd302f142011-03-03 13:54:13 +0000369 // Functions used to map clip-respecting stencil tests into normal
370 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000371 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000372 GrStencilFunc func);
373 static void ConvertStencilFuncAndMask(GrStencilFunc func,
374 bool clipInStencil,
375 unsigned int clipBit,
376 unsigned int userBits,
377 unsigned int* ref,
378 unsigned int* mask);
379
380 // stencil settings to clip drawing when stencil clipping is in effect
381 // and the client isn't using the stencil test.
382 static const GrStencilSettings gClipStencilSettings;
383
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 // defaults to false, subclass can set true to support palleted textures
385 bool f8bitPaletteSupport;
386
bsalomon@google.com0748f212011-02-01 22:56:16 +0000387 // set by subclass
388 bool fNPOTTextureSupport;
389 bool fNPOTTextureTileSupport;
390 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 bool fTwoSidedStencilSupport;
392 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000393 bool fAALineSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
395 // set by subclass to true if index and vertex buffers can be locked, false
396 // otherwise.
397 bool fBufferLockSupport;
398
399 // set by subclass
400 int fMinRenderTargetWidth;
401 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000402 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000403
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000404 Stats fStats;
405
406 const GrVertexBuffer* fCurrPoolVertexBuffer;
407 int fCurrPoolStartVertex;
408
409 const GrIndexBuffer* fCurrPoolIndexBuffer;
410 int fCurrPoolStartIndex;
411
412 // GrDrawTarget overrides
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000413 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
414 void** vertices,
415 void** indices);
416 virtual void onReleaseGeometry();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000418 virtual void onSetVertexSourceToArray(const void* vertexArray,
419 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000420
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000421 virtual void onSetIndexSourceToArray(const void* indexArray,
422 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000423 // Helpers for setting up geometry state
424 void finalizeReservedVertices();
425 void finalizeReservedIndices();
426
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000427 // overridden by API-specific derived class to handle re-emitting 3D API
428 // preample and dirtying state cache.
429 virtual void resetContext() = 0;
430
431 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000432 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000433 const void* srcData,
434 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000435 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000436 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000437 intptr_t platformRenderTarget,
438 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000439 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000440 int width, int height) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000441 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
442 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
443 bool dynamic) = 0;
444 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
445 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000446
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000447 // overridden by API-specific derivated class to perform the clear and
448 // clearRect. NULL rect means clear whole target.
449 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450
451 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000452 virtual void onDrawIndexed(GrPrimitiveType type,
453 uint32_t startVertex,
454 uint32_t startIndex,
455 uint32_t vertexCount,
456 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000458 virtual void onDrawNonIndexed(GrPrimitiveType type,
459 uint32_t vertexCount,
460 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000462 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000463 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000464
465 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000466 virtual bool onReadPixels(GrRenderTarget* target,
467 int left, int top, int width, int height,
468 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000471 // indexed geometry. The subclass may adjust the startVertex and/or
472 // startIndex since it may have already accounted for these in the setup.
473 virtual void setupGeometry(int* startVertex,
474 int* startIndex,
475 int vertexCount,
476 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
478
479 // The GrGpu typically records the clients requested state and then flushes
480 // deltas from previous state at draw time. This function does the
481 // API-specific flush of the state
482 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000483 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
485 // Sets the scissor rect, or disables if rect is NULL.
486 virtual void flushScissor(const GrIRect* rect) = 0;
487
488 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000489 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
reed@google.comac10a2d2010-12-22 21:39:39 +0000491private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000492 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000493
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000494 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000496 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000498 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
499 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000501 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
502 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000503
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000504 GrDefaultPathRenderer* fDefaultPathRenderer;
505 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000506
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000507 bool fContextIsDirty;
508
bsalomon@google.comd302f142011-03-03 13:54:13 +0000509 // when in an internal draw these indicate whether the pools are in use
510 // by one of the outer draws. If false then it is safe to reset the
511 // pool.
512 bool fVertexPoolInUse;
513 bool fIndexPoolInUse;
514
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000515 GrResource* fResourceHead;
516
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000517 // readies the pools to provide vertex/index data.
518 void prepareVertexPool();
519 void prepareIndexPool();
520
521 // determines the path renderer used to draw a clip path element.
522 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
523 GrPathFill fill);
524
525 void handleDirtyContext() {
526 if (fContextIsDirty) {
527 this->resetContext();
528 fContextIsDirty = false;
529 }
530 }
531
bsalomon@google.comd302f142011-03-03 13:54:13 +0000532 // used to save and restore state when the GrGpu needs
533 // to make its geometry pools available internally
534 class AutoInternalDrawGeomRestore {
535 public:
536 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
537 fGpu = gpu;
538
539 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
540 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
541
542 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
543 (kBuffer_GeometrySrcType !=
544 gpu->fGeometrySrc.fVertexSrc);
545 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
546 (kBuffer_GeometrySrcType !=
547 gpu->fGeometrySrc.fIndexSrc);;
548
549 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
550 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
551 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
552 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
553
554 fSavedReservedGeometry = gpu->fReservedGeometry;
555 gpu->fReservedGeometry.fLocked = false;
556 }
557 ~AutoInternalDrawGeomRestore() {
558 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
559 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
560 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
561 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
562 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
563 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
564 fGpu->fReservedGeometry = fSavedReservedGeometry;
565 }
566 private:
567 AutoGeometrySrcRestore fAgsr;
568 GrGpu* fGpu;
569 const GrVertexBuffer* fSavedPoolVertexBuffer;
570 int fSavedPoolStartVertex;
571 const GrIndexBuffer* fSavedPoolIndexBuffer;
572 int fSavedPoolStartIndex;
573 bool fVertexPoolWasInUse;
574 bool fIndexPoolWasInUse;
575 ReservedGeometry fSavedReservedGeometry;
576 };
577
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000578 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000579};
580
581#endif