blob: 5784781996b815ab8c0e1e12ace465e563f44232 [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;
egdaniel8dd688b2015-01-22 10:16:09 -080022class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080023class GrPrimitiveProcessor;
egdaniel8dc7c3a2015-04-16 11:22:42 -070024class GrStencilAttachment;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000025class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000026
joshualitt3322fa42014-11-07 08:48:51 -080027class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000028public:
29 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000030 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000031 * not supported (at compile-time or run-time) this returns NULL. The context will not be
32 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000033 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000034 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000035
36 ////////////////////////////////////////////////////////////////////////////
37
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000038 GrGpu(GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070039 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000040
joshualitt3322fa42014-11-07 08:48:51 -080041 GrContext* getContext() { return fContext; }
42 const GrContext* getContext() const { return fContext; }
43
44 /**
45 * Gets the capabilities of the draw target.
46 */
47 const GrDrawTargetCaps* caps() const { return fCaps.get(); }
48
mtkleinb9eb4ac2015-02-02 18:26:03 -080049 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070050
bsalomonc8dc1f72014-08-21 13:02:13 -070051 // Called by GrContext when the underlying backend context has been destroyed.
52 // GrGpu should use this to ensure that no backend API calls will be made from
53 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070054 // INHERITED::contextAbandoned() if they override this.
55 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070056
reed@google.comac10a2d2010-12-22 21:39:39 +000057 /**
58 * The GrGpu object normally assumes that no outsider is setting state
59 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000060 * the GrGpu that the state was modified and it shouldn't make assumptions
61 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000062 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080063 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000064
65 /**
bsalomon6d467ec2014-11-18 07:36:19 -080066 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
67 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
68 * pixel configs can be used as render targets. Support for configs as textures
69 * or render targets can be checked using GrDrawTargetCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000070 *
reed@google.comac10a2d2010-12-22 21:39:39 +000071 * @param desc describes the texture to be created.
bsalomon5236cf42015-01-14 10:42:08 -080072 * @param budgeted does this texture count against the resource cache budget?
reed@google.comac10a2d2010-12-22 21:39:39 +000073 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -070074 * palette data for paletted textures. For compressed
75 * formats it contains the compressed pixel data. Otherwise,
76 * it contains width*height texels. If NULL texture data
77 * is uninitialized.
78 * @param rowBytes the number of bytes between consecutive rows. Zero
79 * means rows are tightly packed. This field is ignored
80 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +000081 *
82 * @return The texture object if successful, otherwise NULL.
83 */
bsalomon5236cf42015-01-14 10:42:08 -080084 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted,
85 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.come269f212011-11-07 13:29:52 +000087 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000088 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000089 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000090 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000091
92 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000093 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000094 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000095 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000096
97 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000098 * Creates a vertex buffer.
99 *
100 * @param size size in bytes of the vertex buffer
101 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000102 * by either GrVertexBuffer::map() or
103 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 *
105 * @return The vertex buffer if successful, otherwise NULL.
106 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000107 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
109 /**
110 * Creates an index buffer.
111 *
112 * @param size size in bytes of the index buffer
113 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000114 * by either GrIndexBuffer::map() or
115 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 *
117 * @return The index buffer if successful, otherwise NULL.
118 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000119 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
121 /**
joshualitt5ead6da2014-10-22 16:00:29 -0700122 * Creates an index buffer for instance drawing with a specific pattern.
123 *
124 * @param pattern the pattern to repeat
125 * @param patternSize size in bytes of the pattern
126 * @param reps number of times to repeat the pattern
127 * @param vertCount number of vertices the pattern references
128 * @param dynamic hints whether the data will be frequently changed
129 * by either GrIndexBuffer::map() or
130 * GrIndexBuffer::updateData().
131 *
132 * @return The index buffer if successful, otherwise NULL.
133 */
134 GrIndexBuffer* createInstancedIndexBuffer(const uint16_t* pattern,
135 int patternSize,
136 int reps,
137 int vertCount,
138 bool isDynamic = false);
139
140 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000142 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
143 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000144 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000145 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000147 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000148
149 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000150 * Resolves MSAA.
151 */
152 void resolveRenderTarget(GrRenderTarget* target);
153
154 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000155 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
156 * config surfaceConfig. The returned config must have at least as many bits per channel as the
157 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000158 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000159 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
160 GrPixelConfig surfaceConfig) const {
161 return readConfig;
162 }
163 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
164 GrPixelConfig surfaceConfig) const {
165 return writeConfig;
166 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000167
168 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000169 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
170 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000171 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000172 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000173
174 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000175 * OpenGL's readPixels returns the result bottom-to-top while the skia
176 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
177 * solution is to have the subclass do the flip using either the CPU or GPU.
178 * However, the caller (GrContext) may have transformations to apply and can
179 * simply fold in the y-flip for free. On the other hand, the subclass may
180 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000182 * concurrently.
183 *
184 * This function returns true if a y-flip is required to put the pixels in
185 * top-to-bottom order and the subclass cannot do it for free.
186 *
187 * See read pixels for the params
188 * @return true if calling readPixels with the same set of params will
189 * produce bottom-to-top data
190 */
191 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
192 int left, int top,
193 int width, int height,
194 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000195 size_t rowBytes) const = 0;
196 /**
197 * This should return true if reading a NxM rectangle of pixels from a
198 * render target is faster if the target has dimensons N and M and the read
199 * rectangle has its top-left at 0,0.
200 */
201 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000202
203 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000204 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000205 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000206 * @param renderTarget the render target to read from. NULL means the
207 * current render target.
208 * @param left left edge of the rectangle to read (inclusive)
209 * @param top top edge of the rectangle to read (inclusive)
210 * @param width width of rectangle to read in pixels.
211 * @param height height of rectangle to read in pixels.
212 * @param config the pixel config of the destination buffer
213 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000214 * @param rowBytes the number of bytes between consecutive rows. Zero
215 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000216 * @param invertY buffer should be populated bottom-to-top as opposed
217 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000218 *
219 * @return true if the read succeeded, false if not. The read can fail
220 * because of a unsupported pixel config or because no render
221 * target is currently set.
222 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000223 bool readPixels(GrRenderTarget* renderTarget,
224 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000225 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
bsalomon@google.com6f379512011-11-16 20:36:03 +0000227 /**
228 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000229 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000230 * @param left left edge of the rectangle to write (inclusive)
231 * @param top top edge of the rectangle to write (inclusive)
232 * @param width width of rectangle to write in pixels.
233 * @param height height of rectangle to write in pixels.
234 * @param config the pixel config of the source buffer
235 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000236 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000237 * means rows are tightly packed.
238 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000239 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000240 int left, int top, int width, int height,
241 GrPixelConfig config, const void* buffer,
242 size_t rowBytes);
243
joshualitt3322fa42014-11-07 08:48:51 -0800244 /**
245 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
246 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
247 * can be optionally cleared.
248 */
bsalomon6d467ec2014-11-18 07:36:19 -0800249 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800250
251
bsalomon6d467ec2014-11-18 07:36:19 -0800252 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800253
254 /**
255 * Discards the contents render target. NULL indicates that the current render target should
256 * be discarded.
257 **/
258 virtual void discard(GrRenderTarget* = NULL) = 0;
259
260 /**
261 * This is can be called before allocating a texture to be a dst for copySurface. It will
bsalomonf90a02b2014-11-26 12:28:00 -0800262 * populate the origin, config, and flags fields of the desc such that copySurface can
263 * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy
264 * that would be more effecient than drawing the src to a dst render target.
joshualitt3322fa42014-11-07 08:48:51 -0800265 */
bsalomonf90a02b2014-11-26 12:28:00 -0800266 virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700267
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000268 // After the client interacts directly with the 3D context state the GrGpu
269 // must resync its internal state and assumptions about 3D context state.
270 // Each time this occurs the GrGpu bumps a timestamp.
271 // state of the 3D context
272 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
273 // a billion years.
274 typedef uint64_t ResetTimestamp;
275
276 // This timestamp is always older than the current timestamp
277 static const ResetTimestamp kExpiredTimestamp = 0;
278 // Returns a timestamp based on the number of times the context was reset.
279 // This timestamp can be used to lazily detect when cached 3D context state
280 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800281 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000282
joshualitt873ad0e2015-01-20 09:08:51 -0800283 virtual void buildProgramDesc(GrProgramDesc*,
284 const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800285 const GrPipeline&,
joshualitt873ad0e2015-01-20 09:08:51 -0800286 const GrBatchTracker&) const = 0;
joshualitt79f8fae2014-10-28 17:59:26 -0700287
bsalomonf90a02b2014-11-26 12:28:00 -0800288 // Called to determine whether a copySurface call would succeed or not. Derived
289 // classes must keep this consistent with their implementation of onCopySurface(). Fallbacks
290 // to issuing a draw from the src to dst take place at the GrDrawTarget level and this function
291 // should only return true if a faster copy path exists. The rect and point are pre-clipped. The
292 // src rect and implied dst rect are guaranteed to be within the src/dst bounds and non-empty.
joshualitt9853cce2014-11-17 14:22:48 -0800293 virtual bool canCopySurface(const GrSurface* dst,
294 const GrSurface* src,
joshualitt3322fa42014-11-07 08:48:51 -0800295 const SkIRect& srcRect,
296 const SkIPoint& dstPoint) = 0;
297
bsalomonf90a02b2014-11-26 12:28:00 -0800298 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
299 // take place at the GrDrawTarget level and this function implement faster copy paths. The rect
300 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
301 // src/dst bounds and non-empty.
joshualitt3322fa42014-11-07 08:48:51 -0800302 virtual bool copySurface(GrSurface* dst,
303 GrSurface* src,
304 const SkIRect& srcRect,
305 const SkIPoint& dstPoint) = 0;
306
joshualitt873ad0e2015-01-20 09:08:51 -0800307 struct DrawArgs {
308 typedef GrDrawTarget::DrawInfo DrawInfo;
309 DrawArgs(const GrPrimitiveProcessor* primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800310 const GrPipeline* pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -0800311 const GrProgramDesc* desc,
joshualitt17e73142015-01-21 11:52:36 -0800312 const GrBatchTracker* batchTracker)
joshualitt873ad0e2015-01-20 09:08:51 -0800313 : fPrimitiveProcessor(primProc)
egdaniel8dd688b2015-01-22 10:16:09 -0800314 , fPipeline(pipeline)
joshualitt873ad0e2015-01-20 09:08:51 -0800315 , fDesc(desc)
joshualitt17e73142015-01-21 11:52:36 -0800316 , fBatchTracker(batchTracker) {
egdaniel8dd688b2015-01-22 10:16:09 -0800317 SkASSERT(primProc && pipeline && desc && batchTracker);
joshualitt873ad0e2015-01-20 09:08:51 -0800318 }
319 const GrPrimitiveProcessor* fPrimitiveProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -0800320 const GrPipeline* fPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -0800321 const GrProgramDesc* fDesc;
322 const GrBatchTracker* fBatchTracker;
323 };
324
325 void draw(const DrawArgs&, const GrDrawTarget::DrawInfo&);
bsalomon3e791242014-12-17 13:43:13 -0800326
327 /** None of these params are optional, pointers used just to avoid making copies. */
328 struct StencilPathState {
329 bool fUseHWAA;
330 GrRenderTarget* fRenderTarget;
331 const SkMatrix* fViewMatrix;
332 const GrStencilSettings* fStencil;
333 const GrScissorState* fScissor;
334 };
335
336 void stencilPath(const GrPath*, const StencilPathState&);
337
joshualitt873ad0e2015-01-20 09:08:51 -0800338 void drawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&);
339 void drawPaths(const DrawArgs&,
bsalomon830d2062014-12-15 13:12:08 -0800340 const GrPathRange*,
341 const void* indices,
342 GrDrawTarget::PathIndexType,
343 const float transformValues[],
344 GrDrawTarget::PathTransformType,
345 int count,
346 const GrStencilSettings&);
joshualitt4b68ec02014-11-07 14:11:45 -0800347
mtkleinb9eb4ac2015-02-02 18:26:03 -0800348 ///////////////////////////////////////////////////////////////////////////
349 // Debugging and Stats
350
351 class Stats {
352 public:
353#if GR_GPU_STATS
354 Stats() { this->reset(); }
355
bsalomonb12ea412015-02-02 21:19:50 -0800356 void reset() {
357 fRenderTargetBinds = 0;
358 fShaderCompilations = 0;
359 fTextureCreates = 0;
360 fTextureUploads = 0;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700361 fStencilAttachmentCreates = 0;
bsalomonb12ea412015-02-02 21:19:50 -0800362 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800363
364 int renderTargetBinds() const { return fRenderTargetBinds; }
365 void incRenderTargetBinds() { fRenderTargetBinds++; }
366 int shaderCompilations() const { return fShaderCompilations; }
367 void incShaderCompilations() { fShaderCompilations++; }
bsalomonb12ea412015-02-02 21:19:50 -0800368 int textureCreates() const { return fTextureCreates; }
369 void incTextureCreates() { fTextureCreates++; }
370 int textureUploads() const { return fTextureUploads; }
371 void incTextureUploads() { fTextureUploads++; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700372 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800373 void dump(SkString*);
374
375 private:
376 int fRenderTargetBinds;
377 int fShaderCompilations;
bsalomonb12ea412015-02-02 21:19:50 -0800378 int fTextureCreates;
379 int fTextureUploads;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700380 int fStencilAttachmentCreates;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800381#else
382 void dump(SkString*) {};
383 void incRenderTargetBinds() {}
384 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800385 void incTextureCreates() {}
386 void incTextureUploads() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700387 void incStencilAttachmentCreates() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800388#endif
389 };
390
391 Stats* stats() { return &fStats; }
392
393 /**
394 * Called at start and end of gpu trace marking
395 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
396 * and end of a code block respectively
397 */
398 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
399 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
400
401 /**
402 * Takes the current active set of markers and stores them for later use. Any current marker
403 * in the active set is removed from the active set and the targets remove function is called.
404 * These functions do not work as a stack so you cannot call save a second time before calling
405 * restore. Also, it is assumed that when restore is called the current active set of markers
406 * is empty. When the stored markers are added back into the active set, the targets add marker
407 * is called.
408 */
409 void saveActiveTraceMarkers();
410 void restoreActiveTraceMarkers();
411
bsalomon6bc1b5f2015-02-23 09:06:38 -0800412 // Given a rt, find or create a stencil buffer and attach it
egdaniel8dc7c3a2015-04-16 11:22:42 -0700413 bool attachStencilAttachmentToRenderTarget(GrRenderTarget* target);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800414
joshualittd53a8272014-11-10 16:03:14 -0800415protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000416 // Functions used to map clip-respecting stencil tests into normal
417 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000418 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000419 GrStencilFunc func);
420 static void ConvertStencilFuncAndMask(GrStencilFunc func,
421 bool clipInStencil,
422 unsigned int clipBit,
423 unsigned int userBits,
424 unsigned int* ref,
425 unsigned int* mask);
426
mtkleinb9eb4ac2015-02-02 18:26:03 -0800427 const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; }
joshualitt3322fa42014-11-07 08:48:51 -0800428
mtkleinb9eb4ac2015-02-02 18:26:03 -0800429 Stats fStats;
430 SkAutoTDelete<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800431 // Subclass must initialize this in its constructor.
mtkleinb9eb4ac2015-02-02 18:26:03 -0800432 SkAutoTUnref<const GrDrawTargetCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800433
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000434private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000435 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000436 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000437 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000438
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000439 // overridden by backend-specific derived class to create objects.
egdanielb0e1be22015-04-22 13:27:39 -0700440 // Texture size and sample size will have already been validated in base class before
441 // onCreateTexture/CompressedTexture are called.
442 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
443 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -0800444 const void* srcData, size_t rowBytes) = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700445 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
446 GrGpuResource::LifeCycle lifeCycle,
krajcevski9c0e6292014-06-02 07:38:14 -0700447 const void* srcData) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000448 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
449 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000450 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
451 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000452
bsalomon63b21962014-11-05 07:05:34 -0800453 // overridden by backend-specific derived class to perform the clear.
joshualitt4b68ec02014-11-07 14:11:45 -0800454 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
455 bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000456
joshualitt6db519c2014-10-29 08:48:18 -0700457
458 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
459 // ONLY used by the the clip target
mtkleinb9eb4ac2015-02-02 18:26:03 -0800460 virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700461
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000462 // overridden by backend-specific derived class to perform the draw call.
joshualitt873ad0e2015-01-20 09:08:51 -0800463 virtual void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800464 virtual void onStencilPath(const GrPath*, const StencilPathState&) = 0;
465
joshualitt873ad0e2015-01-20 09:08:51 -0800466 virtual void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) = 0;
467 virtual void onDrawPaths(const DrawArgs&,
bsalomond95263c2014-12-16 13:05:12 -0800468 const GrPathRange*,
469 const void* indices,
470 GrDrawTarget::PathIndexType,
471 const float transformValues[],
472 GrDrawTarget::PathTransformType,
473 int count,
474 const GrStencilSettings&) = 0;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000475
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000476 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000477 virtual bool onReadPixels(GrRenderTarget* target,
478 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000479 GrPixelConfig,
480 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000481 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000482
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000483 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000484 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000485 int left, int top, int width, int height,
486 GrPixelConfig config, const void* buffer,
487 size_t rowBytes) = 0;
488
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000489 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000490 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
491
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000492 // width and height may be larger than rt (if underlying API allows it).
493 // Should attach the SB to the RT. Returns false if compatible sb could
494 // not be created.
egdaniel8dc7c3a2015-04-16 11:22:42 -0700495 virtual bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000496
497 // attaches an existing SB to an existing RT.
egdaniel8dc7c3a2015-04-16 11:22:42 -0700498 virtual bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomonb0bd4f62014-09-03 07:19:50 -0700500 // clears target's entire stencil buffer to 0
501 virtual void clearStencil(GrRenderTarget* target) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
joshualitt3322fa42014-11-07 08:48:51 -0800503 virtual void didAddGpuTraceMarker() = 0;
504 virtual void didRemoveGpuTraceMarker() = 0;
505
bsalomon@google.comb635d392011-11-05 12:47:43 +0000506 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000507 this->onResetContext(fResetBits);
508 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000509 ++fResetTimestamp;
510 }
511
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000512 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000513 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000514 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000515 }
516 }
517
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000518 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000519 uint32_t fResetBits;
bsalomon@google.com64386952013-02-08 21:22:44 +0000520 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000521 mutable GrIndexBuffer* fQuadIndexBuffer;
joshualitt3322fa42014-11-07 08:48:51 -0800522 // To keep track that we always have at least as many debug marker adds as removes
523 int fGpuTraceMarkerCount;
524 GrTraceMarkerSet fActiveTraceMarkers;
525 GrTraceMarkerSet fStoredTraceMarkers;
526 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
527 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000528
joshualitt3322fa42014-11-07 08:48:51 -0800529 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530};
531
532#endif