blob: 7bba3558a054d57482146eb608c7e0ee0a973aa4 [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
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000034 uint32_t fProgChngCnt;//<! Number of program changes
bsalomon@google.com05ef5102011-05-02 21:14:59 +000035
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
robertphillips@google.com730ebe52012-04-16 16:33:13 +000054/**
55 * Scissoring needs special handling during stencil clip mask creation
56 * since the creation process re-entrantly invokes setupClipAndFlushState.
57 * During this process the call stack is used to keep
58 * track of (and apply to the GPU) the current scissor settings.
59 */
60struct ScissoringSettings {
61 bool fEnableScissoring;
62 GrIRect fScissorRect;
63
64 void setupScissoring(GrGpu* gpu);
65};
66
67/**
68 * The clip mask creator handles the generation of the clip mask. If anti
69 * aliasing is requested it will (in the future) generate a single channel
70 * (8bit) mask. If no anti aliasing is requested it will generate a 1-bit
71 * mask in the stencil buffer. In the non anti-aliasing case, if the clip
72 * mask can be represented as a rectangle then scissoring is used. In all
73 * cases scissoring is used to bound the range of the clip mask.
74 */
75class GrClipMaskManager {
76public:
77 GrClipMaskManager()
78 : fClipMaskInStencil(false)
79 , fPathRendererChain(NULL) {
80 }
81
82 bool createClipMask(GrGpu* gpu,
83 const GrClip& clip,
84 ScissoringSettings* scissorSettings);
85
86 void freeResources();
87
88 bool isClipInStencil() const { return fClipMaskInStencil; }
89
90 void resetMask() {
91 fClipMaskInStencil = false;
92 }
93
94protected:
95private:
96 bool fClipMaskInStencil; // is the clip mask in the stencil buffer?
97
98 // must be instantiated after GrGpu object has been given its owning
99 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
100 GrPathRendererChain* fPathRendererChain;
101
102 bool createStencilClipMask(GrGpu* gpu,
103 const GrClip& clip,
104 const GrRect& bounds,
105 ScissoringSettings* scissorSettings);
106
107 // determines the path renderer used to draw a clip path element.
108 GrPathRenderer* getClipPathRenderer(GrGpu* gpu,
109 const SkPath& path,
110 GrPathFill fill);
111
112};
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114class GrGpu : public GrDrawTarget {
115
116public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000117
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000119 * Additional blend coeffecients for dual source blending, not exposed
120 * through GrPaint/GrContext.
121 */
122 enum ExtendedBlendCoeffs {
123 // source 2 refers to second output color when
124 // using dual source blending.
125 kS2C_BlendCoeff = kPublicBlendCoeffCount,
126 kIS2C_BlendCoeff,
127 kS2A_BlendCoeff,
128 kIS2A_BlendCoeff,
129
130 kTotalBlendCoeffCount
131 };
132
133 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 * Create an instance of GrGpu that matches the specified Engine backend.
135 * If the requested engine is not supported (at compile-time or run-time)
136 * this returns NULL.
137 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000138 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
140 ////////////////////////////////////////////////////////////////////////////
141
142 GrGpu();
143 virtual ~GrGpu();
144
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000145 // The GrContext sets itself as the owner of this Gpu object
146 void setContext(GrContext* context) {
147 GrAssert(NULL == fContext);
148 fContext = context;
149 }
150 GrContext* getContext() { return fContext; }
151 const GrContext* getContext() const { return fContext; }
152
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 /**
154 * The GrGpu object normally assumes that no outsider is setting state
155 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000156 * the GrGpu that the state was modified and it shouldn't make assumptions
157 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000159 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000160
161 void unimpl(const char[]);
162
163 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000164 * Creates a texture object. If desc width or height is not a power of
165 * two but underlying API requires a power of two texture then srcData
166 * will be embedded in a power of two texture. The extra width and height
167 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000168 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000169 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000170 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
171 * on the render target until its releaseRenderTarget() is called or it is
172 * destroyed.
173 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000174 * @param desc describes the texture to be created.
175 * @param srcData texel data to load texture. Begins with full-size
176 * palette data for paletted textures. Contains width*
177 * height texels. If NULL texture data is uninitialized.
178 *
179 * @return The texture object if successful, otherwise NULL.
180 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000181 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000182 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.come269f212011-11-07 13:29:52 +0000184 /**
185 * Implements GrContext::createPlatformTexture
186 */
187 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
188
189 /**
190 * Implements GrContext::createPlatformTexture
191 */
192 GrRenderTarget* createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc);
193
194 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 * Creates a vertex buffer.
196 *
197 * @param size size in bytes of the vertex buffer
198 * @param dynamic hints whether the data will be frequently changed
199 * by either GrVertexBuffer::lock or
200 * GrVertexBuffer::updateData.
201 *
202 * @return The vertex buffer if successful, otherwise NULL.
203 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
206 /**
207 * Creates an index buffer.
208 *
209 * @param size size in bytes of the index buffer
210 * @param dynamic hints whether the data will be frequently changed
211 * by either GrIndexBuffer::lock or
212 * GrIndexBuffer::updateData.
213 *
214 * @return The index buffer if successful, otherwise NULL.
215 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
218 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000220 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
221 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000222 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000223 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000225 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
227 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000228 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000229 * (1,1), (0,1)].
230 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000231 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000232 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000233
234 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000235 * Resolves MSAA.
236 */
237 void resolveRenderTarget(GrRenderTarget* target);
238
239 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 * Ensures that the current render target is actually set in the
241 * underlying 3D API. Used when client wants to use 3D API to directly
242 * render to the RT.
243 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000244 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000246 /**
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000247 * If this returns true then a sequence that reads unpremultiplied pixels
248 * from a surface, writes back the same values, and reads them again will
249 * give the same pixel values back in both reads.
250 */
251 virtual bool canPreserveReadWriteUnpremulPixels() = 0;
252
253 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000254 * readPixels with some configs may be slow. Given a desired config this
255 * function returns a fast-path config. The returned config must have the
256 * same components, component sizes, and not require conversion between
257 * pre- and unpremultiplied alpha. The caller is free to ignore the result
258 * and call readPixels with the original config.
259 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000260 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
261 const {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000262 return config;
263 }
264
265 /**
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000266 * Same as above but applies to writeTexturePixels
267 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000268 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
269 const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000270 return config;
271 }
272
273 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000274 * OpenGL's readPixels returns the result bottom-to-top while the skia
275 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
276 * solution is to have the subclass do the flip using either the CPU or GPU.
277 * However, the caller (GrContext) may have transformations to apply and can
278 * simply fold in the y-flip for free. On the other hand, the subclass may
279 * be able to do it for free itself. For example, the subclass may have to
280 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
281 * concurrently.
282 *
283 * This function returns true if a y-flip is required to put the pixels in
284 * top-to-bottom order and the subclass cannot do it for free.
285 *
286 * See read pixels for the params
287 * @return true if calling readPixels with the same set of params will
288 * produce bottom-to-top data
289 */
290 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
291 int left, int top,
292 int width, int height,
293 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000294 size_t rowBytes) const = 0;
295 /**
296 * This should return true if reading a NxM rectangle of pixels from a
297 * render target is faster if the target has dimensons N and M and the read
298 * rectangle has its top-left at 0,0.
299 */
300 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301
302 /**
303 * Reads a rectangle of pixels from a render target. Fails if read requires
304 * conversion between premultiplied and unpremultiplied configs. The caller
305 * should do the conversion by rendering to a target with the desire config
306 * first.
307 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000308 * @param renderTarget the render target to read from. NULL means the
309 * current render target.
310 * @param left left edge of the rectangle to read (inclusive)
311 * @param top top edge of the rectangle to read (inclusive)
312 * @param width width of rectangle to read in pixels.
313 * @param height height of rectangle to read in pixels.
314 * @param config the pixel config of the destination buffer
315 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000316 * @param rowBytes the number of bytes between consecutive rows. Zero
317 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 * @param invertY buffer should be populated bottom-to-top as opposed
319 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000320 *
321 * @return true if the read succeeded, false if not. The read can fail
322 * because of a unsupported pixel config or because no render
323 * target is currently set.
324 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000325 bool readPixels(GrRenderTarget* renderTarget,
326 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000327 GrPixelConfig config, void* buffer, size_t rowBytes,
328 bool invertY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
bsalomon@google.com6f379512011-11-16 20:36:03 +0000330 /**
331 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000332 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000333 * @param left left edge of the rectangle to write (inclusive)
334 * @param top top edge of the rectangle to write (inclusive)
335 * @param width width of rectangle to write in pixels.
336 * @param height height of rectangle to write in pixels.
337 * @param config the pixel config of the source buffer
338 * @param buffer memory to read pixels from
339 * @param rowBytes number of bytes bewtween consecutive rows. Zero
340 * means rows are tightly packed.
341 */
342 void writeTexturePixels(GrTexture* texture,
343 int left, int top, int width, int height,
344 GrPixelConfig config, const void* buffer,
345 size_t rowBytes);
346
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000347 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 void resetStats();
349 void printStats() const;
350
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000351 /**
352 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000353 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000354 */
junov@google.com53a55842011-06-08 22:55:10 +0000355 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000356
357 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000358 * Called to tell Gpu object to release all GrResources. Overrides must call
359 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000360 */
361 void releaseResources();
362
363 /**
364 * Add resource to list of resources. Should only be called by GrResource.
365 * @param resource the resource to add.
366 */
367 void insertResource(GrResource* resource);
368
369 /**
370 * Remove resource from list of resources. Should only be called by
371 * GrResource.
372 * @param resource the resource to remove.
373 */
374 void removeResource(GrResource* resource);
375
bsalomon@google.com471d4712011-08-23 15:45:25 +0000376 // GrDrawTarget overrides
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000377 virtual void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000378
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000379 // After the client interacts directly with the 3D context state the GrGpu
380 // must resync its internal state and assumptions about 3D context state.
381 // Each time this occurs the GrGpu bumps a timestamp.
382 // state of the 3D context
383 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
384 // a billion years.
385 typedef uint64_t ResetTimestamp;
386
387 // This timestamp is always older than the current timestamp
388 static const ResetTimestamp kExpiredTimestamp = 0;
389 // Returns a timestamp based on the number of times the context was reset.
390 // This timestamp can be used to lazily detect when cached 3D context state
391 // is dirty.
392 ResetTimestamp getResetTimestamp() const {
393 return fResetTimestamp;
394 }
395
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000396 /**
397 * Can the provided configuration act as a color render target?
398 */
399 bool isConfigRenderable(GrPixelConfig config) const {
400 GrAssert(kGrPixelConfigCount > config);
401 return fConfigRenderSupport[config];
402 }
403
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000404 virtual void enableScissoring(const GrIRect& rect) = 0;
405 virtual void disableScissor() = 0;
406
407 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
408 // free to clear the remaining bits to zero if masked clears are more
409 // expensive than clearing all bits.
410 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
411
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000412 enum PrivateDrawStateStateBits {
413 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000414
415 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
416 // stencil bits used for
417 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000418 };
419
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000420protected:
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000421 // 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.
digit@google.com9b482c42012-02-16 22:03:26 +0000437 static const GrStencilSettings* GetClipStencilSettings();
reed@google.comac10a2d2010-12-22 21:39:39 +0000438
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000439 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000440
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000441 GrClipMaskManager fClipMaskManager;
442
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000443 struct GeometryPoolState {
444 const GrVertexBuffer* fPoolVertexBuffer;
445 int fPoolStartVertex;
446
447 const GrIndexBuffer* fPoolIndexBuffer;
448 int fPoolStartIndex;
449 };
450 const GeometryPoolState& getGeomPoolState() {
451 return fGeomPoolStateStack.back();
452 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000453
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000454 // Derived classes need access to this so they can fill it out in their
455 // constructors
456 bool fConfigRenderSupport[kGrPixelConfigCount];
457
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000458 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
460 int vertexCount,
461 void** vertices);
462 virtual bool onReserveIndexSpace(int indexCount, void** indices);
463 virtual void releaseReservedVertexSpace();
464 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000465 virtual void onSetVertexSourceToArray(const void* vertexArray,
466 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000467 virtual void onSetIndexSourceToArray(const void* indexArray,
468 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469 virtual void releaseVertexArray();
470 virtual void releaseIndexArray();
471 virtual void geometrySourceWillPush();
472 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
473
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000474 // Helpers for setting up geometry state
475 void finalizeReservedVertices();
476 void finalizeReservedIndices();
477
bsalomon@google.comb635d392011-11-05 12:47:43 +0000478 // called when the 3D context state is unknown. Subclass should emit any
479 // assumed 3D context state and dirty any state cache
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000480 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000481
bsalomon@google.comb635d392011-11-05 12:47:43 +0000482
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000483 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000484 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000485 const void* srcData,
486 size_t rowBytes) = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000487 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) = 0;
488 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000489 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
490 bool dynamic) = 0;
491 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
492 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000493
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000494 // overridden by API-specific derivated class to perform the clear and
495 // clearRect. NULL rect means clear whole target.
496 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000497
498 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000499 virtual void onGpuDrawIndexed(GrPrimitiveType type,
500 uint32_t startVertex,
501 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000502 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503 uint32_t indexCount) = 0;
504
505 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
506 uint32_t vertexCount,
507 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000509 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000510 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000511
512 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000513 virtual bool onReadPixels(GrRenderTarget* target,
514 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000515 GrPixelConfig,
516 void* buffer,
517 size_t rowBytes,
518 bool invertY) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000519
bsalomon@google.com6f379512011-11-16 20:36:03 +0000520 // overridden by API-specific derived class to perform the texture update
521 virtual void onWriteTexturePixels(GrTexture* texture,
522 int left, int top, int width, int height,
523 GrPixelConfig config, const void* buffer,
524 size_t rowBytes) = 0;
525
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000526 // overridden by API-specific derived class to perform the resolve
527 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
528
reed@google.comac10a2d2010-12-22 21:39:39 +0000529 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530 // indexed geometry. The subclass may adjust the startVertex and/or
531 // startIndex since it may have already accounted for these in the setup.
532 virtual void setupGeometry(int* startVertex,
533 int* startIndex,
534 int vertexCount,
535 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000537 // width and height may be larger than rt (if underlying API allows it).
538 // Should attach the SB to the RT. Returns false if compatible sb could
539 // not be created.
540 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
541 int width,
542 int height) = 0;
543
544 // attaches an existing SB to an existing RT.
545 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
546 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
548 // The GrGpu typically records the clients requested state and then flushes
549 // deltas from previous state at draw time. This function does the
550 // API-specific flush of the state
551 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000552 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000554 // clears the entire stencil buffer to 0
555 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000556
reed@google.comac10a2d2010-12-22 21:39:39 +0000557private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000558 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000559
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000560 ResetTimestamp fResetTimestamp;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000561
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000562 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000564 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000565
566 // counts number of uses of vertex/index pool in the geometry stack
567 int fVertexPoolUseCnt;
568 int fIndexPoolUseCnt;
569
570 enum {
571 kPreallocGeomPoolStateStackCnt = 4,
572 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000573 SkSTArray<kPreallocGeomPoolStateStackCnt,
574 GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000575
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000576 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
577 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000579 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
580 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000581
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000582 bool fContextIsDirty;
583
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000584 GrResource* fResourceHead;
585
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000586 // Given a rt, find or create a stencil buffer and attach it
587 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
588
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000589 // GrDrawTarget overrides
590 virtual void onDrawIndexed(GrPrimitiveType type,
591 int startVertex,
592 int startIndex,
593 int vertexCount,
594 int indexCount);
595 virtual void onDrawNonIndexed(GrPrimitiveType type,
596 int startVertex,
597 int vertexCount);
598
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000599 // readies the pools to provide vertex/index data.
600 void prepareVertexPool();
601 void prepareIndexPool();
602
bsalomon@google.comb635d392011-11-05 12:47:43 +0000603 void resetContext() {
604 this->onResetContext();
605 ++fResetTimestamp;
606 }
607
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000608 void handleDirtyContext() {
609 if (fContextIsDirty) {
610 this->resetContext();
611 fContextIsDirty = false;
612 }
613 }
614
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000615 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000616};
617
618#endif