blob: dd5b2c83bdd418b7bfb259fb0ebb38a3c512883d [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrGpu_DEFINED
9#define GrGpu_DEFINED
10
bsalomon@google.com669fdc42011-04-05 17:08:27 +000011#include "GrDrawTarget.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000012#include "GrClipMaskManager.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000013#include "SkPath.h"
14
bsalomon@google.com669fdc42011-04-05 17:08:27 +000015class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016class GrIndexBufferAllocPool;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000017class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000018class GrPathRenderer;
19class GrPathRendererChain;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000020class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000021class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000022class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24class GrGpu : public GrDrawTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000025public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000026
reed@google.comac10a2d2010-12-22 21:39:39 +000027 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000028 * Additional blend coefficients for dual source blending, not exposed
bsalomon@google.com271cffc2011-05-20 14:13:56 +000029 * through GrPaint/GrContext.
30 */
31 enum ExtendedBlendCoeffs {
32 // source 2 refers to second output color when
33 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000034 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
35 kIS2C_GrBlendCoeff,
36 kS2A_GrBlendCoeff,
37 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000038
bsalomon@google.com47059542012-06-06 20:51:20 +000039 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000040 };
41
42 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000043 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000044 * not supported (at compile-time or run-time) this returns NULL. The context will not be
45 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000046 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000048
49 ////////////////////////////////////////////////////////////////////////////
50
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000051 GrGpu(GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000052 virtual ~GrGpu();
53
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000054 GrContext* getContext() { return this->INHERITED::getContext(); }
55 const GrContext* getContext() const { return this->INHERITED::getContext(); }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000056
reed@google.comac10a2d2010-12-22 21:39:39 +000057 /**
58 * The GrGpu object normally assumes that no outsider is setting state
59 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000060 * the GrGpu that the state was modified and it shouldn't make assumptions
61 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000062 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000063 void markContextDirty(uint32_t state = kAll_GrBackendState) {
64 fResetBits |= state;
65 }
reed@google.comac10a2d2010-12-22 21:39:39 +000066
67 void unimpl(const char[]);
68
69 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000070 * Creates a texture object. If desc width or height is not a power of
71 * two but underlying API requires a power of two texture then srcData
72 * will be embedded in a power of two texture. The extra width and height
73 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000074 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000075 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000076 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
bsalomon@google.com686bcb82013-04-09 15:04:12 +000077 * on the render target until the texture is destroyed.
bsalomon@google.com1da07462011-03-10 14:51:57 +000078 *
reed@google.comac10a2d2010-12-22 21:39:39 +000079 * @param desc describes the texture to be created.
80 * @param srcData texel data to load texture. Begins with full-size
81 * palette data for paletted textures. Contains width*
82 * height texels. If NULL texture data is uninitialized.
83 *
84 * @return The texture object if successful, otherwise NULL.
85 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +000086 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000087 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000088
bsalomon@google.come269f212011-11-07 13:29:52 +000089 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000090 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000091 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000092 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000093
94 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000095 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000096 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000097 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000098
99 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 * Creates a vertex buffer.
101 *
102 * @param size size in bytes of the vertex buffer
103 * @param dynamic hints whether the data will be frequently changed
104 * by either GrVertexBuffer::lock or
105 * GrVertexBuffer::updateData.
106 *
107 * @return The vertex buffer if successful, otherwise NULL.
108 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000109 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
111 /**
112 * Creates an index buffer.
113 *
114 * @param size size in bytes of the index buffer
115 * @param dynamic hints whether the data will be frequently changed
116 * by either GrIndexBuffer::lock or
117 * GrIndexBuffer::updateData.
118 *
119 * @return The index buffer if successful, otherwise NULL.
120 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000121 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000124 * Creates a path object that can be stenciled using stencilPath(). It is
125 * only legal to call this if the caps report support for path stenciling.
126 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000127 GrPath* createPath(const SkPath& path, const SkStrokeRec& stroke);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000128
129 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000131 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
132 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000133 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000134 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000136 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
138 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000139 * Resolves MSAA.
140 */
141 void resolveRenderTarget(GrRenderTarget* target);
142
143 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000144 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
145 * config surfaceConfig. The returned config must have at least as many bits per channel as the
146 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000147 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000148 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
149 GrPixelConfig surfaceConfig) const {
150 return readConfig;
151 }
152 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
153 GrPixelConfig surfaceConfig) const {
154 return writeConfig;
155 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000156
157 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000158 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
159 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000160 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000161 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000162
163 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000164 * OpenGL's readPixels returns the result bottom-to-top while the skia
165 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
166 * solution is to have the subclass do the flip using either the CPU or GPU.
167 * However, the caller (GrContext) may have transformations to apply and can
168 * simply fold in the y-flip for free. On the other hand, the subclass may
169 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000170 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000171 * concurrently.
172 *
173 * This function returns true if a y-flip is required to put the pixels in
174 * top-to-bottom order and the subclass cannot do it for free.
175 *
176 * See read pixels for the params
177 * @return true if calling readPixels with the same set of params will
178 * produce bottom-to-top data
179 */
180 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
181 int left, int top,
182 int width, int height,
183 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000184 size_t rowBytes) const = 0;
185 /**
186 * This should return true if reading a NxM rectangle of pixels from a
187 * render target is faster if the target has dimensons N and M and the read
188 * rectangle has its top-left at 0,0.
189 */
190 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000191
192 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000193 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000194 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000195 * @param renderTarget the render target to read from. NULL means the
196 * current render target.
197 * @param left left edge of the rectangle to read (inclusive)
198 * @param top top edge of the rectangle to read (inclusive)
199 * @param width width of rectangle to read in pixels.
200 * @param height height of rectangle to read in pixels.
201 * @param config the pixel config of the destination buffer
202 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000203 * @param rowBytes the number of bytes between consecutive rows. Zero
204 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000205 * @param invertY buffer should be populated bottom-to-top as opposed
206 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000207 *
208 * @return true if the read succeeded, false if not. The read can fail
209 * because of a unsupported pixel config or because no render
210 * target is currently set.
211 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000212 bool readPixels(GrRenderTarget* renderTarget,
213 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000214 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
bsalomon@google.com6f379512011-11-16 20:36:03 +0000216 /**
217 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000218 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000219 * @param left left edge of the rectangle to write (inclusive)
220 * @param top top edge of the rectangle to write (inclusive)
221 * @param width width of rectangle to write in pixels.
222 * @param height height of rectangle to write in pixels.
223 * @param config the pixel config of the source buffer
224 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000225 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000226 * means rows are tightly packed.
227 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000228 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000229 int left, int top, int width, int height,
230 GrPixelConfig config, const void* buffer,
231 size_t rowBytes);
232
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000233 /**
234 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000235 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000236 */
junov@google.com53a55842011-06-08 22:55:10 +0000237 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000238
239 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000240 * Called to tell Gpu object to release all GrResources. Overrides must call
241 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000242 */
243 void releaseResources();
244
245 /**
246 * Add resource to list of resources. Should only be called by GrResource.
247 * @param resource the resource to add.
248 */
249 void insertResource(GrResource* resource);
250
251 /**
252 * Remove resource from list of resources. Should only be called by
253 * GrResource.
254 * @param resource the resource to remove.
255 */
256 void removeResource(GrResource* resource);
257
bsalomon@google.com471d4712011-08-23 15:45:25 +0000258 // GrDrawTarget overrides
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000259 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000260 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000261 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000262 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000263
robertphillips@google.comff175842012-05-14 19:31:39 +0000264 virtual void purgeResources() SK_OVERRIDE {
265 // The clip mask manager can rebuild all its clip masks so just
266 // get rid of them all.
267 fClipMaskManager.releaseResources();
268 }
269
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000270 // After the client interacts directly with the 3D context state the GrGpu
271 // must resync its internal state and assumptions about 3D context state.
272 // Each time this occurs the GrGpu bumps a timestamp.
273 // state of the 3D context
274 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
275 // a billion years.
276 typedef uint64_t ResetTimestamp;
277
278 // This timestamp is always older than the current timestamp
279 static const ResetTimestamp kExpiredTimestamp = 0;
280 // Returns a timestamp based on the number of times the context was reset.
281 // This timestamp can be used to lazily detect when cached 3D context state
282 // is dirty.
283 ResetTimestamp getResetTimestamp() const {
284 return fResetTimestamp;
285 }
286
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000287 /**
bsalomon@google.coma3201942012-06-21 19:58:20 +0000288 * These methods are called by the clip manager's setupClipping function
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000289 * which (called as part of GrGpu's implementation of onDraw and
bsalomon@google.coma3201942012-06-21 19:58:20 +0000290 * onStencilPath member functions.) The GrGpu subclass should flush the
291 * stencil state to the 3D API in its implementation of flushGraphicsState.
292 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000293 void enableScissor(const SkIRect& rect) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000294 fScissorState.fEnabled = true;
295 fScissorState.fRect = rect;
296 }
297 void disableScissor() { fScissorState.fEnabled = false; }
298
299 /**
300 * Like the scissor methods above this is called by setupClipping and
301 * should be flushed by the GrGpu subclass in flushGraphicsState. These
302 * stencil settings should be used in place of those on the GrDrawState.
303 * They have been adjusted to account for any interactions between the
304 * GrDrawState's stencil settings and stencil clipping.
305 */
306 void setStencilSettings(const GrStencilSettings& settings) {
307 fStencilSettings = settings;
308 }
309 void disableStencil() { fStencilSettings.setDisabled(); }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000310
311 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
312 // free to clear the remaining bits to zero if masked clears are more
313 // expensive than clearing all bits.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000314 virtual void clearStencilClip(const SkIRect& rect, bool insideClip) = 0;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000315
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000316 enum PrivateDrawStateStateBits {
317 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000318
319 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
320 // stencil bits used for
321 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000322 };
323
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000324 void getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings);
325
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000326 enum DrawType {
327 kDrawPoints_DrawType,
328 kDrawLines_DrawType,
329 kDrawTriangles_DrawType,
330 kStencilPath_DrawType,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000331 kDrawPath_DrawType,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000332 kDrawPaths_DrawType,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000333 };
334
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000335protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000336 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
337 switch (type) {
338 case kTriangles_GrPrimitiveType:
339 case kTriangleStrip_GrPrimitiveType:
340 case kTriangleFan_GrPrimitiveType:
341 return kDrawTriangles_DrawType;
342 case kPoints_GrPrimitiveType:
343 return kDrawPoints_DrawType;
344 case kLines_GrPrimitiveType:
345 case kLineStrip_GrPrimitiveType:
346 return kDrawLines_DrawType;
347 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000348 SkFAIL("Unexpected primitive type");
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000349 return kDrawTriangles_DrawType;
350 }
351 }
352
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000353 // prepares clip flushes gpu state before a draw
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000354 bool setupClipAndFlushState(DrawType,
355 const GrDeviceCoordTexture* dstCopy,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000356 GrDrawState::AutoRestoreEffects* are,
357 const SkRect* devBounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 // Functions used to map clip-respecting stencil tests into normal
360 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000361 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000362 GrStencilFunc func);
363 static void ConvertStencilFuncAndMask(GrStencilFunc func,
364 bool clipInStencil,
365 unsigned int clipBit,
366 unsigned int userBits,
367 unsigned int* ref,
368 unsigned int* mask);
369
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000370 GrClipMaskManager fClipMaskManager;
371
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000372 struct GeometryPoolState {
373 const GrVertexBuffer* fPoolVertexBuffer;
374 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376 const GrIndexBuffer* fPoolIndexBuffer;
377 int fPoolStartIndex;
378 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000379 const GeometryPoolState& getGeomPoolState() {
380 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000381 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000382
bsalomon@google.coma3201942012-06-21 19:58:20 +0000383 // The state of the scissor is controlled by the clip manager
384 struct ScissorState {
385 bool fEnabled;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000386 SkIRect fRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000387 } fScissorState;
388
389 // The final stencil settings to use as determined by the clip manager.
390 GrStencilSettings fStencilSettings;
391
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000392 // Helpers for setting up geometry state
393 void finalizeReservedVertices();
394 void finalizeReservedIndices();
395
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000396private:
397 // GrDrawTarget overrides
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000398 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000399 virtual bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
400 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
401 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
402 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) SK_OVERRIDE;
403 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) SK_OVERRIDE;
404 virtual void releaseVertexArray() SK_OVERRIDE;
405 virtual void releaseIndexArray() SK_OVERRIDE;
406 virtual void geometrySourceWillPush() SK_OVERRIDE;
407 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
408
409
bsalomon@google.comb635d392011-11-05 12:47:43 +0000410 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000411 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000412 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000413
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000414 // overridden by backend-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000415 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000416 const void* srcData,
417 size_t rowBytes) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000418 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
419 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000420 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
421 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000422 virtual GrPath* onCreatePath(const SkPath& path, const SkStrokeRec&) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000423
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000424 // overridden by backend-specific derived class to perform the clear and
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000425 // clearRect. NULL rect means clear whole target. If canIgnoreRect is
426 // true, it is okay to perform a full clear instead of a partial clear
427 virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000429 // overridden by backend-specific derived class to perform the draw call.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000430 virtual void onGpuDraw(const DrawInfo&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000431
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000432 // overridden by backend-specific derived class to perform the path stenciling.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000433 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000434 virtual void onGpuDrawPath(const GrPath*, SkPath::FillType) = 0;
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000435 virtual void onGpuDrawPaths(int, const GrPath**, const SkMatrix*,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000436 SkPath::FillType, SkStrokeRec::Style) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000437
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000438 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000439 virtual bool onReadPixels(GrRenderTarget* target,
440 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000441 GrPixelConfig,
442 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000443 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000444
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000445 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000446 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000447 int left, int top, int width, int height,
448 GrPixelConfig config, const void* buffer,
449 size_t rowBytes) = 0;
450
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000451 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000452 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
453
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000454 // width and height may be larger than rt (if underlying API allows it).
455 // Should attach the SB to the RT. Returns false if compatible sb could
456 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000457 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000458
459 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000460 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
462 // The GrGpu typically records the clients requested state and then flushes
463 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000464 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 // returns false if current state is unsupported.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000466 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
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
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000471 // Given a rt, find or create a stencil buffer and attach it
472 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
473
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 // GrDrawTarget overrides
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000475 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000476 virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
477 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000478 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000479 virtual void onDrawPaths(int, const GrPath**, const SkMatrix*,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000480 SkPath::FillType, SkStrokeRec::Style,
481 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000482
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000483 // readies the pools to provide vertex/index data.
484 void prepareVertexPool();
485 void prepareIndexPool();
486
bsalomon@google.comb635d392011-11-05 12:47:43 +0000487 void resetContext() {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000488 // We call this because the client may have messed with the
489 // stencil buffer. Perhaps we should detect whether it is a
490 // internally created stencil buffer and if so skip the invalidate.
491 fClipMaskManager.invalidateStencilMask();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000492 this->onResetContext(fResetBits);
493 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000494 ++fResetTimestamp;
495 }
496
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000497 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000498 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000499 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000500 }
501 }
502
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000503 enum {
504 kPreallocGeomPoolStateStackCnt = 4,
505 };
506 typedef SkTInternalLList<GrResource> ResourceList;
507 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000508 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000509 uint32_t fResetBits;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000510 GrVertexBufferAllocPool* fVertexPool;
511 GrIndexBufferAllocPool* fIndexPool;
512 // counts number of uses of vertex/index pool in the geometry stack
513 int fVertexPoolUseCnt;
514 int fIndexPoolUseCnt;
bsalomon@google.com64386952013-02-08 21:22:44 +0000515 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000516 mutable GrIndexBuffer* fQuadIndexBuffer;
skia.committer@gmail.com66a4a4c2013-05-11 07:01:34 +0000517 // Used to abandon/release all resources created by this GrGpu. TODO: Move this
bsalomon@google.com76202b82013-05-10 19:08:22 +0000518 // functionality to GrResourceCache.
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000519 ResourceList fResourceList;
520
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000521 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000522};
523
524#endif