blob: 32ce979c96a4c25b2fe95fb0e50a74650ebc86f5 [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
Robert Phillipsf2361d22016-10-25 14:20:06 -0400145 void finishOpList() override;
ethannicholas22793252016-01-30 09:59:10 -0800146
jvanverth84741b32016-09-30 08:39:02 -0700147 GrFence SK_WARN_UNUSED_RESULT insertFence() const override;
148 bool waitFence(GrFence, uint64_t timeout) const override;
149 void deleteFence(GrFence) const override;
150
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000151private:
bsalomon424cc262015-05-22 10:37:30 -0700152 GrGLGpu(GrGLContext* ctx, GrContext* context);
153
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000154 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700155 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000156
bsalomoncb02b382015-08-12 11:14:50 -0700157 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
158
kkinnunen2e6055b2016-04-22 01:48:29 -0700159 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800160 const SkTArray<GrMipLevel>& texels) override;
egdanielb0e1be22015-04-22 13:27:39 -0700161 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -0700162 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800163 const SkTArray<GrMipLevel>& texels) override;
164
cdalton1bf3e712016-04-19 10:00:02 -0700165 GrBuffer* onCreateBuffer(size_t size, GrBufferType intendedType, GrAccessPattern,
166 const void* data) override;
bungeman6bd52842016-10-27 09:30:08 -0700167 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
168 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
169 GrWrapOwnership) override;
170 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&) override;
csmartdaltone0d36292016-07-29 08:14:20 -0700171
172 gr_instanced::InstancedRendering* onCreateInstancedRendering() override;
173
egdanielff1d5472015-09-10 08:37:20 -0700174 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800175 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700176 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177
bsalomon7e68ab72016-04-13 14:29:25 -0700178
erikchen9a1ed5d2016-02-10 16:32:34 -0800179 // Returns whether the texture is successfully created. On success, the
180 // result is stored in |info|.
cblume55f2d2d2016-02-26 13:20:48 -0800181 // The texture is populated with |texels|, if it exists.
erikchen9a1ed5d2016-02-10 16:32:34 -0800182 // The texture parameters are cached in |initialTexParams|.
183 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800184 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
185 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800186
bsalomone179a912016-01-20 06:18:10 -0800187 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
188 GrTextureProducer::CopyParams*) const override;
189
bsalomon1aa20292016-01-22 08:16:09 -0800190 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
191 // render target.
192 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
193
194 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
195 // render target that has renderTargetConfig. This may have to create a temporary
196 // render target and thus is less preferable than the variant that takes a render target.
197 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
198
199 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
200 // render target that has the same config as surfaceForConfig. Calls one of the the two
201 // variations above, depending on whether the surface is a render target or not.
202 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
203
bsalomon6cb3cbe2015-07-30 07:34:27 -0700204 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800205 int left, int top,
206 int width, int height,
207 GrPixelConfig,
208 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700209 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800210
bsalomon6cb3cbe2015-07-30 07:34:27 -0700211 bool onWritePixels(GrSurface*,
212 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800213 GrPixelConfig config,
214 const SkTArray<GrMipLevel>& texels) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000215
jvanverthc3d706f2016-04-20 10:33:27 -0700216 bool onTransferPixels(GrSurface*,
jvanverth17aa0472016-01-05 10:41:27 -0800217 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700218 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800219 size_t offset, size_t rowBytes) override;
220
mtklein36352bf2015-03-25 18:17:31 -0700221 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000222
joshualitt1cbdcde2015-08-21 11:53:29 -0700223 bool onCopySurface(GrSurface* dst,
224 GrSurface* src,
225 const SkIRect& srcRect,
226 const SkIPoint& dstPoint) override;
227
csmartdaltonc25c5d72016-11-01 07:03:59 -0700228 void onQueryMultisampleSpecs(GrRenderTarget*, const GrStencilSettings&,
229 int* effectiveSampleCnt, SamplePattern*) override;
cdalton28f45b92016-03-07 13:58:26 -0800230
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000231 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000232 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
cdalton74b8d322016-04-11 14:47:28 -0700234 void setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]);
235
egdaniel8dd688b2015-01-22 10:16:09 -0800236 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
bsalomon2eda5b32016-09-21 10:53:24 -0700237 // willDrawPoints must be true if point primitives will be rendered after setting the GL state.
238 bool flushGLState(const GrPipeline&, const GrPrimitiveProcessor&, bool willDrawPoints);
bsalomond95263c2014-12-16 13:05:12 -0800239
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000240 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700241 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000242 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800243 void setupGeometry(const GrPrimitiveProcessor&,
egdaniel0e1853c2016-03-17 11:35:45 -0700244 const GrNonInstancedMesh& mesh,
joshualittd53a8272014-11-10 16:03:14 -0800245 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000246
bsalomon7f9b2e42016-01-12 13:29:26 -0800247 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000248
bsalomon424cc262015-05-22 10:37:30 -0700249 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000250
cdaltone2e71c22016-04-07 18:13:29 -0700251 bool copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -0700252 GrSurface* src,
253 const SkIRect& srcRect,
254 const SkIPoint& dstPoint);
255 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
256 GrSurface* src,
257 const SkIRect& srcRect,
258 const SkIPoint& dstPoint);
259 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
260 GrSurface* src,
261 const SkIRect& srcRect,
262 const SkIPoint& dstPoint);
brianosman33f6b3f2016-06-02 05:49:21 -0700263 bool generateMipmap(GrGLTexture* texture, bool gammaCorrect);
bsalomon6df86402015-06-01 10:41:49 -0700264
cdaltone2e71c22016-04-07 18:13:29 -0700265 void stampPLSSetupRect(const SkRect& bounds);
ethannicholas22793252016-01-30 09:59:10 -0800266
egdaniel0e1853c2016-03-17 11:35:45 -0700267 void setupPixelLocalStorage(const GrPipeline&, const GrPrimitiveProcessor&);
ethannicholas22793252016-01-30 09:59:10 -0800268
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000269 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000270
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000271 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000272 public:
bsalomon861e1032014-12-16 07:33:49 -0800273 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000274 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000275
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000276 void abandon();
bsalomon2eda5b32016-09-21 10:53:24 -0700277 GrGLProgram* refProgram(const GrGLGpu*, const GrPipeline&, const GrPrimitiveProcessor&,
278 bool hasPointSize);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000279
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000280 private:
281 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000282 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
283 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000284 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000285 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000286 };
287
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000288 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000289
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000290 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000291
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000292 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
293 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700294 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000295
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000296 // sorted array of all the entries
297 Entry* fEntries[kMaxEntries];
298 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
299 // searching fEntries.
300 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000301
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000302 int fCount;
303 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800304 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000305#ifdef PROGRAM_CACHE_STATS
306 int fTotalRequests;
307 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000308 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000309#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000310 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000311
egdaniel080e6732014-12-22 07:35:52 -0800312 void flushColorWrite(bool writeColor);
robertphillips5fa7f302016-07-21 09:21:04 -0700313 void flushDrawFace(GrDrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000314
bsalomon@google.coma3201942012-06-21 19:58:20 +0000315 // flushes the scissor. see the note on flushBoundTextureAndParams about
316 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800317 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700318 const GrGLIRect& rtViewport,
319 GrSurfaceOrigin rtOrigin);
320
321 // disables the scissor
322 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000323
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700324 void flushWindowRectangles(const GrWindowRectsState&, const GrGLRenderTarget*);
csmartdalton28341fa2016-08-17 10:00:21 -0700325 void disableWindowRectangles();
326
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000327 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000328
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000329 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000330 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000331
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000332 // sets a texture unit to use for texture operations other than binding a texture to a program.
333 // ensures that such operations don't negatively interact with tracking bound textures.
334 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000335
bsalomon083617b2016-02-12 12:10:14 -0800336 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700337 // nullptr means whole target. Can be an empty rect.
brianosman64d094d2016-03-25 06:01:59 -0700338 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds, bool disableSRGB = false);
bsalomon083617b2016-02-12 12:10:14 -0800339
340 // Need not be called if flushRenderTarget is used.
341 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700342
bsalomon3e791242014-12-17 13:43:13 -0800343 void flushStencil(const GrStencilSettings&);
csmartdaltonc7d85332016-10-26 10:13:46 -0700344 void disableStencil();
bsalomon083617b2016-02-12 12:10:14 -0800345
346 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800347 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000348
ethannicholas28ef4452016-03-25 09:26:03 -0700349 void flushMinSampleShading(float minSampleShading);
350
brianosman33f6b3f2016-06-02 05:49:21 -0700351 void flushFramebufferSRGB(bool enable);
352
bsalomon@google.com6f379512011-11-16 20:36:03 +0000353 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800354 enum UploadType {
355 kNewTexture_UploadType, // we are creating a new texture
356 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
357 kTransfer_UploadType, // we are using a transfer buffer to copy data
358 };
bsalomonb15b4c12014-10-29 12:41:57 -0700359 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800360 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800361 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000362 int left, int top, int width, int height,
363 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800364 const SkTArray<GrMipLevel>& texels);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000365
krajcevski145d48c2014-06-11 16:07:50 -0700366 // helper for onCreateCompressedTexture. If width and height are
367 // set to -1, then this function will use desc.fWidth and desc.fHeight
368 // for the size of the data. The isNewTexture flag should be set to true
369 // whenever a new texture needs to be created. Otherwise, we assume that
370 // the texture is already in GPU memory and that it's going to be updated
371 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700372 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700373 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -0800374 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -0800375 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700376 int left = 0, int top = 0,
377 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700378
kkinnunen2e6055b2016-04-22 01:48:29 -0700379 bool createRenderTargetObjects(const GrSurfaceDesc&, const GrGLTextureInfo& texInfo,
380 GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000381
egdanield803f272015-03-18 13:01:52 -0700382 enum TempFBOTarget {
383 kSrc_TempFBOTarget,
384 kDst_TempFBOTarget
385 };
egdaniel0f5f9672015-02-03 11:10:51 -0800386
Brian Salomon71d9d842016-11-03 13:42:00 -0400387 // Binds a surface as a FBO for copying or reading. If the surface already owns an FBO ID then
bsalomon10528f12015-10-14 12:54:52 -0700388 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
Brian Salomon71d9d842016-11-03 13:42:00 -0400389 // This must be paired with a call to unbindSurfaceFBOForPixelOps().
390 void bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
391 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800392
Brian Salomon71d9d842016-11-03 13:42:00 -0400393 // Must be called if bindSurfaceFBOForPixelOps was used to bind a surface for copying.
394 void unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700395
Hal Canary144caf52016-11-07 17:57:18 -0500396 sk_sp<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000397
cdaltone2e71c22016-04-07 18:13:29 -0700398 bool createCopyProgram(int progIdx);
brianosman33f6b3f2016-06-02 05:49:21 -0700399 bool createMipmapProgram(int progIdx);
cdaltone2e71c22016-04-07 18:13:29 -0700400 bool createWireRectProgram();
401 bool createPLSSetupProgram();
ethannicholas22793252016-01-30 09:59:10 -0800402
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000403 // GL program-related state
404 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000405
406 ///////////////////////////////////////////////////////////////////////////
407 ///@name Caching of GL State
408 ///@{
409 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000410 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000411
bsalomon@google.com49209392012-06-05 15:13:46 +0000412 enum TriState {
413 kNo_TriState,
414 kYes_TriState,
415 kUnknown_TriState
416 };
417
egdanield803f272015-03-18 13:01:52 -0700418 GrGLuint fTempSrcFBOID;
419 GrGLuint fTempDstFBOID;
420
421 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800422
bsalomon@google.coma3201942012-06-21 19:58:20 +0000423 // last scissor / viewport scissor state seen by the GL.
424 struct {
425 TriState fEnabled;
426 GrGLIRect fRect;
427 void invalidate() {
428 fEnabled = kUnknown_TriState;
429 fRect.invalidate();
430 }
431 } fHWScissorSettings;
432
csmartdalton28341fa2016-08-17 10:00:21 -0700433 class {
434 public:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700435 bool valid() const { return kInvalidSurfaceOrigin != fRTOrigin; }
436 void invalidate() { fRTOrigin = kInvalidSurfaceOrigin; }
437 bool knownDisabled() const { return this->valid() && !fWindowState.enabled(); }
mtklein8b4a2022016-09-26 08:44:47 -0700438 void setDisabled() {
439 fRTOrigin = kDefault_GrSurfaceOrigin;
440 fWindowState.setDisabled();
441 }
csmartdalton28341fa2016-08-17 10:00:21 -0700442
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700443 void set(GrSurfaceOrigin rtOrigin, const GrGLIRect& viewport,
444 const GrWindowRectsState& windowState) {
445 fRTOrigin = rtOrigin;
446 fViewport = viewport;
447 fWindowState = windowState;
csmartdalton28341fa2016-08-17 10:00:21 -0700448 }
csmartdalton28341fa2016-08-17 10:00:21 -0700449
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700450 bool knownEqualTo(GrSurfaceOrigin rtOrigin, const GrGLIRect& viewport,
451 const GrWindowRectsState& windowState) const {
csmartdalton28341fa2016-08-17 10:00:21 -0700452 if (!this->valid()) {
453 return false;
454 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700455 if (fWindowState.numWindows() && (fRTOrigin != rtOrigin || fViewport != viewport)) {
csmartdalton28341fa2016-08-17 10:00:21 -0700456 return false;
457 }
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700458 return fWindowState.cheapEqualTo(windowState);
csmartdalton28341fa2016-08-17 10:00:21 -0700459 }
460
461 private:
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700462 enum { kInvalidSurfaceOrigin = -1 };
csmartdalton28341fa2016-08-17 10:00:21 -0700463
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700464 int fRTOrigin;
csmartdalton28341fa2016-08-17 10:00:21 -0700465 GrGLIRect fViewport;
csmartdaltonbf4a8f92016-09-06 10:01:06 -0700466 GrWindowRectsState fWindowState;
467 } fHWWindowRectsState;
csmartdalton28341fa2016-08-17 10:00:21 -0700468
bsalomon424cc262015-05-22 10:37:30 -0700469 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000470
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000471 /**
cdaltone2e71c22016-04-07 18:13:29 -0700472 * Tracks vertex attrib array state.
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000473 */
cdaltone2e71c22016-04-07 18:13:29 -0700474 class HWVertexArrayState {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000475 public:
cdaltone2e71c22016-04-07 18:13:29 -0700476 HWVertexArrayState() : fCoreProfileVertexArray(nullptr) { this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000477
cdaltone2e71c22016-04-07 18:13:29 -0700478 ~HWVertexArrayState() { delete fCoreProfileVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000479
bsalomon@google.com6918d482013-03-07 19:09:11 +0000480 void invalidate() {
481 fBoundVertexArrayIDIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000482 fDefaultVertexArrayAttribState.invalidate();
cdaltone2e71c22016-04-07 18:13:29 -0700483 if (fCoreProfileVertexArray) {
484 fCoreProfileVertexArray->invalidateCachedState();
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000485 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000486 }
487
488 void notifyVertexArrayDelete(GrGLuint id) {
489 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
490 // Does implicit bind to 0
491 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000492 }
493 }
494
bsalomon861e1032014-12-16 07:33:49 -0800495 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000496 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000497 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000498 return;
499 }
500 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
501 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
502 fBoundVertexArrayIDIsValid = true;
503 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000504 }
505 }
506
bsalomon@google.com6918d482013-03-07 19:09:11 +0000507 /**
cdaltone2e71c22016-04-07 18:13:29 -0700508 * Binds the vertex array that should be used for internal draws, and returns its attrib
509 * state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
510 * case we use a dummy array instead.
511 *
512 * If an index buffer is privided, it will be bound to the vertex array. Otherwise the
513 * index buffer binding will be left unchanged.
514 *
515 * The returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000516 */
csmartdalton485a1202016-07-13 10:16:32 -0700517 GrGLAttribArrayState* bindInternalVertexArray(GrGLGpu*, const GrBuffer* ibuff = nullptr);
bsalomon6df86402015-06-01 10:41:49 -0700518
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000519 private:
mtklein044d3c12016-04-06 18:24:34 -0700520 GrGLuint fBoundVertexArrayID;
mtklein044d3c12016-04-06 18:24:34 -0700521 bool fBoundVertexArrayIDIsValid;
mtklein044d3c12016-04-06 18:24:34 -0700522
bsalomon@google.com6918d482013-03-07 19:09:11 +0000523 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800524 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
525 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000526 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000527
cdaltone2e71c22016-04-07 18:13:29 -0700528 // This is used when we're using a core profile.
529 GrGLVertexArray* fCoreProfileVertexArray;
530 } fHWVertexArrayState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531
cdaltone2e71c22016-04-07 18:13:29 -0700532 struct {
533 GrGLenum fGLTarget;
534 uint32_t fBoundBufferUniqueID;
535 bool fBufferZeroKnownBound;
536
537 void invalidate() {
538 fBoundBufferUniqueID = SK_InvalidUniqueID;
539 fBufferZeroKnownBound = false;
540 }
541 } fHWBufferState[kGrBufferTypeCount];
cdaltonc1613102016-03-16 07:48:20 -0700542
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000543 struct {
cdalton8917d622015-05-06 13:40:21 -0700544 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000545 GrBlendCoeff fSrcCoeff;
546 GrBlendCoeff fDstCoeff;
547 GrColor fConstColor;
548 bool fConstColorValid;
549 TriState fEnabled;
550
551 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700552 fEquation = static_cast<GrBlendEquation>(-1);
553 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
554 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000555 fConstColorValid = false;
556 fEnabled = kUnknown_TriState;
557 }
558 } fHWBlendState;
559
bsalomon54c6fe82015-12-16 11:51:22 -0800560 TriState fMSAAEnabled;
561
562 GrStencilSettings fHWStencilSettings;
563 TriState fHWStencilTestEnabled;
564
565
robertphillips5fa7f302016-07-21 09:21:04 -0700566 GrDrawFace fHWDrawFace;
bsalomon54c6fe82015-12-16 11:51:22 -0800567 TriState fHWWriteToColor;
568 uint32_t fHWBoundRenderTargetUniqueID;
569 TriState fHWSRGBFramebuffer;
570 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800571
cdalton74b8d322016-04-11 14:47:28 -0700572 struct BufferTexture {
573 BufferTexture() : fTextureID(0), fKnownBound(false),
574 fAttachedBufferUniqueID(SK_InvalidUniqueID),
575 fSwizzle(GrSwizzle::RGBA()) {}
576
577 GrGLuint fTextureID;
578 bool fKnownBound;
cdalton74b8d322016-04-11 14:47:28 -0700579 GrPixelConfig fTexelConfig;
cdalton74b8d322016-04-11 14:47:28 -0700580 uint32_t fAttachedBufferUniqueID;
581 GrSwizzle fSwizzle;
582 };
583
584 SkTArray<BufferTexture, true> fHWBufferTextures;
585 int fHWMaxUsedBufferTextureUnit;
586
cdaltonaf8bc7d2016-02-05 09:35:20 -0800587 // EXT_raster_multisample.
588 TriState fHWRasterMultisampleEnabled;
589 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800590 ///@}
591
bsalomon6df86402015-06-01 10:41:49 -0700592 /** IDs for copy surface program. */
593 struct {
594 GrGLuint fProgram;
595 GrGLint fTextureUniform;
596 GrGLint fTexCoordXformUniform;
597 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800598 } fCopyPrograms[3];
Hal Canary144caf52016-11-07 17:57:18 -0500599 sk_sp<GrGLBuffer> fCopyProgramArrayBuffer;
bsalomon7ea33f52015-11-22 14:51:00 -0800600
brianosman33f6b3f2016-06-02 05:49:21 -0700601 /** IDs for texture mipmap program. (4 filter configurations) */
602 struct {
603 GrGLuint fProgram;
604 GrGLint fTextureUniform;
605 GrGLint fTexCoordXformUniform;
606 } fMipmapPrograms[4];
Hal Canary144caf52016-11-07 17:57:18 -0500607 sk_sp<GrGLBuffer> fMipmapProgramArrayBuffer;
brianosman33f6b3f2016-06-02 05:49:21 -0700608
bsalomon6dea83f2015-12-03 12:58:06 -0800609 struct {
610 GrGLuint fProgram;
611 GrGLint fColorUniform;
612 GrGLint fRectUniform;
613 } fWireRectProgram;
Hal Canary144caf52016-11-07 17:57:18 -0500614 sk_sp<GrGLBuffer> fWireRectArrayBuffer;
bsalomon6dea83f2015-12-03 12:58:06 -0800615
bsalomon7ea33f52015-11-22 14:51:00 -0800616 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800617 switch (target) {
618 case GR_GL_TEXTURE_2D:
619 return 0;
620 case GR_GL_TEXTURE_EXTERNAL:
621 return 1;
622 case GR_GL_TEXTURE_RECTANGLE:
623 return 2;
624 default:
625 SkFAIL("Unexpected texture target type.");
626 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800627 }
628 }
bsalomon6df86402015-06-01 10:41:49 -0700629
brianosman33f6b3f2016-06-02 05:49:21 -0700630 static int TextureSizeToMipmapProgramIdx(int width, int height) {
631 const bool wide = (width > 1) && SkToBool(width & 0x1);
632 const bool tall = (height > 1) && SkToBool(height & 0x1);
633 return (wide ? 0x2 : 0x0) | (tall ? 0x1 : 0x0);
634 }
635
ethannicholas22793252016-01-30 09:59:10 -0800636 struct {
cdaltone2e71c22016-04-07 18:13:29 -0700637 GrGLuint fProgram;
638 GrGLint fPosXformUniform;
Hal Canary144caf52016-11-07 17:57:18 -0500639 sk_sp<GrGLBuffer> fArrayBuffer;
ethannicholas22793252016-01-30 09:59:10 -0800640 } fPLSSetupProgram;
641
642 bool fHWPLSEnabled;
643 bool fPLSHasBeenUsed;
644
ethannicholas28ef4452016-03-25 09:26:03 -0700645 float fHWMinSampleShading;
646
reed@google.comac10a2d2010-12-22 21:39:39 +0000647 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700648 friend class GrGLPathRendering; // For accessing setTextureUnit.
csmartdaltona7f29642016-07-07 08:49:11 -0700649 friend class gr_instanced::GLInstancedRendering; // For accessing flushGLState.
reed@google.comac10a2d2010-12-22 21:39:39 +0000650};
651
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000652#endif