blob: c527b5390910ab67df6b2f3c8f20090aa6a7251d [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"
robertphillipsf8c3ba42016-03-25 04:55:58 -070013#include "GrGLIndexBuffer.h"
kkinnunenccdaa042014-08-20 01:36:23 -070014#include "GrGLPathRendering.h"
bsalomon@google.com5739d2c2012-05-31 15:07:19 +000015#include "GrGLProgram.h"
bsalomon37dd3312014-11-03 08:47:23 -080016#include "GrGLRenderTarget.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070017#include "GrGLStencilAttachment.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#include "GrGLTexture.h"
robertphillipsf8c3ba42016-03-25 04:55:58 -070019#include "GrGLTransferBuffer.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000020#include "GrGLVertexArray.h"
robertphillipsf8c3ba42016-03-25 04:55:58 -070021#include "GrGLVertexBuffer.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000022#include "GrGpu.h"
egdaniel8dd688b2015-01-22 10:16:09 -080023#include "GrPipelineBuilder.h"
cblume55f2d2d2016-02-26 13:20:48 -080024#include "GrTypes.h"
egdaniel080e6732014-12-22 07:35:52 -080025#include "GrXferProcessor.h"
cblume55f2d2d2016-02-26 13:20:48 -080026#include "SkTArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000027#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000028
egdaniel8dd688b2015-01-22 10:16:09 -080029class GrPipeline;
egdaniel0e1853c2016-03-17 11:35:45 -070030class GrNonInstancedMesh;
bsalomon7f9b2e42016-01-12 13:29:26 -080031class GrSwizzle;
egdaniel8dd688b2015-01-22 10:16:09 -080032
commit-bot@chromium.org174db772013-03-21 12:46:01 +000033#ifdef SK_DEVELOPER
jvanverth@google.com94878772013-03-12 16:00:54 +000034#define PROGRAM_CACHE_STATS
35#endif
36
bsalomon861e1032014-12-16 07:33:49 -080037class GrGLGpu : 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
mtklein36352bf2015-03-25 18:17:31 -070043 void contextAbandoned() 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
mtklein36352bf2015-03-25 18:17:31 -070059 void discard(GrRenderTarget*) override;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000060
kkinnunencfe62e32015-07-01 02:58:50 -070061 // Used by GrGLProgram to configure OpenGL state.
brianosmana6359362016-03-21 06:55:37 -070062 void bindTexture(int unitIdx, const GrTextureParams& params, bool dstConfigAllowsSRGB,
63 GrGLTexture* texture);
kkinnunenccdaa042014-08-20 01:36:23 -070064
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) {
78 fHWGeometryState.setVertexArrayID(this, id);
79 }
80 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
81 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
82 }
83 void bindVertexBuffer(GrGLuint id) {
84 fHWGeometryState.setVertexBufferID(this, id);
85 }
86
87 // These callbacks update state tracking when GL objects are deleted. They are called from
88 // GrGLResource onRelease functions.
89 void notifyVertexArrayDelete(GrGLuint id) {
90 fHWGeometryState.notifyVertexArrayDelete(id);
91 }
92 void notifyVertexBufferDelete(GrGLuint id) {
93 fHWGeometryState.notifyVertexBufferDelete(id);
94 }
95 void notifyIndexBufferDelete(GrGLuint id) {
96 fHWGeometryState.notifyIndexBufferDelete(id);
97 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000098
cdaltonc1613102016-03-16 07:48:20 -070099 // id and type (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, etc.) of buffer to bind
joshualitt93316b92015-10-23 09:08:08 -0700100 void bindBuffer(GrGLuint id, GrGLenum type);
101
102 void releaseBuffer(GrGLuint id, GrGLenum type);
103
robertphillipsf8c3ba42016-03-25 04:55:58 -0700104 // sizes are in bytes
105 void* mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, size_t currentSize,
106 size_t requestedSize);
107
108 void unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr);
109
110 void bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, size_t currentSize,
111 const void* src, size_t srcSizeInBytes);
112
reedf9ad5582015-06-25 21:29:25 -0700113 const GrGLContext* glContextForTesting() const override {
114 return &this->glContext();
bsalomon993a4212015-05-29 11:37:25 -0700115 }
116
egdanielec00d942015-09-14 12:56:10 -0700117 void clearStencil(GrRenderTarget*) override;
118
119 void invalidateBoundRenderTarget() {
120 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
121 }
122
123 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
124 int width,
125 int height) override;
126
jvanverth88957922015-07-14 11:02:52 -0700127 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -0800128 GrPixelConfig config) override;
bsalomon67d76202015-11-11 12:40:42 -0800129 bool isTestingOnlyBackendTexture(GrBackendObject) const override;
bsalomone63ffef2016-02-05 07:17:34 -0800130 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override;
jvanverth672bb7f2015-07-13 07:19:57 -0700131
joshualitt8fd844f2015-12-02 13:36:47 -0800132 void resetShaderCacheForTesting() const override;
133
bsalomon6dea83f2015-12-03 12:58:06 -0800134 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override;
135
jvanverthd2d2eb92016-02-17 14:04:46 -0800136 void finishDrawTarget() override;
ethannicholas22793252016-01-30 09:59:10 -0800137
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000138private:
bsalomon424cc262015-05-22 10:37:30 -0700139 GrGLGpu(GrGLContext* ctx, GrContext* context);
140
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700142 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000143
bsalomoncb02b382015-08-12 11:14:50 -0700144 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
145
egdanielb0e1be22015-04-22 13:27:39 -0700146 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800147 const SkTArray<GrMipLevel>& texels) override;
egdanielb0e1be22015-04-22 13:27:39 -0700148 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
149 GrGpuResource::LifeCycle lifeCycle,
cblume55f2d2d2016-02-26 13:20:48 -0800150 const SkTArray<GrMipLevel>& texels) override;
151
robertphillipsf8c3ba42016-03-25 04:55:58 -0700152 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override;
153 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override;
154 GrTransferBuffer* onCreateTransferBuffer(size_t size, TransferType type) override;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700155 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
156 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
157 GrWrapOwnership) override;
ericrkf7b8b8a2016-02-24 14:49:51 -0800158 GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&,
159 GrWrapOwnership) override;
egdanielff1d5472015-09-10 08:37:20 -0700160 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800161 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700162 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000163
erikchen9a1ed5d2016-02-10 16:32:34 -0800164 // If |desc.fTextureStorageAllocator| exists, use that to create the
165 // texture. Otherwise, create the texture directly.
166 // Returns whether the texture is successfully created. On success, the
167 // result is stored in |info|.
cblume55f2d2d2016-02-26 13:20:48 -0800168 // The texture is populated with |texels|, if it exists.
erikchen9a1ed5d2016-02-10 16:32:34 -0800169 // The texture parameters are cached in |initialTexParams|.
170 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800171 bool renderTarget, GrGLTexture::TexParams* initialTexParams,
172 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800173 bool createTextureExternalAllocatorImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
cblume55f2d2d2016-02-26 13:20:48 -0800174 const SkTArray<GrMipLevel>& texels);
erikchen9a1ed5d2016-02-10 16:32:34 -0800175
egdaniel51c8d402015-08-06 10:54:13 -0700176 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177
mtklein36352bf2015-03-25 18:17:31 -0700178 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
joshualitt6db519c2014-10-29 08:48:18 -0700179
bsalomone179a912016-01-20 06:18:10 -0800180 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
181 GrTextureProducer::CopyParams*) const override;
182
bsalomon1aa20292016-01-22 08:16:09 -0800183 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
184 // render target.
185 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
186
187 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
188 // render target that has renderTargetConfig. This may have to create a temporary
189 // render target and thus is less preferable than the variant that takes a render target.
190 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
191
192 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
193 // render target that has the same config as surfaceForConfig. Calls one of the the two
194 // variations above, depending on whether the surface is a render target or not.
195 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
196
bsalomon6cb3cbe2015-07-30 07:34:27 -0700197 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800198 int left, int top,
199 int width, int height,
200 GrPixelConfig,
201 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700202 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800203
bsalomon6cb3cbe2015-07-30 07:34:27 -0700204 bool onWritePixels(GrSurface*,
205 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800206 GrPixelConfig config,
207 const SkTArray<GrMipLevel>& texels) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208
jvanverth17aa0472016-01-05 10:41:27 -0800209 bool onTransferPixels(GrSurface*,
210 int left, int top, int width, int height,
robertphillipsf8c3ba42016-03-25 04:55:58 -0700211 GrPixelConfig config, GrTransferBuffer* buffer,
jvanverth17aa0472016-01-05 10:41:27 -0800212 size_t offset, size_t rowBytes) override;
213
mtklein36352bf2015-03-25 18:17:31 -0700214 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000215
egdaniel0e1853c2016-03-17 11:35:45 -0700216 void onDraw(const GrPipeline&,
217 const GrPrimitiveProcessor&,
218 const GrMesh*,
219 int meshCount) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000220
joshualitt1cbdcde2015-08-21 11:53:29 -0700221 bool onCopySurface(GrSurface* dst,
222 GrSurface* src,
223 const SkIRect& srcRect,
224 const SkIPoint& dstPoint) override;
225
cdalton28f45b92016-03-07 13:58:26 -0800226 void onGetMultisampleSpecs(GrRenderTarget*,
227 const GrStencilSettings&,
228 int* effectiveSampleCnt,
229 SkAutoTDeleteArray<SkPoint>* sampleLocations) override;
230
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
egdaniel8dd688b2015-01-22 10:16:09 -0800234 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
egdaniel0e1853c2016-03-17 11:35:45 -0700235 bool flushGLState(const GrPipeline& pipeline, const GrPrimitiveProcessor& primProc);
bsalomond95263c2014-12-16 13:05:12 -0800236
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000237 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700238 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000239 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800240 void setupGeometry(const GrPrimitiveProcessor&,
egdaniel0e1853c2016-03-17 11:35:45 -0700241 const GrNonInstancedMesh& mesh,
joshualittd53a8272014-11-10 16:03:14 -0800242 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000243
bsalomon7f9b2e42016-01-12 13:29:26 -0800244 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000245
bsalomon424cc262015-05-22 10:37:30 -0700246 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000247
bsalomon6df86402015-06-01 10:41:49 -0700248 void copySurfaceAsDraw(GrSurface* dst,
249 GrSurface* src,
250 const SkIRect& srcRect,
251 const SkIPoint& dstPoint);
252 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
253 GrSurface* src,
254 const SkIRect& srcRect,
255 const SkIPoint& dstPoint);
256 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
257 GrSurface* src,
258 const SkIRect& srcRect,
259 const SkIPoint& dstPoint);
260
ethannicholas22793252016-01-30 09:59:10 -0800261 void stampRectUsingProgram(GrGLuint program, const SkRect& bounds, GrGLint posXformUniform,
262 GrGLuint arrayBuffer);
263
egdaniel0e1853c2016-03-17 11:35:45 -0700264 void setupPixelLocalStorage(const GrPipeline&, const GrPrimitiveProcessor&);
ethannicholas22793252016-01-30 09:59:10 -0800265
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000266 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000267
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000268 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000269 public:
bsalomon861e1032014-12-16 07:33:49 -0800270 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000271 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000272
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000273 void abandon();
egdaniel0e1853c2016-03-17 11:35:45 -0700274 GrGLProgram* refProgram(const GrGLGpu* gpu, const GrPipeline&, const GrPrimitiveProcessor&);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000275
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000276 private:
277 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000278 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
279 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000280 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000281 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000282 };
283
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000284 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000285
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000286 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000287
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000288 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
289 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700290 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000291
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000292 // sorted array of all the entries
293 Entry* fEntries[kMaxEntries];
294 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
295 // searching fEntries.
296 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000297
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000298 int fCount;
299 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800300 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000301#ifdef PROGRAM_CACHE_STATS
302 int fTotalRequests;
303 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000304 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000305#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000306 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000307
egdaniel080e6732014-12-22 07:35:52 -0800308 void flushColorWrite(bool writeColor);
egdaniel8dd688b2015-01-22 10:16:09 -0800309 void flushDrawFace(GrPipelineBuilder::DrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000310
bsalomon@google.coma3201942012-06-21 19:58:20 +0000311 // flushes the scissor. see the note on flushBoundTextureAndParams about
312 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800313 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700314 const GrGLIRect& rtViewport,
315 GrSurfaceOrigin rtOrigin);
316
317 // disables the scissor
318 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000319
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000320 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000321
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000322 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000323 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000324
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000325 // sets a texture unit to use for texture operations other than binding a texture to a program.
326 // ensures that such operations don't negatively interact with tracking bound textures.
327 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomon083617b2016-02-12 12:10:14 -0800329 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700330 // nullptr means whole target. Can be an empty rect.
brianosman64d094d2016-03-25 06:01:59 -0700331 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds, bool disableSRGB = false);
bsalomon083617b2016-02-12 12:10:14 -0800332 // Handles cases where a surface will be updated without a call to flushRenderTarget
333 void didWriteToSurface(GrSurface*, const SkIRect* bounds) const;
334
335 // Need not be called if flushRenderTarget is used.
336 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700337
bsalomon3e791242014-12-17 13:43:13 -0800338 void flushStencil(const GrStencilSettings&);
bsalomon083617b2016-02-12 12:10:14 -0800339
340 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800341 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000342
ethannicholas28ef4452016-03-25 09:26:03 -0700343 void flushMinSampleShading(float minSampleShading);
344
bsalomon@google.com6f379512011-11-16 20:36:03 +0000345 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800346 enum UploadType {
347 kNewTexture_UploadType, // we are creating a new texture
348 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
349 kTransfer_UploadType, // we are using a transfer buffer to copy data
350 };
bsalomonb15b4c12014-10-29 12:41:57 -0700351 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800352 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800353 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000354 int left, int top, int width, int height,
355 GrPixelConfig dataConfig,
cblume55f2d2d2016-02-26 13:20:48 -0800356 const SkTArray<GrMipLevel>& texels);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000357
krajcevski145d48c2014-06-11 16:07:50 -0700358 // helper for onCreateCompressedTexture. If width and height are
359 // set to -1, then this function will use desc.fWidth and desc.fHeight
360 // for the size of the data. The isNewTexture flag should be set to true
361 // whenever a new texture needs to be created. Otherwise, we assume that
362 // the texture is already in GPU memory and that it's going to be updated
363 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700364 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700365 GrGLenum target,
cblume55f2d2d2016-02-26 13:20:48 -0800366 const SkTArray<GrMipLevel>& texels,
jvanverth17aa0472016-01-05 10:41:27 -0800367 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700368 int left = 0, int top = 0,
369 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700370
egdanielb0e1be22015-04-22 13:27:39 -0700371 bool createRenderTargetObjects(const GrSurfaceDesc&, GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800372 const GrGLTextureInfo& texInfo, GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000373
egdanield803f272015-03-18 13:01:52 -0700374 enum TempFBOTarget {
375 kSrc_TempFBOTarget,
376 kDst_TempFBOTarget
377 };
egdaniel0f5f9672015-02-03 11:10:51 -0800378
bsalomon10528f12015-10-14 12:54:52 -0700379 // Binds a surface as a FBO for a copy operation. If the surface already owns an FBO ID then
380 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
381 // This must be paired with a call to unbindSurfaceFBOForCopy().
382 void bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
egdanield803f272015-03-18 13:01:52 -0700383 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800384
bsalomon10528f12015-10-14 12:54:52 -0700385 // Must be called if bindSurfaceFBOForCopy was used to bind a surface for copying.
386 void unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700387
reedf9ad5582015-06-25 21:29:25 -0700388 SkAutoTUnref<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000389
bsalomon7ea33f52015-11-22 14:51:00 -0800390 void createCopyPrograms();
bsalomon6dea83f2015-12-03 12:58:06 -0800391 void createWireRectProgram();
392 void createUnitRectBuffer();
bsalomon6df86402015-06-01 10:41:49 -0700393
ethannicholas22793252016-01-30 09:59:10 -0800394 void createPLSSetupProgram();
395
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000396 // GL program-related state
397 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000398
399 ///////////////////////////////////////////////////////////////////////////
400 ///@name Caching of GL State
401 ///@{
402 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000403 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000404
bsalomon@google.com49209392012-06-05 15:13:46 +0000405 enum TriState {
406 kNo_TriState,
407 kYes_TriState,
408 kUnknown_TriState
409 };
410
egdanield803f272015-03-18 13:01:52 -0700411 GrGLuint fTempSrcFBOID;
412 GrGLuint fTempDstFBOID;
413
414 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800415
bsalomon@google.coma3201942012-06-21 19:58:20 +0000416 // last scissor / viewport scissor state seen by the GL.
417 struct {
418 TriState fEnabled;
419 GrGLIRect fRect;
420 void invalidate() {
421 fEnabled = kUnknown_TriState;
422 fRect.invalidate();
423 }
424 } fHWScissorSettings;
425
bsalomon424cc262015-05-22 10:37:30 -0700426 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000427
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000428 /**
429 * Tracks bound vertex and index buffers and vertex attrib array state.
430 */
431 class HWGeometryState {
432 public:
halcanary96fcdcc2015-08-27 07:41:13 -0700433 HWGeometryState() { fVBOVertexArray = nullptr; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000434
halcanary385fe4d2015-08-26 13:07:48 -0700435 ~HWGeometryState() { delete fVBOVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000436
bsalomon@google.com6918d482013-03-07 19:09:11 +0000437 void invalidate() {
438 fBoundVertexArrayIDIsValid = false;
439 fBoundVertexBufferIDIsValid = false;
440 fDefaultVertexArrayBoundIndexBufferID = false;
441 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
442 fDefaultVertexArrayAttribState.invalidate();
bsalomon49f085d2014-09-05 13:34:00 -0700443 if (fVBOVertexArray) {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000444 fVBOVertexArray->invalidateCachedState();
445 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000446 }
447
448 void notifyVertexArrayDelete(GrGLuint id) {
449 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
450 // Does implicit bind to 0
451 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000452 }
453 }
454
bsalomon861e1032014-12-16 07:33:49 -0800455 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000456 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000457 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000458 return;
459 }
460 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
461 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
462 fBoundVertexArrayIDIsValid = true;
463 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000464 }
465 }
466
bsalomon@google.come49ad452013-02-20 19:33:20 +0000467 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000468 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
469 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000470 }
bsalomon49f085d2014-09-05 13:34:00 -0700471 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000472 fVBOVertexArray->notifyVertexBufferDelete(id);
473 }
474 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000475 }
476
bsalomon@google.come49ad452013-02-20 19:33:20 +0000477 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000478 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
479 id == fDefaultVertexArrayBoundIndexBufferID) {
480 fDefaultVertexArrayBoundIndexBufferID = 0;
481 }
bsalomon49f085d2014-09-05 13:34:00 -0700482 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000483 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000484 }
485 }
486
bsalomon861e1032014-12-16 07:33:49 -0800487 void setVertexBufferID(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000488 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
489 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
490 fBoundVertexBufferIDIsValid = true;
491 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000492 }
493 }
494
bsalomon@google.com6918d482013-03-07 19:09:11 +0000495 /**
496 * Binds the default vertex array and binds the index buffer. This is used when binding
497 * an index buffer in order to update it.
498 */
bsalomon861e1032014-12-16 07:33:49 -0800499 void setIndexBufferIDOnDefaultVertexArray(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000500 this->setVertexArrayID(gpu, 0);
501 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
502 id != fDefaultVertexArrayBoundIndexBufferID) {
503 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
504 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
505 fDefaultVertexArrayBoundIndexBufferID = id;
506 }
507 }
508
509 /**
510 * Binds the vertex array object that should be used to render from the vertex buffer.
511 * The vertex array is bound and its attrib array state object is returned. The vertex
halcanary96fcdcc2015-08-27 07:41:13 -0700512 * 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 +0000513 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000514 */
bsalomon861e1032014-12-16 07:33:49 -0800515 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
robertphillipsf8c3ba42016-03-25 04:55:58 -0700516 const GrGLVertexBuffer* vbuffer,
517 const GrGLIndexBuffer* ibuffer);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000518
bsalomon6df86402015-06-01 10:41:49 -0700519 /** Variants of the above that takes GL buffer IDs. Note that 0 does not imply that a
520 buffer won't be bound. The "default buffer" will be bound, which is used for client-side
521 array rendering. */
522 GrGLAttribArrayState* bindArrayAndBufferToDraw(GrGLGpu* gpu, GrGLuint vbufferID);
523 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
524 GrGLuint vbufferID,
525 GrGLuint ibufferID);
526
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000527 private:
bsalomon6df86402015-06-01 10:41:49 -0700528 GrGLAttribArrayState* internalBind(GrGLGpu* gpu, GrGLuint vbufferID, GrGLuint* ibufferID);
529
bsalomon@google.com6918d482013-03-07 19:09:11 +0000530 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000531 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000532 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000533 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000534
bsalomon@google.com6918d482013-03-07 19:09:11 +0000535 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
536 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
537 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800538 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
539 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000540 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000541
bsalomon@google.com6918d482013-03-07 19:09:11 +0000542 // This is used when we're using a core profile and the vertices are in a VBO.
543 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000544 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000545
cdaltonc1613102016-03-16 07:48:20 -0700546 GrGLuint fHWBoundTextureBufferID;
547 GrGLuint fHWBoundDrawIndirectBufferID;
548 bool fHWBoundTextureBufferIDIsValid;
549 bool fHWBoundDrawIndirectBufferIDIsValid;
550
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000551 struct {
cdalton8917d622015-05-06 13:40:21 -0700552 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000553 GrBlendCoeff fSrcCoeff;
554 GrBlendCoeff fDstCoeff;
555 GrColor fConstColor;
556 bool fConstColorValid;
557 TriState fEnabled;
558
559 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700560 fEquation = static_cast<GrBlendEquation>(-1);
561 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
562 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000563 fConstColorValid = false;
564 fEnabled = kUnknown_TriState;
565 }
566 } fHWBlendState;
567
bsalomon54c6fe82015-12-16 11:51:22 -0800568 TriState fMSAAEnabled;
569
570 GrStencilSettings fHWStencilSettings;
571 TriState fHWStencilTestEnabled;
572
573
574 GrPipelineBuilder::DrawFace fHWDrawFace;
575 TriState fHWWriteToColor;
576 uint32_t fHWBoundRenderTargetUniqueID;
577 TriState fHWSRGBFramebuffer;
578 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800579
580 // EXT_raster_multisample.
581 TriState fHWRasterMultisampleEnabled;
582 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800583 ///@}
584
bsalomon6df86402015-06-01 10:41:49 -0700585 /** IDs for copy surface program. */
586 struct {
587 GrGLuint fProgram;
588 GrGLint fTextureUniform;
589 GrGLint fTexCoordXformUniform;
590 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800591 } fCopyPrograms[3];
bsalomon7ea33f52015-11-22 14:51:00 -0800592 GrGLuint fCopyProgramArrayBuffer;
593
bsalomon6dea83f2015-12-03 12:58:06 -0800594 struct {
595 GrGLuint fProgram;
596 GrGLint fColorUniform;
597 GrGLint fRectUniform;
598 } fWireRectProgram;
599 GrGLuint fWireRectArrayBuffer;
600
bsalomon7ea33f52015-11-22 14:51:00 -0800601 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800602 switch (target) {
603 case GR_GL_TEXTURE_2D:
604 return 0;
605 case GR_GL_TEXTURE_EXTERNAL:
606 return 1;
607 case GR_GL_TEXTURE_RECTANGLE:
608 return 2;
609 default:
610 SkFAIL("Unexpected texture target type.");
611 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800612 }
613 }
bsalomon6df86402015-06-01 10:41:49 -0700614
ethannicholas22793252016-01-30 09:59:10 -0800615 struct {
616 GrGLuint fProgram;
617 GrGLint fPosXformUniform;
618 GrGLuint fArrayBuffer;
619 } fPLSSetupProgram;
620
621 bool fHWPLSEnabled;
622 bool fPLSHasBeenUsed;
623
ethannicholas28ef4452016-03-25 09:26:03 -0700624 float fHWMinSampleShading;
625
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700627 friend class GrGLPathRendering; // For accessing setTextureUnit.
reed@google.comac10a2d2010-12-22 21:39:39 +0000628};
629
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000630#endif