blob: e744ec897a83fc783603a73fdcf65c2acaca34b7 [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"
bsalomon045802d2015-10-20 07:58:01 -070014#include "GrTextureParamsAdjuster.h"
kkinnunencabe20c2015-06-01 01:37:26 -070015#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;
jvanverth73063dc2015-12-03 09:15:47 -080034class GrTransferBuffer;
kkinnunencabe20c2015-06-01 01:37:26 -070035class GrVertexBuffer;
bsalomoncb8979d2015-05-05 09:51:38 -070036class GrVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +000037
joshualitt3322fa42014-11-07 08:48:51 -080038class GrGpu : public SkRefCnt {
reed@google.comac10a2d2010-12-22 21:39:39 +000039public:
40 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000041 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
halcanary96fcdcc2015-08-27 07:41:13 -070042 * not supported (at compile-time or run-time) this returns nullptr. The context will not be
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000043 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000044 */
bsalomon682c2692015-05-22 14:01:46 -070045 static GrGpu* Create(GrBackend, GrBackendContext, const GrContextOptions&, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000046
47 ////////////////////////////////////////////////////////////////////////////
48
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000049 GrGpu(GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070050 ~GrGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000051
joshualitt3322fa42014-11-07 08:48:51 -080052 GrContext* getContext() { return fContext; }
53 const GrContext* getContext() const { return fContext; }
54
55 /**
56 * Gets the capabilities of the draw target.
57 */
bsalomon4b91f762015-05-19 09:29:46 -070058 const GrCaps* caps() const { return fCaps.get(); }
joshualitt3322fa42014-11-07 08:48:51 -080059
kkinnunencabe20c2015-06-01 01:37:26 -070060 GrPathRendering* pathRendering() { return fPathRendering.get(); }
kkinnunenccdaa042014-08-20 01:36:23 -070061
bsalomonc8dc1f72014-08-21 13:02:13 -070062 // Called by GrContext when the underlying backend context has been destroyed.
63 // GrGpu should use this to ensure that no backend API calls will be made from
64 // here onward, including in its destructor. Subclasses should call
robertphillipse3371302014-09-17 06:01:06 -070065 // INHERITED::contextAbandoned() if they override this.
66 virtual void contextAbandoned();
bsalomonc8dc1f72014-08-21 13:02:13 -070067
reed@google.comac10a2d2010-12-22 21:39:39 +000068 /**
69 * The GrGpu object normally assumes that no outsider is setting state
70 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000071 * the GrGpu that the state was modified and it shouldn't make assumptions
72 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000073 */
mtkleinb9eb4ac2015-02-02 18:26:03 -080074 void markContextDirty(uint32_t state = kAll_GrBackendState) { fResetBits |= state; }
reed@google.comac10a2d2010-12-22 21:39:39 +000075
76 /**
bsalomon6d467ec2014-11-18 07:36:19 -080077 * Creates a texture object. If kRenderTarget_GrSurfaceFlag the texture can
78 * be used as a render target by calling GrTexture::asRenderTarget(). Not all
79 * pixel configs can be used as render targets. Support for configs as textures
bsalomon4b91f762015-05-19 09:29:46 -070080 * or render targets can be checked using GrCaps.
bsalomon@google.com1da07462011-03-10 14:51:57 +000081 *
reed@google.comac10a2d2010-12-22 21:39:39 +000082 * @param desc describes the texture to be created.
bsalomon5236cf42015-01-14 10:42:08 -080083 * @param budgeted does this texture count against the resource cache budget?
reed@google.comac10a2d2010-12-22 21:39:39 +000084 * @param srcData texel data to load texture. Begins with full-size
krajcevski9c0e6292014-06-02 07:38:14 -070085 * palette data for paletted textures. For compressed
86 * formats it contains the compressed pixel data. Otherwise,
halcanary96fcdcc2015-08-27 07:41:13 -070087 * it contains width*height texels. If nullptr texture data
krajcevski9c0e6292014-06-02 07:38:14 -070088 * is uninitialized.
89 * @param rowBytes the number of bytes between consecutive rows. Zero
90 * means rows are tightly packed. This field is ignored
91 * for compressed formats.
reed@google.comac10a2d2010-12-22 21:39:39 +000092 *
halcanary96fcdcc2015-08-27 07:41:13 -070093 * @return The texture object if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +000094 */
bsalomon5236cf42015-01-14 10:42:08 -080095 GrTexture* createTexture(const GrSurfaceDesc& desc, bool budgeted,
96 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.come269f212011-11-07 13:29:52 +000098 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000099 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000100 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700101 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000102
103 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000104 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000105 */
bsalomon6dc6f5f2015-06-18 09:12:16 -0700106 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&, GrWrapOwnership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000107
108 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 * Creates a vertex buffer.
110 *
111 * @param size size in bytes of the vertex buffer
112 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000113 * by either GrVertexBuffer::map() or
114 * GrVertexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 *
halcanary96fcdcc2015-08-27 07:41:13 -0700116 * @return The vertex buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000118 GrVertexBuffer* createVertexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
120 /**
121 * Creates an index buffer.
122 *
123 * @param size size in bytes of the index buffer
124 * @param dynamic hints whether the data will be frequently changed
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000125 * by either GrIndexBuffer::map() or
126 * GrIndexBuffer::updateData().
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 *
halcanary96fcdcc2015-08-27 07:41:13 -0700128 * @return The index buffer if successful, otherwise nullptr.
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 */
robertphillips@google.comadacc702013-10-14 21:53:24 +0000130 GrIndexBuffer* createIndexBuffer(size_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
jvanverth73063dc2015-12-03 09:15:47 -0800132 /**
133 * Creates a transfer buffer.
134 *
135 * @param size size in bytes of the index buffer
136 * @param toGpu true if used to transfer from the cpu to the gpu
137 * otherwise to be used to transfer from the gpu to the cpu
138 *
139 * @return The transfer buffer if successful, otherwise nullptr.
140 */
141 GrTransferBuffer* createTransferBuffer(size_t size, TransferType type);
142
reed@google.comac10a2d2010-12-22 21:39:39 +0000143 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000144 * Resolves MSAA.
145 */
146 void resolveRenderTarget(GrRenderTarget* target);
147
bsalomonf0674512015-07-28 13:26:15 -0700148 /** Info struct returned by getReadPixelsInfo about performing intermediate draws before
bsalomon39826022015-07-23 08:07:21 -0700149 reading pixels for performance or correctness. */
150 struct ReadPixelTempDrawInfo {
151 /** If the GrGpu is requesting that the caller do a draw to an intermediate surface then
152 this is descriptor for the temp surface. The draw should always be a rect with
153 dst 0,0,w,h. */
154 GrSurfaceDesc fTempSurfaceDesc;
155 /** Indicates whether there is a performance advantage to using an exact match texture
156 (in terms of width and height) for the intermediate texture instead of approximate. */
157 bool fUseExactScratch;
158 /** The caller should swap the R and B channel in the temp draw and then instead of reading
159 the desired config back it should read GrPixelConfigSwapRAndB(readConfig). The swap
160 during the draw and the swap at readback time cancel and the client gets the correct
161 data. The swapped read back is either faster for or required by the underlying backend
162 3D API. */
163 bool fSwapRAndB;
164 };
165 /** Describes why an intermediate draw must/should be performed before readPixels. */
166 enum DrawPreference {
167 /** On input means that the caller would proceed without draw if the GrGpu doesn't request
168 one.
169 On output means that the GrGpu is not requesting a draw. */
170 kNoDraw_DrawPreference,
171 /** Means that the client would prefer a draw for performance of the readback but
172 can satisfy a straight readPixels call on the inputs without an intermediate draw.
173 getReadPixelsInfo will never set the draw preference to this value but may leave
174 it set. */
175 kCallerPrefersDraw_DrawPreference,
176 /** On output means that GrGpu would prefer a draw for performance of the readback but
177 can satisfy a straight readPixels call on the inputs without an intermediate draw. The
178 caller of getReadPixelsInfo should never specify this on intput. */
179 kGpuPrefersDraw_DrawPreference,
180 /** On input means that the caller requires a draw to do a transformation and there is no
181 CPU fallback.
182 On output means that GrGpu can only satisfy the readPixels request if the intermediate
183 draw is performed.
184 */
185 kRequireDraw_DrawPreference
186 };
187
bsalomonf0674512015-07-28 13:26:15 -0700188 /**
189 * Used to negotiate whether and how an intermediate draw should or must be performed before
190 * a readPixels call. If this returns false then GrGpu could not deduce an intermediate draw
191 * that would allow a successful readPixels call. The passed width, height, and rowBytes,
192 * must be non-zero and already reflect clipping to the src bounds.
193 */
194 bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
195 GrPixelConfig readConfig, DrawPreference*, ReadPixelTempDrawInfo*);
196
197 /** Info struct returned by getWritePixelsInfo about performing an intermediate draw in order
198 to write pixels to a GrSurface for either performance or correctness reasons. */
199 struct WritePixelTempDrawInfo {
200 /** If the GrGpu is requesting that the caller upload to an intermediate surface and draw
201 that to the dst then this is the descriptor for the intermediate surface. The caller
202 should upload the pixels such that the upper left pixel of the upload rect is at 0,0 in
203 the intermediate surface.*/
204 GrSurfaceDesc fTempSurfaceDesc;
205 /** If set, fTempSurfaceDesc's config will be a R/B swap of the src pixel config. The caller
206 should upload the pixels as is such that R and B will be swapped in the intermediate
207 surface. When the intermediate is drawn to the dst the shader should swap R/B again
208 such that the correct swizzle results in the dst. This is done to work around either
209 performance or API restrictions in the backend 3D API implementation. */
210 bool fSwapRAndB;
211 };
bsalomon39826022015-07-23 08:07:21 -0700212
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000213 /**
bsalomonf0674512015-07-28 13:26:15 -0700214 * Used to negotiate whether and how an intermediate surface should be used to write pixels to
215 * a GrSurface. If this returns false then GrGpu could not deduce an intermediate draw
216 * that would allow a successful transfer of the src pixels to the dst. The passed width,
217 * height, and rowBytes, must be non-zero and already reflect clipping to the dst bounds.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000218 */
bsalomonf0674512015-07-28 13:26:15 -0700219 bool getWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
220 GrPixelConfig srcConfig, DrawPreference*, WritePixelTempDrawInfo*);
bsalomon@google.coma85449d2011-11-19 02:36:05 +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 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700225 * @param surface The surface to read from
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000226 * @param left left edge of the rectangle to read (inclusive)
227 * @param top top edge of the rectangle to read (inclusive)
228 * @param width width of rectangle to read in pixels.
229 * @param height height of rectangle to read in pixels.
230 * @param config the pixel config of the destination buffer
231 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000232 * @param rowBytes the number of bytes between consecutive rows. Zero
233 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 * @param invertY buffer should be populated bottom-to-top as opposed
235 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236 *
237 * @return true if the read succeeded, false if not. The read can fail
238 * because of a unsupported pixel config or because no render
239 * target is currently set.
240 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700241 bool readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000243 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
bsalomon@google.com6f379512011-11-16 20:36:03 +0000245 /**
bsalomon6cb3cbe2015-07-30 07:34:27 -0700246 * Updates the pixels in a rectangle of a surface.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000247 *
bsalomon6cb3cbe2015-07-30 07:34:27 -0700248 * @param surface The surface to write to.
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 */
bsalomon6cb3cbe2015-07-30 07:34:27 -0700258 bool writePixels(GrSurface* surface,
259 int left, int top, int width, int height,
260 GrPixelConfig config, const void* buffer,
261 size_t rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000262
joshualitt3322fa42014-11-07 08:48:51 -0800263 /**
jvanverth17aa0472016-01-05 10:41:27 -0800264 * Updates the pixels in a rectangle of a surface using a GrTransferBuffer
265 *
266 * @param surface The surface to write to.
267 * @param left left edge of the rectangle to write (inclusive)
268 * @param top top edge of the rectangle to write (inclusive)
269 * @param width width of rectangle to write in pixels.
270 * @param height height of rectangle to write in pixels.
271 * @param config the pixel config of the source buffer
272 * @param buffer GrTransferBuffer to read pixels from
273 * @param offset offset from the start of the buffer
274 * @param rowBytes number of bytes between consecutive rows. Zero
275 * means rows are tightly packed.
276 */
277 bool transferPixels(GrSurface* surface,
278 int left, int top, int width, int height,
279 GrPixelConfig config, GrTransferBuffer* buffer,
280 size_t offset, size_t rowBytes);
281
282 /**
egdaniel51c8d402015-08-06 10:54:13 -0700283 * Clear the passed in render target. Ignores the draw state and clip.
joshualitt3322fa42014-11-07 08:48:51 -0800284 */
egdaniel51c8d402015-08-06 10:54:13 -0700285 void clear(const SkIRect& rect, GrColor color, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800286
287
bsalomon6d467ec2014-11-18 07:36:19 -0800288 void clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* renderTarget);
joshualitt3322fa42014-11-07 08:48:51 -0800289
290 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700291 * Discards the contents render target. nullptr indicates that the current render target should
joshualitt3322fa42014-11-07 08:48:51 -0800292 * be discarded.
293 **/
halcanary96fcdcc2015-08-27 07:41:13 -0700294 virtual void discard(GrRenderTarget* = nullptr) = 0;
joshualitt3322fa42014-11-07 08:48:51 -0800295
296 /**
297 * This is can be called before allocating a texture to be a dst for copySurface. It will
bsalomonf90a02b2014-11-26 12:28:00 -0800298 * populate the origin, config, and flags fields of the desc such that copySurface can
299 * efficiently succeed. It should only succeed if it can allow copySurface to perform a copy
300 * that would be more effecient than drawing the src to a dst render target.
joshualitt3322fa42014-11-07 08:48:51 -0800301 */
joshualitt1c735482015-07-13 08:08:25 -0700302 virtual bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700303
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000304 // After the client interacts directly with the 3D context state the GrGpu
305 // must resync its internal state and assumptions about 3D context state.
306 // Each time this occurs the GrGpu bumps a timestamp.
307 // state of the 3D context
308 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
309 // a billion years.
310 typedef uint64_t ResetTimestamp;
311
312 // This timestamp is always older than the current timestamp
313 static const ResetTimestamp kExpiredTimestamp = 0;
314 // Returns a timestamp based on the number of times the context was reset.
315 // This timestamp can be used to lazily detect when cached 3D context state
316 // is dirty.
bsalomon6d467ec2014-11-18 07:36:19 -0800317 ResetTimestamp getResetTimestamp() const { return fResetTimestamp; }
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000318
joshualitt873ad0e2015-01-20 09:08:51 -0800319 virtual void buildProgramDesc(GrProgramDesc*,
320 const GrPrimitiveProcessor&,
joshualitt465283c2015-09-11 08:19:35 -0700321 const GrPipeline&) const = 0;
joshualitt79f8fae2014-10-28 17:59:26 -0700322
bsalomonf90a02b2014-11-26 12:28:00 -0800323 // Called to perform a surface to surface copy. Fallbacks to issuing a draw from the src to dst
324 // take place at the GrDrawTarget level and this function implement faster copy paths. The rect
325 // and point are pre-clipped. The src rect and implied dst rect are guaranteed to be within the
326 // src/dst bounds and non-empty.
joshualitt1cbdcde2015-08-21 11:53:29 -0700327 bool copySurface(GrSurface* dst,
328 GrSurface* src,
329 const SkIRect& srcRect,
330 const SkIPoint& dstPoint);
joshualitt3322fa42014-11-07 08:48:51 -0800331
joshualitt873ad0e2015-01-20 09:08:51 -0800332 struct DrawArgs {
joshualitt873ad0e2015-01-20 09:08:51 -0800333 DrawArgs(const GrPrimitiveProcessor* primProc,
egdaniel8dd688b2015-01-22 10:16:09 -0800334 const GrPipeline* pipeline,
joshualitt465283c2015-09-11 08:19:35 -0700335 const GrProgramDesc* desc)
joshualitt873ad0e2015-01-20 09:08:51 -0800336 : fPrimitiveProcessor(primProc)
egdaniel8dd688b2015-01-22 10:16:09 -0800337 , fPipeline(pipeline)
joshualitt465283c2015-09-11 08:19:35 -0700338 , fDesc(desc) {
339 SkASSERT(primProc && pipeline && desc);
joshualitt873ad0e2015-01-20 09:08:51 -0800340 }
341 const GrPrimitiveProcessor* fPrimitiveProcessor;
egdaniel8dd688b2015-01-22 10:16:09 -0800342 const GrPipeline* fPipeline;
joshualitt873ad0e2015-01-20 09:08:51 -0800343 const GrProgramDesc* fDesc;
joshualitt873ad0e2015-01-20 09:08:51 -0800344 };
345
bsalomoncb8979d2015-05-05 09:51:38 -0700346 void draw(const DrawArgs&, const GrVertices&);
bsalomon3e791242014-12-17 13:43:13 -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;
jvanverth17aa0472016-01-05 10:41:27 -0800361 fTransfersToTexture = 0;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700362 fStencilAttachmentCreates = 0;
joshualitt87a5c9f2015-09-08 13:42:05 -0700363 fNumDraws = 0;
bsalomonb12ea412015-02-02 21:19:50 -0800364 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800365
366 int renderTargetBinds() const { return fRenderTargetBinds; }
367 void incRenderTargetBinds() { fRenderTargetBinds++; }
368 int shaderCompilations() const { return fShaderCompilations; }
369 void incShaderCompilations() { fShaderCompilations++; }
bsalomonb12ea412015-02-02 21:19:50 -0800370 int textureCreates() const { return fTextureCreates; }
371 void incTextureCreates() { fTextureCreates++; }
372 int textureUploads() const { return fTextureUploads; }
373 void incTextureUploads() { fTextureUploads++; }
jvanverth17aa0472016-01-05 10:41:27 -0800374 int transfersToTexture() const { return fTransfersToTexture; }
375 void incTransfersToTexture() { fTransfersToTexture++; }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700376 void incStencilAttachmentCreates() { fStencilAttachmentCreates++; }
joshualitt87a5c9f2015-09-08 13:42:05 -0700377 void incNumDraws() { fNumDraws++; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800378 void dump(SkString*);
joshualitte45c81c2015-12-02 09:05:37 -0800379 void dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800380
381 private:
382 int fRenderTargetBinds;
383 int fShaderCompilations;
bsalomonb12ea412015-02-02 21:19:50 -0800384 int fTextureCreates;
385 int fTextureUploads;
jvanverth17aa0472016-01-05 10:41:27 -0800386 int fTransfersToTexture;
egdaniel8dc7c3a2015-04-16 11:22:42 -0700387 int fStencilAttachmentCreates;
joshualitt87a5c9f2015-09-08 13:42:05 -0700388 int fNumDraws;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800389#else
joshualitte45c81c2015-12-02 09:05:37 -0800390 void dump(SkString*) {}
391 void dumpKeyValuePairs(SkTArray<SkString>*, SkTArray<double>*) {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800392 void incRenderTargetBinds() {}
393 void incShaderCompilations() {}
bsalomonb12ea412015-02-02 21:19:50 -0800394 void incTextureCreates() {}
395 void incTextureUploads() {}
jvanverth17aa0472016-01-05 10:41:27 -0800396 void incTransfersToTexture() {}
egdaniel8dc7c3a2015-04-16 11:22:42 -0700397 void incStencilAttachmentCreates() {}
joshualitt87a5c9f2015-09-08 13:42:05 -0700398 void incNumDraws() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -0800399#endif
400 };
401
402 Stats* stats() { return &fStats; }
403
bsalomon67d76202015-11-11 12:40:42 -0800404 /** Creates a texture directly in the backend API without wrapping it in a GrTexture. This is
405 only to be used for testing (particularly for testing the methods that import an externally
406 created texture into Skia. Must be matched with a call to deleteTestingOnlyTexture(). */
jvanverth88957922015-07-14 11:02:52 -0700407 virtual GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
408 GrPixelConfig config) const = 0;
bsalomon67d76202015-11-11 12:40:42 -0800409 /** Check a handle represents an actual texture in the backend API that has not been freed. */
410 virtual bool isTestingOnlyBackendTexture(GrBackendObject) const = 0;
411 /** If ownership of the backend texture has been transferred pass true for abandonTexture. This
412 will do any necessary cleanup of the handle without freeing the texture in the backend
413 API. */
414 virtual void deleteTestingOnlyBackendTexture(GrBackendObject,
415 bool abandonTexture = false) const = 0;
jvanverth672bb7f2015-07-13 07:19:57 -0700416
egdanielec00d942015-09-14 12:56:10 -0700417 // width and height may be larger than rt (if underlying API allows it).
418 // Returns nullptr if compatible sb could not be created, otherwise the caller owns the ref on
419 // the GrStencilAttachment.
420 virtual GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
421 int width,
422 int height) = 0;
423 // clears target's entire stencil buffer to 0
424 virtual void clearStencil(GrRenderTarget* target) = 0;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800425
bsalomon6dea83f2015-12-03 12:58:06 -0800426 // draws an outline rectangle for debugging/visualization purposes.
427 virtual void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) = 0;
bsalomon045802d2015-10-20 07:58:01 -0700428
bsalomone179a912016-01-20 06:18:10 -0800429 // Determines whether a texture will need to be rescaled in order to be used with the
430 // GrTextureParams. This variation is called when the caller will create a new texture using the
431 // texture provider from a non-texture src (cpu-backed image, ...).
bsalomon045802d2015-10-20 07:58:01 -0700432 bool makeCopyForTextureParams(int width, int height, const GrTextureParams&,
bsalomone179a912016-01-20 06:18:10 -0800433 GrTextureProducer::CopyParams*) const;
434
435 // Like the above but this variation should be called when the caller is not creating the
436 // original texture but rather was handed the original texture. It adds additional checks
437 // relevant to original textures that were created external to Skia via
438 // GrTextureProvider::wrap methods.
439 bool makeCopyForTextureParams(GrTexture* texture, const GrTextureParams& params,
440 GrTextureProducer::CopyParams* copyParams) const {
441 if (this->makeCopyForTextureParams(texture->width(), texture->height(), params,
442 copyParams)) {
443 return true;
444 }
445 return this->onMakeCopyForTextureParams(texture, params, copyParams);
446 }
bsalomon045802d2015-10-20 07:58:01 -0700447
jvanverth672bb7f2015-07-13 07:19:57 -0700448 // This is only to be used in GL-specific tests.
halcanary96fcdcc2015-08-27 07:41:13 -0700449 virtual const GrGLContext* glContextForTesting() const { return nullptr; }
bsalomon993a4212015-05-29 11:37:25 -0700450
joshualitt8fd844f2015-12-02 13:36:47 -0800451 // This is only to be used by testing code
452 virtual void resetShaderCacheForTesting() const {}
453
joshualittd53a8272014-11-10 16:03:14 -0800454protected:
bsalomon@google.comd302f142011-03-03 13:54:13 +0000455 // Functions used to map clip-respecting stencil tests into normal
456 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000457 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000458 GrStencilFunc func);
459 static void ConvertStencilFuncAndMask(GrStencilFunc func,
460 bool clipInStencil,
461 unsigned int clipBit,
462 unsigned int userBits,
463 unsigned int* ref,
464 unsigned int* mask);
465
bsalomonf0674512015-07-28 13:26:15 -0700466 static void ElevateDrawPreference(GrGpu::DrawPreference* preference,
467 GrGpu::DrawPreference elevation) {
468 GR_STATIC_ASSERT(GrGpu::kCallerPrefersDraw_DrawPreference > GrGpu::kNoDraw_DrawPreference);
469 GR_STATIC_ASSERT(GrGpu::kGpuPrefersDraw_DrawPreference >
470 GrGpu::kCallerPrefersDraw_DrawPreference);
471 GR_STATIC_ASSERT(GrGpu::kRequireDraw_DrawPreference >
472 GrGpu::kGpuPrefersDraw_DrawPreference);
473 *preference = SkTMax(*preference, elevation);
474 }
475
joshualitt93316b92015-10-23 09:08:08 -0700476 void handleDirtyContext() {
477 if (fResetBits) {
478 this->resetContext();
479 }
480 }
481
mtkleinb9eb4ac2015-02-02 18:26:03 -0800482 Stats fStats;
483 SkAutoTDelete<GrPathRendering> fPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800484 // Subclass must initialize this in its constructor.
bsalomon4b91f762015-05-19 09:29:46 -0700485 SkAutoTUnref<const GrCaps> fCaps;
joshualitt3322fa42014-11-07 08:48:51 -0800486
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000487private:
bsalomon@google.comb635d392011-11-05 12:47:43 +0000488 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000489 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000490 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000491
bsalomoncb02b382015-08-12 11:14:50 -0700492 // Called before certain draws in order to guarantee coherent results from dst reads.
493 virtual void xferBarrier(GrRenderTarget*, GrXferBarrierType) = 0;
494
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000495 // overridden by backend-specific derived class to create objects.
egdanielb0e1be22015-04-22 13:27:39 -0700496 // Texture size and sample size will have already been validated in base class before
497 // onCreateTexture/CompressedTexture are called.
498 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
499 GrGpuResource::LifeCycle lifeCycle,
bsalomon5236cf42015-01-14 10:42:08 -0800500 const void* srcData, size_t rowBytes) = 0;
egdanielb0e1be22015-04-22 13:27:39 -0700501 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
502 GrGpuResource::LifeCycle lifeCycle,
krajcevski9c0e6292014-06-02 07:38:14 -0700503 const void* srcData) = 0;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700504 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) = 0;
505 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
506 GrWrapOwnership) = 0;
robertphillips@google.comadacc702013-10-14 21:53:24 +0000507 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) = 0;
508 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) = 0;
jvanverth73063dc2015-12-03 09:15:47 -0800509 virtual GrTransferBuffer* onCreateTransferBuffer(size_t size, TransferType type) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000510
bsalomon63b21962014-11-05 07:05:34 -0800511 // overridden by backend-specific derived class to perform the clear.
egdaniel51c8d402015-08-06 10:54:13 -0700512 virtual void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000513
joshualitt6db519c2014-10-29 08:48:18 -0700514
515 // Overridden by backend specific classes to perform a clear of the stencil clip bits. This is
516 // ONLY used by the the clip target
mtkleinb9eb4ac2015-02-02 18:26:03 -0800517 virtual void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) = 0;
joshualitt6db519c2014-10-29 08:48:18 -0700518
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000519 // overridden by backend-specific derived class to perform the draw call.
bsalomone64eb572015-05-07 11:35:55 -0700520 virtual void onDraw(const DrawArgs&, const GrNonInstancedVertices&) = 0;
bsalomon3e791242014-12-17 13:43:13 -0800521
bsalomone179a912016-01-20 06:18:10 -0800522 virtual bool onMakeCopyForTextureParams(GrTexture* texture, const GrTextureParams&,
523 GrTextureProducer::CopyParams*) const { return false; }
524
bsalomonf0674512015-07-28 13:26:15 -0700525 virtual bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight,
526 size_t rowBytes, GrPixelConfig readConfig, DrawPreference*,
527 ReadPixelTempDrawInfo*) = 0;
528 virtual bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
529 GrPixelConfig srcConfig, DrawPreference*,
530 WritePixelTempDrawInfo*) = 0;
531
bsalomon6cb3cbe2015-07-30 07:34:27 -0700532 // overridden by backend-specific derived class to perform the surface read
533 virtual bool onReadPixels(GrSurface*,
egdaniel6d901da2015-07-30 12:02:15 -0700534 int left, int top,
535 int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000536 GrPixelConfig,
537 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000538 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000539
bsalomon6cb3cbe2015-07-30 07:34:27 -0700540 // overridden by backend-specific derived class to perform the surface write
541 virtual bool onWritePixels(GrSurface*,
542 int left, int top, int width, int height,
543 GrPixelConfig config, const void* buffer,
544 size_t rowBytes) = 0;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000545
jvanverth17aa0472016-01-05 10:41:27 -0800546 // overridden by backend-specific derived class to perform the surface write
547 virtual bool onTransferPixels(GrSurface*,
548 int left, int top, int width, int height,
549 GrPixelConfig config, GrTransferBuffer* buffer,
550 size_t offset, size_t rowBytes) = 0;
551
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000552 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000553 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
554
joshualitt1cbdcde2015-08-21 11:53:29 -0700555 // overridden by backend specific derived class to perform the copy surface
556 virtual bool onCopySurface(GrSurface* dst,
557 GrSurface* src,
558 const SkIRect& srcRect,
559 const SkIPoint& dstPoint) = 0;
560
bsalomon@google.comb635d392011-11-05 12:47:43 +0000561 void resetContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000562 this->onResetContext(fResetBits);
563 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000564 ++fResetTimestamp;
565 }
566
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000567 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000568 uint32_t fResetBits;
joshualitt3322fa42014-11-07 08:48:51 -0800569 // The context owns us, not vice-versa, so this ptr is not ref'ed by Gpu.
570 GrContext* fContext;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000571
kkinnunencabe20c2015-06-01 01:37:26 -0700572 friend class GrPathRendering;
joshualitt3322fa42014-11-07 08:48:51 -0800573 typedef SkRefCnt INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000574};
575
576#endif