blob: 62803ee84c943eb378bbbfbac0b72a506ff66ddf [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
jvanverth39edf762014-12-22 11:44:19 -08008#ifndef GrGLGpu_DEFINED
9#define GrGLGpu_DEFINED
reed@google.comac10a2d2010-12-22 21:39:39 +000010
robertphillips@google.com6177e692013-02-28 20:16:25 +000011#include "GrGLContext.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000012#include "GrGLIRect.h"
kkinnunenccdaa042014-08-20 01:36:23 -070013#include "GrGLPathRendering.h"
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000014#include "GrGLProgram.h"
bsalomon37dd3312014-11-03 08:47:23 -080015#include "GrGLRenderTarget.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070016#include "GrGLStencilAttachment.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrGLTexture.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000018#include "GrGLVertexArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "GrGpu.h"
cblume55f2d2d2016-02-26 13:20:48 -080020#include "GrTypes.h"
csmartdaltonbf4a8f92016-09-06 10:01:06 -070021#include "GrWindowRectsState.h"
egdaniel080e6732014-12-22 07:35:52 -080022#include "GrXferProcessor.h"
cblume55f2d2d2016-02-26 13:20:48 -080023#include "SkTArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000024#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
cdalton397536c2016-03-25 12:15:03 -070026class GrGLBuffer;
egdaniel8dd688b2015-01-22 10:16:09 -080027class GrPipeline;
egdaniel0e1853c2016-03-17 11:35:45 -070028class GrNonInstancedMesh;
bsalomon7f9b2e42016-01-12 13:29:26 -080029class GrSwizzle;
egdaniel8dd688b2015-01-22 10:16:09 -080030
csmartdaltona7f29642016-07-07 08:49:11 -070031namespace gr_instanced { class GLInstancedRendering; }
32
djsollenefe46d22016-04-29 06:41:35 -070033#ifdef SK_DEBUG
jvanverth@google.com94878772013-03-12 16:00:54 +000034#define PROGRAM_CACHE_STATS
35#endif
36
cdaltone2e71c22016-04-07 18:13:29 -070037class GrGLGpu final : public GrGpu {
reed@google.comac10a2d2010-12-22 21:39:39 +000038public:
bsalomon682c2692015-05-22 14:01:46 -070039 static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options,
40 GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070041 ~GrGLGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000042
bsalomon6e2aad42016-04-01 11:54:31 -070043 void disconnect(DisconnectType) override;
bsalomonc8dc1f72014-08-21 13:02:13 -070044
bsalomon424cc262015-05-22 10:37:30 -070045 const GrGLContext& glContext() const { return *fGLContext; }
commit-bot@chromium.orgc9424b82013-10-30 20:03:16 +000046
bsalomon424cc262015-05-22 10:37:30 -070047 const GrGLInterface* glInterface() const { return fGLContext->interface(); }
48 const GrGLContextInfo& ctxInfo() const { return *fGLContext; }
49 GrGLStandard glStandard() const { return fGLContext->standard(); }
50 GrGLVersion glVersion() const { return fGLContext->version(); }
51 GrGLSLGeneration glslGeneration() const { return fGLContext->glslGeneration(); }
52 const GrGLCaps& glCaps() const { return *fGLContext->caps(); }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000053
kkinnunenccdaa042014-08-20 01:36:23 -070054 GrGLPathRendering* glPathRendering() {
jvanverthe9c0fc62015-04-29 11:18:05 -070055 SkASSERT(glCaps().shaderCaps()->pathRenderingSupport());
kkinnunenccdaa042014-08-20 01:36:23 -070056 return static_cast<GrGLPathRendering*>(pathRendering());
cdaltonc7103a12014-08-11 14:05:05 -070057 }
58
kkinnunencfe62e32015-07-01 02:58:50 -070059 // Used by GrGLProgram to configure OpenGL state.
brianosmana167e742016-05-24 06:18:48 -070060 void bindTexture(int unitIdx, const GrTextureParams& params, bool allowSRGBInputs,
brianosmana6359362016-03-21 06:55:37 -070061 GrGLTexture* texture);
kkinnunenccdaa042014-08-20 01:36:23 -070062
csmartdalton1897cfd2016-06-03 08:50:54 -070063 void bindTexelBuffer(int unitIdx, GrPixelConfig, GrGLBuffer*);
cdalton74b8d322016-04-11 14:47:28 -070064
brianosman33f6b3f2016-06-02 05:49:21 -070065 void generateMipmaps(const GrTextureParams& params, bool allowSRGBInputs, GrGLTexture* texture);
66
bsalomonf0674512015-07-28 13:26:15 -070067 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
68 GrPixelConfig readConfig, DrawPreference*,
69 ReadPixelTempDrawInfo*) override;
bsalomon39826022015-07-23 08:07:21 -070070
bsalomonf0674512015-07-28 13:26:15 -070071 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -080072 GrPixelConfig srcConfig, DrawPreference*,
bsalomonf0674512015-07-28 13:26:15 -070073 WritePixelTempDrawInfo*) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000074
egdaniel4bcd62e2016-08-31 07:37:31 -070075 bool initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const override;
bsalomon@google.comeb851172013-04-15 13:51:00 +000076
bsalomon@google.com6918d482013-03-07 19:09:11 +000077 // These functions should be used to bind GL objects. They track the GL state and skip redundant
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +000078 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000079 void bindVertexArray(GrGLuint id) {
cdaltone2e71c22016-04-07 18:13:29 -070080 fHWVertexArrayState.setVertexArrayID(this, id);
bsalomon@google.com6918d482013-03-07 19:09:11 +000081 }
82
83 // These callbacks update state tracking when GL objects are deleted. They are called from
84 // GrGLResource onRelease functions.
85 void notifyVertexArrayDelete(GrGLuint id) {
cdaltone2e71c22016-04-07 18:13:29 -070086 fHWVertexArrayState.notifyVertexArrayDelete(id);
bsalomon@google.com6918d482013-03-07 19:09:11 +000087 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000088
cdaltone2e71c22016-04-07 18:13:29 -070089 // Binds a buffer to the GL target corresponding to 'type', updates internal state tracking, and
90 // returns the GL target the buffer was bound to.
91 // When 'type' is kIndex_GrBufferType, this function will also implicitly bind the default VAO.
92 // If the caller wishes to bind an index buffer to a specific VAO, it can call glBind directly.
csmartdalton485a1202016-07-13 10:16:32 -070093 GrGLenum bindBuffer(GrBufferType type, const GrBuffer*);
joshualitt93316b92015-10-23 09:08:08 -070094
cdalton74b8d322016-04-11 14:47:28 -070095 // Called by GrGLBuffer after its buffer object has been destroyed.
96 void notifyBufferReleased(const GrGLBuffer*);
97
egdaniel9cb63402016-06-23 08:37:05 -070098 // The GrGLGpuCommandBuffer does not buffer up draws before submitting them to the gpu.
99 // Thus this is the implementation of the draw call for the corresponding passthrough function
100 // on GrGLGpuCommandBuffer.
101 void draw(const GrPipeline&,
102 const GrPrimitiveProcessor&,
103 const GrMesh*,
104 int meshCount);
105
106 // The GrGLGpuCommandBuffer does not buffer up draws before submitting them to the gpu.
107 // Thus this is the implementation of the clear call for the corresponding passthrough function
108 // on GrGLGpuCommandBuffer.
csmartdalton29df7602016-08-31 11:55:52 -0700109 void clear(const GrFixedClip&, GrColor, GrRenderTarget*);
egdaniel9cb63402016-06-23 08:37:05 -0700110
111 // The GrGLGpuCommandBuffer does not buffer up draws before submitting them to the gpu.
112 // Thus this is the implementation of the clearStencil call for the corresponding passthrough
113 // function on GrGLGpuCommandBuffer.
csmartdalton29df7602016-08-31 11:55:52 -0700114 void clearStencilClip(const GrFixedClip&, bool insideStencilMask, GrRenderTarget*);
egdaniel9cb63402016-06-23 08:37:05 -0700115
reedf9ad5582015-06-25 21:29:25 -0700116 const GrGLContext* glContextForTesting() const override {
117 return &this->glContext();
bsalomon993a4212015-05-29 11:37:25 -0700118 }
119
egdanielec00d942015-09-14 12:56:10 -0700120 void clearStencil(GrRenderTarget*) override;
121
egdaniel9cb63402016-06-23 08:37:05 -0700122 GrGpuCommandBuffer* createCommandBuffer(
123 GrRenderTarget* target,
124 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
125 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) override;
egdaniel066df7c2016-06-08 14:02:27 -0700126
egdanielec00d942015-09-14 12:56:10 -0700127 void invalidateBoundRenderTarget() {
128 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
129 }
130
131 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
132 int width,
133 int height) override;
134
jvanverth88957922015-07-14 11:02:52 -0700135 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -0700136 GrPixelConfig config,
137 bool isRenderTarget = false) override;
bsalomon67d76202015-11-11 12:40:42 -0800138 bool isTestingOnlyBackendTexture(GrBackendObject) const override;
bsalomone63ffef2016-02-05 07:17:34 -0800139 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override;
jvanverth672bb7f2015-07-13 07:19:57 -0700140
joshualitt8fd844f2015-12-02 13:36:47 -0800141 void resetShaderCacheForTesting() const override;
142
bsalomon6dea83f2015-12-03 12:58:06 -0800143 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override;
144
jvanverthd2d2eb92016-02-17 14:04:46 -0800145 void finishDrawTarget() override;
ethannicholas22793252016-01-30 09:59:10 -0800146
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000147private:
bsalomon424cc262015-05-22 10:37:30 -0700148 GrGLGpu(GrGLContext* ctx, GrContext* context);
149
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000150 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700151 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152
bsalomoncb02b382015-08-12 11:14:50 -0700153 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
154
kkinnunen2e6055b2016-04-22 01:48:29 -0700155 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800156 const SkTArray<GrMipLevel>& texels) override;
egdanielb0e1be22015-04-22 13:27:39 -0700157 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -0700158 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800159 const SkTArray<GrMipLevel>& texels) override;
160
cdalton1bf3e712016-04-19 10:00:02 -0700161 GrBuffer* onCreateBuffer(size_t size, GrBufferType intendedType, GrAccessPattern,
162 const void* data) override;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700163 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
164 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
165 GrWrapOwnership) override;
kkinnunen49c4c222016-04-01 04:50:37 -0700166 GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&) override;
csmartdaltone0d36292016-07-29 08:14:20 -0700167
168 gr_instanced::InstancedRendering* onCreateInstancedRendering() override;
169
egdanielff1d5472015-09-10 08:37:20 -0700170 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800171 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700172 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000173
bsalomon7e68ab72016-04-13 14:29:25 -0700174
erikchen9a1ed5d2016-02-10 16:32:34 -0800175 // Returns whether the texture is successfully created. On success, the
176 // result is stored in |info|.
cblume55f2d2d2016-02-26 13:20:48 -0800177 // The texture is populated with |texels|, if it exists.
erikchen9a1ed5d2016-02-10 16:32:34 -0800178 // The texture parameters are cached in |initialTexParams|.
179 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800180 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
181 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800182
bsalomone179a912016-01-20 06:18:10 -0800183 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
184 GrTextureProducer::CopyParams*) const override;
185
bsalomon1aa20292016-01-22 08:16:09 -0800186 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
187 // render target.
188 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
189
190 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
191 // render target that has renderTargetConfig. This may have to create a temporary
192 // render target and thus is less preferable than the variant that takes a render target.
193 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
194
195 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
196 // render target that has the same config as surfaceForConfig. Calls one of the the two
197 // variations above, depending on whether the surface is a render target or not.
198 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
199
bsalomon6cb3cbe2015-07-30 07:34:27 -0700200 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800201 int left, int top,
202 int width, int height,
203 GrPixelConfig,
204 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700205 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800206
bsalomon6cb3cbe2015-07-30 07:34:27 -0700207 bool onWritePixels(GrSurface*,
208 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800209 GrPixelConfig config,
210 const SkTArray<GrMipLevel>& texels) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211
jvanverthc3d706f2016-04-20 10:33:27 -0700212 bool onTransferPixels(GrSurface*,
jvanverth17aa0472016-01-05 10:41:27 -0800213 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700214 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800215 size_t offset, size_t rowBytes) override;
216
mtklein36352bf2015-03-25 18:17:31 -0700217 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000218
joshualitt1cbdcde2015-08-21 11:53:29 -0700219 bool onCopySurface(GrSurface* dst,
220 GrSurface* src,
221 const SkIRect& srcRect,
222 const SkIPoint& dstPoint) override;
223
csmartdalton0d28e572016-07-06 09:59:43 -0700224 void onGetMultisampleSpecs(GrRenderTarget*, const GrStencilSettings&,
225 int* effectiveSampleCnt, SamplePattern*) override;
cdalton28f45b92016-03-07 13:58:26 -0800226
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000227 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000228 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
cdalton74b8d322016-04-11 14:47:28 -0700230 void setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]);
231
egdaniel8dd688b2015-01-22 10:16:09 -0800232 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
egdaniel0e1853c2016-03-17 11:35:45 -0700233 bool flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc);
bsalomond95263c2014-12-16 13:05:12 -0800234
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000235 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700236 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000237 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800238 void setupGeometry(const GrPrimitiveProcessor&,
egdaniel0e1853c2016-03-17 11:35:45 -0700239 const GrNonInstancedMesh& mesh,
joshualittd53a8272014-11-10 16:03:14 -0800240 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000241
bsalomon7f9b2e42016-01-12 13:29:26 -0800242 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000243
bsalomon424cc262015-05-22 10:37:30 -0700244 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000245
cdaltone2e71c22016-04-07 18:13:29 -0700246 bool copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -0700247 GrSurface* src,
248 const SkIRect& srcRect,
249 const SkIPoint& dstPoint);
250 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
251 GrSurface* src,
252 const SkIRect& srcRect,
253 const SkIPoint& dstPoint);
254 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
255 GrSurface* src,
256 const SkIRect& srcRect,
257 const SkIPoint& dstPoint);
brianosman33f6b3f2016-06-02 05:49:21 -0700258 bool generateMipmap(GrGLTexture* texture, bool gammaCorrect);
bsalomon6df86402015-06-01 10:41:49 -0700259
cdaltone2e71c22016-04-07 18:13:29 -0700260 void stampPLSSetupRect(const SkRect& bounds);
ethannicholas22793252016-01-30 09:59:10 -0800261
egdaniel0e1853c2016-03-17 11:35:45 -0700262 void setupPixelLocalStorage(const GrPipeline&, const GrPrimitiveProcessor&);
ethannicholas22793252016-01-30 09:59:10 -0800263
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000264 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000265
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000266 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000267 public:
bsalomon861e1032014-12-16 07:33:49 -0800268 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000269 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000270
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000271 void abandon();
egdaniel0e1853c2016-03-17 11:35:45 -0700272 GrGLProgram* refProgram(const GrGLGpu* gpu, const GrPipeline&, const GrPrimitiveProcessor&);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000273
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000274 private:
275 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000276 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
277 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000278 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000279 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000280 };
281
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000282 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000283
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000284 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000285
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000286 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
287 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700288 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000289
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000290 // sorted array of all the entries
291 Entry* fEntries[kMaxEntries];
292 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
293 // searching fEntries.
294 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000295
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000296 int fCount;
297 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800298 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000299#ifdef PROGRAM_CACHE_STATS
300 int fTotalRequests;
301 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000302 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000303#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000304 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000305
egdaniel080e6732014-12-22 07:35:52 -0800306 void flushColorWrite(bool writeColor);
robertphillips5fa7f302016-07-21 09:21:04 -0700307 void flushDrawFace(GrDrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000308
bsalomon@google.coma3201942012-06-21 19:58:20 +0000309 // flushes the scissor. see the note on flushBoundTextureAndParams about
310 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800311 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700312 const GrGLIRect& rtViewport,
313 GrSurfaceOrigin rtOrigin);
314
315 // disables the scissor
316 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000317
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700318 void flushWindowRectangles(const GrWindowRectsState&, const GrGLRenderTarget*);
csmartdalton28341fa2016-08-17 10:00:21 -0700319 void disableWindowRectangles();
320
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000321 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000322
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000323 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000324 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000325
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000326 // sets a texture unit to use for texture operations other than binding a texture to a program.
327 // ensures that such operations don't negatively interact with tracking bound textures.
328 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
bsalomon083617b2016-02-12 12:10:14 -0800330 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700331 // nullptr means whole target. Can be an empty rect.
brianosman64d094d2016-03-25 06:01:59 -0700332 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds, bool disableSRGB = false);
bsalomon083617b2016-02-12 12:10:14 -0800333
334 // Need not be called if flushRenderTarget is used.
335 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700336
bsalomon3e791242014-12-17 13:43:13 -0800337 void flushStencil(const GrStencilSettings&);
bsalomon083617b2016-02-12 12:10:14 -0800338
339 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800340 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000341
ethannicholas28ef4452016-03-25 09:26:03 -0700342 void flushMinSampleShading(float minSampleShading);
343
brianosman33f6b3f2016-06-02 05:49:21 -0700344 void flushFramebufferSRGB(bool enable);
345
bsalomon@google.com6f379512011-11-16 20:36:03 +0000346 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800347 enum UploadType {
348 kNewTexture_UploadType, // we are creating a new texture
349 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
350 kTransfer_UploadType, // we are using a transfer buffer to copy data
351 };
bsalomonb15b4c12014-10-29 12:41:57 -0700352 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800353 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800354 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000355 int left, int top, int width, int height,
356 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800357 const SkTArray<GrMipLevel>& texels);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000358
krajcevski145d48c2014-06-11 16:07:50 -0700359 // helper for onCreateCompressedTexture. If width and height are
360 // set to -1, then this function will use desc.fWidth and desc.fHeight
361 // for the size of the data. The isNewTexture flag should be set to true
362 // whenever a new texture needs to be created. Otherwise, we assume that
363 // the texture is already in GPU memory and that it's going to be updated
364 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700365 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700366 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -0800367 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -0800368 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700369 int left = 0, int top = 0,
370 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700371
kkinnunen2e6055b2016-04-22 01:48:29 -0700372 bool createRenderTargetObjects(const GrSurfaceDesc&, const GrGLTextureInfo& texInfo,
373 GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000374
egdanield803f272015-03-18 13:01:52 -0700375 enum TempFBOTarget {
376 kSrc_TempFBOTarget,
377 kDst_TempFBOTarget
378 };
egdaniel0f5f9672015-02-03 11:10:51 -0800379
bsalomon10528f12015-10-14 12:54:52 -0700380 // Binds a surface as a FBO for a copy operation. If the surface already owns an FBO ID then
381 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
382 // This must be paired with a call to unbindSurfaceFBOForCopy().
383 void bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
egdanield803f272015-03-18 13:01:52 -0700384 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800385
bsalomon10528f12015-10-14 12:54:52 -0700386 // Must be called if bindSurfaceFBOForCopy was used to bind a surface for copying.
387 void unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700388
reedf9ad5582015-06-25 21:29:25 -0700389 SkAutoTUnref<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000390
cdaltone2e71c22016-04-07 18:13:29 -0700391 bool createCopyProgram(int progIdx);
brianosman33f6b3f2016-06-02 05:49:21 -0700392 bool createMipmapProgram(int progIdx);
cdaltone2e71c22016-04-07 18:13:29 -0700393 bool createWireRectProgram();
394 bool createPLSSetupProgram();
ethannicholas22793252016-01-30 09:59:10 -0800395
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000396 // GL program-related state
397 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000398
399 ///////////////////////////////////////////////////////////////////////////
400 ///@name Caching of GL State
401 ///@{
402 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000403 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000404
bsalomon@google.com49209392012-06-05 15:13:46 +0000405 enum TriState {
406 kNo_TriState,
407 kYes_TriState,
408 kUnknown_TriState
409 };
410
egdanield803f272015-03-18 13:01:52 -0700411 GrGLuint fTempSrcFBOID;
412 GrGLuint fTempDstFBOID;
413
414 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800415
bsalomon@google.coma3201942012-06-21 19:58:20 +0000416 // last scissor / viewport scissor state seen by the GL.
417 struct {
418 TriState fEnabled;
419 GrGLIRect fRect;
420 void invalidate() {
421 fEnabled = kUnknown_TriState;
422 fRect.invalidate();
423 }
424 } fHWScissorSettings;
425
csmartdalton28341fa2016-08-17 10:00:21 -0700426 class {
427 public:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700428 bool valid() const { return kInvalidSurfaceOrigin != fRTOrigin; }
429 void invalidate() { fRTOrigin = kInvalidSurfaceOrigin; }
430 bool knownDisabled() const { return this->valid() && !fWindowState.enabled(); }
431 void setDisabled() { fRTOrigin = kDefault_GrSurfaceOrigin, fWindowState.setDisabled(); }
csmartdalton28341fa2016-08-17 10:00:21 -0700432
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700433 void set(GrSurfaceOrigin rtOrigin, const GrGLIRect& viewport,
434 const GrWindowRectsState& windowState) {
435 fRTOrigin = rtOrigin;
436 fViewport = viewport;
437 fWindowState = windowState;
csmartdalton28341fa2016-08-17 10:00:21 -0700438 }
csmartdalton28341fa2016-08-17 10:00:21 -0700439
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700440 bool knownEqualTo(GrSurfaceOrigin rtOrigin, const GrGLIRect& viewport,
441 const GrWindowRectsState& windowState) const {
csmartdalton28341fa2016-08-17 10:00:21 -0700442 if (!this->valid()) {
443 return false;
444 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700445 if (fWindowState.numWindows() && (fRTOrigin != rtOrigin || fViewport != viewport)) {
csmartdalton28341fa2016-08-17 10:00:21 -0700446 return false;
447 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700448 return fWindowState.cheapEqualTo(windowState);
csmartdalton28341fa2016-08-17 10:00:21 -0700449 }
450
451 private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700452 enum { kInvalidSurfaceOrigin = -1 };
csmartdalton28341fa2016-08-17 10:00:21 -0700453
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700454 int fRTOrigin;
csmartdalton28341fa2016-08-17 10:00:21 -0700455 GrGLIRect fViewport;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700456 GrWindowRectsState fWindowState;
457 } fHWWindowRectsState;
csmartdalton28341fa2016-08-17 10:00:21 -0700458
bsalomon424cc262015-05-22 10:37:30 -0700459 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000460
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000461 /**
cdaltone2e71c22016-04-07 18:13:29 -0700462 * Tracks vertex attrib array state.
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000463 */
cdaltone2e71c22016-04-07 18:13:29 -0700464 class HWVertexArrayState {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000465 public:
cdaltone2e71c22016-04-07 18:13:29 -0700466 HWVertexArrayState() : fCoreProfileVertexArray(nullptr) { this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000467
cdaltone2e71c22016-04-07 18:13:29 -0700468 ~HWVertexArrayState() { delete fCoreProfileVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000469
bsalomon@google.com6918d482013-03-07 19:09:11 +0000470 void invalidate() {
471 fBoundVertexArrayIDIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000472 fDefaultVertexArrayAttribState.invalidate();
cdaltone2e71c22016-04-07 18:13:29 -0700473 if (fCoreProfileVertexArray) {
474 fCoreProfileVertexArray->invalidateCachedState();
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000475 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000476 }
477
478 void notifyVertexArrayDelete(GrGLuint id) {
479 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
480 // Does implicit bind to 0
481 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000482 }
483 }
484
bsalomon861e1032014-12-16 07:33:49 -0800485 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000486 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000488 return;
489 }
490 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
491 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
492 fBoundVertexArrayIDIsValid = true;
493 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000494 }
495 }
496
bsalomon@google.com6918d482013-03-07 19:09:11 +0000497 /**
cdaltone2e71c22016-04-07 18:13:29 -0700498 * Binds the vertex array that should be used for internal draws, and returns its attrib
499 * state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
500 * case we use a dummy array instead.
501 *
502 * If an index buffer is privided, it will be bound to the vertex array. Otherwise the
503 * index buffer binding will be left unchanged.
504 *
505 * The returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000506 */
csmartdalton485a1202016-07-13 10:16:32 -0700507 GrGLAttribArrayState* bindInternalVertexArray(GrGLGpu*, const GrBuffer* ibuff = nullptr);
bsalomon6df86402015-06-01 10:41:49 -0700508
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000509 private:
mtklein044d3c12016-04-06 18:24:34 -0700510 GrGLuint fBoundVertexArrayID;
mtklein044d3c12016-04-06 18:24:34 -0700511 bool fBoundVertexArrayIDIsValid;
mtklein044d3c12016-04-06 18:24:34 -0700512
bsalomon@google.com6918d482013-03-07 19:09:11 +0000513 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800514 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
515 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000516 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000517
cdaltone2e71c22016-04-07 18:13:29 -0700518 // This is used when we're using a core profile.
519 GrGLVertexArray* fCoreProfileVertexArray;
520 } fHWVertexArrayState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521
cdaltone2e71c22016-04-07 18:13:29 -0700522 struct {
523 GrGLenum fGLTarget;
524 uint32_t fBoundBufferUniqueID;
525 bool fBufferZeroKnownBound;
526
527 void invalidate() {
528 fBoundBufferUniqueID = SK_InvalidUniqueID;
529 fBufferZeroKnownBound = false;
530 }
531 } fHWBufferState[kGrBufferTypeCount];
cdaltonc1613102016-03-16 07:48:20 -0700532
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000533 struct {
cdalton8917d622015-05-06 13:40:21 -0700534 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000535 GrBlendCoeff fSrcCoeff;
536 GrBlendCoeff fDstCoeff;
537 GrColor fConstColor;
538 bool fConstColorValid;
539 TriState fEnabled;
540
541 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700542 fEquation = static_cast<GrBlendEquation>(-1);
543 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
544 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000545 fConstColorValid = false;
546 fEnabled = kUnknown_TriState;
547 }
548 } fHWBlendState;
549
bsalomon54c6fe82015-12-16 11:51:22 -0800550 TriState fMSAAEnabled;
551
552 GrStencilSettings fHWStencilSettings;
553 TriState fHWStencilTestEnabled;
554
555
robertphillips5fa7f302016-07-21 09:21:04 -0700556 GrDrawFace fHWDrawFace;
bsalomon54c6fe82015-12-16 11:51:22 -0800557 TriState fHWWriteToColor;
558 uint32_t fHWBoundRenderTargetUniqueID;
559 TriState fHWSRGBFramebuffer;
560 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800561
cdalton74b8d322016-04-11 14:47:28 -0700562 struct BufferTexture {
563 BufferTexture() : fTextureID(0), fKnownBound(false),
564 fAttachedBufferUniqueID(SK_InvalidUniqueID),
565 fSwizzle(GrSwizzle::RGBA()) {}
566
567 GrGLuint fTextureID;
568 bool fKnownBound;
cdalton74b8d322016-04-11 14:47:28 -0700569 GrPixelConfig fTexelConfig;
cdalton74b8d322016-04-11 14:47:28 -0700570 uint32_t fAttachedBufferUniqueID;
571 GrSwizzle fSwizzle;
572 };
573
574 SkTArray<BufferTexture, true> fHWBufferTextures;
575 int fHWMaxUsedBufferTextureUnit;
576
cdaltonaf8bc7d2016-02-05 09:35:20 -0800577 // EXT_raster_multisample.
578 TriState fHWRasterMultisampleEnabled;
579 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800580 ///@}
581
bsalomon6df86402015-06-01 10:41:49 -0700582 /** IDs for copy surface program. */
583 struct {
584 GrGLuint fProgram;
585 GrGLint fTextureUniform;
586 GrGLint fTexCoordXformUniform;
587 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800588 } fCopyPrograms[3];
cdaltone2e71c22016-04-07 18:13:29 -0700589 SkAutoTUnref<GrGLBuffer> fCopyProgramArrayBuffer;
bsalomon7ea33f52015-11-22 14:51:00 -0800590
brianosman33f6b3f2016-06-02 05:49:21 -0700591 /** IDs for texture mipmap program. (4 filter configurations) */
592 struct {
593 GrGLuint fProgram;
594 GrGLint fTextureUniform;
595 GrGLint fTexCoordXformUniform;
596 } fMipmapPrograms[4];
597 SkAutoTUnref<GrGLBuffer> fMipmapProgramArrayBuffer;
598
bsalomon6dea83f2015-12-03 12:58:06 -0800599 struct {
600 GrGLuint fProgram;
601 GrGLint fColorUniform;
602 GrGLint fRectUniform;
603 } fWireRectProgram;
cdaltone2e71c22016-04-07 18:13:29 -0700604 SkAutoTUnref<GrGLBuffer> fWireRectArrayBuffer;
bsalomon6dea83f2015-12-03 12:58:06 -0800605
bsalomon7ea33f52015-11-22 14:51:00 -0800606 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800607 switch (target) {
608 case GR_GL_TEXTURE_2D:
609 return 0;
610 case GR_GL_TEXTURE_EXTERNAL:
611 return 1;
612 case GR_GL_TEXTURE_RECTANGLE:
613 return 2;
614 default:
615 SkFAIL("Unexpected texture target type.");
616 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800617 }
618 }
bsalomon6df86402015-06-01 10:41:49 -0700619
brianosman33f6b3f2016-06-02 05:49:21 -0700620 static int TextureSizeToMipmapProgramIdx(int width, int height) {
621 const bool wide = (width > 1) && SkToBool(width & 0x1);
622 const bool tall = (height > 1) && SkToBool(height & 0x1);
623 return (wide ? 0x2 : 0x0) | (tall ? 0x1 : 0x0);
624 }
625
ethannicholas22793252016-01-30 09:59:10 -0800626 struct {
cdaltone2e71c22016-04-07 18:13:29 -0700627 GrGLuint fProgram;
628 GrGLint fPosXformUniform;
629 SkAutoTUnref<GrGLBuffer> fArrayBuffer;
ethannicholas22793252016-01-30 09:59:10 -0800630 } fPLSSetupProgram;
631
632 bool fHWPLSEnabled;
633 bool fPLSHasBeenUsed;
634
ethannicholas28ef4452016-03-25 09:26:03 -0700635 float fHWMinSampleShading;
636
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700638 friend class GrGLPathRendering; // For accessing setTextureUnit.
csmartdaltona7f29642016-07-07 08:49:11 -0700639 friend class gr_instanced::GLInstancedRendering; // For accessing flushGLState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000640};
641
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000642#endif