blob: 5602d1ac21a9e36ef73b5b368c9aa7129c26bc89 [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
bsalomon@google.com05ef5102011-05-02 21:14:59 +000031/**
32 * Gpu usage statistics.
33 */
34struct GrGpuStats {
35 uint32_t fVertexCnt; //<! Number of vertices drawn
36 uint32_t fIndexCnt; //<! Number of indices drawn
37 uint32_t fDrawCnt; //<! Number of draws
38
39 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
40
41 /*
42 * Number of times the texture is set in 3D API
43 */
44 uint32_t fTextureChngCnt;
45 /*
46 * Number of times the render target is set in 3D API
47 */
48 uint32_t fRenderTargetChngCnt;
49 /*
50 * Number of textures created (includes textures that are rendertargets).
51 */
52 uint32_t fTextureCreateCnt;
53 /*
54 * Number of rendertargets created.
55 */
56 uint32_t fRenderTargetCreateCnt;
57};
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059class GrGpu : public GrDrawTarget {
60
61public:
62 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000063 * Create an instance of GrGpu that matches the specified Engine backend.
64 * If the requested engine is not supported (at compile-time or run-time)
65 * this returns NULL.
66 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000067 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000068
69 ////////////////////////////////////////////////////////////////////////////
70
71 GrGpu();
72 virtual ~GrGpu();
73
bsalomon@google.com669fdc42011-04-05 17:08:27 +000074 // The GrContext sets itself as the owner of this Gpu object
75 void setContext(GrContext* context) {
76 GrAssert(NULL == fContext);
77 fContext = context;
78 }
79 GrContext* getContext() { return fContext; }
80 const GrContext* getContext() const { return fContext; }
81
reed@google.comac10a2d2010-12-22 21:39:39 +000082 /**
83 * The GrGpu object normally assumes that no outsider is setting state
84 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000085 * the GrGpu that the state was modified and it shouldn't make assumptions
86 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000087 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000088 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000089
90 void unimpl(const char[]);
91
92 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000093 * Creates a texture object. If desc width or height is not a power of
94 * two but underlying API requires a power of two texture then srcData
95 * will be embedded in a power of two texture. The extra width and height
96 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000097 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000099 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
100 * on the render target until its releaseRenderTarget() is called or it is
101 * destroyed.
102 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 * @param desc describes the texture to be created.
104 * @param srcData texel data to load texture. Begins with full-size
105 * palette data for paletted textures. Contains width*
106 * height texels. If NULL texture data is uninitialized.
107 *
108 * @return The texture object if successful, otherwise NULL.
109 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000110 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000111 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 /**
113 * Wraps an externally-created rendertarget in a GrRenderTarget.
114 * @param platformRenderTarget handle to the the render target in the
115 * underlying 3D API. Interpretation depends on
116 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000117 * @param stencilBits number of stencil bits the target has
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000118 * @param isMultisampled specify whether the RT is multisampled
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 * @param width width of the render target
120 * @param height height of the render target
121 */
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000122 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000123 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000124 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000125 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000127 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000130 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
131 * viewport state from the underlying 3D API and wraps it in a
132 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
133 * underlying object in its destructor and it is up to caller to guarantee
134 * that it remains valid while the GrRenderTarget is used.
135 *
136 * @return the newly created GrRenderTarget
137 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000138 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000139
140 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 * Creates a vertex buffer.
142 *
143 * @param size size in bytes of the vertex buffer
144 * @param dynamic hints whether the data will be frequently changed
145 * by either GrVertexBuffer::lock or
146 * GrVertexBuffer::updateData.
147 *
148 * @return The vertex buffer if successful, otherwise NULL.
149 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000150 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
152 /**
153 * Creates an index buffer.
154 *
155 * @param size size in bytes of the index buffer
156 * @param dynamic hints whether the data will be frequently changed
157 * by either GrIndexBuffer::lock or
158 * GrIndexBuffer::updateData.
159 *
160 * @return The index buffer if successful, otherwise NULL.
161 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000162 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000163
164 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 * Are 8 bit paletted textures supported.
166 *
167 * @return true if 8bit palette textures are supported, false otherwise
168 */
169 bool supports8BitPalette() const { return f8bitPaletteSupport; }
170
171 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000172 * returns true if two sided stenciling is supported. If false then only
173 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 * @return true if only a single stencil pass is needed.
175 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000176 bool supportsTwoSidedStencil() const
177 { return fTwoSidedStencilSupport; }
178
179 /**
180 * returns true if stencil wrap is supported. If false then
181 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
182 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
183 * @return true if stencil wrap ops are supported.
184 */
185 bool supportsStencilWrapOps() const
186 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000187
188 /**
189 * Checks whether locking vertex and index buffers is supported.
190 *
191 * @return true if locking is supported.
192 */
193 bool supportsBufferLocking() const { return fBufferLockSupport; }
194
195 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000196 * Does the 3D API support anti-aliased lines. If so then line primitive
197 * types will use this functionality when the AA state flag is set.
198 */
199 bool supportsAALines() const { return fAALineSupport; }
200
201 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 * Gets the minimum width of a render target. If a texture/rt is created
203 * with a width less than this size the GrGpu object will clamp it to this
204 * value.
205 */
206 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
207
208 /**
209 * Gets the minimum width of a render target. If a texture/rt is created
210 * with a height less than this size the GrGpu object will clamp it to this
211 * value.
212 */
213 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000214
215 /**
216 * Reports whether full scene anti-aliasing is supported.
217 */
218 bool supportsFullsceneAA() const { return fFSAASupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
220 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000221 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000222 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000223 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000225 bool npotTextureSupport() const { return fNPOTTextureSupport; }
226
227 /**
228 * Returns true if NPOT textures can be repeat/mirror tiled.
229 *
230 * @return true if NPOT textures can be tiled
231 */
232 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
233
234 /**
235 * Returns true if a NPOT texture can be a rendertarget
236 *
237 * @return the true if NPOT texture/rendertarget can be created.
238 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.com02a7e6c2011-01-28 21:21:49 +0000241 int maxTextureDimension() const { return fMaxTextureDimension; }
242
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000244 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000245 int startVertex,
246 int startIndex,
247 int vertexCount,
248 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
bsalomon@google.comffca4002011-02-22 20:34:01 +0000250 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000251 int startVertex,
252 int vertexCount);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000253 virtual void clear(const GrIRect* rect, GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
255 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000256 * Installs a path renderer that will be used to draw paths that are
257 * part of the clip.
258 */
259 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
260 GrSafeAssign(fClientPathRenderer, pathRenderer);
261 }
262
263 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000265 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
266 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000268 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000270 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
272 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000273 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000274 * (1,1), (0,1)].
275 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000276 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000277 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000278
279 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 * Ensures that the current render target is actually set in the
281 * underlying 3D API. Used when client wants to use 3D API to directly
282 * render to the RT.
283 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000284 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000285
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000286 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000287 * Reads a rectangle of pixels from a render target.
288 * @param renderTarget the render target to read from. NULL means the
289 * current render target.
290 * @param left left edge of the rectangle to read (inclusive)
291 * @param top top edge of the rectangle to read (inclusive)
292 * @param width width of rectangle to read in pixels.
293 * @param height height of rectangle to read in pixels.
294 * @param config the pixel config of the destination buffer
295 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000296 *
297 * @return true if the read succeeded, false if not. The read can fail
298 * because of a unsupported pixel config or because no render
299 * target is currently set.
300 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000301 bool readPixels(GrRenderTarget* renderTarget,
302 int left, int top, int width, int height,
303 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000304
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000305 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000306 void resetStats();
307 void printStats() const;
308
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000309 /**
310 * Called to tell Gpu object that all GrResources have been lost and should
311 * be abandoned.
312 */
313 void abandonResources();
314
315 /**
316 * Called to tell Gpu object to release all GrResources.
317 */
318 void releaseResources();
319
320 /**
321 * Add resource to list of resources. Should only be called by GrResource.
322 * @param resource the resource to add.
323 */
324 void insertResource(GrResource* resource);
325
326 /**
327 * Remove resource from list of resources. Should only be called by
328 * GrResource.
329 * @param resource the resource to remove.
330 */
331 void removeResource(GrResource* resource);
332
reed@google.comac10a2d2010-12-22 21:39:39 +0000333protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000334 enum PrivateStateBits {
335 kFirstBit = (kLastPublicStateBit << 1),
336
337 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
338 // stencil bits used for
339 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000340 };
341
342 /**
343 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
344 */
345 struct ClipState {
346 bool fClipInStencil;
347 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 } fClipState;
349
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000350 // GrDrawTarget override
351 virtual void clipWillBeSet(const GrClip& newClip);
352
353 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000354 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
bsalomon@google.comd302f142011-03-03 13:54:13 +0000356 // Functions used to map clip-respecting stencil tests into normal
357 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000358 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 GrStencilFunc func);
360 static void ConvertStencilFuncAndMask(GrStencilFunc func,
361 bool clipInStencil,
362 unsigned int clipBit,
363 unsigned int userBits,
364 unsigned int* ref,
365 unsigned int* mask);
366
367 // stencil settings to clip drawing when stencil clipping is in effect
368 // and the client isn't using the stencil test.
369 static const GrStencilSettings gClipStencilSettings;
370
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 // defaults to false, subclass can set true to support palleted textures
372 bool f8bitPaletteSupport;
373
bsalomon@google.com0748f212011-02-01 22:56:16 +0000374 // set by subclass
375 bool fNPOTTextureSupport;
376 bool fNPOTTextureTileSupport;
377 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 bool fTwoSidedStencilSupport;
379 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000380 bool fAALineSupport;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000381 bool fFSAASupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000382
383 // set by subclass to true if index and vertex buffers can be locked, false
384 // otherwise.
385 bool fBufferLockSupport;
386
387 // set by subclass
388 int fMinRenderTargetWidth;
389 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000390 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000392 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000393
394 const GrVertexBuffer* fCurrPoolVertexBuffer;
395 int fCurrPoolStartVertex;
396
397 const GrIndexBuffer* fCurrPoolIndexBuffer;
398 int fCurrPoolStartIndex;
399
400 // GrDrawTarget overrides
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000401 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
402 void** vertices,
403 void** indices);
404 virtual void onReleaseGeometry();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000405
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000406 virtual void onSetVertexSourceToArray(const void* vertexArray,
407 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000408
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000409 virtual void onSetIndexSourceToArray(const void* indexArray,
410 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000411 // Helpers for setting up geometry state
412 void finalizeReservedVertices();
413 void finalizeReservedIndices();
414
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000415 // overridden by API-specific derived class to handle re-emitting 3D API
416 // preample and dirtying state cache.
417 virtual void resetContext() = 0;
418
419 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000420 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000421 const void* srcData,
422 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000423 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000424 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000425 intptr_t platformRenderTarget,
426 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000427 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428 int width, int height) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000429 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
430 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
431 bool dynamic) = 0;
432 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
433 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000434
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000435 // overridden by API-specific derivated class to perform the clear and
436 // clearRect. NULL rect means clear whole target.
437 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438
439 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000440 virtual void onDrawIndexed(GrPrimitiveType type,
441 uint32_t startVertex,
442 uint32_t startIndex,
443 uint32_t vertexCount,
444 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000445
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000446 virtual void onDrawNonIndexed(GrPrimitiveType type,
447 uint32_t vertexCount,
448 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000451 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452
453 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000454 virtual bool onReadPixels(GrRenderTarget* target,
455 int left, int top, int width, int height,
456 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000457
reed@google.comac10a2d2010-12-22 21:39:39 +0000458 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000459 // indexed geometry. The subclass may adjust the startVertex and/or
460 // startIndex since it may have already accounted for these in the setup.
461 virtual void setupGeometry(int* startVertex,
462 int* startIndex,
463 int vertexCount,
464 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
466
467 // The GrGpu typically records the clients requested state and then flushes
468 // deltas from previous state at draw time. This function does the
469 // API-specific flush of the state
470 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000471 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000472
473 // Sets the scissor rect, or disables if rect is NULL.
474 virtual void flushScissor(const GrIRect* rect) = 0;
475
476 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000477 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000478
reed@google.comac10a2d2010-12-22 21:39:39 +0000479private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000480 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000481
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000482 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000484 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000486 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
487 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000488
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000489 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
490 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000491
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000492 GrDefaultPathRenderer* fDefaultPathRenderer;
493 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000494
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000495 bool fContextIsDirty;
496
bsalomon@google.comd302f142011-03-03 13:54:13 +0000497 // when in an internal draw these indicate whether the pools are in use
498 // by one of the outer draws. If false then it is safe to reset the
499 // pool.
500 bool fVertexPoolInUse;
501 bool fIndexPoolInUse;
502
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000503 GrResource* fResourceHead;
504
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000505 // readies the pools to provide vertex/index data.
506 void prepareVertexPool();
507 void prepareIndexPool();
508
509 // determines the path renderer used to draw a clip path element.
510 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
511 GrPathFill fill);
512
513 void handleDirtyContext() {
514 if (fContextIsDirty) {
515 this->resetContext();
516 fContextIsDirty = false;
517 }
518 }
519
bsalomon@google.comd302f142011-03-03 13:54:13 +0000520 // used to save and restore state when the GrGpu needs
521 // to make its geometry pools available internally
522 class AutoInternalDrawGeomRestore {
523 public:
524 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
525 fGpu = gpu;
526
527 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
528 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
529
530 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
531 (kBuffer_GeometrySrcType !=
532 gpu->fGeometrySrc.fVertexSrc);
533 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
534 (kBuffer_GeometrySrcType !=
535 gpu->fGeometrySrc.fIndexSrc);;
536
537 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
538 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
539 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
540 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
541
542 fSavedReservedGeometry = gpu->fReservedGeometry;
543 gpu->fReservedGeometry.fLocked = false;
544 }
545 ~AutoInternalDrawGeomRestore() {
546 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
547 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
548 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
549 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
550 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
551 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
552 fGpu->fReservedGeometry = fSavedReservedGeometry;
553 }
554 private:
555 AutoGeometrySrcRestore fAgsr;
556 GrGpu* fGpu;
557 const GrVertexBuffer* fSavedPoolVertexBuffer;
558 int fSavedPoolStartVertex;
559 const GrIndexBuffer* fSavedPoolIndexBuffer;
560 int fSavedPoolStartIndex;
561 bool fVertexPoolWasInUse;
562 bool fIndexPoolWasInUse;
563 ReservedGeometry fSavedReservedGeometry;
564 };
565
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000566 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000567};
568
569#endif