blob: 24d491d63f7e949144d04d677c8a4cb630210149 [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
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#ifndef GrGpuGL_DEFINED
11#define GrGpuGL_DEFINED
12
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013#include "GrDrawState.h"
14#include "GrGpu.h"
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000015#include "GrGLContextInfo.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000016#include "GrGLIndexBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrGLIRect.h"
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000018#include "GrGLProgram.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000019#include "GrGLStencilBuffer.h"
20#include "GrGLTexture.h"
21#include "GrGLVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
23class GrGpuGL : public GrGpu {
24public:
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000025 GrGpuGL(const GrGLContextInfo& ctxInfo);
reed@google.comac10a2d2010-12-22 21:39:39 +000026 virtual ~GrGpuGL();
27
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000028 const GrGLInterface* glInterface() const {
29 return fGLContextInfo.interface();
30 }
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000031 GrGLBinding glBinding() const { return fGLContextInfo.binding(); }
32 GrGLVersion glVersion() const { return fGLContextInfo.version(); }
33 GrGLSLGeneration glslGeneration() const {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000034 return fGLContextInfo.glslGeneration();
35 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000036
bsalomon@google.com56d11e02011-11-30 19:59:08 +000037 // GrGpu overrides
bsalomon@google.com0a97be22011-11-08 19:20:57 +000038 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000039 const SK_OVERRIDE;
bsalomon@google.coma85449d2011-11-19 02:36:05 +000040 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000041 const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000042 virtual bool readPixelsWillPayForYFlip(
43 GrRenderTarget* renderTarget,
44 int left, int top,
45 int width, int height,
46 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000047 size_t rowBytes) const SK_OVERRIDE;
48 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000049
bsalomon@google.coma91e9232012-02-23 15:39:54 +000050 virtual bool canPreserveReadWriteUnpremulPixels() SK_OVERRIDE;
51
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000052 virtual void abandonResources() SK_OVERRIDE;
53
54 bool programUnitTest();
55
bsalomon@google.com88939ae2011-12-14 15:58:11 +000056protected:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000057
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +000058 enum TriState {
59 kNo_TriState,
60 kYes_TriState,
61 kUnknown_TriState
62 };
63
reed@google.comac10a2d2010-12-22 21:39:39 +000064 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000065 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000066 GrVertexLayout fVertexLayout;
67 const GrVertexBuffer* fVertexBuffer;
68 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000069 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000070 } fHWGeometryState;
71
bsalomon@google.coma91e9232012-02-23 15:39:54 +000072 enum UnpremulConversion {
73 kUpOnWrite_DownOnRead_UnpremulConversion,
74 kDownOnWrite_UpOnRead_UnpremulConversion
75 } fUnpremulConversion;
76
bsalomon@google.combcc84862012-05-22 15:44:03 +000077 // The current render target and textures are bound by GrGpuGL when it
78 // flushes state to GL. After the bindings occur the variables that track
79 // the current GL state are updated to reflect the new bindings. However,
80 // the GrGpuGL subclass may have subsequent GL state manipulation it must
81 // perform whenever RT or textures change. So the GrGpuGL will set these
82 // dirty flags when it changes the RT or texture bindings. The subclass can
83 // use them to trigger its dependent state flushing. The subclass should
84 // call resetDirtyFlags to zero these out after it has consumed them.
85 //
bsalomon@google.comf3fcbbe2012-05-21 22:15:46 +000086 // TODO: Merge GrGpuGLShaders into GrGpuGL and remove the need for these
bsalomon@google.combcc84862012-05-22 15:44:03 +000087 // flags.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000088 struct {
89 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000090 int fTextureChangedMask;
91 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +000092 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000093
94 // clears the dirty flags
95 void resetDirtyFlags();
96
97 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000098 struct {
99 bool fScissorEnabled;
100 GrGLIRect fScissorRect;
101 GrGLIRect fViewportRect;
102 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000103
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000104 const GrGLCaps& glCaps() const { return fGLContextInfo.caps(); }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000105
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000107 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000108
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000109 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000110 const void* srcData,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000111 size_t rowBytes) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000112 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000113 bool dynamic) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000114 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000115 bool dynamic) SK_OVERRIDE;
116 virtual GrTexture* onCreatePlatformTexture(
117 const GrPlatformTextureDesc& desc) SK_OVERRIDE;
118 virtual GrRenderTarget* onCreatePlatformRenderTarget(
119 const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000120 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000121 int width,
122 int height) SK_OVERRIDE;
123 virtual bool attachStencilBufferToRenderTarget(
124 GrStencilBuffer* sb,
125 GrRenderTarget* rt) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000126
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000127 virtual void onClear(const GrIRect* rect, GrColor color) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000128
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000129 virtual void onForceRenderTargetFlush() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000130
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000131 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000132 int left, int top,
133 int width, int height,
134 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000135 void* buffer,
136 size_t rowBytes,
137 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000138
bsalomon@google.com6f379512011-11-16 20:36:03 +0000139 virtual void onWriteTexturePixels(GrTexture* texture,
140 int left, int top, int width, int height,
141 GrPixelConfig config, const void* buffer,
142 size_t rowBytes) SK_OVERRIDE;
143
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000144 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
145
146
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000147 virtual void onGpuDrawIndexed(GrPrimitiveType type,
148 uint32_t startVertex,
149 uint32_t startIndex,
150 uint32_t vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000151 uint32_t indexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000152 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
153 uint32_t vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000154 uint32_t numVertices) SK_OVERRIDE;
155 virtual void enableScissoring(const GrIRect& rect) SK_OVERRIDE;
156 virtual void disableScissor() SK_OVERRIDE;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000157
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000158 virtual void clearStencil() SK_OVERRIDE;
159 virtual void clearStencilClip(const GrIRect& rect,
160 bool insideClip) SK_OVERRIDE;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000161 virtual bool flushGraphicsState(GrPrimitiveType type) SK_OVERRIDE;
162 virtual void setupGeometry(int* startVertex,
163 int* startIndex,
164 int vertexCount,
165 int indexCount) SK_OVERRIDE;
166
bsalomon@google.com5782d712011-01-21 21:03:59 +0000167
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000169 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000170
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000171 // binds appropriate vertex and index buffers, also returns any extra
172 // extra verts or indices to offset by.
173 void setBuffers(bool indexed,
174 int* extraVertexOffset,
175 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000176
reed@google.comac10a2d2010-12-22 21:39:39 +0000177 // flushes state that is common to fixed and programmable GL
178 // dither
179 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000180 // texture binding
181 // sampler state (filtering, tiling)
182 // FBO binding
183 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000184 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000186 // Subclasses should call this to flush the blend state.
187 // The params should be the final coeffecients to apply
188 // (after any blending optimizations or dual source blending considerations
189 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000190 void flushBlend(GrPrimitiveType type,
191 GrBlendCoeff srcCoeff,
192 GrBlendCoeff dstCoeff);
193
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000194 bool hasExtension(const char* ext) const {
195 return fGLContextInfo.hasExtension(ext);
bsalomon@google.com96399942012-02-13 14:39:16 +0000196 }
197
198 const GrGLContextInfo& glContextInfo() const { return fGLContextInfo; }
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000199
bsalomon@google.com99621082011-11-15 16:47:16 +0000200 // adjusts texture matrix to account for orientation
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000201 static void AdjustTextureMatrix(const GrGLTexture* texture,
202 GrSamplerState::SampleMode mode,
203 GrMatrix* matrix);
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000204
205 // subclass may try to take advantage of identity tex matrices.
206 // This helper determines if matrix will be identity after all
207 // adjustments are applied.
208 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
209 const GrSamplerState& sampler);
210
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000211 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000212
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000213private:
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000214
215 // for readability of function impls
216 typedef GrGLProgram::ProgramDesc ProgramDesc;
217 typedef ProgramDesc::StageDesc StageDesc;
218 typedef GrGLProgram::CachedData CachedData;
219
220 class ProgramCache;
221
222 void createProgramCache();
223 void deleteProgramCache();
224
225 // sets the texture matrix uniform for currently bound program
226 void flushTextureMatrix(int stage);
227
228 // sets the texture domain uniform for currently bound program
229 void flushTextureDomain(int stage);
230
231 // sets the color specified by GrDrawState::setColor()
232 void flushColor(GrColor color);
233
234 // sets the color specified by GrDrawState::setCoverage()
235 void flushCoverage(GrColor color);
236
237 // sets the MVP matrix uniform for currently bound program
238 void flushViewMatrix();
239
240 // flushes the parameters to two point radial gradient
241 void flushRadial2(int stage);
242
243 // flushes the parameters for convolution
244 void flushConvolution(int stage);
245
246 // flushes the normalized texel size
247 void flushTexelSize(int stage);
248
249 // flushes the color matrix
250 void flushColorMatrix();
251
252 static void DeleteProgram(const GrGLInterface* gl,
253 CachedData* programData);
254
255 void buildProgram(GrPrimitiveType typeBlend,
256 BlendOptFlags blendOpts,
257 GrBlendCoeff dstCoeff,
258 GrCustomStage** customStages);
259
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000260 // Inits GrDrawTarget::Caps, sublcass may enable additional caps.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000261 void initCaps();
262
263 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000264
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000265 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000266 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000267
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 // notify callbacks to update state tracking when related
269 // objects are bound to GL or deleted outside of the class
270 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
271 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
272 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
273 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 void notifyTextureDelete(GrGLTexture* texture);
275 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000276
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000277 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000279 // bound is region that may be modified and therefore has to be resolved.
280 // NULL means whole target. Can be an empty rect.
281 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000283 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000284
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000285 bool configToGLFormats(GrPixelConfig config,
286 bool getSizedInternal,
287 GrGLenum* internalFormat,
288 GrGLenum* externalFormat,
289 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000290 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000291 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000292 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000293 int left, int top, int width, int height,
294 GrPixelConfig dataConfig,
295 const void* data,
296 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000297
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000298 bool createRenderTargetObjects(int width, int height,
299 GrGLuint texID,
300 GrGLRenderTarget::Desc* desc);
301
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000302 void fillInConfigRenderableTable();
303
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 friend class GrGLVertexBuffer;
305 friend class GrGLIndexBuffer;
306 friend class GrGLTexture;
307 friend class GrGLRenderTarget;
308
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000309 GrGLContextInfo fGLContextInfo;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000310
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000311 // GL program-related state
312 ProgramCache* fProgramCache;
313 CachedData* fProgramData;
314 GrGLuint fHWProgramID;
315 GrColor fHWConstAttribColor;
316 GrColor fHWConstAttribCoverage;
317 GrGLProgram fCurrentProgram;
318 // If we get rid of fixed function subclass this should move
319 // to the GLCaps struct in parent class
320 GrGLint fMaxVertexAttribs;
321
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000322 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000323
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000324 struct {
325 GrBlendCoeff fSrcCoeff;
326 GrBlendCoeff fDstCoeff;
327 GrColor fConstColor;
328 bool fConstColorValid;
329 TriState fEnabled;
330
331 void invalidate() {
332 fSrcCoeff = kInvalid_BlendCoeff;
333 fDstCoeff = kInvalid_BlendCoeff;
334 fConstColorValid = false;
335 fEnabled = kUnknown_TriState;
336 }
337 } fHWBlendState;
338
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000339 struct {
340 TriState fMSAAEnabled;
341 TriState fSmoothLineEnabled;
342 void invalidate() {
343 fMSAAEnabled = kUnknown_TriState;
344 fSmoothLineEnabled = kUnknown_TriState;
345 }
346 } fHWAAState;
347
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000348 // The high bit of the stencil buffer is used for clipping. This enum is
349 // used to track whether the clip bit of the stencil buffer is being used,
350 // manipulated, or neither.
351 enum StencilClipMode {
352 // Draw to the clip bit of the stencil buffer
353 kModifyClip_StencilClipMode,
354 // Clip against the existing representation of the clip in the high bit
355 // of the stencil buffer.
356 kUseClip_StencilClipMode,
357 // Neither writing to nor clipping against the clip bit.
358 kIgnoreClip_StencilClipMode,
359 // Unknown state of HW
360 kInvalid_StencilClipMode,
361 };
362 StencilClipMode fHWStencilClipMode;
363 GrStencilSettings fHWStencilSettings;
364
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000365 GrDrawState::DrawFace fHWDrawFace;
366 TriState fHWWriteToColor;
367 TriState fHWDitherEnabled;
368 GrRenderTarget* fHWBoundRenderTarget;
369 GrTexture* fHWBoundTextures[GrDrawState::kNumStages];
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000370
bsalomon@google.comfe676522011-06-17 18:12:21 +0000371 // we record what stencil format worked last time to hopefully exit early
372 // from our loop that tries stencil formats and calls check fb status.
373 int fLastSuccessfulStencilFmtIdx;
374
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000375 enum CanPreserveUnpremulRoundtrip {
376 kUnknown_CanPreserveUnpremulRoundtrip,
377 kNo_CanPreserveUnpremulRoundtrip,
378 kYes_CanPreserveUnpremulRoundtrip,
379 } fCanPreserveUnpremulRoundtrip;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000380
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000381 bool fPrintedCaps;
382
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 typedef GrGpu INHERITED;
384};
385
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000386#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000387