blob: d39ace0bf0f82e8ec9e4201a1174ab92dc3ca6cb [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"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000017#include "GrClipMaskManager.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.com30085192011-08-19 15:42:31 +000021class GrPathRenderer;
22class GrPathRendererChain;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000024class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000025class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27class GrGpu : public GrDrawTarget {
28
29public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000030
reed@google.comac10a2d2010-12-22 21:39:39 +000031 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +000032 * Additional blend coeffecients for dual source blending, not exposed
33 * through GrPaint/GrContext.
34 */
35 enum ExtendedBlendCoeffs {
36 // source 2 refers to second output color when
37 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000038 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
39 kIS2C_GrBlendCoeff,
40 kS2A_GrBlendCoeff,
41 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042
bsalomon@google.com47059542012-06-06 20:51:20 +000043 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000044 };
45
46 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000047 * Create an instance of GrGpu that matches the specified Engine backend.
48 * If the requested engine is not supported (at compile-time or run-time)
49 * this returns NULL.
50 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000051 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000052
53 ////////////////////////////////////////////////////////////////////////////
54
55 GrGpu();
56 virtual ~GrGpu();
57
bsalomon@google.com669fdc42011-04-05 17:08:27 +000058 // The GrContext sets itself as the owner of this Gpu object
59 void setContext(GrContext* context) {
60 GrAssert(NULL == fContext);
61 fContext = context;
robertphillips@google.comf105b102012-05-14 12:18:26 +000062 fClipMaskManager.setContext(context);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000063 }
64 GrContext* getContext() { return fContext; }
65 const GrContext* getContext() const { return fContext; }
66
reed@google.comac10a2d2010-12-22 21:39:39 +000067 /**
68 * The GrGpu object normally assumes that no outsider is setting state
69 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000070 * the GrGpu that the state was modified and it shouldn't make assumptions
71 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000072 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000073 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 void unimpl(const char[]);
76
77 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000078 * Creates a texture object. If desc width or height is not a power of
79 * two but underlying API requires a power of two texture then srcData
80 * will be embedded in a power of two texture. The extra width and height
81 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000082 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000084 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
85 * on the render target until its releaseRenderTarget() is called or it is
86 * destroyed.
87 *
reed@google.comac10a2d2010-12-22 21:39:39 +000088 * @param desc describes the texture to be created.
89 * @param srcData texel data to load texture. Begins with full-size
90 * palette data for paletted textures. Contains width*
91 * height texels. If NULL texture data is uninitialized.
92 *
93 * @return The texture object if successful, otherwise NULL.
94 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +000095 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000096 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.come269f212011-11-07 13:29:52 +000098 /**
99 * Implements GrContext::createPlatformTexture
100 */
101 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
102
103 /**
104 * Implements GrContext::createPlatformTexture
105 */
106 GrRenderTarget* createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc);
107
108 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 * Creates a vertex buffer.
110 *
111 * @param size size in bytes of the vertex buffer
112 * @param dynamic hints whether the data will be frequently changed
113 * by either GrVertexBuffer::lock or
114 * GrVertexBuffer::updateData.
115 *
116 * @return The vertex buffer if successful, otherwise NULL.
117 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000118 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
120 /**
121 * Creates an index buffer.
122 *
123 * @param size size in bytes of the index buffer
124 * @param dynamic hints whether the data will be frequently changed
125 * by either GrIndexBuffer::lock or
126 * GrIndexBuffer::updateData.
127 *
128 * @return The index buffer if successful, otherwise NULL.
129 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000130 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
132 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000134 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
135 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000136 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000137 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000138 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000139 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140
141 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000143 * (1,1), (0,1)].
144 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000145 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000146 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000147
148 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000149 * Resolves MSAA.
150 */
151 void resolveRenderTarget(GrRenderTarget* target);
152
153 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000154 * Ensures that the current render target is actually set in the
155 * underlying 3D API. Used when client wants to use 3D API to directly
156 * render to the RT.
157 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000160 /**
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000161 * If this returns true then a sequence that reads unpremultiplied pixels
162 * from a surface, writes back the same values, and reads them again will
163 * give the same pixel values back in both reads.
164 */
165 virtual bool canPreserveReadWriteUnpremulPixels() = 0;
166
167 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000168 * readPixels with some configs may be slow. Given a desired config this
169 * function returns a fast-path config. The returned config must have the
170 * same components, component sizes, and not require conversion between
171 * pre- and unpremultiplied alpha. The caller is free to ignore the result
172 * and call readPixels with the original config.
173 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000174 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
175 const {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000176 return config;
177 }
178
179 /**
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000180 * Same as above but applies to writeTexturePixels
181 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000182 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
183 const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000184 return config;
185 }
186
187 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000188 * OpenGL's readPixels returns the result bottom-to-top while the skia
189 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
190 * solution is to have the subclass do the flip using either the CPU or GPU.
191 * However, the caller (GrContext) may have transformations to apply and can
192 * simply fold in the y-flip for free. On the other hand, the subclass may
193 * be able to do it for free itself. For example, the subclass may have to
194 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
195 * concurrently.
196 *
197 * This function returns true if a y-flip is required to put the pixels in
198 * top-to-bottom order and the subclass cannot do it for free.
199 *
200 * See read pixels for the params
201 * @return true if calling readPixels with the same set of params will
202 * produce bottom-to-top data
203 */
204 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
205 int left, int top,
206 int width, int height,
207 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000208 size_t rowBytes) const = 0;
209 /**
210 * This should return true if reading a NxM rectangle of pixels from a
211 * render target is faster if the target has dimensons N and M and the read
212 * rectangle has its top-left at 0,0.
213 */
214 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000215
216 /**
217 * Reads a rectangle of pixels from a render target. Fails if read requires
218 * conversion between premultiplied and unpremultiplied configs. The caller
219 * should do the conversion by rendering to a target with the desire config
220 * first.
221 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000222 * @param renderTarget the render target to read from. NULL means the
223 * current render target.
224 * @param left left edge of the rectangle to read (inclusive)
225 * @param top top edge of the rectangle to read (inclusive)
226 * @param width width of rectangle to read in pixels.
227 * @param height height of rectangle to read in pixels.
228 * @param config the pixel config of the destination buffer
229 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000230 * @param rowBytes the number of bytes between consecutive rows. Zero
231 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000232 * @param invertY buffer should be populated bottom-to-top as opposed
233 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234 *
235 * @return true if the read succeeded, false if not. The read can fail
236 * because of a unsupported pixel config or because no render
237 * target is currently set.
238 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000239 bool readPixels(GrRenderTarget* renderTarget,
240 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000241 GrPixelConfig config, void* buffer, size_t rowBytes,
242 bool invertY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
bsalomon@google.com6f379512011-11-16 20:36:03 +0000244 /**
245 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000246 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000247 * @param left left edge of the rectangle to write (inclusive)
248 * @param top top edge of the rectangle to write (inclusive)
249 * @param width width of rectangle to write in pixels.
250 * @param height height of rectangle to write in pixels.
251 * @param config the pixel config of the source buffer
252 * @param buffer memory to read pixels from
253 * @param rowBytes number of bytes bewtween consecutive rows. Zero
254 * means rows are tightly packed.
255 */
256 void writeTexturePixels(GrTexture* texture,
257 int left, int top, int width, int height,
258 GrPixelConfig config, const void* buffer,
259 size_t rowBytes);
260
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000261 /**
262 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000263 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000264 */
junov@google.com53a55842011-06-08 22:55:10 +0000265 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000266
267 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000268 * Called to tell Gpu object to release all GrResources. Overrides must call
269 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000270 */
271 void releaseResources();
272
273 /**
274 * Add resource to list of resources. Should only be called by GrResource.
275 * @param resource the resource to add.
276 */
277 void insertResource(GrResource* resource);
278
279 /**
280 * Remove resource from list of resources. Should only be called by
281 * GrResource.
282 * @param resource the resource to remove.
283 */
284 void removeResource(GrResource* resource);
285
bsalomon@google.com471d4712011-08-23 15:45:25 +0000286 // GrDrawTarget overrides
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000287 virtual void clear(const GrIRect* rect, GrColor color) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000288
robertphillips@google.comff175842012-05-14 19:31:39 +0000289 virtual void purgeResources() SK_OVERRIDE {
290 // The clip mask manager can rebuild all its clip masks so just
291 // get rid of them all.
292 fClipMaskManager.releaseResources();
293 }
294
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000295 // After the client interacts directly with the 3D context state the GrGpu
296 // must resync its internal state and assumptions about 3D context state.
297 // Each time this occurs the GrGpu bumps a timestamp.
298 // state of the 3D context
299 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
300 // a billion years.
301 typedef uint64_t ResetTimestamp;
302
303 // This timestamp is always older than the current timestamp
304 static const ResetTimestamp kExpiredTimestamp = 0;
305 // Returns a timestamp based on the number of times the context was reset.
306 // This timestamp can be used to lazily detect when cached 3D context state
307 // is dirty.
308 ResetTimestamp getResetTimestamp() const {
309 return fResetTimestamp;
310 }
311
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000312 /**
313 * Can the provided configuration act as a color render target?
314 */
315 bool isConfigRenderable(GrPixelConfig config) const {
316 GrAssert(kGrPixelConfigCount > config);
317 return fConfigRenderSupport[config];
318 }
319
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000320 virtual void enableScissoring(const GrIRect& rect) = 0;
321 virtual void disableScissor() = 0;
322
323 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
324 // free to clear the remaining bits to zero if masked clears are more
325 // expensive than clearing all bits.
326 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
327
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000328 enum PrivateDrawStateStateBits {
329 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330
331 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
332 // stencil bits used for
333 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 };
335
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000336 virtual void postClipPush() SK_OVERRIDE {
337 fClipMaskManager.postClipPush();
338 }
339 virtual void preClipPop() SK_OVERRIDE {
340 fClipMaskManager.preClipPop();
341 }
342
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000343protected:
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000344 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000345 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347 // Functions used to map clip-respecting stencil tests into normal
348 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000349 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 GrStencilFunc func);
351 static void ConvertStencilFuncAndMask(GrStencilFunc func,
352 bool clipInStencil,
353 unsigned int clipBit,
354 unsigned int userBits,
355 unsigned int* ref,
356 unsigned int* mask);
357
358 // stencil settings to clip drawing when stencil clipping is in effect
359 // and the client isn't using the stencil test.
digit@google.com9b482c42012-02-16 22:03:26 +0000360 static const GrStencilSettings* GetClipStencilSettings();
reed@google.comac10a2d2010-12-22 21:39:39 +0000361
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000362 GrClipMaskManager fClipMaskManager;
363
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000364 struct GeometryPoolState {
365 const GrVertexBuffer* fPoolVertexBuffer;
366 int fPoolStartVertex;
367
368 const GrIndexBuffer* fPoolIndexBuffer;
369 int fPoolStartIndex;
370 };
371 const GeometryPoolState& getGeomPoolState() {
372 return fGeomPoolStateStack.back();
373 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000374
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000375 // Derived classes need access to this so they can fill it out in their
376 // constructors
377 bool fConfigRenderSupport[kGrPixelConfigCount];
378
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000379 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
381 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000382 void** vertices) SK_OVERRIDE;
383 virtual bool onReserveIndexSpace(int indexCount,
384 void** indices) SK_OVERRIDE;
385 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
386 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000387 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000388 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000389 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000390 int indexCount) SK_OVERRIDE;
391 virtual void releaseVertexArray() SK_OVERRIDE;
392 virtual void releaseIndexArray() SK_OVERRIDE;
393 virtual void geometrySourceWillPush() SK_OVERRIDE;
394 virtual void geometrySourceWillPop(
395 const GeometrySrcState& restoredState) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000396
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397 // Helpers for setting up geometry state
398 void finalizeReservedVertices();
399 void finalizeReservedIndices();
400
bsalomon@google.comb635d392011-11-05 12:47:43 +0000401 // called when the 3D context state is unknown. Subclass should emit any
402 // assumed 3D context state and dirty any state cache
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000403 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000404
bsalomon@google.comb635d392011-11-05 12:47:43 +0000405
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000406 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000407 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000408 const void* srcData,
409 size_t rowBytes) = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000410 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) = 0;
411 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000412 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
413 bool dynamic) = 0;
414 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
415 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000416
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000417 // overridden by API-specific derivated class to perform the clear and
418 // clearRect. NULL rect means clear whole target.
419 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000420
421 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000422 virtual void onGpuDrawIndexed(GrPrimitiveType type,
423 uint32_t startVertex,
424 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000425 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000426 uint32_t indexCount) = 0;
427
428 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
429 uint32_t vertexCount,
430 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000431
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000432 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000433 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000434
435 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000436 virtual bool onReadPixels(GrRenderTarget* target,
437 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000438 GrPixelConfig,
439 void* buffer,
440 size_t rowBytes,
441 bool invertY) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442
bsalomon@google.com6f379512011-11-16 20:36:03 +0000443 // overridden by API-specific derived class to perform the texture update
444 virtual void onWriteTexturePixels(GrTexture* texture,
445 int left, int top, int width, int height,
446 GrPixelConfig config, const void* buffer,
447 size_t rowBytes) = 0;
448
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000449 // overridden by API-specific derived class to perform the resolve
450 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
451
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000453 // indexed geometry. The subclass may adjust the startVertex and/or
454 // startIndex since it may have already accounted for these in the setup.
455 virtual void setupGeometry(int* startVertex,
456 int* startIndex,
457 int vertexCount,
458 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000460 // width and height may be larger than rt (if underlying API allows it).
461 // Should attach the SB to the RT. Returns false if compatible sb could
462 // not be created.
463 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
464 int width,
465 int height) = 0;
466
467 // attaches an existing SB to an existing RT.
468 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
469 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
471 // The GrGpu typically records the clients requested state and then flushes
472 // deltas from previous state at draw time. This function does the
473 // API-specific flush of the state
474 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000475 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000477 // clears the entire stencil buffer to 0
478 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
reed@google.comac10a2d2010-12-22 21:39:39 +0000480private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000481 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000482
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000483 ResetTimestamp fResetTimestamp;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000484
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000485 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000487 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000488
489 // counts number of uses of vertex/index pool in the geometry stack
490 int fVertexPoolUseCnt;
491 int fIndexPoolUseCnt;
492
493 enum {
494 kPreallocGeomPoolStateStackCnt = 4,
495 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000496 SkSTArray<kPreallocGeomPoolStateStackCnt,
497 GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000498
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000499 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
500 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000502 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
503 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000504
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000505 bool fContextIsDirty;
506
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000507 GrResource* fResourceHead;
508
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000509 // Given a rt, find or create a stencil buffer and attach it
510 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
511
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000512 // GrDrawTarget overrides
513 virtual void onDrawIndexed(GrPrimitiveType type,
514 int startVertex,
515 int startIndex,
516 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000517 int indexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518 virtual void onDrawNonIndexed(GrPrimitiveType type,
519 int startVertex,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000520 int vertexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000521
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000522 // readies the pools to provide vertex/index data.
523 void prepareVertexPool();
524 void prepareIndexPool();
525
bsalomon@google.comb635d392011-11-05 12:47:43 +0000526 void resetContext() {
527 this->onResetContext();
528 ++fResetTimestamp;
529 }
530
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000531 void handleDirtyContext() {
532 if (fContextIsDirty) {
533 this->resetContext();
534 fContextIsDirty = false;
535 }
536 }
537
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000538 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000539};
540
541#endif