blob: eac8d4f5024657a5db5ad303d044be9fd27bc21c [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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrGpuGL_DEFINED
12#define GrGpuGL_DEFINED
13
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000014#include "../GrDrawState.h"
15#include "../GrGpu.h"
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000016#include "GrGLContextInfo.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000017#include "GrGLIndexBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#include "GrGLIRect.h"
19#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:
reed@google.comac10a2d2010-12-22 21:39:39 +000025 virtual ~GrGpuGL();
26
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000027 const GrGLInterface* glInterface() const {
28 return fGLContextInfo.interface();
29 }
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000030 GrGLBinding glBinding() const { return fGLContextInfo.binding(); }
31 GrGLVersion glVersion() const { return fGLContextInfo.version(); }
32 GrGLSLGeneration glslGeneration() const {
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000033 return fGLContextInfo.glslGeneration();
34 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000035
bsalomon@google.com56d11e02011-11-30 19:59:08 +000036 // GrGpu overrides
bsalomon@google.com0a97be22011-11-08 19:20:57 +000037 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000038 const SK_OVERRIDE;
bsalomon@google.coma85449d2011-11-19 02:36:05 +000039 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig config)
bsalomon@google.com56d11e02011-11-30 19:59:08 +000040 const SK_OVERRIDE;
bsalomon@google.comc4364992011-11-07 15:54:49 +000041 virtual bool readPixelsWillPayForYFlip(
42 GrRenderTarget* renderTarget,
43 int left, int top,
44 int width, int height,
45 GrPixelConfig config,
bsalomon@google.com56d11e02011-11-30 19:59:08 +000046 size_t rowBytes) const SK_OVERRIDE;
47 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000048
bsalomon@google.com88939ae2011-12-14 15:58:11 +000049protected:
bsalomon@google.com89ec61e2012-02-10 20:05:18 +000050 GrGpuGL(const GrGLContextInfo& ctxInfo);
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000051
reed@google.comac10a2d2010-12-22 21:39:39 +000052 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000053 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000054 GrVertexLayout fVertexLayout;
55 const GrVertexBuffer* fVertexBuffer;
56 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000057 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000058 } fHWGeometryState;
59
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000060 struct AAState {
61 bool fMSAAEnabled;
62 bool fSmoothLineEnabled;
63 } fHWAAState;
64
tomhudson@google.com93813632011-10-27 20:21:16 +000065 GrDrawState fHWDrawState;
66 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000067
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000068 // As flush of GL state proceeds it updates fHDrawState
69 // to reflect the new state. Later parts of the state flush
70 // may perform cascaded changes but cannot refer to fHWDrawState.
71 // These code paths can refer to the dirty flags. Subclass should
72 // call resetDirtyFlags after its flush is complete
73 struct {
74 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000075 int fTextureChangedMask;
76 } fDirtyFlags;
tomhudson@google.com93813632011-10-27 20:21:16 +000077 GR_STATIC_ASSERT(8 * sizeof(int) >= GrDrawState::kNumStages);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000078
79 // clears the dirty flags
80 void resetDirtyFlags();
81
82 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000083 struct {
84 bool fScissorEnabled;
85 GrGLIRect fScissorRect;
86 GrGLIRect fViewportRect;
87 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000088
bsalomon@google.comf7fa8062012-02-14 14:09:57 +000089 const GrGLCaps& glCaps() const { return fGLContextInfo.caps(); }
bsalomon@google.com88939ae2011-12-14 15:58:11 +000090
bsalomon@google.com1c13c962011-02-14 16:51:21 +000091 // GrGpu overrides
bsalomon@google.com1bf1c212011-11-05 12:18:58 +000092 virtual void onResetContext() SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000093
bsalomon@google.comfea37b52011-04-25 15:51:06 +000094 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000095 const void* srcData,
96 size_t rowBytes);
97 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
98 bool dynamic);
99 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
100 bool dynamic);
bsalomon@google.come269f212011-11-07 13:29:52 +0000101 virtual GrTexture* onCreatePlatformTexture(const GrPlatformTextureDesc& desc) SK_OVERRIDE;
102 virtual GrRenderTarget* onCreatePlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) SK_OVERRIDE;
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000103 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt,
104 int width, int height);
105 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer* sb,
106 GrRenderTarget* rt);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000107
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000108 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000109
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000110 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000111
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000112 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000113 int left, int top,
114 int width, int height,
115 GrPixelConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000116 void* buffer,
117 size_t rowBytes,
118 bool invertY) SK_OVERRIDE;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000119
bsalomon@google.com6f379512011-11-16 20:36:03 +0000120 virtual void onWriteTexturePixels(GrTexture* texture,
121 int left, int top, int width, int height,
122 GrPixelConfig config, const void* buffer,
123 size_t rowBytes) SK_OVERRIDE;
124
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000125 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE;
126
127
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000128 virtual void onGpuDrawIndexed(GrPrimitiveType type,
129 uint32_t startVertex,
130 uint32_t startIndex,
131 uint32_t vertexCount,
132 uint32_t indexCount);
133 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
134 uint32_t vertexCount,
135 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000137 virtual void clearStencil();
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000138 virtual void clearStencilClip(const GrIRect& rect, bool insideClip);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000139 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000140
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000142 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000144 // binds appropriate vertex and index buffers, also returns any extra
145 // extra verts or indices to offset by.
146 void setBuffers(bool indexed,
147 int* extraVertexOffset,
148 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000149
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 // flushes state that is common to fixed and programmable GL
151 // dither
152 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000153 // texture binding
154 // sampler state (filtering, tiling)
155 // FBO binding
156 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000157 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000159 // Subclasses should call this to flush the blend state.
160 // The params should be the final coeffecients to apply
161 // (after any blending optimizations or dual source blending considerations
162 // have been accounted for).
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000163 void flushBlend(GrPrimitiveType type,
164 GrBlendCoeff srcCoeff,
165 GrBlendCoeff dstCoeff);
166
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000167 bool hasExtension(const char* ext) const {
168 return fGLContextInfo.hasExtension(ext);
bsalomon@google.com96399942012-02-13 14:39:16 +0000169 }
170
171 const GrGLContextInfo& glContextInfo() const { return fGLContextInfo; }
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000172
bsalomon@google.com99621082011-11-15 16:47:16 +0000173 // adjusts texture matrix to account for orientation
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000174 static void AdjustTextureMatrix(const GrGLTexture* texture,
175 GrSamplerState::SampleMode mode,
176 GrMatrix* matrix);
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000177
178 // subclass may try to take advantage of identity tex matrices.
179 // This helper determines if matrix will be identity after all
180 // adjustments are applied.
181 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
182 const GrSamplerState& sampler);
183
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000184 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000185
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000186private:
bsalomon@google.comf7fa8062012-02-14 14:09:57 +0000187 // Inits GrDrawTarget::Caps, sublcass may enable additional caps.
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000188 void initCaps();
189
190 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000191
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000192 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000193 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 // notify callbacks to update state tracking when related
196 // objects are bound to GL or deleted outside of the class
197 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
198 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
199 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
200 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 void notifyTextureDelete(GrGLTexture* texture);
202 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000203
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000204 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000206 // bound is region that may be modified and therefore has to be resolved.
207 // NULL means whole target. Can be an empty rect.
208 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000210 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000211
bsalomon@google.com280e99f2012-01-05 16:17:38 +0000212 bool configToGLFormats(GrPixelConfig config,
213 bool getSizedInternal,
214 GrGLenum* internalFormat,
215 GrGLenum* externalFormat,
216 GrGLenum* externalType);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000217 // helper for onCreateTexture and writeTexturePixels
bsalomon@google.com136f55b2011-11-28 18:34:44 +0000218 bool uploadTexData(const GrGLTexture::Desc& desc,
bsalomon@google.com1e0e6072011-11-28 18:49:37 +0000219 bool isNewTexture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000220 int left, int top, int width, int height,
221 GrPixelConfig dataConfig,
222 const void* data,
223 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000224
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000225 bool createRenderTargetObjects(int width, int height,
226 GrGLuint texID,
227 GrGLRenderTarget::Desc* desc);
228
reed@google.comac10a2d2010-12-22 21:39:39 +0000229 friend class GrGLVertexBuffer;
230 friend class GrGLIndexBuffer;
231 friend class GrGLTexture;
232 friend class GrGLRenderTarget;
233
bsalomon@google.com89ec61e2012-02-10 20:05:18 +0000234 GrGLContextInfo fGLContextInfo;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000235
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000236 // we want to clear stencil buffers when they are created. We want to clear
237 // the entire buffer even if it is larger than the color attachment. We
238 // attach it to this fbo with no color attachment to do the initial clear.
239 GrGLuint fStencilClearFBO;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000240
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 bool fHWBlendDisabled;
242
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000243 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000244
bsalomon@google.comfe676522011-06-17 18:12:21 +0000245 // we record what stencil format worked last time to hopefully exit early
246 // from our loop that tries stencil formats and calls check fb status.
247 int fLastSuccessfulStencilFmtIdx;
248
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000249
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000250 bool fPrintedCaps;
251
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 typedef GrGpu INHERITED;
253};
254
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000255#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000256