blob: 38c109defe02cc6a84bea7088459ddf36cf68c86 [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"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000016#include "GrClipMaskManager.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
sugoi@google.com12b4e272012-12-06 20:13:11 +000018#include "SkPath.h"
19
bsalomon@google.com669fdc42011-04-05 17:08:27 +000020class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021class GrIndexBufferAllocPool;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000022class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000023class GrPathRenderer;
24class GrPathRendererChain;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000025class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000026class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000027class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29class GrGpu : public GrDrawTarget {
30
31public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000034 * Additional blend coefficients for dual source blending, not exposed
bsalomon@google.com271cffc2011-05-20 14:13:56 +000035 * through GrPaint/GrContext.
36 */
37 enum ExtendedBlendCoeffs {
38 // source 2 refers to second output color when
39 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000040 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
41 kIS2C_GrBlendCoeff,
42 kS2A_GrBlendCoeff,
43 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000044
bsalomon@google.com47059542012-06-06 20:51:20 +000045 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000046 };
47
48 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000049 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
50 * not supported (at compile-time or run-time) this returns NULL.
reed@google.comac10a2d2010-12-22 21:39:39 +000051 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000052 static GrGpu* Create(GrBackend, GrBackendContext);
reed@google.comac10a2d2010-12-22 21:39:39 +000053
54 ////////////////////////////////////////////////////////////////////////////
55
56 GrGpu();
57 virtual ~GrGpu();
58
bsalomon@google.com669fdc42011-04-05 17:08:27 +000059 // The GrContext sets itself as the owner of this Gpu object
60 void setContext(GrContext* context) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000061 GrAssert(NULL == fContext);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000062 fContext = context;
robertphillips@google.comf105b102012-05-14 12:18:26 +000063 fClipMaskManager.setContext(context);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000064 }
65 GrContext* getContext() { return fContext; }
66 const GrContext* getContext() const { return fContext; }
67
reed@google.comac10a2d2010-12-22 21:39:39 +000068 /**
69 * The GrGpu object normally assumes that no outsider is setting state
70 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000071 * the GrGpu that the state was modified and it shouldn't make assumptions
72 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000073 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000074 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000075
76 void unimpl(const char[]);
77
78 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000079 * Creates a texture object. If desc width or height is not a power of
80 * two but underlying API requires a power of two texture then srcData
81 * will be embedded in a power of two texture. The extra width and height
82 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000083 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000085 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
86 * on the render target until its releaseRenderTarget() is called or it is
87 * destroyed.
88 *
reed@google.comac10a2d2010-12-22 21:39:39 +000089 * @param desc describes the texture to be created.
90 * @param srcData texel data to load texture. Begins with full-size
91 * palette data for paletted textures. Contains width*
92 * height texels. If NULL texture data is uninitialized.
93 *
94 * @return The texture object if successful, otherwise NULL.
95 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +000096 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000097 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000098
bsalomon@google.come269f212011-11-07 13:29:52 +000099 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000100 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000101 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000102 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000103
104 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000105 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000106 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000107 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000108
109 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 * Creates a vertex buffer.
111 *
112 * @param size size in bytes of the vertex buffer
113 * @param dynamic hints whether the data will be frequently changed
114 * by either GrVertexBuffer::lock or
115 * GrVertexBuffer::updateData.
116 *
117 * @return The vertex buffer if successful, otherwise NULL.
118 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000119 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121 /**
122 * Creates an index buffer.
123 *
124 * @param size size in bytes of the index buffer
125 * @param dynamic hints whether the data will be frequently changed
126 * by either GrIndexBuffer::lock or
127 * GrIndexBuffer::updateData.
128 *
129 * @return The index buffer if successful, otherwise NULL.
130 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000131 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
133 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000134 * Creates a path object that can be stenciled using stencilPath(). It is
135 * only legal to call this if the caps report support for path stenciling.
136 */
137 GrPath* createPath(const SkPath& path);
138
139 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000141 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
142 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000143 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000144 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000146 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
148 /**
bsalomon@google.comd302f142011-03-03 13:54:13 +0000149 * Returns a vertex buffer with four position-only vertices [(0,0), (1,0),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000150 * (1,1), (0,1)].
151 * @ return unit square vertex buffer
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000152 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000153 const GrVertexBuffer* getUnitSquareVertexBuffer() const;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000154
155 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000156 * Resolves MSAA.
157 */
158 void resolveRenderTarget(GrRenderTarget* target);
159
160 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 * Ensures that the current render target is actually set in the
162 * underlying 3D API. Used when client wants to use 3D API to directly
163 * render to the RT.
164 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000165 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000167 /**
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
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000170 * same components and component sizes. The caller is free to ignore the
171 * result and call readPixels with the original config.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000172 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000173 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
174 const {
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000175 return config;
176 }
177
178 /**
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000179 * Same as above but applies to writeTexturePixels
180 */
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000181 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
182 const {
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000183 return config;
184 }
185
186 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000187 * OpenGL's readPixels returns the result bottom-to-top while the skia
188 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
189 * solution is to have the subclass do the flip using either the CPU or GPU.
190 * However, the caller (GrContext) may have transformations to apply and can
191 * simply fold in the y-flip for free. On the other hand, the subclass may
192 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000194 * concurrently.
195 *
196 * This function returns true if a y-flip is required to put the pixels in
197 * top-to-bottom order and the subclass cannot do it for free.
198 *
199 * See read pixels for the params
200 * @return true if calling readPixels with the same set of params will
201 * produce bottom-to-top data
202 */
203 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
204 int left, int top,
205 int width, int height,
206 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000207 size_t rowBytes) const = 0;
208 /**
209 * This should return true if reading a NxM rectangle of pixels from a
210 * render target is faster if the target has dimensons N and M and the read
211 * rectangle has its top-left at 0,0.
212 */
213 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000214
215 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000216 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000217 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000218 * @param renderTarget the render target to read from. NULL means the
219 * current render target.
220 * @param left left edge of the rectangle to read (inclusive)
221 * @param top top edge of the rectangle to read (inclusive)
222 * @param width width of rectangle to read in pixels.
223 * @param height height of rectangle to read in pixels.
224 * @param config the pixel config of the destination buffer
225 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000226 * @param rowBytes the number of bytes between consecutive rows. Zero
227 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000228 * @param invertY buffer should be populated bottom-to-top as opposed
229 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230 *
231 * @return true if the read succeeded, false if not. The read can fail
232 * because of a unsupported pixel config or because no render
233 * target is currently set.
234 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000235 bool readPixels(GrRenderTarget* renderTarget,
236 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000237 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
bsalomon@google.com6f379512011-11-16 20:36:03 +0000239 /**
240 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000241 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000242 * @param left left edge of the rectangle to write (inclusive)
243 * @param top top edge of the rectangle to write (inclusive)
244 * @param width width of rectangle to write in pixels.
245 * @param height height of rectangle to write in pixels.
246 * @param config the pixel config of the source buffer
247 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000248 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000249 * means rows are tightly packed.
250 */
251 void writeTexturePixels(GrTexture* texture,
252 int left, int top, int width, int height,
253 GrPixelConfig config, const void* buffer,
254 size_t rowBytes);
255
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000256 /**
257 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000258 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000259 */
junov@google.com53a55842011-06-08 22:55:10 +0000260 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000261
262 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000263 * Called to tell Gpu object to release all GrResources. Overrides must call
264 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000265 */
266 void releaseResources();
267
268 /**
269 * Add resource to list of resources. Should only be called by GrResource.
270 * @param resource the resource to add.
271 */
272 void insertResource(GrResource* resource);
273
274 /**
275 * Remove resource from list of resources. Should only be called by
276 * GrResource.
277 * @param resource the resource to remove.
278 */
279 void removeResource(GrResource* resource);
280
bsalomon@google.com471d4712011-08-23 15:45:25 +0000281 // GrDrawTarget overrides
rmistry@google.comd6176b02012-08-23 18:14:13 +0000282 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000283 GrColor color,
284 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000285
robertphillips@google.comff175842012-05-14 19:31:39 +0000286 virtual void purgeResources() SK_OVERRIDE {
287 // The clip mask manager can rebuild all its clip masks so just
288 // get rid of them all.
289 fClipMaskManager.releaseResources();
290 }
291
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000292 // After the client interacts directly with the 3D context state the GrGpu
293 // must resync its internal state and assumptions about 3D context state.
294 // Each time this occurs the GrGpu bumps a timestamp.
295 // state of the 3D context
296 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
297 // a billion years.
298 typedef uint64_t ResetTimestamp;
299
300 // This timestamp is always older than the current timestamp
301 static const ResetTimestamp kExpiredTimestamp = 0;
302 // Returns a timestamp based on the number of times the context was reset.
303 // This timestamp can be used to lazily detect when cached 3D context state
304 // is dirty.
305 ResetTimestamp getResetTimestamp() const {
306 return fResetTimestamp;
307 }
308
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000309 /**
310 * Can the provided configuration act as a color render target?
311 */
312 bool isConfigRenderable(GrPixelConfig config) const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000313 GrAssert(kGrPixelConfigCount > config);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000314 return fConfigRenderSupport[config];
315 }
316
bsalomon@google.coma3201942012-06-21 19:58:20 +0000317 /**
318 * These methods are called by the clip manager's setupClipping function
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000319 * which (called as part of GrGpu's implementation of onDraw and
bsalomon@google.coma3201942012-06-21 19:58:20 +0000320 * onStencilPath member functions.) The GrGpu subclass should flush the
321 * stencil state to the 3D API in its implementation of flushGraphicsState.
322 */
323 void enableScissor(const GrIRect& rect) {
324 fScissorState.fEnabled = true;
325 fScissorState.fRect = rect;
326 }
327 void disableScissor() { fScissorState.fEnabled = false; }
328
329 /**
330 * Like the scissor methods above this is called by setupClipping and
331 * should be flushed by the GrGpu subclass in flushGraphicsState. These
332 * stencil settings should be used in place of those on the GrDrawState.
333 * They have been adjusted to account for any interactions between the
334 * GrDrawState's stencil settings and stencil clipping.
335 */
336 void setStencilSettings(const GrStencilSettings& settings) {
337 fStencilSettings = settings;
338 }
339 void disableStencil() { fStencilSettings.setDisabled(); }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000340
341 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
342 // free to clear the remaining bits to zero if masked clears are more
343 // expensive than clearing all bits.
344 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
345
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000346 enum PrivateDrawStateStateBits {
347 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348
349 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
350 // stencil bits used for
351 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000352 };
353
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000354protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000355 enum DrawType {
356 kDrawPoints_DrawType,
357 kDrawLines_DrawType,
358 kDrawTriangles_DrawType,
359 kStencilPath_DrawType,
360 };
361
362 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
363 switch (type) {
364 case kTriangles_GrPrimitiveType:
365 case kTriangleStrip_GrPrimitiveType:
366 case kTriangleFan_GrPrimitiveType:
367 return kDrawTriangles_DrawType;
368 case kPoints_GrPrimitiveType:
369 return kDrawPoints_DrawType;
370 case kLines_GrPrimitiveType:
371 case kLineStrip_GrPrimitiveType:
372 return kDrawLines_DrawType;
373 default:
374 GrCrash("Unexpected primitive type");
375 return kDrawTriangles_DrawType;
376 }
377 }
378
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000379 // prepares clip flushes gpu state before a draw
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000380 bool setupClipAndFlushState(DrawType);
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 // Functions used to map clip-respecting stencil tests into normal
383 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000384 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000385 GrStencilFunc func);
386 static void ConvertStencilFuncAndMask(GrStencilFunc func,
387 bool clipInStencil,
388 unsigned int clipBit,
389 unsigned int userBits,
390 unsigned int* ref,
391 unsigned int* mask);
392
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000393 GrClipMaskManager fClipMaskManager;
394
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000395 struct GeometryPoolState {
396 const GrVertexBuffer* fPoolVertexBuffer;
397 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000399 const GrIndexBuffer* fPoolIndexBuffer;
400 int fPoolStartIndex;
401 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000402 const GeometryPoolState& getGeomPoolState() {
403 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000404 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000405
bsalomon@google.coma3201942012-06-21 19:58:20 +0000406 // The state of the scissor is controlled by the clip manager
407 struct ScissorState {
408 bool fEnabled;
409 GrIRect fRect;
410 } fScissorState;
411
412 // The final stencil settings to use as determined by the clip manager.
413 GrStencilSettings fStencilSettings;
414
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000415 // Derived classes need access to this so they can fill it out in their
416 // constructors
417 bool fConfigRenderSupport[kGrPixelConfigCount];
418
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000419 // Helpers for setting up geometry state
420 void finalizeReservedVertices();
421 void finalizeReservedIndices();
422
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000423private:
424 // GrDrawTarget overrides
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000425 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000426 virtual bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
427 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
428 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
429 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) SK_OVERRIDE;
430 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) SK_OVERRIDE;
431 virtual void releaseVertexArray() SK_OVERRIDE;
432 virtual void releaseIndexArray() SK_OVERRIDE;
433 virtual void geometrySourceWillPush() SK_OVERRIDE;
434 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
435
436
bsalomon@google.comb635d392011-11-05 12:47:43 +0000437 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000438 // assumed 3D context state and dirty any state cache.
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000439 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000440
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000441 // overridden by backend-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000442 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000443 const void* srcData,
444 size_t rowBytes) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000445 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
446 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000447 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, bool dynamic) = 0;
448 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, bool dynamic) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000449 virtual GrPath* onCreatePath(const SkPath& path) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000451 // overridden by backend-specific derived class to perform the clear and
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000452 // clearRect. NULL rect means clear whole target.
453 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000454
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000455 // overridden by backend-specific derived class to perform the draw call.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000456 virtual void onGpuDraw(const DrawInfo&) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000457 // when GrDrawTarget::stencilPath is called the draw state's current stencil
458 // settings are ignored. Instead the GrGpu decides the stencil rules
459 // necessary to stencil the path. These are still subject to filtering by
460 // the clip mask manager.
461 virtual void setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000462 SkPath::FillType,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000463 GrStencilSettings* settings) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000464 // overridden by backend-specific derived class to perform the path stenciling.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000465 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000467 // overridden by backend-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000468 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000469
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000470 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000471 virtual bool onReadPixels(GrRenderTarget* target,
472 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000473 GrPixelConfig,
474 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000475 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000476
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000477 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com6f379512011-11-16 20:36:03 +0000478 virtual void onWriteTexturePixels(GrTexture* texture,
479 int left, int top, int width, int height,
480 GrPixelConfig config, const void* buffer,
481 size_t rowBytes) = 0;
482
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000483 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000484 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
485
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000486 // width and height may be larger than rt (if underlying API allows it).
487 // Should attach the SB to the RT. Returns false if compatible sb could
488 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000489 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000490
491 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000492 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
494 // The GrGpu typically records the clients requested state and then flushes
495 // deltas from previous state at draw time. This function does the
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000496 // backend-specific flush of the state
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 // returns false if current state is unsupported.
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000498 virtual bool flushGraphicsState(DrawType) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000500 // clears the entire stencil buffer to 0
501 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000503 // Given a rt, find or create a stencil buffer and attach it
504 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
505
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000506 // GrDrawTarget overrides
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000507 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000508 virtual void onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000509 SkPath::FillType) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000510
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000511 // readies the pools to provide vertex/index data.
512 void prepareVertexPool();
513 void prepareIndexPool();
514
bsalomon@google.comb635d392011-11-05 12:47:43 +0000515 void resetContext() {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000516 // We call this because the client may have messed with the
517 // stencil buffer. Perhaps we should detect whether it is a
518 // internally created stencil buffer and if so skip the invalidate.
519 fClipMaskManager.invalidateStencilMask();
bsalomon@google.comb635d392011-11-05 12:47:43 +0000520 this->onResetContext();
521 ++fResetTimestamp;
522 }
523
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000524 void handleDirtyContext() {
525 if (fContextIsDirty) {
526 this->resetContext();
527 fContextIsDirty = false;
528 }
529 }
530
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000531 enum {
532 kPreallocGeomPoolStateStackCnt = 4,
533 };
534 typedef SkTInternalLList<GrResource> ResourceList;
535 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
536 GrContext* fContext; // not reffed
537 ResetTimestamp fResetTimestamp;
538 GrVertexBufferAllocPool* fVertexPool;
539 GrIndexBufferAllocPool* fIndexPool;
540 // counts number of uses of vertex/index pool in the geometry stack
541 int fVertexPoolUseCnt;
542 int fIndexPoolUseCnt;
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000543 // these are mutable so they can be created on-demand
544 mutable GrVertexBuffer* fUnitSquareVertexBuffer;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000545 mutable GrIndexBuffer* fQuadIndexBuffer;
546 bool fContextIsDirty;
547 ResourceList fResourceList;
548
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000549 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000550};
551
552#endif