blob: 5bbe85d877714cba999a2393def0e430e3a71a12 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#ifndef GrGpu_DEFINED
18#define GrGpu_DEFINED
19
bsalomon@google.com669fdc42011-04-05 17:08:27 +000020#include "GrDrawTarget.h"
21#include "GrPathRenderer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022#include "GrRect.h"
23#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com669fdc42011-04-05 17:08:27 +000026class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027class GrIndexBufferAllocPool;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000028class GrResource;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000029class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000030
31class GrGpu : public GrDrawTarget {
32
33public:
34 /**
35 * Possible 3D APIs that may be used by Ganesh.
36 */
37 enum Engine {
38 kOpenGL_Shaders_Engine,
39 kOpenGL_Fixed_Engine,
40 kDirect3D9_Engine
41 };
42
43 /**
44 * Platform specific 3D context.
45 * For
46 * kOpenGL_Shaders_Engine use NULL
47 * kOpenGL_Fixed_Engine use NULL
48 * kDirect3D9_Engine use an IDirect3DDevice9*
49 */
50 typedef void* Platform3DContext;
51
52 /**
53 * Create an instance of GrGpu that matches the specified Engine backend.
54 * If the requested engine is not supported (at compile-time or run-time)
55 * this returns NULL.
56 */
57 static GrGpu* Create(Engine, Platform3DContext context3D);
58
59 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000060 * Used to control the level of antialiasing available for a rendertarget.
61 * Anti-alias quality levels depend on the underlying API/GPU capabilities.
62 */
63 enum AALevels {
64 kNone_AALevel, //<! No antialiasing available.
65 kLow_AALevel, //<! Low quality antialiased rendering. Actual
66 // interpretation is platform-dependent.
67 kMed_AALevel, //<! Medium quality antialiased rendering. Actual
68 // interpretation is platform-dependent.
69 kHigh_AALevel, //<! High quality antialiased rendering. Actual
70 // interpretation is platform-dependent.
71 };
72
73
74 /**
75 * Optional bitfield flags that can be passed to createTexture.
76 */
77 enum TextureFlags {
78 kRenderTarget_TextureFlag = 0x1, //<! Creates a texture that can be
79 // rendered to by calling
80 // GrGpu::setRenderTarget() with
81 // GrTexture::asRenderTarget().
bsalomon@google.comf6a7c112011-03-24 16:14:10 +000082 kNoStencil_TextureFlag = 0x2, //<! If the texture is used as a
83 // rendertarget but a stencil
84 // buffer is not required. Stencil
85 // may be required for clipping and
86 // path rendering.
reed@google.comac10a2d2010-12-22 21:39:39 +000087 kDynamicUpdate_TextureFlag = 0x4 //!< Hint that the CPU may modify
88 // this texture after creation
89 };
90
91 enum {
92 /**
93 * For Index8 pixel config, the colortable must be 256 entries
94 */
95 kColorTableSize = 256 * sizeof(GrColor)
96 };
97 /**
98 * Describes a texture to be created.
99 */
100 struct TextureDesc {
101 uint32_t fFlags; //!< bitfield of TextureFlags
102 GrGpu::AALevels fAALevel;//!< The level of antialiasing available
103 // for a rendertarget texture. Only
104 // flags contains
105 // kRenderTarget_TextureFlag.
106 uint32_t fWidth; //!< Width of the texture
107 uint32_t fHeight; //!< Height of the texture
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000108 GrPixelConfig fFormat; //!< Format of source data of the
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 // texture. Not guaraunteed to be the
110 // same as internal format used by
111 // 3D API.
112 };
113
114 /**
115 * Gpu usage statistics.
116 */
117 struct Stats {
118 uint32_t fVertexCnt; //<! Number of vertices drawn
119 uint32_t fIndexCnt; //<! Number of indices drawn
120 uint32_t fDrawCnt; //<! Number of draws
121
122 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
123
124 /*
125 * Number of times the texture is set in 3D API
126 */
127 uint32_t fTextureChngCnt;
128 /*
129 * Number of times the render target is set in 3D API
130 */
131 uint32_t fRenderTargetChngCnt;
132 /*
133 * Number of textures created (includes textures that are rendertargets).
134 */
135 uint32_t fTextureCreateCnt;
136 /*
137 * Number of rendertargets created.
138 */
139 uint32_t fRenderTargetCreateCnt;
140 };
141
142 ////////////////////////////////////////////////////////////////////////////
143
144 GrGpu();
145 virtual ~GrGpu();
146
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000147 // The GrContext sets itself as the owner of this Gpu object
148 void setContext(GrContext* context) {
149 GrAssert(NULL == fContext);
150 fContext = context;
151 }
152 GrContext* getContext() { return fContext; }
153 const GrContext* getContext() const { return fContext; }
154
reed@google.comac10a2d2010-12-22 21:39:39 +0000155 /**
156 * The GrGpu object normally assumes that no outsider is setting state
157 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158 * the GrGpu that the state was modified and it shouldn't make assumptions
159 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
163 void unimpl(const char[]);
164
165 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000166 * Creates a texture object. If desc width or height is not a power of
167 * two but underlying API requires a power of two texture then srcData
168 * will be embedded in a power of two texture. The extra width and height
169 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000171 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000172 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
173 * on the render target until its releaseRenderTarget() is called or it is
174 * destroyed.
175 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 * @param desc describes the texture to be created.
177 * @param srcData texel data to load texture. Begins with full-size
178 * palette data for paletted textures. Contains width*
179 * height texels. If NULL texture data is uninitialized.
180 *
181 * @return The texture object if successful, otherwise NULL.
182 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000183 GrTexture* createTexture(const TextureDesc& desc,
184 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185 /**
186 * Wraps an externally-created rendertarget in a GrRenderTarget.
187 * @param platformRenderTarget handle to the the render target in the
188 * underlying 3D API. Interpretation depends on
189 * GrGpu subclass in use.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000190 * @param stencilBits number of stencil bits the target has
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000191 * @param isMultisampled specify whether the RT is multisampled
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 * @param width width of the render target
193 * @param height height of the render target
194 */
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000195 GrRenderTarget* createPlatformRenderTarget(intptr_t platformRenderTarget,
bsalomon@google.com8895a7a2011-02-18 16:09:55 +0000196 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000197 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000198 int width, int height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000199
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000200 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
201
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000203 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
204 * viewport state from the underlying 3D API and wraps it in a
205 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
206 * underlying object in its destructor and it is up to caller to guarantee
207 * that it remains valid while the GrRenderTarget is used.
208 *
209 * @return the newly created GrRenderTarget
210 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000212
213 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 * Creates a vertex buffer.
215 *
216 * @param size size in bytes of the vertex buffer
217 * @param dynamic hints whether the data will be frequently changed
218 * by either GrVertexBuffer::lock or
219 * GrVertexBuffer::updateData.
220 *
221 * @return The vertex buffer if successful, otherwise NULL.
222 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
225 /**
226 * Creates an index buffer.
227 *
228 * @param size size in bytes of the index buffer
229 * @param dynamic hints whether the data will be frequently changed
230 * by either GrIndexBuffer::lock or
231 * GrIndexBuffer::updateData.
232 *
233 * @return The index buffer if successful, otherwise NULL.
234 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
237 /**
bsalomon@google.com398109c2011-04-14 18:40:27 +0000238 * Clear the entire render target, ignoring any clips/scissors.
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 *
240 * This is issued to the GPU driver immediately.
241 */
bsalomon@google.com398109c2011-04-14 18:40:27 +0000242 void clear(GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
244 /**
245 * Are 8 bit paletted textures supported.
246 *
247 * @return true if 8bit palette textures are supported, false otherwise
248 */
249 bool supports8BitPalette() const { return f8bitPaletteSupport; }
250
251 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000252 * returns true if two sided stenciling is supported. If false then only
253 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 * @return true if only a single stencil pass is needed.
255 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000256 bool supportsTwoSidedStencil() const
257 { return fTwoSidedStencilSupport; }
258
259 /**
260 * returns true if stencil wrap is supported. If false then
261 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
262 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
263 * @return true if stencil wrap ops are supported.
264 */
265 bool supportsStencilWrapOps() const
266 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
268 /**
269 * Checks whether locking vertex and index buffers is supported.
270 *
271 * @return true if locking is supported.
272 */
273 bool supportsBufferLocking() const { return fBufferLockSupport; }
274
275 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000276 * Does the 3D API support anti-aliased lines. If so then line primitive
277 * types will use this functionality when the AA state flag is set.
278 */
279 bool supportsAALines() const { return fAALineSupport; }
280
281 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 * Gets the minimum width of a render target. If a texture/rt is created
283 * with a width less than this size the GrGpu object will clamp it to this
284 * value.
285 */
286 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
287
288 /**
289 * Gets the minimum width of a render target. If a texture/rt is created
290 * with a height less than this size the GrGpu object will clamp it to this
291 * value.
292 */
293 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
294
295 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000296 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000298 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000300 bool npotTextureSupport() const { return fNPOTTextureSupport; }
301
302 /**
303 * Returns true if NPOT textures can be repeat/mirror tiled.
304 *
305 * @return true if NPOT textures can be tiled
306 */
307 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
308
309 /**
310 * Returns true if a NPOT texture can be a rendertarget
311 *
312 * @return the true if NPOT texture/rendertarget can be created.
313 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000314 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
reed@google.com02a7e6c2011-01-28 21:21:49 +0000316 int maxTextureDimension() const { return fMaxTextureDimension; }
317
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000319 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000320 int startVertex,
321 int startIndex,
322 int vertexCount,
323 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000324
bsalomon@google.comffca4002011-02-22 20:34:01 +0000325 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000326 int startVertex,
327 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
329 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000330 * Installs a path renderer that will be used to draw paths that are
331 * part of the clip.
332 */
333 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
334 GrSafeAssign(fClientPathRenderer, pathRenderer);
335 }
336
337 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000339 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
340 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000342 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000344 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
346 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000348 * (1,1), (0,1)].
349 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000350 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000351 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000352
353 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 * Ensures that the current render target is actually set in the
355 * underlying 3D API. Used when client wants to use 3D API to directly
356 * render to the RT.
357 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000358 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000359
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000360 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000361 * Reads a rectangle of pixels from a render target.
362 * @param renderTarget the render target to read from. NULL means the
363 * current render target.
364 * @param left left edge of the rectangle to read (inclusive)
365 * @param top top edge of the rectangle to read (inclusive)
366 * @param width width of rectangle to read in pixels.
367 * @param height height of rectangle to read in pixels.
368 * @param config the pixel config of the destination buffer
369 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000370 *
371 * @return true if the read succeeded, false if not. The read can fail
372 * because of a unsupported pixel config or because no render
373 * target is currently set.
374 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000375 bool readPixels(GrRenderTarget* renderTarget,
376 int left, int top, int width, int height,
377 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 const Stats& getStats() const;
380 void resetStats();
381 void printStats() const;
382
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000383 /**
384 * Called to tell Gpu object that all GrResources have been lost and should
385 * be abandoned.
386 */
387 void abandonResources();
388
389 /**
390 * Called to tell Gpu object to release all GrResources.
391 */
392 void releaseResources();
393
394 /**
395 * Add resource to list of resources. Should only be called by GrResource.
396 * @param resource the resource to add.
397 */
398 void insertResource(GrResource* resource);
399
400 /**
401 * Remove resource from list of resources. Should only be called by
402 * GrResource.
403 * @param resource the resource to remove.
404 */
405 void removeResource(GrResource* resource);
406
reed@google.comac10a2d2010-12-22 21:39:39 +0000407protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000408 enum PrivateStateBits {
409 kFirstBit = (kLastPublicStateBit << 1),
410
411 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
412 // stencil bits used for
413 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 };
415
416 /**
417 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
418 */
419 struct ClipState {
420 bool fClipInStencil;
421 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 } fClipState;
423
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000424 // GrDrawTarget override
425 virtual void clipWillBeSet(const GrClip& newClip);
426
427 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000428 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429
bsalomon@google.comd302f142011-03-03 13:54:13 +0000430 // Functions used to map clip-respecting stencil tests into normal
431 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000432 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000433 GrStencilFunc func);
434 static void ConvertStencilFuncAndMask(GrStencilFunc func,
435 bool clipInStencil,
436 unsigned int clipBit,
437 unsigned int userBits,
438 unsigned int* ref,
439 unsigned int* mask);
440
441 // stencil settings to clip drawing when stencil clipping is in effect
442 // and the client isn't using the stencil test.
443 static const GrStencilSettings gClipStencilSettings;
444
reed@google.comac10a2d2010-12-22 21:39:39 +0000445 // defaults to false, subclass can set true to support palleted textures
446 bool f8bitPaletteSupport;
447
bsalomon@google.com0748f212011-02-01 22:56:16 +0000448 // set by subclass
449 bool fNPOTTextureSupport;
450 bool fNPOTTextureTileSupport;
451 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000452 bool fTwoSidedStencilSupport;
453 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000454 bool fAALineSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
456 // set by subclass to true if index and vertex buffers can be locked, false
457 // otherwise.
458 bool fBufferLockSupport;
459
460 // set by subclass
461 int fMinRenderTargetWidth;
462 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000463 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000465 Stats fStats;
466
467 const GrVertexBuffer* fCurrPoolVertexBuffer;
468 int fCurrPoolStartVertex;
469
470 const GrIndexBuffer* fCurrPoolIndexBuffer;
471 int fCurrPoolStartIndex;
472
473 // GrDrawTarget overrides
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000474 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
475 void** vertices,
476 void** indices);
477 virtual void onReleaseGeometry();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000478
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000479 virtual void onSetVertexSourceToArray(const void* vertexArray,
480 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000481
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000482 virtual void onSetIndexSourceToArray(const void* indexArray,
483 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000484 // Helpers for setting up geometry state
485 void finalizeReservedVertices();
486 void finalizeReservedIndices();
487
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000488 // overridden by API-specific derived class to handle re-emitting 3D API
489 // preample and dirtying state cache.
490 virtual void resetContext() = 0;
491
492 // overridden by API-specific derived class to create objects.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000493 virtual GrTexture* onCreateTexture(const TextureDesc& desc,
494 const void* srcData,
495 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000496 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000497 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000498 intptr_t platformRenderTarget,
499 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000500 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000501 int width, int height) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000502 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
503 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
504 bool dynamic) = 0;
505 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
506 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000507
bsalomon@google.com398109c2011-04-14 18:40:27 +0000508 // overridden by API-specific derivated class to perform the clear.
509 virtual void onClear(GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000510
511 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000512 virtual void onDrawIndexed(GrPrimitiveType type,
513 uint32_t startVertex,
514 uint32_t startIndex,
515 uint32_t vertexCount,
516 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000518 virtual void onDrawNonIndexed(GrPrimitiveType type,
519 uint32_t vertexCount,
520 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000522 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000523 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000524
525 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000526 virtual bool onReadPixels(GrRenderTarget* target,
527 int left, int top, int width, int height,
528 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000529
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000531 // indexed geometry. The subclass may adjust the startVertex and/or
532 // startIndex since it may have already accounted for these in the setup.
533 virtual void setupGeometry(int* startVertex,
534 int* startIndex,
535 int vertexCount,
536 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
538
539 // The GrGpu typically records the clients requested state and then flushes
540 // deltas from previous state at draw time. This function does the
541 // API-specific flush of the state
542 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000543 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
545 // Sets the scissor rect, or disables if rect is NULL.
546 virtual void flushScissor(const GrIRect* rect) = 0;
547
548 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000549 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
reed@google.comac10a2d2010-12-22 21:39:39 +0000551private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000552 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000553
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000556 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000558 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
559 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000560
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000561 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
562 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000564 GrDefaultPathRenderer* fDefaultPathRenderer;
565 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000566
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000567 bool fContextIsDirty;
568
bsalomon@google.comd302f142011-03-03 13:54:13 +0000569 // when in an internal draw these indicate whether the pools are in use
570 // by one of the outer draws. If false then it is safe to reset the
571 // pool.
572 bool fVertexPoolInUse;
573 bool fIndexPoolInUse;
574
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000575 GrResource* fResourceHead;
576
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000577 // readies the pools to provide vertex/index data.
578 void prepareVertexPool();
579 void prepareIndexPool();
580
581 // determines the path renderer used to draw a clip path element.
582 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
583 GrPathFill fill);
584
585 void handleDirtyContext() {
586 if (fContextIsDirty) {
587 this->resetContext();
588 fContextIsDirty = false;
589 }
590 }
591
bsalomon@google.comd302f142011-03-03 13:54:13 +0000592 // used to save and restore state when the GrGpu needs
593 // to make its geometry pools available internally
594 class AutoInternalDrawGeomRestore {
595 public:
596 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
597 fGpu = gpu;
598
599 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
600 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
601
602 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
603 (kBuffer_GeometrySrcType !=
604 gpu->fGeometrySrc.fVertexSrc);
605 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
606 (kBuffer_GeometrySrcType !=
607 gpu->fGeometrySrc.fIndexSrc);;
608
609 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
610 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
611 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
612 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
613
614 fSavedReservedGeometry = gpu->fReservedGeometry;
615 gpu->fReservedGeometry.fLocked = false;
616 }
617 ~AutoInternalDrawGeomRestore() {
618 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
619 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
620 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
621 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
622 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
623 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
624 fGpu->fReservedGeometry = fSavedReservedGeometry;
625 }
626 private:
627 AutoGeometrySrcRestore fAgsr;
628 GrGpu* fGpu;
629 const GrVertexBuffer* fSavedPoolVertexBuffer;
630 int fSavedPoolStartVertex;
631 const GrIndexBuffer* fSavedPoolIndexBuffer;
632 int fSavedPoolStartIndex;
633 bool fVertexPoolWasInUse;
634 bool fIndexPoolWasInUse;
635 ReservedGeometry fSavedReservedGeometry;
636 };
637
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000638 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000639};
640
641#endif