blob: 00acd34d9d52cd39c8b2a94d134fe059e16b453a [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"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#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"
jvanverth73063dc2015-12-03 09:15:47 -080019#include "GrGLTransferBuffer.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000020#include "GrGLVertexArray.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000021#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"
egdaniel080e6732014-12-22 07:35:52 -080024#include "GrXferProcessor.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000025#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
egdaniel8dd688b2015-01-22 10:16:09 -080027class GrPipeline;
bsalomone64eb572015-05-07 11:35:55 -070028class GrNonInstancedVertices;
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.
bsalomon@google.com34cccde2013-01-04 18:34:30 +000060 void bindTexture(int unitIdx, const GrTextureParams& params, GrGLTexture* texture);
kkinnunenccdaa042014-08-20 01:36:23 -070061
bsalomonf0674512015-07-28 13:26:15 -070062 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
63 GrPixelConfig readConfig, DrawPreference*,
64 ReadPixelTempDrawInfo*) override;
bsalomon39826022015-07-23 08:07:21 -070065
bsalomonf0674512015-07-28 13:26:15 -070066 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
cblumeed828002016-02-16 13:00:01 -080067 GrPixelConfig srcConfig, DrawPreference*,
bsalomonf0674512015-07-28 13:26:15 -070068 WritePixelTempDrawInfo*) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +000069
joshualitt1c735482015-07-13 08:08:25 -070070 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override;
bsalomon@google.comeb851172013-04-15 13:51:00 +000071
bsalomon@google.com6918d482013-03-07 19:09:11 +000072 // 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 +000073 // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
bsalomon@google.com6918d482013-03-07 19:09:11 +000074 void bindVertexArray(GrGLuint id) {
75 fHWGeometryState.setVertexArrayID(this, id);
76 }
77 void bindIndexBufferAndDefaultVertexArray(GrGLuint id) {
78 fHWGeometryState.setIndexBufferIDOnDefaultVertexArray(this, id);
79 }
80 void bindVertexBuffer(GrGLuint id) {
81 fHWGeometryState.setVertexBufferID(this, id);
82 }
83
84 // These callbacks update state tracking when GL objects are deleted. They are called from
85 // GrGLResource onRelease functions.
86 void notifyVertexArrayDelete(GrGLuint id) {
87 fHWGeometryState.notifyVertexArrayDelete(id);
88 }
89 void notifyVertexBufferDelete(GrGLuint id) {
90 fHWGeometryState.notifyVertexBufferDelete(id);
91 }
92 void notifyIndexBufferDelete(GrGLuint id) {
93 fHWGeometryState.notifyIndexBufferDelete(id);
94 }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +000095
joshualitt873ad0e2015-01-20 09:08:51 -080096 void buildProgramDesc(GrProgramDesc*,
97 const GrPrimitiveProcessor&,
joshualitt465283c2015-09-11 08:19:35 -070098 const GrPipeline&) const override;
joshualitt79f8fae2014-10-28 17:59:26 -070099
joshualitt93316b92015-10-23 09:08:08 -0700100 // id and type (GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER) of buffer to bind
101 void bindBuffer(GrGLuint id, GrGLenum type);
102
103 void releaseBuffer(GrGLuint id, GrGLenum type);
104
105 // sizes are in bytes
jvanverth73063dc2015-12-03 09:15:47 -0800106 void* mapBuffer(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, size_t currentSize,
joshualitt93316b92015-10-23 09:08:08 -0700107 size_t requestedSize);
108
109 void unmapBuffer(GrGLuint id, GrGLenum type, void* mapPtr);
110
jvanverth73063dc2015-12-03 09:15:47 -0800111 void bufferData(GrGLuint id, GrGLenum type, GrGLBufferImpl::Usage usage, size_t currentSize,
joshualitt93316b92015-10-23 09:08:08 -0700112 const void* src, size_t srcSizeInBytes);
113
reedf9ad5582015-06-25 21:29:25 -0700114 const GrGLContext* glContextForTesting() const override {
115 return &this->glContext();
bsalomon993a4212015-05-29 11:37:25 -0700116 }
117
egdanielec00d942015-09-14 12:56:10 -0700118 void clearStencil(GrRenderTarget*) override;
119
120 void invalidateBoundRenderTarget() {
121 fHWBoundRenderTargetUniqueID = SK_InvalidUniqueID;
122 }
123
124 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
125 int width,
126 int height) override;
127
jvanverth88957922015-07-14 11:02:52 -0700128 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
bsalomone63ffef2016-02-05 07:17:34 -0800129 GrPixelConfig config) override;
bsalomon67d76202015-11-11 12:40:42 -0800130 bool isTestingOnlyBackendTexture(GrBackendObject) const override;
bsalomone63ffef2016-02-05 07:17:34 -0800131 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override;
jvanverth672bb7f2015-07-13 07:19:57 -0700132
joshualitt8fd844f2015-12-02 13:36:47 -0800133 void resetShaderCacheForTesting() const override;
134
bsalomon6dea83f2015-12-03 12:58:06 -0800135 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override;
136
jvanverthd2d2eb92016-02-17 14:04:46 -0800137 void finishDrawTarget() override;
ethannicholas22793252016-01-30 09:59:10 -0800138
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000139private:
bsalomon424cc262015-05-22 10:37:30 -0700140 GrGLGpu(GrGLContext* ctx, GrContext* context);
141
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000142 // GrGpu overrides
mtklein36352bf2015-03-25 18:17:31 -0700143 void onResetContext(uint32_t resetBits) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144
bsalomoncb02b382015-08-12 11:14:50 -0700145 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
146
egdanielb0e1be22015-04-22 13:27:39 -0700147 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
148 const void* srcData, size_t rowBytes) override;
149 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
150 GrGpuResource::LifeCycle lifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700151 const void* srcData) override;
152 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override;
153 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override;
jvanverth73063dc2015-12-03 09:15:47 -0800154 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;
egdanielff1d5472015-09-10 08:37:20 -0700158 // Given a GrPixelConfig return the index into the stencil format array on GrGLCaps to a
bsalomon62a627b2015-12-17 09:50:47 -0800159 // compatible stencil format, or negative if there is no compatible stencil format.
egdanielff1d5472015-09-10 08:37:20 -0700160 int getCompatibleStencilIndex(GrPixelConfig config);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161
erikchen9a1ed5d2016-02-10 16:32:34 -0800162 // If |desc.fTextureStorageAllocator| exists, use that to create the
163 // texture. Otherwise, create the texture directly.
164 // Returns whether the texture is successfully created. On success, the
165 // result is stored in |info|.
166 // The texture is populated with |srcData|, if it exists.
167 // The texture parameters are cached in |initialTexParams|.
168 bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
169 bool renderTarget, const void* srcData,
170 GrGLTexture::TexParams* initialTexParams, size_t rowBytes);
171 bool createTextureExternalAllocatorImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info,
172 const void* srcData, size_t rowBytes);
173
egdaniel51c8d402015-08-06 10:54:13 -0700174 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000175
mtklein36352bf2015-03-25 18:17:31 -0700176 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
joshualitt6db519c2014-10-29 08:48:18 -0700177
bsalomone179a912016-01-20 06:18:10 -0800178 bool onMakeCopyForTextureParams(GrTexture*, const GrTextureParams&,
179 GrTextureProducer::CopyParams*) const override;
180
bsalomon1aa20292016-01-22 08:16:09 -0800181 // Checks whether glReadPixels can be called to get pixel values in readConfig from the
182 // render target.
183 bool readPixelsSupported(GrRenderTarget* target, GrPixelConfig readConfig);
184
185 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
186 // render target that has renderTargetConfig. This may have to create a temporary
187 // render target and thus is less preferable than the variant that takes a render target.
188 bool readPixelsSupported(GrPixelConfig renderTargetConfig, GrPixelConfig readConfig);
189
190 // Checks whether glReadPixels can be called to get pixel values in readConfig from a
191 // render target that has the same config as surfaceForConfig. Calls one of the the two
192 // variations above, depending on whether the surface is a render target or not.
193 bool readPixelsSupported(GrSurface* surfaceForConfig, GrPixelConfig readConfig);
194
bsalomon6cb3cbe2015-07-30 07:34:27 -0700195 bool onReadPixels(GrSurface*,
bsalomond95263c2014-12-16 13:05:12 -0800196 int left, int top,
197 int width, int height,
198 GrPixelConfig,
199 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700200 size_t rowBytes) override;
bsalomond95263c2014-12-16 13:05:12 -0800201
bsalomon6cb3cbe2015-07-30 07:34:27 -0700202 bool onWritePixels(GrSurface*,
203 int left, int top, int width, int height,
204 GrPixelConfig config, const void* buffer,
205 size_t rowBytes) override;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000206
jvanverth17aa0472016-01-05 10:41:27 -0800207 bool onTransferPixels(GrSurface*,
208 int left, int top, int width, int height,
209 GrPixelConfig config, GrTransferBuffer* buffer,
210 size_t offset, size_t rowBytes) override;
211
mtklein36352bf2015-03-25 18:17:31 -0700212 void onResolveRenderTarget(GrRenderTarget* target) override;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000213
bsalomone64eb572015-05-07 11:35:55 -0700214 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override;
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000215
joshualitt1cbdcde2015-08-21 11:53:29 -0700216 bool onCopySurface(GrSurface* dst,
217 GrSurface* src,
218 const SkIRect& srcRect,
219 const SkIPoint& dstPoint) override;
220
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000221 // binds texture unit in GL
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000222 void setTextureUnit(int unitIdx);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
egdaniel8dd688b2015-01-22 10:16:09 -0800224 // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
cdaltond0a840d2015-03-16 17:19:58 -0700225 bool flushGLState(const DrawArgs&);
bsalomond95263c2014-12-16 13:05:12 -0800226
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000227 // Sets up vertex attribute pointers and strides. On return indexOffsetInBytes gives the offset
bsalomoncb8979d2015-05-05 09:51:38 -0700228 // an into the index buffer. It does not account for vertices.startIndex() but rather the start
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000229 // index is relative to the returned offset.
joshualitt873ad0e2015-01-20 09:08:51 -0800230 void setupGeometry(const GrPrimitiveProcessor&,
bsalomone64eb572015-05-07 11:35:55 -0700231 const GrNonInstancedVertices& vertices,
joshualittd53a8272014-11-10 16:03:14 -0800232 size_t* indexOffsetInBytes);
bsalomon@google.com7acdb8e2011-02-11 14:07:02 +0000233
bsalomon7f9b2e42016-01-12 13:29:26 -0800234 void flushBlend(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000235
bsalomon424cc262015-05-22 10:37:30 -0700236 bool hasExtension(const char* ext) const { return fGLContext->hasExtension(ext); }
bsalomon@google.com96399942012-02-13 14:39:16 +0000237
bsalomon6df86402015-06-01 10:41:49 -0700238 void copySurfaceAsDraw(GrSurface* dst,
239 GrSurface* src,
240 const SkIRect& srcRect,
241 const SkIPoint& dstPoint);
242 void copySurfaceAsCopyTexSubImage(GrSurface* dst,
243 GrSurface* src,
244 const SkIRect& srcRect,
245 const SkIPoint& dstPoint);
246 bool copySurfaceAsBlitFramebuffer(GrSurface* dst,
247 GrSurface* src,
248 const SkIRect& srcRect,
249 const SkIPoint& dstPoint);
250
ethannicholas22793252016-01-30 09:59:10 -0800251 void stampRectUsingProgram(GrGLuint program, const SkRect& bounds, GrGLint posXformUniform,
252 GrGLuint arrayBuffer);
253
254 void setupPixelLocalStorage(const DrawArgs& args);
255
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000256 static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
bsalomon@google.com080773c2011-03-15 19:09:25 +0000257
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000258 class ProgramCache : public ::SkNoncopyable {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000259 public:
bsalomon861e1032014-12-16 07:33:49 -0800260 ProgramCache(GrGLGpu* gpu);
jvanverth@google.com94878772013-03-12 16:00:54 +0000261 ~ProgramCache();
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000262
joshualitt8fd844f2015-12-02 13:36:47 -0800263 void reset();
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000264 void abandon();
bsalomon6df86402015-06-01 10:41:49 -0700265 GrGLProgram* refProgram(const DrawArgs&);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000266
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000267 private:
268 enum {
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000269 // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
270 // shader before evicting from the cache.
commit-bot@chromium.orgb5564222014-03-28 15:52:18 +0000271 kMaxEntries = 128,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000272 kHashBits = 6,
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000273 };
274
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000275 struct Entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000276
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000277 struct ProgDescLess;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000278
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000279 // binary search for entry matching desc. returns index into fEntries that matches desc or ~
280 // of the index of where it should be inserted.
joshualitt79f8fae2014-10-28 17:59:26 -0700281 int search(const GrProgramDesc& desc) const;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000282
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000283 // sorted array of all the entries
284 Entry* fEntries[kMaxEntries];
285 // hash table based on lowest kHashBits bits of the program key. Used to avoid binary
286 // searching fEntries.
287 Entry* fHashTable[1 << kHashBits];
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000288
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000289 int fCount;
290 unsigned int fCurrLRUStamp;
bsalomon861e1032014-12-16 07:33:49 -0800291 GrGLGpu* fGpu;
jvanverth@google.com94878772013-03-12 16:00:54 +0000292#ifdef PROGRAM_CACHE_STATS
293 int fTotalRequests;
294 int fCacheMisses;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000295 int fHashMisses; // cache hit but hash table missed
jvanverth@google.com94878772013-03-12 16:00:54 +0000296#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000297 };
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000298
egdaniel080e6732014-12-22 07:35:52 -0800299 void flushColorWrite(bool writeColor);
egdaniel8dd688b2015-01-22 10:16:09 -0800300 void flushDrawFace(GrPipelineBuilder::DrawFace face);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000301
bsalomon@google.coma3201942012-06-21 19:58:20 +0000302 // flushes the scissor. see the note on flushBoundTextureAndParams about
303 // flushing the scissor after that function is called.
bsalomon3e791242014-12-17 13:43:13 -0800304 void flushScissor(const GrScissorState&,
joshualitt77b13072014-10-27 14:51:01 -0700305 const GrGLIRect& rtViewport,
306 GrSurfaceOrigin rtOrigin);
307
308 // disables the scissor
309 void disableScissor();
bsalomon@google.coma3201942012-06-21 19:58:20 +0000310
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000311 void initFSAASupport();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000312
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000313 // determines valid stencil formats
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000314 void initStencilFormats();
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000315
commit-bot@chromium.orga15f7e52013-06-05 23:29:25 +0000316 // sets a texture unit to use for texture operations other than binding a texture to a program.
317 // ensures that such operations don't negatively interact with tracking bound textures.
318 void setScratchTextureUnit();
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
bsalomon083617b2016-02-12 12:10:14 -0800320 // bounds is region that may be modified.
halcanary96fcdcc2015-08-27 07:41:13 -0700321 // nullptr means whole target. Can be an empty rect.
cdaltond4727922015-11-10 12:49:06 -0800322 void flushRenderTarget(GrGLRenderTarget*, const SkIRect* bounds);
bsalomon083617b2016-02-12 12:10:14 -0800323 // Handles cases where a surface will be updated without a call to flushRenderTarget
324 void didWriteToSurface(GrSurface*, const SkIRect* bounds) const;
325
326 // Need not be called if flushRenderTarget is used.
327 void flushViewport(const GrGLIRect&);
bsalomonb0bd4f62014-09-03 07:19:50 -0700328
bsalomon3e791242014-12-17 13:43:13 -0800329 void flushStencil(const GrStencilSettings&);
bsalomon083617b2016-02-12 12:10:14 -0800330
331 // rt is used only if useHWAA is true.
cdaltonaf8bc7d2016-02-05 09:35:20 -0800332 void flushHWAAState(GrRenderTarget* rt, bool useHWAA, bool stencilEnabled);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000333
bsalomon@google.com6f379512011-11-16 20:36:03 +0000334 // helper for onCreateTexture and writeTexturePixels
jvanverth17aa0472016-01-05 10:41:27 -0800335 enum UploadType {
336 kNewTexture_UploadType, // we are creating a new texture
337 kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
338 kTransfer_UploadType, // we are using a transfer buffer to copy data
339 };
bsalomonb15b4c12014-10-29 12:41:57 -0700340 bool uploadTexData(const GrSurfaceDesc& desc,
jcgregorioe7d7f902016-02-04 19:51:25 -0800341 GrGLenum target,
jvanverth17aa0472016-01-05 10:41:27 -0800342 UploadType uploadType,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 int left, int top, int width, int height,
344 GrPixelConfig dataConfig,
345 const void* data,
346 size_t rowBytes);
bsalomon@google.com0650e812011-04-08 18:07:53 +0000347
krajcevski145d48c2014-06-11 16:07:50 -0700348 // helper for onCreateCompressedTexture. If width and height are
349 // set to -1, then this function will use desc.fWidth and desc.fHeight
350 // for the size of the data. The isNewTexture flag should be set to true
351 // whenever a new texture needs to be created. Otherwise, we assume that
352 // the texture is already in GPU memory and that it's going to be updated
353 // with new data.
bsalomonb15b4c12014-10-29 12:41:57 -0700354 bool uploadCompressedTexData(const GrSurfaceDesc& desc,
bsalomon10528f12015-10-14 12:54:52 -0700355 GrGLenum target,
krajcevski145d48c2014-06-11 16:07:50 -0700356 const void* data,
jvanverth17aa0472016-01-05 10:41:27 -0800357 UploadType uploadType = kNewTexture_UploadType,
krajcevski145d48c2014-06-11 16:07:50 -0700358 int left = 0, int top = 0,
359 int width = -1, int height = -1);
krajcevski9c0e6292014-06-02 07:38:14 -0700360
egdanielb0e1be22015-04-22 13:27:39 -0700361 bool createRenderTargetObjects(const GrSurfaceDesc&, GrGpuResource::LifeCycle lifeCycle,
bsalomon091f60c2015-11-10 11:54:56 -0800362 const GrGLTextureInfo& texInfo, GrGLRenderTarget::IDDesc*);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000363
egdanield803f272015-03-18 13:01:52 -0700364 enum TempFBOTarget {
365 kSrc_TempFBOTarget,
366 kDst_TempFBOTarget
367 };
egdaniel0f5f9672015-02-03 11:10:51 -0800368
bsalomon10528f12015-10-14 12:54:52 -0700369 // Binds a surface as a FBO for a copy operation. If the surface already owns an FBO ID then
370 // that ID is bound. If not the surface is temporarily bound to a FBO and that FBO is bound.
371 // This must be paired with a call to unbindSurfaceFBOForCopy().
372 void bindSurfaceFBOForCopy(GrSurface* surface, GrGLenum fboTarget, GrGLIRect* viewport,
egdanield803f272015-03-18 13:01:52 -0700373 TempFBOTarget tempFBOTarget);
egdaniel0f5f9672015-02-03 11:10:51 -0800374
bsalomon10528f12015-10-14 12:54:52 -0700375 // Must be called if bindSurfaceFBOForCopy was used to bind a surface for copying.
376 void unbindTextureFBOForCopy(GrGLenum fboTarget, GrSurface* surface);
robertphillips754f4e92014-09-18 13:52:08 -0700377
reedf9ad5582015-06-25 21:29:25 -0700378 SkAutoTUnref<GrGLContext> fGLContext;
bsalomon@google.com2c17fcd2011-07-06 17:47:02 +0000379
bsalomon7ea33f52015-11-22 14:51:00 -0800380 void createCopyPrograms();
bsalomon6dea83f2015-12-03 12:58:06 -0800381 void createWireRectProgram();
382 void createUnitRectBuffer();
bsalomon6df86402015-06-01 10:41:49 -0700383
ethannicholas22793252016-01-30 09:59:10 -0800384 void createPLSSetupProgram();
385
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000386 // GL program-related state
387 ProgramCache* fProgramCache;
bsalomon@google.com49209392012-06-05 15:13:46 +0000388
389 ///////////////////////////////////////////////////////////////////////////
390 ///@name Caching of GL State
391 ///@{
392 int fHWActiveTextureUnitIdx;
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000393 GrGLuint fHWProgramID;
bsalomon@google.com91207482013-02-12 21:45:24 +0000394
bsalomon@google.com49209392012-06-05 15:13:46 +0000395 enum TriState {
396 kNo_TriState,
397 kYes_TriState,
398 kUnknown_TriState
399 };
400
egdanield803f272015-03-18 13:01:52 -0700401 GrGLuint fTempSrcFBOID;
402 GrGLuint fTempDstFBOID;
403
404 GrGLuint fStencilClearFBOID;
bsalomondd3143b2015-02-23 09:27:45 -0800405
bsalomon@google.coma3201942012-06-21 19:58:20 +0000406 // last scissor / viewport scissor state seen by the GL.
407 struct {
408 TriState fEnabled;
409 GrGLIRect fRect;
410 void invalidate() {
411 fEnabled = kUnknown_TriState;
412 fRect.invalidate();
413 }
414 } fHWScissorSettings;
415
bsalomon424cc262015-05-22 10:37:30 -0700416 GrGLIRect fHWViewport;
bsalomon@google.coma3201942012-06-21 19:58:20 +0000417
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000418 /**
419 * Tracks bound vertex and index buffers and vertex attrib array state.
420 */
421 class HWGeometryState {
422 public:
halcanary96fcdcc2015-08-27 07:41:13 -0700423 HWGeometryState() { fVBOVertexArray = nullptr; this->invalidate(); }
skia.committer@gmail.com754a3eb2013-03-08 07:01:25 +0000424
halcanary385fe4d2015-08-26 13:07:48 -0700425 ~HWGeometryState() { delete fVBOVertexArray; }
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000426
bsalomon@google.com6918d482013-03-07 19:09:11 +0000427 void invalidate() {
428 fBoundVertexArrayIDIsValid = false;
429 fBoundVertexBufferIDIsValid = false;
430 fDefaultVertexArrayBoundIndexBufferID = false;
431 fDefaultVertexArrayBoundIndexBufferIDIsValid = false;
432 fDefaultVertexArrayAttribState.invalidate();
bsalomon49f085d2014-09-05 13:34:00 -0700433 if (fVBOVertexArray) {
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +0000434 fVBOVertexArray->invalidateCachedState();
435 }
bsalomon@google.com6918d482013-03-07 19:09:11 +0000436 }
437
438 void notifyVertexArrayDelete(GrGLuint id) {
439 if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
440 // Does implicit bind to 0
441 fBoundVertexArrayID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000442 }
443 }
444
bsalomon861e1032014-12-16 07:33:49 -0800445 void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000446 if (!gpu->glCaps().vertexArrayObjectSupport()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000447 SkASSERT(0 == arrayID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000448 return;
449 }
450 if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
451 GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
452 fBoundVertexArrayIDIsValid = true;
453 fBoundVertexArrayID = arrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000454 }
455 }
456
bsalomon@google.come49ad452013-02-20 19:33:20 +0000457 void notifyVertexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000458 if (fBoundVertexBufferIDIsValid && id == fBoundVertexBufferID) {
459 fBoundVertexBufferID = 0;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000460 }
bsalomon49f085d2014-09-05 13:34:00 -0700461 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000462 fVBOVertexArray->notifyVertexBufferDelete(id);
463 }
464 fDefaultVertexArrayAttribState.notifyVertexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000465 }
466
bsalomon@google.come49ad452013-02-20 19:33:20 +0000467 void notifyIndexBufferDelete(GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000468 if (fDefaultVertexArrayBoundIndexBufferIDIsValid &&
469 id == fDefaultVertexArrayBoundIndexBufferID) {
470 fDefaultVertexArrayBoundIndexBufferID = 0;
471 }
bsalomon49f085d2014-09-05 13:34:00 -0700472 if (fVBOVertexArray) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000473 fVBOVertexArray->notifyIndexBufferDelete(id);
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000474 }
475 }
476
bsalomon861e1032014-12-16 07:33:49 -0800477 void setVertexBufferID(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000478 if (!fBoundVertexBufferIDIsValid || id != fBoundVertexBufferID) {
479 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ARRAY_BUFFER, id));
480 fBoundVertexBufferIDIsValid = true;
481 fBoundVertexBufferID = id;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000482 }
483 }
484
bsalomon@google.com6918d482013-03-07 19:09:11 +0000485 /**
486 * Binds the default vertex array and binds the index buffer. This is used when binding
487 * an index buffer in order to update it.
488 */
bsalomon861e1032014-12-16 07:33:49 -0800489 void setIndexBufferIDOnDefaultVertexArray(GrGLGpu* gpu, GrGLuint id) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000490 this->setVertexArrayID(gpu, 0);
491 if (!fDefaultVertexArrayBoundIndexBufferIDIsValid ||
492 id != fDefaultVertexArrayBoundIndexBufferID) {
493 GR_GL_CALL(gpu->glInterface(), BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, id));
494 fDefaultVertexArrayBoundIndexBufferIDIsValid = true;
495 fDefaultVertexArrayBoundIndexBufferID = id;
496 }
497 }
498
499 /**
500 * Binds the vertex array object that should be used to render from the vertex buffer.
501 * The vertex array is bound and its attrib array state object is returned. The vertex
halcanary96fcdcc2015-08-27 07:41:13 -0700502 * 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 +0000503 * returned GrGLAttribArrayState should be used to set vertex attribute arrays.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000504 */
bsalomon861e1032014-12-16 07:33:49 -0800505 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
bsalomon@google.com6918d482013-03-07 19:09:11 +0000506 const GrGLVertexBuffer* vbuffer,
507 const GrGLIndexBuffer* ibuffer);
508
bsalomon6df86402015-06-01 10:41:49 -0700509 /** Variants of the above that takes GL buffer IDs. Note that 0 does not imply that a
510 buffer won't be bound. The "default buffer" will be bound, which is used for client-side
511 array rendering. */
512 GrGLAttribArrayState* bindArrayAndBufferToDraw(GrGLGpu* gpu, GrGLuint vbufferID);
513 GrGLAttribArrayState* bindArrayAndBuffersToDraw(GrGLGpu* gpu,
514 GrGLuint vbufferID,
515 GrGLuint ibufferID);
516
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000517 private:
bsalomon6df86402015-06-01 10:41:49 -0700518 GrGLAttribArrayState* internalBind(GrGLGpu* gpu, GrGLuint vbufferID, GrGLuint* ibufferID);
519
bsalomon@google.com6918d482013-03-07 19:09:11 +0000520 GrGLuint fBoundVertexArrayID;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000521 GrGLuint fBoundVertexBufferID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000522 bool fBoundVertexArrayIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000523 bool fBoundVertexBufferIDIsValid;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000524
bsalomon@google.com6918d482013-03-07 19:09:11 +0000525 GrGLuint fDefaultVertexArrayBoundIndexBufferID;
526 bool fDefaultVertexArrayBoundIndexBufferIDIsValid;
527 // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
jvanverth39edf762014-12-22 11:44:19 -0800528 // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
529 // GrGLGpu.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000530 GrGLAttribArrayState fDefaultVertexArrayAttribState;
bsalomon@google.com880b8fc2013-02-19 20:17:28 +0000531
bsalomon@google.com6918d482013-03-07 19:09:11 +0000532 // This is used when we're using a core profile and the vertices are in a VBO.
533 GrGLVertexArray* fVBOVertexArray;
bsalomon@google.com49209392012-06-05 15:13:46 +0000534 } fHWGeometryState;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000536 struct {
cdalton8917d622015-05-06 13:40:21 -0700537 GrBlendEquation fEquation;
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000538 GrBlendCoeff fSrcCoeff;
539 GrBlendCoeff fDstCoeff;
540 GrColor fConstColor;
541 bool fConstColorValid;
542 TriState fEnabled;
543
544 void invalidate() {
bsalomonf7cc8772015-05-11 11:21:14 -0700545 fEquation = static_cast<GrBlendEquation>(-1);
546 fSrcCoeff = static_cast<GrBlendCoeff>(-1);
547 fDstCoeff = static_cast<GrBlendCoeff>(-1);
bsalomon@google.coma4d8fc22012-05-21 13:21:46 +0000548 fConstColorValid = false;
549 fEnabled = kUnknown_TriState;
550 }
551 } fHWBlendState;
552
bsalomon54c6fe82015-12-16 11:51:22 -0800553 TriState fMSAAEnabled;
554
555 GrStencilSettings fHWStencilSettings;
556 TriState fHWStencilTestEnabled;
557
558
559 GrPipelineBuilder::DrawFace fHWDrawFace;
560 TriState fHWWriteToColor;
561 uint32_t fHWBoundRenderTargetUniqueID;
562 TriState fHWSRGBFramebuffer;
563 SkTArray<uint32_t, true> fHWBoundTextureUniqueIDs;
cdaltonaf8bc7d2016-02-05 09:35:20 -0800564
565 // EXT_raster_multisample.
566 TriState fHWRasterMultisampleEnabled;
567 int fHWNumRasterSamples;
bsalomon54c6fe82015-12-16 11:51:22 -0800568 ///@}
569
bsalomon6df86402015-06-01 10:41:49 -0700570 /** IDs for copy surface program. */
571 struct {
572 GrGLuint fProgram;
573 GrGLint fTextureUniform;
574 GrGLint fTexCoordXformUniform;
575 GrGLint fPosXformUniform;
bsalomone5286e02016-01-14 09:24:09 -0800576 } fCopyPrograms[3];
bsalomon7ea33f52015-11-22 14:51:00 -0800577 GrGLuint fCopyProgramArrayBuffer;
578
bsalomon6dea83f2015-12-03 12:58:06 -0800579 struct {
580 GrGLuint fProgram;
581 GrGLint fColorUniform;
582 GrGLint fRectUniform;
583 } fWireRectProgram;
584 GrGLuint fWireRectArrayBuffer;
585
bsalomon7ea33f52015-11-22 14:51:00 -0800586 static int TextureTargetToCopyProgramIdx(GrGLenum target) {
bsalomone5286e02016-01-14 09:24:09 -0800587 switch (target) {
588 case GR_GL_TEXTURE_2D:
589 return 0;
590 case GR_GL_TEXTURE_EXTERNAL:
591 return 1;
592 case GR_GL_TEXTURE_RECTANGLE:
593 return 2;
594 default:
595 SkFAIL("Unexpected texture target type.");
596 return 0;
bsalomon7ea33f52015-11-22 14:51:00 -0800597 }
598 }
bsalomon6df86402015-06-01 10:41:49 -0700599
ethannicholas22793252016-01-30 09:59:10 -0800600 struct {
601 GrGLuint fProgram;
602 GrGLint fPosXformUniform;
603 GrGLuint fArrayBuffer;
604 } fPLSSetupProgram;
605
606 bool fHWPLSEnabled;
607 bool fPLSHasBeenUsed;
608
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 typedef GrGpu INHERITED;
kkinnunenccdaa042014-08-20 01:36:23 -0700610 friend class GrGLPathRendering; // For accessing setTextureUnit.
reed@google.comac10a2d2010-12-22 21:39:39 +0000611};
612
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000613#endif