blob: dfc71fe9709df5cd39c3d7c13c939c43a8e3b28b [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
kkinnunenccdaa042014-08-20 01:36:23 -070063 GrPathRendering* pathRendering() {
64 return fPathRendering.get();
65 }
66
bsalomonc8dc1f72014-08-21 13:02:13 -070067 // Called by GrContext when the underlying backend context has been destroyed.
68 // GrGpu should use this to ensure that no backend API calls will be made from
69 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070070 // INHERITED::contextAbandoned() if they override this.
71 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070072
reed@google.comac10a2d2010-12-22 21:39:39 +000073 /**
74 * The GrGpu object normally assumes that no outsider is setting state
75 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000076 * the GrGpu that the state was modified and it shouldn't make assumptions
77 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000078 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000079 void markContextDirty(uint32_t state = kAll_GrBackendState) {
80 fResetBits |= state;
81 }
reed@google.comac10a2d2010-12-22 21:39:39 +000082
83 void unimpl(const char[]);
84
85 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000086 * Creates a texture object. If desc width or height is not a power of
87 * two but underlying API requires a power of two texture then srcData
88 * will be embedded in a power of two texture. The extra width and height
89 * is filled as though srcData were rendered clamped into the texture.
krajcevski9c0e6292014-06-02 07:38:14 -070090 * The exception is when using compressed data formats. In this case, the
91 * desc width and height must be a multiple of the compressed format block
92 * size otherwise this function returns NULL. Similarly, if the underlying
93 * API requires a power of two texture and the source width and height are not
94 * a power of two, then this function returns NULL.
reed@google.comac10a2d2010-12-22 21:39:39 +000095 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000096 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000097 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
krajcevski9c0e6292014-06-02 07:38:14 -070098 * on the render target until the texture is destroyed. Compressed textures
99 * cannot have the kRenderTarget_TextureFlag set.
bsalomon@google.com1da07462011-03-10 14:51:57 +0000100 *
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 * @param desc describes the texture to be created.
102 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -0700103 * palette data for paletted textures. For compressed
104 * formats it contains the compressed pixel data. Otherwise,
105 * it contains width*height texels. If NULL texture data
106 * is uninitialized.
107 * @param rowBytes the number of bytes between consecutive rows. Zero
108 * means rows are tightly packed. This field is ignored
109 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 *
111 * @return The texture object if successful, otherwise NULL.
112 */
bsalomonf2703d82014-10-28 14:33:06 -0700113 GrTexture* createTexture(const GrSurfaceDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000114 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000115
bsalomon@google.come269f212011-11-07 13:29:52 +0000116 /**
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 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000120
121 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000122 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000123 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000124 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000125
126 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 * Creates a vertex buffer.
128 *
129 * @param size size in bytes of the vertex buffer
130 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000131 * by either GrVertexBuffer::map() or
132 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 *
134 * @return The vertex buffer if successful, otherwise NULL.
135 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000136 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
138 /**
139 * Creates an index buffer.
140 *
141 * @param size size in bytes of the index buffer
142 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000143 * by either GrIndexBuffer::map() or
144 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 *
146 * @return The index buffer if successful, otherwise NULL.
147 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000148 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000149
150 /**
joshualitt5ead6da2014-10-22 16:00:29 -0700151 * Creates an index buffer for instance drawing with a specific pattern.
152 *
153 * @param pattern the pattern to repeat
154 * @param patternSize size in bytes of the pattern
155 * @param reps number of times to repeat the pattern
156 * @param vertCount number of vertices the pattern references
157 * @param dynamic hints whether the data will be frequently changed
158 * by either GrIndexBuffer::map() or
159 * GrIndexBuffer::updateData().
160 *
161 * @return The index buffer if successful, otherwise NULL.
162 */
163 GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
164 int patternSize,
165 int reps,
166 int vertCount,
167 bool isDynamic = false);
168
169 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000171 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
172 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000173 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000174 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000175 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000176 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
178 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000179 * Resolves MSAA.
180 */
181 void resolveRenderTarget(GrRenderTarget* target);
182
183 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000184 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
185 * config surfaceConfig. The returned config must have at least as many bits per channel as the
186 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000187 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000188 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
189 GrPixelConfig surfaceConfig) const {
190 return readConfig;
191 }
192 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
193 GrPixelConfig surfaceConfig) const {
194 return writeConfig;
195 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000196
197 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000198 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
199 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000200 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000201 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000202
203 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000204 * OpenGL's readPixels returns the result bottom-to-top while the skia
205 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
206 * solution is to have the subclass do the flip using either the CPU or GPU.
207 * However, the caller (GrContext) may have transformations to apply and can
208 * simply fold in the y-flip for free. On the other hand, the subclass may
209 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000210 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000211 * concurrently.
212 *
213 * This function returns true if a y-flip is required to put the pixels in
214 * top-to-bottom order and the subclass cannot do it for free.
215 *
216 * See read pixels for the params
217 * @return true if calling readPixels with the same set of params will
218 * produce bottom-to-top data
219 */
220 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
221 int left, int top,
222 int width, int height,
223 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000224 size_t rowBytes) const = 0;
225 /**
226 * This should return true if reading a NxM rectangle of pixels from a
227 * render target is faster if the target has dimensons N and M and the read
228 * rectangle has its top-left at 0,0.
229 */
230 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000231
232 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000233 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000235 * @param renderTarget the render target to read from. NULL means the
236 * current render target.
237 * @param left left edge of the rectangle to read (inclusive)
238 * @param top top edge of the rectangle to read (inclusive)
239 * @param width width of rectangle to read in pixels.
240 * @param height height of rectangle to read in pixels.
241 * @param config the pixel config of the destination buffer
242 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000243 * @param rowBytes the number of bytes between consecutive rows. Zero
244 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000245 * @param invertY buffer should be populated bottom-to-top as opposed
246 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000247 *
248 * @return true if the read succeeded, false if not. The read can fail
249 * because of a unsupported pixel config or because no render
250 * target is currently set.
251 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000252 bool readPixels(GrRenderTarget* renderTarget,
253 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000254 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256 /**
257 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000258 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000259 * @param left left edge of the rectangle to write (inclusive)
260 * @param top top edge of the rectangle to write (inclusive)
261 * @param width width of rectangle to write in pixels.
262 * @param height height of rectangle to write in pixels.
263 * @param config the pixel config of the source buffer
264 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000265 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000266 * means rows are tightly packed.
267 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000268 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000269 int left, int top, int width, int height,
270 GrPixelConfig config, const void* buffer,
271 size_t rowBytes);
272
joshualitt3322fa42014-11-07 08:48:51 -0800273 /**
274 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
275 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
276 * can be optionally cleared.
277 */
278 void clear(const SkIRect* rect,
279 GrColor color,
280 bool canIgnoreRect,
281 GrRenderTarget* renderTarget);
282
283
284 void clearStencilClip(const SkIRect& rect,
285 bool insideClip,
joshualittd53a8272014-11-10 16:03:14 -0800286 GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800287
288 /**
289 * Discards the contents render target. NULL indicates that the current render target should
290 * be discarded.
291 **/
292 virtual void discard(GrRenderTarget* = NULL) = 0;
293
294 /**
295 * This is can be called before allocating a texture to be a dst for copySurface. It will
296 * populate the origin, config, and flags fields of the desc such that copySurface is more
297 * likely to succeed and be efficient.
298 */
299 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
joshualitt6db519c2014-10-29 08:48:18 -0700300
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000301 // After the client interacts directly with the 3D context state the GrGpu
302 // must resync its internal state and assumptions about 3D context state.
303 // Each time this occurs the GrGpu bumps a timestamp.
304 // state of the 3D context
305 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
306 // a billion years.
307 typedef uint64_t ResetTimestamp;
308
309 // This timestamp is always older than the current timestamp
310 static const ResetTimestamp kExpiredTimestamp = 0;
311 // Returns a timestamp based on the number of times the context was reset.
312 // This timestamp can be used to lazily detect when cached 3D context state
313 // is dirty.
314 ResetTimestamp getResetTimestamp() const {
315 return fResetTimestamp;
316 }
317
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000318 enum DrawType {
319 kDrawPoints_DrawType,
320 kDrawLines_DrawType,
321 kDrawTriangles_DrawType,
322 kStencilPath_DrawType,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000323 kDrawPath_DrawType,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000324 kDrawPaths_DrawType,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000325 };
326
kkinnunenec56e452014-08-25 22:21:16 -0700327 static bool IsPathRenderingDrawType(DrawType type) {
328 return kDrawPath_DrawType == type || kDrawPaths_DrawType == type;
329 }
330
robertphillips754f4e92014-09-18 13:52:08 -0700331 GrContext::GPUStats* gpuStats() { return &fGPUStats; }
332
joshualitt79f8fae2014-10-28 17:59:26 -0700333 virtual void buildProgramDesc(const GrOptDrawState&,
334 const GrProgramDesc::DescInfo&,
335 GrGpu::DrawType,
336 const GrDeviceCoordTexture* dstCopy,
337 GrProgramDesc*) = 0;
338
joshualitt3322fa42014-11-07 08:48:51 -0800339 /**
340 * Called at start and end of gpu trace marking
341 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
342 * and end of a code block respectively
343 */
344 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
345 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
346
347 /**
348 * Takes the current active set of markers and stores them for later use. Any current marker
349 * in the active set is removed from the active set and the targets remove function is called.
350 * These functions do not work as a stack so you cannot call save a second time before calling
351 * restore. Also, it is assumed that when restore is called the current active set of markers
352 * is empty. When the stored markers are added back into the active set, the targets add marker
353 * is called.
354 */
355 void saveActiveTraceMarkers();
356 void restoreActiveTraceMarkers();
357
joshualitt3322fa42014-11-07 08:48:51 -0800358 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
359 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
360 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
361 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
362 // and dst bounds.
363 virtual bool canCopySurface(GrSurface* dst,
364 GrSurface* src,
365 const SkIRect& srcRect,
366 const SkIPoint& dstPoint) = 0;
367
368 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
369 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
370 // entirely within the dst. The default implementation will draw a rect from the src to the
371 // dst if the src is a texture and the dst is a render target and fail otherwise.
372 virtual bool copySurface(GrSurface* dst,
373 GrSurface* src,
374 const SkIRect& srcRect,
375 const SkIPoint& dstPoint) = 0;
376
377 /**
378 * Sets source of vertex data for the next draw. Data does not have to be
379 * in the buffer until drawIndexed, drawNonIndexed, or drawIndexedInstances.
380 *
381 * @param buffer vertex buffer containing vertex data. Must be
382 * unlocked before draw call. Vertex size is queried
383 * from current GrDrawState.
384 */
joshualittd53a8272014-11-10 16:03:14 -0800385 void setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride);
joshualitt3322fa42014-11-07 08:48:51 -0800386
387 /**
388 * Sets source of index data for the next indexed draw. Data does not have
389 * to be in the buffer until drawIndexed.
390 *
391 * @param buffer index buffer containing indices. Must be unlocked
392 * before indexed draw call.
393 */
394 void setIndexSourceToBuffer(const GrIndexBuffer* buffer);
395
joshualittd53a8272014-11-10 16:03:14 -0800396 virtual void draw(const GrOptDrawState&,
397 const GrDrawTarget::DrawInfo&,
joshualitt4b68ec02014-11-07 14:11:45 -0800398 const GrClipMaskManager::ScissorState&);
joshualittd53a8272014-11-10 16:03:14 -0800399 virtual void stencilPath(const GrOptDrawState&,
400 const GrPath*,
joshualitt4b68ec02014-11-07 14:11:45 -0800401 const GrClipMaskManager::ScissorState&,
402 const GrStencilSettings&);
joshualittd53a8272014-11-10 16:03:14 -0800403 virtual void drawPath(const GrOptDrawState&,
404 const GrPath*,
joshualitt4b68ec02014-11-07 14:11:45 -0800405 const GrClipMaskManager::ScissorState&,
406 const GrStencilSettings&,
407 const GrDeviceCoordTexture* dstCopy);
joshualittd53a8272014-11-10 16:03:14 -0800408 virtual void drawPaths(const GrOptDrawState&,
409 const GrPathRange*,
joshualitt4b68ec02014-11-07 14:11:45 -0800410 const uint32_t indices[],
411 int count,
412 const float transforms[],
413 GrDrawTarget::PathTransformType,
414 const GrClipMaskManager::ScissorState&,
415 const GrStencilSettings&,
416 const GrDeviceCoordTexture*);
417
joshualittd53a8272014-11-10 16:03:14 -0800418 static DrawType PrimTypeToDrawType(GrPrimitiveType type) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000419 switch (type) {
420 case kTriangles_GrPrimitiveType:
421 case kTriangleStrip_GrPrimitiveType:
422 case kTriangleFan_GrPrimitiveType:
423 return kDrawTriangles_DrawType;
424 case kPoints_GrPrimitiveType:
425 return kDrawPoints_DrawType;
426 case kLines_GrPrimitiveType:
427 case kLineStrip_GrPrimitiveType:
428 return kDrawLines_DrawType;
429 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000430 SkFAIL("Unexpected primitive type");
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000431 return kDrawTriangles_DrawType;
432 }
433 }
434
joshualittd53a8272014-11-10 16:03:14 -0800435protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000436 // Functions used to map clip-respecting stencil tests into normal
437 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000439 GrStencilFunc func);
440 static void ConvertStencilFuncAndMask(GrStencilFunc func,
441 bool clipInStencil,
442 unsigned int clipBit,
443 unsigned int userBits,
444 unsigned int* ref,
445 unsigned int* mask);
446
joshualittf4e5e332014-11-07 12:58:46 -0800447 struct GeometrySrcState {
448 GeometrySrcState() : fVertexBuffer(NULL), fIndexBuffer(NULL), fVertexSize(0) {}
449 const GrVertexBuffer* fVertexBuffer;
450 const GrIndexBuffer* fIndexBuffer;
451 size_t fVertexSize;
452 };
joshualitt3322fa42014-11-07 08:48:51 -0800453
454 // accessors for derived classes
joshualittf4e5e332014-11-07 12:58:46 -0800455 const GeometrySrcState& getGeomSrc() const { return fGeoSrcState; }
joshualitt3322fa42014-11-07 08:48:51 -0800456
457 // it is preferable to call this rather than getGeomSrc()->fVertexSize because of the assert.
458 size_t getVertexSize() const {
459 // the vertex layout is only valid if a vertex source has been specified.
joshualittf4e5e332014-11-07 12:58:46 -0800460 SkASSERT(this->getGeomSrc().fVertexBuffer);
joshualitt3322fa42014-11-07 08:48:51 -0800461 return this->getGeomSrc().fVertexSize;
462 }
463
464 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
465
robertphillips754f4e92014-09-18 13:52:08 -0700466 GrContext::GPUStats fGPUStats;
467
kkinnunenccdaa042014-08-20 01:36:23 -0700468 SkAutoTDelete<GrPathRendering> fPathRendering;
469
joshualitt3322fa42014-11-07 08:48:51 -0800470 // Subclass must initialize this in its constructor.
471 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
472
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000473private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000474 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000475 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000476 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000477
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000478 // overridden by backend-specific derived class to create objects.
bsalomonf2703d82014-10-28 14:33:06 -0700479 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000480 const void* srcData,
481 size_t rowBytes) = 0;
bsalomonf2703d82014-10-28 14:33:06 -0700482 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
krajcevski9c0e6292014-06-02 07:38:14 -0700483 const void* srcData) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000484 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
485 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000486 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
487 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000488
bsalomon63b21962014-11-05 07:05:34 -0800489 // overridden by backend-specific derived class to perform the clear.
joshualitt4b68ec02014-11-07 14:11:45 -0800490 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
491 bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000492
joshualitt6db519c2014-10-29 08:48:18 -0700493
494 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
495 // ONLY used by the the clip target
496 virtual void onClearStencilClip(GrRenderTarget*,
497 const SkIRect& rect,
498 bool insideClip) = 0;
499
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000500 // overridden by backend-specific derived class to perform the draw call.
joshualittd53a8272014-11-10 16:03:14 -0800501 virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000502
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000503 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000504 virtual bool onReadPixels(GrRenderTarget* target,
505 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000506 GrPixelConfig,
507 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000508 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000509
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000510 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000511 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000512 int left, int top, int width, int height,
513 GrPixelConfig config, const void* buffer,
514 size_t rowBytes) = 0;
515
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000516 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000517 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
518
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000519 // width and height may be larger than rt (if underlying API allows it).
520 // Should attach the SB to the RT. Returns false if compatible sb could
521 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000522 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000523
524 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000525 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526
527 // The GrGpu typically records the clients requested state and then flushes
528 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000529 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 // returns false if current state is unsupported.
joshualittd53a8272014-11-10 16:03:14 -0800531 virtual bool flushGraphicsState(const GrOptDrawState&,
532 DrawType,
joshualitt6db519c2014-10-29 08:48:18 -0700533 const GrClipMaskManager::ScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700534 const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000535
bsalomonb0bd4f62014-09-03 07:19:50 -0700536 // clears target's entire stencil buffer to 0
537 virtual void clearStencil(GrRenderTarget* target) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000539 // Given a rt, find or create a stencil buffer and attach it
540 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
541
joshualitt3322fa42014-11-07 08:48:51 -0800542 virtual void didAddGpuTraceMarker() = 0;
543 virtual void didRemoveGpuTraceMarker() = 0;
544
bsalomon@google.comb635d392011-11-05 12:47:43 +0000545 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000546 this->onResetContext(fResetBits);
547 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000548 ++fResetTimestamp;
549 }
550
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000551 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000552 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000553 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000554 }
555 }
556
joshualittf4e5e332014-11-07 12:58:46 -0800557 GeometrySrcState fGeoSrcState;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000558 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000559 uint32_t fResetBits;
bsalomon@google.com64386952013-02-08 21:22:44 +0000560 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000561 mutable GrIndexBuffer* fQuadIndexBuffer;
joshualitt3322fa42014-11-07 08:48:51 -0800562 // To keep track that we always have at least as many debug marker adds as removes
563 int fGpuTraceMarkerCount;
564 GrTraceMarkerSet fActiveTraceMarkers;
565 GrTraceMarkerSet fStoredTraceMarkers;
566 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
567 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000568
joshualitt3322fa42014-11-07 08:48:51 -0800569 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570};
571
572#endif