blob: de14935e7f0141abdd48fe540e83a75e1b4eef38 [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
bsalomon@google.com05ef5102011-05-02 21:14:59 +000027/**
28 * Gpu usage statistics.
29 */
30struct GrGpuStats {
31 uint32_t fVertexCnt; //<! Number of vertices drawn
32 uint32_t fIndexCnt; //<! Number of indices drawn
33 uint32_t fDrawCnt; //<! Number of draws
34
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000035 uint32_t fProgChngCnt;//<! Number of program changes
bsalomon@google.com05ef5102011-05-02 21:14:59 +000036
bsalomon@google.com030302c2011-08-01 17:35:01 +000037 /**
38 * Number of times the texture is set in 3D API
39 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000040 uint32_t fTextureChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000041 /**
42 * Number of times the render target is set in 3D API
43 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000044 uint32_t fRenderTargetChngCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000045 /**
46 * Number of textures created (includes textures that are rendertargets).
47 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000048 uint32_t fTextureCreateCnt;
bsalomon@google.com030302c2011-08-01 17:35:01 +000049 /**
50 * Number of rendertargets created.
51 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000052 uint32_t fRenderTargetCreateCnt;
53};
54
reed@google.comac10a2d2010-12-22 21:39:39 +000055class GrGpu : public GrDrawTarget {
56
57public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000058
reed@google.comac10a2d2010-12-22 21:39:39 +000059 /**
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060 * Additional blend coeffecients for dual source blending, not exposed
61 * through GrPaint/GrContext.
62 */
63 enum ExtendedBlendCoeffs {
64 // source 2 refers to second output color when
65 // using dual source blending.
66 kS2C_BlendCoeff = kPublicBlendCoeffCount,
67 kIS2C_BlendCoeff,
68 kS2A_BlendCoeff,
69 kIS2A_BlendCoeff,
70
71 kTotalBlendCoeffCount
72 };
73
74 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000075 * Create an instance of GrGpu that matches the specified Engine backend.
76 * If the requested engine is not supported (at compile-time or run-time)
77 * this returns NULL.
78 */
bsalomon@google.com05ef5102011-05-02 21:14:59 +000079 static GrGpu* Create(GrEngine, GrPlatform3DContext context3D);
reed@google.comac10a2d2010-12-22 21:39:39 +000080
81 ////////////////////////////////////////////////////////////////////////////
82
83 GrGpu();
84 virtual ~GrGpu();
85
bsalomon@google.com669fdc42011-04-05 17:08:27 +000086 // The GrContext sets itself as the owner of this Gpu object
87 void setContext(GrContext* context) {
88 GrAssert(NULL == fContext);
89 fContext = context;
90 }
91 GrContext* getContext() { return fContext; }
92 const GrContext* getContext() const { return fContext; }
93
reed@google.comac10a2d2010-12-22 21:39:39 +000094 /**
95 * The GrGpu object normally assumes that no outsider is setting state
96 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000097 * the GrGpu that the state was modified and it shouldn't make assumptions
98 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000099 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000100 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
102 void unimpl(const char[]);
103
104 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000105 * Creates a texture object. If desc width or height is not a power of
106 * two but underlying API requires a power of two texture then srcData
107 * will be embedded in a power of two texture. The extra width and height
108 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000111 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
112 * on the render target until its releaseRenderTarget() is called or it is
113 * destroyed.
114 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 * @param desc describes the texture to be created.
116 * @param srcData texel data to load texture. Begins with full-size
117 * palette data for paletted textures. Contains width*
118 * height texels. If NULL texture data is uninitialized.
119 *
120 * @return The texture object if successful, otherwise NULL.
121 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000122 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000123 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
bsalomon@google.come269f212011-11-07 13:29:52 +0000125 /**
126 * Implements GrContext::createPlatformTexture
127 */
128 GrTexture* createPlatformTexture(const GrPlatformTextureDesc& desc);
129
130 /**
131 * Implements GrContext::createPlatformTexture
132 */
133 GrRenderTarget* createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc);
134
135 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 * Creates a vertex buffer.
137 *
138 * @param size size in bytes of the vertex buffer
139 * @param dynamic hints whether the data will be frequently changed
140 * by either GrVertexBuffer::lock or
141 * GrVertexBuffer::updateData.
142 *
143 * @return The vertex buffer if successful, otherwise NULL.
144 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000145 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000146
147 /**
148 * Creates an index buffer.
149 *
150 * @param size size in bytes of the index buffer
151 * @param dynamic hints whether the data will be frequently changed
152 * by either GrIndexBuffer::lock or
153 * GrIndexBuffer::updateData.
154 *
155 * @return The index buffer if successful, otherwise NULL.
156 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000157 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
159 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000161 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
162 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
reed@google.comac10a2d2010-12-22 21:39:39 +0000163 * Draw with kTriangles_PrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000164 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000166 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
168 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000169 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000170 * (1,1), (0,1)].
171 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000172 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000173 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000174
175 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000176 * Resolves MSAA.
177 */
178 void resolveRenderTarget(GrRenderTarget* target);
179
180 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000181 * Ensures that the current render target is actually set in the
182 * underlying 3D API. Used when client wants to use 3D API to directly
183 * render to the RT.
184 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000185 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000186
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000187 /**
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000188 * If this returns true then a sequence that reads unpremultiplied pixels
189 * from a surface, writes back the same values, and reads them again will
190 * give the same pixel values back in both reads.
191 */
192 virtual bool canPreserveReadWriteUnpremulPixels() = 0;
193
194 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000195 * readPixels with some configs may be slow. Given a desired config this
196 * function returns a fast-path config. The returned config must have the
197 * same components, component sizes, and not require conversion between
198 * pre- and unpremultiplied alpha. The caller is free to ignore the result
199 * and call readPixels with the original config.
200 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000201 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
202 const {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000203 return config;
204 }
205
206 /**
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000207 * Same as above but applies to writeTexturePixels
208 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000209 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
210 const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000211 return config;
212 }
213
214 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000215 * OpenGL's readPixels returns the result bottom-to-top while the skia
216 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
217 * solution is to have the subclass do the flip using either the CPU or GPU.
218 * However, the caller (GrContext) may have transformations to apply and can
219 * simply fold in the y-flip for free. On the other hand, the subclass may
220 * be able to do it for free itself. For example, the subclass may have to
221 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
222 * concurrently.
223 *
224 * This function returns true if a y-flip is required to put the pixels in
225 * top-to-bottom order and the subclass cannot do it for free.
226 *
227 * See read pixels for the params
228 * @return true if calling readPixels with the same set of params will
229 * produce bottom-to-top data
230 */
231 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
232 int left, int top,
233 int width, int height,
234 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000235 size_t rowBytes) const = 0;
236 /**
237 * This should return true if reading a NxM rectangle of pixels from a
238 * render target is faster if the target has dimensons N and M and the read
239 * rectangle has its top-left at 0,0.
240 */
241 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000242
243 /**
244 * Reads a rectangle of pixels from a render target. Fails if read requires
245 * conversion between premultiplied and unpremultiplied configs. The caller
246 * should do the conversion by rendering to a target with the desire config
247 * first.
248 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000249 * @param renderTarget the render target to read from. NULL means the
250 * current render target.
251 * @param left left edge of the rectangle to read (inclusive)
252 * @param top top edge of the rectangle to read (inclusive)
253 * @param width width of rectangle to read in pixels.
254 * @param height height of rectangle to read in pixels.
255 * @param config the pixel config of the destination buffer
256 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000257 * @param rowBytes the number of bytes between consecutive rows. Zero
258 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000259 * @param invertY buffer should be populated bottom-to-top as opposed
260 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000261 *
262 * @return true if the read succeeded, false if not. The read can fail
263 * because of a unsupported pixel config or because no render
264 * target is currently set.
265 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000266 bool readPixels(GrRenderTarget* renderTarget,
267 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000268 GrPixelConfig config, void* buffer, size_t rowBytes,
269 bool invertY);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
bsalomon@google.com6f379512011-11-16 20:36:03 +0000271 /**
272 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000273 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000274 * @param left left edge of the rectangle to write (inclusive)
275 * @param top top edge of the rectangle to write (inclusive)
276 * @param width width of rectangle to write in pixels.
277 * @param height height of rectangle to write in pixels.
278 * @param config the pixel config of the source buffer
279 * @param buffer memory to read pixels from
280 * @param rowBytes number of bytes bewtween consecutive rows. Zero
281 * means rows are tightly packed.
282 */
283 void writeTexturePixels(GrTexture* texture,
284 int left, int top, int width, int height,
285 GrPixelConfig config, const void* buffer,
286 size_t rowBytes);
287
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000288 const GrGpuStats& getStats() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 void resetStats();
290 void printStats() const;
291
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000292 /**
293 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000294 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000295 */
junov@google.com53a55842011-06-08 22:55:10 +0000296 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000297
298 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000299 * Called to tell Gpu object to release all GrResources. Overrides must call
300 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000301 */
302 void releaseResources();
303
304 /**
305 * Add resource to list of resources. Should only be called by GrResource.
306 * @param resource the resource to add.
307 */
308 void insertResource(GrResource* resource);
309
310 /**
311 * Remove resource from list of resources. Should only be called by
312 * GrResource.
313 * @param resource the resource to remove.
314 */
315 void removeResource(GrResource* resource);
316
bsalomon@google.com471d4712011-08-23 15:45:25 +0000317 // GrDrawTarget overrides
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000318 virtual void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000319
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000320 // After the client interacts directly with the 3D context state the GrGpu
321 // must resync its internal state and assumptions about 3D context state.
322 // Each time this occurs the GrGpu bumps a timestamp.
323 // state of the 3D context
324 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
325 // a billion years.
326 typedef uint64_t ResetTimestamp;
327
328 // This timestamp is always older than the current timestamp
329 static const ResetTimestamp kExpiredTimestamp = 0;
330 // Returns a timestamp based on the number of times the context was reset.
331 // This timestamp can be used to lazily detect when cached 3D context state
332 // is dirty.
333 ResetTimestamp getResetTimestamp() const {
334 return fResetTimestamp;
335 }
336
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000337 /**
338 * Can the provided configuration act as a color render target?
339 */
340 bool isConfigRenderable(GrPixelConfig config) const {
341 GrAssert(kGrPixelConfigCount > config);
342 return fConfigRenderSupport[config];
343 }
344
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000345 virtual void enableScissoring(const GrIRect& rect) = 0;
346 virtual void disableScissor() = 0;
347
348 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
349 // free to clear the remaining bits to zero if masked clears are more
350 // expensive than clearing all bits.
351 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
352
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000353 enum PrivateDrawStateStateBits {
354 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355
356 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
357 // stencil bits used for
358 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 };
360
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000361protected:
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000362 // prepares clip flushes gpu state before a draw
bsalomon@google.comffca4002011-02-22 20:34:01 +0000363 bool setupClipAndFlushState(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 // Functions used to map clip-respecting stencil tests into normal
366 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000367 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000368 GrStencilFunc func);
369 static void ConvertStencilFuncAndMask(GrStencilFunc func,
370 bool clipInStencil,
371 unsigned int clipBit,
372 unsigned int userBits,
373 unsigned int* ref,
374 unsigned int* mask);
375
376 // stencil settings to clip drawing when stencil clipping is in effect
377 // and the client isn't using the stencil test.
digit@google.com9b482c42012-02-16 22:03:26 +0000378 static const GrStencilSettings* GetClipStencilSettings();
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000380 GrGpuStats fStats;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000381
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000382 GrClipMaskManager fClipMaskManager;
383
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000384 struct GeometryPoolState {
385 const GrVertexBuffer* fPoolVertexBuffer;
386 int fPoolStartVertex;
387
388 const GrIndexBuffer* fPoolIndexBuffer;
389 int fPoolStartIndex;
390 };
391 const GeometryPoolState& getGeomPoolState() {
392 return fGeomPoolStateStack.back();
393 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000394
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000395 // Derived classes need access to this so they can fill it out in their
396 // constructors
397 bool fConfigRenderSupport[kGrPixelConfigCount];
398
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000399 // GrDrawTarget overrides
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000400 virtual bool onReserveVertexSpace(GrVertexLayout vertexLayout,
401 int vertexCount,
402 void** vertices);
403 virtual bool onReserveIndexSpace(int indexCount, void** indices);
404 virtual void releaseReservedVertexSpace();
405 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000406 virtual void onSetVertexSourceToArray(const void* vertexArray,
407 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000408 virtual void onSetIndexSourceToArray(const void* indexArray,
409 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000410 virtual void releaseVertexArray();
411 virtual void releaseIndexArray();
412 virtual void geometrySourceWillPush();
413 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
414
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000415 // Helpers for setting up geometry state
416 void finalizeReservedVertices();
417 void finalizeReservedIndices();
418
bsalomon@google.comb635d392011-11-05 12:47:43 +0000419 // called when the 3D context state is unknown. Subclass should emit any
420 // assumed 3D context state and dirty any state cache
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000421 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000422
bsalomon@google.comb635d392011-11-05 12:47:43 +0000423
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000424 // overridden by API-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000425 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000426 const void* srcData,
427 size_t rowBytes) = 0;
bsalomon@google.come269f212011-11-07 13:29:52 +0000428 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) = 0;
429 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) = 0;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000430 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
431 bool dynamic) = 0;
432 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
433 bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000434
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000435 // overridden by API-specific derivated class to perform the clear and
436 // clearRect. NULL rect means clear whole target.
437 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438
439 // overridden by API-specific derived class to perform the draw call.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 virtual void onGpuDrawIndexed(GrPrimitiveType type,
441 uint32_t startVertex,
442 uint32_t startIndex,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000443 uint32_t vertexCount,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 uint32_t indexCount) = 0;
445
446 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
447 uint32_t vertexCount,
448 uint32_t numVertices) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000449
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450 // overridden by API-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000451 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452
453 // overridden by API-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000454 virtual bool onReadPixels(GrRenderTarget* target,
455 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000456 GrPixelConfig,
457 void* buffer,
458 size_t rowBytes,
459 bool invertY) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000460
bsalomon@google.com6f379512011-11-16 20:36:03 +0000461 // overridden by API-specific derived class to perform the texture update
462 virtual void onWriteTexturePixels(GrTexture* texture,
463 int left, int top, int width, int height,
464 GrPixelConfig config, const void* buffer,
465 size_t rowBytes) = 0;
466
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000467 // overridden by API-specific derived class to perform the resolve
468 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // called to program the vertex data, indexCount will be 0 if drawing non-
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000471 // indexed geometry. The subclass may adjust the startVertex and/or
472 // startIndex since it may have already accounted for these in the setup.
473 virtual void setupGeometry(int* startVertex,
474 int* startIndex,
475 int vertexCount,
476 int indexCount) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000478 // width and height may be larger than rt (if underlying API allows it).
479 // Should attach the SB to the RT. Returns false if compatible sb could
480 // not be created.
481 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
482 int width,
483 int height) = 0;
484
485 // attaches an existing SB to an existing RT.
486 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
487 GrRenderTarget* rt) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488
489 // The GrGpu typically records the clients requested state and then flushes
490 // deltas from previous state at draw time. This function does the
491 // API-specific flush of the state
492 // returns false if current state is unsupported.
bsalomon@google.comffca4002011-02-22 20:34:01 +0000493 virtual bool flushGraphicsState(GrPrimitiveType type) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
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.com1bf1c212011-11-05 12:18:58 +0000500
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000501 ResetTimestamp fResetTimestamp;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000502
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000503 GrVertexBufferAllocPool* fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000505 GrIndexBufferAllocPool* fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000506
507 // counts number of uses of vertex/index pool in the geometry stack
508 int fVertexPoolUseCnt;
509 int fIndexPoolUseCnt;
510
511 enum {
512 kPreallocGeomPoolStateStackCnt = 4,
513 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000514 SkSTArray<kPreallocGeomPoolStateStackCnt,
515 GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000517 mutable GrIndexBuffer* fQuadIndexBuffer; // mutable so it can be
518 // created on-demand
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000520 mutable GrVertexBuffer* fUnitSquareVertexBuffer; // mutable so it can be
521 // created on-demand
bsalomon@google.comd302f142011-03-03 13:54:13 +0000522
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000523 bool fContextIsDirty;
524
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000525 GrResource* fResourceHead;
526
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000527 // Given a rt, find or create a stencil buffer and attach it
528 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
529
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000530 // GrDrawTarget overrides
531 virtual void onDrawIndexed(GrPrimitiveType type,
532 int startVertex,
533 int startIndex,
534 int vertexCount,
535 int indexCount);
536 virtual void onDrawNonIndexed(GrPrimitiveType type,
537 int startVertex,
538 int vertexCount);
539
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000540 // readies the pools to provide vertex/index data.
541 void prepareVertexPool();
542 void prepareIndexPool();
543
bsalomon@google.comb635d392011-11-05 12:47:43 +0000544 void resetContext() {
545 this->onResetContext();
546 ++fResetTimestamp;
547 }
548
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000549 void handleDirtyContext() {
550 if (fContextIsDirty) {
551 this->resetContext();
552 fContextIsDirty = false;
553 }
554 }
555
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000556 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557};
558
559#endif