blob: 59e32cf5ae77db59a3905e9a6e0048df975369d4 [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
14#include "GrGpu.h"
bsalomon@google.coma16d6502011-08-02 14:07:52 +000015#include "GrGLIRect.h"
bsalomon@google.coma16d6502011-08-02 14:07:52 +000016#include "GrGLTexture.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000017
bsalomon@google.coma16d6502011-08-02 14:07:52 +000018#include "GrGLVertexBuffer.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000019#include "GrGLIndexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000021#include "SkString.h"
22
reed@google.comac10a2d2010-12-22 21:39:39 +000023class GrGpuGL : public GrGpu {
24public:
reed@google.comac10a2d2010-12-22 21:39:39 +000025 virtual ~GrGpuGL();
26
reed@google.comac10a2d2010-12-22 21:39:39 +000027protected:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000028 GrGpuGL();
29
reed@google.comac10a2d2010-12-22 21:39:39 +000030 struct {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000031 size_t fVertexOffset;
reed@google.comac10a2d2010-12-22 21:39:39 +000032 GrVertexLayout fVertexLayout;
33 const GrVertexBuffer* fVertexBuffer;
34 const GrIndexBuffer* fIndexBuffer;
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +000035 bool fArrayPtrsDirty;
reed@google.comac10a2d2010-12-22 21:39:39 +000036 } fHWGeometryState;
37
bsalomon@google.comf954d8d2011-04-06 17:50:02 +000038 struct AAState {
39 bool fMSAAEnabled;
40 bool fSmoothLineEnabled;
41 } fHWAAState;
42
reed@google.com8195f672011-01-12 18:14:28 +000043 DrState fHWDrawState;
44 bool fHWStencilClip;
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +000046 // read these once at begining and then never again
47 SkString fExtensionString;
48 float fGLVersion;
49
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000050 // As flush of GL state proceeds it updates fHDrawState
51 // to reflect the new state. Later parts of the state flush
52 // may perform cascaded changes but cannot refer to fHWDrawState.
53 // These code paths can refer to the dirty flags. Subclass should
54 // call resetDirtyFlags after its flush is complete
55 struct {
56 bool fRenderTargetChanged : 1;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000057 int fTextureChangedMask;
58 } fDirtyFlags;
59 GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages);
60
61 // clears the dirty flags
62 void resetDirtyFlags();
63
64 // last scissor / viewport scissor state seen by the GL.
bsalomon@google.com8895a7a2011-02-18 16:09:55 +000065 struct {
66 bool fScissorEnabled;
67 GrGLIRect fScissorRect;
68 GrGLIRect fViewportRect;
69 } fHWBounds;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000070
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071 // GrGpu overrides
bsalomon@google.com4043ae22011-08-02 14:19:11 +000072 // overrides from GrGpu
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000073 virtual void resetContext();
74
bsalomon@google.comfea37b52011-04-25 15:51:06 +000075 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc,
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000076 const void* srcData,
77 size_t rowBytes);
78 virtual GrVertexBuffer* onCreateVertexBuffer(uint32_t size,
79 bool dynamic);
80 virtual GrIndexBuffer* onCreateIndexBuffer(uint32_t size,
81 bool dynamic);
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000082 virtual GrResource* onCreatePlatformSurface(const GrPlatformSurfaceDesc& desc);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000083 virtual GrRenderTarget* onCreateRenderTargetFrom3DApiState();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000084
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000085 virtual void onClear(const GrIRect* rect, GrColor color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000086
bsalomon@google.combcdbbe62011-04-12 15:40:00 +000087 virtual void onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000088
bsalomon@google.com5877ffd2011-04-11 17:58:48 +000089 virtual bool onReadPixels(GrRenderTarget* target,
90 int left, int top, int width, int height,
91 GrPixelConfig, void* buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000092
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000093 virtual void onGpuDrawIndexed(GrPrimitiveType type,
94 uint32_t startVertex,
95 uint32_t startIndex,
96 uint32_t vertexCount,
97 uint32_t indexCount);
98 virtual void onGpuDrawNonIndexed(GrPrimitiveType type,
99 uint32_t vertexCount,
100 uint32_t numVertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 virtual void flushScissor(const GrIRect* rect);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000102 void clearStencil(uint32_t value, uint32_t mask);
103 virtual void clearStencilClip(const GrIRect& rect);
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000104 virtual int getMaxEdges() const;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000105
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000106 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000107 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000109 // binds appropriate vertex and index buffers, also returns any extra
110 // extra verts or indices to offset by.
111 void setBuffers(bool indexed,
112 int* extraVertexOffset,
113 int* extraIndexOffset);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000114
reed@google.comac10a2d2010-12-22 21:39:39 +0000115 // flushes state that is common to fixed and programmable GL
116 // dither
117 // line smoothing
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 // texture binding
119 // sampler state (filtering, tiling)
120 // FBO binding
121 // line width
bsalomon@google.comffca4002011-02-22 20:34:01 +0000122 bool flushGLStateCommon(GrPrimitiveType type);
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000124 // subclass should call this to flush the blend state
125 void flushBlend(GrPrimitiveType type,
126 GrBlendCoeff srcCoeff,
127 GrBlendCoeff dstCoeff);
128
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000129 bool hasExtension(const char* ext) {
130 return has_gl_extension_from_string(ext, fExtensionString.c_str());
131 }
132
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000133 // adjusts texture matrix to account for orientation, size, and npotness
134 static void AdjustTextureMatrix(const GrGLTexture* texture,
135 GrSamplerState::SampleMode mode,
136 GrMatrix* matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000138 // subclass may try to take advantage of identity tex matrices.
139 // This helper determines if matrix will be identity after all
140 // adjustments are applied.
141 static bool TextureMatrixIsIdentity(const GrGLTexture* texture,
142 const GrSamplerState& sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000144 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000145
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000146private:
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000147
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000148 // determines valid stencil formats
149 void setupStencilFormats();
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151 // notify callbacks to update state tracking when related
152 // objects are bound to GL or deleted outside of the class
153 void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
154 void notifyVertexBufferDelete(const GrGLVertexBuffer* buffer);
155 void notifyIndexBufferBind(const GrGLIndexBuffer* buffer);
156 void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000157 void notifyTextureDelete(GrGLTexture* texture);
158 void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000159
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000160 void setSpareTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000161
bsalomon@google.com0650e812011-04-08 18:07:53 +0000162 bool useSmoothLines();
163
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000164 // bound is region that may be modified and therefore has to be resolved.
165 // NULL means whole target. Can be an empty rect.
166 void flushRenderTarget(const GrIRect* bound);
reed@google.comac10a2d2010-12-22 21:39:39 +0000167 void flushStencil();
bsalomon@google.comf954d8d2011-04-06 17:50:02 +0000168 void flushAAState(GrPrimitiveType type);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000169
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000170 void resolveRenderTarget(GrGLRenderTarget* texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000172 bool canBeTexture(GrPixelConfig config,
twiz@google.com0f31ca72011-03-18 17:38:11 +0000173 GrGLenum* internalFormat,
174 GrGLenum* format,
175 GrGLenum* type);
bsalomon@google.com4043ae22011-08-02 14:19:11 +0000176 // helper for onCreateTexture
bsalomon@google.com71f341a2011-08-01 13:36:00 +0000177 void allocateAndUploadTexData(const GrGLTexture::Desc& desc,
178 GrGLenum internalFormat,
179 const void* data,
180 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000181
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000182 bool fboInternalFormat(GrPixelConfig config, GrGLenum* format);
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
184 friend class GrGLVertexBuffer;
185 friend class GrGLIndexBuffer;
186 friend class GrGLTexture;
187 friend class GrGLRenderTarget;
188
bsalomon@google.com4043ae22011-08-02 14:19:11 +0000189 static const GrGLuint gUNKNOWN_BITCOUNT = ~0;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000190
bsalomon@google.com4043ae22011-08-02 14:19:11 +0000191 struct StencilFormat {
192 GrGLenum fEnum;
193 GrGLuint fBits;
194 bool fPacked;
195 };
196
197 GrTArray<StencilFormat, true> fStencilFormats;
198
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 bool fHWBlendDisabled;
200
twiz@google.com0f31ca72011-03-18 17:38:11 +0000201 GrGLuint fAASamples[4];
reed@google.comac10a2d2010-12-22 21:39:39 +0000202 enum {
bsalomon@google.coma9ecdad2011-03-23 13:50:34 +0000203 kNone_MSFBO = 0, //<! no support for MSAA FBOs
204 kDesktopARB_MSFBO,//<! GL3.0-style MSAA FBO (GL_ARB_framebuffer_object)
205 kDesktopEXT_MSFBO,//<! earlier GL_EXT_framebuffer* extensions
206 kAppleES_MSFBO, //<! GL_APPLE_framebuffer_multisample ES extension
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 } fMSFBOType;
208
209 // Do we have stencil wrap ops.
210 bool fHasStencilWrap;
211
senorblanco@chromium.orgef3913b2011-05-19 17:11:07 +0000212 // The maximum number of fragment uniform vectors (GLES has min. 16).
213 int fMaxFragmentUniformVectors;
214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 // ES requires an extension to support RGBA8 in RenderBufferStorage
216 bool fRGBA8Renderbuffer;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000218 int fActiveTextureUnitIdx;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000219
bsalomon@google.comfe676522011-06-17 18:12:21 +0000220 // we record what stencil format worked last time to hopefully exit early
221 // from our loop that tries stencil formats and calls check fb status.
222 int fLastSuccessfulStencilFmtIdx;
223
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 typedef GrGpu INHERITED;
225};
226
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227#endif
bsalomon@google.comfe676522011-06-17 18:12:21 +0000228