/src/skia/src/gpu/gl/GrGLGpu.h
Line | Count | Source (jump to first uncovered line) |
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 GrGLGpu_DEFINED |
9 | | #define GrGLGpu_DEFINED |
10 | | |
11 | | #include "include/core/SkTypes.h" |
12 | | #include "include/private/SkTArray.h" |
13 | | #include "src/core/SkLRUCache.h" |
14 | | #include "src/gpu/GrFinishCallbacks.h" |
15 | | #include "src/gpu/GrGpu.h" |
16 | | #include "src/gpu/GrNativeRect.h" |
17 | | #include "src/gpu/GrProgramDesc.h" |
18 | | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
19 | | #include "src/gpu/GrWindowRectsState.h" |
20 | | #include "src/gpu/GrXferProcessor.h" |
21 | | #include "src/gpu/gl/GrGLAttachment.h" |
22 | | #include "src/gpu/gl/GrGLContext.h" |
23 | | #include "src/gpu/gl/GrGLProgram.h" |
24 | | #include "src/gpu/gl/GrGLRenderTarget.h" |
25 | | #include "src/gpu/gl/GrGLTexture.h" |
26 | | #include "src/gpu/gl/GrGLVertexArray.h" |
27 | | |
28 | | class GrGLBuffer; |
29 | | class GrGLOpsRenderPass; |
30 | | class GrPipeline; |
31 | | class GrSwizzle; |
32 | | |
33 | | class GrGLGpu final : public GrGpu { |
34 | | public: |
35 | | static sk_sp<GrGpu> Make(sk_sp<const GrGLInterface>, const GrContextOptions&, GrDirectContext*); |
36 | | ~GrGLGpu() override; |
37 | | |
38 | | void disconnect(DisconnectType) override; |
39 | | |
40 | | GrThreadSafePipelineBuilder* pipelineBuilder() override; |
41 | | sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() override; |
42 | | |
43 | 0 | const GrGLContext& glContext() const { return *fGLContext; } |
44 | | |
45 | 0 | const GrGLInterface* glInterface() const { return fGLContext->glInterface(); } |
46 | 0 | const GrGLContextInfo& ctxInfo() const { return *fGLContext; } |
47 | 0 | GrGLStandard glStandard() const { return fGLContext->standard(); } |
48 | 0 | GrGLVersion glVersion() const { return fGLContext->version(); } |
49 | 0 | GrGLSLGeneration glslGeneration() const { return fGLContext->glslGeneration(); } |
50 | 0 | const GrGLCaps& glCaps() const { return *fGLContext->caps(); } |
51 | | |
52 | | // Used by GrGLProgram to configure OpenGL state. |
53 | | void bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle&, GrGLTexture*); |
54 | | |
55 | | // These functions should be used to bind GL objects. They track the GL state and skip redundant |
56 | | // bindings. Making the equivalent glBind calls directly will confuse the state tracking. |
57 | 0 | void bindVertexArray(GrGLuint id) { |
58 | 0 | fHWVertexArrayState.setVertexArrayID(this, id); |
59 | 0 | } |
60 | | |
61 | | // These callbacks update state tracking when GL objects are deleted. They are called from |
62 | | // GrGLResource onRelease functions. |
63 | 0 | void notifyVertexArrayDelete(GrGLuint id) { |
64 | 0 | fHWVertexArrayState.notifyVertexArrayDelete(id); |
65 | 0 | } |
66 | | |
67 | | // Binds a buffer to the GL target corresponding to 'type', updates internal state tracking, and |
68 | | // returns the GL target the buffer was bound to. |
69 | | // When 'type' is kIndex_GrBufferType, this function will also implicitly bind the default VAO. |
70 | | // If the caller wishes to bind an index buffer to a specific VAO, it can call glBind directly. |
71 | | GrGLenum bindBuffer(GrGpuBufferType type, const GrBuffer*); |
72 | | |
73 | | // Flushes state from GrProgramInfo to GL. Returns false if the state couldn't be set. |
74 | | bool flushGLState(GrRenderTarget*, bool useMultisampleFBO, const GrProgramInfo&); |
75 | | void flushScissorRect(const SkIRect& scissor, int rtHeight, GrSurfaceOrigin); |
76 | | |
77 | | // The flushRenderTarget methods will all set the initial viewport to the full extent of the |
78 | | // backing render target. |
79 | | void flushViewport(const SkIRect& viewport, int rtHeight, GrSurfaceOrigin); |
80 | | |
81 | | // Returns the last program bound by flushGLState(), or nullptr if a different program has since |
82 | | // been put into use via some other method (e.g., resetContext, copySurfaceAsDraw). |
83 | | // The returned GrGLProgram can be used for binding textures and vertex attributes. |
84 | 0 | GrGLProgram* currentProgram() { |
85 | 0 | this->handleDirtyContext(); |
86 | 0 | return fHWProgram.get(); |
87 | 0 | } |
88 | | |
89 | | // Binds the vertex array that should be used for internal draws, enables 'numAttribs' vertex |
90 | | // arrays, and flushes the desired primitive restart settings. If an index buffer is provided, |
91 | | // it will be bound to the vertex array. Otherwise the index buffer binding will be left |
92 | | // unchanged. |
93 | | // |
94 | | // NOTE: This binds the default VAO (ID=zero) unless we are on a core profile, in which case we |
95 | | // use a placeholder array instead. |
96 | | GrGLAttribArrayState* bindInternalVertexArray(const GrBuffer* indexBuffer, int numAttribs, |
97 | 0 | GrPrimitiveRestart primitiveRestart) { |
98 | 0 | auto* attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer); |
99 | 0 | attribState->enableVertexArrays(this, numAttribs, primitiveRestart); |
100 | 0 | return attribState; |
101 | 0 | } |
102 | | |
103 | | // Applies any necessary workarounds and returns the GL primitive type to use in draw calls. |
104 | | GrGLenum prepareToDraw(GrPrimitiveType primitiveType); |
105 | | |
106 | | enum class ResolveDirection : bool { |
107 | | kSingleToMSAA, // glCaps.canResolveSingleToMSAA() must be true. |
108 | | kMSAAToSingle |
109 | | }; |
110 | | |
111 | | // Resolves the render target's single sample FBO into the MSAA, or vice versa. |
112 | | // If glCaps.framebufferResolvesMustBeFullSize() is true, resolveRect must be equal the render |
113 | | // target's bounds rect. |
114 | | // If blitting single to MSAA, glCaps.canResolveSingleToMSAA() must be true. |
115 | | void resolveRenderFBOs(GrGLRenderTarget*, const SkIRect& resolveRect, ResolveDirection, |
116 | | bool invalidateReadBufferAfterBlit = false); |
117 | | |
118 | | // For loading a dynamic MSAA framebuffer when glCaps.canResolveSingleToMSAA() is false. |
119 | | // NOTE: If glCaps.framebufferResolvesMustBeFullSize() is also true, the drawBounds should be |
120 | | // equal to the proxy bounds. This is because the render pass will have to do a full size |
121 | | // resolve back into the single sample FBO when rendering is complete. |
122 | 0 | void drawSingleIntoMSAAFBO(GrGLRenderTarget* rt, const SkIRect& drawBounds) { |
123 | 0 | this->copySurfaceAsDraw(rt, true/*drawToMultisampleFBO*/, rt, drawBounds, |
124 | 0 | drawBounds.topLeft()); |
125 | 0 | } |
126 | | |
127 | | // The GrGLOpsRenderPass does not buffer up draws before submitting them to the gpu. |
128 | | // Thus this is the implementation of the clear call for the corresponding passthrough function |
129 | | // on GrGLOpsRenderPass. |
130 | | void clear(const GrScissorState&, std::array<float, 4> color, GrRenderTarget*, |
131 | | bool useMultisampleFBO, GrSurfaceOrigin); |
132 | | |
133 | | // The GrGLOpsRenderPass does not buffer up draws before submitting them to the gpu. |
134 | | // Thus this is the implementation of the clearStencil call for the corresponding passthrough |
135 | | // function on GrGLOpsrenderPass. |
136 | | void clearStencilClip(const GrScissorState&, bool insideStencilMask, |
137 | | GrRenderTarget*, bool useMultisampleFBO, GrSurfaceOrigin); |
138 | | |
139 | | void beginCommandBuffer(GrGLRenderTarget*, bool useMultisampleFBO, |
140 | | const SkIRect& bounds, GrSurfaceOrigin, |
141 | | const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore, |
142 | | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore); |
143 | | |
144 | | void endCommandBuffer(GrGLRenderTarget*, bool useMultisampleFBO, |
145 | | const GrOpsRenderPass::LoadAndStoreInfo& colorLoadStore, |
146 | | const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilLoadStore); |
147 | | |
148 | 0 | void invalidateBoundRenderTarget() { |
149 | 0 | fHWBoundRenderTargetUniqueID.makeInvalid(); |
150 | 0 | } |
151 | | |
152 | | sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& colorFormat, |
153 | | SkISize dimensions, int numStencilSamples) override; |
154 | | |
155 | | sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions, |
156 | | const GrBackendFormat& format, |
157 | | int numSamples, |
158 | | GrProtected isProtected) override; |
159 | | |
160 | | void deleteBackendTexture(const GrBackendTexture&) override; |
161 | | |
162 | | bool compile(const GrProgramDesc&, const GrProgramInfo&) override; |
163 | | |
164 | 0 | bool precompileShader(const SkData& key, const SkData& data) override { |
165 | 0 | return fProgramCache->precompileShader(this->getContext(), key, data); |
166 | 0 | } |
167 | | |
168 | | #if GR_TEST_UTILS |
169 | | bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override; |
170 | | |
171 | | GrBackendRenderTarget createTestingOnlyBackendRenderTarget(SkISize dimensions, |
172 | | GrColorType, |
173 | | int sampleCnt, |
174 | | GrProtected) override; |
175 | | void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override; |
176 | | |
177 | 0 | const GrGLContext* glContextForTesting() const override { return &this->glContext(); } |
178 | | |
179 | 0 | void resetShaderCacheForTesting() const override { fProgramCache->reset(); } |
180 | | #endif |
181 | | |
182 | | void submit(GrOpsRenderPass* renderPass) override; |
183 | | |
184 | | GrFence SK_WARN_UNUSED_RESULT insertFence() override; |
185 | | bool waitFence(GrFence) override; |
186 | | void deleteFence(GrFence) const override; |
187 | | |
188 | | std::unique_ptr<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override; |
189 | | std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&, |
190 | | GrSemaphoreWrapType, |
191 | | GrWrapOwnership) override; |
192 | | void insertSemaphore(GrSemaphore* semaphore) override; |
193 | | void waitSemaphore(GrSemaphore* semaphore) override; |
194 | | |
195 | | void checkFinishProcs() override; |
196 | | void finishOutstandingGpuWork() override; |
197 | | |
198 | | // Calls glGetError() until no errors are reported. Also looks for OOMs. |
199 | | void clearErrorsAndCheckForOOM(); |
200 | | // Calls glGetError() once and returns the result. Also looks for an OOM. |
201 | | GrGLenum getErrorAndCheckForOOM(); |
202 | | |
203 | | std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override; |
204 | | |
205 | | void deleteSync(GrGLsync) const; |
206 | | |
207 | | void bindFramebuffer(GrGLenum fboTarget, GrGLuint fboid); |
208 | | void deleteFramebuffer(GrGLuint fboid); |
209 | | |
210 | | void insertManualFramebufferBarrier() override; |
211 | | |
212 | | void flushProgram(sk_sp<GrGLProgram>); |
213 | | |
214 | | // Version for programs that aren't GrGLProgram. |
215 | | void flushProgram(GrGLuint); |
216 | | |
217 | | private: |
218 | | GrGLGpu(std::unique_ptr<GrGLContext>, GrDirectContext*); |
219 | | |
220 | | // GrGpu overrides |
221 | | GrBackendTexture onCreateBackendTexture(SkISize dimensions, |
222 | | const GrBackendFormat&, |
223 | | GrRenderable, |
224 | | GrMipmapped, |
225 | | GrProtected) override; |
226 | | |
227 | | GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions, |
228 | | const GrBackendFormat&, |
229 | | GrMipmapped, |
230 | | GrProtected) override; |
231 | | |
232 | | bool onClearBackendTexture(const GrBackendTexture&, |
233 | | sk_sp<GrRefCntedCallback> finishedCallback, |
234 | | std::array<float, 4> color) override; |
235 | | |
236 | | bool onUpdateCompressedBackendTexture(const GrBackendTexture&, |
237 | | sk_sp<GrRefCntedCallback> finishedCallback, |
238 | | const void* data, |
239 | | size_t length) override; |
240 | | |
241 | | void onResetContext(uint32_t resetBits) override; |
242 | | |
243 | | void onResetTextureBindings() override; |
244 | | |
245 | | void xferBarrier(GrRenderTarget*, GrXferBarrierType) override; |
246 | | |
247 | | sk_sp<GrTexture> onCreateTexture(SkISize dimensions, |
248 | | const GrBackendFormat&, |
249 | | GrRenderable, |
250 | | int renderTargetSampleCnt, |
251 | | SkBudgeted, |
252 | | GrProtected, |
253 | | int mipLevelCount, |
254 | | uint32_t levelClearMask) override; |
255 | | sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions, |
256 | | const GrBackendFormat&, |
257 | | SkBudgeted, |
258 | | GrMipmapped, |
259 | | GrProtected, |
260 | | const void* data, size_t dataSize) override; |
261 | | |
262 | | sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern, |
263 | | const void* data) override; |
264 | | |
265 | | sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, |
266 | | GrWrapOwnership, |
267 | | GrWrapCacheable, |
268 | | GrIOType) override; |
269 | | sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&, |
270 | | GrWrapOwnership, |
271 | | GrWrapCacheable) override; |
272 | | sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, |
273 | | int sampleCnt, |
274 | | GrWrapOwnership, |
275 | | GrWrapCacheable) override; |
276 | | sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; |
277 | | |
278 | | // Given a GL format return the index into the stencil format array on GrGLCaps to a |
279 | | // compatible stencil format, or negative if there is no compatible stencil format. |
280 | | int getCompatibleStencilIndex(GrGLFormat format); |
281 | | |
282 | 0 | GrBackendFormat getPreferredStencilFormat(const GrBackendFormat& format) override { |
283 | 0 | int idx = this->getCompatibleStencilIndex(format.asGLFormat()); |
284 | 0 | if (idx < 0) { |
285 | 0 | return {}; |
286 | 0 | } |
287 | 0 | return GrBackendFormat::MakeGL(GrGLFormatToEnum(this->glCaps().stencilFormats()[idx]), |
288 | 0 | GR_GL_TEXTURE_NONE); |
289 | 0 | } |
290 | | |
291 | | void onFBOChanged(); |
292 | | |
293 | | // Returns whether the texture is successfully created. On success, a non-zero texture ID is |
294 | | // returned. On failure, zero is returned. |
295 | | // The texture is populated with |texels|, if it is non-null. |
296 | | // The texture parameters are cached in |initialTexParams|. |
297 | | GrGLuint createTexture(SkISize dimensions, |
298 | | GrGLFormat, |
299 | | GrGLenum target, |
300 | | GrRenderable, |
301 | | GrGLTextureParameters::SamplerOverriddenState*, |
302 | | int mipLevelCount); |
303 | | |
304 | | GrGLuint createCompressedTexture2D(SkISize dimensions, |
305 | | SkImage::CompressionType compression, |
306 | | GrGLFormat, |
307 | | GrMipmapped, |
308 | | GrGLTextureParameters::SamplerOverriddenState*); |
309 | | |
310 | | bool onReadPixels(GrSurface*, |
311 | | SkIRect, |
312 | | GrColorType surfaceColorType, |
313 | | GrColorType dstColorType, |
314 | | void*, |
315 | | size_t rowBytes) override; |
316 | | |
317 | | bool onWritePixels(GrSurface*, |
318 | | SkIRect, |
319 | | GrColorType surfaceColorType, |
320 | | GrColorType srcColorType, |
321 | | const GrMipLevel[], |
322 | | int mipLevelCount, |
323 | | bool prepForTexSampling) override; |
324 | | |
325 | | bool onTransferPixelsTo(GrTexture*, |
326 | | SkIRect, |
327 | | GrColorType textureColorType, |
328 | | GrColorType bufferColorType, |
329 | | sk_sp<GrGpuBuffer>, |
330 | | size_t offset, |
331 | | size_t rowBytes) override; |
332 | | |
333 | | bool onTransferPixelsFrom(GrSurface*, |
334 | | SkIRect, |
335 | | GrColorType surfaceColorType, |
336 | | GrColorType bufferColorType, |
337 | | sk_sp<GrGpuBuffer>, |
338 | | size_t offset) override; |
339 | | |
340 | | bool readOrTransferPixelsFrom(GrSurface*, |
341 | | SkIRect rect, |
342 | | GrColorType surfaceColorType, |
343 | | GrColorType dstColorType, |
344 | | void* offsetOrPtr, |
345 | | int rowWidthInPixels); |
346 | | |
347 | | // Unbinds xfer buffers from GL for operations that don't need them. |
348 | | // Before calling any variation of TexImage, TexSubImage, etc..., call this with |
349 | | // GrGpuBufferType::kXferCpuToGpu to ensure that the PIXEL_UNPACK_BUFFER is unbound. |
350 | | // Before calling ReadPixels and reading back into cpu memory call this with |
351 | | // GrGpuBufferType::kXferGpuToCpu to ensure that the PIXEL_PACK_BUFFER is unbound. |
352 | | void unbindXferBuffer(GrGpuBufferType type); |
353 | | |
354 | | void onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) override; |
355 | | |
356 | | bool onRegenerateMipMapLevels(GrTexture*) override; |
357 | | |
358 | | bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
359 | | const SkIPoint& dstPoint) override; |
360 | | |
361 | | // binds texture unit in GL |
362 | | void setTextureUnit(int unitIdx); |
363 | | |
364 | | void flushBlendAndColorWrite(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&); |
365 | | |
366 | | void addFinishedProc(GrGpuFinishedProc finishedProc, |
367 | | GrGpuFinishedContext finishedContext) override; |
368 | | |
369 | | GrOpsRenderPass* onGetOpsRenderPass(GrRenderTarget*, |
370 | | bool useMultisampleFBO, |
371 | | GrAttachment*, |
372 | | GrSurfaceOrigin, |
373 | | const SkIRect&, |
374 | | const GrOpsRenderPass::LoadAndStoreInfo&, |
375 | | const GrOpsRenderPass::StencilLoadAndStoreInfo&, |
376 | | const SkTArray<GrSurfaceProxy*, true>& sampledProxies, |
377 | | GrXferBarrierFlags renderPassXferBarriers) override; |
378 | | |
379 | | bool onSubmitToGpu(bool syncCpu) override; |
380 | | |
381 | | bool waitSync(GrGLsync, uint64_t timeout, bool flush); |
382 | | |
383 | | bool copySurfaceAsDraw(GrSurface* dst, bool drawToMultisampleFBO, GrSurface* src, |
384 | | const SkIRect& srcRect, const SkIPoint& dstPoint); |
385 | | void copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
386 | | const SkIPoint& dstPoint); |
387 | | bool copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect, |
388 | | const SkIPoint& dstPoint); |
389 | | |
390 | | class ProgramCache : public GrThreadSafePipelineBuilder { |
391 | | public: |
392 | | ProgramCache(int runtimeProgramCacheSize); |
393 | | ~ProgramCache() override; |
394 | | |
395 | | void abandon(); |
396 | | void reset(); |
397 | | sk_sp<GrGLProgram> findOrCreateProgram(GrDirectContext*, |
398 | | const GrProgramInfo&); |
399 | | sk_sp<GrGLProgram> findOrCreateProgram(GrDirectContext*, |
400 | | const GrProgramDesc&, |
401 | | const GrProgramInfo&, |
402 | | Stats::ProgramCacheResult*); |
403 | | bool precompileShader(GrDirectContext*, const SkData& key, const SkData& data); |
404 | | |
405 | | private: |
406 | | struct Entry; |
407 | | |
408 | | sk_sp<GrGLProgram> findOrCreateProgramImpl(GrDirectContext*, |
409 | | const GrProgramDesc&, |
410 | | const GrProgramInfo&, |
411 | | Stats::ProgramCacheResult*); |
412 | | |
413 | | struct DescHash { |
414 | 0 | uint32_t operator()(const GrProgramDesc& desc) const { |
415 | 0 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
416 | 0 | } |
417 | | }; |
418 | | |
419 | | SkLRUCache<GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap; |
420 | | }; |
421 | | |
422 | | void flushPatchVertexCount(uint8_t count); |
423 | | |
424 | | void flushColorWrite(bool writeColor); |
425 | | void flushClearColor(std::array<float, 4>); |
426 | | |
427 | | // flushes the scissor. see the note on flushBoundTextureAndParams about |
428 | | // flushing the scissor after that function is called. |
429 | 0 | void flushScissor(const GrScissorState& scissorState, int rtHeight, GrSurfaceOrigin rtOrigin) { |
430 | 0 | this->flushScissorTest(GrScissorTest(scissorState.enabled())); |
431 | 0 | if (scissorState.enabled()) { |
432 | 0 | this->flushScissorRect(scissorState.rect(), rtHeight, rtOrigin); |
433 | 0 | } |
434 | 0 | } |
435 | | void flushScissorTest(GrScissorTest); |
436 | | |
437 | | void flushWindowRectangles(const GrWindowRectsState&, const GrGLRenderTarget*, GrSurfaceOrigin); |
438 | | void disableWindowRectangles(); |
439 | | |
440 | 0 | int numTextureUnits() const { return this->caps()->shaderCaps()->maxFragmentSamplers(); } |
441 | | |
442 | | // Binds a texture to a target on the "scratch" texture unit to use for texture operations |
443 | | // other than usual draw flow (i.e. a GrGLProgram derived from a GrPipeline used to draw). It |
444 | | // ensures that such operations don't negatively interact with draws. The active texture unit |
445 | | // and the binding for 'target' will change. |
446 | | void bindTextureToScratchUnit(GrGLenum target, GrGLint textureID); |
447 | | |
448 | | // The passed bounds contains the render target's color values that will subsequently be |
449 | | // written. |
450 | | void flushRenderTarget(GrGLRenderTarget*, bool useMultisampleFBO, GrSurfaceOrigin, |
451 | | const SkIRect& bounds); |
452 | | // This version has an implicit bounds of the entire render target. |
453 | | void flushRenderTarget(GrGLRenderTarget*, bool useMultisampleFBO); |
454 | | // This version can be used when the render target's colors will not be written. |
455 | | void flushRenderTargetNoColorWrites(GrGLRenderTarget*, bool useMultisampleFBO); |
456 | | |
457 | | void flushStencil(const GrStencilSettings&, GrSurfaceOrigin); |
458 | | void disableStencil(); |
459 | | |
460 | | void flushConservativeRasterState(bool enable); |
461 | | |
462 | | void flushWireframeState(bool enable); |
463 | | |
464 | | void flushFramebufferSRGB(bool enable); |
465 | | |
466 | | // Uploads src data of a color type to the currently bound texture on the active texture unit. |
467 | | // The caller specifies color type that the texture is being used with, which may be different |
468 | | // than the src color type. This fails if the combination of texture format, texture color type, |
469 | | // and src data color type are not valid. No conversion is performed on the data before passing |
470 | | // it to GL. 'dstRect' must be the texture bounds if mipLevelCount is greater than 1. |
471 | | bool uploadColorTypeTexData(GrGLFormat textureFormat, |
472 | | GrColorType textureColorType, |
473 | | SkISize texDims, |
474 | | GrGLenum target, |
475 | | SkIRect dstRect, |
476 | | GrColorType srcColorType, |
477 | | const GrMipLevel texels[], |
478 | | int mipLevelCount); |
479 | | |
480 | | // Uploads a constant color to a texture using the "default" format and color type. Overwrites |
481 | | // entire levels. Bit n in 'levelMask' indicates whether level n should be written. This |
482 | | // function doesn't know if MIP levels have been allocated, thus levelMask should not have bits |
483 | | // beyond the low bit set if the texture is not MIP mapped. |
484 | | bool uploadColorToTex(GrGLFormat textureFormat, |
485 | | SkISize texDims, |
486 | | GrGLenum target, |
487 | | std::array<float, 4> color, |
488 | | uint32_t levelMask); |
489 | | |
490 | | // Pushes data to the currently bound texture to the currently active unit. 'dstRect' must be |
491 | | // the texture bounds if mipLevelCount is greater than 1. |
492 | | void uploadTexData(SkISize dimensions, |
493 | | GrGLenum target, |
494 | | SkIRect dstRect, |
495 | | GrGLenum externalFormat, |
496 | | GrGLenum externalType, |
497 | | size_t bpp, |
498 | | const GrMipLevel texels[], |
499 | | int mipLevelCount); |
500 | | |
501 | | // Helper for onCreateCompressedTexture. Compressed textures are read-only so we only use this |
502 | | // to populate a new texture. Returns false if we failed to create and upload the texture. |
503 | | bool uploadCompressedTexData(SkImage::CompressionType compressionType, |
504 | | GrGLFormat, |
505 | | SkISize dimensions, |
506 | | GrMipmapped, |
507 | | GrGLenum target, |
508 | | const void* data, size_t dataSize); |
509 | | |
510 | | // Calls one of various versions of renderBufferStorageMultisample. |
511 | | bool renderbufferStorageMSAA(const GrGLContext& ctx, int sampleCount, GrGLenum format, |
512 | | int width, int height); |
513 | | |
514 | | bool createRenderTargetObjects(const GrGLTexture::Desc&, |
515 | | int sampleCount, |
516 | | GrGLRenderTarget::IDs*); |
517 | | enum TempFBOTarget { |
518 | | kSrc_TempFBOTarget, |
519 | | kDst_TempFBOTarget |
520 | | }; |
521 | | |
522 | | // Binds a surface as a FBO for copying, reading, or clearing. If the surface already owns an |
523 | | // FBO ID then that ID is bound. If not the surface is temporarily bound to a FBO and that FBO |
524 | | // is bound. This must be paired with a call to unbindSurfaceFBOForPixelOps(). |
525 | | void bindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget, |
526 | | TempFBOTarget tempFBOTarget); |
527 | | |
528 | | // Must be called if bindSurfaceFBOForPixelOps was used to bind a surface for copying. |
529 | | void unbindSurfaceFBOForPixelOps(GrSurface* surface, int mipLevel, GrGLenum fboTarget); |
530 | | |
531 | | #ifdef SK_ENABLE_DUMP_GPU |
532 | | void onDumpJSON(SkJSONWriter*) const override; |
533 | | #endif |
534 | | |
535 | | bool createCopyProgram(GrTexture* srcTexture); |
536 | | bool createMipmapProgram(int progIdx); |
537 | | |
538 | | std::unique_ptr<GrGLContext> fGLContext; |
539 | | |
540 | | // GL program-related state |
541 | | sk_sp<ProgramCache> fProgramCache; |
542 | | |
543 | | /////////////////////////////////////////////////////////////////////////// |
544 | | ///@name Caching of GL State |
545 | | ///@{ |
546 | | int fHWActiveTextureUnitIdx; |
547 | | |
548 | | GrGLuint fHWProgramID; |
549 | | sk_sp<GrGLProgram> fHWProgram; |
550 | | |
551 | | enum TriState { |
552 | | kNo_TriState, |
553 | | kYes_TriState, |
554 | | kUnknown_TriState |
555 | | }; |
556 | | |
557 | | GrGLuint fTempSrcFBOID; |
558 | | GrGLuint fTempDstFBOID; |
559 | | |
560 | | GrGLuint fStencilClearFBOID; |
561 | | |
562 | | // last scissor / viewport scissor state seen by the GL. |
563 | | struct { |
564 | | TriState fEnabled; |
565 | | GrNativeRect fRect; |
566 | 0 | void invalidate() { |
567 | 0 | fEnabled = kUnknown_TriState; |
568 | 0 | fRect.invalidate(); |
569 | 0 | } |
570 | | } fHWScissorSettings; |
571 | | |
572 | | class { |
573 | | public: |
574 | 0 | bool valid() const { return kInvalidSurfaceOrigin != fRTOrigin; } |
575 | 0 | void invalidate() { fRTOrigin = kInvalidSurfaceOrigin; } |
576 | 0 | bool knownDisabled() const { return this->valid() && !fWindowState.enabled(); } |
577 | 0 | void setDisabled() { |
578 | 0 | fRTOrigin = kTopLeft_GrSurfaceOrigin; |
579 | 0 | fWindowState.setDisabled(); |
580 | 0 | } |
581 | | |
582 | | void set(GrSurfaceOrigin rtOrigin, int width, int height, |
583 | 0 | const GrWindowRectsState& windowState) { |
584 | 0 | fRTOrigin = rtOrigin; |
585 | 0 | fWidth = width; |
586 | 0 | fHeight = height; |
587 | 0 | fWindowState = windowState; |
588 | 0 | } |
589 | | |
590 | | bool knownEqualTo(GrSurfaceOrigin rtOrigin, int width, int height, |
591 | 0 | const GrWindowRectsState& windowState) const { |
592 | 0 | if (!this->valid()) { |
593 | 0 | return false; |
594 | 0 | } |
595 | 0 | if (fWindowState.numWindows() && |
596 | 0 | (fRTOrigin != rtOrigin || fWidth != width || fHeight != height)) { |
597 | 0 | return false; |
598 | 0 | } |
599 | 0 | return fWindowState == windowState; |
600 | 0 | } |
601 | | |
602 | | private: |
603 | | enum { kInvalidSurfaceOrigin = -1 }; |
604 | | |
605 | | int fRTOrigin; |
606 | | int fWidth; |
607 | | int fHeight; |
608 | | GrWindowRectsState fWindowState; |
609 | | } fHWWindowRectsState; |
610 | | |
611 | | GrNativeRect fHWViewport; |
612 | | |
613 | | /** |
614 | | * Tracks vertex attrib array state. |
615 | | */ |
616 | | class HWVertexArrayState { |
617 | | public: |
618 | 0 | HWVertexArrayState() : fCoreProfileVertexArray(nullptr) { this->invalidate(); } |
619 | | |
620 | 0 | ~HWVertexArrayState() { delete fCoreProfileVertexArray; } |
621 | | |
622 | 0 | void invalidate() { |
623 | 0 | fBoundVertexArrayIDIsValid = false; |
624 | 0 | fDefaultVertexArrayAttribState.invalidate(); |
625 | 0 | if (fCoreProfileVertexArray) { |
626 | 0 | fCoreProfileVertexArray->invalidateCachedState(); |
627 | 0 | } |
628 | 0 | } |
629 | | |
630 | 0 | void notifyVertexArrayDelete(GrGLuint id) { |
631 | 0 | if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) { |
632 | 0 | // Does implicit bind to 0 |
633 | 0 | fBoundVertexArrayID = 0; |
634 | 0 | } |
635 | 0 | } |
636 | | |
637 | 0 | void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) { |
638 | 0 | if (!gpu->glCaps().vertexArrayObjectSupport()) { |
639 | 0 | SkASSERT(0 == arrayID); |
640 | 0 | return; |
641 | 0 | } |
642 | 0 | if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) { |
643 | 0 | GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID)); |
644 | 0 | fBoundVertexArrayIDIsValid = true; |
645 | 0 | fBoundVertexArrayID = arrayID; |
646 | 0 | } |
647 | 0 | } Unexecuted instantiation: GrGLGpu::HWVertexArrayState::setVertexArrayID(GrGLGpu*, unsigned int) Unexecuted instantiation: GrGLGpu::HWVertexArrayState::setVertexArrayID(GrGLGpu*, unsigned int) |
648 | | |
649 | | /** |
650 | | * Binds the vertex array that should be used for internal draws, and returns its attrib |
651 | | * state. This binds the default VAO (ID=zero) unless we are on a core profile, in which |
652 | | * case we use a placeholder array instead. |
653 | | * |
654 | | * If an index buffer is provided, it will be bound to the vertex array. Otherwise the |
655 | | * index buffer binding will be left unchanged. |
656 | | * |
657 | | * The returned GrGLAttribArrayState should be used to set vertex attribute arrays. |
658 | | */ |
659 | | GrGLAttribArrayState* bindInternalVertexArray(GrGLGpu*, const GrBuffer* ibuff = nullptr); |
660 | | |
661 | | private: |
662 | | GrGLuint fBoundVertexArrayID; |
663 | | bool fBoundVertexArrayIDIsValid; |
664 | | |
665 | | // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0 |
666 | | // is bound. However, this class is internal to GrGLGpu and this object never leaks out of |
667 | | // GrGLGpu. |
668 | | GrGLAttribArrayState fDefaultVertexArrayAttribState; |
669 | | |
670 | | // This is used when we're using a core profile. |
671 | | GrGLVertexArray* fCoreProfileVertexArray; |
672 | | } fHWVertexArrayState; |
673 | | |
674 | | uint8_t fHWPatchVertexCount; |
675 | | |
676 | | struct { |
677 | | GrGLenum fGLTarget; |
678 | | GrGpuResource::UniqueID fBoundBufferUniqueID; |
679 | | bool fBufferZeroKnownBound; |
680 | | |
681 | 0 | void invalidate() { |
682 | 0 | fBoundBufferUniqueID.makeInvalid(); |
683 | 0 | fBufferZeroKnownBound = false; |
684 | 0 | } |
685 | | } fHWBufferState[kGrGpuBufferTypeCount]; |
686 | | |
687 | 0 | auto* hwBufferState(GrGpuBufferType type) { |
688 | 0 | unsigned typeAsUInt = static_cast<unsigned>(type); |
689 | 0 | SkASSERT(typeAsUInt < SK_ARRAY_COUNT(fHWBufferState)); |
690 | 0 | SkASSERT(type != GrGpuBufferType::kUniform); |
691 | 0 | return &fHWBufferState[typeAsUInt]; |
692 | 0 | } Unexecuted instantiation: GrGLGpu::hwBufferState(GrGpuBufferType) Unexecuted instantiation: GrGLGpu::hwBufferState(GrGpuBufferType) |
693 | | |
694 | | enum class FlushType { |
695 | | kIfRequired, |
696 | | kForce, |
697 | | }; |
698 | | |
699 | | // This calls glFlush if it is required for previous operations or kForce is passed. |
700 | | void flush(FlushType flushType = FlushType::kIfRequired); |
701 | | |
702 | 0 | void setNeedsFlush() { fNeedsGLFlush = true; } |
703 | | |
704 | | struct { |
705 | | GrBlendEquation fEquation; |
706 | | GrBlendCoeff fSrcCoeff; |
707 | | GrBlendCoeff fDstCoeff; |
708 | | SkPMColor4f fConstColor; |
709 | | bool fConstColorValid; |
710 | | TriState fEnabled; |
711 | | |
712 | 0 | void invalidate() { |
713 | 0 | fEquation = kIllegal_GrBlendEquation; |
714 | 0 | fSrcCoeff = kIllegal_GrBlendCoeff; |
715 | 0 | fDstCoeff = kIllegal_GrBlendCoeff; |
716 | 0 | fConstColorValid = false; |
717 | 0 | fEnabled = kUnknown_TriState; |
718 | 0 | } |
719 | | } fHWBlendState; |
720 | | |
721 | | TriState fHWConservativeRasterEnabled; |
722 | | |
723 | | TriState fHWWireframeEnabled; |
724 | | |
725 | | GrStencilSettings fHWStencilSettings; |
726 | | GrSurfaceOrigin fHWStencilOrigin; |
727 | | TriState fHWStencilTestEnabled; |
728 | | |
729 | | TriState fHWWriteToColor; |
730 | | GrGpuResource::UniqueID fHWBoundRenderTargetUniqueID; |
731 | | bool fHWBoundFramebufferIsMSAA; |
732 | | TriState fHWSRGBFramebuffer; |
733 | | |
734 | | class TextureUnitBindings { |
735 | | public: |
736 | 0 | TextureUnitBindings() = default; |
737 | | TextureUnitBindings(const TextureUnitBindings&) = delete; |
738 | | TextureUnitBindings& operator=(const TextureUnitBindings&) = delete; |
739 | | |
740 | | GrGpuResource::UniqueID boundID(GrGLenum target) const; |
741 | | bool hasBeenModified(GrGLenum target) const; |
742 | | void setBoundID(GrGLenum target, GrGpuResource::UniqueID); |
743 | | void invalidateForScratchUse(GrGLenum target); |
744 | | void invalidateAllTargets(bool markUnmodified); |
745 | | |
746 | | private: |
747 | | struct TargetBinding { |
748 | | GrGpuResource::UniqueID fBoundResourceID; |
749 | | bool fHasBeenModified = false; |
750 | | }; |
751 | | TargetBinding fTargetBindings[3]; |
752 | | }; |
753 | | SkAutoTArray<TextureUnitBindings> fHWTextureUnitBindings; |
754 | | |
755 | | GrGLfloat fHWClearColor[4]; |
756 | | |
757 | | GrGLuint fBoundDrawFramebuffer = 0; |
758 | | |
759 | | /** IDs for copy surface program. (3 sampler types) */ |
760 | | struct { |
761 | | GrGLuint fProgram = 0; |
762 | | GrGLint fTextureUniform = 0; |
763 | | GrGLint fTexCoordXformUniform = 0; |
764 | | GrGLint fPosXformUniform = 0; |
765 | | } fCopyPrograms[3]; |
766 | | sk_sp<GrGLBuffer> fCopyProgramArrayBuffer; |
767 | | |
768 | | /** IDs for texture mipmap program. (4 filter configurations) */ |
769 | | struct { |
770 | | GrGLuint fProgram = 0; |
771 | | GrGLint fTextureUniform = 0; |
772 | | GrGLint fTexCoordXformUniform = 0; |
773 | | } fMipmapPrograms[4]; |
774 | | sk_sp<GrGLBuffer> fMipmapProgramArrayBuffer; |
775 | | |
776 | | static int TextureToCopyProgramIdx(GrTexture* texture); |
777 | | |
778 | 0 | static int TextureSizeToMipmapProgramIdx(int width, int height) { |
779 | 0 | const bool wide = (width > 1) && SkToBool(width & 0x1); |
780 | 0 | const bool tall = (height > 1) && SkToBool(height & 0x1); |
781 | 0 | return (wide ? 0x2 : 0x0) | (tall ? 0x1 : 0x0); |
782 | 0 | } |
783 | | |
784 | | GrPrimitiveType fLastPrimitiveType; |
785 | | |
786 | | GrGLTextureParameters::ResetTimestamp fResetTimestampForTextureParameters = 0; |
787 | | |
788 | | class SamplerObjectCache; |
789 | | std::unique_ptr<SamplerObjectCache> fSamplerObjectCache; |
790 | | |
791 | | std::unique_ptr<GrGLOpsRenderPass> fCachedOpsRenderPass; |
792 | | GrFinishCallbacks fFinishCallbacks; |
793 | | |
794 | | // If we've called a command that requires us to call glFlush than this will be set to true |
795 | | // since we defer calling flush until submit time. When we call submitToGpu if this is true then |
796 | | // we call glFlush and reset this to false. |
797 | | bool fNeedsGLFlush = false; |
798 | | |
799 | | SkDEBUGCODE(bool fIsExecutingCommandBuffer_DebugOnly = false); |
800 | | |
801 | | friend class GrGLPathRendering; // For accessing setTextureUnit. |
802 | | |
803 | | using INHERITED = GrGpu; |
804 | | }; |
805 | | |
806 | | #endif |