blob: 353ec33437f24382eea8dcd37b21a9bff663956d [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"
kkinnunenccdaa042014-08-20 01:36:23 -070013#include "GrPathRendering.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000014#include "SkPath.h"
15
bsalomon@google.com669fdc42011-04-05 17:08:27 +000016class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000017class GrIndexBufferAllocPool;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000018class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070019class GrPathRange;
bsalomon@google.com30085192011-08-19 15:42:31 +000020class GrPathRenderer;
21class GrPathRendererChain;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000022class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000023class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
25class GrGpu : public GrDrawTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000026public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000027
reed@google.comac10a2d2010-12-22 21:39:39 +000028 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000029 * Additional blend coefficients for dual source blending, not exposed
bsalomon@google.com271cffc2011-05-20 14:13:56 +000030 * through GrPaint/GrContext.
31 */
32 enum ExtendedBlendCoeffs {
33 // source 2 refers to second output color when
34 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000035 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
36 kIS2C_GrBlendCoeff,
37 kS2A_GrBlendCoeff,
38 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000039
bsalomon@google.com47059542012-06-06 20:51:20 +000040 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000041 };
42
43 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000044 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000045 * not supported (at compile-time or run-time) this returns NULL. The context will not be
46 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000047 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000048 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000049
50 ////////////////////////////////////////////////////////////////////////////
51
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000052 GrGpu(GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000053 virtual ~GrGpu();
54
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055 GrContext* getContext() { return this->INHERITED::getContext(); }
56 const GrContext* getContext() const { return this->INHERITED::getContext(); }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000057
kkinnunenccdaa042014-08-20 01:36:23 -070058 GrPathRendering* pathRendering() {
59 return fPathRendering.get();
60 }
61
bsalomonc8dc1f72014-08-21 13:02:13 -070062 // Called by GrContext when the underlying backend context has been destroyed.
63 // GrGpu should use this to ensure that no backend API calls will be made from
64 // here onward, including in its destructor. Subclasses should call
65 // INHERITED::contextAbandonded() if they override this.
66 virtual void contextAbandonded();
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.com0a208a12013-06-28 18:57:35 +000074 void markContextDirty(uint32_t state = kAll_GrBackendState) {
75 fResetBits |= state;
76 }
reed@google.comac10a2d2010-12-22 21:39:39 +000077
78 void unimpl(const char[]);
79
80 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000081 * Creates a texture object. If desc width or height is not a power of
82 * two but underlying API requires a power of two texture then srcData
83 * will be embedded in a power of two texture. The extra width and height
84 * is filled as though srcData were rendered clamped into the texture.
krajcevski9c0e6292014-06-02 07:38:14 -070085 * The exception is when using compressed data formats. In this case, the
86 * desc width and height must be a multiple of the compressed format block
87 * size otherwise this function returns NULL. Similarly, if the underlying
88 * API requires a power of two texture and the source width and height are not
89 * a power of two, then this function returns NULL.
reed@google.comac10a2d2010-12-22 21:39:39 +000090 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000092 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
krajcevski9c0e6292014-06-02 07:38:14 -070093 * on the render target until the texture is destroyed. Compressed textures
94 * cannot have the kRenderTarget_TextureFlag set.
bsalomon@google.com1da07462011-03-10 14:51:57 +000095 *
reed@google.comac10a2d2010-12-22 21:39:39 +000096 * @param desc describes the texture to be created.
97 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -070098 * palette data for paletted textures. For compressed
99 * formats it contains the compressed pixel data. Otherwise,
100 * it contains width*height texels. If NULL texture data
101 * is uninitialized.
102 * @param rowBytes the number of bytes between consecutive rows. Zero
103 * means rows are tightly packed. This field is ignored
104 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 *
106 * @return The texture object if successful, otherwise NULL.
107 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000108 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000109 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
bsalomon@google.come269f212011-11-07 13:29:52 +0000111 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000112 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000113 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000114 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000115
116 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000117 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000118 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000119 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000120
121 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 * Creates a vertex buffer.
123 *
124 * @param size size in bytes of the vertex buffer
125 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000126 * by either GrVertexBuffer::map() or
127 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 *
129 * @return The vertex buffer if successful, otherwise NULL.
130 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000131 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000132
133 /**
134 * Creates an index buffer.
135 *
136 * @param size size in bytes of the index buffer
137 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000138 * by either GrIndexBuffer::map() or
139 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 *
141 * @return The index buffer if successful, otherwise NULL.
142 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000143 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
145 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000146 * Creates a path object that can be stenciled using stencilPath(). It is
147 * only legal to call this if the caps report support for path stenciling.
148 */
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000149 GrPath* createPath(const SkPath& path, const SkStrokeRec& stroke);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000150
151 /**
cdaltonb85a0aa2014-07-21 15:32:44 -0700152 * Creates a path range object that can be used to draw multiple paths via
153 * drawPaths(). It is only legal to call this if the caps report support for
154 * path rendering.
155 */
156 GrPathRange* createPathRange(size_t size, const SkStrokeRec&);
157
158 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000160 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
161 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000162 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000163 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000165 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166
167 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000168 * Resolves MSAA.
169 */
170 void resolveRenderTarget(GrRenderTarget* target);
171
172 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000173 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
174 * config surfaceConfig. The returned config must have at least as many bits per channel as the
175 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000176 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000177 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
178 GrPixelConfig surfaceConfig) const {
179 return readConfig;
180 }
181 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
182 GrPixelConfig surfaceConfig) const {
183 return writeConfig;
184 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000185
186 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000187 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
188 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000189 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000190 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000191
192 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000193 * OpenGL's readPixels returns the result bottom-to-top while the skia
194 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
195 * solution is to have the subclass do the flip using either the CPU or GPU.
196 * However, the caller (GrContext) may have transformations to apply and can
197 * simply fold in the y-flip for free. On the other hand, the subclass may
198 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000200 * concurrently.
201 *
202 * This function returns true if a y-flip is required to put the pixels in
203 * top-to-bottom order and the subclass cannot do it for free.
204 *
205 * See read pixels for the params
206 * @return true if calling readPixels with the same set of params will
207 * produce bottom-to-top data
208 */
209 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
210 int left, int top,
211 int width, int height,
212 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000213 size_t rowBytes) const = 0;
214 /**
215 * This should return true if reading a NxM rectangle of pixels from a
216 * render target is faster if the target has dimensons N and M and the read
217 * rectangle has its top-left at 0,0.
218 */
219 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000220
221 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000222 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000223 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000224 * @param renderTarget the render target to read from. NULL means the
225 * current render target.
226 * @param left left edge of the rectangle to read (inclusive)
227 * @param top top edge of the rectangle to read (inclusive)
228 * @param width width of rectangle to read in pixels.
229 * @param height height of rectangle to read in pixels.
230 * @param config the pixel config of the destination buffer
231 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000232 * @param rowBytes the number of bytes between consecutive rows. Zero
233 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 * @param invertY buffer should be populated bottom-to-top as opposed
235 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236 *
237 * @return true if the read succeeded, false if not. The read can fail
238 * because of a unsupported pixel config or because no render
239 * target is currently set.
240 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241 bool readPixels(GrRenderTarget* renderTarget,
242 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000243 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
bsalomon@google.com6f379512011-11-16 20:36:03 +0000245 /**
246 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000247 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000248 * @param left left edge of the rectangle to write (inclusive)
249 * @param top top edge of the rectangle to write (inclusive)
250 * @param width width of rectangle to write in pixels.
251 * @param height height of rectangle to write in pixels.
252 * @param config the pixel config of the source buffer
253 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000254 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000255 * means rows are tightly packed.
256 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000257 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000258 int left, int top, int width, int height,
259 GrPixelConfig config, const void* buffer,
260 size_t rowBytes);
261
bsalomon@google.com471d4712011-08-23 15:45:25 +0000262 // GrDrawTarget overrides
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000263 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000264 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000265 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000266 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000267
robertphillips@google.comff175842012-05-14 19:31:39 +0000268 virtual void purgeResources() SK_OVERRIDE {
269 // The clip mask manager can rebuild all its clip masks so just
270 // get rid of them all.
bsalomonc8dc1f72014-08-21 13:02:13 -0700271 fClipMaskManager.purgeResources();
robertphillips@google.comff175842012-05-14 19:31:39 +0000272 }
273
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000274 // After the client interacts directly with the 3D context state the GrGpu
275 // must resync its internal state and assumptions about 3D context state.
276 // Each time this occurs the GrGpu bumps a timestamp.
277 // state of the 3D context
278 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
279 // a billion years.
280 typedef uint64_t ResetTimestamp;
281
282 // This timestamp is always older than the current timestamp
283 static const ResetTimestamp kExpiredTimestamp = 0;
284 // Returns a timestamp based on the number of times the context was reset.
285 // This timestamp can be used to lazily detect when cached 3D context state
286 // is dirty.
287 ResetTimestamp getResetTimestamp() const {
288 return fResetTimestamp;
289 }
290
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000291 /**
bsalomon@google.coma3201942012-06-21 19:58:20 +0000292 * These methods are called by the clip manager's setupClipping function
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000293 * which (called as part of GrGpu's implementation of onDraw and
bsalomon@google.coma3201942012-06-21 19:58:20 +0000294 * onStencilPath member functions.) The GrGpu subclass should flush the
295 * stencil state to the 3D API in its implementation of flushGraphicsState.
296 */
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000297 void enableScissor(const SkIRect& rect) {
bsalomon@google.coma3201942012-06-21 19:58:20 +0000298 fScissorState.fEnabled = true;
299 fScissorState.fRect = rect;
300 }
301 void disableScissor() { fScissorState.fEnabled = false; }
302
303 /**
304 * Like the scissor methods above this is called by setupClipping and
305 * should be flushed by the GrGpu subclass in flushGraphicsState. These
306 * stencil settings should be used in place of those on the GrDrawState.
307 * They have been adjusted to account for any interactions between the
308 * GrDrawState's stencil settings and stencil clipping.
309 */
310 void setStencilSettings(const GrStencilSettings& settings) {
311 fStencilSettings = settings;
312 }
313 void disableStencil() { fStencilSettings.setDisabled(); }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000314
315 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
316 // free to clear the remaining bits to zero if masked clears are more
317 // expensive than clearing all bits.
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000318 virtual void clearStencilClip(const SkIRect& rect, bool insideClip) = 0;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000319
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000320 enum PrivateDrawStateStateBits {
321 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000322
323 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
324 // stencil bits used for
325 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 };
327
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000328 void getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings);
329
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000330 enum DrawType {
331 kDrawPoints_DrawType,
332 kDrawLines_DrawType,
333 kDrawTriangles_DrawType,
334 kStencilPath_DrawType,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000335 kDrawPath_DrawType,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000336 kDrawPaths_DrawType,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000337 };
338
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000339protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000340 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
341 switch (type) {
342 case kTriangles_GrPrimitiveType:
343 case kTriangleStrip_GrPrimitiveType:
344 case kTriangleFan_GrPrimitiveType:
345 return kDrawTriangles_DrawType;
346 case kPoints_GrPrimitiveType:
347 return kDrawPoints_DrawType;
348 case kLines_GrPrimitiveType:
349 case kLineStrip_GrPrimitiveType:
350 return kDrawLines_DrawType;
351 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000352 SkFAIL("Unexpected primitive type");
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000353 return kDrawTriangles_DrawType;
354 }
355 }
356
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000357 // prepares clip flushes gpu state before a draw
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000358 bool setupClipAndFlushState(DrawType,
359 const GrDeviceCoordTexture* dstCopy,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000360 GrDrawState::AutoRestoreEffects* are,
361 const SkRect* devBounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 // Functions used to map clip-respecting stencil tests into normal
364 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000365 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000366 GrStencilFunc func);
367 static void ConvertStencilFuncAndMask(GrStencilFunc func,
368 bool clipInStencil,
369 unsigned int clipBit,
370 unsigned int userBits,
371 unsigned int* ref,
372 unsigned int* mask);
373
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000374 GrClipMaskManager fClipMaskManager;
375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376 struct GeometryPoolState {
377 const GrVertexBuffer* fPoolVertexBuffer;
378 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000379
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380 const GrIndexBuffer* fPoolIndexBuffer;
381 int fPoolStartIndex;
382 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383 const GeometryPoolState& getGeomPoolState() {
384 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000385 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000386
bsalomon@google.coma3201942012-06-21 19:58:20 +0000387 // The state of the scissor is controlled by the clip manager
388 struct ScissorState {
389 bool fEnabled;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000390 SkIRect fRect;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000391 } fScissorState;
392
393 // The final stencil settings to use as determined by the clip manager.
394 GrStencilSettings fStencilSettings;
395
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396 // Helpers for setting up geometry state
397 void finalizeReservedVertices();
398 void finalizeReservedIndices();
399
kkinnunenccdaa042014-08-20 01:36:23 -0700400 SkAutoTDelete<GrPathRendering> fPathRendering;
401
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000402private:
403 // GrDrawTarget overrides
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000404 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000405 virtual bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
406 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
407 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
408 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) SK_OVERRIDE;
409 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) SK_OVERRIDE;
410 virtual void releaseVertexArray() SK_OVERRIDE;
411 virtual void releaseIndexArray() SK_OVERRIDE;
412 virtual void geometrySourceWillPush() SK_OVERRIDE;
413 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
414
415
bsalomon@google.comb635d392011-11-05 12:47:43 +0000416 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000417 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000418 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000419
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000420 // overridden by backend-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000421 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000422 const void* srcData,
423 size_t rowBytes) = 0;
krajcevski9c0e6292014-06-02 07:38:14 -0700424 virtual GrTexture* onCreateCompressedTexture(const GrTextureDesc& desc,
425 const void* srcData) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000426 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
427 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000428 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
429 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000430
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000431 // overridden by backend-specific derived class to perform the clear and
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000432 // clearRect. NULL rect means clear whole target. If canIgnoreRect is
433 // true, it is okay to perform a full clear instead of a partial clear
434 virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000435
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000436 // overridden by backend-specific derived class to perform the draw call.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000437 virtual void onGpuDraw(const DrawInfo&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000438
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000439 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000440 virtual bool onReadPixels(GrRenderTarget* target,
441 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000442 GrPixelConfig,
443 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000444 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000445
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000446 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000447 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000448 int left, int top, int width, int height,
449 GrPixelConfig config, const void* buffer,
450 size_t rowBytes) = 0;
451
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000452 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000453 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
454
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000455 // width and height may be larger than rt (if underlying API allows it).
456 // Should attach the SB to the RT. Returns false if compatible sb could
457 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000458 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000459
460 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000461 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000462
463 // The GrGpu typically records the clients requested state and then flushes
464 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000465 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 // returns false if current state is unsupported.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000467 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000469 // clears the entire stencil buffer to 0
470 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000472 // Given a rt, find or create a stencil buffer and attach it
473 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
474
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000475 // GrDrawTarget overrides
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000476 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000477 virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
478 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000479 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
cdaltonb85a0aa2014-07-21 15:32:44 -0700480 virtual void onDrawPaths(const GrPathRange*,
481 const uint32_t indices[], int count,
482 const float transforms[], PathTransformType,
483 SkPath::FillType, const GrDeviceCoordTexture*) SK_OVERRIDE;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000484
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000485 // readies the pools to provide vertex/index data.
486 void prepareVertexPool();
487 void prepareIndexPool();
488
bsalomon@google.comb635d392011-11-05 12:47:43 +0000489 void resetContext() {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000490 // We call this because the client may have messed with the
491 // stencil buffer. Perhaps we should detect whether it is a
492 // internally created stencil buffer and if so skip the invalidate.
493 fClipMaskManager.invalidateStencilMask();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000494 this->onResetContext(fResetBits);
495 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000496 ++fResetTimestamp;
497 }
498
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000499 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000500 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000501 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000502 }
503 }
504
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000505 enum {
506 kPreallocGeomPoolStateStackCnt = 4,
507 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000508 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000509 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000510 uint32_t fResetBits;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000511 GrVertexBufferAllocPool* fVertexPool;
512 GrIndexBufferAllocPool* fIndexPool;
513 // counts number of uses of vertex/index pool in the geometry stack
514 int fVertexPoolUseCnt;
515 int fIndexPoolUseCnt;
bsalomon@google.com64386952013-02-08 21:22:44 +0000516 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000517 mutable GrIndexBuffer* fQuadIndexBuffer;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000518
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000519 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000520};
521
522#endif