blob: 7dd9959af90708998b67e069582fa5209c253a3d [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 /**
276 * Gets the minimum width of a render target. If a texture/rt is created
277 * with a width less than this size the GrGpu object will clamp it to this
278 * value.
279 */
280 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
281
282 /**
283 * Gets the minimum width of a render target. If a texture/rt is created
284 * with a height less than this size the GrGpu object will clamp it to this
285 * value.
286 */
287 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
288
289 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000290 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000292 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000294 bool npotTextureSupport() const { return fNPOTTextureSupport; }
295
296 /**
297 * Returns true if NPOT textures can be repeat/mirror tiled.
298 *
299 * @return true if NPOT textures can be tiled
300 */
301 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
302
303 /**
304 * Returns true if a NPOT texture can be a rendertarget
305 *
306 * @return the true if NPOT texture/rendertarget can be created.
307 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000308 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
reed@google.com02a7e6c2011-01-28 21:21:49 +0000310 int maxTextureDimension() const { return fMaxTextureDimension; }
311
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 // GrDrawTarget overrides
bsalomon@google.comffca4002011-02-22 20:34:01 +0000313 virtual void drawIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000314 int startVertex,
315 int startIndex,
316 int vertexCount,
317 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000318
bsalomon@google.comffca4002011-02-22 20:34:01 +0000319 virtual void drawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000320 int startVertex,
321 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000322
323 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000324 * Installs a path renderer that will be used to draw paths that are
325 * part of the clip.
326 */
327 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
328 GrSafeAssign(fClientPathRenderer, pathRenderer);
329 }
330
331 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
334 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000336 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000338 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000339
340 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000342 * (1,1), (0,1)].
343 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000344 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000345 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000346
347 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 * Ensures that the current render target is actually set in the
349 * underlying 3D API. Used when client wants to use 3D API to directly
350 * render to the RT.
351 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000352 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000353
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000354 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000355 * Reads a rectangle of pixels from a render target.
356 * @param renderTarget the render target to read from. NULL means the
357 * current render target.
358 * @param left left edge of the rectangle to read (inclusive)
359 * @param top top edge of the rectangle to read (inclusive)
360 * @param width width of rectangle to read in pixels.
361 * @param height height of rectangle to read in pixels.
362 * @param config the pixel config of the destination buffer
363 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000364 *
365 * @return true if the read succeeded, false if not. The read can fail
366 * because of a unsupported pixel config or because no render
367 * target is currently set.
368 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000369 bool readPixels(GrRenderTarget* renderTarget,
370 int left, int top, int width, int height,
371 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000372
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 const Stats& getStats() const;
374 void resetStats();
375 void printStats() const;
376
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000377 /**
378 * Called to tell Gpu object that all GrResources have been lost and should
379 * be abandoned.
380 */
381 void abandonResources();
382
383 /**
384 * Called to tell Gpu object to release all GrResources.
385 */
386 void releaseResources();
387
388 /**
389 * Add resource to list of resources. Should only be called by GrResource.
390 * @param resource the resource to add.
391 */
392 void insertResource(GrResource* resource);
393
394 /**
395 * Remove resource from list of resources. Should only be called by
396 * GrResource.
397 * @param resource the resource to remove.
398 */
399 void removeResource(GrResource* resource);
400
reed@google.comac10a2d2010-12-22 21:39:39 +0000401protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000402 enum PrivateStateBits {
403 kFirstBit = (kLastPublicStateBit << 1),
404
405 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
406 // stencil bits used for
407 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 };
409
410 /**
411 * Extensions to GrDrawTarget::StateBits to implement stencil clipping
412 */
413 struct ClipState {
414 bool fClipInStencil;
415 bool fClipIsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 } fClipState;
417
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000418 // GrDrawTarget override
419 virtual void clipWillBeSet(const GrClip& newClip);
420
421 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000422 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.comd302f142011-03-03 13:54:13 +0000424 // Functions used to map clip-respecting stencil tests into normal
425 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000426 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000427 GrStencilFunc func);
428 static void ConvertStencilFuncAndMask(GrStencilFunc func,
429 bool clipInStencil,
430 unsigned int clipBit,
431 unsigned int userBits,
432 unsigned int* ref,
433 unsigned int* mask);
434
435 // stencil settings to clip drawing when stencil clipping is in effect
436 // and the client isn't using the stencil test.
437 static const GrStencilSettings gClipStencilSettings;
438
reed@google.comac10a2d2010-12-22 21:39:39 +0000439 // defaults to false, subclass can set true to support palleted textures
440 bool f8bitPaletteSupport;
441
bsalomon@google.com0748f212011-02-01 22:56:16 +0000442 // set by subclass
443 bool fNPOTTextureSupport;
444 bool fNPOTTextureTileSupport;
445 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000446 bool fTwoSidedStencilSupport;
447 bool fStencilWrapOpsSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
449 // set by subclass to true if index and vertex buffers can be locked, false
450 // otherwise.
451 bool fBufferLockSupport;
452
453 // set by subclass
454 int fMinRenderTargetWidth;
455 int fMinRenderTargetHeight;
reed@google.com02a7e6c2011-01-28 21:21:49 +0000456 int fMaxTextureDimension;
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000458 Stats fStats;
459
460 const GrVertexBuffer* fCurrPoolVertexBuffer;
461 int fCurrPoolStartVertex;
462
463 const GrIndexBuffer* fCurrPoolIndexBuffer;
464 int fCurrPoolStartIndex;
465
466 // GrDrawTarget overrides
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000467 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
468 void** vertices,
469 void** indices);
470 virtual void onReleaseGeometry();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000471
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000472 virtual void onSetVertexSourceToArray(const void* vertexArray,
473 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000474
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000475 virtual void onSetIndexSourceToArray(const void* indexArray,
476 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000477 // Helpers for setting up geometry state
478 void finalizeReservedVertices();
479 void finalizeReservedIndices();
480
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000481 // overridden by API-specific derived class to handle re-emitting 3D API
482 // preample and dirtying state cache.
483 virtual void resetContext() = 0;
484
485 // overridden by API-specific derived class to create objects.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000486 virtual GrTexture* onCreateTexture(const TextureDesc& desc,
487 const void* srcData,
488 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000489 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000490 virtual GrRenderTarget* onCreatePlatformRenderTarget(
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000491 intptr_t platformRenderTarget,
492 int stencilBits,
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000493 bool isMultisampled,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000494 int width, int height) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000495 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
496 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
497 bool dynamic) = 0;
498 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
499 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000500
bsalomon@google.com398109c2011-04-14 18:40:27 +0000501 // overridden by API-specific derivated class to perform the clear.
502 virtual void onClear(GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000503
504 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000505 virtual void onDrawIndexed(GrPrimitiveType type,
506 uint32_t startVertex,
507 uint32_t startIndex,
508 uint32_t vertexCount,
509 uint32_t indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000511 virtual void onDrawNonIndexed(GrPrimitiveType type,
512 uint32_t vertexCount,
513 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000514
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000515 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000516 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000517
518 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000519 virtual bool onReadPixels(GrRenderTarget* target,
520 int left, int top, int width, int height,
521 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000522
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000524 // indexed geometry. The subclass may adjust the startVertex and/or
525 // startIndex since it may have already accounted for these in the setup.
526 virtual void setupGeometry(int* startVertex,
527 int* startIndex,
528 int vertexCount,
529 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530
531
532 // The GrGpu typically records the clients requested state and then flushes
533 // deltas from previous state at draw time. This function does the
534 // API-specific flush of the state
535 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000536 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
538 // Sets the scissor rect, or disables if rect is NULL.
539 virtual void flushScissor(const GrIRect* rect) = 0;
540
541 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000542 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000543
reed@google.comac10a2d2010-12-22 21:39:39 +0000544private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000545 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000546
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000547 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000548
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000549 GrIndexBufferAllocPool* fIndexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000551 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
552 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
555 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000556
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000557 GrDefaultPathRenderer* fDefaultPathRenderer;
558 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000559
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000560 bool fContextIsDirty;
561
bsalomon@google.comd302f142011-03-03 13:54:13 +0000562 // when in an internal draw these indicate whether the pools are in use
563 // by one of the outer draws. If false then it is safe to reset the
564 // pool.
565 bool fVertexPoolInUse;
566 bool fIndexPoolInUse;
567
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000568 GrResource* fResourceHead;
569
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000570 // readies the pools to provide vertex/index data.
571 void prepareVertexPool();
572 void prepareIndexPool();
573
574 // determines the path renderer used to draw a clip path element.
575 GrPathRenderer* getClipPathRenderer(GrPathIter* path,
576 GrPathFill fill);
577
578 void handleDirtyContext() {
579 if (fContextIsDirty) {
580 this->resetContext();
581 fContextIsDirty = false;
582 }
583 }
584
bsalomon@google.comd302f142011-03-03 13:54:13 +0000585 // used to save and restore state when the GrGpu needs
586 // to make its geometry pools available internally
587 class AutoInternalDrawGeomRestore {
588 public:
589 AutoInternalDrawGeomRestore(GrGpu* gpu) : fAgsr(gpu) {
590 fGpu = gpu;
591
592 fVertexPoolWasInUse = gpu->fVertexPoolInUse;
593 fIndexPoolWasInUse = gpu->fIndexPoolInUse;
594
595 gpu->fVertexPoolInUse = fVertexPoolWasInUse ||
596 (kBuffer_GeometrySrcType !=
597 gpu->fGeometrySrc.fVertexSrc);
598 gpu->fIndexPoolInUse = fIndexPoolWasInUse ||
599 (kBuffer_GeometrySrcType !=
600 gpu->fGeometrySrc.fIndexSrc);;
601
602 fSavedPoolVertexBuffer = gpu->fCurrPoolVertexBuffer;
603 fSavedPoolStartVertex = gpu->fCurrPoolStartVertex;
604 fSavedPoolIndexBuffer = gpu->fCurrPoolIndexBuffer;
605 fSavedPoolStartIndex = gpu->fCurrPoolStartIndex;
606
607 fSavedReservedGeometry = gpu->fReservedGeometry;
608 gpu->fReservedGeometry.fLocked = false;
609 }
610 ~AutoInternalDrawGeomRestore() {
611 fGpu->fCurrPoolVertexBuffer = fSavedPoolVertexBuffer;
612 fGpu->fCurrPoolStartVertex = fSavedPoolStartVertex;
613 fGpu->fCurrPoolIndexBuffer = fSavedPoolIndexBuffer;
614 fGpu->fCurrPoolStartIndex = fSavedPoolStartIndex;
615 fGpu->fVertexPoolInUse = fVertexPoolWasInUse;
616 fGpu->fIndexPoolInUse = fIndexPoolWasInUse;
617 fGpu->fReservedGeometry = fSavedReservedGeometry;
618 }
619 private:
620 AutoGeometrySrcRestore fAgsr;
621 GrGpu* fGpu;
622 const GrVertexBuffer* fSavedPoolVertexBuffer;
623 int fSavedPoolStartVertex;
624 const GrIndexBuffer* fSavedPoolIndexBuffer;
625 int fSavedPoolStartIndex;
626 bool fVertexPoolWasInUse;
627 bool fIndexPoolWasInUse;
628 ReservedGeometry fSavedReservedGeometry;
629 };
630
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000631 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000632};
633
634#endif