blob: fff40fb1474ce61360ff7299cb831771d1336ad9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGpu_DEFINED
11#define GrGpu_DEFINED
12
bsalomon@google.com669fdc42011-04-05 17:08:27 +000013#include "GrDrawTarget.h"
14#include "GrPathRenderer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrRect.h"
16#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com669fdc42011-04-05 17:08:27 +000019class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020class GrIndexBufferAllocPool;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000021class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000022class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000023class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com05ef5102011-05-02 21:14:59 +000025/**
26 * Gpu usage statistics.
27 */
28struct GrGpuStats {
29 uint32_t fVertexCnt; //<! Number of vertices drawn
30 uint32_t fIndexCnt; //<! Number of indices drawn
31 uint32_t fDrawCnt; //<! Number of draws
32
33 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
34
bsalomon@google.com030302c2011-08-01 17:35:01 +000035 /**
36 * Number of times the texture is set in 3D API
37 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000038 uint32_t fTextureChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000039 /**
40 * Number of times the render target is set in 3D API
41 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000042 uint32_t fRenderTargetChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000043 /**
44 * Number of textures created (includes textures that are rendertargets).
45 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000046 uint32_t fTextureCreateCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000047 /**
48 * Number of rendertargets created.
49 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000050 uint32_t fRenderTargetCreateCnt;
51};
52
reed@google.comac10a2d2010-12-22 21:39:39 +000053class GrGpu : public GrDrawTarget {
54
55public:
56 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057 * Additional blend coeffecients for dual source blending, not exposed
58 * through GrPaint/GrContext.
59 */
60 enum ExtendedBlendCoeffs {
61 // source 2 refers to second output color when
62 // using dual source blending.
63 kS2C_BlendCoeff = kPublicBlendCoeffCount,
64 kIS2C_BlendCoeff,
65 kS2A_BlendCoeff,
66 kIS2A_BlendCoeff,
67
68 kTotalBlendCoeffCount
69 };
70
71 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000072 * Create an instance of GrGpu that matches the specified Engine backend.
73 * If the requested engine is not supported (at compile-time or run-time)
74 * this returns NULL.
75 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000076 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000077
78 ////////////////////////////////////////////////////////////////////////////
79
80 GrGpu();
81 virtual ~GrGpu();
82
bsalomon@google.com669fdc42011-04-05 17:08:27 +000083 // The GrContext sets itself as the owner of this Gpu object
84 void setContext(GrContext* context) {
85 GrAssert(NULL == fContext);
86 fContext = context;
87 }
88 GrContext* getContext() { return fContext; }
89 const GrContext* getContext() const { return fContext; }
90
reed@google.comac10a2d2010-12-22 21:39:39 +000091 /**
92 * The GrGpu object normally assumes that no outsider is setting state
93 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000094 * the GrGpu that the state was modified and it shouldn't make assumptions
95 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000096 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000097 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000098
99 void unimpl(const char[]);
100
101 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000102 * Creates a texture object. If desc width or height is not a power of
103 * two but underlying API requires a power of two texture then srcData
104 * will be embedded in a power of two texture. The extra width and height
105 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000108 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
109 * on the render target until its releaseRenderTarget() is called or it is
110 * destroyed.
111 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000112 * @param desc describes the texture to be created.
113 * @param srcData texel data to load texture. Begins with full-size
114 * palette data for paletted textures. Contains width*
115 * height texels. If NULL texture data is uninitialized.
116 *
117 * @return The texture object if successful, otherwise NULL.
118 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000119 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000120 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000122 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000125 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
126 * viewport state from the underlying 3D API and wraps it in a
127 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
128 * underlying object in its destructor and it is up to caller to guarantee
129 * that it remains valid while the GrRenderTarget is used.
130 *
131 * @return the newly created GrRenderTarget
132 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000133 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000134
135 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 * Creates a vertex buffer.
137 *
138 * @param size size in bytes of the vertex buffer
139 * @param dynamic hints whether the data will be frequently changed
140 * by either GrVertexBuffer::lock or
141 * GrVertexBuffer::updateData.
142 *
143 * @return The vertex buffer if successful, otherwise NULL.
144 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000145 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000146
147 /**
148 * Creates an index buffer.
149 *
150 * @param size size in bytes of the index buffer
151 * @param dynamic hints whether the data will be frequently changed
152 * by either GrIndexBuffer::lock or
153 * GrIndexBuffer::updateData.
154 *
155 * @return The index buffer if successful, otherwise NULL.
156 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000157 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
159 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 * Are 8 bit paletted textures supported.
161 *
162 * @return true if 8bit palette textures are supported, false otherwise
163 */
164 bool supports8BitPalette() const { return f8bitPaletteSupport; }
165
166 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000167 * returns true if two sided stenciling is supported. If false then only
168 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 * @return true if only a single stencil pass is needed.
170 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000171 bool supportsTwoSidedStencil() const
172 { return fTwoSidedStencilSupport; }
173
174 /**
175 * returns true if stencil wrap is supported. If false then
176 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
177 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
178 * @return true if stencil wrap ops are supported.
179 */
180 bool supportsStencilWrapOps() const
181 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000182
183 /**
184 * Checks whether locking vertex and index buffers is supported.
185 *
186 * @return true if locking is supported.
187 */
188 bool supportsBufferLocking() const { return fBufferLockSupport; }
189
190 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000191 * Does the 3D API support anti-aliased lines. If so then line primitive
192 * types will use this functionality when the AA state flag is set.
193 */
194 bool supportsAALines() const { return fAALineSupport; }
195
196 /**
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000197 * Does the subclass support GrSamplerState::k4x4Downsample_Filter
198 */
199 bool supports4x4DownsampleFilter() const { return f4X4DownsampleFilterSupport; }
200
201 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000202 * Does this instance support dual-source blending? Required for proper
203 * blending with partial coverage with certain blend modes (dst coeff is
204 * not 1, ISA, or ISC)
205 */
206 bool supportsDualSourceBlending() const {
207 return fDualSourceBlendingSupport;
208 }
209
210 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 * Gets the minimum width of a render target. If a texture/rt is created
212 * with a width less than this size the GrGpu object will clamp it to this
213 * value.
214 */
215 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
216
217 /**
218 * Gets the minimum width of a render target. If a texture/rt is created
219 * with a height less than this size the GrGpu object will clamp it to this
220 * value.
221 */
222 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000223
224 /**
225 * Reports whether full scene anti-aliasing is supported.
226 */
227 bool supportsFullsceneAA() const { return fFSAASupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
229 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000230 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000232 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000234 bool npotTextureSupport() const { return fNPOTTextureSupport; }
235
236 /**
237 * Returns true if NPOT textures can be repeat/mirror tiled.
238 *
239 * @return true if NPOT textures can be tiled
240 */
241 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
242
243 /**
244 * Returns true if a NPOT texture can be a rendertarget
245 *
246 * @return the true if NPOT texture/rendertarget can be created.
247 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000248 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
bsalomon@google.com91958362011-06-13 17:58:13 +0000250 /**
251 * Gets the largest allowed width and height of a texture.
252 */
253 int maxTextureSize() const { return fMaxTextureSize; }
254 /**
255 * Gets the largest allowed width and height of a render target.
256 */
257 int maxRenderTargetSize() const { return fMaxRenderTargetSize; }
reed@google.com02a7e6c2011-01-28 21:21:49 +0000258
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000259 virtual void clear(const GrIRect* rect, GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
261 /**
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000262 * Installs a path renderer that will be used to draw paths that are
263 * part of the clip.
264 */
265 void setClipPathRenderer(GrPathRenderer* pathRenderer) {
266 GrSafeAssign(fClientPathRenderer, pathRenderer);
267 }
268
269 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000270 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000271 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
272 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000274 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000276 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
278 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000279 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000280 * (1,1), (0,1)].
281 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000282 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000283 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000284
285 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 * Ensures that the current render target is actually set in the
287 * underlying 3D API. Used when client wants to use 3D API to directly
288 * render to the RT.
289 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000290 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000291
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000292 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000293 * Reads a rectangle of pixels from a render target.
294 * @param renderTarget the render target to read from. NULL means the
295 * current render target.
296 * @param left left edge of the rectangle to read (inclusive)
297 * @param top top edge of the rectangle to read (inclusive)
298 * @param width width of rectangle to read in pixels.
299 * @param height height of rectangle to read in pixels.
300 * @param config the pixel config of the destination buffer
301 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000302 *
303 * @return true if the read succeeded, false if not. The read can fail
304 * because of a unsupported pixel config or because no render
305 * target is currently set.
306 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000307 bool readPixels(GrRenderTarget* renderTarget,
308 int left, int top, int width, int height,
309 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000310
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000311 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000312 void resetStats();
313 void printStats() const;
314
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000315 /**
316 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000317 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000318 */
junov@google.com53a55842011-06-08 22:55:10 +0000319 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000320
321 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000322 * Called to tell Gpu object to release all GrResources. Overrides must call
323 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000324 */
325 void releaseResources();
326
327 /**
328 * Add resource to list of resources. Should only be called by GrResource.
329 * @param resource the resource to add.
330 */
331 void insertResource(GrResource* resource);
332
333 /**
334 * Remove resource from list of resources. Should only be called by
335 * GrResource.
336 * @param resource the resource to remove.
337 */
338 void removeResource(GrResource* resource);
339
reed@google.comac10a2d2010-12-22 21:39:39 +0000340protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341 enum PrivateStateBits {
342 kFirstBit = (kLastPublicStateBit << 1),
343
344 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
345 // stencil bits used for
346 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000347 };
348
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000349 // keep track of whether we are using stencil clipping (as opposed to
350 // scissor).
351 bool fClipInStencil;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000352
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;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000382 bool f4X4DownsampleFilterSupport; // supports GrSamplerState::k4x4Downsample_Filter
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000383 bool fDualSourceBlendingSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000384
385 // set by subclass to true if index and vertex buffers can be locked, false
386 // otherwise.
387 bool fBufferLockSupport;
388
389 // set by subclass
390 int fMinRenderTargetWidth;
391 int fMinRenderTargetHeight;
bsalomon@google.com91958362011-06-13 17:58:13 +0000392 int fMaxRenderTargetSize;
393 int fMaxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000395 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000397 struct GeometryPoolState {
398 const GrVertexBuffer* fPoolVertexBuffer;
399 int fPoolStartVertex;
400
401 const GrIndexBuffer* fPoolIndexBuffer;
402 int fPoolStartIndex;
403 };
404 const GeometryPoolState& getGeomPoolState() {
405 return fGeomPoolStateStack.back();
406 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000407
408 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000409 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
410 int vertexCount,
411 void** vertices);
412 virtual bool onReserveIndexSpace(int indexCount, void** indices);
413 virtual void releaseReservedVertexSpace();
414 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000415 virtual void onSetVertexSourceToArray(const void* vertexArray,
416 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000417 virtual void onSetIndexSourceToArray(const void* indexArray,
418 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000419 virtual void releaseVertexArray();
420 virtual void releaseIndexArray();
421 virtual void geometrySourceWillPush();
422 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
423
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000424 // Helpers for setting up geometry state
425 void finalizeReservedVertices();
426 void finalizeReservedIndices();
427
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428 // overridden by API-specific derived class to handle re-emitting 3D API
429 // preample and dirtying state cache.
430 virtual void resetContext() = 0;
431
432 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000433 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000434 const void* srcData,
435 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000436 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000437 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
438 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
439 bool dynamic) = 0;
440 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
441 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000443 // overridden by API-specific derivated class to perform the clear and
444 // clearRect. NULL rect means clear whole target.
445 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000446
447 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000448 virtual void onGpuDrawIndexed(GrPrimitiveType type,
449 uint32_t startVertex,
450 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000451 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000452 uint32_t indexCount) = 0;
453
454 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
455 uint32_t vertexCount,
456 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000458 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000459 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000460
461 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000462 virtual bool onReadPixels(GrRenderTarget* target,
463 int left, int top, int width, int height,
464 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000465
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000467 // indexed geometry. The subclass may adjust the startVertex and/or
468 // startIndex since it may have already accounted for these in the setup.
469 virtual void setupGeometry(int* startVertex,
470 int* startIndex,
471 int vertexCount,
472 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000474 // width and height may be larger than rt (if underlying API allows it).
475 // Should attach the SB to the RT. Returns false if compatible sb could
476 // not be created.
477 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
478 int width,
479 int height) = 0;
480
481 // attaches an existing SB to an existing RT.
482 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
483 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
485 // The GrGpu typically records the clients requested state and then flushes
486 // deltas from previous state at draw time. This function does the
487 // API-specific flush of the state
488 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000489 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490
491 // Sets the scissor rect, or disables if rect is NULL.
492 virtual void flushScissor(const GrIRect* rect) = 0;
493
494 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000495 virtual void clearStencilClip(const GrIRect& rect) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
reed@google.comac10a2d2010-12-22 21:39:39 +0000497private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000498 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000499
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000500 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000502 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503
504 // counts number of uses of vertex/index pool in the geometry stack
505 int fVertexPoolUseCnt;
506 int fIndexPoolUseCnt;
507
508 enum {
509 kPreallocGeomPoolStateStackCnt = 4,
510 };
511 GrAlignedSTStorage<kPreallocGeomPoolStateStackCnt,
512 GeometryPoolState> fGeoSrcStateStackStorage;
513 GrTArray<GeometryPoolState, true> fGeomPoolStateStack;
514
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000515 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
516 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000518 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
519 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000520
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000521 GrDefaultPathRenderer* fDefaultPathRenderer;
522 GrPathRenderer* fClientPathRenderer;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000523
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000524 bool fContextIsDirty;
525
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000526 GrResource* fResourceHead;
527
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000528 // Given a rt, find or create a stencil buffer and attach it
529 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
530
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 // GrDrawTarget overrides
532 virtual void onDrawIndexed(GrPrimitiveType type,
533 int startVertex,
534 int startIndex,
535 int vertexCount,
536 int indexCount);
537 virtual void onDrawNonIndexed(GrPrimitiveType type,
538 int startVertex,
539 int vertexCount);
540
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000541 // readies the pools to provide vertex/index data.
542 void prepareVertexPool();
543 void prepareIndexPool();
544
545 // determines the path renderer used to draw a clip path element.
reed@google.com07f3ee12011-05-16 17:21:57 +0000546 GrPathRenderer* getClipPathRenderer(const SkPath& path, GrPathFill fill);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000547
548 void handleDirtyContext() {
549 if (fContextIsDirty) {
550 this->resetContext();
551 fContextIsDirty = false;
552 }
553 }
554
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000555 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000556};
557
558#endif