blob: e1e85f773c57fba3743519f43186bd7604c2bf8c [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"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrRect.h"
15#include "GrRefCnt.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrTexture.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com669fdc42011-04-05 17:08:27 +000018class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000019class GrIndexBufferAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +000020class GrPathRenderer;
21class GrPathRendererChain;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000022class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000023class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000024class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com05ef5102011-05-02 21:14:59 +000026/**
27 * Gpu usage statistics.
28 */
29struct GrGpuStats {
30 uint32_t fVertexCnt; //<! Number of vertices drawn
31 uint32_t fIndexCnt; //<! Number of indices drawn
32 uint32_t fDrawCnt; //<! Number of draws
33
34 uint32_t fProgChngCnt;//<! Number of program changes (N/A for fixed)
35
bsalomon@google.com030302c2011-08-01 17:35:01 +000036 /**
37 * Number of times the texture is set in 3D API
38 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000039 uint32_t fTextureChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000040 /**
41 * Number of times the render target is set in 3D API
42 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000043 uint32_t fRenderTargetChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000044 /**
45 * Number of textures created (includes textures that are rendertargets).
46 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000047 uint32_t fTextureCreateCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000048 /**
49 * Number of rendertargets created.
50 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000051 uint32_t fRenderTargetCreateCnt;
52};
53
reed@google.comac10a2d2010-12-22 21:39:39 +000054class GrGpu : public GrDrawTarget {
55
56public:
57 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +000058 * Additional blend coeffecients for dual source blending, not exposed
59 * through GrPaint/GrContext.
60 */
61 enum ExtendedBlendCoeffs {
62 // source 2 refers to second output color when
63 // using dual source blending.
64 kS2C_BlendCoeff = kPublicBlendCoeffCount,
65 kIS2C_BlendCoeff,
66 kS2A_BlendCoeff,
67 kIS2A_BlendCoeff,
68
69 kTotalBlendCoeffCount
70 };
71
72 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000073 * Create an instance of GrGpu that matches the specified Engine backend.
74 * If the requested engine is not supported (at compile-time or run-time)
75 * this returns NULL.
76 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000077 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000078
79 ////////////////////////////////////////////////////////////////////////////
80
81 GrGpu();
82 virtual ~GrGpu();
83
bsalomon@google.com669fdc42011-04-05 17:08:27 +000084 // The GrContext sets itself as the owner of this Gpu object
85 void setContext(GrContext* context) {
86 GrAssert(NULL == fContext);
87 fContext = context;
88 }
89 GrContext* getContext() { return fContext; }
90 const GrContext* getContext() const { return fContext; }
91
reed@google.comac10a2d2010-12-22 21:39:39 +000092 /**
93 * The GrGpu object normally assumes that no outsider is setting state
94 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000095 * the GrGpu that the state was modified and it shouldn't make assumptions
96 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000097 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000098 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000099
100 void unimpl(const char[]);
101
102 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000103 * Creates a texture object. If desc width or height is not a power of
104 * two but underlying API requires a power of two texture then srcData
105 * will be embedded in a power of two texture. The extra width and height
106 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000109 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
110 * on the render target until its releaseRenderTarget() is called or it is
111 * destroyed.
112 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 * @param desc describes the texture to be created.
114 * @param srcData texel data to load texture. Begins with full-size
115 * palette data for paletted textures. Contains width*
116 * height texels. If NULL texture data is uninitialized.
117 *
118 * @return The texture object if successful, otherwise NULL.
119 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000120 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000121 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000123 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125 /**
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000126 * Reads the current target object (e.g. FBO or IDirect3DSurface9*) and
127 * viewport state from the underlying 3D API and wraps it in a
128 * GrRenderTarget. The GrRenderTarget will not attempt to delete/destroy the
129 * underlying object in its destructor and it is up to caller to guarantee
130 * that it remains valid while the GrRenderTarget is used.
131 *
132 * @return the newly created GrRenderTarget
133 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000134 GrRenderTarget* createRenderTargetFrom3DApiState();
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000135
136 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000137 * Creates a vertex buffer.
138 *
139 * @param size size in bytes of the vertex buffer
140 * @param dynamic hints whether the data will be frequently changed
141 * by either GrVertexBuffer::lock or
142 * GrVertexBuffer::updateData.
143 *
144 * @return The vertex buffer if successful, otherwise NULL.
145 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
148 /**
149 * Creates an index buffer.
150 *
151 * @param size size in bytes of the index buffer
152 * @param dynamic hints whether the data will be frequently changed
153 * by either GrIndexBuffer::lock or
154 * GrIndexBuffer::updateData.
155 *
156 * @return The index buffer if successful, otherwise NULL.
157 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
160 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 * Are 8 bit paletted textures supported.
162 *
163 * @return true if 8bit palette textures are supported, false otherwise
164 */
165 bool supports8BitPalette() const { return f8bitPaletteSupport; }
166
167 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000168 * returns true if two sided stenciling is supported. If false then only
169 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 * @return true if only a single stencil pass is needed.
171 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000172 bool supportsTwoSidedStencil() const
173 { return fTwoSidedStencilSupport; }
174
175 /**
176 * returns true if stencil wrap is supported. If false then
177 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
178 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
179 * @return true if stencil wrap ops are supported.
180 */
181 bool supportsStencilWrapOps() const
182 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
184 /**
185 * Checks whether locking vertex and index buffers is supported.
186 *
187 * @return true if locking is supported.
188 */
189 bool supportsBufferLocking() const { return fBufferLockSupport; }
190
191 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000192 * Does the 3D API support anti-aliased lines. If so then line primitive
193 * types will use this functionality when the AA state flag is set.
194 */
195 bool supportsAALines() const { return fAALineSupport; }
196
197 /**
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000198 * Does the subclass support GrSamplerState::k4x4Downsample_Filter
199 */
200 bool supports4x4DownsampleFilter() const { return f4X4DownsampleFilterSupport; }
201
202 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000203 * Does this instance support dual-source blending? Required for proper
204 * blending with partial coverage with certain blend modes (dst coeff is
205 * not 1, ISA, or ISC)
206 */
207 bool supportsDualSourceBlending() const {
208 return fDualSourceBlendingSupport;
209 }
210
211 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 * Gets the minimum width of a render target. If a texture/rt is created
213 * with a width less than this size the GrGpu object will clamp it to this
214 * value.
215 */
216 int minRenderTargetWidth() const { return fMinRenderTargetWidth; }
217
218 /**
219 * Gets the minimum width of a render target. If a texture/rt is created
220 * with a height less than this size the GrGpu object will clamp it to this
221 * value.
222 */
223 int minRenderTargetHeight() const { return fMinRenderTargetHeight; }
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000224
225 /**
226 * Reports whether full scene anti-aliasing is supported.
227 */
228 bool supportsFullsceneAA() const { return fFSAASupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
230 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000231 * Returns true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 *
bsalomon@google.com0748f212011-02-01 22:56:16 +0000233 * @return true if NPOT textures can be created
reed@google.comac10a2d2010-12-22 21:39:39 +0000234 */
bsalomon@google.com0748f212011-02-01 22:56:16 +0000235 bool npotTextureSupport() const { return fNPOTTextureSupport; }
236
237 /**
238 * Returns true if NPOT textures can be repeat/mirror tiled.
239 *
240 * @return true if NPOT textures can be tiled
241 */
242 bool npotTextureTileSupport() const { return fNPOTTextureTileSupport; }
243
244 /**
245 * Returns true if a NPOT texture can be a rendertarget
246 *
247 * @return the true if NPOT texture/rendertarget can be created.
248 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000249 bool npotRenderTargetSupport() const { return fNPOTRenderTargetSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
bsalomon@google.com91958362011-06-13 17:58:13 +0000251 /**
252 * Gets the largest allowed width and height of a texture.
253 */
254 int maxTextureSize() const { return fMaxTextureSize; }
255 /**
256 * Gets the largest allowed width and height of a render target.
257 */
258 int maxRenderTargetSize() const { return fMaxRenderTargetSize; }
reed@google.com02a7e6c2011-01-28 21:21:49 +0000259
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000260 virtual void clear(const GrIRect* rect, GrColor color);
reed@google.comac10a2d2010-12-22 21:39:39 +0000261
262 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000264 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
265 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000266 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000267 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000269 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000272 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000273 * (1,1), (0,1)].
274 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000275 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000276 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000277
278 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 * Ensures that the current render target is actually set in the
280 * underlying 3D API. Used when client wants to use 3D API to directly
281 * render to the RT.
282 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000283 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000285 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000286 * Reads a rectangle of pixels from a render target.
287 * @param renderTarget the render target to read from. NULL means the
288 * current render target.
289 * @param left left edge of the rectangle to read (inclusive)
290 * @param top top edge of the rectangle to read (inclusive)
291 * @param width width of rectangle to read in pixels.
292 * @param height height of rectangle to read in pixels.
293 * @param config the pixel config of the destination buffer
294 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000295 *
296 * @return true if the read succeeded, false if not. The read can fail
297 * because of a unsupported pixel config or because no render
298 * target is currently set.
299 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000300 bool readPixels(GrRenderTarget* renderTarget,
301 int left, int top, int width, int height,
302 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000304 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 void resetStats();
306 void printStats() const;
307
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000308 /**
309 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000310 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000311 */
junov@google.com53a55842011-06-08 22:55:10 +0000312 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000313
314 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000315 * Called to tell Gpu object to release all GrResources. Overrides must call
316 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000317 */
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
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000342 // keep track of whether we are using stencil clipping (as opposed to
343 // scissor).
344 bool fClipInStencil;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000345
346 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000347 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 // Functions used to map clip-respecting stencil tests into normal
350 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000351 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000352 GrStencilFunc func);
353 static void ConvertStencilFuncAndMask(GrStencilFunc func,
354 bool clipInStencil,
355 unsigned int clipBit,
356 unsigned int userBits,
357 unsigned int* ref,
358 unsigned int* mask);
359
360 // stencil settings to clip drawing when stencil clipping is in effect
361 // and the client isn't using the stencil test.
362 static const GrStencilSettings gClipStencilSettings;
363
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 // defaults to false, subclass can set true to support palleted textures
365 bool f8bitPaletteSupport;
366
bsalomon@google.com0748f212011-02-01 22:56:16 +0000367 // set by subclass
368 bool fNPOTTextureSupport;
369 bool fNPOTTextureTileSupport;
370 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000371 bool fTwoSidedStencilSupport;
372 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000373 bool fAALineSupport;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000374 bool fFSAASupport;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000375 bool f4X4DownsampleFilterSupport; // supports GrSamplerState::k4x4Downsample_Filter
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000376 bool fDualSourceBlendingSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
378 // set by subclass to true if index and vertex buffers can be locked, false
379 // otherwise.
380 bool fBufferLockSupport;
381
382 // set by subclass
383 int fMinRenderTargetWidth;
384 int fMinRenderTargetHeight;
bsalomon@google.com91958362011-06-13 17:58:13 +0000385 int fMaxRenderTargetSize;
386 int fMaxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000387
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000388 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000389
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000390 struct GeometryPoolState {
391 const GrVertexBuffer* fPoolVertexBuffer;
392 int fPoolStartVertex;
393
394 const GrIndexBuffer* fPoolIndexBuffer;
395 int fPoolStartIndex;
396 };
397 const GeometryPoolState& getGeomPoolState() {
398 return fGeomPoolStateStack.back();
399 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400
401 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000402 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
403 int vertexCount,
404 void** vertices);
405 virtual bool onReserveIndexSpace(int indexCount, void** indices);
406 virtual void releaseReservedVertexSpace();
407 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000408 virtual void onSetVertexSourceToArray(const void* vertexArray,
409 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000410 virtual void onSetIndexSourceToArray(const void* indexArray,
411 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000412 virtual void releaseVertexArray();
413 virtual void releaseIndexArray();
414 virtual void geometrySourceWillPush();
415 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
416
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417 // Helpers for setting up geometry state
418 void finalizeReservedVertices();
419 void finalizeReservedIndices();
420
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000421 // overridden by API-specific derived class to handle re-emitting 3D API
422 // preample and dirtying state cache.
423 virtual void resetContext() = 0;
424
425 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000426 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000427 const void* srcData,
428 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000429 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000430 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState() = 0;
431 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
432 bool dynamic) = 0;
433 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
434 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000435
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000436 // overridden by API-specific derivated class to perform the clear and
437 // clearRect. NULL rect means clear whole target.
438 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000439
440 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000441 virtual void onGpuDrawIndexed(GrPrimitiveType type,
442 uint32_t startVertex,
443 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000444 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000445 uint32_t indexCount) = 0;
446
447 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
448 uint32_t vertexCount,
449 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000450
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000451 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000452 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000453
454 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000455 virtual bool onReadPixels(GrRenderTarget* target,
456 int left, int top, int width, int height,
457 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000458
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000460 // indexed geometry. The subclass may adjust the startVertex and/or
461 // startIndex since it may have already accounted for these in the setup.
462 virtual void setupGeometry(int* startVertex,
463 int* startIndex,
464 int vertexCount,
465 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000467 // width and height may be larger than rt (if underlying API allows it).
468 // Should attach the SB to the RT. Returns false if compatible sb could
469 // not be created.
470 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
471 int width,
472 int height) = 0;
473
474 // attaches an existing SB to an existing RT.
475 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
476 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
478 // The GrGpu typically records the clients requested state and then flushes
479 // deltas from previous state at draw time. This function does the
480 // API-specific flush of the state
481 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000482 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
484 // Sets the scissor rect, or disables if rect is NULL.
485 virtual void flushScissor(const GrIRect* rect) = 0;
486
487 // GrGpu subclass removes the clip from the stencil buffer
bsalomon@google.com398109c2011-04-14 18:40:27 +0000488 virtual void clearStencilClip(const GrIRect& rect) = 0;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000489 // clears the entire stencil buffer to 0
490 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000491
reed@google.comac10a2d2010-12-22 21:39:39 +0000492private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000493 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000494
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000495 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000497 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000498
499 // counts number of uses of vertex/index pool in the geometry stack
500 int fVertexPoolUseCnt;
501 int fIndexPoolUseCnt;
502
503 enum {
504 kPreallocGeomPoolStateStackCnt = 4,
505 };
506 GrAlignedSTStorage<kPreallocGeomPoolStateStackCnt,
507 GeometryPoolState> fGeoSrcStateStackStorage;
508 GrTArray<GeometryPoolState, true> fGeomPoolStateStack;
509
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000510 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
511 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000513 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
514 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000515
bsalomon@google.com30085192011-08-19 15:42:31 +0000516 // must be instantiated after GrGpu object has been given its owning
517 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
518 GrPathRendererChain* fPathRendererChain;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000519
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000520 bool fContextIsDirty;
521
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000522 GrResource* fResourceHead;
523
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000524 // Given a rt, find or create a stencil buffer and attach it
525 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
526
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527 // GrDrawTarget overrides
528 virtual void onDrawIndexed(GrPrimitiveType type,
529 int startVertex,
530 int startIndex,
531 int vertexCount,
532 int indexCount);
533 virtual void onDrawNonIndexed(GrPrimitiveType type,
534 int startVertex,
535 int vertexCount);
536
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000537 // readies the pools to provide vertex/index data.
538 void prepareVertexPool();
539 void prepareIndexPool();
540
541 // determines the path renderer used to draw a clip path element.
reed@google.com07f3ee12011-05-16 17:21:57 +0000542 GrPathRenderer* getClipPathRenderer(const SkPath& path, GrPathFill fill);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543
544 void handleDirtyContext() {
545 if (fContextIsDirty) {
546 this->resetContext();
547 fContextIsDirty = false;
548 }
549 }
550
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000551 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552};
553
554#endif