blob: e6f37cc1cfc881e9350444b23071bc202acd8fe0 [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
kkinnunencabe20c2015-06-01 01:37:26 -070011#include "GrPipelineBuilder.h"
joshualitt79f8fae2014-10-28 17:59:26 -070012#include "GrProgramDesc.h"
kkinnunencabe20c2015-06-01 01:37:26 -070013#include "GrStencil.h"
14#include "GrTraceMarker.h"
15#include "GrXferProcessor.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000016#include "SkPath.h"
17
kkinnunencabe20c2015-06-01 01:37:26 -070018class GrBatchTracker;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000019class GrContext;
reedf9ad5582015-06-25 21:29:25 -070020class GrGLContext;
kkinnunencabe20c2015-06-01 01:37:26 -070021class GrIndexBuffer;
bsalomone64eb572015-05-07 11:35:55 -070022class GrNonInstancedVertices;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000023class GrPath;
cdaltonb85a0aa2014-07-21 15:32:44 -070024class GrPathRange;
bsalomon@google.com30085192011-08-19 15:42:31 +000025class GrPathRenderer;
26class GrPathRendererChain;
kkinnunencabe20c2015-06-01 01:37:26 -070027class GrPathRendering;
egdaniel8dd688b2015-01-22 10:16:09 -080028class GrPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -080029class GrPrimitiveProcessor;
kkinnunencabe20c2015-06-01 01:37:26 -070030class GrRenderTarget;
egdaniel8dc7c3a2015-04-16 11:22:42 -070031class GrStencilAttachment;
kkinnunencabe20c2015-06-01 01:37:26 -070032class GrSurface;
33class GrTexture;
34class GrVertexBuffer;
bsalomoncb8979d2015-05-05 09:51:38 -070035class GrVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +000036
joshualitt3322fa42014-11-07 08:48:51 -080037class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000038public:
39 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000040 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000041 * not supported (at compile-time or run-time) this returns NULL. The context will not be
42 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000043 */
bsalomon682c2692015-05-22 14:01:46 -070044 static GrGpu* Create(GrBackend, GrBackendContext, const GrContextOptions&, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000045
46 ////////////////////////////////////////////////////////////////////////////
47
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000048 GrGpu(GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070049 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000050
joshualitt3322fa42014-11-07 08:48:51 -080051 GrContext* getContext() { return fContext; }
52 const GrContext* getContext() const { return fContext; }
53
54 /**
55 * Gets the capabilities of the draw target.
56 */
bsalomon4b91f762015-05-19 09:29:46 -070057 const GrCaps* caps() const { return fCaps.get(); }
joshualitt3322fa42014-11-07 08:48:51 -080058
kkinnunencabe20c2015-06-01 01:37:26 -070059 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070060
bsalomonc8dc1f72014-08-21 13:02:13 -070061 // Called by GrContext when the underlying backend context has been destroyed.
62 // GrGpu should use this to ensure that no backend API calls will be made from
63 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070064 // INHERITED::contextAbandoned() if they override this.
65 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070066
reed@google.comac10a2d2010-12-22 21:39:39 +000067 /**
68 * The GrGpu object normally assumes that no outsider is setting state
69 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000070 * the GrGpu that the state was modified and it shouldn't make assumptions
71 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000072 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080073 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000074
75 /**
bsalomon6d467ec2014-11-18 07:36:19 -080076 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
77 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
78 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -070079 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000080 *
reed@google.comac10a2d2010-12-22 21:39:39 +000081 * @param desc describes the texture to be created.
bsalomon5236cf42015-01-14 10:42:08 -080082 * @param budgeted does this texture count against the resource cache budget?
reed@google.comac10a2d2010-12-22 21:39:39 +000083 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -070084 * palette data for paletted textures. For compressed
85 * formats it contains the compressed pixel data. Otherwise,
86 * it contains width*height texels. If NULL texture data
87 * is uninitialized.
88 * @param rowBytes the number of bytes between consecutive rows. Zero
89 * means rows are tightly packed. This field is ignored
90 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +000091 *
92 * @return The texture object if successful, otherwise NULL.
93 */
bsalomon5236cf42015-01-14 10:42:08 -080094 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted,
95 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000096
bsalomon@google.come269f212011-11-07 13:29:52 +000097 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000098 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000099 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700100 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000101
102 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000103 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000104 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700105 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000106
107 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000108 * Creates a vertex buffer.
109 *
110 * @param size size in bytes of the vertex buffer
111 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000112 * by either GrVertexBuffer::map() or
113 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 *
115 * @return The vertex buffer if successful, otherwise NULL.
116 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000117 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000118
119 /**
120 * Creates an index buffer.
121 *
122 * @param size size in bytes of the index buffer
123 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000124 * by either GrIndexBuffer::map() or
125 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 *
127 * @return The index buffer if successful, otherwise NULL.
128 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000129 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
131 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000132 * Resolves MSAA.
133 */
134 void resolveRenderTarget(GrRenderTarget* target);
135
bsalomonf0674512015-07-28 13:26:15 -0700136 /** Info struct returned by getReadPixelsInfo about performing intermediate draws before
bsalomon39826022015-07-23 08:07:21 -0700137 reading pixels for performance or correctness. */
138 struct ReadPixelTempDrawInfo {
139 /** If the GrGpu is requesting that the caller do a draw to an intermediate surface then
140 this is descriptor for the temp surface. The draw should always be a rect with
141 dst 0,0,w,h. */
142 GrSurfaceDesc fTempSurfaceDesc;
143 /** Indicates whether there is a performance advantage to using an exact match texture
144 (in terms of width and height) for the intermediate texture instead of approximate. */
145 bool fUseExactScratch;
146 /** The caller should swap the R and B channel in the temp draw and then instead of reading
147 the desired config back it should read GrPixelConfigSwapRAndB(readConfig). The swap
148 during the draw and the swap at readback time cancel and the client gets the correct
149 data. The swapped read back is either faster for or required by the underlying backend
150 3D API. */
151 bool fSwapRAndB;
152 };
153 /** Describes why an intermediate draw must/should be performed before readPixels. */
154 enum DrawPreference {
155 /** On input means that the caller would proceed without draw if the GrGpu doesn't request
156 one.
157 On output means that the GrGpu is not requesting a draw. */
158 kNoDraw_DrawPreference,
159 /** Means that the client would prefer a draw for performance of the readback but
160 can satisfy a straight readPixels call on the inputs without an intermediate draw.
161 getReadPixelsInfo will never set the draw preference to this value but may leave
162 it set. */
163 kCallerPrefersDraw_DrawPreference,
164 /** On output means that GrGpu would prefer a draw for performance of the readback but
165 can satisfy a straight readPixels call on the inputs without an intermediate draw. The
166 caller of getReadPixelsInfo should never specify this on intput. */
167 kGpuPrefersDraw_DrawPreference,
168 /** On input means that the caller requires a draw to do a transformation and there is no
169 CPU fallback.
170 On output means that GrGpu can only satisfy the readPixels request if the intermediate
171 draw is performed.
172 */
173 kRequireDraw_DrawPreference
174 };
175
bsalomonf0674512015-07-28 13:26:15 -0700176 /**
177 * Used to negotiate whether and how an intermediate draw should or must be performed before
178 * a readPixels call. If this returns false then GrGpu could not deduce an intermediate draw
179 * that would allow a successful readPixels call. The passed width, height, and rowBytes,
180 * must be non-zero and already reflect clipping to the src bounds.
181 */
182 bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
183 GrPixelConfig readConfig, DrawPreference*, ReadPixelTempDrawInfo*);
184
185 /** Info struct returned by getWritePixelsInfo about performing an intermediate draw in order
186 to write pixels to a GrSurface for either performance or correctness reasons. */
187 struct WritePixelTempDrawInfo {
188 /** If the GrGpu is requesting that the caller upload to an intermediate surface and draw
189 that to the dst then this is the descriptor for the intermediate surface. The caller
190 should upload the pixels such that the upper left pixel of the upload rect is at 0,0 in
191 the intermediate surface.*/
192 GrSurfaceDesc fTempSurfaceDesc;
193 /** If set, fTempSurfaceDesc's config will be a R/B swap of the src pixel config. The caller
194 should upload the pixels as is such that R and B will be swapped in the intermediate
195 surface. When the intermediate is drawn to the dst the shader should swap R/B again
196 such that the correct swizzle results in the dst. This is done to work around either
197 performance or API restrictions in the backend 3D API implementation. */
198 bool fSwapRAndB;
199 };
bsalomon39826022015-07-23 08:07:21 -0700200
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000201 /**
bsalomonf0674512015-07-28 13:26:15 -0700202 * Used to negotiate whether and how an intermediate surface should be used to write pixels to
203 * a GrSurface. If this returns false then GrGpu could not deduce an intermediate draw
204 * that would allow a successful transfer of the src pixels to the dst. The passed width,
205 * height, and rowBytes, must be non-zero and already reflect clipping to the dst bounds.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000206 */
bsalomonf0674512015-07-28 13:26:15 -0700207 bool getWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
208 GrPixelConfig srcConfig, DrawPreference*, WritePixelTempDrawInfo*);
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000209
210 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000211 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000212 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700213 * @param surface The surface to read from
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000214 * @param left left edge of the rectangle to read (inclusive)
215 * @param top top edge of the rectangle to read (inclusive)
216 * @param width width of rectangle to read in pixels.
217 * @param height height of rectangle to read in pixels.
218 * @param config the pixel config of the destination buffer
219 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000220 * @param rowBytes the number of bytes between consecutive rows. Zero
221 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000222 * @param invertY buffer should be populated bottom-to-top as opposed
223 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224 *
225 * @return true if the read succeeded, false if not. The read can fail
226 * because of a unsupported pixel config or because no render
227 * target is currently set.
228 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700229 bool readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000230 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000231 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
bsalomon@google.com6f379512011-11-16 20:36:03 +0000233 /**
bsalomon6cb3cbe2015-07-30 07:34:27 -0700234 * Updates the pixels in a rectangle of a surface.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000235 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700236 * @param surface The surface to write to.
bsalomon@google.com6f379512011-11-16 20:36:03 +0000237 * @param left left edge of the rectangle to write (inclusive)
238 * @param top top edge of the rectangle to write (inclusive)
239 * @param width width of rectangle to write in pixels.
240 * @param height height of rectangle to write in pixels.
241 * @param config the pixel config of the source buffer
242 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000243 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000244 * means rows are tightly packed.
245 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700246 bool writePixels(GrSurface* surface,
247 int left, int top, int width, int height,
248 GrPixelConfig config, const void* buffer,
249 size_t rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000250
joshualitt3322fa42014-11-07 08:48:51 -0800251 /**
252 * Clear the passed in render target. Ignores the draw state and clip. Clears the whole thing if
253 * rect is NULL, otherwise just the rect. If canIgnoreRect is set then the entire render target
254 * can be optionally cleared.
255 */
bsalomon6d467ec2014-11-18 07:36:19 -0800256 void clear(const SkIRect* rect, GrColor color, bool canIgnoreRect,GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800257
258
bsalomon6d467ec2014-11-18 07:36:19 -0800259 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800260
261 /**
262 * Discards the contents render target. NULL indicates that the current render target should
263 * be discarded.
264 **/
265 virtual void discard(GrRenderTarget* = NULL) = 0;
266
267 /**
268 * This is can be called before allocating a texture to be a dst for copySurface. It will
bsalomonf90a02b2014-11-26 12:28:00 -0800269 * populate the origin, config, and flags fields of the desc such that copySurface can
270 * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy
271 * that would be more effecient than drawing the src to a dst render target.
joshualitt3322fa42014-11-07 08:48:51 -0800272 */
joshualitt1c735482015-07-13 08:08:25 -0700273 virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700274
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000275 // After the client interacts directly with the 3D context state the GrGpu
276 // must resync its internal state and assumptions about 3D context state.
277 // Each time this occurs the GrGpu bumps a timestamp.
278 // state of the 3D context
279 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
280 // a billion years.
281 typedef uint64_t ResetTimestamp;
282
283 // This timestamp is always older than the current timestamp
284 static const ResetTimestamp kExpiredTimestamp = 0;
285 // Returns a timestamp based on the number of times the context was reset.
286 // This timestamp can be used to lazily detect when cached 3D context state
287 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800288 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000289
joshualitt873ad0e2015-01-20 09:08:51 -0800290 virtual void buildProgramDesc(GrProgramDesc*,
291 const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800292 const GrPipeline&,
joshualitt873ad0e2015-01-20 09:08:51 -0800293 const GrBatchTracker&) const = 0;
joshualitt79f8fae2014-10-28 17:59:26 -0700294
bsalomonf90a02b2014-11-26 12:28:00 -0800295 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
296 // take place at the GrDrawTarget level and this function implement faster copy paths. The rect
297 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
298 // src/dst bounds and non-empty.
joshualitt3322fa42014-11-07 08:48:51 -0800299 virtual bool copySurface(GrSurface* dst,
300 GrSurface* src,
301 const SkIRect& srcRect,
302 const SkIPoint& dstPoint) = 0;
303
cdalton9954bc32015-04-29 14:17:00 -0700304 // Called before certain draws in order to guarantee coherent results from dst reads.
cdalton231c5fd2015-05-13 12:35:36 -0700305 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
cdalton9954bc32015-04-29 14:17:00 -0700306
joshualitt873ad0e2015-01-20 09:08:51 -0800307 struct DrawArgs {
joshualitt873ad0e2015-01-20 09:08:51 -0800308 DrawArgs(const GrPrimitiveProcessor* primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800309 const GrPipeline* pipeline,
joshualitt873ad0e2015-01-20 09:08:51 -0800310 const GrProgramDesc* desc,
joshualitt17e73142015-01-21 11:52:36 -0800311 const GrBatchTracker* batchTracker)
joshualitt873ad0e2015-01-20 09:08:51 -0800312 : fPrimitiveProcessor(primProc)
egdaniel8dd688b2015-01-22 10:16:09 -0800313 , fPipeline(pipeline)
joshualitt873ad0e2015-01-20 09:08:51 -0800314 , fDesc(desc)
joshualitt17e73142015-01-21 11:52:36 -0800315 , fBatchTracker(batchTracker) {
egdaniel8dd688b2015-01-22 10:16:09 -0800316 SkASSERT(primProc && pipeline && desc && batchTracker);
joshualitt873ad0e2015-01-20 09:08:51 -0800317 }
318 const GrPrimitiveProcessor* fPrimitiveProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -0800319 const GrPipeline* fPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -0800320 const GrProgramDesc* fDesc;
321 const GrBatchTracker* fBatchTracker;
322 };
323
bsalomoncb8979d2015-05-05 09:51:38 -0700324 void draw(const DrawArgs&, const GrVertices&);
bsalomon3e791242014-12-17 13:43:13 -0800325
mtkleinb9eb4ac2015-02-02 18:26:03 -0800326 ///////////////////////////////////////////////////////////////////////////
327 // Debugging and Stats
328
329 class Stats {
330 public:
331#if GR_GPU_STATS
332 Stats() { this->reset(); }
333
bsalomonb12ea412015-02-02 21:19:50 -0800334 void reset() {
335 fRenderTargetBinds = 0;
336 fShaderCompilations = 0;
337 fTextureCreates = 0;
338 fTextureUploads = 0;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700339 fStencilAttachmentCreates = 0;
bsalomonb12ea412015-02-02 21:19:50 -0800340 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800341
342 int renderTargetBinds() const { return fRenderTargetBinds; }
343 void incRenderTargetBinds() { fRenderTargetBinds++; }
344 int shaderCompilations() const { return fShaderCompilations; }
345 void incShaderCompilations() { fShaderCompilations++; }
bsalomonb12ea412015-02-02 21:19:50 -0800346 int textureCreates() const { return fTextureCreates; }
347 void incTextureCreates() { fTextureCreates++; }
348 int textureUploads() const { return fTextureUploads; }
349 void incTextureUploads() { fTextureUploads++; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700350 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800351 void dump(SkString*);
352
353 private:
354 int fRenderTargetBinds;
355 int fShaderCompilations;
bsalomonb12ea412015-02-02 21:19:50 -0800356 int fTextureCreates;
357 int fTextureUploads;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700358 int fStencilAttachmentCreates;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800359#else
360 void dump(SkString*) {};
361 void incRenderTargetBinds() {}
362 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800363 void incTextureCreates() {}
364 void incTextureUploads() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700365 void incStencilAttachmentCreates() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800366#endif
367 };
368
369 Stats* stats() { return &fStats; }
370
371 /**
372 * Called at start and end of gpu trace marking
373 * GR_CREATE_GPU_TRACE_MARKER(marker_str, target) will automatically call these at the start
374 * and end of a code block respectively
375 */
376 void addGpuTraceMarker(const GrGpuTraceMarker* marker);
377 void removeGpuTraceMarker(const GrGpuTraceMarker* marker);
378
379 /**
380 * Takes the current active set of markers and stores them for later use. Any current marker
381 * in the active set is removed from the active set and the targets remove function is called.
382 * These functions do not work as a stack so you cannot call save a second time before calling
383 * restore. Also, it is assumed that when restore is called the current active set of markers
384 * is empty. When the stored markers are added back into the active set, the targets add marker
385 * is called.
386 */
387 void saveActiveTraceMarkers();
388 void restoreActiveTraceMarkers();
389
jvanverth672bb7f2015-07-13 07:19:57 -0700390 // creation and deletion of raw texture for testing
jvanverth88957922015-07-14 11:02:52 -0700391 // only to be used in GPU-specific tests
392 virtual GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
393 GrPixelConfig config) const = 0;
394 virtual bool isTestingOnlyBackendTexture(GrBackendObject id) const = 0;
395 virtual void deleteTestingOnlyBackendTexture(GrBackendObject id) const = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700396
bsalomon6bc1b5f2015-02-23 09:06:38 -0800397 // Given a rt, find or create a stencil buffer and attach it
egdaniel8dc7c3a2015-04-16 11:22:42 -0700398 bool attachStencilAttachmentToRenderTarget(GrRenderTarget* target);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800399
jvanverth672bb7f2015-07-13 07:19:57 -0700400 // This is only to be used in GL-specific tests.
reedf9ad5582015-06-25 21:29:25 -0700401 virtual const GrGLContext* glContextForTesting() const { return NULL; }
bsalomon993a4212015-05-29 11:37:25 -0700402
joshualittd53a8272014-11-10 16:03:14 -0800403protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000404 // Functions used to map clip-respecting stencil tests into normal
405 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000406 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000407 GrStencilFunc func);
408 static void ConvertStencilFuncAndMask(GrStencilFunc func,
409 bool clipInStencil,
410 unsigned int clipBit,
411 unsigned int userBits,
412 unsigned int* ref,
413 unsigned int* mask);
414
bsalomonf0674512015-07-28 13:26:15 -0700415 static void ElevateDrawPreference(GrGpu::DrawPreference* preference,
416 GrGpu::DrawPreference elevation) {
417 GR_STATIC_ASSERT(GrGpu::kCallerPrefersDraw_DrawPreference > GrGpu::kNoDraw_DrawPreference);
418 GR_STATIC_ASSERT(GrGpu::kGpuPrefersDraw_DrawPreference >
419 GrGpu::kCallerPrefersDraw_DrawPreference);
420 GR_STATIC_ASSERT(GrGpu::kRequireDraw_DrawPreference >
421 GrGpu::kGpuPrefersDraw_DrawPreference);
422 *preference = SkTMax(*preference, elevation);
423 }
424
mtkleinb9eb4ac2015-02-02 18:26:03 -0800425 const GrTraceMarkerSet& getActiveTraceMarkers() const { return fActiveTraceMarkers; }
joshualitt3322fa42014-11-07 08:48:51 -0800426
mtkleinb9eb4ac2015-02-02 18:26:03 -0800427 Stats fStats;
428 SkAutoTDelete<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800429 // Subclass must initialize this in its constructor.
bsalomon4b91f762015-05-19 09:29:46 -0700430 SkAutoTUnref<const GrCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800431
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000432private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000433 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000434 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000435 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000436
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000437 // overridden by backend-specific derived class to create objects.
egdanielb0e1be22015-04-22 13:27:39 -0700438 // Texture size and sample size will have already been validated in base class before
439 // onCreateTexture/CompressedTexture are called.
440 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
441 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -0800442 const void* srcData, size_t rowBytes) = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700443 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
444 GrGpuResource::LifeCycle lifeCycle,
krajcevski9c0e6292014-06-02 07:38:14 -0700445 const void* srcData) = 0;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700446 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) = 0;
447 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
448 GrWrapOwnership) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000449 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
450 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000451
bsalomon63b21962014-11-05 07:05:34 -0800452 // overridden by backend-specific derived class to perform the clear.
joshualitt4b68ec02014-11-07 14:11:45 -0800453 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
454 bool canIgnoreRect) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000455
joshualitt6db519c2014-10-29 08:48:18 -0700456
457 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
458 // ONLY used by the the clip target
mtkleinb9eb4ac2015-02-02 18:26:03 -0800459 virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700460
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000461 // overridden by backend-specific derived class to perform the draw call.
bsalomone64eb572015-05-07 11:35:55 -0700462 virtual void onDraw(const DrawArgs&, const GrNonInstancedVertices&) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800463
bsalomonf0674512015-07-28 13:26:15 -0700464 virtual bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight,
465 size_t rowBytes, GrPixelConfig readConfig, DrawPreference*,
466 ReadPixelTempDrawInfo*) = 0;
467 virtual bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
468 GrPixelConfig srcConfig, DrawPreference*,
469 WritePixelTempDrawInfo*) = 0;
470
bsalomon6cb3cbe2015-07-30 07:34:27 -0700471 // overridden by backend-specific derived class to perform the surface read
472 virtual bool onReadPixels(GrSurface*,
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000473 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000474 GrPixelConfig,
475 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000476 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000477
bsalomon6cb3cbe2015-07-30 07:34:27 -0700478 // overridden by backend-specific derived class to perform the surface write
479 virtual bool onWritePixels(GrSurface*,
480 int left, int top, int width, int height,
481 GrPixelConfig config, const void* buffer,
482 size_t rowBytes) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000484 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000485 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
486
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000487 // width and height may be larger than rt (if underlying API allows it).
488 // Should attach the SB to the RT. Returns false if compatible sb could
489 // not be created.
egdaniel8dc7c3a2015-04-16 11:22:42 -0700490 virtual bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000491
492 // attaches an existing SB to an existing RT.
egdaniel8dc7c3a2015-04-16 11:22:42 -0700493 virtual bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomonb0bd4f62014-09-03 07:19:50 -0700495 // clears target's entire stencil buffer to 0
496 virtual void clearStencil(GrRenderTarget* target) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
joshualitt3322fa42014-11-07 08:48:51 -0800498 virtual void didAddGpuTraceMarker() = 0;
499 virtual void didRemoveGpuTraceMarker() = 0;
500
bsalomon@google.comb635d392011-11-05 12:47:43 +0000501 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000502 this->onResetContext(fResetBits);
503 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000504 ++fResetTimestamp;
505 }
506
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000507 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000508 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000509 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000510 }
511 }
512
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000513 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000514 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800515 // To keep track that we always have at least as many debug marker adds as removes
516 int fGpuTraceMarkerCount;
517 GrTraceMarkerSet fActiveTraceMarkers;
518 GrTraceMarkerSet fStoredTraceMarkers;
519 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
520 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000521
kkinnunencabe20c2015-06-01 01:37:26 -0700522 friend class GrPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800523 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000524};
525
526#endif