blob: d140de34770e25fad7d6634e052dd7c3fe85e4a1 [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"
18#include "GrGLStencilBuffer.h"
19#include "GrGLTexture.h"
20#include "GrGLVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
22class GrGpuGL : public GrGpu {
23public:
reed@google.comac10a2d2010-12-22 21:39:39 +000024 virtual ~GrGpuGL();
25
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000026 const GrGLInterface* glInterface() const {
27 return fGLContextInfo.interface();
28 }
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000029 GrGLBinding glBinding() const { return fGLContextInfo.binding(); }
30 GrGLVersion glVersion() const { return fGLContextInfo.version(); }
31 GrGLSLGeneration glslGeneration() const {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000032 return fGLContextInfo.glslGeneration();
33 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034
bsalomon@google.com56d11e02011-11-30 19:59:08 +000035 // GrGpu overrides
bsalomon@google.com0a97be22011-11-08 19:20:57 +000036 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000037 const SK_OVERRIDE;
bsalomon@google.coma85449d2011-11-19 02:36:05 +000038 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000039 const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000040 virtual bool readPixelsWillPayForYFlip(
41 GrRenderTarget* renderTarget,
42 int left, int top,
43 int width, int height,
44 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000045 size_t rowBytes) const SK_OVERRIDE;
46 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000047
bsalomon@google.coma91e9232012-02-23 15:39:54 +000048 virtual bool canPreserveReadWriteUnpremulPixels() SK_OVERRIDE;
49
bsalomon@google.com88939ae2011-12-14 15:58:11 +000050protected:
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000051 GrGpuGL(const GrGLContextInfo& ctxInfo);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000052
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +000053 enum TriState {
54 kNo_TriState,
55 kYes_TriState,
56 kUnknown_TriState
57 };
58
reed@google.comac10a2d2010-12-22 21:39:39 +000059 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000060 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000061 GrVertexLayout fVertexLayout;
62 const GrVertexBuffer* fVertexBuffer;
63 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000064 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000065 } fHWGeometryState;
66
bsalomon@google.coma91e9232012-02-23 15:39:54 +000067 enum UnpremulConversion {
68 kUpOnWrite_DownOnRead_UnpremulConversion,
69 kDownOnWrite_UpOnRead_UnpremulConversion
70 } fUnpremulConversion;
71
bsalomon@google.combcc84862012-05-22 15:44:03 +000072 // The current render target and textures are bound by GrGpuGL when it
73 // flushes state to GL. After the bindings occur the variables that track
74 // the current GL state are updated to reflect the new bindings. However,
75 // the GrGpuGL subclass may have subsequent GL state manipulation it must
76 // perform whenever RT or textures change. So the GrGpuGL will set these
77 // dirty flags when it changes the RT or texture bindings. The subclass can
78 // use them to trigger its dependent state flushing. The subclass should
79 // call resetDirtyFlags to zero these out after it has consumed them.
80 //
bsalomon@google.comf3fcbbe2012-05-21 22:15:46 +000081 // TODO: Merge GrGpuGLShaders into GrGpuGL and remove the need for these
bsalomon@google.combcc84862012-05-22 15:44:03 +000082 // flags.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000083 struct {
84 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000085 int fTextureChangedMask;
86 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +000087 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000088
89 // clears the dirty flags
90 void resetDirtyFlags();
91
92 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000093 struct {
94 bool fScissorEnabled;
95 GrGLIRect fScissorRect;
96 GrGLIRect fViewportRect;
97 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000098
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000099 const GrGLCaps& glCaps() const { return fGLContextInfo.caps(); }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000100
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +0000102 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000103
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000104 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000105 const void* srcData,
106 size_t rowBytes);
107 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
108 bool dynamic);
109 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
110 bool dynamic);
bsalomon@google.come269f212011-11-07 13:29:52 +0000111 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
112 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000113 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
114 int width, int height);
115 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
116 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000117
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000118 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000119
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000120 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000121
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000122 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000123 int left, int top,
124 int width, int height,
125 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000126 void* buffer,
127 size_t rowBytes,
128 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000129
bsalomon@google.com6f379512011-11-16 20:36:03 +0000130 virtual void onWriteTexturePixels(GrTexture* texture,
131 int left, int top, int width, int height,
132 GrPixelConfig config, const void* buffer,
133 size_t rowBytes) SK_OVERRIDE;
134
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000135 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
136
137
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000138 virtual void onGpuDrawIndexed(GrPrimitiveType type,
139 uint32_t startVertex,
140 uint32_t startIndex,
141 uint32_t vertexCount,
142 uint32_t indexCount);
143 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
144 uint32_t vertexCount,
145 uint32_t numVertices);
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000146 virtual void enableScissoring(const GrIRect& rect);
147 virtual void disableScissor();
148
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000149 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000150 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000151
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000152 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000153 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 // binds appropriate vertex and index buffers, also returns any extra
156 // extra verts or indices to offset by.
157 void setBuffers(bool indexed,
158 int* extraVertexOffset,
159 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000160
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 // flushes state that is common to fixed and programmable GL
162 // dither
163 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 // texture binding
165 // sampler state (filtering, tiling)
166 // FBO binding
167 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000168 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000169
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000170 // Subclasses should call this to flush the blend state.
171 // The params should be the final coeffecients to apply
172 // (after any blending optimizations or dual source blending considerations
173 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000174 void flushBlend(GrPrimitiveType type,
175 GrBlendCoeff srcCoeff,
176 GrBlendCoeff dstCoeff);
177
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000178 bool hasExtension(const char* ext) const {
179 return fGLContextInfo.hasExtension(ext);
bsalomon@google.com96399942012-02-13 14:39:16 +0000180 }
181
182 const GrGLContextInfo& glContextInfo() const { return fGLContextInfo; }
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000183
bsalomon@google.com99621082011-11-15 16:47:16 +0000184 // adjusts texture matrix to account for orientation
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000185 static void AdjustTextureMatrix(const GrGLTexture* texture,
186 GrSamplerState::SampleMode mode,
187 GrMatrix* matrix);
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000188
189 // subclass may try to take advantage of identity tex matrices.
190 // This helper determines if matrix will be identity after all
191 // adjustments are applied.
192 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
193 const GrSamplerState& sampler);
194
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000195 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000196
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000197private:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000198 // Inits GrDrawTarget::Caps, sublcass may enable additional caps.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000199 void initCaps();
200
201 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000202
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000203 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000204 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000205
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 // notify callbacks to update state tracking when related
207 // objects are bound to GL or deleted outside of the class
208 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
209 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
210 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
211 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 void notifyTextureDelete(GrGLTexture* texture);
213 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000215 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000216
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000217 // bound is region that may be modified and therefore has to be resolved.
218 // NULL means whole target. Can be an empty rect.
219 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000221 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000222
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000223 bool configToGLFormats(GrPixelConfig config,
224 bool getSizedInternal,
225 GrGLenum* internalFormat,
226 GrGLenum* externalFormat,
227 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000228 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000229 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000230 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000231 int left, int top, int width, int height,
232 GrPixelConfig dataConfig,
233 const void* data,
234 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000235
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000236 bool createRenderTargetObjects(int width, int height,
237 GrGLuint texID,
238 GrGLRenderTarget::Desc* desc);
239
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000240 void fillInConfigRenderableTable();
241
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 friend class GrGLVertexBuffer;
243 friend class GrGLIndexBuffer;
244 friend class GrGLTexture;
245 friend class GrGLRenderTarget;
246
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000247 GrGLContextInfo fGLContextInfo;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000248
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000249 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000250
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000251 struct {
252 GrBlendCoeff fSrcCoeff;
253 GrBlendCoeff fDstCoeff;
254 GrColor fConstColor;
255 bool fConstColorValid;
256 TriState fEnabled;
257
258 void invalidate() {
259 fSrcCoeff = kInvalid_BlendCoeff;
260 fDstCoeff = kInvalid_BlendCoeff;
261 fConstColorValid = false;
262 fEnabled = kUnknown_TriState;
263 }
264 } fHWBlendState;
265
bsalomon@google.com4d5f3fe2012-05-21 17:11:44 +0000266 struct {
267 TriState fMSAAEnabled;
268 TriState fSmoothLineEnabled;
269 void invalidate() {
270 fMSAAEnabled = kUnknown_TriState;
271 fSmoothLineEnabled = kUnknown_TriState;
272 }
273 } fHWAAState;
274
bsalomon@google.com457b8a32012-05-21 21:19:58 +0000275 // The high bit of the stencil buffer is used for clipping. This enum is
276 // used to track whether the clip bit of the stencil buffer is being used,
277 // manipulated, or neither.
278 enum StencilClipMode {
279 // Draw to the clip bit of the stencil buffer
280 kModifyClip_StencilClipMode,
281 // Clip against the existing representation of the clip in the high bit
282 // of the stencil buffer.
283 kUseClip_StencilClipMode,
284 // Neither writing to nor clipping against the clip bit.
285 kIgnoreClip_StencilClipMode,
286 // Unknown state of HW
287 kInvalid_StencilClipMode,
288 };
289 StencilClipMode fHWStencilClipMode;
290 GrStencilSettings fHWStencilSettings;
291
bsalomon@google.comc811ea32012-05-21 15:33:09 +0000292 GrDrawState::DrawFace fHWDrawFace;
293 TriState fHWWriteToColor;
294 TriState fHWDitherEnabled;
295 GrRenderTarget* fHWBoundRenderTarget;
296 GrTexture* fHWBoundTextures[GrDrawState::kNumStages];
bsalomon@google.com978c8c62012-05-21 14:45:49 +0000297
bsalomon@google.comfe676522011-06-17 18:12:21 +0000298 // we record what stencil format worked last time to hopefully exit early
299 // from our loop that tries stencil formats and calls check fb status.
300 int fLastSuccessfulStencilFmtIdx;
301
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000302 enum CanPreserveUnpremulRoundtrip {
303 kUnknown_CanPreserveUnpremulRoundtrip,
304 kNo_CanPreserveUnpremulRoundtrip,
305 kYes_CanPreserveUnpremulRoundtrip,
306 } fCanPreserveUnpremulRoundtrip;
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000307
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000308 bool fPrintedCaps;
309
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 typedef GrGpu INHERITED;
311};
312
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000313#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000314