blob: 85e398807b331193d772e2cee93f5b1c09ce8e1f [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.com0a208a12013-06-28 18:57:35 +000069 void markContextDirty(uint32_t state = kAll_GrBackendState) {
70 fResetBits |= state;
71 }
reed@google.comac10a2d2010-12-22 21:39:39 +000072
73 void unimpl(const char[]);
74
75 /**
bsalomon@google.com0748f212011-02-01 22:56:16 +000076 * Creates a texture object. If desc width or height is not a power of
77 * two but underlying API requires a power of two texture then srcData
78 * will be embedded in a power of two texture. The extra width and height
79 * is filled as though srcData were rendered clamped into the texture.
reed@google.comac10a2d2010-12-22 21:39:39 +000080 *
bsalomon@google.com8fe72472011-03-30 21:26:44 +000081 * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
bsalomon@google.com1da07462011-03-10 14:51:57 +000082 * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
bsalomon@google.com686bcb82013-04-09 15:04:12 +000083 * on the render target until the texture is destroyed.
bsalomon@google.com1da07462011-03-10 14:51:57 +000084 *
reed@google.comac10a2d2010-12-22 21:39:39 +000085 * @param desc describes the texture to be created.
86 * @param srcData texel data to load texture. Begins with full-size
87 * palette data for paletted textures. Contains width*
88 * height texels. If NULL texture data is uninitialized.
89 *
90 * @return The texture object if successful, otherwise NULL.
91 */
bsalomon@google.comfea37b52011-04-25 15:51:06 +000092 GrTexture* createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093 const void* srcData, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +000094
bsalomon@google.come269f212011-11-07 13:29:52 +000095 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000096 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +000097 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000098 GrTexture* wrapBackendTexture(const GrBackendTextureDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +000099
100 /**
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000101 * Implements GrContext::wrapBackendTexture
bsalomon@google.come269f212011-11-07 13:29:52 +0000102 */
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000103 GrRenderTarget* wrapBackendRenderTarget(const GrBackendRenderTargetDesc&);
bsalomon@google.come269f212011-11-07 13:29:52 +0000104
105 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000106 * Creates a vertex buffer.
107 *
108 * @param size size in bytes of the vertex buffer
109 * @param dynamic hints whether the data will be frequently changed
110 * by either GrVertexBuffer::lock or
111 * GrVertexBuffer::updateData.
112 *
113 * @return The vertex buffer if successful, otherwise NULL.
114 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000115 GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
117 /**
118 * Creates an index buffer.
119 *
120 * @param size size in bytes of the index buffer
121 * @param dynamic hints whether the data will be frequently changed
122 * by either GrIndexBuffer::lock or
123 * GrIndexBuffer::updateData.
124 *
125 * @return The index buffer if successful, otherwise NULL.
126 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000127 GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
129 /**
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000130 * Creates a path object that can be stenciled using stencilPath(). It is
131 * only legal to call this if the caps report support for path stenciling.
132 */
133 GrPath* createPath(const SkPath& path);
134
135 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 * Returns an index buffer that can be used to render quads.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000137 * Six indices per quad: 0, 1, 2, 0, 2, 3, etc.
138 * The max number of quads can be queried using GrIndexBuffer::maxQuads().
bsalomon@google.com47059542012-06-06 20:51:20 +0000139 * Draw with kTriangles_GrPrimitiveType
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000140 * @ return the quad index buffer
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 */
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000142 const GrIndexBuffer* getQuadIndexBuffer() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
144 /**
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000145 * Resolves MSAA.
146 */
147 void resolveRenderTarget(GrRenderTarget* target);
148
149 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 * Ensures that the current render target is actually set in the
151 * underlying 3D API. Used when client wants to use 3D API to directly
152 * render to the RT.
153 */
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000154 void forceRenderTargetFlush();
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000156 /**
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000157 * Gets a preferred 8888 config to use for writing/reading pixel data to/from a surface with
158 * config surfaceConfig. The returned config must have at least as many bits per channel as the
159 * readConfig or writeConfig param.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000160 */
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000161 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig,
162 GrPixelConfig surfaceConfig) const {
163 return readConfig;
164 }
165 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig,
166 GrPixelConfig surfaceConfig) const {
167 return writeConfig;
168 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000169
170 /**
bsalomon@google.com9c680582013-02-06 18:17:50 +0000171 * Called before uploading writing pixels to a GrTexture when the src pixel config doesn't
172 * match the texture's config.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000173 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000174 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const = 0;
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000175
176 /**
bsalomon@google.comc4364992011-11-07 15:54:49 +0000177 * OpenGL's readPixels returns the result bottom-to-top while the skia
178 * API is top-to-bottom. Thus we have to do a y-axis flip. The obvious
179 * solution is to have the subclass do the flip using either the CPU or GPU.
180 * However, the caller (GrContext) may have transformations to apply and can
181 * simply fold in the y-flip for free. On the other hand, the subclass may
182 * be able to do it for free itself. For example, the subclass may have to
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183 * do memcpys to handle rowBytes that aren't tight. It could do the y-flip
bsalomon@google.comc4364992011-11-07 15:54:49 +0000184 * concurrently.
185 *
186 * This function returns true if a y-flip is required to put the pixels in
187 * top-to-bottom order and the subclass cannot do it for free.
188 *
189 * See read pixels for the params
190 * @return true if calling readPixels with the same set of params will
191 * produce bottom-to-top data
192 */
193 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
194 int left, int top,
195 int width, int height,
196 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000197 size_t rowBytes) const = 0;
198 /**
199 * This should return true if reading a NxM rectangle of pixels from a
200 * render target is faster if the target has dimensons N and M and the read
201 * rectangle has its top-left at 0,0.
202 */
203 virtual bool fullReadPixelsIsFasterThanPartial() const { return false; };
bsalomon@google.comc4364992011-11-07 15:54:49 +0000204
205 /**
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000206 * Reads a rectangle of pixels from a render target.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000207 *
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000208 * @param renderTarget the render target to read from. NULL means the
209 * current render target.
210 * @param left left edge of the rectangle to read (inclusive)
211 * @param top top edge of the rectangle to read (inclusive)
212 * @param width width of rectangle to read in pixels.
213 * @param height height of rectangle to read in pixels.
214 * @param config the pixel config of the destination buffer
215 * @param buffer memory to read the rectangle into.
bsalomon@google.comc6980972011-11-02 19:57:21 +0000216 * @param rowBytes the number of bytes between consecutive rows. Zero
217 * means rows are tightly packed.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000218 * @param invertY buffer should be populated bottom-to-top as opposed
219 * to top-to-bottom (skia's usual order)
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220 *
221 * @return true if the read succeeded, false if not. The read can fail
222 * because of a unsupported pixel config or because no render
223 * target is currently set.
224 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000225 bool readPixels(GrRenderTarget* renderTarget,
226 int left, int top, int width, int height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000227 GrPixelConfig config, void* buffer, size_t rowBytes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
bsalomon@google.com6f379512011-11-16 20:36:03 +0000229 /**
230 * Updates the pixels in a rectangle of a texture.
bsalomon@google.coma85449d2011-11-19 02:36:05 +0000231 *
bsalomon@google.com6f379512011-11-16 20:36:03 +0000232 * @param left left edge of the rectangle to write (inclusive)
233 * @param top top edge of the rectangle to write (inclusive)
234 * @param width width of rectangle to write in pixels.
235 * @param height height of rectangle to write in pixels.
236 * @param config the pixel config of the source buffer
237 * @param buffer memory to read pixels from
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000238 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +0000239 * means rows are tightly packed.
240 */
bsalomon@google.com9c680582013-02-06 18:17:50 +0000241 bool writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000242 int left, int top, int width, int height,
243 GrPixelConfig config, const void* buffer,
244 size_t rowBytes);
245
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000246 /**
247 * Called to tell Gpu object that all GrResources have been lost and should
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000248 * be abandoned. Overrides must call INHERITED::abandonResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000249 */
junov@google.com53a55842011-06-08 22:55:10 +0000250 virtual void abandonResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000251
252 /**
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000253 * Called to tell Gpu object to release all GrResources. Overrides must call
254 * INHERITED::releaseResources().
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000255 */
256 void releaseResources();
257
258 /**
259 * Add resource to list of resources. Should only be called by GrResource.
260 * @param resource the resource to add.
261 */
262 void insertResource(GrResource* resource);
263
264 /**
265 * Remove resource from list of resources. Should only be called by
266 * GrResource.
267 * @param resource the resource to remove.
268 */
269 void removeResource(GrResource* resource);
270
bsalomon@google.com471d4712011-08-23 15:45:25 +0000271 // GrDrawTarget overrides
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272 virtual void clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000273 GrColor color,
274 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com471d4712011-08-23 15:45:25 +0000275
robertphillips@google.comff175842012-05-14 19:31:39 +0000276 virtual void purgeResources() SK_OVERRIDE {
277 // The clip mask manager can rebuild all its clip masks so just
278 // get rid of them all.
279 fClipMaskManager.releaseResources();
280 }
281
bsalomon@google.com80d09b92011-11-05 21:21:13 +0000282 // After the client interacts directly with the 3D context state the GrGpu
283 // must resync its internal state and assumptions about 3D context state.
284 // Each time this occurs the GrGpu bumps a timestamp.
285 // state of the 3D context
286 // At 10 resets / frame and 60fps a 64bit timestamp will overflow in about
287 // a billion years.
288 typedef uint64_t ResetTimestamp;
289
290 // This timestamp is always older than the current timestamp
291 static const ResetTimestamp kExpiredTimestamp = 0;
292 // Returns a timestamp based on the number of times the context was reset.
293 // This timestamp can be used to lazily detect when cached 3D context state
294 // is dirty.
295 ResetTimestamp getResetTimestamp() const {
296 return fResetTimestamp;
297 }
298
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000299 /**
300 * Can the provided configuration act as a color render target?
301 */
302 bool isConfigRenderable(GrPixelConfig config) const {
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000303 GrAssert(kGrPixelConfigCnt > config);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000304 return fConfigRenderSupport[config];
305 }
306
bsalomon@google.coma3201942012-06-21 19:58:20 +0000307 /**
308 * These methods are called by the clip manager's setupClipping function
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000309 * which (called as part of GrGpu's implementation of onDraw and
bsalomon@google.coma3201942012-06-21 19:58:20 +0000310 * onStencilPath member functions.) The GrGpu subclass should flush the
311 * stencil state to the 3D API in its implementation of flushGraphicsState.
312 */
313 void enableScissor(const GrIRect& rect) {
314 fScissorState.fEnabled = true;
315 fScissorState.fRect = rect;
316 }
317 void disableScissor() { fScissorState.fEnabled = false; }
318
319 /**
320 * Like the scissor methods above this is called by setupClipping and
321 * should be flushed by the GrGpu subclass in flushGraphicsState. These
322 * stencil settings should be used in place of those on the GrDrawState.
323 * They have been adjusted to account for any interactions between the
324 * GrDrawState's stencil settings and stencil clipping.
325 */
326 void setStencilSettings(const GrStencilSettings& settings) {
327 fStencilSettings = settings;
328 }
329 void disableStencil() { fStencilSettings.setDisabled(); }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000330
331 // GrGpu subclass sets clip bit in the stencil buffer. The subclass is
332 // free to clear the remaining bits to zero if masked clears are more
333 // expensive than clearing all bits.
334 virtual void clearStencilClip(const GrIRect& rect, bool insideClip) = 0;
335
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000336 enum PrivateDrawStateStateBits {
337 kFirstBit = (GrDrawState::kLastPublicStateBit << 1),
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338
339 kModifyStencilClip_StateBit = kFirstBit, // allows draws to modify
340 // stencil bits used for
341 // clipping.
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 };
343
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000344protected:
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000345 enum DrawType {
346 kDrawPoints_DrawType,
347 kDrawLines_DrawType,
348 kDrawTriangles_DrawType,
349 kStencilPath_DrawType,
350 };
351
352 DrawType PrimTypeToDrawType(GrPrimitiveType type) {
353 switch (type) {
354 case kTriangles_GrPrimitiveType:
355 case kTriangleStrip_GrPrimitiveType:
356 case kTriangleFan_GrPrimitiveType:
357 return kDrawTriangles_DrawType;
358 case kPoints_GrPrimitiveType:
359 return kDrawPoints_DrawType;
360 case kLines_GrPrimitiveType:
361 case kLineStrip_GrPrimitiveType:
362 return kDrawLines_DrawType;
363 default:
364 GrCrash("Unexpected primitive type");
365 return kDrawTriangles_DrawType;
366 }
367 }
368
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000369 // prepares clip flushes gpu state before a draw
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000370 bool setupClipAndFlushState(DrawType,
371 const GrDeviceCoordTexture* dstCopy,
372 GrDrawState::AutoRestoreEffects* are);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 // Functions used to map clip-respecting stencil tests into normal
375 // stencil funcs supported by GPUs.
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000376 static GrStencilFunc ConvertStencilFunc(bool stencilInClip,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000377 GrStencilFunc func);
378 static void ConvertStencilFuncAndMask(GrStencilFunc func,
379 bool clipInStencil,
380 unsigned int clipBit,
381 unsigned int userBits,
382 unsigned int* ref,
383 unsigned int* mask);
384
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000385 GrClipMaskManager fClipMaskManager;
386
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387 struct GeometryPoolState {
388 const GrVertexBuffer* fPoolVertexBuffer;
389 int fPoolStartVertex;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000390
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000391 const GrIndexBuffer* fPoolIndexBuffer;
392 int fPoolStartIndex;
393 };
rmistry@google.comd6176b02012-08-23 18:14:13 +0000394 const GeometryPoolState& getGeomPoolState() {
395 return fGeomPoolStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000396 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397
bsalomon@google.coma3201942012-06-21 19:58:20 +0000398 // The state of the scissor is controlled by the clip manager
399 struct ScissorState {
400 bool fEnabled;
401 GrIRect fRect;
402 } fScissorState;
403
404 // The final stencil settings to use as determined by the clip manager.
405 GrStencilSettings fStencilSettings;
406
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000407 // Derived classes need access to this so they can fill it out in their
408 // constructors
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +0000409 bool fConfigRenderSupport[kGrPixelConfigCnt];
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000410
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000411 // Helpers for setting up geometry state
412 void finalizeReservedVertices();
413 void finalizeReservedIndices();
414
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000415private:
416 // GrDrawTarget overrides
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000417 virtual bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000418 virtual bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
419 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
420 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
421 virtual void onSetVertexSourceToArray(const void* vertexArray, int vertexCount) SK_OVERRIDE;
422 virtual void onSetIndexSourceToArray(const void* indexArray, int indexCount) SK_OVERRIDE;
423 virtual void releaseVertexArray() SK_OVERRIDE;
424 virtual void releaseIndexArray() SK_OVERRIDE;
425 virtual void geometrySourceWillPush() SK_OVERRIDE;
426 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
427
428
bsalomon@google.comb635d392011-11-05 12:47:43 +0000429 // called when the 3D context state is unknown. Subclass should emit any
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000430 // assumed 3D context state and dirty any state cache.
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000431 virtual void onResetContext(uint32_t resetBits) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000432
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000433 // overridden by backend-specific derived class to create objects.
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000434 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000435 const void* srcData,
436 size_t rowBytes) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000437 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) = 0;
438 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) = 0;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000439 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size, bool dynamic) = 0;
440 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size, bool dynamic) = 0;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000441 virtual GrPath* onCreatePath(const SkPath& path) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000442
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000443 // overridden by backend-specific derived class to perform the clear and
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000444 // clearRect. NULL rect means clear whole target.
445 virtual void onClear(const GrIRect* rect, GrColor color) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000446
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000447 // overridden by backend-specific derived class to perform the draw call.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000448 virtual void onGpuDraw(const DrawInfo&) = 0;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000449 // when GrDrawTarget::stencilPath is called the draw state's current stencil
450 // settings are ignored. Instead the GrGpu decides the stencil rules
451 // necessary to stencil the path. These are still subject to filtering by
452 // the clip mask manager.
453 virtual void setStencilPathSettings(const GrPath&,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000454 SkPath::FillType,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000455 GrStencilSettings* settings) = 0;
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000456 // overridden by backend-specific derived class to perform the path stenciling.
sugoi@google.com12b4e272012-12-06 20:13:11 +0000457 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000458
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000459 // overridden by backend-specific derived class to perform flush
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000460 virtual void onForceRenderTargetFlush() = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000461
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000462 // overridden by backend-specific derived class to perform the read pixels.
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000463 virtual bool onReadPixels(GrRenderTarget* target,
464 int left, int top, int width, int height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000465 GrPixelConfig,
466 void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000467 size_t rowBytes) = 0;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000468
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000469 // overridden by backend-specific derived class to perform the texture update
bsalomon@google.com9c680582013-02-06 18:17:50 +0000470 virtual bool onWriteTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000471 int left, int top, int width, int height,
472 GrPixelConfig config, const void* buffer,
473 size_t rowBytes) = 0;
474
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000475 // overridden by backend-specific derived class to perform the resolve
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000476 virtual void onResolveRenderTarget(GrRenderTarget* target) = 0;
477
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000478 // width and height may be larger than rt (if underlying API allows it).
479 // Should attach the SB to the RT. Returns false if compatible sb could
480 // not be created.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000481 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) = 0;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000482
483 // attaches an existing SB to an existing RT.
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000484 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
486 // The GrGpu typically records the clients requested state and then flushes
487 // deltas from previous state at draw time. This function does the
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000488 // backend-specific flush of the state.
reed@google.comac10a2d2010-12-22 21:39:39 +0000489 // returns false if current state is unsupported.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000490 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000491
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000492 // clears the entire stencil buffer to 0
493 virtual void clearStencil() = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000495 // Given a rt, find or create a stencil buffer and attach it
496 bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
497
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000498 // GrDrawTarget overrides
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000499 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000500 virtual void onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000501 SkPath::FillType) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000502
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000503 // readies the pools to provide vertex/index data.
504 void prepareVertexPool();
505 void prepareIndexPool();
506
bsalomon@google.comb635d392011-11-05 12:47:43 +0000507 void resetContext() {
bsalomon@google.comc8f7f472012-06-18 13:44:51 +0000508 // We call this because the client may have messed with the
509 // stencil buffer. Perhaps we should detect whether it is a
510 // internally created stencil buffer and if so skip the invalidate.
511 fClipMaskManager.invalidateStencilMask();
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000512 this->onResetContext(fResetBits);
513 fResetBits = 0;
bsalomon@google.comb635d392011-11-05 12:47:43 +0000514 ++fResetTimestamp;
515 }
516
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000517 void handleDirtyContext() {
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000518 if (fResetBits) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000519 this->resetContext();
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000520 }
521 }
522
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000523 enum {
524 kPreallocGeomPoolStateStackCnt = 4,
525 };
526 typedef SkTInternalLList<GrResource> ResourceList;
527 SkSTArray<kPreallocGeomPoolStateStackCnt, GeometryPoolState, true> fGeomPoolStateStack;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000528 ResetTimestamp fResetTimestamp;
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000529 uint32_t fResetBits;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000530 GrVertexBufferAllocPool* fVertexPool;
531 GrIndexBufferAllocPool* fIndexPool;
532 // counts number of uses of vertex/index pool in the geometry stack
533 int fVertexPoolUseCnt;
534 int fIndexPoolUseCnt;
bsalomon@google.com64386952013-02-08 21:22:44 +0000535 // these are mutable so they can be created on-demand
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000536 mutable GrIndexBuffer* fQuadIndexBuffer;
skia.committer@gmail.com66a4a4c2013-05-11 07:01:34 +0000537 // Used to abandon/release all resources created by this GrGpu. TODO: Move this
bsalomon@google.com76202b82013-05-10 19:08:22 +0000538 // functionality to GrResourceCache.
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000539 ResourceList fResourceList;
540
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000541 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000542};
543
544#endif