blob: ade64312b866ac0f831ad9b20e413143cd6c1ba9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGpu_DEFINED
11#define GrGpu_DEFINED
12
bsalomon@google.com669fdc42011-04-05 17:08:27 +000013#include "GrDrawTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrRect.h"
15#include "GrRefCnt.h"
robertphillips@google.com1e945b72012-04-16 18:03:03 +000016#include "GrClipMaskManager.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
sugoi@google.com12b4e272012-12-06 20:13:11 +000018#include "SkPath.h"
19
bsalomon@google.com669fdc42011-04-05 17:08:27 +000020class GrContext;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021class GrIndexBufferAllocPool;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +000022class GrPath;
bsalomon@google.com30085192011-08-19 15:42:31 +000023class GrPathRenderer;
24class GrPathRendererChain;
bsalomon@google.com8fe72472011-03-30 21:26:44 +000025class GrResource;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000026class GrStencilBuffer;
bsalomon@google.com669fdc42011-04-05 17:08:27 +000027class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29class GrGpu : public GrDrawTarget {
30
31public:
bsalomon@google.com18c9c192011-09-22 21:01:31 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000034 * Additional blend coefficients for dual source blending, not exposed
bsalomon@google.com271cffc2011-05-20 14:13:56 +000035 * through GrPaint/GrContext.
36 */
37 enum ExtendedBlendCoeffs {
38 // source 2 refers to second output color when
39 // using dual source blending.
bsalomon@google.com47059542012-06-06 20:51:20 +000040 kS2C_GrBlendCoeff = kPublicGrBlendCoeffCount,
41 kIS2C_GrBlendCoeff,
42 kS2A_GrBlendCoeff,
43 kIS2A_GrBlendCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000044
bsalomon@google.com47059542012-06-06 20:51:20 +000045 kTotalGrBlendCoeffCount
bsalomon@google.com271cffc2011-05-20 14:13:56 +000046 };
47
48 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000049 * Create an instance of GrGpu that matches the specified backend. If the requested backend is
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000050 * not supported (at compile-time or run-time) this returns NULL. The context will not be
51 * fully constructed and should not be used by GrGpu until after this function returns.
reed@google.comac10a2d2010-12-22 21:39:39 +000052 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000053 static GrGpu* Create(GrBackend, GrBackendContext, GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000054
55 ////////////////////////////////////////////////////////////////////////////
56
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000057 GrGpu(GrContext* context);
reed@google.comac10a2d2010-12-22 21:39:39 +000058 virtual ~GrGpu();
59
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000060 GrContext* getContext() { return this->INHERITED::getContext(); }
61 const GrContext* getContext() const { return this->INHERITED::getContext(); }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000062
reed@google.comac10a2d2010-12-22 21:39:39 +000063 /**
64 * The GrGpu object normally assumes that no outsider is setting state
65 * within the underlying 3D API's context/device/whatever. This call informs
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000066 * the GrGpu that the state was modified and it shouldn't make assumptions
67 * about the state.
reed@google.comac10a2d2010-12-22 21:39:39 +000068 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000069 void markContextDirty() { fContextIsDirty = true; }
reed@google.comac10a2d2010-12-22 21:39:39 +000070
71 void unimpl(const char[]);
72
73 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000074 * Creates a texture object. If desc width or height is not a power of
75 * two but underlying API requires a power of two texture then srcData
76 * will be embedded in a power of two texture. The extra width and height
77 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000078 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000080 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
bsalomon@google.com686bcb82013-04-09 15:04:12 +000081 * on the render target until the texture is destroyed.
bsalomon@google.com1da07462011-03-10 14:51:57 +000082 *
reed@google.comac10a2d2010-12-22 21:39:39 +000083 * @param desc describes the texture to be created.
84 * @param srcData texel data to load texture. Begins with full-size
85 * palette data for paletted textures. Contains width*
86 * height texels. If NULL texture data is uninitialized.
87 *
88 * @return The texture object if successful, otherwise NULL.
89 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +000090 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000091 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.come269f212011-11-07 13:29:52 +000093 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000094 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000095 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000096 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000097
98 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000099 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000100 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000101 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000102
103 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 * Creates a vertex buffer.
105 *
106 * @param size size in bytes of the vertex buffer
107 * @param dynamic hints whether the data will be frequently changed
108 * by either GrVertexBuffer::lock or
109 * GrVertexBuffer::updateData.
110 *
111 * @return The vertex buffer if successful, otherwise NULL.
112 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000113 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
115 /**
116 * Creates an index buffer.
117 *
118 * @param size size in bytes of the index buffer
119 * @param dynamic hints whether the data will be frequently changed
120 * by either GrIndexBuffer::lock or
121 * GrIndexBuffer::updateData.
122 *
123 * @return The index buffer if successful, otherwise NULL.
124 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000125 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
127 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000128 * Creates a path object that can be stenciled using stencilPath(). It is
129 * only legal to call this if the caps report support for path stenciling.
130 */
131 GrPath* createPath(const SkPath& path);
132
133 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000135 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
136 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000137 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000138 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000139 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000140 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000141
142 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000143 * Resolves MSAA.
144 */
145 void resolveRenderTarget(GrRenderTarget* target);
146
147 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 * Ensures that the current render target is actually set in the
149 * underlying 3D API. Used when client wants to use 3D API to directly
150 * render to the RT.
151 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000154 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000155 * Gets a preferred 8888 config to use for writing / reading pixel data. The returned config
156 * must have at least as many bits per channel as the config param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000157 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000158 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config) const { return config; }
159 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config) const { return config; }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000160
161 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000162 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
163 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000164 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000165 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000166
167 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000168 * OpenGL's readPixels returns the result bottom-to-top while the skia
169 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
170 * solution is to have the subclass do the flip using either the CPU or GPU.
171 * However, the caller (GrContext) may have transformations to apply and can
172 * simply fold in the y-flip for free. On the other hand, the subclass may
173 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000174 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000175 * concurrently.
176 *
177 * This function returns true if a y-flip is required to put the pixels in
178 * top-to-bottom order and the subclass cannot do it for free.
179 *
180 * See read pixels for the params
181 * @return true if calling readPixels with the same set of params will
182 * produce bottom-to-top data
183 */
184 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
185 int left, int top,
186 int width, int height,
187 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000188 size_t rowBytes) const = 0;
189 /**
190 * This should return true if reading a NxM rectangle of pixels from a
191 * render target is faster if the target has dimensons N and M and the read
192 * rectangle has its top-left at 0,0.
193 */
194 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000195
196 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000197 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000198 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000199 * @param renderTarget the render target to read from. NULL means the
200 * current render target.
201 * @param left left edge of the rectangle to read (inclusive)
202 * @param top top edge of the rectangle to read (inclusive)
203 * @param width width of rectangle to read in pixels.
204 * @param height height of rectangle to read in pixels.
205 * @param config the pixel config of the destination buffer
206 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000207 * @param rowBytes the number of bytes between consecutive rows. Zero
208 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000209 * @param invertY buffer should be populated bottom-to-top as opposed
210 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211 *
212 * @return true if the read succeeded, false if not. The read can fail
213 * because of a unsupported pixel config or because no render
214 * target is currently set.
215 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000216 bool readPixels(GrRenderTarget* renderTarget,
217 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000218 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
bsalomon@google.com6f379512011-11-16 20:36:03 +0000220 /**
221 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000222 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000223 * @param left left edge of the rectangle to write (inclusive)
224 * @param top top edge of the rectangle to write (inclusive)
225 * @param width width of rectangle to write in pixels.
226 * @param height height of rectangle to write in pixels.
227 * @param config the pixel config of the source buffer
228 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000229 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000230 * means rows are tightly packed.
231 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000232 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000233 int left, int top, int width, int height,
234 GrPixelConfig config, const void* buffer,
235 size_t rowBytes);
236
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000237 /**
238 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000239 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000240 */
junov@google.com53a55842011-06-08 22:55:10 +0000241 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000242
243 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000244 * Called to tell Gpu object to release all GrResources. Overrides must call
245 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000246 */
247 void releaseResources();
248
249 /**
250 * Add resource to list of resources. Should only be called by GrResource.
251 * @param resource the resource to add.
252 */
253 void insertResource(GrResource* resource);
254
255 /**
256 * Remove resource from list of resources. Should only be called by
257 * GrResource.
258 * @param resource the resource to remove.
259 */
260 void removeResource(GrResource* resource);
261
bsalomon@google.com471d4712011-08-23 15:45:25 +0000262 // GrDrawTarget overrides
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000264 GrColor color,
265 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000266
robertphillips@google.comff175842012-05-14 19:31:39 +0000267 virtual void purgeResources() SK_OVERRIDE {
268 // The clip mask manager can rebuild all its clip masks so just
269 // get rid of them all.
270 fClipMaskManager.releaseResources();
271 }
272
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000273 // After the client interacts directly with the 3D context state the GrGpu
274 // must resync its internal state and assumptions about 3D context state.
275 // Each time this occurs the GrGpu bumps a timestamp.
276 // state of the 3D context
277 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
278 // a billion years.
279 typedef uint64_t ResetTimestamp;
280
281 // This timestamp is always older than the current timestamp
282 static const ResetTimestamp kExpiredTimestamp = 0;
283 // Returns a timestamp based on the number of times the context was reset.
284 // This timestamp can be used to lazily detect when cached 3D context state
285 // is dirty.
286 ResetTimestamp getResetTimestamp() const {
287 return fResetTimestamp;
288 }
289
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000290 /**
291 * Can the provided configuration act as a color render target?
292 */
293 bool isConfigRenderable(GrPixelConfig config) const {
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000294 GrAssert(kGrPixelConfigCnt > config);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000295 return fConfigRenderSupport[config];
296 }
297
bsalomon@google.coma3201942012-06-21 19:58:20 +0000298 /**
299 * These methods are called by the clip manager's setupClipping function
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000300 * which (called as part of GrGpu's implementation of onDraw and
bsalomon@google.coma3201942012-06-21 19:58:20 +0000301 * onStencilPath member functions.) The GrGpu subclass should flush the
302 * stencil state to the 3D API in its implementation of flushGraphicsState.
303 */
304 void enableScissor(const GrIRect& rect) {
305 fScissorState.fEnabled = true;
306 fScissorState.fRect = rect;
307 }
308 void disableScissor() { fScissorState.fEnabled = false; }
309
310 /**
311 * Like the scissor methods above this is called by setupClipping and
312 * should be flushed by the GrGpu subclass in flushGraphicsState. These
313 * stencil settings should be used in place of those on the GrDrawState.
314 * They have been adjusted to account for any interactions between the
315 * GrDrawState's stencil settings and stencil clipping.
316 */
317 void setStencilSettings(const GrStencilSettings& settings) {
318 fStencilSettings = settings;
319 }
320 void disableStencil() { fStencilSettings.setDisabled(); }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000321
322 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
323 // free to clear the remaining bits to zero if masked clears are more
324 // expensive than clearing all bits.
325 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
326
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000327 enum PrivateDrawStateStateBits {
328 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329
330 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
331 // stencil bits used for
332 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 };
334
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000335protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000336 enum DrawType {
337 kDrawPoints_DrawType,
338 kDrawLines_DrawType,
339 kDrawTriangles_DrawType,
340 kStencilPath_DrawType,
341 };
342
343 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
344 switch (type) {
345 case kTriangles_GrPrimitiveType:
346 case kTriangleStrip_GrPrimitiveType:
347 case kTriangleFan_GrPrimitiveType:
348 return kDrawTriangles_DrawType;
349 case kPoints_GrPrimitiveType:
350 return kDrawPoints_DrawType;
351 case kLines_GrPrimitiveType:
352 case kLineStrip_GrPrimitiveType:
353 return kDrawLines_DrawType;
354 default:
355 GrCrash("Unexpected primitive type");
356 return kDrawTriangles_DrawType;
357 }
358 }
359
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000360 // prepares clip flushes gpu state before a draw
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000361 bool setupClipAndFlushState(DrawType, const GrDeviceCoordTexture* dstCopy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 // Functions used to map clip-respecting stencil tests into normal
364 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000365 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000366 GrStencilFunc func);
367 static void ConvertStencilFuncAndMask(GrStencilFunc func,
368 bool clipInStencil,
369 unsigned int clipBit,
370 unsigned int userBits,
371 unsigned int* ref,
372 unsigned int* mask);
373
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000374 GrClipMaskManager fClipMaskManager;
375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376 struct GeometryPoolState {
377 const GrVertexBuffer* fPoolVertexBuffer;
378 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000379
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380 const GrIndexBuffer* fPoolIndexBuffer;
381 int fPoolStartIndex;
382 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383 const GeometryPoolState& getGeomPoolState() {
384 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000385 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000386
bsalomon@google.coma3201942012-06-21 19:58:20 +0000387 // The state of the scissor is controlled by the clip manager
388 struct ScissorState {
389 bool fEnabled;
390 GrIRect fRect;
391 } fScissorState;
392
393 // The final stencil settings to use as determined by the clip manager.
394 GrStencilSettings fStencilSettings;
395
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000396 // Derived classes need access to this so they can fill it out in their
397 // constructors
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000398 bool fConfigRenderSupport[kGrPixelConfigCnt];
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000399
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400 // Helpers for setting up geometry state
401 void finalizeReservedVertices();
402 void finalizeReservedIndices();
403
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000404private:
405 // GrDrawTarget overrides
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000406 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000407 virtual bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
408 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
409 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
410 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) SK_OVERRIDE;
411 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) SK_OVERRIDE;
412 virtual void releaseVertexArray() SK_OVERRIDE;
413 virtual void releaseIndexArray() SK_OVERRIDE;
414 virtual void geometrySourceWillPush() SK_OVERRIDE;
415 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
416
417
bsalomon@google.comb635d392011-11-05 12:47:43 +0000418 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000419 // assumed 3D context state and dirty any state cache.
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000420 virtual void onResetContext() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000421
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000422 // overridden by backend-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000423 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000424 const void* srcData,
425 size_t rowBytes) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000426 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
427 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000428 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, bool dynamic) = 0;
429 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, bool dynamic) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000430 virtual GrPath* onCreatePath(const SkPath& path) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000431
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000432 // overridden by backend-specific derived class to perform the clear and
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000433 // clearRect. NULL rect means clear whole target.
434 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000435
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000436 // overridden by backend-specific derived class to perform the draw call.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000437 virtual void onGpuDraw(const DrawInfo&) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000438 // when GrDrawTarget::stencilPath is called the draw state's current stencil
439 // settings are ignored. Instead the GrGpu decides the stencil rules
440 // necessary to stencil the path. These are still subject to filtering by
441 // the clip mask manager.
442 virtual void setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000443 SkPath::FillType,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000444 GrStencilSettings* settings) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000445 // overridden by backend-specific derived class to perform the path stenciling.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000446 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000448 // overridden by backend-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000449 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000450
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000451 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000452 virtual bool onReadPixels(GrRenderTarget* target,
453 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000454 GrPixelConfig,
455 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000456 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000457
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000458 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000459 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000460 int left, int top, int width, int height,
461 GrPixelConfig config, const void* buffer,
462 size_t rowBytes) = 0;
463
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000464 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000465 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
466
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000467 // width and height may be larger than rt (if underlying API allows it).
468 // Should attach the SB to the RT. Returns false if compatible sb could
469 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000470 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000471
472 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000473 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000474
475 // The GrGpu typically records the clients requested state and then flushes
476 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000477 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 // returns false if current state is unsupported.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000479 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000481 // clears the entire stencil buffer to 0
482 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000484 // Given a rt, find or create a stencil buffer and attach it
485 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
486
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487 // GrDrawTarget overrides
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000488 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000489 virtual void onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000490 SkPath::FillType) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000491
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000492 // readies the pools to provide vertex/index data.
493 void prepareVertexPool();
494 void prepareIndexPool();
495
bsalomon@google.comb635d392011-11-05 12:47:43 +0000496 void resetContext() {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000497 // We call this because the client may have messed with the
498 // stencil buffer. Perhaps we should detect whether it is a
499 // internally created stencil buffer and if so skip the invalidate.
500 fClipMaskManager.invalidateStencilMask();
bsalomon@google.comb635d392011-11-05 12:47:43 +0000501 this->onResetContext();
502 ++fResetTimestamp;
503 }
504
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000505 void handleDirtyContext() {
506 if (fContextIsDirty) {
507 this->resetContext();
508 fContextIsDirty = false;
509 }
510 }
511
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000512 enum {
513 kPreallocGeomPoolStateStackCnt = 4,
514 };
515 typedef SkTInternalLList<GrResource> ResourceList;
516 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000517 ResetTimestamp fResetTimestamp;
518 GrVertexBufferAllocPool* fVertexPool;
519 GrIndexBufferAllocPool* fIndexPool;
520 // counts number of uses of vertex/index pool in the geometry stack
521 int fVertexPoolUseCnt;
522 int fIndexPoolUseCnt;
bsalomon@google.com64386952013-02-08 21:22:44 +0000523 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000524 mutable GrIndexBuffer* fQuadIndexBuffer;
525 bool fContextIsDirty;
skia.committer@gmail.com66a4a4c2013-05-11 07:01:34 +0000526 // Used to abandon/release all resources created by this GrGpu. TODO: Move this
bsalomon@google.com76202b82013-05-10 19:08:22 +0000527 // functionality to GrResourceCache.
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000528 ResourceList fResourceList;
529
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531};
532
533#endif