blob: 02fc75e5545a54595e3355bcbb232dca4c1f5d89 [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"
kkinnunenccdaa042014-08-20 01:36:23 -070012#include "GrPathRendering.h"
joshualitt79f8fae2014-10-28 17:59:26 -070013#include "GrProgramDesc.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
joshualitt3322fa42014-11-07 08:48:51 -080025class GrGpu : public SkRefCnt {
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
joshualitt3322fa42014-11-07 08:48:51 -080055 GrContext* getContext() { return fContext; }
56 const GrContext* getContext() const { return fContext; }
57
58 /**
59 * Gets the capabilities of the draw target.
60 */
61 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
62
63 /**
64 * Sets the draw state object for the gpu. Note that this does not
65 * make a copy. The GrGpu will take a reference to passed object.
66 * Passing NULL will cause the GrGpu to use its own internal draw
67 * state object rather than an externally provided one.
68 */
69 void setDrawState(GrDrawState* drawState);
70
71 /**
72 * Read-only access to the GrGpu current draw state.
73 */
74 const GrDrawState& getDrawState() const { return *fDrawState; }
75
76 /**
77 * Read-write access to the GrGpu current draw state. Note that
78 * this doesn't ref.
79 */
80 GrDrawState* drawState() { return fDrawState; }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000081
kkinnunenccdaa042014-08-20 01:36:23 -070082 GrPathRendering* pathRendering() {
83 return fPathRendering.get();
84 }
85
bsalomonc8dc1f72014-08-21 13:02:13 -070086 // Called by GrContext when the underlying backend context has been destroyed.
87 // GrGpu should use this to ensure that no backend API calls will be made from
88 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070089 // INHERITED::contextAbandoned() if they override this.
90 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070091
reed@google.comac10a2d2010-12-22 21:39:39 +000092 /**
93 * The GrGpu object normally assumes that no outsider is setting state
94 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000095 * the GrGpu that the state was modified and it shouldn't make assumptions
96 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000097 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000098 void markContextDirty(uint32_t state = kAll_GrBackendState) {
99 fResetBits |= state;
100 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101
102 void unimpl(const char[]);
103
104 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +0000105 * Creates a texture object. If desc width or height is not a power of
106 * two but underlying API requires a power of two texture then srcData
107 * will be embedded in a power of two texture. The extra width and height
108 * is filled as though srcData were rendered clamped into the texture.
krajcevski9c0e6292014-06-02 07:38:14 -0700109 * The exception is when using compressed data formats. In this case, the
110 * desc width and height must be a multiple of the compressed format block
111 * size otherwise this function returns NULL. Similarly, if the underlying
112 * API requires a power of two texture and the source width and height are not
113 * a power of two, then this function returns NULL.
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +0000116 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
krajcevski9c0e6292014-06-02 07:38:14 -0700117 * on the render target until the texture is destroyed. Compressed textures
118 * cannot have the kRenderTarget_TextureFlag set.
bsalomon@google.com1da07462011-03-10 14:51:57 +0000119 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 * @param desc describes the texture to be created.
121 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -0700122 * palette data for paletted textures. For compressed
123 * formats it contains the compressed pixel data. Otherwise,
124 * it contains width*height texels. If NULL texture data
125 * is uninitialized.
126 * @param rowBytes the number of bytes between consecutive rows. Zero
127 * means rows are tightly packed. This field is ignored
128 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 *
130 * @return The texture object if successful, otherwise NULL.
131 */
bsalomonf2703d82014-10-28 14:33:06 -0700132 GrTexture* createTexture(const GrSurfaceDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000133 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
bsalomon@google.come269f212011-11-07 13:29:52 +0000135 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000136 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000137 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000138 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000139
140 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000141 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000142 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000143 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000144
145 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 * Creates a vertex buffer.
147 *
148 * @param size size in bytes of the vertex buffer
149 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000150 * by either GrVertexBuffer::map() or
151 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 *
153 * @return The vertex buffer if successful, otherwise NULL.
154 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000155 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
157 /**
158 * Creates an index buffer.
159 *
160 * @param size size in bytes of the index buffer
161 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000162 * by either GrIndexBuffer::map() or
163 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 *
165 * @return The index buffer if successful, otherwise NULL.
166 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000167 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
169 /**
joshualitt5ead6da2014-10-22 16:00:29 -0700170 * Creates an index buffer for instance drawing with a specific pattern.
171 *
172 * @param pattern the pattern to repeat
173 * @param patternSize size in bytes of the pattern
174 * @param reps number of times to repeat the pattern
175 * @param vertCount number of vertices the pattern references
176 * @param dynamic hints whether the data will be frequently changed
177 * by either GrIndexBuffer::map() or
178 * GrIndexBuffer::updateData().
179 *
180 * @return The index buffer if successful, otherwise NULL.
181 */
182 GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
183 int patternSize,
184 int reps,
185 int vertCount,
186 bool isDynamic = false);
187
188 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000190 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
191 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000192 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000193 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000195 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000196
197 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000198 * Resolves MSAA.
199 */
200 void resolveRenderTarget(GrRenderTarget* target);
201
202 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000203 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
204 * config surfaceConfig. The returned config must have at least as many bits per channel as the
205 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000206 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000207 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
208 GrPixelConfig surfaceConfig) const {
209 return readConfig;
210 }
211 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
212 GrPixelConfig surfaceConfig) const {
213 return writeConfig;
214 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000215
216 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000217 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
218 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000219 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000220 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000221
222 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000223 * OpenGL's readPixels returns the result bottom-to-top while the skia
224 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
225 * solution is to have the subclass do the flip using either the CPU or GPU.
226 * However, the caller (GrContext) may have transformations to apply and can
227 * simply fold in the y-flip for free. On the other hand, the subclass may
228 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000229 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000230 * concurrently.
231 *
232 * This function returns true if a y-flip is required to put the pixels in
233 * top-to-bottom order and the subclass cannot do it for free.
234 *
235 * See read pixels for the params
236 * @return true if calling readPixels with the same set of params will
237 * produce bottom-to-top data
238 */
239 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
240 int left, int top,
241 int width, int height,
242 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000243 size_t rowBytes) const = 0;
244 /**
245 * This should return true if reading a NxM rectangle of pixels from a
246 * render target is faster if the target has dimensons N and M and the read
247 * rectangle has its top-left at 0,0.
248 */
249 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000250
251 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000252 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000253 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000254 * @param renderTarget the render target to read from. NULL means the
255 * current render target.
256 * @param left left edge of the rectangle to read (inclusive)
257 * @param top top edge of the rectangle to read (inclusive)
258 * @param width width of rectangle to read in pixels.
259 * @param height height of rectangle to read in pixels.
260 * @param config the pixel config of the destination buffer
261 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000262 * @param rowBytes the number of bytes between consecutive rows. Zero
263 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000264 * @param invertY buffer should be populated bottom-to-top as opposed
265 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000266 *
267 * @return true if the read succeeded, false if not. The read can fail
268 * because of a unsupported pixel config or because no render
269 * target is currently set.
270 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000271 bool readPixels(GrRenderTarget* renderTarget,
272 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000273 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000274
bsalomon@google.com6f379512011-11-16 20:36:03 +0000275 /**
276 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000277 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000278 * @param left left edge of the rectangle to write (inclusive)
279 * @param top top edge of the rectangle to write (inclusive)
280 * @param width width of rectangle to write in pixels.
281 * @param height height of rectangle to write in pixels.
282 * @param config the pixel config of the source buffer
283 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000284 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000285 * means rows are tightly packed.
286 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000287 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000288 int left, int top, int width, int height,
289 GrPixelConfig config, const void* buffer,
290 size_t rowBytes);
291
joshualitt3322fa42014-11-07 08:48:51 -0800292 /**
293 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
294 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
295 * can be optionally cleared.
296 */
297 void clear(const SkIRect* rect,
298 GrColor color,
299 bool canIgnoreRect,
300 GrRenderTarget* renderTarget);
301
302
303 void clearStencilClip(const SkIRect& rect,
304 bool insideClip,
305 GrRenderTarget* renderTarget = NULL);
306
307 /**
308 * Discards the contents render target. NULL indicates that the current render target should
309 * be discarded.
310 **/
311 virtual void discard(GrRenderTarget* = NULL) = 0;
312
313 /**
314 * This is can be called before allocating a texture to be a dst for copySurface. It will
315 * populate the origin, config, and flags fields of the desc such that copySurface is more
316 * likely to succeed and be efficient.
317 */
318 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
joshualitt6db519c2014-10-29 08:48:18 -0700319
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000320 // After the client interacts directly with the 3D context state the GrGpu
321 // must resync its internal state and assumptions about 3D context state.
322 // Each time this occurs the GrGpu bumps a timestamp.
323 // state of the 3D context
324 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
325 // a billion years.
326 typedef uint64_t ResetTimestamp;
327
328 // This timestamp is always older than the current timestamp
329 static const ResetTimestamp kExpiredTimestamp = 0;
330 // Returns a timestamp based on the number of times the context was reset.
331 // This timestamp can be used to lazily detect when cached 3D context state
332 // is dirty.
333 ResetTimestamp getResetTimestamp() const {
334 return fResetTimestamp;
335 }
336
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000337 enum DrawType {
338 kDrawPoints_DrawType,
339 kDrawLines_DrawType,
340 kDrawTriangles_DrawType,
341 kStencilPath_DrawType,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000342 kDrawPath_DrawType,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000343 kDrawPaths_DrawType,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000344 };
345
kkinnunenec56e452014-08-25 22:21:16 -0700346 static bool IsPathRenderingDrawType(DrawType type) {
347 return kDrawPath_DrawType == type || kDrawPaths_DrawType == type;
348 }
349
robertphillips754f4e92014-09-18 13:52:08 -0700350 GrContext::GPUStats* gpuStats() { return &fGPUStats; }
351
joshualitt79f8fae2014-10-28 17:59:26 -0700352 virtual void buildProgramDesc(const GrOptDrawState&,
353 const GrProgramDesc::DescInfo&,
354 GrGpu::DrawType,
355 const GrDeviceCoordTexture* dstCopy,
356 GrProgramDesc*) = 0;
357
joshualitt3322fa42014-11-07 08:48:51 -0800358 /**
359 * Called at start and end of gpu trace marking
360 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
361 * and end of a code block respectively
362 */
363 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
364 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
365
366 /**
367 * Takes the current active set of markers and stores them for later use. Any current marker
368 * in the active set is removed from the active set and the targets remove function is called.
369 * These functions do not work as a stack so you cannot call save a second time before calling
370 * restore. Also, it is assumed that when restore is called the current active set of markers
371 * is empty. When the stored markers are added back into the active set, the targets add marker
372 * is called.
373 */
374 void saveActiveTraceMarkers();
375 void restoreActiveTraceMarkers();
376
377 /**
378 * Query to find out if the vertex or index source is reserved.
379 */
380 bool hasReservedVerticesOrIndices() const {
381 return GrDrawTarget::kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc ||
382 GrDrawTarget::kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
383 }
384
385 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
386 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
387 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
388 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
389 // and dst bounds.
390 virtual bool canCopySurface(GrSurface* dst,
391 GrSurface* src,
392 const SkIRect& srcRect,
393 const SkIPoint& dstPoint) = 0;
394
395 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
396 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
397 // entirely within the dst. The default implementation will draw a rect from the src to the
398 // dst if the src is a texture and the dst is a render target and fail otherwise.
399 virtual bool copySurface(GrSurface* dst,
400 GrSurface* src,
401 const SkIRect& srcRect,
402 const SkIPoint& dstPoint) = 0;
403
404 /**
405 * Sets source of vertex data for the next draw. Data does not have to be
406 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
407 *
408 * @param buffer vertex buffer containing vertex data. Must be
409 * unlocked before draw call. Vertex size is queried
410 * from current GrDrawState.
411 */
412 void setVertexSourceToBuffer(const GrVertexBuffer* buffer);
413
414 /**
415 * Sets source of index data for the next indexed draw. Data does not have
416 * to be in the buffer until drawIndexed.
417 *
418 * @param buffer index buffer containing indices. Must be unlocked
419 * before indexed draw call.
420 */
421 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
422
423 /**
424 * Resets vertex source. Drawing from reset vertices is illegal. Set vertex
425 * source to reserved, array, or buffer before next draw. May be able to free
426 * up temporary storage allocated by setVertexSourceToArray or
427 * reserveVertexSpace.
428 */
429 void resetVertexSource();
430
431 /**
432 * Resets index source. Indexed Drawing from reset indices is illegal. Set
433 * index source to reserved, array, or buffer before next indexed draw. May
434 * be able to free up temporary storage allocated by setIndexSourceToArray
435 * or reserveIndexSpace.
436 */
437 void resetIndexSource();
438
439 /**
440 * Pushes and resets the vertex/index sources. Any reserved vertex / index
441 * data is finalized (i.e. cannot be updated after the matching pop but can
442 * be drawn from). Must be balanced by a pop.
443 */
444 void pushGeometrySource();
445
446 /**
447 * Pops the vertex / index sources from the matching push.
448 */
449 void popGeometrySource();
450
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000451protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000452 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
453 switch (type) {
454 case kTriangles_GrPrimitiveType:
455 case kTriangleStrip_GrPrimitiveType:
456 case kTriangleFan_GrPrimitiveType:
457 return kDrawTriangles_DrawType;
458 case kPoints_GrPrimitiveType:
459 return kDrawPoints_DrawType;
460 case kLines_GrPrimitiveType:
461 case kLineStrip_GrPrimitiveType:
462 return kDrawLines_DrawType;
463 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000464 SkFAIL("Unexpected primitive type");
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000465 return kDrawTriangles_DrawType;
466 }
467 }
468
bsalomon@google.comd302f142011-03-03 13:54:13 +0000469 // Functions used to map clip-respecting stencil tests into normal
470 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000471 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000472 GrStencilFunc func);
473 static void ConvertStencilFuncAndMask(GrStencilFunc func,
474 bool clipInStencil,
475 unsigned int clipBit,
476 unsigned int userBits,
477 unsigned int* ref,
478 unsigned int* mask);
479
joshualitt3322fa42014-11-07 08:48:51 -0800480 // subclasses must call this in their destructors to ensure all vertex
481 // and index sources have been released (including those held by
482 // pushGeometrySource())
483 void releaseGeometry();
484
485 // accessors for derived classes
486 const GrDrawTarget::GeometrySrcState& getGeomSrc() const { return fGeoSrcStateStack.back(); }
487
488 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
489 size_t getVertexSize() const {
490 // the vertex layout is only valid if a vertex source has been specified.
491 SkASSERT(this->getGeomSrc().fVertexSrc != GrDrawTarget::kNone_GeometrySrcType);
492 return this->getGeomSrc().fVertexSize;
493 }
494
495 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
496
robertphillips754f4e92014-09-18 13:52:08 -0700497 GrContext::GPUStats fGPUStats;
498
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000499 struct GeometryPoolState {
500 const GrVertexBuffer* fPoolVertexBuffer;
501 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000502
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503 const GrIndexBuffer* fPoolIndexBuffer;
504 int fPoolStartIndex;
505 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000506 const GeometryPoolState& getGeomPoolState() {
507 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000508 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000509
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000510 // Helpers for setting up geometry state
511 void finalizeReservedVertices();
512 void finalizeReservedIndices();
513
kkinnunenccdaa042014-08-20 01:36:23 -0700514 SkAutoTDelete<GrPathRendering> fPathRendering;
515
joshualitt3322fa42014-11-07 08:48:51 -0800516 // Subclass must initialize this in its constructor.
517 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
518
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000519private:
520 // GrDrawTarget overrides
joshualitt3322fa42014-11-07 08:48:51 -0800521 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices);
522 virtual bool onReserveIndexSpace(int indexCount, void** indices);
523 virtual void releaseReservedVertexSpace();
524 virtual void releaseReservedIndexSpace();
525 virtual void geometrySourceWillPush();
526 virtual void geometrySourceWillPop(const GrDrawTarget::GeometrySrcState& restoredState);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000527
bsalomon@google.comb635d392011-11-05 12:47:43 +0000528 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000529 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000530 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000531
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000532 // overridden by backend-specific derived class to create objects.
bsalomonf2703d82014-10-28 14:33:06 -0700533 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000534 const void* srcData,
535 size_t rowBytes) = 0;
bsalomonf2703d82014-10-28 14:33:06 -0700536 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
krajcevski9c0e6292014-06-02 07:38:14 -0700537 const void* srcData) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000538 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
539 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000540 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
541 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000542
bsalomon63b21962014-11-05 07:05:34 -0800543 // overridden by backend-specific derived class to perform the clear.
544 virtual void onGpuClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
545 bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000546
joshualitt6db519c2014-10-29 08:48:18 -0700547
548 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
549 // ONLY used by the the clip target
550 virtual void onClearStencilClip(GrRenderTarget*,
551 const SkIRect& rect,
552 bool insideClip) = 0;
553
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000554 // overridden by backend-specific derived class to perform the draw call.
joshualitt3322fa42014-11-07 08:48:51 -0800555 virtual void onGpuDraw(const GrDrawTarget::DrawInfo&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000556
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000557 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000558 virtual bool onReadPixels(GrRenderTarget* target,
559 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000560 GrPixelConfig,
561 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000562 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000563
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000564 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000565 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000566 int left, int top, int width, int height,
567 GrPixelConfig config, const void* buffer,
568 size_t rowBytes) = 0;
569
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000570 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000571 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
572
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000573 // width and height may be larger than rt (if underlying API allows it).
574 // Should attach the SB to the RT. Returns false if compatible sb could
575 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000576 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000577
578 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000579 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
581 // The GrGpu typically records the clients requested state and then flushes
582 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000583 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 // returns false if current state is unsupported.
joshualitt77b13072014-10-27 14:51:01 -0700585 virtual bool flushGraphicsState(DrawType,
joshualitt6db519c2014-10-29 08:48:18 -0700586 const GrClipMaskManager::ScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700587 const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588
bsalomonb0bd4f62014-09-03 07:19:50 -0700589 // clears target's entire stencil buffer to 0
590 virtual void clearStencil(GrRenderTarget* target) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000591
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000592 // Given a rt, find or create a stencil buffer and attach it
593 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
594
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000595 // GrDrawTarget overrides
joshualitt3322fa42014-11-07 08:48:51 -0800596 virtual void onDraw(const GrDrawTarget::DrawInfo&,
597 const GrClipMaskManager::ScissorState&);
joshualitt2c93efe2014-11-06 12:57:13 -0800598 virtual void onStencilPath(const GrPath*,
599 const GrClipMaskManager::ScissorState&,
joshualitt3322fa42014-11-07 08:48:51 -0800600 const GrStencilSettings&);
joshualitt2c93efe2014-11-06 12:57:13 -0800601 virtual void onDrawPath(const GrPath*,
602 const GrClipMaskManager::ScissorState&,
603 const GrStencilSettings&,
joshualitt3322fa42014-11-07 08:48:51 -0800604 const GrDeviceCoordTexture* dstCopy);
cdaltonb85a0aa2014-07-21 15:32:44 -0700605 virtual void onDrawPaths(const GrPathRange*,
joshualitt2c93efe2014-11-06 12:57:13 -0800606 const uint32_t indices[],
607 int count,
608 const float transforms[],
joshualitt3322fa42014-11-07 08:48:51 -0800609 GrDrawTarget::PathTransformType,
joshualitt2c93efe2014-11-06 12:57:13 -0800610 const GrClipMaskManager::ScissorState&,
611 const GrStencilSettings&,
joshualitt3322fa42014-11-07 08:48:51 -0800612 const GrDeviceCoordTexture*);
613
614 virtual void didAddGpuTraceMarker() = 0;
615 virtual void didRemoveGpuTraceMarker() = 0;
616
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000617
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000618 // readies the pools to provide vertex/index data.
619 void prepareVertexPool();
620 void prepareIndexPool();
621
bsalomon@google.comb635d392011-11-05 12:47:43 +0000622 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000623 this->onResetContext(fResetBits);
624 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000625 ++fResetTimestamp;
626 }
627
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000628 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000629 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000630 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000631 }
632 }
633
joshualitt3322fa42014-11-07 08:48:51 -0800634 // called when setting a new vert/idx source to unref prev vb/ib
635 void releasePreviousVertexSource();
636 void releasePreviousIndexSource();
637
638 enum {
639 kPreallocGeoSrcStateStackCnt = 4,
640 };
641 SkSTArray<kPreallocGeoSrcStateStackCnt, GrDrawTarget::GeometrySrcState, true> fGeoSrcStateStack;
642
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000643 enum {
644 kPreallocGeomPoolStateStackCnt = 4,
645 };
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000646 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000647 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000648 uint32_t fResetBits;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000649 GrVertexBufferAllocPool* fVertexPool;
650 GrIndexBufferAllocPool* fIndexPool;
651 // counts number of uses of vertex/index pool in the geometry stack
652 int fVertexPoolUseCnt;
653 int fIndexPoolUseCnt;
bsalomon@google.com64386952013-02-08 21:22:44 +0000654 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000655 mutable GrIndexBuffer* fQuadIndexBuffer;
joshualitt3322fa42014-11-07 08:48:51 -0800656 GrDrawState fDefaultDrawState;
657 GrDrawState* fDrawState;
658 // To keep track that we always have at least as many debug marker adds as removes
659 int fGpuTraceMarkerCount;
660 GrTraceMarkerSet fActiveTraceMarkers;
661 GrTraceMarkerSet fStoredTraceMarkers;
662 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
663 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000664
joshualitt3322fa42014-11-07 08:48:51 -0800665 // TODO fix this
666 friend class GrInOrderDrawBuffer;
667
668 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000669};
670
671#endif