blob: 6741aec513704887ffdef767829d754c78f9eee9 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000054class GrGpu : public GrDrawTarget {
55
56public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000057
reed@google.comac10a2d2010-12-22 21:39:39 +000058 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +000059 * Additional blend coeffecients for dual source blending, not exposed
60 * through GrPaint/GrContext.
61 */
62 enum ExtendedBlendCoeffs {
63 // source 2 refers to second output color when
64 // using dual source blending.
65 kS2C_BlendCoeff = kPublicBlendCoeffCount,
66 kIS2C_BlendCoeff,
67 kS2A_BlendCoeff,
68 kIS2A_BlendCoeff,
69
70 kTotalBlendCoeffCount
71 };
72
73 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000074 * Create an instance of GrGpu that matches the specified Engine backend.
75 * If the requested engine is not supported (at compile-time or run-time)
76 * this returns NULL.
77 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000078 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000079
80 ////////////////////////////////////////////////////////////////////////////
81
82 GrGpu();
83 virtual ~GrGpu();
84
bsalomon@google.com669fdc42011-04-05 17:08:27 +000085 // The GrContext sets itself as the owner of this Gpu object
86 void setContext(GrContext* context) {
87 GrAssert(NULL == fContext);
88 fContext = context;
89 }
90 GrContext* getContext() { return fContext; }
91 const GrContext* getContext() const { return fContext; }
92
reed@google.comac10a2d2010-12-22 21:39:39 +000093 /**
94 * The GrGpu object normally assumes that no outsider is setting state
95 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000096 * the GrGpu that the state was modified and it shouldn't make assumptions
97 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000098 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000099 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000100
101 void unimpl(const char[]);
102
103 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000104 * Creates a texture object. If desc width or height is not a power of
105 * two but underlying API requires a power of two texture then srcData
106 * will be embedded in a power of two texture. The extra width and height
107 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000110 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
111 * on the render target until its releaseRenderTarget() is called or it is
112 * destroyed.
113 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 * @param desc describes the texture to be created.
115 * @param srcData texel data to load texture. Begins with full-size
116 * palette data for paletted textures. Contains width*
117 * height texels. If NULL texture data is uninitialized.
118 *
119 * @return The texture object if successful, otherwise NULL.
120 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000121 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000122 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
bsalomon@google.come269f212011-11-07 13:29:52 +0000124 /**
125 * Implements GrContext::createPlatformTexture
126 */
127 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
128
129 /**
130 * Implements GrContext::createPlatformTexture
131 */
132 GrRenderTarget* createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc);
133
134 /**
135 * DEPRECATED. This will be removed.
136 */
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000137 GrResource* createPlatformSurface(const GrPlatformSurfaceDesc& desc);
138
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 /**
140 * Creates a vertex buffer.
141 *
142 * @param size size in bytes of the vertex buffer
143 * @param dynamic hints whether the data will be frequently changed
144 * by either GrVertexBuffer::lock or
145 * GrVertexBuffer::updateData.
146 *
147 * @return The vertex buffer if successful, otherwise NULL.
148 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000149 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000150
151 /**
152 * Creates an index buffer.
153 *
154 * @param size size in bytes of the index buffer
155 * @param dynamic hints whether the data will be frequently changed
156 * by either GrIndexBuffer::lock or
157 * GrIndexBuffer::updateData.
158 *
159 * @return The index buffer if successful, otherwise NULL.
160 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
163 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000165 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
166 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000168 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000169 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000170 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
172 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000173 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000174 * (1,1), (0,1)].
175 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000176 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000177 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000178
179 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 * Ensures that the current render target is actually set in the
181 * underlying 3D API. Used when client wants to use 3D API to directly
182 * render to the RT.
183 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000184 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000186 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000187 * readPixels with some configs may be slow. Given a desired config this
188 * function returns a fast-path config. The returned config must have the
189 * same components, component sizes, and not require conversion between
190 * pre- and unpremultiplied alpha. The caller is free to ignore the result
191 * and call readPixels with the original config.
192 */
193 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config) {
194 return config;
195 }
196
197 /**
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000198 * Same as above but applies to writeTexturePixels
199 */
200 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config) {
201 return config;
202 }
203
204 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000205 * OpenGL's readPixels returns the result bottom-to-top while the skia
206 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
207 * solution is to have the subclass do the flip using either the CPU or GPU.
208 * However, the caller (GrContext) may have transformations to apply and can
209 * simply fold in the y-flip for free. On the other hand, the subclass may
210 * be able to do it for free itself. For example, the subclass may have to
211 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
212 * concurrently.
213 *
214 * This function returns true if a y-flip is required to put the pixels in
215 * top-to-bottom order and the subclass cannot do it for free.
216 *
217 * See read pixels for the params
218 * @return true if calling readPixels with the same set of params will
219 * produce bottom-to-top data
220 */
221 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
222 int left, int top,
223 int width, int height,
224 GrPixelConfig config,
225 size_t rowBytes) = 0;
226
227 /**
228 * Reads a rectangle of pixels from a render target. Fails if read requires
229 * conversion between premultiplied and unpremultiplied configs. The caller
230 * should do the conversion by rendering to a target with the desire config
231 * first.
232 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000233 * @param renderTarget the render target to read from. NULL means the
234 * current render target.
235 * @param left left edge of the rectangle to read (inclusive)
236 * @param top top edge of the rectangle to read (inclusive)
237 * @param width width of rectangle to read in pixels.
238 * @param height height of rectangle to read in pixels.
239 * @param config the pixel config of the destination buffer
240 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000241 * @param rowBytes the number of bytes between consecutive rows. Zero
242 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000243 * @param invertY buffer should be populated bottom-to-top as opposed
244 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000245 *
246 * @return true if the read succeeded, false if not. The read can fail
247 * because of a unsupported pixel config or because no render
248 * target is currently set.
249 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000250 bool readPixels(GrRenderTarget* renderTarget,
251 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000252 GrPixelConfig config, void* buffer, size_t rowBytes,
253 bool invertY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
bsalomon@google.com6f379512011-11-16 20:36:03 +0000255 /**
256 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000257 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000258 * @param left left edge of the rectangle to write (inclusive)
259 * @param top top edge of the rectangle to write (inclusive)
260 * @param width width of rectangle to write in pixels.
261 * @param height height of rectangle to write in pixels.
262 * @param config the pixel config of the source buffer
263 * @param buffer memory to read pixels from
264 * @param rowBytes number of bytes bewtween consecutive rows. Zero
265 * means rows are tightly packed.
266 */
267 void writeTexturePixels(GrTexture* texture,
268 int left, int top, int width, int height,
269 GrPixelConfig config, const void* buffer,
270 size_t rowBytes);
271
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000272 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 void resetStats();
274 void printStats() const;
275
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000276 /**
277 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000278 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000279 */
junov@google.com53a55842011-06-08 22:55:10 +0000280 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000281
282 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000283 * Called to tell Gpu object to release all GrResources. Overrides must call
284 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000285 */
286 void releaseResources();
287
288 /**
289 * Add resource to list of resources. Should only be called by GrResource.
290 * @param resource the resource to add.
291 */
292 void insertResource(GrResource* resource);
293
294 /**
295 * Remove resource from list of resources. Should only be called by
296 * GrResource.
297 * @param resource the resource to remove.
298 */
299 void removeResource(GrResource* resource);
300
bsalomon@google.com471d4712011-08-23 15:45:25 +0000301 // GrDrawTarget overrides
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000302 virtual void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000303
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000304 // After the client interacts directly with the 3D context state the GrGpu
305 // must resync its internal state and assumptions about 3D context state.
306 // Each time this occurs the GrGpu bumps a timestamp.
307 // state of the 3D context
308 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
309 // a billion years.
310 typedef uint64_t ResetTimestamp;
311
312 // This timestamp is always older than the current timestamp
313 static const ResetTimestamp kExpiredTimestamp = 0;
314 // Returns a timestamp based on the number of times the context was reset.
315 // This timestamp can be used to lazily detect when cached 3D context state
316 // is dirty.
317 ResetTimestamp getResetTimestamp() const {
318 return fResetTimestamp;
319 }
320
reed@google.comac10a2d2010-12-22 21:39:39 +0000321protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000322 enum PrivateStateBits {
323 kFirstBit = (kLastPublicStateBit << 1),
324
325 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
326 // stencil bits used for
327 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 };
329
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000330 // keep track of whether we are using stencil clipping (as opposed to
331 // scissor).
332 bool fClipInStencil;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333
334 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000335 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000336
bsalomon@google.comd302f142011-03-03 13:54:13 +0000337 // Functions used to map clip-respecting stencil tests into normal
338 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000339 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000340 GrStencilFunc func);
341 static void ConvertStencilFuncAndMask(GrStencilFunc func,
342 bool clipInStencil,
343 unsigned int clipBit,
344 unsigned int userBits,
345 unsigned int* ref,
346 unsigned int* mask);
347
348 // stencil settings to clip drawing when stencil clipping is in effect
349 // and the client isn't using the stencil test.
350 static const GrStencilSettings gClipStencilSettings;
351
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000353 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000354
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000355 struct GeometryPoolState {
356 const GrVertexBuffer* fPoolVertexBuffer;
357 int fPoolStartVertex;
358
359 const GrIndexBuffer* fPoolIndexBuffer;
360 int fPoolStartIndex;
361 };
362 const GeometryPoolState& getGeomPoolState() {
363 return fGeomPoolStateStack.back();
364 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365
366 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000367 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
368 int vertexCount,
369 void** vertices);
370 virtual bool onReserveIndexSpace(int indexCount, void** indices);
371 virtual void releaseReservedVertexSpace();
372 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000373 virtual void onSetVertexSourceToArray(const void* vertexArray,
374 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000375 virtual void onSetIndexSourceToArray(const void* indexArray,
376 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000377 virtual void releaseVertexArray();
378 virtual void releaseIndexArray();
379 virtual void geometrySourceWillPush();
380 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
381
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000382 // Helpers for setting up geometry state
383 void finalizeReservedVertices();
384 void finalizeReservedIndices();
385
bsalomon@google.comb635d392011-11-05 12:47:43 +0000386 // called when the 3D context state is unknown. Subclass should emit any
387 // assumed 3D context state and dirty any state cache
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000388 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000389
bsalomon@google.comb635d392011-11-05 12:47:43 +0000390
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000391 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000392 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000393 const void* srcData,
394 size_t rowBytes) = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000395 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) = 0;
396 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) = 0;
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000397 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000398 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
399 bool dynamic) = 0;
400 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
401 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000402
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000403 // overridden by API-specific derivated class to perform the clear and
404 // clearRect. NULL rect means clear whole target.
405 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000406
407 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000408 virtual void onGpuDrawIndexed(GrPrimitiveType type,
409 uint32_t startVertex,
410 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000411 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000412 uint32_t indexCount) = 0;
413
414 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
415 uint32_t vertexCount,
416 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000418 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000419 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000420
421 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000422 virtual bool onReadPixels(GrRenderTarget* target,
423 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000424 GrPixelConfig,
425 void* buffer,
426 size_t rowBytes,
427 bool invertY) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428
bsalomon@google.com6f379512011-11-16 20:36:03 +0000429 // overridden by API-specific derived class to perform the texture update
430 virtual void onWriteTexturePixels(GrTexture* texture,
431 int left, int top, int width, int height,
432 GrPixelConfig config, const void* buffer,
433 size_t rowBytes) = 0;
434
reed@google.comac10a2d2010-12-22 21:39:39 +0000435 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000436 // indexed geometry. The subclass may adjust the startVertex and/or
437 // startIndex since it may have already accounted for these in the setup.
438 virtual void setupGeometry(int* startVertex,
439 int* startIndex,
440 int vertexCount,
441 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000443 // width and height may be larger than rt (if underlying API allows it).
444 // Should attach the SB to the RT. Returns false if compatible sb could
445 // not be created.
446 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
447 int width,
448 int height) = 0;
449
450 // attaches an existing SB to an existing RT.
451 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
452 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000453
454 // The GrGpu typically records the clients requested state and then flushes
455 // deltas from previous state at draw time. This function does the
456 // API-specific flush of the state
457 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000458 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
460 // Sets the scissor rect, or disables if rect is NULL.
461 virtual void flushScissor(const GrIRect* rect) = 0;
462
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000463 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
464 // free to clear the remaining bits to zero if masked clears are more
465 // expensive than clearing all bits.
466 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
467
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000468 // clears the entire stencil buffer to 0
469 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
reed@google.comac10a2d2010-12-22 21:39:39 +0000471private:
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000472 GrContext* fContext; // not reffed (context refs gpu)
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000473
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000474 ResetTimestamp fResetTimestamp;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000475
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000476 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000478 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000479
480 // counts number of uses of vertex/index pool in the geometry stack
481 int fVertexPoolUseCnt;
482 int fIndexPoolUseCnt;
483
484 enum {
485 kPreallocGeomPoolStateStackCnt = 4,
486 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000487 SkSTArray<kPreallocGeomPoolStateStackCnt,
488 GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000490 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
491 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000492
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000493 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
494 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000495
bsalomon@google.com30085192011-08-19 15:42:31 +0000496 // must be instantiated after GrGpu object has been given its owning
497 // GrContext ptr. (GrGpu is constructed first then handed off to GrContext).
498 GrPathRendererChain* fPathRendererChain;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000499
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000500 bool fContextIsDirty;
501
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000502 GrResource* fResourceHead;
503
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000504 // Given a rt, find or create a stencil buffer and attach it
505 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
506
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000507 // GrDrawTarget overrides
508 virtual void onDrawIndexed(GrPrimitiveType type,
509 int startVertex,
510 int startIndex,
511 int vertexCount,
512 int indexCount);
513 virtual void onDrawNonIndexed(GrPrimitiveType type,
514 int startVertex,
515 int vertexCount);
516
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000517 // readies the pools to provide vertex/index data.
518 void prepareVertexPool();
519 void prepareIndexPool();
520
521 // determines the path renderer used to draw a clip path element.
reed@google.com07f3ee12011-05-16 17:21:57 +0000522 GrPathRenderer* getClipPathRenderer(const SkPath& path, GrPathFill fill);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523
bsalomon@google.comb635d392011-11-05 12:47:43 +0000524 void resetContext() {
525 this->onResetContext();
526 ++fResetTimestamp;
527 }
528
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000529 void handleDirtyContext() {
530 if (fContextIsDirty) {
531 this->resetContext();
532 fContextIsDirty = false;
533 }
534 }
535
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000536 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537};
538
539#endif