|
1 /* |
|
2 * 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. |
|
6 */ |
|
7 |
|
8 #ifndef GrGpuGL_DEFINED |
|
9 #define GrGpuGL_DEFINED |
|
10 |
|
11 #include "GrBinHashKey.h" |
|
12 #include "GrDrawState.h" |
|
13 #include "GrGLContext.h" |
|
14 #include "GrGLIRect.h" |
|
15 #include "GrGLIndexBuffer.h" |
|
16 #include "GrGLProgram.h" |
|
17 #include "GrGLStencilBuffer.h" |
|
18 #include "GrGLTexture.h" |
|
19 #include "GrGLVertexArray.h" |
|
20 #include "GrGLVertexBuffer.h" |
|
21 #include "GrGpu.h" |
|
22 #include "GrTHashTable.h" |
|
23 #include "SkTypes.h" |
|
24 |
|
25 #ifdef SK_DEVELOPER |
|
26 #define PROGRAM_CACHE_STATS |
|
27 #endif |
|
28 |
|
29 class GrGpuGL : public GrGpu { |
|
30 public: |
|
31 GrGpuGL(const GrGLContext& ctx, GrContext* context); |
|
32 virtual ~GrGpuGL(); |
|
33 |
|
34 const GrGLContext& glContext() const { return fGLContext; } |
|
35 |
|
36 const GrGLInterface* glInterface() const { return fGLContext.interface(); } |
|
37 const GrGLContextInfo& ctxInfo() const { return fGLContext; } |
|
38 GrGLStandard glStandard() const { return fGLContext.standard(); } |
|
39 GrGLVersion glVersion() const { return fGLContext.version(); } |
|
40 GrGLSLGeneration glslGeneration() const { return fGLContext.glslGeneration(); } |
|
41 const GrGLCaps& glCaps() const { return *fGLContext.caps(); } |
|
42 |
|
43 // Used by GrGLProgram and GrGLTexGenProgramEffects to configure OpenGL state. |
|
44 void bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture); |
|
45 void setProjectionMatrix(const SkMatrix& matrix, |
|
46 const SkISize& renderTargetSize, |
|
47 GrSurfaceOrigin renderTargetOrigin); |
|
48 enum TexGenComponents { |
|
49 kS_TexGenComponents = 1, |
|
50 kST_TexGenComponents = 2, |
|
51 kSTR_TexGenComponents = 3 |
|
52 }; |
|
53 void enableTexGen(int unitIdx, TexGenComponents, const GrGLfloat* coefficients); |
|
54 void enableTexGen(int unitIdx, TexGenComponents, const SkMatrix& matrix); |
|
55 void flushTexGenSettings(int numUsedTexCoordSets); |
|
56 bool shouldUseFixedFunctionTexturing() const { |
|
57 return this->glCaps().fixedFunctionSupport() && |
|
58 this->glCaps().pathRenderingSupport(); |
|
59 } |
|
60 |
|
61 bool programUnitTest(int maxStages); |
|
62 |
|
63 // GrGpu overrides |
|
64 virtual GrPixelConfig preferredReadPixelsConfig(GrPixelConfig readConfig, |
|
65 GrPixelConfig surfaceConfig) const SK_OVERRIDE; |
|
66 virtual GrPixelConfig preferredWritePixelsConfig(GrPixelConfig writeConfig, |
|
67 GrPixelConfig surfaceConfig) const SK_OVERRIDE; |
|
68 virtual bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE; |
|
69 virtual bool readPixelsWillPayForYFlip( |
|
70 GrRenderTarget* renderTarget, |
|
71 int left, int top, |
|
72 int width, int height, |
|
73 GrPixelConfig config, |
|
74 size_t rowBytes) const SK_OVERRIDE; |
|
75 virtual bool fullReadPixelsIsFasterThanPartial() const SK_OVERRIDE; |
|
76 |
|
77 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE; |
|
78 |
|
79 virtual void abandonResources() SK_OVERRIDE; |
|
80 |
|
81 // These functions should be used to bind GL objects. They track the GL state and skip redundant |
|
82 // bindings. Making the equivalent glBind calls directly will confuse the state tracking. |
|
83 void bindVertexArray(GrGLuint id) { |
|
84 fHWGeometryState.setVertexArrayID(this, id); |
|
85 } |
|
86 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) { |
|
87 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id); |
|
88 } |
|
89 void bindVertexBuffer(GrGLuint id) { |
|
90 fHWGeometryState.setVertexBufferID(this, id); |
|
91 } |
|
92 |
|
93 // These callbacks update state tracking when GL objects are deleted. They are called from |
|
94 // GrGLResource onRelease functions. |
|
95 void notifyVertexArrayDelete(GrGLuint id) { |
|
96 fHWGeometryState.notifyVertexArrayDelete(id); |
|
97 } |
|
98 void notifyVertexBufferDelete(GrGLuint id) { |
|
99 fHWGeometryState.notifyVertexBufferDelete(id); |
|
100 } |
|
101 void notifyIndexBufferDelete(GrGLuint id) { |
|
102 fHWGeometryState.notifyIndexBufferDelete(id); |
|
103 } |
|
104 void notifyTextureDelete(GrGLTexture* texture); |
|
105 void notifyRenderTargetDelete(GrRenderTarget* renderTarget); |
|
106 |
|
107 protected: |
|
108 virtual bool onCopySurface(GrSurface* dst, |
|
109 GrSurface* src, |
|
110 const SkIRect& srcRect, |
|
111 const SkIPoint& dstPoint) SK_OVERRIDE; |
|
112 |
|
113 virtual bool onCanCopySurface(GrSurface* dst, |
|
114 GrSurface* src, |
|
115 const SkIRect& srcRect, |
|
116 const SkIPoint& dstPoint) SK_OVERRIDE; |
|
117 |
|
118 private: |
|
119 // GrGpu overrides |
|
120 virtual void onResetContext(uint32_t resetBits) SK_OVERRIDE; |
|
121 |
|
122 virtual GrTexture* onCreateTexture(const GrTextureDesc& desc, |
|
123 const void* srcData, |
|
124 size_t rowBytes) SK_OVERRIDE; |
|
125 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE; |
|
126 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE; |
|
127 virtual GrPath* onCreatePath(const SkPath&, const SkStrokeRec&) SK_OVERRIDE; |
|
128 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE; |
|
129 virtual GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE; |
|
130 virtual bool createStencilBufferForRenderTarget(GrRenderTarget* rt, |
|
131 int width, |
|
132 int height) SK_OVERRIDE; |
|
133 virtual bool attachStencilBufferToRenderTarget( |
|
134 GrStencilBuffer* sb, |
|
135 GrRenderTarget* rt) SK_OVERRIDE; |
|
136 |
|
137 virtual void onClear(const SkIRect* rect, GrColor color, bool canIgnoreRect) SK_OVERRIDE; |
|
138 |
|
139 virtual void onForceRenderTargetFlush() SK_OVERRIDE; |
|
140 |
|
141 virtual bool onReadPixels(GrRenderTarget* target, |
|
142 int left, int top, |
|
143 int width, int height, |
|
144 GrPixelConfig, |
|
145 void* buffer, |
|
146 size_t rowBytes) SK_OVERRIDE; |
|
147 |
|
148 virtual bool onWriteTexturePixels(GrTexture* texture, |
|
149 int left, int top, int width, int height, |
|
150 GrPixelConfig config, const void* buffer, |
|
151 size_t rowBytes) SK_OVERRIDE; |
|
152 |
|
153 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE; |
|
154 |
|
155 virtual void onGpuDraw(const DrawInfo&) SK_OVERRIDE; |
|
156 |
|
157 virtual void onGpuStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE; |
|
158 virtual void onGpuDrawPath(const GrPath*, SkPath::FillType) SK_OVERRIDE; |
|
159 |
|
160 virtual void clearStencil() SK_OVERRIDE; |
|
161 virtual void clearStencilClip(const SkIRect& rect, |
|
162 bool insideClip) SK_OVERRIDE; |
|
163 virtual bool flushGraphicsState(DrawType, const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE; |
|
164 |
|
165 // GrDrawTarget ovverides |
|
166 virtual void onInstantGpuTraceEvent(const char* marker) SK_OVERRIDE; |
|
167 virtual void onPushGpuTraceEvent(const char* marker) SK_OVERRIDE; |
|
168 virtual void onPopGpuTraceEvent() SK_OVERRIDE; |
|
169 |
|
170 |
|
171 // binds texture unit in GL |
|
172 void setTextureUnit(int unitIdx); |
|
173 |
|
174 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset |
|
175 // an into the index buffer. It does not account for drawInfo.startIndex() but rather the start |
|
176 // index is relative to the returned offset. |
|
177 void setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes); |
|
178 |
|
179 // Subclasses should call this to flush the blend state. |
|
180 // The params should be the final coefficients to apply |
|
181 // (after any blending optimizations or dual source blending considerations |
|
182 // have been accounted for). |
|
183 void flushBlend(bool isLines, GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff); |
|
184 |
|
185 bool hasExtension(const char* ext) const { return fGLContext.hasExtension(ext); } |
|
186 |
|
187 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff); |
|
188 |
|
189 class ProgramCache : public ::SkNoncopyable { |
|
190 public: |
|
191 ProgramCache(GrGpuGL* gpu); |
|
192 ~ProgramCache(); |
|
193 |
|
194 void abandon(); |
|
195 GrGLProgram* getProgram(const GrGLProgramDesc& desc, |
|
196 const GrEffectStage* colorStages[], |
|
197 const GrEffectStage* coverageStages[]); |
|
198 |
|
199 private: |
|
200 enum { |
|
201 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new |
|
202 // shader before evicting from the cache. |
|
203 kMaxEntries = 32, |
|
204 kHashBits = 6, |
|
205 }; |
|
206 |
|
207 struct Entry; |
|
208 |
|
209 struct ProgDescLess; |
|
210 |
|
211 // binary search for entry matching desc. returns index into fEntries that matches desc or ~ |
|
212 // of the index of where it should be inserted. |
|
213 int search(const GrGLProgramDesc& desc) const; |
|
214 |
|
215 // sorted array of all the entries |
|
216 Entry* fEntries[kMaxEntries]; |
|
217 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary |
|
218 // searching fEntries. |
|
219 Entry* fHashTable[1 << kHashBits]; |
|
220 |
|
221 int fCount; |
|
222 unsigned int fCurrLRUStamp; |
|
223 GrGpuGL* fGpu; |
|
224 #ifdef PROGRAM_CACHE_STATS |
|
225 int fTotalRequests; |
|
226 int fCacheMisses; |
|
227 int fHashMisses; // cache hit but hash table missed |
|
228 #endif |
|
229 }; |
|
230 |
|
231 // flushes dithering, color-mask, and face culling stat |
|
232 void flushMiscFixedFunctionState(); |
|
233 |
|
234 // flushes the scissor. see the note on flushBoundTextureAndParams about |
|
235 // flushing the scissor after that function is called. |
|
236 void flushScissor(); |
|
237 |
|
238 void initFSAASupport(); |
|
239 |
|
240 // determines valid stencil formats |
|
241 void initStencilFormats(); |
|
242 |
|
243 // sets a texture unit to use for texture operations other than binding a texture to a program. |
|
244 // ensures that such operations don't negatively interact with tracking bound textures. |
|
245 void setScratchTextureUnit(); |
|
246 |
|
247 // bound is region that may be modified and therefore has to be resolved. |
|
248 // NULL means whole target. Can be an empty rect. |
|
249 void flushRenderTarget(const SkIRect* bound); |
|
250 void flushStencil(DrawType); |
|
251 void flushAAState(DrawType); |
|
252 void flushPathStencilSettings(SkPath::FillType fill); |
|
253 |
|
254 bool configToGLFormats(GrPixelConfig config, |
|
255 bool getSizedInternal, |
|
256 GrGLenum* internalFormat, |
|
257 GrGLenum* externalFormat, |
|
258 GrGLenum* externalType); |
|
259 // helper for onCreateTexture and writeTexturePixels |
|
260 bool uploadTexData(const GrGLTexture::Desc& desc, |
|
261 bool isNewTexture, |
|
262 int left, int top, int width, int height, |
|
263 GrPixelConfig dataConfig, |
|
264 const void* data, |
|
265 size_t rowBytes); |
|
266 |
|
267 bool createRenderTargetObjects(int width, int height, |
|
268 GrGLuint texID, |
|
269 GrGLRenderTarget::Desc* desc); |
|
270 |
|
271 GrGLContext fGLContext; |
|
272 |
|
273 // GL program-related state |
|
274 ProgramCache* fProgramCache; |
|
275 SkAutoTUnref<GrGLProgram> fCurrentProgram; |
|
276 |
|
277 /////////////////////////////////////////////////////////////////////////// |
|
278 ///@name Caching of GL State |
|
279 ///@{ |
|
280 int fHWActiveTextureUnitIdx; |
|
281 GrGLuint fHWProgramID; |
|
282 |
|
283 GrGLProgram::SharedGLState fSharedGLProgramState; |
|
284 |
|
285 enum TriState { |
|
286 kNo_TriState, |
|
287 kYes_TriState, |
|
288 kUnknown_TriState |
|
289 }; |
|
290 |
|
291 // last scissor / viewport scissor state seen by the GL. |
|
292 struct { |
|
293 TriState fEnabled; |
|
294 GrGLIRect fRect; |
|
295 void invalidate() { |
|
296 fEnabled = kUnknown_TriState; |
|
297 fRect.invalidate(); |
|
298 } |
|
299 } fHWScissorSettings; |
|
300 |
|
301 GrGLIRect fHWViewport; |
|
302 |
|
303 /** |
|
304 * Tracks bound vertex and index buffers and vertex attrib array state. |
|
305 */ |
|
306 class HWGeometryState { |
|
307 public: |
|
308 HWGeometryState() { fVBOVertexArray = NULL; this->invalidate(); } |
|
309 |
|
310 ~HWGeometryState() { SkSafeUnref(fVBOVertexArray); } |
|
311 |
|
312 void invalidate() { |
|
313 fBoundVertexArrayIDIsValid = false; |
|
314 fBoundVertexBufferIDIsValid = false; |
|
315 fDefaultVertexArrayBoundIndexBufferID = false; |
|
316 fDefaultVertexArrayBoundIndexBufferIDIsValid = false; |
|
317 fDefaultVertexArrayAttribState.invalidate(); |
|
318 if (NULL != fVBOVertexArray) { |
|
319 fVBOVertexArray->invalidateCachedState(); |
|
320 } |
|
321 } |
|
322 |
|
323 void notifyVertexArrayDelete(GrGLuint id) { |
|
324 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) { |
|
325 // Does implicit bind to 0 |
|
326 fBoundVertexArrayID = 0; |
|
327 } |
|
328 } |
|
329 |
|
330 void setVertexArrayID(GrGpuGL* gpu, GrGLuint arrayID) { |
|
331 if (!gpu->glCaps().vertexArrayObjectSupport()) { |
|
332 SkASSERT(0 == arrayID); |
|
333 return; |
|
334 } |
|
335 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) { |
|
336 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID)); |
|
337 fBoundVertexArrayIDIsValid = true; |
|
338 fBoundVertexArrayID = arrayID; |
|
339 } |
|
340 } |
|
341 |
|
342 void notifyVertexBufferDelete(GrGLuint id) { |
|
343 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) { |
|
344 fBoundVertexBufferID = 0; |
|
345 } |
|
346 if (NULL != fVBOVertexArray) { |
|
347 fVBOVertexArray->notifyVertexBufferDelete(id); |
|
348 } |
|
349 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id); |
|
350 } |
|
351 |
|
352 void notifyIndexBufferDelete(GrGLuint id) { |
|
353 if (fDefaultVertexArrayBoundIndexBufferIDIsValid && |
|
354 id == fDefaultVertexArrayBoundIndexBufferID) { |
|
355 fDefaultVertexArrayBoundIndexBufferID = 0; |
|
356 } |
|
357 if (NULL != fVBOVertexArray) { |
|
358 fVBOVertexArray->notifyIndexBufferDelete(id); |
|
359 } |
|
360 } |
|
361 |
|
362 void setVertexBufferID(GrGpuGL* gpu, GrGLuint id) { |
|
363 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) { |
|
364 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id)); |
|
365 fBoundVertexBufferIDIsValid = true; |
|
366 fBoundVertexBufferID = id; |
|
367 } |
|
368 } |
|
369 |
|
370 /** |
|
371 * Binds the default vertex array and binds the index buffer. This is used when binding |
|
372 * an index buffer in order to update it. |
|
373 */ |
|
374 void setIndexBufferIDOnDefaultVertexArray(GrGpuGL* gpu, GrGLuint id) { |
|
375 this->setVertexArrayID(gpu, 0); |
|
376 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid || |
|
377 id != fDefaultVertexArrayBoundIndexBufferID) { |
|
378 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id)); |
|
379 fDefaultVertexArrayBoundIndexBufferIDIsValid = true; |
|
380 fDefaultVertexArrayBoundIndexBufferID = id; |
|
381 } |
|
382 } |
|
383 |
|
384 /** |
|
385 * Binds the vertex array object that should be used to render from the vertex buffer. |
|
386 * The vertex array is bound and its attrib array state object is returned. The vertex |
|
387 * buffer is bound. The index buffer (if non-NULL) is bound to the vertex array. The |
|
388 * returned GrGLAttribArrayState should be used to set vertex attribute arrays. |
|
389 */ |
|
390 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGpuGL* gpu, |
|
391 const GrGLVertexBuffer* vbuffer, |
|
392 const GrGLIndexBuffer* ibuffer); |
|
393 |
|
394 private: |
|
395 GrGLuint fBoundVertexArrayID; |
|
396 GrGLuint fBoundVertexBufferID; |
|
397 bool fBoundVertexArrayIDIsValid; |
|
398 bool fBoundVertexBufferIDIsValid; |
|
399 |
|
400 GrGLuint fDefaultVertexArrayBoundIndexBufferID; |
|
401 bool fDefaultVertexArrayBoundIndexBufferIDIsValid; |
|
402 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0 |
|
403 // is bound. However, this class is internal to GrGpuGL and this object never leaks out of |
|
404 // GrGpuGL. |
|
405 GrGLAttribArrayState fDefaultVertexArrayAttribState; |
|
406 |
|
407 // This is used when we're using a core profile and the vertices are in a VBO. |
|
408 GrGLVertexArray* fVBOVertexArray; |
|
409 } fHWGeometryState; |
|
410 |
|
411 struct { |
|
412 GrBlendCoeff fSrcCoeff; |
|
413 GrBlendCoeff fDstCoeff; |
|
414 GrColor fConstColor; |
|
415 bool fConstColorValid; |
|
416 TriState fEnabled; |
|
417 |
|
418 void invalidate() { |
|
419 fSrcCoeff = kInvalid_GrBlendCoeff; |
|
420 fDstCoeff = kInvalid_GrBlendCoeff; |
|
421 fConstColorValid = false; |
|
422 fEnabled = kUnknown_TriState; |
|
423 } |
|
424 } fHWBlendState; |
|
425 |
|
426 struct { |
|
427 TriState fMSAAEnabled; |
|
428 TriState fSmoothLineEnabled; |
|
429 void invalidate() { |
|
430 fMSAAEnabled = kUnknown_TriState; |
|
431 fSmoothLineEnabled = kUnknown_TriState; |
|
432 } |
|
433 } fHWAAState; |
|
434 |
|
435 |
|
436 GrGLProgram::MatrixState fHWProjectionMatrixState; |
|
437 |
|
438 GrStencilSettings fHWStencilSettings; |
|
439 TriState fHWStencilTestEnabled; |
|
440 GrStencilSettings fHWPathStencilSettings; |
|
441 |
|
442 GrDrawState::DrawFace fHWDrawFace; |
|
443 TriState fHWWriteToColor; |
|
444 TriState fHWDitherEnabled; |
|
445 GrRenderTarget* fHWBoundRenderTarget; |
|
446 SkTArray<GrTexture*, true> fHWBoundTextures; |
|
447 |
|
448 struct TexGenData { |
|
449 GrGLenum fMode; |
|
450 GrGLint fNumComponents; |
|
451 GrGLfloat fCoefficients[3 * 3]; |
|
452 }; |
|
453 int fHWActiveTexGenSets; |
|
454 SkTArray<TexGenData, true> fHWTexGenSettings; |
|
455 ///@} |
|
456 |
|
457 // we record what stencil format worked last time to hopefully exit early |
|
458 // from our loop that tries stencil formats and calls check fb status. |
|
459 int fLastSuccessfulStencilFmtIdx; |
|
460 |
|
461 typedef GrGpu INHERITED; |
|
462 }; |
|
463 |
|
464 #endif |