blob: b10a63f2dff2597bd81a4c623f77cbbdb205d72d [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;
bsalomon04ddf892014-11-19 12:36:22 -080018class GrOptDrawState;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000019class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070020class GrPathRange;
bsalomon@google.com30085192011-08-19 15:42:31 +000021class GrPathRenderer;
22class GrPathRendererChain;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000023class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000024class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
joshualitt3322fa42014-11-07 08:48:51 -080026class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000027public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000028
reed@google.comac10a2d2010-12-22 21:39:39 +000029 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000030 * Additional blend coefficients for dual source blending, not exposed
bsalomon@google.com271cffc2011-05-20 14:13:56 +000031 * through GrPaint/GrContext.
32 */
33 enum ExtendedBlendCoeffs {
34 // source 2 refers to second output color when
35 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000036 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
37 kIS2C_GrBlendCoeff,
38 kS2A_GrBlendCoeff,
39 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000040
bsalomon@google.com47059542012-06-06 20:51:20 +000041 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000042 };
43
44 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000045 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046 * not supported (at compile-time or run-time) this returns NULL. The context will not be
47 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000048 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000049 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000050
51 ////////////////////////////////////////////////////////////////////////////
52
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000053 GrGpu(GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000054 virtual ~GrGpu();
55
joshualitt3322fa42014-11-07 08:48:51 -080056 GrContext* getContext() { return fContext; }
57 const GrContext* getContext() const { return fContext; }
58
59 /**
60 * Gets the capabilities of the draw target.
61 */
62 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
63
kkinnunenccdaa042014-08-20 01:36:23 -070064 GrPathRendering* pathRendering() {
65 return fPathRendering.get();
66 }
67
bsalomonc8dc1f72014-08-21 13:02:13 -070068 // Called by GrContext when the underlying backend context has been destroyed.
69 // GrGpu should use this to ensure that no backend API calls will be made from
70 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070071 // INHERITED::contextAbandoned() if they override this.
72 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070073
reed@google.comac10a2d2010-12-22 21:39:39 +000074 /**
75 * The GrGpu object normally assumes that no outsider is setting state
76 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000077 * the GrGpu that the state was modified and it shouldn't make assumptions
78 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000079 */
bsalomon@google.com0a208a12013-06-28 18:57:35 +000080 void markContextDirty(uint32_t state = kAll_GrBackendState) {
81 fResetBits |= state;
82 }
reed@google.comac10a2d2010-12-22 21:39:39 +000083
84 void unimpl(const char[]);
85
86 /**
bsalomon6d467ec2014-11-18 07:36:19 -080087 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
88 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
89 * pixel configs can be used as render targets. Support for configs as textures
90 * or render targets can be checked using GrDrawTargetCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000091 *
reed@google.comac10a2d2010-12-22 21:39:39 +000092 * @param desc describes the texture to be created.
93 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -070094 * palette data for paletted textures. For compressed
95 * formats it contains the compressed pixel data. Otherwise,
96 * it contains width*height texels. If NULL texture data
97 * is uninitialized.
98 * @param rowBytes the number of bytes between consecutive rows. Zero
99 * means rows are tightly packed. This field is ignored
100 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 *
102 * @return The texture object if successful, otherwise NULL.
103 */
bsalomon6d467ec2014-11-18 07:36:19 -0800104 GrTexture* createTexture(const GrSurfaceDesc& desc, const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000105
bsalomon@google.come269f212011-11-07 13:29:52 +0000106 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000107 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000108 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000109 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000110
111 /**
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 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000115
116 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 * Creates a vertex buffer.
118 *
119 * @param size size in bytes of the vertex buffer
120 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000121 * by either GrVertexBuffer::map() or
122 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000123 *
124 * @return The vertex buffer if successful, otherwise NULL.
125 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000126 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
128 /**
129 * Creates an index buffer.
130 *
131 * @param size size in bytes of the index buffer
132 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000133 * by either GrIndexBuffer::map() or
134 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 *
136 * @return The index buffer if successful, otherwise NULL.
137 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000138 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
140 /**
joshualitt5ead6da2014-10-22 16:00:29 -0700141 * Creates an index buffer for instance drawing with a specific pattern.
142 *
143 * @param pattern the pattern to repeat
144 * @param patternSize size in bytes of the pattern
145 * @param reps number of times to repeat the pattern
146 * @param vertCount number of vertices the pattern references
147 * @param dynamic hints whether the data will be frequently changed
148 * by either GrIndexBuffer::map() or
149 * GrIndexBuffer::updateData().
150 *
151 * @return The index buffer if successful, otherwise NULL.
152 */
153 GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
154 int patternSize,
155 int reps,
156 int vertCount,
157 bool isDynamic = false);
158
159 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000161 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
162 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000163 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000164 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000165 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000166 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
168 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000169 * Resolves MSAA.
170 */
171 void resolveRenderTarget(GrRenderTarget* target);
172
173 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000174 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
175 * config surfaceConfig. The returned config must have at least as many bits per channel as the
176 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000177 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000178 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
179 GrPixelConfig surfaceConfig) const {
180 return readConfig;
181 }
182 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
183 GrPixelConfig surfaceConfig) const {
184 return writeConfig;
185 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000186
187 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000188 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
189 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000190 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000191 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000192
193 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000194 * OpenGL's readPixels returns the result bottom-to-top while the skia
195 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
196 * solution is to have the subclass do the flip using either the CPU or GPU.
197 * However, the caller (GrContext) may have transformations to apply and can
198 * simply fold in the y-flip for free. On the other hand, the subclass may
199 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000200 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000201 * concurrently.
202 *
203 * This function returns true if a y-flip is required to put the pixels in
204 * top-to-bottom order and the subclass cannot do it for free.
205 *
206 * See read pixels for the params
207 * @return true if calling readPixels with the same set of params will
208 * produce bottom-to-top data
209 */
210 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
211 int left, int top,
212 int width, int height,
213 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000214 size_t rowBytes) const = 0;
215 /**
216 * This should return true if reading a NxM rectangle of pixels from a
217 * render target is faster if the target has dimensons N and M and the read
218 * rectangle has its top-left at 0,0.
219 */
220 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000221
222 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000223 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000224 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000225 * @param renderTarget the render target to read from. NULL means the
226 * current render target.
227 * @param left left edge of the rectangle to read (inclusive)
228 * @param top top edge of the rectangle to read (inclusive)
229 * @param width width of rectangle to read in pixels.
230 * @param height height of rectangle to read in pixels.
231 * @param config the pixel config of the destination buffer
232 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000233 * @param rowBytes the number of bytes between consecutive rows. Zero
234 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000235 * @param invertY buffer should be populated bottom-to-top as opposed
236 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000237 *
238 * @return true if the read succeeded, false if not. The read can fail
239 * because of a unsupported pixel config or because no render
240 * target is currently set.
241 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 bool readPixels(GrRenderTarget* renderTarget,
243 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000244 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
bsalomon@google.com6f379512011-11-16 20:36:03 +0000246 /**
247 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000248 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000249 * @param left left edge of the rectangle to write (inclusive)
250 * @param top top edge of the rectangle to write (inclusive)
251 * @param width width of rectangle to write in pixels.
252 * @param height height of rectangle to write in pixels.
253 * @param config the pixel config of the source buffer
254 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000255 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256 * means rows are tightly packed.
257 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000258 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000259 int left, int top, int width, int height,
260 GrPixelConfig config, const void* buffer,
261 size_t rowBytes);
262
joshualitt3322fa42014-11-07 08:48:51 -0800263 /**
264 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
265 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
266 * can be optionally cleared.
267 */
bsalomon6d467ec2014-11-18 07:36:19 -0800268 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800269
270
bsalomon6d467ec2014-11-18 07:36:19 -0800271 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800272
273 /**
274 * Discards the contents render target. NULL indicates that the current render target should
275 * be discarded.
276 **/
277 virtual void discard(GrRenderTarget* = NULL) = 0;
278
279 /**
280 * This is can be called before allocating a texture to be a dst for copySurface. It will
bsalomon8ee4e602014-11-26 10:20:45 -0800281 * populate the origin, config, and flags fields of the desc such that copySurface is more
282 * likely to succeed and be efficient.
joshualitt3322fa42014-11-07 08:48:51 -0800283 */
bsalomon8ee4e602014-11-26 10:20:45 -0800284 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc);
joshualitt6db519c2014-10-29 08:48:18 -0700285
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000286 // After the client interacts directly with the 3D context state the GrGpu
287 // must resync its internal state and assumptions about 3D context state.
288 // Each time this occurs the GrGpu bumps a timestamp.
289 // state of the 3D context
290 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
291 // a billion years.
292 typedef uint64_t ResetTimestamp;
293
294 // This timestamp is always older than the current timestamp
295 static const ResetTimestamp kExpiredTimestamp = 0;
296 // Returns a timestamp based on the number of times the context was reset.
297 // This timestamp can be used to lazily detect when cached 3D context state
298 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800299 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000300
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000301 enum DrawType {
302 kDrawPoints_DrawType,
303 kDrawLines_DrawType,
304 kDrawTriangles_DrawType,
305 kStencilPath_DrawType,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000306 kDrawPath_DrawType,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000307 kDrawPaths_DrawType,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000308 };
309
kkinnunenec56e452014-08-25 22:21:16 -0700310 static bool IsPathRenderingDrawType(DrawType type) {
311 return kDrawPath_DrawType == type || kDrawPaths_DrawType == type;
312 }
313
robertphillips754f4e92014-09-18 13:52:08 -0700314 GrContext::GPUStats* gpuStats() { return &fGPUStats; }
315
joshualitt79f8fae2014-10-28 17:59:26 -0700316 virtual void buildProgramDesc(const GrOptDrawState&,
317 const GrProgramDesc::DescInfo&,
318 GrGpu::DrawType,
joshualitt79f8fae2014-10-28 17:59:26 -0700319 GrProgramDesc*) = 0;
320
joshualitt3322fa42014-11-07 08:48:51 -0800321 /**
322 * Called at start and end of gpu trace marking
323 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
324 * and end of a code block respectively
325 */
326 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
327 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
328
329 /**
330 * Takes the current active set of markers and stores them for later use. Any current marker
331 * in the active set is removed from the active set and the targets remove function is called.
332 * These functions do not work as a stack so you cannot call save a second time before calling
333 * restore. Also, it is assumed that when restore is called the current active set of markers
334 * is empty. When the stored markers are added back into the active set, the targets add marker
335 * is called.
336 */
337 void saveActiveTraceMarkers();
338 void restoreActiveTraceMarkers();
339
bsalomon8ee4e602014-11-26 10:20:45 -0800340 // Called to determine whether an onCopySurface call would succeed or not. This is useful for
341 // proxy subclasses to test whether the copy would succeed without executing it yet. Derived
342 // classes must keep this consistent with their implementation of onCopySurface(). The inputs
343 // are the same as onCopySurface(), i.e. srcRect and dstPoint are clipped to be inside the src
344 // and dst bounds.
joshualitt9853cce2014-11-17 14:22:48 -0800345 virtual bool canCopySurface(const GrSurface* dst,
346 const GrSurface* src,
joshualitt3322fa42014-11-07 08:48:51 -0800347 const SkIRect& srcRect,
348 const SkIPoint& dstPoint) = 0;
349
bsalomon8ee4e602014-11-26 10:20:45 -0800350 // This method is called by copySurface The srcRect is guaranteed to be entirely within the
351 // src bounds. Likewise, the dst rect implied by dstPoint and srcRect's width and height falls
352 // entirely within the dst. The default implementation will draw a rect from the src to the
353 // dst if the src is a texture and the dst is a render target and fail otherwise.
joshualitt3322fa42014-11-07 08:48:51 -0800354 virtual bool copySurface(GrSurface* dst,
355 GrSurface* src,
356 const SkIRect& srcRect,
357 const SkIPoint& dstPoint) = 0;
358
joshualitt54e0c122014-11-19 09:38:51 -0800359 virtual void draw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&);
joshualittd53a8272014-11-10 16:03:14 -0800360 virtual void stencilPath(const GrOptDrawState&,
361 const GrPath*,
joshualitt4b68ec02014-11-07 14:11:45 -0800362 const GrStencilSettings&);
joshualittd53a8272014-11-10 16:03:14 -0800363 virtual void drawPath(const GrOptDrawState&,
364 const GrPath*,
joshualitt9176e2c2014-11-20 07:28:52 -0800365 const GrStencilSettings&);
joshualittd53a8272014-11-10 16:03:14 -0800366 virtual void drawPaths(const GrOptDrawState&,
367 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800368 const void* indices,
369 GrDrawTarget::PathIndexType,
370 const float transformValues[],
joshualitt4b68ec02014-11-07 14:11:45 -0800371 GrDrawTarget::PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800372 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800373 const GrStencilSettings&);
joshualitt4b68ec02014-11-07 14:11:45 -0800374
joshualittd53a8272014-11-10 16:03:14 -0800375 static DrawType PrimTypeToDrawType(GrPrimitiveType type) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000376 switch (type) {
377 case kTriangles_GrPrimitiveType:
378 case kTriangleStrip_GrPrimitiveType:
379 case kTriangleFan_GrPrimitiveType:
380 return kDrawTriangles_DrawType;
381 case kPoints_GrPrimitiveType:
382 return kDrawPoints_DrawType;
383 case kLines_GrPrimitiveType:
384 case kLineStrip_GrPrimitiveType:
385 return kDrawLines_DrawType;
386 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000387 SkFAIL("Unexpected primitive type");
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000388 return kDrawTriangles_DrawType;
389 }
390 }
391
joshualittd53a8272014-11-10 16:03:14 -0800392protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000393 // Functions used to map clip-respecting stencil tests into normal
394 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000395 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000396 GrStencilFunc func);
397 static void ConvertStencilFuncAndMask(GrStencilFunc func,
398 bool clipInStencil,
399 unsigned int clipBit,
400 unsigned int userBits,
401 unsigned int* ref,
402 unsigned int* mask);
403
joshualitt3322fa42014-11-07 08:48:51 -0800404 const GrTraceMarkerSet& getActiveTraceMarkers() { return fActiveTraceMarkers; }
405
robertphillips754f4e92014-09-18 13:52:08 -0700406 GrContext::GPUStats fGPUStats;
407
kkinnunenccdaa042014-08-20 01:36:23 -0700408 SkAutoTDelete<GrPathRendering> fPathRendering;
409
joshualitt3322fa42014-11-07 08:48:51 -0800410 // Subclass must initialize this in its constructor.
411 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
412
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000413private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000414 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000415 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000416 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000417
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000418 // overridden by backend-specific derived class to create objects.
bsalomonf2703d82014-10-28 14:33:06 -0700419 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000420 const void* srcData,
421 size_t rowBytes) = 0;
bsalomonf2703d82014-10-28 14:33:06 -0700422 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
krajcevski9c0e6292014-06-02 07:38:14 -0700423 const void* srcData) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000424 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
425 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000426 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
427 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428
bsalomon63b21962014-11-05 07:05:34 -0800429 // overridden by backend-specific derived class to perform the clear.
joshualitt4b68ec02014-11-07 14:11:45 -0800430 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
431 bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000432
joshualitt6db519c2014-10-29 08:48:18 -0700433
434 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
435 // ONLY used by the the clip target
436 virtual void onClearStencilClip(GrRenderTarget*,
437 const SkIRect& rect,
438 bool insideClip) = 0;
439
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000440 // overridden by backend-specific derived class to perform the draw call.
joshualittd53a8272014-11-10 16:03:14 -0800441 virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000442
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000443 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000444 virtual bool onReadPixels(GrRenderTarget* target,
445 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000446 GrPixelConfig,
447 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000448 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000449
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000450 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000451 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000452 int left, int top, int width, int height,
453 GrPixelConfig config, const void* buffer,
454 size_t rowBytes) = 0;
455
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000456 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000457 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
458
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000459 // width and height may be larger than rt (if underlying API allows it).
460 // Should attach the SB to the RT. Returns false if compatible sb could
461 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000462 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000463
464 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000465 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
467 // The GrGpu typically records the clients requested state and then flushes
468 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000469 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 // returns false if current state is unsupported.
joshualitt9176e2c2014-11-20 07:28:52 -0800471 virtual bool flushGraphicsState(const GrOptDrawState&, DrawType) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000472
bsalomonb0bd4f62014-09-03 07:19:50 -0700473 // clears target's entire stencil buffer to 0
474 virtual void clearStencil(GrRenderTarget* target) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000476 // Given a rt, find or create a stencil buffer and attach it
477 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
478
joshualitt3322fa42014-11-07 08:48:51 -0800479 virtual void didAddGpuTraceMarker() = 0;
480 virtual void didRemoveGpuTraceMarker() = 0;
481
bsalomon@google.comb635d392011-11-05 12:47:43 +0000482 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000483 this->onResetContext(fResetBits);
484 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000485 ++fResetTimestamp;
486 }
487
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000488 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000489 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000490 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000491 }
492 }
493
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000494 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000495 uint32_t fResetBits;
bsalomon@google.com64386952013-02-08 21:22:44 +0000496 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000497 mutable GrIndexBuffer* fQuadIndexBuffer;
joshualitt3322fa42014-11-07 08:48:51 -0800498 // To keep track that we always have at least as many debug marker adds as removes
499 int fGpuTraceMarkerCount;
500 GrTraceMarkerSet fActiveTraceMarkers;
501 GrTraceMarkerSet fStoredTraceMarkers;
502 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
503 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000504
joshualitt3322fa42014-11-07 08:48:51 -0800505 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000506};
507
508#endif