blob: 0b2198a8342ae69d66fb865140e1df56e94c383f [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"
egdaniel8dd688b2015-01-22 10:16:09 -080020#include "GrPipelineBuilder.h"
cblume55f2d2d2016-02-26 13:20:48 -080021#include "GrTypes.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
djsollenefe46d22016-04-29 06:41:35 -070031#ifdef SK_DEBUG
jvanverth@google.com94878772013-03-12 16:00:54 +000032#define PROGRAM_CACHE_STATS
33#endif
34
cdaltone2e71c22016-04-07 18:13:29 -070035class GrGLGpu final : public GrGpu {
reed@google.comac10a2d2010-12-22 21:39:39 +000036public:
bsalomon682c2692015-05-22 14:01:46 -070037 static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options,
38 GrContext* context);
mtklein36352bf2015-03-25 18:17:31 -070039 ~GrGLGpu() override;
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomon6e2aad42016-04-01 11:54:31 -070041 void disconnect(DisconnectType) override;
bsalomonc8dc1f72014-08-21 13:02:13 -070042
bsalomon424cc262015-05-22 10:37:30 -070043 const GrGLContext& glContext() const { return *fGLContext; }
commit-bot@chromium.orgc9424b82013-10-30 20:03:16 +000044
bsalomon424cc262015-05-22 10:37:30 -070045 const GrGLInterface* glInterface() const { return fGLContext->interface(); }
46 const GrGLContextInfo& ctxInfo() const { return *fGLContext; }
47 GrGLStandard glStandard() const { return fGLContext->standard(); }
48 GrGLVersion glVersion() const { return fGLContext->version(); }
49 GrGLSLGeneration glslGeneration() const { return fGLContext->glslGeneration(); }
50 const GrGLCaps& glCaps() const { return *fGLContext->caps(); }
bsalomon@google.com0b77d682011-08-19 13:28:54 +000051
kkinnunenccdaa042014-08-20 01:36:23 -070052 GrGLPathRendering* glPathRendering() {
jvanverthe9c0fc62015-04-29 11:18:05 -070053 SkASSERT(glCaps().shaderCaps()->pathRenderingSupport());
kkinnunenccdaa042014-08-20 01:36:23 -070054 return static_cast<GrGLPathRendering*>(pathRendering());
cdaltonc7103a12014-08-11 14:05:05 -070055 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 void discard(GrRenderTarget*) override;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000058
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
cdalton74b8d322016-04-11 14:47:28 -070063 void bindTexelBuffer(int unitIdx, intptr_t offsetInBytes, GrPixelConfig, GrGLBuffer*);
64
bsalomonf0674512015-07-28 13:26:15 -070065 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
66 GrPixelConfig readConfig, DrawPreference*,
67 ReadPixelTempDrawInfo*) override;
bsalomon39826022015-07-23 08:07:21 -070068
bsalomonf0674512015-07-28 13:26:15 -070069 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -080070 GrPixelConfig srcConfig, DrawPreference*,
bsalomonf0674512015-07-28 13:26:15 -070071 WritePixelTempDrawInfo*) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000072
joshualitt1c735482015-07-13 08:08:25 -070073 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override;
bsalomon@google.comeb851172013-04-15 13:51:00 +000074
bsalomon@google.com6918d482013-03-07 19:09:11 +000075 // 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 +000076 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000077 void bindVertexArray(GrGLuint id) {
cdaltone2e71c22016-04-07 18:13:29 -070078 fHWVertexArrayState.setVertexArrayID(this, id);
bsalomon@google.com6918d482013-03-07 19:09:11 +000079 }
80
81 // These callbacks update state tracking when GL objects are deleted. They are called from
82 // GrGLResource onRelease functions.
83 void notifyVertexArrayDelete(GrGLuint id) {
cdaltone2e71c22016-04-07 18:13:29 -070084 fHWVertexArrayState.notifyVertexArrayDelete(id);
bsalomon@google.com6918d482013-03-07 19:09:11 +000085 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000086
cdaltone2e71c22016-04-07 18:13:29 -070087 // Binds a buffer to the GL target corresponding to 'type', updates internal state tracking, and
88 // returns the GL target the buffer was bound to.
89 // When 'type' is kIndex_GrBufferType, this function will also implicitly bind the default VAO.
90 // If the caller wishes to bind an index buffer to a specific VAO, it can call glBind directly.
91 GrGLenum bindBuffer(GrBufferType type, const GrGLBuffer*);
joshualitt93316b92015-10-23 09:08:08 -070092
cdalton74b8d322016-04-11 14:47:28 -070093 // Called by GrGLBuffer after its buffer object has been destroyed.
94 void notifyBufferReleased(const GrGLBuffer*);
95
reedf9ad5582015-06-25 21:29:25 -070096 const GrGLContext* glContextForTesting() const override {
97 return &this->glContext();
bsalomon993a4212015-05-29 11:37:25 -070098 }
99
egdanielec00d942015-09-14 12:56:10 -0700100 void clearStencil(GrRenderTarget*) override;
101
102 void invalidateBoundRenderTarget() {
103 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
104 }
105
106 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
107 int width,
108 int height) override;
109
jvanverth88957922015-07-14 11:02:52 -0700110 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -0800111 GrPixelConfig config) override;
bsalomon67d76202015-11-11 12:40:42 -0800112 bool isTestingOnlyBackendTexture(GrBackendObject) const override;
bsalomone63ffef2016-02-05 07:17:34 -0800113 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override;
jvanverth672bb7f2015-07-13 07:19:57 -0700114
joshualitt8fd844f2015-12-02 13:36:47 -0800115 void resetShaderCacheForTesting() const override;
116
bsalomon6dea83f2015-12-03 12:58:06 -0800117 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override;
118
jvanverthd2d2eb92016-02-17 14:04:46 -0800119 void finishDrawTarget() override;
ethannicholas22793252016-01-30 09:59:10 -0800120
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000121private:
bsalomon424cc262015-05-22 10:37:30 -0700122 GrGLGpu(GrGLContext* ctx, GrContext* context);
123
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700125 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000126
bsalomoncb02b382015-08-12 11:14:50 -0700127 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
128
kkinnunen2e6055b2016-04-22 01:48:29 -0700129 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800130 const SkTArray<GrMipLevel>& texels) override;
egdanielb0e1be22015-04-22 13:27:39 -0700131 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -0700132 SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800133 const SkTArray<GrMipLevel>& texels) override;
134
cdalton1bf3e712016-04-19 10:00:02 -0700135 GrBuffer* onCreateBuffer(size_t size, GrBufferType intendedType, GrAccessPattern,
136 const void* data) override;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700137 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
138 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
139 GrWrapOwnership) override;
kkinnunen49c4c222016-04-01 04:50:37 -0700140 GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&) override;
egdanielff1d5472015-09-10 08:37:20 -0700141 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800142 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700143 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144
bsalomon7e68ab72016-04-13 14:29:25 -0700145
erikchen9a1ed5d2016-02-10 16:32:34 -0800146 // Returns whether the texture is successfully created. On success, the
147 // result is stored in |info|.
cblume55f2d2d2016-02-26 13:20:48 -0800148 // The texture is populated with |texels|, if it exists.
erikchen9a1ed5d2016-02-10 16:32:34 -0800149 // The texture parameters are cached in |initialTexParams|.
150 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800151 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
152 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800153
egdaniel51c8d402015-08-06 10:54:13 -0700154 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000155
mtklein36352bf2015-03-25 18:17:31 -0700156 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
joshualitt6db519c2014-10-29 08:48:18 -0700157
bsalomone179a912016-01-20 06:18:10 -0800158 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
159 GrTextureProducer::CopyParams*) const override;
160
bsalomon1aa20292016-01-22 08:16:09 -0800161 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
162 // render target.
163 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
164
165 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
166 // render target that has renderTargetConfig. This may have to create a temporary
167 // render target and thus is less preferable than the variant that takes a render target.
168 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
169
170 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
171 // render target that has the same config as surfaceForConfig. Calls one of the the two
172 // variations above, depending on whether the surface is a render target or not.
173 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
174
bsalomon6cb3cbe2015-07-30 07:34:27 -0700175 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800176 int left, int top,
177 int width, int height,
178 GrPixelConfig,
179 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700180 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800181
bsalomon6cb3cbe2015-07-30 07:34:27 -0700182 bool onWritePixels(GrSurface*,
183 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800184 GrPixelConfig config,
185 const SkTArray<GrMipLevel>& texels) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000186
jvanverthc3d706f2016-04-20 10:33:27 -0700187 bool onTransferPixels(GrSurface*,
jvanverth17aa0472016-01-05 10:41:27 -0800188 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700189 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800190 size_t offset, size_t rowBytes) override;
191
mtklein36352bf2015-03-25 18:17:31 -0700192 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000193
egdaniel0e1853c2016-03-17 11:35:45 -0700194 void onDraw(const GrPipeline&,
195 const GrPrimitiveProcessor&,
196 const GrMesh*,
197 int meshCount) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000198
joshualitt1cbdcde2015-08-21 11:53:29 -0700199 bool onCopySurface(GrSurface* dst,
200 GrSurface* src,
201 const SkIRect& srcRect,
202 const SkIPoint& dstPoint) override;
203
cdalton28f45b92016-03-07 13:58:26 -0800204 void onGetMultisampleSpecs(GrRenderTarget*,
205 const GrStencilSettings&,
206 int* effectiveSampleCnt,
207 SkAutoTDeleteArray<SkPoint>* sampleLocations) override;
208
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000209 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000210 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000211
cdalton74b8d322016-04-11 14:47:28 -0700212 void setTextureSwizzle(int unitIdx, GrGLenum target, const GrGLenum swizzle[]);
213
egdaniel8dd688b2015-01-22 10:16:09 -0800214 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
egdaniel0e1853c2016-03-17 11:35:45 -0700215 bool flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc);
bsalomond95263c2014-12-16 13:05:12 -0800216
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000217 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700218 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000219 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800220 void setupGeometry(const GrPrimitiveProcessor&,
egdaniel0e1853c2016-03-17 11:35:45 -0700221 const GrNonInstancedMesh& mesh,
joshualittd53a8272014-11-10 16:03:14 -0800222 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000223
bsalomon7f9b2e42016-01-12 13:29:26 -0800224 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000225
bsalomon424cc262015-05-22 10:37:30 -0700226 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000227
cdaltone2e71c22016-04-07 18:13:29 -0700228 bool copySurfaceAsDraw(GrSurface* dst,
bsalomon6df86402015-06-01 10:41:49 -0700229 GrSurface* src,
230 const SkIRect& srcRect,
231 const SkIPoint& dstPoint);
232 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
233 GrSurface* src,
234 const SkIRect& srcRect,
235 const SkIPoint& dstPoint);
236 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
237 GrSurface* src,
238 const SkIRect& srcRect,
239 const SkIPoint& dstPoint);
240
cdaltone2e71c22016-04-07 18:13:29 -0700241 void stampPLSSetupRect(const SkRect& bounds);
ethannicholas22793252016-01-30 09:59:10 -0800242
egdaniel0e1853c2016-03-17 11:35:45 -0700243 void setupPixelLocalStorage(const GrPipeline&, const GrPrimitiveProcessor&);
ethannicholas22793252016-01-30 09:59:10 -0800244
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000245 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000246
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000247 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000248 public:
bsalomon861e1032014-12-16 07:33:49 -0800249 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000250 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000251
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000252 void abandon();
egdaniel0e1853c2016-03-17 11:35:45 -0700253 GrGLProgram* refProgram(const GrGLGpu* gpu, const GrPipeline&, const GrPrimitiveProcessor&);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000254
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000255 private:
256 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000257 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
258 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000259 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000260 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000261 };
262
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000263 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000264
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000265 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000266
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000267 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
268 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700269 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000270
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000271 // sorted array of all the entries
272 Entry* fEntries[kMaxEntries];
273 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
274 // searching fEntries.
275 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000276
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000277 int fCount;
278 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800279 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000280#ifdef PROGRAM_CACHE_STATS
281 int fTotalRequests;
282 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000283 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000284#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000285 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000286
egdaniel080e6732014-12-22 07:35:52 -0800287 void flushColorWrite(bool writeColor);
egdaniel8dd688b2015-01-22 10:16:09 -0800288 void flushDrawFace(GrPipelineBuilder::DrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000289
bsalomon@google.coma3201942012-06-21 19:58:20 +0000290 // flushes the scissor. see the note on flushBoundTextureAndParams about
291 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800292 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700293 const GrGLIRect& rtViewport,
294 GrSurfaceOrigin rtOrigin);
295
296 // disables the scissor
297 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000298
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000299 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000300
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000301 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000302 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000303
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000304 // sets a texture unit to use for texture operations other than binding a texture to a program.
305 // ensures that such operations don't negatively interact with tracking bound textures.
306 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
bsalomon083617b2016-02-12 12:10:14 -0800308 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700309 // nullptr means whole target. Can be an empty rect.
brianosman64d094d2016-03-25 06:01:59 -0700310 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds, bool disableSRGB = false);
bsalomon083617b2016-02-12 12:10:14 -0800311
312 // Need not be called if flushRenderTarget is used.
313 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700314
bsalomon3e791242014-12-17 13:43:13 -0800315 void flushStencil(const GrStencilSettings&);
bsalomon083617b2016-02-12 12:10:14 -0800316
317 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800318 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000319
ethannicholas28ef4452016-03-25 09:26:03 -0700320 void flushMinSampleShading(float minSampleShading);
321
bsalomon@google.com6f379512011-11-16 20:36:03 +0000322 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800323 enum UploadType {
324 kNewTexture_UploadType, // we are creating a new texture
325 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
326 kTransfer_UploadType, // we are using a transfer buffer to copy data
327 };
bsalomonb15b4c12014-10-29 12:41:57 -0700328 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800329 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800330 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000331 int left, int top, int width, int height,
332 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800333 const SkTArray<GrMipLevel>& texels);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000334
krajcevski145d48c2014-06-11 16:07:50 -0700335 // helper for onCreateCompressedTexture. If width and height are
336 // set to -1, then this function will use desc.fWidth and desc.fHeight
337 // for the size of the data. The isNewTexture flag should be set to true
338 // whenever a new texture needs to be created. Otherwise, we assume that
339 // the texture is already in GPU memory and that it's going to be updated
340 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700341 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700342 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -0800343 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -0800344 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700345 int left = 0, int top = 0,
346 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700347
kkinnunen2e6055b2016-04-22 01:48:29 -0700348 bool createRenderTargetObjects(const GrSurfaceDesc&, const GrGLTextureInfo& texInfo,
349 GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000350
egdanield803f272015-03-18 13:01:52 -0700351 enum TempFBOTarget {
352 kSrc_TempFBOTarget,
353 kDst_TempFBOTarget
354 };
egdaniel0f5f9672015-02-03 11:10:51 -0800355
bsalomon10528f12015-10-14 12:54:52 -0700356 // Binds a surface as a FBO for a copy operation. If the surface already owns an FBO ID then
357 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
358 // This must be paired with a call to unbindSurfaceFBOForCopy().
359 void bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
egdanield803f272015-03-18 13:01:52 -0700360 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800361
bsalomon10528f12015-10-14 12:54:52 -0700362 // Must be called if bindSurfaceFBOForCopy was used to bind a surface for copying.
363 void unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700364
reedf9ad5582015-06-25 21:29:25 -0700365 SkAutoTUnref<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000366
cdaltone2e71c22016-04-07 18:13:29 -0700367 bool createCopyProgram(int progIdx);
368 bool createWireRectProgram();
369 bool createPLSSetupProgram();
ethannicholas22793252016-01-30 09:59:10 -0800370
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000371 // GL program-related state
372 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000373
374 ///////////////////////////////////////////////////////////////////////////
375 ///@name Caching of GL State
376 ///@{
377 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000378 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000379
bsalomon@google.com49209392012-06-05 15:13:46 +0000380 enum TriState {
381 kNo_TriState,
382 kYes_TriState,
383 kUnknown_TriState
384 };
385
egdanield803f272015-03-18 13:01:52 -0700386 GrGLuint fTempSrcFBOID;
387 GrGLuint fTempDstFBOID;
388
389 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800390
bsalomon@google.coma3201942012-06-21 19:58:20 +0000391 // last scissor / viewport scissor state seen by the GL.
392 struct {
393 TriState fEnabled;
394 GrGLIRect fRect;
395 void invalidate() {
396 fEnabled = kUnknown_TriState;
397 fRect.invalidate();
398 }
399 } fHWScissorSettings;
400
bsalomon424cc262015-05-22 10:37:30 -0700401 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000402
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000403 /**
cdaltone2e71c22016-04-07 18:13:29 -0700404 * Tracks vertex attrib array state.
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000405 */
cdaltone2e71c22016-04-07 18:13:29 -0700406 class HWVertexArrayState {
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000407 public:
cdaltone2e71c22016-04-07 18:13:29 -0700408 HWVertexArrayState() : fCoreProfileVertexArray(nullptr) { this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000409
cdaltone2e71c22016-04-07 18:13:29 -0700410 ~HWVertexArrayState() { delete fCoreProfileVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000411
bsalomon@google.com6918d482013-03-07 19:09:11 +0000412 void invalidate() {
413 fBoundVertexArrayIDIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000414 fDefaultVertexArrayAttribState.invalidate();
cdaltone2e71c22016-04-07 18:13:29 -0700415 if (fCoreProfileVertexArray) {
416 fCoreProfileVertexArray->invalidateCachedState();
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000417 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000418 }
419
420 void notifyVertexArrayDelete(GrGLuint id) {
421 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
422 // Does implicit bind to 0
423 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000424 }
425 }
426
bsalomon861e1032014-12-16 07:33:49 -0800427 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000428 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000429 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000430 return;
431 }
432 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
433 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
434 fBoundVertexArrayIDIsValid = true;
435 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000436 }
437 }
438
bsalomon@google.com6918d482013-03-07 19:09:11 +0000439 /**
cdaltone2e71c22016-04-07 18:13:29 -0700440 * Binds the vertex array that should be used for internal draws, and returns its attrib
441 * state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
442 * case we use a dummy array instead.
443 *
444 * If an index buffer is privided, it will be bound to the vertex array. Otherwise the
445 * index buffer binding will be left unchanged.
446 *
447 * The returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000448 */
cdaltone2e71c22016-04-07 18:13:29 -0700449 GrGLAttribArrayState* bindInternalVertexArray(GrGLGpu*, const GrGLBuffer* ibuff = nullptr);
bsalomon6df86402015-06-01 10:41:49 -0700450
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000451 private:
mtklein044d3c12016-04-06 18:24:34 -0700452 GrGLuint fBoundVertexArrayID;
mtklein044d3c12016-04-06 18:24:34 -0700453 bool fBoundVertexArrayIDIsValid;
mtklein044d3c12016-04-06 18:24:34 -0700454
bsalomon@google.com6918d482013-03-07 19:09:11 +0000455 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800456 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
457 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000458 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000459
cdaltone2e71c22016-04-07 18:13:29 -0700460 // This is used when we're using a core profile.
461 GrGLVertexArray* fCoreProfileVertexArray;
462 } fHWVertexArrayState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463
cdaltone2e71c22016-04-07 18:13:29 -0700464 struct {
465 GrGLenum fGLTarget;
466 uint32_t fBoundBufferUniqueID;
467 bool fBufferZeroKnownBound;
468
469 void invalidate() {
470 fBoundBufferUniqueID = SK_InvalidUniqueID;
471 fBufferZeroKnownBound = false;
472 }
473 } fHWBufferState[kGrBufferTypeCount];
cdaltonc1613102016-03-16 07:48:20 -0700474
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000475 struct {
cdalton8917d622015-05-06 13:40:21 -0700476 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000477 GrBlendCoeff fSrcCoeff;
478 GrBlendCoeff fDstCoeff;
479 GrColor fConstColor;
480 bool fConstColorValid;
481 TriState fEnabled;
482
483 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700484 fEquation = static_cast<GrBlendEquation>(-1);
485 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
486 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000487 fConstColorValid = false;
488 fEnabled = kUnknown_TriState;
489 }
490 } fHWBlendState;
491
bsalomon54c6fe82015-12-16 11:51:22 -0800492 TriState fMSAAEnabled;
493
494 GrStencilSettings fHWStencilSettings;
495 TriState fHWStencilTestEnabled;
496
497
498 GrPipelineBuilder::DrawFace fHWDrawFace;
499 TriState fHWWriteToColor;
500 uint32_t fHWBoundRenderTargetUniqueID;
501 TriState fHWSRGBFramebuffer;
502 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800503
cdalton74b8d322016-04-11 14:47:28 -0700504 struct BufferTexture {
505 BufferTexture() : fTextureID(0), fKnownBound(false),
506 fAttachedBufferUniqueID(SK_InvalidUniqueID),
507 fSwizzle(GrSwizzle::RGBA()) {}
508
509 GrGLuint fTextureID;
510 bool fKnownBound;
511 intptr_t fOffsetInBytes;
512 GrPixelConfig fTexelConfig;
513 size_t fAttachedSizeInBytes;
514 uint32_t fAttachedBufferUniqueID;
515 GrSwizzle fSwizzle;
516 };
517
518 SkTArray<BufferTexture, true> fHWBufferTextures;
519 int fHWMaxUsedBufferTextureUnit;
520
cdaltonaf8bc7d2016-02-05 09:35:20 -0800521 // EXT_raster_multisample.
522 TriState fHWRasterMultisampleEnabled;
523 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800524 ///@}
525
bsalomon6df86402015-06-01 10:41:49 -0700526 /** IDs for copy surface program. */
527 struct {
528 GrGLuint fProgram;
529 GrGLint fTextureUniform;
530 GrGLint fTexCoordXformUniform;
531 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800532 } fCopyPrograms[3];
cdaltone2e71c22016-04-07 18:13:29 -0700533 SkAutoTUnref<GrGLBuffer> fCopyProgramArrayBuffer;
bsalomon7ea33f52015-11-22 14:51:00 -0800534
bsalomon6dea83f2015-12-03 12:58:06 -0800535 struct {
536 GrGLuint fProgram;
537 GrGLint fColorUniform;
538 GrGLint fRectUniform;
539 } fWireRectProgram;
cdaltone2e71c22016-04-07 18:13:29 -0700540 SkAutoTUnref<GrGLBuffer> fWireRectArrayBuffer;
bsalomon6dea83f2015-12-03 12:58:06 -0800541
bsalomon7ea33f52015-11-22 14:51:00 -0800542 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800543 switch (target) {
544 case GR_GL_TEXTURE_2D:
545 return 0;
546 case GR_GL_TEXTURE_EXTERNAL:
547 return 1;
548 case GR_GL_TEXTURE_RECTANGLE:
549 return 2;
550 default:
551 SkFAIL("Unexpected texture target type.");
552 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800553 }
554 }
bsalomon6df86402015-06-01 10:41:49 -0700555
ethannicholas22793252016-01-30 09:59:10 -0800556 struct {
cdaltone2e71c22016-04-07 18:13:29 -0700557 GrGLuint fProgram;
558 GrGLint fPosXformUniform;
559 SkAutoTUnref<GrGLBuffer> fArrayBuffer;
ethannicholas22793252016-01-30 09:59:10 -0800560 } fPLSSetupProgram;
561
562 bool fHWPLSEnabled;
563 bool fPLSHasBeenUsed;
564
ethannicholas28ef4452016-03-25 09:26:03 -0700565 float fHWMinSampleShading;
566
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700568 friend class GrGLPathRendering; // For accessing setTextureUnit.
reed@google.comac10a2d2010-12-22 21:39:39 +0000569};
570
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000571#endif