GrGLGpu.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * 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.
  6. */
  7. #ifndef GrGLGpu_DEFINED
  8. #define GrGLGpu_DEFINED
  9. #include <list>
  10. #include "include/core/SkTypes.h"
  11. #include "include/private/SkTArray.h"
  12. #include "src/core/SkLRUCache.h"
  13. #include "src/gpu/GrGpu.h"
  14. #include "src/gpu/GrMesh.h"
  15. #include "src/gpu/GrWindowRectsState.h"
  16. #include "src/gpu/GrXferProcessor.h"
  17. #include "src/gpu/gl/GrGLContext.h"
  18. #include "src/gpu/gl/GrGLIRect.h"
  19. #include "src/gpu/gl/GrGLPathRendering.h"
  20. #include "src/gpu/gl/GrGLProgram.h"
  21. #include "src/gpu/gl/GrGLRenderTarget.h"
  22. #include "src/gpu/gl/GrGLStencilAttachment.h"
  23. #include "src/gpu/gl/GrGLTexture.h"
  24. #include "src/gpu/gl/GrGLVertexArray.h"
  25. class GrGLBuffer;
  26. class GrGLGpuRTCommandBuffer;
  27. class GrGLGpuTextureCommandBuffer;
  28. class GrPipeline;
  29. class GrSwizzle;
  30. #ifdef SK_DEBUG
  31. #define PROGRAM_CACHE_STATS
  32. #endif
  33. class GrGLGpu final : public GrGpu, private GrMesh::SendToGpuImpl {
  34. public:
  35. static sk_sp<GrGpu> Make(sk_sp<const GrGLInterface>, const GrContextOptions&, GrContext*);
  36. ~GrGLGpu() override;
  37. void disconnect(DisconnectType) override;
  38. const GrGLContext& glContext() const { return *fGLContext; }
  39. const GrGLInterface* glInterface() const { return fGLContext->interface(); }
  40. const GrGLContextInfo& ctxInfo() const { return *fGLContext; }
  41. GrGLStandard glStandard() const { return fGLContext->standard(); }
  42. GrGLVersion glVersion() const { return fGLContext->version(); }
  43. GrGLSLGeneration glslGeneration() const { return fGLContext->glslGeneration(); }
  44. const GrGLCaps& glCaps() const { return *fGLContext->caps(); }
  45. GrGLPathRendering* glPathRendering() {
  46. SkASSERT(glCaps().shaderCaps()->pathRenderingSupport());
  47. return static_cast<GrGLPathRendering*>(pathRendering());
  48. }
  49. // Used by GrGLProgram to configure OpenGL state.
  50. void bindTexture(int unitIdx, GrSamplerState samplerState, const GrSwizzle&, GrGLTexture*);
  51. // These functions should be used to bind GL objects. They track the GL state and skip redundant
  52. // bindings. Making the equivalent glBind calls directly will confuse the state tracking.
  53. void bindVertexArray(GrGLuint id) {
  54. fHWVertexArrayState.setVertexArrayID(this, id);
  55. }
  56. // These callbacks update state tracking when GL objects are deleted. They are called from
  57. // GrGLResource onRelease functions.
  58. void notifyVertexArrayDelete(GrGLuint id) {
  59. fHWVertexArrayState.notifyVertexArrayDelete(id);
  60. }
  61. // Binds a buffer to the GL target corresponding to 'type', updates internal state tracking, and
  62. // returns the GL target the buffer was bound to.
  63. // When 'type' is kIndex_GrBufferType, this function will also implicitly bind the default VAO.
  64. // If the caller wishes to bind an index buffer to a specific VAO, it can call glBind directly.
  65. GrGLenum bindBuffer(GrGpuBufferType type, const GrBuffer*);
  66. // The GrGLGpuRTCommandBuffer does not buffer up draws before submitting them to the gpu.
  67. // Thus this is the implementation of the draw call for the corresponding passthrough function
  68. // on GrGLRTGpuCommandBuffer.
  69. void draw(GrRenderTarget*, GrSurfaceOrigin,
  70. const GrPrimitiveProcessor&,
  71. const GrPipeline&,
  72. const GrPipeline::FixedDynamicState*,
  73. const GrPipeline::DynamicStateArrays*,
  74. const GrMesh[],
  75. int meshCount);
  76. // GrMesh::SendToGpuImpl methods. These issue the actual GL draw calls.
  77. // Marked final as a hint to the compiler to not use virtual dispatch.
  78. void sendMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
  79. int baseVertex) final;
  80. void sendIndexedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
  81. int baseIndex, uint16_t minIndexValue, uint16_t maxIndexValue,
  82. const GrBuffer* vertexBuffer, int baseVertex,
  83. GrPrimitiveRestart) final;
  84. void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
  85. int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
  86. int baseInstance) final;
  87. void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
  88. int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
  89. const GrBuffer* instanceBuffer, int instanceCount,
  90. int baseInstance, GrPrimitiveRestart) final;
  91. // The GrGLGpuRTCommandBuffer does not buffer up draws before submitting them to the gpu.
  92. // Thus this is the implementation of the clear call for the corresponding passthrough function
  93. // on GrGLGpuRTCommandBuffer.
  94. void clear(const GrFixedClip&, const SkPMColor4f&, GrRenderTarget*, GrSurfaceOrigin);
  95. // The GrGLGpuRTCommandBuffer does not buffer up draws before submitting them to the gpu.
  96. // Thus this is the implementation of the clearStencil call for the corresponding passthrough
  97. // function on GrGLGpuRTCommandBuffer.
  98. void clearStencilClip(const GrFixedClip&, bool insideStencilMask,
  99. GrRenderTarget*, GrSurfaceOrigin);
  100. // FIXME (michaelludwig): Can this go away and just use clearStencilClip() + marking the
  101. // stencil buffer as not dirty?
  102. void clearStencil(GrRenderTarget*, int clearValue);
  103. GrGpuRTCommandBuffer* getCommandBuffer(
  104. GrRenderTarget*, GrSurfaceOrigin, const SkRect&,
  105. const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
  106. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
  107. GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
  108. void invalidateBoundRenderTarget() {
  109. fHWBoundRenderTargetUniqueID.makeInvalid();
  110. }
  111. GrStencilAttachment* createStencilAttachmentForRenderTarget(
  112. const GrRenderTarget* rt, int width, int height, int numStencilSamples) override;
  113. GrBackendTexture createBackendTexture(int w, int h, const GrBackendFormat&,
  114. GrMipMapped, GrRenderable,
  115. const void* pixels, size_t rowBytes,
  116. const SkColor4f* color,
  117. GrProtected isProtected) override;
  118. void deleteBackendTexture(const GrBackendTexture&) override;
  119. #if GR_TEST_UTILS
  120. bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
  121. GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override;
  122. void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
  123. const GrGLContext* glContextForTesting() const override { return &this->glContext(); }
  124. void resetShaderCacheForTesting() const override { fProgramCache->reset(); }
  125. void testingOnly_flushGpuAndSync() override;
  126. #endif
  127. void submit(GrGpuCommandBuffer* buffer) override;
  128. GrFence SK_WARN_UNUSED_RESULT insertFence() override;
  129. bool waitFence(GrFence, uint64_t timeout) override;
  130. void deleteFence(GrFence) const override;
  131. sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
  132. sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
  133. GrResourceProvider::SemaphoreWrapType wrapType,
  134. GrWrapOwnership ownership) override;
  135. void insertSemaphore(sk_sp<GrSemaphore> semaphore) override;
  136. void waitSemaphore(sk_sp<GrSemaphore> semaphore) override;
  137. void checkFinishProcs() override;
  138. sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
  139. void deleteSync(GrGLsync) const;
  140. void insertEventMarker(const char*);
  141. void bindFramebuffer(GrGLenum fboTarget, GrGLuint fboid);
  142. void deleteFramebuffer(GrGLuint fboid);
  143. private:
  144. GrGLGpu(std::unique_ptr<GrGLContext>, GrContext*);
  145. // GrGpu overrides
  146. void onResetContext(uint32_t resetBits) override;
  147. void onResetTextureBindings() override;
  148. void querySampleLocations(GrRenderTarget*, SkTArray<SkPoint>*) override;
  149. void xferBarrier(GrRenderTarget*, GrXferBarrierType) override;
  150. sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  151. SkBudgeted, GrProtected, const GrMipLevel[],
  152. int mipLevelCount) override;
  153. sk_sp<GrTexture> onCreateCompressedTexture(int width, int height,
  154. SkImage::CompressionType compression, SkBudgeted,
  155. const void* data) override;
  156. sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType intendedType, GrAccessPattern,
  157. const void* data) override;
  158. sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrColorType, GrWrapOwnership,
  159. GrWrapCacheable, GrIOType) override;
  160. sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, int sampleCnt,
  161. GrColorType, GrWrapOwnership,
  162. GrWrapCacheable) override;
  163. sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&,
  164. GrColorType) override;
  165. sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
  166. int sampleCnt, GrColorType) override;
  167. // Given a GL format return the index into the stencil format array on GrGLCaps to a
  168. // compatible stencil format, or negative if there is no compatible stencil format.
  169. int getCompatibleStencilIndex(GrGLFormat format);
  170. void onFBOChanged();
  171. // Returns whether the texture is successfully created. On success, the
  172. // result is stored in |info|.
  173. // The texture is populated with |texels|, if it exists.
  174. // The texture parameters are cached in |initialTexParams|.
  175. bool createTextureImpl(const GrSurfaceDesc& desc, GrGLTextureInfo* info, GrRenderable,
  176. GrGLTextureParameters::SamplerOverriddenState* initialState,
  177. const GrMipLevel texels[], int mipLevelCount,
  178. GrMipMapsStatus* mipMapsStatus);
  179. bool createCompressedTextureImpl(GrGLTextureInfo* info, int width, int height,
  180. SkImage::CompressionType compression,
  181. GrGLTextureParameters::SamplerOverriddenState* initialState,
  182. const void* data);
  183. bool onReadPixels(GrSurface*, int left, int top, int width, int height, GrColorType,
  184. void* buffer, size_t rowBytes) override;
  185. bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
  186. const GrMipLevel texels[], int mipLevelCount) override;
  187. bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height, GrColorType,
  188. GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) override;
  189. bool onTransferPixelsFrom(GrSurface*, int left, int top, int width, int height, GrColorType,
  190. GrGpuBuffer* transferBuffer, size_t offset) override;
  191. bool readOrTransferPixelsFrom(GrSurface*, int left, int top, int width, int height, GrColorType,
  192. void* offsetOrPtr, int rowWidthInPixels);
  193. // Before calling any variation of TexImage, TexSubImage, etc..., call this to ensure that the
  194. // PIXEL_UNPACK_BUFFER is unbound.
  195. void unbindCpuToGpuXferBuffer();
  196. void onResolveRenderTarget(GrRenderTarget* target) override;
  197. bool onRegenerateMipMapLevels(GrTexture*) override;
  198. bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  199. const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) override;
  200. // binds texture unit in GL
  201. void setTextureUnit(int unitIdx);
  202. /**
  203. * primitiveProcessorTextures must contain GrPrimitiveProcessor::numTextureSamplers() *
  204. * numPrimitiveProcessorTextureSets entries.
  205. */
  206. void resolveAndGenerateMipMapsForProcessorTextures(
  207. const GrPrimitiveProcessor&, const GrPipeline&,
  208. const GrTextureProxy* const primitiveProcessorTextures[],
  209. int numPrimitiveProcessorTextureSets);
  210. // Flushes state from GrPipeline to GL. Returns false if the state couldn't be set.
  211. // willDrawPoints must be true if point primitives will be rendered after setting the GL state.
  212. // If DynamicStateArrays is not null then dynamicStateArraysLength is the number of dynamic
  213. // state entries in each array.
  214. bool flushGLState(GrRenderTarget*, GrSurfaceOrigin, const GrPrimitiveProcessor&,
  215. const GrPipeline&, const GrPipeline::FixedDynamicState*,
  216. const GrPipeline::DynamicStateArrays*, int dynamicStateArraysLength,
  217. bool willDrawPoints);
  218. void flushProgram(sk_sp<GrGLProgram>);
  219. // Version for programs that aren't GrGLProgram.
  220. void flushProgram(GrGLuint);
  221. // Sets up vertex/instance attribute pointers and strides.
  222. void setupGeometry(const GrBuffer* indexBuffer,
  223. const GrBuffer* vertexBuffer,
  224. int baseVertex,
  225. const GrBuffer* instanceBuffer,
  226. int baseInstance,
  227. GrPrimitiveRestart);
  228. void flushBlendAndColorWrite(const GrXferProcessor::BlendInfo& blendInfo, const GrSwizzle&);
  229. void onFinishFlush(GrSurfaceProxy*[], int n, SkSurface::BackendSurfaceAccess access,
  230. const GrFlushInfo&, const GrPrepareForExternalIORequests&) override;
  231. bool waitSync(GrGLsync, uint64_t timeout, bool flush);
  232. bool copySurfaceAsDraw(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  233. const SkIPoint& dstPoint);
  234. void copySurfaceAsCopyTexSubImage(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  235. const SkIPoint& dstPoint);
  236. bool copySurfaceAsBlitFramebuffer(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  237. const SkIPoint& dstPoint);
  238. static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
  239. class ProgramCache : public ::SkNoncopyable {
  240. public:
  241. ProgramCache(GrGLGpu* gpu);
  242. ~ProgramCache();
  243. void abandon();
  244. void reset();
  245. GrGLProgram* refProgram(GrGLGpu*, GrRenderTarget*, GrSurfaceOrigin,
  246. const GrPrimitiveProcessor&,
  247. const GrTextureProxy* const primProcProxies[],
  248. const GrPipeline&, bool hasPointSize);
  249. private:
  250. // We may actually have kMaxEntries+1 shaders in the GL context because we create a new
  251. // shader before evicting from the cache.
  252. static const int kMaxEntries = 128;
  253. struct Entry;
  254. // binary search for entry matching desc. returns index into fEntries that matches desc or ~
  255. // of the index of where it should be inserted.
  256. int search(const GrProgramDesc& desc) const;
  257. struct DescHash {
  258. uint32_t operator()(const GrProgramDesc& desc) const {
  259. return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
  260. }
  261. };
  262. SkLRUCache<GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap;
  263. GrGLGpu* fGpu;
  264. #ifdef PROGRAM_CACHE_STATS
  265. int fTotalRequests;
  266. int fCacheMisses;
  267. int fHashMisses; // cache hit but hash table missed
  268. #endif
  269. };
  270. void flushColorWrite(bool writeColor);
  271. void flushClearColor(GrGLfloat r, GrGLfloat g, GrGLfloat b, GrGLfloat a);
  272. // flushes the scissor. see the note on flushBoundTextureAndParams about
  273. // flushing the scissor after that function is called.
  274. void flushScissor(const GrScissorState&, int rtWidth, int rtHeight, GrSurfaceOrigin rtOrigin);
  275. // disables the scissor
  276. void disableScissor();
  277. void flushWindowRectangles(const GrWindowRectsState&, const GrGLRenderTarget*, GrSurfaceOrigin);
  278. void disableWindowRectangles();
  279. int numTextureUnits() const { return this->caps()->shaderCaps()->maxFragmentSamplers(); }
  280. // Binds a texture to a target on the "scratch" texture unit to use for texture operations
  281. // other than usual draw flow (i.e. a GrGLProgram derived from a GrPipeline used to draw
  282. // GrMesh). It ensures that such operations don't negatively interact with draws.
  283. // The active texture unit and the binding for 'target' will change.
  284. void bindTextureToScratchUnit(GrGLenum target, GrGLint textureID);
  285. // The passed bounds contains the render target's color values that will subsequently be
  286. // written.
  287. void flushRenderTarget(GrGLRenderTarget*, GrSurfaceOrigin, const SkIRect& bounds);
  288. // This version has an implicit bounds of the entire render target.
  289. void flushRenderTarget(GrGLRenderTarget*);
  290. // This version can be used when the render target's colors will not be written.
  291. void flushRenderTargetNoColorWrites(GrGLRenderTarget*);
  292. // Need not be called if flushRenderTarget is used.
  293. void flushViewport(int width, int height);
  294. void flushStencil(const GrStencilSettings&, GrSurfaceOrigin);
  295. void disableStencil();
  296. // rt is used only if useHWAA is true.
  297. void flushHWAAState(GrRenderTarget* rt, bool useHWAA);
  298. void flushFramebufferSRGB(bool enable);
  299. // helper for onCreateTexture and writeTexturePixels
  300. enum UploadType {
  301. kNewTexture_UploadType, // we are creating a new texture
  302. kWrite_UploadType, // we are using TexSubImage2D to copy data to an existing texture
  303. };
  304. bool uploadTexData(GrPixelConfig texConfig, int texWidth, int texHeight, GrGLenum target,
  305. UploadType uploadType, int left, int top, int width, int height,
  306. GrPixelConfig dataConfig, const GrMipLevel texels[], int mipLevelCount,
  307. GrMipMapsStatus* mipMapsStatus = nullptr);
  308. // Helper for onCreateCompressedTexture. Compressed textures are read-only so we
  309. // only use this to populate a new texture. Returns the internal format of the texture
  310. // or 0 on failure.
  311. GrGLenum uploadCompressedTexData(SkImage::CompressionType, int width, int height,
  312. GrGLenum target, const void* data);
  313. bool createRenderTargetObjects(const GrSurfaceDesc&, int sampleCount,
  314. const GrGLTextureInfo& texInfo, GrGLRenderTarget::IDDesc*);
  315. enum TempFBOTarget {
  316. kSrc_TempFBOTarget,
  317. kDst_TempFBOTarget
  318. };
  319. // Binds a surface as a FBO for copying, reading, or clearing. If the surface already owns an
  320. // FBO ID then that ID is bound. If not the surface is temporarily bound to a FBO and that FBO
  321. // is bound. This must be paired with a call to unbindSurfaceFBOForPixelOps().
  322. void bindSurfaceFBOForPixelOps(GrSurface* surface, GrGLenum fboTarget,
  323. TempFBOTarget tempFBOTarget);
  324. // Must be called if bindSurfaceFBOForPixelOps was used to bind a surface for copying.
  325. void unbindTextureFBOForPixelOps(GrGLenum fboTarget, GrSurface* surface);
  326. #ifdef SK_ENABLE_DUMP_GPU
  327. void onDumpJSON(SkJSONWriter*) const override;
  328. #endif
  329. bool createCopyProgram(GrTexture* srcTexture);
  330. bool createMipmapProgram(int progIdx);
  331. std::unique_ptr<GrGLContext> fGLContext;
  332. // GL program-related state
  333. ProgramCache* fProgramCache;
  334. ///////////////////////////////////////////////////////////////////////////
  335. ///@name Caching of GL State
  336. ///@{
  337. int fHWActiveTextureUnitIdx;
  338. GrGLuint fHWProgramID;
  339. sk_sp<GrGLProgram> fHWProgram;
  340. enum TriState {
  341. kNo_TriState,
  342. kYes_TriState,
  343. kUnknown_TriState
  344. };
  345. GrGLuint fTempSrcFBOID;
  346. GrGLuint fTempDstFBOID;
  347. GrGLuint fStencilClearFBOID;
  348. // last scissor / viewport scissor state seen by the GL.
  349. struct {
  350. TriState fEnabled;
  351. GrGLIRect fRect;
  352. void invalidate() {
  353. fEnabled = kUnknown_TriState;
  354. fRect.invalidate();
  355. }
  356. } fHWScissorSettings;
  357. class {
  358. public:
  359. bool valid() const { return kInvalidSurfaceOrigin != fRTOrigin; }
  360. void invalidate() { fRTOrigin = kInvalidSurfaceOrigin; }
  361. bool knownDisabled() const { return this->valid() && !fWindowState.enabled(); }
  362. void setDisabled() {
  363. fRTOrigin = kTopLeft_GrSurfaceOrigin;
  364. fWindowState.setDisabled();
  365. }
  366. void set(GrSurfaceOrigin rtOrigin, int width, int height,
  367. const GrWindowRectsState& windowState) {
  368. fRTOrigin = rtOrigin;
  369. fWidth = width;
  370. fHeight = height;
  371. fWindowState = windowState;
  372. }
  373. bool knownEqualTo(GrSurfaceOrigin rtOrigin, int width, int height,
  374. const GrWindowRectsState& windowState) const {
  375. if (!this->valid()) {
  376. return false;
  377. }
  378. if (fWindowState.numWindows() &&
  379. (fRTOrigin != rtOrigin || fWidth != width || fHeight != height)) {
  380. return false;
  381. }
  382. return fWindowState == windowState;
  383. }
  384. private:
  385. enum { kInvalidSurfaceOrigin = -1 };
  386. int fRTOrigin;
  387. int fWidth;
  388. int fHeight;
  389. GrWindowRectsState fWindowState;
  390. } fHWWindowRectsState;
  391. GrGLIRect fHWViewport;
  392. /**
  393. * Tracks vertex attrib array state.
  394. */
  395. class HWVertexArrayState {
  396. public:
  397. HWVertexArrayState() : fCoreProfileVertexArray(nullptr) { this->invalidate(); }
  398. ~HWVertexArrayState() { delete fCoreProfileVertexArray; }
  399. void invalidate() {
  400. fBoundVertexArrayIDIsValid = false;
  401. fDefaultVertexArrayAttribState.invalidate();
  402. if (fCoreProfileVertexArray) {
  403. fCoreProfileVertexArray->invalidateCachedState();
  404. }
  405. }
  406. void notifyVertexArrayDelete(GrGLuint id) {
  407. if (fBoundVertexArrayIDIsValid && fBoundVertexArrayID == id) {
  408. // Does implicit bind to 0
  409. fBoundVertexArrayID = 0;
  410. }
  411. }
  412. void setVertexArrayID(GrGLGpu* gpu, GrGLuint arrayID) {
  413. if (!gpu->glCaps().vertexArrayObjectSupport()) {
  414. SkASSERT(0 == arrayID);
  415. return;
  416. }
  417. if (!fBoundVertexArrayIDIsValid || arrayID != fBoundVertexArrayID) {
  418. GR_GL_CALL(gpu->glInterface(), BindVertexArray(arrayID));
  419. fBoundVertexArrayIDIsValid = true;
  420. fBoundVertexArrayID = arrayID;
  421. }
  422. }
  423. /**
  424. * Binds the vertex array that should be used for internal draws, and returns its attrib
  425. * state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
  426. * case we use a dummy array instead.
  427. *
  428. * If an index buffer is privided, it will be bound to the vertex array. Otherwise the
  429. * index buffer binding will be left unchanged.
  430. *
  431. * The returned GrGLAttribArrayState should be used to set vertex attribute arrays.
  432. */
  433. GrGLAttribArrayState* bindInternalVertexArray(GrGLGpu*, const GrBuffer* ibuff = nullptr);
  434. private:
  435. GrGLuint fBoundVertexArrayID;
  436. bool fBoundVertexArrayIDIsValid;
  437. // We return a non-const pointer to this from bindArrayAndBuffersToDraw when vertex array 0
  438. // is bound. However, this class is internal to GrGLGpu and this object never leaks out of
  439. // GrGLGpu.
  440. GrGLAttribArrayState fDefaultVertexArrayAttribState;
  441. // This is used when we're using a core profile.
  442. GrGLVertexArray* fCoreProfileVertexArray;
  443. } fHWVertexArrayState;
  444. struct {
  445. GrGLenum fGLTarget;
  446. GrGpuResource::UniqueID fBoundBufferUniqueID;
  447. bool fBufferZeroKnownBound;
  448. void invalidate() {
  449. fBoundBufferUniqueID.makeInvalid();
  450. fBufferZeroKnownBound = false;
  451. }
  452. } fHWBufferState[kGrGpuBufferTypeCount];
  453. auto* hwBufferState(GrGpuBufferType type) {
  454. unsigned typeAsUInt = static_cast<unsigned>(type);
  455. SkASSERT(typeAsUInt < SK_ARRAY_COUNT(fHWBufferState));
  456. return &fHWBufferState[typeAsUInt];
  457. }
  458. struct {
  459. GrBlendEquation fEquation;
  460. GrBlendCoeff fSrcCoeff;
  461. GrBlendCoeff fDstCoeff;
  462. SkPMColor4f fConstColor;
  463. bool fConstColorValid;
  464. TriState fEnabled;
  465. void invalidate() {
  466. fEquation = kIllegal_GrBlendEquation;
  467. fSrcCoeff = kIllegal_GrBlendCoeff;
  468. fDstCoeff = kIllegal_GrBlendCoeff;
  469. fConstColorValid = false;
  470. fEnabled = kUnknown_TriState;
  471. }
  472. } fHWBlendState;
  473. TriState fMSAAEnabled;
  474. GrStencilSettings fHWStencilSettings;
  475. GrSurfaceOrigin fHWStencilOrigin;
  476. TriState fHWStencilTestEnabled;
  477. TriState fHWWriteToColor;
  478. GrGpuResource::UniqueID fHWBoundRenderTargetUniqueID;
  479. TriState fHWSRGBFramebuffer;
  480. class TextureUnitBindings {
  481. public:
  482. TextureUnitBindings() = default;
  483. TextureUnitBindings(const TextureUnitBindings&) = delete;
  484. TextureUnitBindings& operator=(const TextureUnitBindings&) = delete;
  485. GrGpuResource::UniqueID boundID(GrGLenum target) const;
  486. bool hasBeenModified(GrGLenum target) const;
  487. void setBoundID(GrGLenum target, GrGpuResource::UniqueID);
  488. void invalidateForScratchUse(GrGLenum target);
  489. void invalidateAllTargets(bool markUnmodified);
  490. private:
  491. struct TargetBinding {
  492. GrGpuResource::UniqueID fBoundResourceID;
  493. bool fHasBeenModified = false;
  494. };
  495. TargetBinding fTargetBindings[3];
  496. };
  497. SkAutoTArray<TextureUnitBindings> fHWTextureUnitBindings;
  498. GrGLfloat fHWClearColor[4];
  499. GrGLuint fBoundDrawFramebuffer = 0;
  500. /** IDs for copy surface program. (3 sampler types) */
  501. struct {
  502. GrGLuint fProgram = 0;
  503. GrGLint fTextureUniform = 0;
  504. GrGLint fTexCoordXformUniform = 0;
  505. GrGLint fPosXformUniform = 0;
  506. } fCopyPrograms[3];
  507. sk_sp<GrGLBuffer> fCopyProgramArrayBuffer;
  508. /** IDs for texture mipmap program. (4 filter configurations) */
  509. struct {
  510. GrGLuint fProgram = 0;
  511. GrGLint fTextureUniform = 0;
  512. GrGLint fTexCoordXformUniform = 0;
  513. } fMipmapPrograms[4];
  514. sk_sp<GrGLBuffer> fMipmapProgramArrayBuffer;
  515. static int TextureToCopyProgramIdx(GrTexture* texture);
  516. static int TextureSizeToMipmapProgramIdx(int width, int height) {
  517. const bool wide = (width > 1) && SkToBool(width & 0x1);
  518. const bool tall = (height > 1) && SkToBool(height & 0x1);
  519. return (wide ? 0x2 : 0x0) | (tall ? 0x1 : 0x0);
  520. }
  521. GrPrimitiveType fLastPrimitiveType;
  522. GrGLTextureParameters::ResetTimestamp fResetTimestampForTextureParameters = 0;
  523. class SamplerObjectCache;
  524. std::unique_ptr<SamplerObjectCache> fSamplerObjectCache;
  525. std::unique_ptr<GrGLGpuRTCommandBuffer> fCachedRTCommandBuffer;
  526. std::unique_ptr<GrGLGpuTextureCommandBuffer> fCachedTexCommandBuffer;
  527. struct FinishCallback {
  528. GrGpuFinishedProc fCallback;
  529. GrGpuFinishedContext fContext;
  530. GrGLsync fSync;
  531. };
  532. std::list<FinishCallback> fFinishCallbacks;
  533. friend class GrGLPathRendering; // For accessing setTextureUnit.
  534. typedef GrGpu INHERITED;
  535. };
  536. #endif