blob: 09f0ae43798ac310aac3fcc7aa59fcf0374889be [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
commit-bot@chromium.org174db772013-03-21 12:46:01 +000031#ifdef SK_DEVELOPER
jvanverth@google.com94878772013-03-12 16:00:54 +000032#define PROGRAM_CACHE_STATS
33#endif
34
bsalomon861e1032014-12-16 07:33:49 -080035class GrGLGpu : 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
mtklein36352bf2015-03-25 18:17:31 -070041 void contextAbandoned() 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.
brianosmana6359362016-03-21 06:55:37 -070060 void bindTexture(int unitIdx, const GrTextureParams& params, bool dstConfigAllowsSRGB,
61 GrGLTexture* texture);
kkinnunenccdaa042014-08-20 01:36:23 -070062
bsalomonf0674512015-07-28 13:26:15 -070063 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
64 GrPixelConfig readConfig, DrawPreference*,
65 ReadPixelTempDrawInfo*) override;
bsalomon39826022015-07-23 08:07:21 -070066
bsalomonf0674512015-07-28 13:26:15 -070067 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -080068 GrPixelConfig srcConfig, DrawPreference*,
bsalomonf0674512015-07-28 13:26:15 -070069 WritePixelTempDrawInfo*) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000070
joshualitt1c735482015-07-13 08:08:25 -070071 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override;
bsalomon@google.comeb851172013-04-15 13:51:00 +000072
bsalomon@google.com6918d482013-03-07 19:09:11 +000073 // 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 +000074 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000075 void bindVertexArray(GrGLuint id) {
76 fHWGeometryState.setVertexArrayID(this, id);
77 }
78 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
79 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
80 }
81 void bindVertexBuffer(GrGLuint id) {
82 fHWGeometryState.setVertexBufferID(this, id);
83 }
84
85 // These callbacks update state tracking when GL objects are deleted. They are called from
86 // GrGLResource onRelease functions.
87 void notifyVertexArrayDelete(GrGLuint id) {
88 fHWGeometryState.notifyVertexArrayDelete(id);
89 }
90 void notifyVertexBufferDelete(GrGLuint id) {
91 fHWGeometryState.notifyVertexBufferDelete(id);
92 }
93 void notifyIndexBufferDelete(GrGLuint id) {
94 fHWGeometryState.notifyIndexBufferDelete(id);
95 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000096
cdaltonc1613102016-03-16 07:48:20 -070097 // id and type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, etc.) of buffer to bind
joshualitt93316b92015-10-23 09:08:08 -070098 void bindBuffer(GrGLuint id, GrGLenum type);
99
100 void releaseBuffer(GrGLuint id, GrGLenum type);
101
reedf9ad5582015-06-25 21:29:25 -0700102 const GrGLContext* glContextForTesting() const override {
103 return &this->glContext();
bsalomon993a4212015-05-29 11:37:25 -0700104 }
105
egdanielec00d942015-09-14 12:56:10 -0700106 void clearStencil(GrRenderTarget*) override;
107
108 void invalidateBoundRenderTarget() {
109 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
110 }
111
112 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
113 int width,
114 int height) override;
115
jvanverth88957922015-07-14 11:02:52 -0700116 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -0800117 GrPixelConfig config) override;
bsalomon67d76202015-11-11 12:40:42 -0800118 bool isTestingOnlyBackendTexture(GrBackendObject) const override;
bsalomone63ffef2016-02-05 07:17:34 -0800119 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override;
jvanverth672bb7f2015-07-13 07:19:57 -0700120
joshualitt8fd844f2015-12-02 13:36:47 -0800121 void resetShaderCacheForTesting() const override;
122
bsalomon6dea83f2015-12-03 12:58:06 -0800123 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override;
124
jvanverthd2d2eb92016-02-17 14:04:46 -0800125 void finishDrawTarget() override;
ethannicholas22793252016-01-30 09:59:10 -0800126
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000127private:
bsalomon424cc262015-05-22 10:37:30 -0700128 GrGLGpu(GrGLContext* ctx, GrContext* context);
129
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000130 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700131 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000132
bsalomoncb02b382015-08-12 11:14:50 -0700133 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
134
egdanielb0e1be22015-04-22 13:27:39 -0700135 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800136 const SkTArray<GrMipLevel>& texels) override;
egdanielb0e1be22015-04-22 13:27:39 -0700137 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
138 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800139 const SkTArray<GrMipLevel>& texels) override;
140
cdalton397536c2016-03-25 12:15:03 -0700141 GrBuffer* onCreateBuffer(GrBufferType, size_t size, GrAccessPattern) override;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700142 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
143 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
144 GrWrapOwnership) override;
ericrkf7b8b8a2016-02-24 14:49:51 -0800145 GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&,
146 GrWrapOwnership) override;
egdanielff1d5472015-09-10 08:37:20 -0700147 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800148 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700149 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000150
erikchen9a1ed5d2016-02-10 16:32:34 -0800151 // If |desc.fTextureStorageAllocator| exists, use that to create the
152 // texture. Otherwise, create the texture directly.
153 // Returns whether the texture is successfully created. On success, the
154 // result is stored in |info|.
cblume55f2d2d2016-02-26 13:20:48 -0800155 // The texture is populated with |texels|, if it exists.
erikchen9a1ed5d2016-02-10 16:32:34 -0800156 // The texture parameters are cached in |initialTexParams|.
157 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800158 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
159 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800160 bool createTextureExternalAllocatorImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800161 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800162
egdaniel51c8d402015-08-06 10:54:13 -0700163 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
joshualitt6db519c2014-10-29 08:48:18 -0700166
bsalomone179a912016-01-20 06:18:10 -0800167 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
168 GrTextureProducer::CopyParams*) const override;
169
bsalomon1aa20292016-01-22 08:16:09 -0800170 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
171 // render target.
172 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
173
174 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
175 // render target that has renderTargetConfig. This may have to create a temporary
176 // render target and thus is less preferable than the variant that takes a render target.
177 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
178
179 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
180 // render target that has the same config as surfaceForConfig. Calls one of the the two
181 // variations above, depending on whether the surface is a render target or not.
182 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
183
bsalomon6cb3cbe2015-07-30 07:34:27 -0700184 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800185 int left, int top,
186 int width, int height,
187 GrPixelConfig,
188 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700189 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800190
bsalomon6cb3cbe2015-07-30 07:34:27 -0700191 bool onWritePixels(GrSurface*,
192 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800193 GrPixelConfig config,
194 const SkTArray<GrMipLevel>& texels) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000195
jvanverth17aa0472016-01-05 10:41:27 -0800196 bool onTransferPixels(GrSurface*,
197 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700198 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800199 size_t offset, size_t rowBytes) override;
200
mtklein36352bf2015-03-25 18:17:31 -0700201 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000202
egdaniel0e1853c2016-03-17 11:35:45 -0700203 void onDraw(const GrPipeline&,
204 const GrPrimitiveProcessor&,
205 const GrMesh*,
206 int meshCount) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000207
joshualitt1cbdcde2015-08-21 11:53:29 -0700208 bool onCopySurface(GrSurface* dst,
209 GrSurface* src,
210 const SkIRect& srcRect,
211 const SkIPoint& dstPoint) override;
212
cdalton28f45b92016-03-07 13:58:26 -0800213 void onGetMultisampleSpecs(GrRenderTarget*,
214 const GrStencilSettings&,
215 int* effectiveSampleCnt,
216 SkAutoTDeleteArray<SkPoint>* sampleLocations) override;
217
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000218 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000219 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
egdaniel8dd688b2015-01-22 10:16:09 -0800221 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
egdaniel0e1853c2016-03-17 11:35:45 -0700222 bool flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc);
bsalomond95263c2014-12-16 13:05:12 -0800223
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000224 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700225 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000226 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800227 void setupGeometry(const GrPrimitiveProcessor&,
egdaniel0e1853c2016-03-17 11:35:45 -0700228 const GrNonInstancedMesh& mesh,
joshualittd53a8272014-11-10 16:03:14 -0800229 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000230
bsalomon7f9b2e42016-01-12 13:29:26 -0800231 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000232
bsalomon424cc262015-05-22 10:37:30 -0700233 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000234
bsalomon6df86402015-06-01 10:41:49 -0700235 void copySurfaceAsDraw(GrSurface* dst,
236 GrSurface* src,
237 const SkIRect& srcRect,
238 const SkIPoint& dstPoint);
239 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
240 GrSurface* src,
241 const SkIRect& srcRect,
242 const SkIPoint& dstPoint);
243 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
244 GrSurface* src,
245 const SkIRect& srcRect,
246 const SkIPoint& dstPoint);
247
ethannicholas22793252016-01-30 09:59:10 -0800248 void stampRectUsingProgram(GrGLuint program, const SkRect& bounds, GrGLint posXformUniform,
249 GrGLuint arrayBuffer);
250
egdaniel0e1853c2016-03-17 11:35:45 -0700251 void setupPixelLocalStorage(const GrPipeline&, const GrPrimitiveProcessor&);
ethannicholas22793252016-01-30 09:59:10 -0800252
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000253 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000254
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000255 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000256 public:
bsalomon861e1032014-12-16 07:33:49 -0800257 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000258 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000259
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000260 void abandon();
egdaniel0e1853c2016-03-17 11:35:45 -0700261 GrGLProgram* refProgram(const GrGLGpu* gpu, const GrPipeline&, const GrPrimitiveProcessor&);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000262
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000263 private:
264 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000265 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
266 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000267 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000268 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000269 };
270
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000271 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000272
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000273 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000274
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000275 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
276 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700277 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000278
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000279 // sorted array of all the entries
280 Entry* fEntries[kMaxEntries];
281 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
282 // searching fEntries.
283 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000284
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000285 int fCount;
286 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800287 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000288#ifdef PROGRAM_CACHE_STATS
289 int fTotalRequests;
290 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000291 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000292#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000293 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000294
egdaniel080e6732014-12-22 07:35:52 -0800295 void flushColorWrite(bool writeColor);
egdaniel8dd688b2015-01-22 10:16:09 -0800296 void flushDrawFace(GrPipelineBuilder::DrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000297
bsalomon@google.coma3201942012-06-21 19:58:20 +0000298 // flushes the scissor. see the note on flushBoundTextureAndParams about
299 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800300 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700301 const GrGLIRect& rtViewport,
302 GrSurfaceOrigin rtOrigin);
303
304 // disables the scissor
305 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000306
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000307 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000308
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000309 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000310 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000311
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000312 // sets a texture unit to use for texture operations other than binding a texture to a program.
313 // ensures that such operations don't negatively interact with tracking bound textures.
314 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon083617b2016-02-12 12:10:14 -0800316 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700317 // nullptr means whole target. Can be an empty rect.
brianosman64d094d2016-03-25 06:01:59 -0700318 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds, bool disableSRGB = false);
bsalomon083617b2016-02-12 12:10:14 -0800319 // Handles cases where a surface will be updated without a call to flushRenderTarget
320 void didWriteToSurface(GrSurface*, const SkIRect* bounds) const;
321
322 // Need not be called if flushRenderTarget is used.
323 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700324
bsalomon3e791242014-12-17 13:43:13 -0800325 void flushStencil(const GrStencilSettings&);
bsalomon083617b2016-02-12 12:10:14 -0800326
327 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800328 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000329
ethannicholas28ef4452016-03-25 09:26:03 -0700330 void flushMinSampleShading(float minSampleShading);
331
bsalomon@google.com6f379512011-11-16 20:36:03 +0000332 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800333 enum UploadType {
334 kNewTexture_UploadType, // we are creating a new texture
335 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
336 kTransfer_UploadType, // we are using a transfer buffer to copy data
337 };
bsalomonb15b4c12014-10-29 12:41:57 -0700338 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800339 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800340 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000341 int left, int top, int width, int height,
342 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800343 const SkTArray<GrMipLevel>& texels);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000344
krajcevski145d48c2014-06-11 16:07:50 -0700345 // helper for onCreateCompressedTexture. If width and height are
346 // set to -1, then this function will use desc.fWidth and desc.fHeight
347 // for the size of the data. The isNewTexture flag should be set to true
348 // whenever a new texture needs to be created. Otherwise, we assume that
349 // the texture is already in GPU memory and that it's going to be updated
350 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700351 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700352 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -0800353 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -0800354 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700355 int left = 0, int top = 0,
356 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700357
egdanielb0e1be22015-04-22 13:27:39 -0700358 bool createRenderTargetObjects(const GrSurfaceDesc&, GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800359 const GrGLTextureInfo& texInfo, GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000360
egdanield803f272015-03-18 13:01:52 -0700361 enum TempFBOTarget {
362 kSrc_TempFBOTarget,
363 kDst_TempFBOTarget
364 };
egdaniel0f5f9672015-02-03 11:10:51 -0800365
bsalomon10528f12015-10-14 12:54:52 -0700366 // Binds a surface as a FBO for a copy operation. If the surface already owns an FBO ID then
367 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
368 // This must be paired with a call to unbindSurfaceFBOForCopy().
369 void bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
egdanield803f272015-03-18 13:01:52 -0700370 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800371
bsalomon10528f12015-10-14 12:54:52 -0700372 // Must be called if bindSurfaceFBOForCopy was used to bind a surface for copying.
373 void unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700374
reedf9ad5582015-06-25 21:29:25 -0700375 SkAutoTUnref<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000376
bsalomon7ea33f52015-11-22 14:51:00 -0800377 void createCopyPrograms();
bsalomon6dea83f2015-12-03 12:58:06 -0800378 void createWireRectProgram();
379 void createUnitRectBuffer();
bsalomon6df86402015-06-01 10:41:49 -0700380
ethannicholas22793252016-01-30 09:59:10 -0800381 void createPLSSetupProgram();
382
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000383 // GL program-related state
384 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000385
386 ///////////////////////////////////////////////////////////////////////////
387 ///@name Caching of GL State
388 ///@{
389 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000390 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000391
bsalomon@google.com49209392012-06-05 15:13:46 +0000392 enum TriState {
393 kNo_TriState,
394 kYes_TriState,
395 kUnknown_TriState
396 };
397
egdanield803f272015-03-18 13:01:52 -0700398 GrGLuint fTempSrcFBOID;
399 GrGLuint fTempDstFBOID;
400
401 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800402
bsalomon@google.coma3201942012-06-21 19:58:20 +0000403 // last scissor / viewport scissor state seen by the GL.
404 struct {
405 TriState fEnabled;
406 GrGLIRect fRect;
407 void invalidate() {
408 fEnabled = kUnknown_TriState;
409 fRect.invalidate();
410 }
411 } fHWScissorSettings;
412
bsalomon424cc262015-05-22 10:37:30 -0700413 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000414
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000415 /**
416 * Tracks bound vertex and index buffers and vertex attrib array state.
417 */
418 class HWGeometryState {
419 public:
halcanary96fcdcc2015-08-27 07:41:13 -0700420 HWGeometryState() { fVBOVertexArray = nullptr; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000421
halcanary385fe4d2015-08-26 13:07:48 -0700422 ~HWGeometryState() { delete fVBOVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000423
bsalomon@google.com6918d482013-03-07 19:09:11 +0000424 void invalidate() {
425 fBoundVertexArrayIDIsValid = false;
426 fBoundVertexBufferIDIsValid = false;
427 fDefaultVertexArrayBoundIndexBufferID = false;
428 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
429 fDefaultVertexArrayAttribState.invalidate();
bsalomon49f085d2014-09-05 13:34:00 -0700430 if (fVBOVertexArray) {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000431 fVBOVertexArray->invalidateCachedState();
432 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000433 }
434
435 void notifyVertexArrayDelete(GrGLuint id) {
436 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
437 // Does implicit bind to 0
438 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000439 }
440 }
441
bsalomon861e1032014-12-16 07:33:49 -0800442 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000443 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000444 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000445 return;
446 }
447 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
448 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
449 fBoundVertexArrayIDIsValid = true;
450 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000451 }
452 }
453
bsalomon@google.come49ad452013-02-20 19:33:20 +0000454 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000455 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
456 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000457 }
bsalomon49f085d2014-09-05 13:34:00 -0700458 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000459 fVBOVertexArray->notifyVertexBufferDelete(id);
460 }
461 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000462 }
463
bsalomon@google.come49ad452013-02-20 19:33:20 +0000464 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000465 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
466 id == fDefaultVertexArrayBoundIndexBufferID) {
467 fDefaultVertexArrayBoundIndexBufferID = 0;
468 }
bsalomon49f085d2014-09-05 13:34:00 -0700469 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000470 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000471 }
472 }
473
bsalomon861e1032014-12-16 07:33:49 -0800474 void setVertexBufferID(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000475 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
476 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
477 fBoundVertexBufferIDIsValid = true;
478 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000479 }
480 }
481
bsalomon@google.com6918d482013-03-07 19:09:11 +0000482 /**
483 * Binds the default vertex array and binds the index buffer. This is used when binding
484 * an index buffer in order to update it.
485 */
bsalomon861e1032014-12-16 07:33:49 -0800486 void setIndexBufferIDOnDefaultVertexArray(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000487 this->setVertexArrayID(gpu, 0);
488 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
489 id != fDefaultVertexArrayBoundIndexBufferID) {
490 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
491 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
492 fDefaultVertexArrayBoundIndexBufferID = id;
493 }
494 }
495
496 /**
497 * Binds the vertex array object that should be used to render from the vertex buffer.
498 * The vertex array is bound and its attrib array state object is returned. The vertex
halcanary96fcdcc2015-08-27 07:41:13 -0700499 * buffer is bound. The index buffer (if non-nullptr) is bound to the vertex array. The
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000500 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000501 */
bsalomon861e1032014-12-16 07:33:49 -0800502 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
cdalton397536c2016-03-25 12:15:03 -0700503 const GrGLBuffer* vbuffer,
504 const GrGLBuffer* ibuffer);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000505
bsalomon6df86402015-06-01 10:41:49 -0700506 /** Variants of the above that takes GL buffer IDs. Note that 0 does not imply that a
507 buffer won't be bound. The "default buffer" will be bound, which is used for client-side
508 array rendering. */
509 GrGLAttribArrayState* bindArrayAndBufferToDraw(GrGLGpu* gpu, GrGLuint vbufferID);
510 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
511 GrGLuint vbufferID,
512 GrGLuint ibufferID);
513
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000514 private:
bsalomon6df86402015-06-01 10:41:49 -0700515 GrGLAttribArrayState* internalBind(GrGLGpu* gpu, GrGLuint vbufferID, GrGLuint* ibufferID);
516
bsalomon@google.com6918d482013-03-07 19:09:11 +0000517 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000518 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000519 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000520 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000521
bsalomon@google.com6918d482013-03-07 19:09:11 +0000522 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
523 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
524 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800525 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
526 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000527 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000528
bsalomon@google.com6918d482013-03-07 19:09:11 +0000529 // This is used when we're using a core profile and the vertices are in a VBO.
530 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000531 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000532
cdaltonc1613102016-03-16 07:48:20 -0700533 GrGLuint fHWBoundTextureBufferID;
534 GrGLuint fHWBoundDrawIndirectBufferID;
535 bool fHWBoundTextureBufferIDIsValid;
536 bool fHWBoundDrawIndirectBufferIDIsValid;
537
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000538 struct {
cdalton8917d622015-05-06 13:40:21 -0700539 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000540 GrBlendCoeff fSrcCoeff;
541 GrBlendCoeff fDstCoeff;
542 GrColor fConstColor;
543 bool fConstColorValid;
544 TriState fEnabled;
545
546 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700547 fEquation = static_cast<GrBlendEquation>(-1);
548 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
549 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000550 fConstColorValid = false;
551 fEnabled = kUnknown_TriState;
552 }
553 } fHWBlendState;
554
bsalomon54c6fe82015-12-16 11:51:22 -0800555 TriState fMSAAEnabled;
556
557 GrStencilSettings fHWStencilSettings;
558 TriState fHWStencilTestEnabled;
559
560
561 GrPipelineBuilder::DrawFace fHWDrawFace;
562 TriState fHWWriteToColor;
563 uint32_t fHWBoundRenderTargetUniqueID;
564 TriState fHWSRGBFramebuffer;
565 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800566
567 // EXT_raster_multisample.
568 TriState fHWRasterMultisampleEnabled;
569 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800570 ///@}
571
bsalomon6df86402015-06-01 10:41:49 -0700572 /** IDs for copy surface program. */
573 struct {
574 GrGLuint fProgram;
575 GrGLint fTextureUniform;
576 GrGLint fTexCoordXformUniform;
577 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800578 } fCopyPrograms[3];
bsalomon7ea33f52015-11-22 14:51:00 -0800579 GrGLuint fCopyProgramArrayBuffer;
580
bsalomon6dea83f2015-12-03 12:58:06 -0800581 struct {
582 GrGLuint fProgram;
583 GrGLint fColorUniform;
584 GrGLint fRectUniform;
585 } fWireRectProgram;
586 GrGLuint fWireRectArrayBuffer;
587
bsalomon7ea33f52015-11-22 14:51:00 -0800588 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800589 switch (target) {
590 case GR_GL_TEXTURE_2D:
591 return 0;
592 case GR_GL_TEXTURE_EXTERNAL:
593 return 1;
594 case GR_GL_TEXTURE_RECTANGLE:
595 return 2;
596 default:
597 SkFAIL("Unexpected texture target type.");
598 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800599 }
600 }
bsalomon6df86402015-06-01 10:41:49 -0700601
ethannicholas22793252016-01-30 09:59:10 -0800602 struct {
603 GrGLuint fProgram;
604 GrGLint fPosXformUniform;
605 GrGLuint fArrayBuffer;
606 } fPLSSetupProgram;
607
608 bool fHWPLSEnabled;
609 bool fPLSHasBeenUsed;
610
ethannicholas28ef4452016-03-25 09:26:03 -0700611 float fHWMinSampleShading;
612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700614 friend class GrGLPathRendering; // For accessing setTextureUnit.
reed@google.comac10a2d2010-12-22 21:39:39 +0000615};
616
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000617#endif