blob: e970554bcfa78e9ef693d58116d7a38af460682b [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 /**
126 * Creates a vertex buffer.
127 *
128 * @param size size in bytes of the vertex buffer
129 * @param dynamic hints whether the data will be frequently changed
130 * by either GrVertexBuffer::lock or
131 * GrVertexBuffer::updateData.
132 *
133 * @return The vertex buffer if successful, otherwise NULL.
134 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000135 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
137 /**
138 * Creates an index buffer.
139 *
140 * @param size size in bytes of the index buffer
141 * @param dynamic hints whether the data will be frequently changed
142 * by either GrIndexBuffer::lock or
143 * GrIndexBuffer::updateData.
144 *
145 * @return The index buffer if successful, otherwise NULL.
146 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000147 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000148
149 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 * Are 8 bit paletted textures supported.
151 *
152 * @return true if 8bit palette textures are supported, false otherwise
153 */
154 bool supports8BitPalette() const { return f8bitPaletteSupport; }
155
156 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000157 * returns true if two sided stenciling is supported. If false then only
158 * the front face values of the GrStencilSettings
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 * @return true if only a single stencil pass is needed.
160 */
bsalomon@google.comd302f142011-03-03 13:54:13 +0000161 bool supportsTwoSidedStencil() const
162 { return fTwoSidedStencilSupport; }
163
164 /**
165 * returns true if stencil wrap is supported. If false then
166 * kIncWrap_StencilOp and kDecWrap_StencilOp are treated as
167 * kIncClamp_StencilOp and kDecClamp_StencilOp, respectively.
168 * @return true if stencil wrap ops are supported.
169 */
170 bool supportsStencilWrapOps() const
171 { return fStencilWrapOpsSupport; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000172
173 /**
174 * Checks whether locking vertex and index buffers is supported.
175 *
176 * @return true if locking is supported.
177 */
178 bool supportsBufferLocking() const { return fBufferLockSupport; }
179
180 /**
bsalomon@google.com205d4602011-04-25 12:43:45 +0000181 * Does the 3D API support anti-aliased lines. If so then line primitive
182 * types will use this functionality when the AA state flag is set.
183 */
bsalomon@google.com471d4712011-08-23 15:45:25 +0000184 bool supportsHWAALines() const { return fAALineSupport; }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000185
186 /**
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000187 * Are shaders supported.
188 */
189 bool supportsShaders() const { return fShaderSupport; }
190
191 /**
192 * Are derivative instructions supported in fragment shaders
193 */
194 bool supportsShaderDerivatives() const { return fShaderDerivativeSupport; }
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 */
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000206 bool supportsDualSourceBlending() const {
207 return fDualSourceBlendingSupport;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000208 }
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 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000262 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000263 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
264 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000266 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000268 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
270 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000271 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000272 * (1,1), (0,1)].
273 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000274 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000275 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000276
277 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 * Ensures that the current render target is actually set in the
279 * underlying 3D API. Used when client wants to use 3D API to directly
280 * render to the RT.
281 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000282 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000283
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000284 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000285 * Reads a rectangle of pixels from a render target.
286 * @param renderTarget the render target to read from. NULL means the
287 * current render target.
288 * @param left left edge of the rectangle to read (inclusive)
289 * @param top top edge of the rectangle to read (inclusive)
290 * @param width width of rectangle to read in pixels.
291 * @param height height of rectangle to read in pixels.
292 * @param config the pixel config of the destination buffer
293 * @param buffer memory to read the rectangle into.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000294 *
295 * @return true if the read succeeded, false if not. The read can fail
296 * because of a unsupported pixel config or because no render
297 * target is currently set.
298 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000299 bool readPixels(GrRenderTarget* renderTarget,
300 int left, int top, int width, int height,
301 GrPixelConfig config, void* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000302
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000303 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 void resetStats();
305 void printStats() const;
306
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000307 /**
308 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000309 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000310 */
junov@google.com53a55842011-06-08 22:55:10 +0000311 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000312
313 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000314 * Called to tell Gpu object to release all GrResources. Overrides must call
315 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000316 */
317 void releaseResources();
318
319 /**
320 * Add resource to list of resources. Should only be called by GrResource.
321 * @param resource the resource to add.
322 */
323 void insertResource(GrResource* resource);
324
325 /**
326 * Remove resource from list of resources. Should only be called by
327 * GrResource.
328 * @param resource the resource to remove.
329 */
330 void removeResource(GrResource* resource);
331
bsalomon@google.com471d4712011-08-23 15:45:25 +0000332 // GrDrawTarget overrides
333 virtual bool willUseHWAALines() const;
334
reed@google.comac10a2d2010-12-22 21:39:39 +0000335protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336 enum PrivateStateBits {
337 kFirstBit = (kLastPublicStateBit << 1),
338
339 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
340 // stencil bits used for
341 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 };
343
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000344 // keep track of whether we are using stencil clipping (as opposed to
345 // scissor).
346 bool fClipInStencil;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000347
348 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000349 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351 // Functions used to map clip-respecting stencil tests into normal
352 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000353 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 GrStencilFunc func);
355 static void ConvertStencilFuncAndMask(GrStencilFunc func,
356 bool clipInStencil,
357 unsigned int clipBit,
358 unsigned int userBits,
359 unsigned int* ref,
360 unsigned int* mask);
361
362 // stencil settings to clip drawing when stencil clipping is in effect
363 // and the client isn't using the stencil test.
364 static const GrStencilSettings gClipStencilSettings;
365
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 // defaults to false, subclass can set true to support palleted textures
367 bool f8bitPaletteSupport;
368
bsalomon@google.com0748f212011-02-01 22:56:16 +0000369 // set by subclass
370 bool fNPOTTextureSupport;
371 bool fNPOTTextureTileSupport;
372 bool fNPOTRenderTargetSupport;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 bool fTwoSidedStencilSupport;
374 bool fStencilWrapOpsSupport;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000375 bool fAALineSupport;
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000376 bool fShaderSupport;
377 bool fShaderDerivativeSupport;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000378 bool fFSAASupport;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000379 bool f4X4DownsampleFilterSupport; // supports GrSamplerState::k4x4Downsample_Filter
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000380 bool fDualSourceBlendingSupport;
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 // set by subclass to true if index and vertex buffers can be locked, false
383 // otherwise.
384 bool fBufferLockSupport;
385
386 // set by subclass
387 int fMinRenderTargetWidth;
388 int fMinRenderTargetHeight;
bsalomon@google.com91958362011-06-13 17:58:13 +0000389 int fMaxRenderTargetSize;
390 int fMaxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000392 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000393
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000394 struct GeometryPoolState {
395 const GrVertexBuffer* fPoolVertexBuffer;
396 int fPoolStartVertex;
397
398 const GrIndexBuffer* fPoolIndexBuffer;
399 int fPoolStartIndex;
400 };
401 const GeometryPoolState& getGeomPoolState() {
402 return fGeomPoolStateStack.back();
403 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000404
405 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
407 int vertexCount,
408 void** vertices);
409 virtual bool onReserveIndexSpace(int indexCount, void** indices);
410 virtual void releaseReservedVertexSpace();
411 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000412 virtual void onSetVertexSourceToArray(const void* vertexArray,
413 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000414 virtual void onSetIndexSourceToArray(const void* indexArray,
415 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000416 virtual void releaseVertexArray();
417 virtual void releaseIndexArray();
418 virtual void geometrySourceWillPush();
419 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
420
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000421 // Helpers for setting up geometry state
422 void finalizeReservedVertices();
423 void finalizeReservedIndices();
424
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000425 // overridden by API-specific derived class to handle re-emitting 3D API
426 // preample and dirtying state cache.
427 virtual void resetContext() = 0;
428
429 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000430 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000431 const void* srcData,
432 size_t rowBytes) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000433 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000434 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
435 bool dynamic) = 0;
436 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
437 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000439 // overridden by API-specific derivated class to perform the clear and
440 // clearRect. NULL rect means clear whole target.
441 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442
443 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 virtual void onGpuDrawIndexed(GrPrimitiveType type,
445 uint32_t startVertex,
446 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000447 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000448 uint32_t indexCount) = 0;
449
450 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
451 uint32_t vertexCount,
452 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000453
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000454 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000455 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000456
457 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000458 virtual bool onReadPixels(GrRenderTarget* target,
459 int left, int top, int width, int height,
460 GrPixelConfig, void* buffer) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000461
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000463 // indexed geometry. The subclass may adjust the startVertex and/or
464 // startIndex since it may have already accounted for these in the setup.
465 virtual void setupGeometry(int* startVertex,
466 int* startIndex,
467 int vertexCount,
468 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000470 // width and height may be larger than rt (if underlying API allows it).
471 // Should attach the SB to the RT. Returns false if compatible sb could
472 // not be created.
473 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
474 int width,
475 int height) = 0;
476
477 // attaches an existing SB to an existing RT.
478 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
479 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
481 // The GrGpu typically records the clients requested state and then flushes
482 // deltas from previous state at draw time. This function does the
483 // API-specific flush of the state
484 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000485 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
487 // Sets the scissor rect, or disables if rect is NULL.
488 virtual void flushScissor(const GrIRect* rect) = 0;
489
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000490 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
491 // free to clear the remaining bits to zero if masked clears are more
492 // expensive than clearing all bits.
493 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
494
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000495 // clears the entire stencil buffer to 0
496 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
reed@google.comac10a2d2010-12-22 21:39:39 +0000498private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000499 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000500
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000501 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000503 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000504
505 // counts number of uses of vertex/index pool in the geometry stack
506 int fVertexPoolUseCnt;
507 int fIndexPoolUseCnt;
508
509 enum {
510 kPreallocGeomPoolStateStackCnt = 4,
511 };
bsalomon@google.com49313f62011-09-14 13:54:05 +0000512 SkAlignedSTStorage<kPreallocGeomPoolStateStackCnt,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000513 GeometryPoolState> fGeoSrcStateStackStorage;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000514 SkTArray<GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000515
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000516 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
517 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000519 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
520 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000521
bsalomon@google.com30085192011-08-19 15:42:31 +0000522 // must be instantiated after GrGpu object has been given its owning
523 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
524 GrPathRendererChain* fPathRendererChain;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000525
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000526 bool fContextIsDirty;
527
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000528 GrResource* fResourceHead;
529
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000530 // Given a rt, find or create a stencil buffer and attach it
531 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
532
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 // GrDrawTarget overrides
534 virtual void onDrawIndexed(GrPrimitiveType type,
535 int startVertex,
536 int startIndex,
537 int vertexCount,
538 int indexCount);
539 virtual void onDrawNonIndexed(GrPrimitiveType type,
540 int startVertex,
541 int vertexCount);
542
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000543 // readies the pools to provide vertex/index data.
544 void prepareVertexPool();
545 void prepareIndexPool();
546
547 // determines the path renderer used to draw a clip path element.
reed@google.com07f3ee12011-05-16 17:21:57 +0000548 GrPathRenderer* getClipPathRenderer(const SkPath& path, GrPathFill fill);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549
550 void handleDirtyContext() {
551 if (fContextIsDirty) {
552 this->resetContext();
553 fContextIsDirty = false;
554 }
555 }
556
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000557 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000558};
559
560#endif