WebGLContext.h 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef WEBGLCONTEXT_H_
  6. #define WEBGLCONTEXT_H_
  7. #include <stdarg.h>
  8. #include "GLContextTypes.h"
  9. #include "GLDefs.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/CheckedInt.h"
  12. #include "mozilla/dom/BindingDeclarations.h"
  13. #include "mozilla/dom/HTMLCanvasElement.h"
  14. #include "mozilla/dom/TypedArray.h"
  15. #include "mozilla/EnumeratedArray.h"
  16. #include "mozilla/ErrorResult.h"
  17. #include "mozilla/gfx/2D.h"
  18. #include "mozilla/LinkedList.h"
  19. #include "mozilla/UniquePtr.h"
  20. #include "mozilla/WeakPtr.h"
  21. #include "nsCycleCollectionNoteChild.h"
  22. #include "nsICanvasRenderingContextInternal.h"
  23. #include "nsLayoutUtils.h"
  24. #include "nsTArray.h"
  25. #include "nsWrapperCache.h"
  26. #include "SurfaceTypes.h"
  27. #include "ScopedGLHelpers.h"
  28. #include "TexUnpackBlob.h"
  29. // Local
  30. #include "WebGLContextLossHandler.h"
  31. #include "WebGLContextUnchecked.h"
  32. #include "WebGLFormats.h"
  33. #include "WebGLObjectModel.h"
  34. #include "WebGLStrongTypes.h"
  35. #include "WebGLTexture.h"
  36. // Generated
  37. #include "nsIDOMEventListener.h"
  38. #include "nsIDOMWebGLRenderingContext.h"
  39. #include "nsICanvasRenderingContextInternal.h"
  40. #include "nsIObserver.h"
  41. #include "mozilla/dom/HTMLCanvasElement.h"
  42. #include "nsWrapperCache.h"
  43. #include "nsLayoutUtils.h"
  44. #include "mozilla/dom/WebGLRenderingContextBinding.h"
  45. #include "mozilla/dom/WebGL2RenderingContextBinding.h"
  46. class nsIDocShell;
  47. /*
  48. * Minimum value constants defined in 6.2 State Tables of OpenGL ES - 2.0.25
  49. * https://bugzilla.mozilla.org/show_bug.cgi?id=686732
  50. *
  51. * Exceptions: some of the following values are set to higher values than in the spec because
  52. * the values in the spec are ridiculously low. They are explicitly marked below
  53. */
  54. #define MINVALUE_GL_MAX_TEXTURE_SIZE 1024 // Different from the spec, which sets it to 64 on page 162
  55. #define MINVALUE_GL_MAX_CUBE_MAP_TEXTURE_SIZE 512 // Different from the spec, which sets it to 16 on page 162
  56. #define MINVALUE_GL_MAX_VERTEX_ATTRIBS 8 // Page 164
  57. #define MINVALUE_GL_MAX_FRAGMENT_UNIFORM_VECTORS 16 // Page 164
  58. #define MINVALUE_GL_MAX_VERTEX_UNIFORM_VECTORS 128 // Page 164
  59. #define MINVALUE_GL_MAX_VARYING_VECTORS 8 // Page 164
  60. #define MINVALUE_GL_MAX_TEXTURE_IMAGE_UNITS 8 // Page 164
  61. #define MINVALUE_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 0 // Page 164
  62. #define MINVALUE_GL_MAX_RENDERBUFFER_SIZE 1024 // Different from the spec, which sets it to 1 on page 164
  63. #define MINVALUE_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 8 // Page 164
  64. /*
  65. * Minimum value constants define in 6.2 State Tables of OpenGL ES - 3.0.4
  66. */
  67. #define MINVALUE_GL_MAX_3D_TEXTURE_SIZE 256
  68. #define MINVALUE_GL_MAX_ARRAY_TEXTURE_LAYERS 256
  69. /*
  70. * WebGL-only GLenums
  71. */
  72. #define LOCAL_GL_BROWSER_DEFAULT_WEBGL 0x9244
  73. #define LOCAL_GL_CONTEXT_LOST_WEBGL 0x9242
  74. #define LOCAL_GL_MAX_CLIENT_WAIT_TIMEOUT_WEBGL 0x9247
  75. #define LOCAL_GL_UNPACK_COLORSPACE_CONVERSION_WEBGL 0x9243
  76. #define LOCAL_GL_UNPACK_FLIP_Y_WEBGL 0x9240
  77. #define LOCAL_GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL 0x9241
  78. namespace mozilla {
  79. class ScopedCopyTexImageSource;
  80. class ScopedResolveTexturesForDraw;
  81. class ScopedUnpackReset;
  82. class WebGLActiveInfo;
  83. class WebGLBuffer;
  84. class WebGLExtensionBase;
  85. class WebGLFramebuffer;
  86. class WebGLProgram;
  87. class WebGLQuery;
  88. class WebGLRenderbuffer;
  89. class WebGLSampler;
  90. class WebGLShader;
  91. class WebGLShaderPrecisionFormat;
  92. class WebGLSync;
  93. class WebGLTexture;
  94. class WebGLTransformFeedback;
  95. class WebGLUniformLocation;
  96. class WebGLVertexArray;
  97. namespace dom {
  98. class Element;
  99. class ImageData;
  100. class OwningHTMLCanvasElementOrOffscreenCanvas;
  101. struct WebGLContextAttributes;
  102. template<typename> struct Nullable;
  103. } // namespace dom
  104. namespace gfx {
  105. class SourceSurface;
  106. class VRLayerChild;
  107. } // namespace gfx
  108. namespace webgl {
  109. struct LinkedProgramInfo;
  110. class ShaderValidator;
  111. class TexUnpackBlob;
  112. struct UniformInfo;
  113. struct UniformBlockInfo;
  114. } // namespace webgl
  115. WebGLTexelFormat GetWebGLTexelFormat(TexInternalFormat format);
  116. void AssertUintParamCorrect(gl::GLContext* gl, GLenum pname, GLuint shadow);
  117. struct WebGLContextOptions
  118. {
  119. // these are defaults
  120. WebGLContextOptions();
  121. bool operator==(const WebGLContextOptions& other) const {
  122. return
  123. alpha == other.alpha &&
  124. depth == other.depth &&
  125. stencil == other.stencil &&
  126. premultipliedAlpha == other.premultipliedAlpha &&
  127. antialias == other.antialias &&
  128. preserveDrawingBuffer == other.preserveDrawingBuffer;
  129. }
  130. bool operator!=(const WebGLContextOptions& other) const {
  131. return !operator==(other);
  132. }
  133. bool alpha;
  134. bool depth;
  135. bool stencil;
  136. bool premultipliedAlpha;
  137. bool antialias;
  138. bool preserveDrawingBuffer;
  139. bool failIfMajorPerformanceCaveat;
  140. };
  141. // From WebGLContextUtils
  142. TexTarget TexImageTargetToTexTarget(TexImageTarget texImageTarget);
  143. struct WebGLIntOrFloat {
  144. const enum {
  145. Int,
  146. Float,
  147. Uint
  148. } mType;
  149. union {
  150. GLint i;
  151. GLfloat f;
  152. GLuint u;
  153. } mValue;
  154. explicit WebGLIntOrFloat(GLint i) : mType(Int) { mValue.i = i; }
  155. explicit WebGLIntOrFloat(GLfloat f) : mType(Float) { mValue.f = f; }
  156. GLint AsInt() const { return (mType == Int) ? mValue.i : NS_lroundf(mValue.f); }
  157. GLfloat AsFloat() const { return (mType == Float) ? mValue.f : GLfloat(mValue.i); }
  158. };
  159. struct IndexedBufferBinding
  160. {
  161. WebGLRefPtr<WebGLBuffer> mBufferBinding;
  162. uint64_t mRangeStart;
  163. uint64_t mRangeSize;
  164. IndexedBufferBinding();
  165. uint64_t ByteCount() const;
  166. };
  167. ////
  168. struct FloatOrInt final // For TexParameter[fi] and friends.
  169. {
  170. const bool isFloat;
  171. const GLfloat f;
  172. const GLint i;
  173. explicit FloatOrInt(GLint x)
  174. : isFloat(false)
  175. , f(x)
  176. , i(x)
  177. { }
  178. explicit FloatOrInt(GLfloat x)
  179. : isFloat(true)
  180. , f(x)
  181. , i(roundf(x))
  182. { }
  183. FloatOrInt& operator =(const FloatOrInt& x) {
  184. memcpy(this, &x, sizeof(x));
  185. return *this;
  186. }
  187. };
  188. ////////////////////////////////////
  189. struct TexImageSource
  190. {
  191. const dom::ArrayBufferView* mView;
  192. GLuint mViewElemOffset;
  193. GLuint mViewElemLengthOverride;
  194. const WebGLsizeiptr* mPboOffset;
  195. const dom::ImageBitmap* mImageBitmap;
  196. const dom::ImageData* mImageData;
  197. const dom::Element* mDomElem;
  198. ErrorResult* mOut_error;
  199. protected:
  200. TexImageSource() {
  201. memset(this, 0, sizeof(*this));
  202. }
  203. };
  204. ////
  205. struct TexImageSourceAdapter final : public TexImageSource
  206. {
  207. TexImageSourceAdapter(const dom::Nullable<dom::ArrayBufferView>* maybeView,
  208. ErrorResult*)
  209. {
  210. if (!maybeView->IsNull()) {
  211. mView = &(maybeView->Value());
  212. }
  213. }
  214. TexImageSourceAdapter(const dom::ArrayBufferView* view, ErrorResult*) {
  215. mView = view;
  216. }
  217. TexImageSourceAdapter(const dom::ArrayBufferView* view, GLuint viewElemOffset,
  218. GLuint viewElemLengthOverride = 0)
  219. {
  220. mView = view;
  221. mViewElemOffset = viewElemOffset;
  222. mViewElemLengthOverride = viewElemLengthOverride;
  223. }
  224. TexImageSourceAdapter(const WebGLsizeiptr* pboOffset, GLuint ignored1, GLuint ignored2 = 0) {
  225. mPboOffset = pboOffset;
  226. }
  227. TexImageSourceAdapter(const WebGLsizeiptr* pboOffset, ErrorResult* ignored) {
  228. mPboOffset = pboOffset;
  229. }
  230. TexImageSourceAdapter(const dom::ImageBitmap* imageBitmap, ErrorResult* out_error) {
  231. mImageBitmap = imageBitmap;
  232. mOut_error = out_error;
  233. }
  234. TexImageSourceAdapter(const dom::ImageData* imageData, ErrorResult*) {
  235. mImageData = imageData;
  236. }
  237. TexImageSourceAdapter(const dom::Element* domElem, ErrorResult* const out_error) {
  238. mDomElem = domElem;
  239. mOut_error = out_error;
  240. }
  241. };
  242. ////////////////////////////////////////////////////////////////////////////////
  243. class WebGLContext
  244. : public nsIDOMWebGLRenderingContext
  245. , public nsICanvasRenderingContextInternal
  246. , public nsSupportsWeakReference
  247. , public WebGLContextUnchecked
  248. , public WebGLRectangleObject
  249. , public nsWrapperCache
  250. , public SupportsWeakPtr<WebGLContext>
  251. {
  252. friend class ScopedDrawHelper;
  253. friend class ScopedDrawWithTransformFeedback;
  254. friend class ScopedFBRebinder;
  255. friend class WebGL2Context;
  256. friend class WebGLContextUserData;
  257. friend class WebGLExtensionCompressedTextureATC;
  258. friend class WebGLExtensionCompressedTextureES3;
  259. friend class WebGLExtensionCompressedTextureETC1;
  260. friend class WebGLExtensionCompressedTexturePVRTC;
  261. friend class WebGLExtensionCompressedTextureS3TC;
  262. friend class WebGLExtensionDepthTexture;
  263. friend class WebGLExtensionDisjointTimerQuery;
  264. friend class WebGLExtensionDrawBuffers;
  265. friend class WebGLExtensionLoseContext;
  266. friend class WebGLExtensionVertexArray;
  267. friend class WebGLMemoryTracker;
  268. friend struct webgl::UniformBlockInfo;
  269. enum {
  270. UNPACK_FLIP_Y_WEBGL = 0x9240,
  271. UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241,
  272. CONTEXT_LOST_WEBGL = 0x9242,
  273. UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243,
  274. BROWSER_DEFAULT_WEBGL = 0x9244,
  275. UNMASKED_VENDOR_WEBGL = 0x9245,
  276. UNMASKED_RENDERER_WEBGL = 0x9246
  277. };
  278. static const uint32_t kMinMaxColorAttachments;
  279. static const uint32_t kMinMaxDrawBuffers;
  280. public:
  281. WebGLContext();
  282. protected:
  283. virtual ~WebGLContext();
  284. public:
  285. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  286. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WebGLContext,
  287. nsIDOMWebGLRenderingContext)
  288. MOZ_DECLARE_WEAKREFERENCE_TYPENAME(WebGLContext)
  289. virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) override = 0;
  290. NS_DECL_NSIDOMWEBGLRENDERINGCONTEXT
  291. virtual void OnVisibilityChange() override;
  292. virtual void OnMemoryPressure() override;
  293. // nsICanvasRenderingContextInternal
  294. virtual int32_t GetWidth() const override;
  295. virtual int32_t GetHeight() const override;
  296. NS_IMETHOD SetDimensions(int32_t width, int32_t height) override;
  297. NS_IMETHOD InitializeWithDrawTarget(nsIDocShell*,
  298. NotNull<gfx::DrawTarget*>) override
  299. {
  300. return NS_ERROR_NOT_IMPLEMENTED;
  301. }
  302. NS_IMETHOD Reset() override {
  303. /* (InitializeWithSurface) */
  304. return NS_ERROR_NOT_IMPLEMENTED;
  305. }
  306. virtual UniquePtr<uint8_t[]> GetImageBuffer(int32_t* out_format) override;
  307. NS_IMETHOD GetInputStream(const char* mimeType,
  308. const char16_t* encoderOptions,
  309. nsIInputStream** out_stream) override;
  310. already_AddRefed<mozilla::gfx::SourceSurface>
  311. GetSurfaceSnapshot(bool* out_premultAlpha) override;
  312. NS_IMETHOD SetIsOpaque(bool) override { return NS_OK; };
  313. bool GetIsOpaque() override { return false; }
  314. NS_IMETHOD SetContextOptions(JSContext* cx,
  315. JS::Handle<JS::Value> options,
  316. ErrorResult& aRvForDictionaryInit) override;
  317. NS_IMETHOD SetIsIPC(bool) override {
  318. return NS_ERROR_NOT_IMPLEMENTED;
  319. }
  320. /**
  321. * An abstract base class to be implemented by callers wanting to be notified
  322. * that a refresh has occurred. Callers must ensure an observer is removed
  323. * before it is destroyed.
  324. */
  325. virtual void DidRefresh() override;
  326. NS_IMETHOD Redraw(const gfxRect&) override {
  327. return NS_ERROR_NOT_IMPLEMENTED;
  328. }
  329. void SynthesizeGLError(GLenum err);
  330. void SynthesizeGLError(GLenum err, const char* fmt, ...);
  331. void ErrorInvalidEnum(const char* fmt = 0, ...);
  332. void ErrorInvalidOperation(const char* fmt = 0, ...);
  333. void ErrorInvalidValue(const char* fmt = 0, ...);
  334. void ErrorInvalidFramebufferOperation(const char* fmt = 0, ...);
  335. void ErrorInvalidEnumInfo(const char* info, GLenum enumValue);
  336. void ErrorInvalidEnumInfo(const char* info, const char* funcName,
  337. GLenum enumValue);
  338. void ErrorOutOfMemory(const char* fmt = 0, ...);
  339. void ErrorImplementationBug(const char* fmt = 0, ...);
  340. void ErrorInvalidEnumArg(const char* funcName, const char* argName, GLenum val);
  341. const char* ErrorName(GLenum error);
  342. /**
  343. * Return displayable name for GLenum.
  344. * This version is like gl::GLenumToStr but with out the GL_ prefix to
  345. * keep consistency with how errors are reported from WebGL.
  346. * Returns hex formatted version of glenum if glenum is unknown.
  347. */
  348. static void EnumName(GLenum val, nsCString* out_name);
  349. void DummyReadFramebufferOperation(const char* funcName);
  350. WebGLTexture* ActiveBoundTextureForTarget(const TexTarget texTarget) const {
  351. switch (texTarget.get()) {
  352. case LOCAL_GL_TEXTURE_2D:
  353. return mBound2DTextures[mActiveTexture];
  354. case LOCAL_GL_TEXTURE_CUBE_MAP:
  355. return mBoundCubeMapTextures[mActiveTexture];
  356. case LOCAL_GL_TEXTURE_3D:
  357. return mBound3DTextures[mActiveTexture];
  358. case LOCAL_GL_TEXTURE_2D_ARRAY:
  359. return mBound2DArrayTextures[mActiveTexture];
  360. default:
  361. MOZ_CRASH("GFX: bad target");
  362. }
  363. }
  364. /* Use this function when you have the texture image target, for example:
  365. * GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_[POSITIVE|NEGATIVE]_[X|Y|Z], and
  366. * not the actual texture binding target: GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.
  367. */
  368. WebGLTexture*
  369. ActiveBoundTextureForTexImageTarget(const TexImageTarget texImgTarget) const
  370. {
  371. const TexTarget texTarget = TexImageTargetToTexTarget(texImgTarget);
  372. return ActiveBoundTextureForTarget(texTarget);
  373. }
  374. void InvalidateResolveCacheForTextureWithTexUnit(const GLuint);
  375. already_AddRefed<Layer>
  376. GetCanvasLayer(nsDisplayListBuilder* builder, Layer* oldLayer,
  377. LayerManager* manager,
  378. bool aMirror = false) override;
  379. // Note that 'clean' here refers to its invalidation state, not the
  380. // contents of the buffer.
  381. void MarkContextClean() override { mInvalidated = false; }
  382. void MarkContextCleanForFrameCapture() override { mCapturedFrameInvalidated = false; }
  383. bool IsContextCleanForFrameCapture() override { return !mCapturedFrameInvalidated; }
  384. gl::GLContext* GL() const { return gl; }
  385. bool IsPremultAlpha() const { return mOptions.premultipliedAlpha; }
  386. bool IsPreservingDrawingBuffer() const { return mOptions.preserveDrawingBuffer; }
  387. bool PresentScreenBuffer();
  388. // Prepare the context for capture before compositing
  389. void BeginComposition();
  390. // Clean up the context after captured for compositing
  391. void EndComposition();
  392. // a number that increments every time we have an event that causes
  393. // all context resources to be lost.
  394. uint32_t Generation() const { return mGeneration.value(); }
  395. // This is similar to GLContext::ClearSafely, but tries to minimize the
  396. // amount of work it does.
  397. // It only clears the buffers we specify, and can reset its state without
  398. // first having to query anything, as WebGL knows its state at all times.
  399. void ForceClearFramebufferWithDefaultValues(GLbitfield bufferBits, bool fakeNoAlpha);
  400. // Calls ForceClearFramebufferWithDefaultValues() for the Context's 'screen'.
  401. void ClearScreen();
  402. void ClearBackbufferIfNeeded();
  403. bool MinCapabilityMode() const { return mMinCapability; }
  404. void RunContextLossTimer();
  405. void UpdateContextLossStatus();
  406. void EnqueueUpdateContextLossStatus();
  407. bool TryToRestoreContext();
  408. void AssertCachedBindings();
  409. void AssertCachedGlobalState();
  410. dom::HTMLCanvasElement* GetCanvas() const { return mCanvasElement; }
  411. // WebIDL WebGLRenderingContext API
  412. void Commit();
  413. void GetCanvas(Nullable<dom::OwningHTMLCanvasElementOrOffscreenCanvas>& retval);
  414. GLsizei DrawingBufferWidth() const { return IsContextLost() ? 0 : mWidth; }
  415. GLsizei DrawingBufferHeight() const {
  416. return IsContextLost() ? 0 : mHeight;
  417. }
  418. layers::LayersBackend GetCompositorBackendType() const;
  419. void
  420. GetContextAttributes(dom::Nullable<dom::WebGLContextAttributes>& retval);
  421. bool IsContextLost() const { return mContextStatus != ContextNotLost; }
  422. void GetSupportedExtensions(dom::Nullable< nsTArray<nsString> >& retval,
  423. dom::CallerType callerType);
  424. void GetExtension(JSContext* cx, const nsAString& name,
  425. JS::MutableHandle<JSObject*> retval,
  426. dom::CallerType callerType, ErrorResult& rv);
  427. void AttachShader(WebGLProgram& prog, WebGLShader& shader);
  428. void BindAttribLocation(WebGLProgram& prog, GLuint location,
  429. const nsAString& name);
  430. void BindFramebuffer(GLenum target, WebGLFramebuffer* fb);
  431. void BindRenderbuffer(GLenum target, WebGLRenderbuffer* fb);
  432. void BindVertexArray(WebGLVertexArray* vao);
  433. void BlendColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a);
  434. void BlendEquation(GLenum mode);
  435. void BlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha);
  436. void BlendFunc(GLenum sfactor, GLenum dfactor);
  437. void BlendFuncSeparate(GLenum srcRGB, GLenum dstRGB,
  438. GLenum srcAlpha, GLenum dstAlpha);
  439. GLenum CheckFramebufferStatus(GLenum target);
  440. void Clear(GLbitfield mask);
  441. void ClearColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a);
  442. void ClearDepth(GLclampf v);
  443. void ClearStencil(GLint v);
  444. void ColorMask(WebGLboolean r, WebGLboolean g, WebGLboolean b, WebGLboolean a);
  445. void CompileShader(WebGLShader& shader);
  446. void CompileShaderANGLE(WebGLShader* shader);
  447. void CompileShaderBypass(WebGLShader* shader, const nsCString& shaderSource);
  448. already_AddRefed<WebGLFramebuffer> CreateFramebuffer();
  449. already_AddRefed<WebGLProgram> CreateProgram();
  450. already_AddRefed<WebGLRenderbuffer> CreateRenderbuffer();
  451. already_AddRefed<WebGLShader> CreateShader(GLenum type);
  452. already_AddRefed<WebGLVertexArray> CreateVertexArray();
  453. void CullFace(GLenum face);
  454. void DeleteFramebuffer(WebGLFramebuffer* fb);
  455. void DeleteProgram(WebGLProgram* prog);
  456. void DeleteRenderbuffer(WebGLRenderbuffer* rb);
  457. void DeleteShader(WebGLShader* shader);
  458. void DeleteVertexArray(WebGLVertexArray* vao);
  459. void DepthFunc(GLenum func);
  460. void DepthMask(WebGLboolean b);
  461. void DepthRange(GLclampf zNear, GLclampf zFar);
  462. void DetachShader(WebGLProgram& prog, const WebGLShader& shader);
  463. void DrawBuffers(const dom::Sequence<GLenum>& buffers);
  464. void Flush();
  465. void Finish();
  466. void FramebufferRenderbuffer(GLenum target, GLenum attachment,
  467. GLenum rbTarget, WebGLRenderbuffer* rb);
  468. void FramebufferTexture2D(GLenum target, GLenum attachment,
  469. GLenum texImageTarget, WebGLTexture* tex,
  470. GLint level);
  471. void FrontFace(GLenum mode);
  472. already_AddRefed<WebGLActiveInfo> GetActiveAttrib(const WebGLProgram& prog,
  473. GLuint index);
  474. already_AddRefed<WebGLActiveInfo> GetActiveUniform(const WebGLProgram& prog,
  475. GLuint index);
  476. void
  477. GetAttachedShaders(const WebGLProgram& prog,
  478. dom::Nullable<nsTArray<RefPtr<WebGLShader>>>& retval);
  479. GLint GetAttribLocation(const WebGLProgram& prog, const nsAString& name);
  480. JS::Value GetBufferParameter(GLenum target, GLenum pname);
  481. void GetBufferParameter(JSContext*, GLenum target, GLenum pname,
  482. JS::MutableHandle<JS::Value> retval)
  483. {
  484. retval.set(GetBufferParameter(target, pname));
  485. }
  486. GLenum GetError();
  487. virtual JS::Value GetFramebufferAttachmentParameter(JSContext* cx, GLenum target,
  488. GLenum attachment, GLenum pname,
  489. ErrorResult& rv);
  490. void GetFramebufferAttachmentParameter(JSContext* cx, GLenum target,
  491. GLenum attachment, GLenum pname,
  492. JS::MutableHandle<JS::Value> retval,
  493. ErrorResult& rv)
  494. {
  495. retval.set(GetFramebufferAttachmentParameter(cx, target, attachment,
  496. pname, rv));
  497. }
  498. JS::Value GetProgramParameter(const WebGLProgram& prog, GLenum pname);
  499. void GetProgramParameter(JSContext*, const WebGLProgram& prog, GLenum pname,
  500. JS::MutableHandle<JS::Value> retval)
  501. {
  502. retval.set(GetProgramParameter(prog, pname));
  503. }
  504. void GetProgramInfoLog(const WebGLProgram& prog, nsACString& retval);
  505. void GetProgramInfoLog(const WebGLProgram& prog, nsAString& retval);
  506. JS::Value GetRenderbufferParameter(GLenum target, GLenum pname);
  507. void GetRenderbufferParameter(JSContext*, GLenum target, GLenum pname,
  508. JS::MutableHandle<JS::Value> retval)
  509. {
  510. retval.set(GetRenderbufferParameter(target, pname));
  511. }
  512. JS::Value GetShaderParameter(const WebGLShader& shader, GLenum pname);
  513. void GetShaderParameter(JSContext*, const WebGLShader& shader, GLenum pname,
  514. JS::MutableHandle<JS::Value> retval)
  515. {
  516. retval.set(GetShaderParameter(shader, pname));
  517. }
  518. already_AddRefed<WebGLShaderPrecisionFormat>
  519. GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype);
  520. void GetShaderInfoLog(const WebGLShader& shader, nsACString& retval);
  521. void GetShaderInfoLog(const WebGLShader& shader, nsAString& retval);
  522. void GetShaderSource(const WebGLShader& shader, nsAString& retval);
  523. JS::Value GetUniform(JSContext* cx, const WebGLProgram& prog,
  524. const WebGLUniformLocation& loc);
  525. void GetUniform(JSContext* cx, const WebGLProgram& prog,
  526. const WebGLUniformLocation& loc,
  527. JS::MutableHandle<JS::Value> retval)
  528. {
  529. retval.set(GetUniform(cx, prog, loc));
  530. }
  531. already_AddRefed<WebGLUniformLocation>
  532. GetUniformLocation(const WebGLProgram& prog, const nsAString& name);
  533. void Hint(GLenum target, GLenum mode);
  534. bool IsFramebuffer(const WebGLFramebuffer* fb);
  535. bool IsProgram(const WebGLProgram* prog);
  536. bool IsRenderbuffer(const WebGLRenderbuffer* rb);
  537. bool IsShader(const WebGLShader* shader);
  538. bool IsVertexArray(const WebGLVertexArray* vao);
  539. void LineWidth(GLfloat width);
  540. void LinkProgram(WebGLProgram& prog);
  541. void PixelStorei(GLenum pname, GLint param);
  542. void PolygonOffset(GLfloat factor, GLfloat units);
  543. ////
  544. webgl::PackingInfo
  545. ValidImplementationColorReadPI(const webgl::FormatUsageInfo* usage) const;
  546. protected:
  547. bool ReadPixels_SharedPrecheck(ErrorResult* const out_error);
  548. void ReadPixelsImpl(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
  549. GLenum type, void* data, uint32_t dataLen);
  550. bool DoReadPixelsAndConvert(const webgl::FormatInfo* srcFormat, GLint x, GLint y,
  551. GLsizei width, GLsizei height, GLenum format,
  552. GLenum destType, void* dest, uint32_t dataLen,
  553. uint32_t rowStride);
  554. public:
  555. void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
  556. GLenum type, const dom::Nullable<dom::ArrayBufferView>& maybeView,
  557. ErrorResult& rv)
  558. {
  559. const char funcName[] = "readPixels";
  560. if (maybeView.IsNull()) {
  561. ErrorInvalidValue("%s: `pixels` must not be null.", funcName);
  562. return;
  563. }
  564. ReadPixels(x, y, width, height, format, type, maybeView.Value(), 0, rv);
  565. }
  566. void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
  567. GLenum type, WebGLsizeiptr offset, ErrorResult& out_error);
  568. void ReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
  569. GLenum type, const dom::ArrayBufferView& dstData, GLuint dstOffset,
  570. ErrorResult& out_error);
  571. ////
  572. void RenderbufferStorage(GLenum target, GLenum internalFormat,
  573. GLsizei width, GLsizei height);
  574. protected:
  575. void RenderbufferStorage_base(const char* funcName, GLenum target,
  576. GLsizei samples, GLenum internalformat,
  577. GLsizei width, GLsizei height);
  578. public:
  579. void SampleCoverage(GLclampf value, WebGLboolean invert);
  580. void Scissor(GLint x, GLint y, GLsizei width, GLsizei height);
  581. void ShaderSource(WebGLShader& shader, const nsAString& source);
  582. void StencilFunc(GLenum func, GLint ref, GLuint mask);
  583. void StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask);
  584. void StencilMask(GLuint mask);
  585. void StencilMaskSeparate(GLenum face, GLuint mask);
  586. void StencilOp(GLenum sfail, GLenum dpfail, GLenum dppass);
  587. void StencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail,
  588. GLenum dppass);
  589. //////
  590. void Uniform1f(WebGLUniformLocation* loc, GLfloat x);
  591. void Uniform2f(WebGLUniformLocation* loc, GLfloat x, GLfloat y);
  592. void Uniform3f(WebGLUniformLocation* loc, GLfloat x, GLfloat y, GLfloat z);
  593. void Uniform4f(WebGLUniformLocation* loc, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  594. void Uniform1i(WebGLUniformLocation* loc, GLint x);
  595. void Uniform2i(WebGLUniformLocation* loc, GLint x, GLint y);
  596. void Uniform3i(WebGLUniformLocation* loc, GLint x, GLint y, GLint z);
  597. void Uniform4i(WebGLUniformLocation* loc, GLint x, GLint y, GLint z, GLint w);
  598. void Uniform1ui(WebGLUniformLocation* loc, GLuint v0);
  599. void Uniform2ui(WebGLUniformLocation* loc, GLuint v0, GLuint v1);
  600. void Uniform3ui(WebGLUniformLocation* loc, GLuint v0, GLuint v1, GLuint v2);
  601. void Uniform4ui(WebGLUniformLocation* loc, GLuint v0, GLuint v1, GLuint v2,
  602. GLuint v3);
  603. //////////////////////////
  604. typedef dom::Float32ArrayOrUnrestrictedFloatSequence Float32ListU;
  605. typedef dom::Int32ArrayOrLongSequence Int32ListU;
  606. typedef dom::Uint32ArrayOrUnsignedLongSequence Uint32ListU;
  607. protected:
  608. template<typename elemT, typename viewT>
  609. struct Arr {
  610. const size_t elemCount;
  611. const elemT* const elemBytes;
  612. private:
  613. static size_t ComputeAndReturnLength(const viewT& view) {
  614. view.ComputeLengthAndData();
  615. return view.LengthAllowShared();
  616. }
  617. public:
  618. explicit Arr(const viewT& view)
  619. : elemCount(ComputeAndReturnLength(view))
  620. , elemBytes(view.DataAllowShared())
  621. { }
  622. explicit Arr(const dom::Sequence<elemT>& seq)
  623. : elemCount(seq.Length())
  624. , elemBytes(seq.Elements())
  625. { }
  626. Arr(size_t _elemCount, const elemT* _elemBytes)
  627. : elemCount(_elemCount)
  628. , elemBytes(_elemBytes)
  629. { }
  630. ////
  631. static Arr From(const Float32ListU& list) {
  632. if (list.IsFloat32Array())
  633. return Arr(list.GetAsFloat32Array());
  634. return Arr(list.GetAsUnrestrictedFloatSequence());
  635. }
  636. static Arr From(const Int32ListU& list) {
  637. if (list.IsInt32Array())
  638. return Arr(list.GetAsInt32Array());
  639. return Arr(list.GetAsLongSequence());
  640. }
  641. static Arr From(const Uint32ListU& list) {
  642. if (list.IsUint32Array())
  643. return Arr(list.GetAsUint32Array());
  644. return Arr(list.GetAsUnsignedLongSequence());
  645. }
  646. };
  647. typedef Arr<GLfloat, dom::Float32Array> Float32Arr;
  648. typedef Arr<GLint, dom::Int32Array> Int32Arr;
  649. typedef Arr<GLuint, dom::Uint32Array> Uint32Arr;
  650. ////////////////
  651. void UniformNfv(const char* funcName, uint8_t N, WebGLUniformLocation* loc,
  652. const Float32Arr& arr, GLuint elemOffset, GLuint elemCountOverride);
  653. void UniformNiv(const char* funcName, uint8_t N, WebGLUniformLocation* loc,
  654. const Int32Arr& arr, GLuint elemOffset, GLuint elemCountOverride);
  655. void UniformNuiv(const char* funcName, uint8_t N, WebGLUniformLocation* loc,
  656. const Uint32Arr& arr, GLuint elemOffset, GLuint elemCountOverride);
  657. void UniformMatrixAxBfv(const char* funcName, uint8_t A, uint8_t B,
  658. WebGLUniformLocation* loc, bool transpose,
  659. const Float32Arr& arr, GLuint elemOffset,
  660. GLuint elemCountOverride);
  661. ////////////////
  662. public:
  663. #define FOO(N) \
  664. void Uniform ## N ## fv(WebGLUniformLocation* loc, const Float32ListU& list, \
  665. GLuint elemOffset = 0, GLuint elemCountOverride = 0) \
  666. { \
  667. UniformNfv("uniform" #N "fv", N, loc, Float32Arr::From(list), elemOffset, \
  668. elemCountOverride); \
  669. }
  670. FOO(1)
  671. FOO(2)
  672. FOO(3)
  673. FOO(4)
  674. #undef FOO
  675. //////
  676. #define FOO(N) \
  677. void Uniform ## N ## iv(WebGLUniformLocation* loc, const Int32ListU& list, \
  678. GLuint elemOffset = 0, GLuint elemCountOverride = 0) \
  679. { \
  680. UniformNiv("uniform" #N "iv", N, loc, Int32Arr::From(list), elemOffset, \
  681. elemCountOverride); \
  682. }
  683. FOO(1)
  684. FOO(2)
  685. FOO(3)
  686. FOO(4)
  687. #undef FOO
  688. //////
  689. #define FOO(N) \
  690. void Uniform ## N ## uiv(WebGLUniformLocation* loc, const Uint32ListU& list, \
  691. GLuint elemOffset = 0, GLuint elemCountOverride = 0) \
  692. { \
  693. UniformNuiv("uniform" #N "uiv", N, loc, Uint32Arr::From(list), elemOffset, \
  694. elemCountOverride); \
  695. }
  696. FOO(1)
  697. FOO(2)
  698. FOO(3)
  699. FOO(4)
  700. #undef FOO
  701. //////
  702. #define FOO(X,A,B) \
  703. void UniformMatrix ## X ## fv(WebGLUniformLocation* loc, bool transpose, \
  704. const Float32ListU& list, GLuint elemOffset = 0, \
  705. GLuint elemCountOverride = 0) \
  706. { \
  707. UniformMatrixAxBfv("uniformMatrix" #X "fv", A, B, loc, transpose, \
  708. Float32Arr::From(list), elemOffset, elemCountOverride); \
  709. }
  710. FOO(2,2,2)
  711. FOO(2x3,2,3)
  712. FOO(2x4,2,4)
  713. FOO(3x2,3,2)
  714. FOO(3,3,3)
  715. FOO(3x4,3,4)
  716. FOO(4x2,4,2)
  717. FOO(4x3,4,3)
  718. FOO(4,4,4)
  719. #undef FOO
  720. ////////////////////////////////////
  721. void UseProgram(WebGLProgram* prog);
  722. bool ValidateAttribArraySetter(const char* name, uint32_t count,
  723. uint32_t arrayLength);
  724. bool ValidateUniformLocation(WebGLUniformLocation* loc, const char* funcName);
  725. bool ValidateUniformSetter(WebGLUniformLocation* loc, uint8_t setterSize,
  726. GLenum setterType, const char* funcName);
  727. bool ValidateUniformArraySetter(WebGLUniformLocation* loc,
  728. uint8_t setterElemSize, GLenum setterType,
  729. uint32_t setterArraySize, const char* funcName,
  730. uint32_t* out_numElementsToUpload);
  731. bool ValidateUniformMatrixArraySetter(WebGLUniformLocation* loc,
  732. uint8_t setterCols,
  733. uint8_t setterRows,
  734. GLenum setterType,
  735. uint32_t setterArraySize,
  736. bool setterTranspose,
  737. const char* funcName,
  738. uint32_t* out_numElementsToUpload);
  739. void ValidateProgram(const WebGLProgram& prog);
  740. bool ValidateUniformLocation(const char* info, WebGLUniformLocation* loc);
  741. bool ValidateSamplerUniformSetter(const char* info,
  742. WebGLUniformLocation* loc, GLint value);
  743. void Viewport(GLint x, GLint y, GLsizei width, GLsizei height);
  744. // -----------------------------------------------------------------------------
  745. // WEBGL_lose_context
  746. public:
  747. void LoseContext();
  748. void RestoreContext();
  749. // -----------------------------------------------------------------------------
  750. // Buffer Objects (WebGLContextBuffers.cpp)
  751. void BindBuffer(GLenum target, WebGLBuffer* buffer);
  752. void BindBufferBase(GLenum target, GLuint index, WebGLBuffer* buf);
  753. void BindBufferRange(GLenum target, GLuint index, WebGLBuffer* buf,
  754. WebGLintptr offset, WebGLsizeiptr size);
  755. private:
  756. void BufferDataImpl(GLenum target, size_t dataLen, const uint8_t* data, GLenum usage);
  757. public:
  758. void BufferData(GLenum target, WebGLsizeiptr size, GLenum usage);
  759. void BufferData(GLenum target, const dom::Nullable<dom::ArrayBuffer>& maybeSrc,
  760. GLenum usage);
  761. void BufferData(GLenum target, const dom::ArrayBufferView& srcData, GLenum usage,
  762. GLuint srcElemOffset = 0, GLuint srcElemCountOverride = 0);
  763. private:
  764. void BufferSubDataImpl(GLenum target, WebGLsizeiptr dstByteOffset,
  765. size_t srcDataLen, const uint8_t* srcData);
  766. public:
  767. void BufferSubData(GLenum target, WebGLsizeiptr dstByteOffset,
  768. const dom::ArrayBufferView& src, GLuint srcElemOffset = 0,
  769. GLuint srcElemCountOverride = 0);
  770. void BufferSubData(GLenum target, WebGLsizeiptr dstByteOffset,
  771. const dom::ArrayBuffer& src);
  772. void BufferSubData(GLenum target, WebGLsizeiptr dstByteOffset,
  773. const dom::SharedArrayBuffer& src);
  774. already_AddRefed<WebGLBuffer> CreateBuffer();
  775. void DeleteBuffer(WebGLBuffer* buf);
  776. bool IsBuffer(WebGLBuffer* buf);
  777. protected:
  778. // bound buffer state
  779. WebGLRefPtr<WebGLBuffer> mBoundArrayBuffer;
  780. WebGLRefPtr<WebGLBuffer> mBoundCopyReadBuffer;
  781. WebGLRefPtr<WebGLBuffer> mBoundCopyWriteBuffer;
  782. WebGLRefPtr<WebGLBuffer> mBoundPixelPackBuffer;
  783. WebGLRefPtr<WebGLBuffer> mBoundPixelUnpackBuffer;
  784. WebGLRefPtr<WebGLBuffer> mBoundUniformBuffer;
  785. std::vector<IndexedBufferBinding> mIndexedUniformBufferBindings;
  786. WebGLRefPtr<WebGLBuffer>& GetBufferSlotByTarget(GLenum target);
  787. WebGLRefPtr<WebGLBuffer>& GetBufferSlotByTargetIndexed(GLenum target,
  788. GLuint index);
  789. // -----------------------------------------------------------------------------
  790. // Queries (WebGL2ContextQueries.cpp)
  791. protected:
  792. WebGLRefPtr<WebGLQuery> mQuerySlot_SamplesPassed;
  793. WebGLRefPtr<WebGLQuery> mQuerySlot_TFPrimsWritten;
  794. WebGLRefPtr<WebGLQuery> mQuerySlot_TimeElapsed;
  795. WebGLRefPtr<WebGLQuery>*
  796. ValidateQuerySlotByTarget(const char* funcName, GLenum target);
  797. public:
  798. already_AddRefed<WebGLQuery> CreateQuery(const char* funcName = nullptr);
  799. void DeleteQuery(WebGLQuery* query, const char* funcName = nullptr);
  800. bool IsQuery(const WebGLQuery* query, const char* funcName = nullptr);
  801. void BeginQuery(GLenum target, WebGLQuery& query, const char* funcName = nullptr);
  802. void EndQuery(GLenum target, const char* funcName = nullptr);
  803. void GetQuery(JSContext* cx, GLenum target, GLenum pname,
  804. JS::MutableHandleValue retval, const char* funcName = nullptr);
  805. void GetQueryParameter(JSContext* cx, const WebGLQuery& query, GLenum pname,
  806. JS::MutableHandleValue retval, const char* funcName = nullptr);
  807. // -----------------------------------------------------------------------------
  808. // State and State Requests (WebGLContextState.cpp)
  809. public:
  810. void Disable(GLenum cap);
  811. void Enable(GLenum cap);
  812. bool GetStencilBits(GLint* const out_stencilBits);
  813. bool GetChannelBits(const char* funcName, GLenum pname, GLint* const out_val);
  814. virtual JS::Value GetParameter(JSContext* cx, GLenum pname, ErrorResult& rv);
  815. void GetParameter(JSContext* cx, GLenum pname,
  816. JS::MutableHandle<JS::Value> retval, ErrorResult& rv)
  817. {
  818. retval.set(GetParameter(cx, pname, rv));
  819. }
  820. void GetParameterIndexed(JSContext* cx, GLenum pname, GLuint index,
  821. JS::MutableHandle<JS::Value> retval);
  822. bool IsEnabled(GLenum cap);
  823. private:
  824. // State tracking slots
  825. realGLboolean mDitherEnabled;
  826. realGLboolean mRasterizerDiscardEnabled;
  827. realGLboolean mScissorTestEnabled;
  828. realGLboolean mDepthTestEnabled;
  829. realGLboolean mStencilTestEnabled;
  830. GLenum mGenerateMipmapHint;
  831. bool ValidateCapabilityEnum(GLenum cap, const char* info);
  832. realGLboolean* GetStateTrackingSlot(GLenum cap);
  833. // -----------------------------------------------------------------------------
  834. // Texture funcions (WebGLContextTextures.cpp)
  835. public:
  836. void ActiveTexture(GLenum texUnit);
  837. void BindTexture(GLenum texTarget, WebGLTexture* tex);
  838. already_AddRefed<WebGLTexture> CreateTexture();
  839. void DeleteTexture(WebGLTexture* tex);
  840. void GenerateMipmap(GLenum texTarget);
  841. void GetTexParameter(JSContext*, GLenum texTarget, GLenum pname,
  842. JS::MutableHandle<JS::Value> retval)
  843. {
  844. retval.set(GetTexParameter(texTarget, pname));
  845. }
  846. bool IsTexture(WebGLTexture* tex);
  847. void TexParameterf(GLenum texTarget, GLenum pname, GLfloat param) {
  848. TexParameter_base(texTarget, pname, FloatOrInt(param));
  849. }
  850. void TexParameteri(GLenum texTarget, GLenum pname, GLint param) {
  851. TexParameter_base(texTarget, pname, FloatOrInt(param));
  852. }
  853. protected:
  854. JS::Value GetTexParameter(GLenum texTarget, GLenum pname);
  855. void TexParameter_base(GLenum texTarget, GLenum pname, const FloatOrInt& param);
  856. virtual bool IsTexParamValid(GLenum pname) const;
  857. ////////////////////////////////////
  858. public:
  859. template<typename T>
  860. void CompressedTexImage2D(GLenum target, GLint level, GLenum internalFormat,
  861. GLsizei width, GLsizei height, GLint border,
  862. const T& anySrc, GLuint viewElemOffset = 0,
  863. GLuint viewElemLengthOverride = 0)
  864. {
  865. const char funcName[] = "compressedTexImage2D";
  866. const uint8_t funcDims = 2;
  867. const GLsizei depth = 1;
  868. const TexImageSourceAdapter src(&anySrc, viewElemOffset, viewElemLengthOverride);
  869. CompressedTexImage(funcName, funcDims, target, level, internalFormat, width,
  870. height, depth, border, src);
  871. }
  872. template<typename T>
  873. void CompressedTexSubImage2D(GLenum target, GLint level, GLint xOffset, GLint yOffset,
  874. GLsizei width, GLsizei height, GLenum unpackFormat,
  875. const T& anySrc, GLuint viewElemOffset = 0,
  876. GLuint viewElemLengthOverride = 0)
  877. {
  878. const char funcName[] = "compressedTexSubImage2D";
  879. const uint8_t funcDims = 2;
  880. const GLint zOffset = 0;
  881. const GLsizei depth = 1;
  882. const TexImageSourceAdapter src(&anySrc, viewElemOffset, viewElemLengthOverride);
  883. CompressedTexSubImage(funcName, funcDims, target, level, xOffset, yOffset,
  884. zOffset, width, height, depth, unpackFormat, src);
  885. }
  886. protected:
  887. void CompressedTexImage(const char* funcName, uint8_t funcDims, GLenum target,
  888. GLint level, GLenum internalFormat, GLsizei width,
  889. GLsizei height, GLsizei depth, GLint border,
  890. const TexImageSource& src);
  891. void CompressedTexSubImage(const char* funcName, uint8_t funcDims, GLenum target,
  892. GLint level, GLint xOffset, GLint yOffset, GLint zOffset,
  893. GLsizei width, GLsizei height, GLsizei depth,
  894. GLenum unpackFormat, const TexImageSource& src);
  895. ////////////////////////////////////
  896. public:
  897. void CopyTexImage2D(GLenum target, GLint level, GLenum internalFormat, GLint x,
  898. GLint y, GLsizei width, GLsizei height, GLint border);
  899. void CopyTexSubImage2D(GLenum target, GLint level, GLint xOffset,
  900. GLint yOffset, GLint x, GLint y, GLsizei width,
  901. GLsizei height)
  902. {
  903. const char funcName[] = "copyTexSubImage2D";
  904. const uint8_t funcDims = 2;
  905. const GLint zOffset = 0;
  906. CopyTexSubImage(funcName, funcDims, target, level, xOffset, yOffset, zOffset,
  907. x, y, width, height);
  908. }
  909. protected:
  910. void CopyTexSubImage(const char* funcName, uint8_t funcDims, GLenum target,
  911. GLint level, GLint xOffset, GLint yOffset, GLint zOffset,
  912. GLint x, GLint y, GLsizei width, GLsizei height);
  913. ////////////////////////////////////
  914. // TexImage
  915. // Implicit width/height uploads
  916. public:
  917. template<typename T>
  918. void TexImage2D(GLenum target, GLint level, GLenum internalFormat,
  919. GLenum unpackFormat, GLenum unpackType, const T& src,
  920. ErrorResult& out_error)
  921. {
  922. GLsizei width = 0;
  923. GLsizei height = 0;
  924. GLint border = 0;
  925. TexImage2D(target, level, internalFormat, width, height, border, unpackFormat,
  926. unpackType, src, out_error);
  927. }
  928. template<typename T>
  929. void TexSubImage2D(GLenum target, GLint level, GLint xOffset, GLint yOffset,
  930. GLenum unpackFormat, GLenum unpackType, const T& src,
  931. ErrorResult& out_error)
  932. {
  933. GLsizei width = 0;
  934. GLsizei height = 0;
  935. TexSubImage2D(target, level, xOffset, yOffset, width, height, unpackFormat,
  936. unpackType, src, out_error);
  937. }
  938. ////
  939. template<typename T>
  940. void TexImage2D(GLenum target, GLint level, GLenum internalFormat, GLsizei width,
  941. GLsizei height, GLint border, GLenum unpackFormat, GLenum unpackType,
  942. const T& anySrc, ErrorResult& out_error)
  943. {
  944. const TexImageSourceAdapter src(&anySrc, &out_error);
  945. TexImage2D(target, level, internalFormat, width, height, border, unpackFormat,
  946. unpackType, src);
  947. }
  948. void TexImage2D(GLenum target, GLint level, GLenum internalFormat, GLsizei width,
  949. GLsizei height, GLint border, GLenum unpackFormat, GLenum unpackType,
  950. const dom::ArrayBufferView& view, GLuint viewElemOffset,
  951. ErrorResult&)
  952. {
  953. const TexImageSourceAdapter src(&view, viewElemOffset);
  954. TexImage2D(target, level, internalFormat, width, height, border, unpackFormat,
  955. unpackType, src);
  956. }
  957. protected:
  958. void TexImage2D(GLenum target, GLint level, GLenum internalFormat, GLsizei width,
  959. GLsizei height, GLint border, GLenum unpackFormat,
  960. GLenum unpackType, const TexImageSource& src)
  961. {
  962. const char funcName[] = "texImage2D";
  963. const uint8_t funcDims = 2;
  964. const GLsizei depth = 1;
  965. TexImage(funcName, funcDims, target, level, internalFormat, width, height, depth,
  966. border, unpackFormat, unpackType, src);
  967. }
  968. void TexImage(const char* funcName, uint8_t funcDims, GLenum target, GLint level,
  969. GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth,
  970. GLint border, GLenum unpackFormat, GLenum unpackType,
  971. const TexImageSource& src);
  972. ////
  973. public:
  974. template<typename T>
  975. void TexSubImage2D(GLenum target, GLint level, GLint xOffset, GLint yOffset,
  976. GLsizei width, GLsizei height, GLenum unpackFormat,
  977. GLenum unpackType, const T& anySrc, ErrorResult& out_error)
  978. {
  979. const TexImageSourceAdapter src(&anySrc, &out_error);
  980. TexSubImage2D(target, level, xOffset, yOffset, width, height, unpackFormat,
  981. unpackType, src);
  982. }
  983. void TexSubImage2D(GLenum target, GLint level, GLint xOffset, GLint yOffset,
  984. GLsizei width, GLsizei height, GLenum unpackFormat,
  985. GLenum unpackType, const dom::ArrayBufferView& view,
  986. GLuint viewElemOffset, ErrorResult&)
  987. {
  988. const TexImageSourceAdapter src(&view, viewElemOffset);
  989. TexSubImage2D(target, level, xOffset, yOffset, width, height, unpackFormat,
  990. unpackType, src);
  991. }
  992. protected:
  993. void TexSubImage2D(GLenum target, GLint level, GLint xOffset, GLint yOffset,
  994. GLsizei width, GLsizei height, GLenum unpackFormat,
  995. GLenum unpackType, const TexImageSource& src)
  996. {
  997. const char funcName[] = "texSubImage2D";
  998. const uint8_t funcDims = 2;
  999. const GLint zOffset = 0;
  1000. const GLsizei depth = 1;
  1001. TexSubImage(funcName, funcDims, target, level, xOffset, yOffset, zOffset, width,
  1002. height, depth, unpackFormat, unpackType, src);
  1003. }
  1004. void TexSubImage(const char* funcName, uint8_t funcDims, GLenum target, GLint level,
  1005. GLint xOffset, GLint yOffset, GLint zOffset, GLsizei width,
  1006. GLsizei height, GLsizei depth, GLenum unpackFormat,
  1007. GLenum unpackType, const TexImageSource& src);
  1008. ////////////////////////////////////
  1009. // WebGLTextureUpload.cpp
  1010. public:
  1011. UniquePtr<webgl::TexUnpackBlob>
  1012. From(const char* funcName, TexImageTarget target, GLsizei rawWidth, GLsizei rawHeight,
  1013. GLsizei rawDepth, GLint border, const TexImageSource& src,
  1014. dom::Uint8ClampedArray* const scopedArr);
  1015. protected:
  1016. bool ValidateTexImageSpecification(const char* funcName, uint8_t funcDims,
  1017. GLenum texImageTarget, GLint level,
  1018. GLsizei width, GLsizei height, GLsizei depth,
  1019. GLint border,
  1020. TexImageTarget* const out_target,
  1021. WebGLTexture** const out_texture,
  1022. WebGLTexture::ImageInfo** const out_imageInfo);
  1023. bool ValidateTexImageSelection(const char* funcName, uint8_t funcDims,
  1024. GLenum texImageTarget, GLint level, GLint xOffset,
  1025. GLint yOffset, GLint zOffset, GLsizei width,
  1026. GLsizei height, GLsizei depth,
  1027. TexImageTarget* const out_target,
  1028. WebGLTexture** const out_texture,
  1029. WebGLTexture::ImageInfo** const out_imageInfo);
  1030. bool ValidateUnpackInfo(const char* funcName, bool usePBOs, GLenum format,
  1031. GLenum type, webgl::PackingInfo* const out);
  1032. UniquePtr<webgl::TexUnpackBlob>
  1033. FromDomElem(const char* funcName, TexImageTarget target, uint32_t width,
  1034. uint32_t height, uint32_t depth, const dom::Element& elem,
  1035. ErrorResult* const out_error);
  1036. UniquePtr<webgl::TexUnpackBytes>
  1037. FromCompressed(const char* funcName, TexImageTarget target, GLsizei rawWidth,
  1038. GLsizei rawHeight, GLsizei rawDepth, GLint border,
  1039. const TexImageSource& src);
  1040. // -----------------------------------------------------------------------------
  1041. // Vertices Feature (WebGLContextVertices.cpp)
  1042. GLenum mPrimRestartTypeBytes;
  1043. public:
  1044. void DrawArrays(GLenum mode, GLint first, GLsizei count);
  1045. void DrawArraysInstanced(GLenum mode, GLint first, GLsizei count,
  1046. GLsizei primcount);
  1047. void DrawElements(GLenum mode, GLsizei count, GLenum type,
  1048. WebGLintptr byteOffset, const char* funcName = nullptr);
  1049. void DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type,
  1050. WebGLintptr byteOffset, GLsizei primcount);
  1051. void EnableVertexAttribArray(GLuint index);
  1052. void DisableVertexAttribArray(GLuint index);
  1053. JS::Value GetVertexAttrib(JSContext* cx, GLuint index, GLenum pname,
  1054. ErrorResult& rv);
  1055. void GetVertexAttrib(JSContext* cx, GLuint index, GLenum pname,
  1056. JS::MutableHandle<JS::Value> retval, ErrorResult& rv)
  1057. {
  1058. retval.set(GetVertexAttrib(cx, index, pname, rv));
  1059. }
  1060. WebGLsizeiptr GetVertexAttribOffset(GLuint index, GLenum pname);
  1061. ////
  1062. void VertexAttrib4f(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w,
  1063. const char* funcName = nullptr);
  1064. ////
  1065. void VertexAttrib1f(GLuint index, GLfloat x) {
  1066. VertexAttrib4f(index, x, 0, 0, 1, "vertexAttrib1f");
  1067. }
  1068. void VertexAttrib2f(GLuint index, GLfloat x, GLfloat y) {
  1069. VertexAttrib4f(index, x, y, 0, 1, "vertexAttrib2f");
  1070. }
  1071. void VertexAttrib3f(GLuint index, GLfloat x, GLfloat y, GLfloat z) {
  1072. VertexAttrib4f(index, x, y, z, 1, "vertexAttrib3f");
  1073. }
  1074. ////
  1075. void VertexAttrib1fv(GLuint index, const Float32ListU& list) {
  1076. const char funcName[] = "vertexAttrib1fv";
  1077. const auto& arr = Float32Arr::From(list);
  1078. if (!ValidateAttribArraySetter(funcName, 1, arr.elemCount))
  1079. return;
  1080. VertexAttrib4f(index, arr.elemBytes[0], 0, 0, 1, funcName);
  1081. }
  1082. void VertexAttrib2fv(GLuint index, const Float32ListU& list) {
  1083. const char funcName[] = "vertexAttrib2fv";
  1084. const auto& arr = Float32Arr::From(list);
  1085. if (!ValidateAttribArraySetter(funcName, 2, arr.elemCount))
  1086. return;
  1087. VertexAttrib4f(index, arr.elemBytes[0], arr.elemBytes[1], 0, 1, funcName);
  1088. }
  1089. void VertexAttrib3fv(GLuint index, const Float32ListU& list) {
  1090. const char funcName[] = "vertexAttrib3fv";
  1091. const auto& arr = Float32Arr::From(list);
  1092. if (!ValidateAttribArraySetter(funcName, 3, arr.elemCount))
  1093. return;
  1094. VertexAttrib4f(index, arr.elemBytes[0], arr.elemBytes[1], arr.elemBytes[2], 1,
  1095. funcName);
  1096. }
  1097. void VertexAttrib4fv(GLuint index, const Float32ListU& list) {
  1098. const char funcName[] = "vertexAttrib4fv";
  1099. const auto& arr = Float32Arr::From(list);
  1100. if (!ValidateAttribArraySetter(funcName, 4, arr.elemCount))
  1101. return;
  1102. VertexAttrib4f(index, arr.elemBytes[0], arr.elemBytes[1], arr.elemBytes[2],
  1103. arr.elemBytes[3], funcName);
  1104. }
  1105. ////
  1106. void VertexAttribPointer(GLuint index, GLint size, GLenum type,
  1107. WebGLboolean normalized, GLsizei stride,
  1108. WebGLintptr byteOffset);
  1109. void VertexAttribDivisor(GLuint index, GLuint divisor);
  1110. private:
  1111. // Cache the max number of vertices and instances that can be read from
  1112. // bound VBOs (result of ValidateBuffers).
  1113. bool mBufferFetchingIsVerified;
  1114. bool mBufferFetchingHasPerVertex;
  1115. uint32_t mMaxFetchedVertices;
  1116. uint32_t mMaxFetchedInstances;
  1117. bool mBufferFetch_IsAttrib0Active;
  1118. bool DrawArrays_check(const char* funcName, GLenum mode, GLint first,
  1119. GLsizei vertCount, GLsizei instanceCount);
  1120. bool DrawElements_check(const char* funcName, GLenum mode, GLsizei vertCount,
  1121. GLenum type, WebGLintptr byteOffset,
  1122. GLsizei instanceCount);
  1123. bool DrawInstanced_check(const char* info);
  1124. void Draw_cleanup(const char* funcName);
  1125. void VertexAttrib1fv_base(GLuint index, uint32_t arrayLength,
  1126. const GLfloat* ptr);
  1127. void VertexAttrib2fv_base(GLuint index, uint32_t arrayLength,
  1128. const GLfloat* ptr);
  1129. void VertexAttrib3fv_base(GLuint index, uint32_t arrayLength,
  1130. const GLfloat* ptr);
  1131. void VertexAttrib4fv_base(GLuint index, uint32_t arrayLength,
  1132. const GLfloat* ptr);
  1133. bool ValidateBufferFetching(const char* info);
  1134. bool BindArrayAttribToLocation0(WebGLProgram* prog);
  1135. // -----------------------------------------------------------------------------
  1136. // PROTECTED
  1137. protected:
  1138. WebGLVertexAttrib0Status WhatDoesVertexAttrib0Need() const;
  1139. bool DoFakeVertexAttrib0(const char* funcName, GLuint vertexCount);
  1140. void UndoFakeVertexAttrib0();
  1141. inline void InvalidateBufferFetching()
  1142. {
  1143. mBufferFetchingIsVerified = false;
  1144. mBufferFetchingHasPerVertex = false;
  1145. mMaxFetchedVertices = 0;
  1146. mMaxFetchedInstances = 0;
  1147. }
  1148. CheckedUint32 mGeneration;
  1149. WebGLContextOptions mOptions;
  1150. bool mInvalidated;
  1151. bool mCapturedFrameInvalidated;
  1152. bool mResetLayer;
  1153. bool mLayerIsMirror;
  1154. bool mOptionsFrozen;
  1155. bool mMinCapability;
  1156. bool mDisableExtensions;
  1157. bool mIsMesa;
  1158. bool mLoseContextOnMemoryPressure;
  1159. bool mCanLoseContextInForeground;
  1160. bool mRestoreWhenVisible;
  1161. bool mShouldPresent;
  1162. bool mBackbufferNeedsClear;
  1163. bool mDisableFragHighP;
  1164. template<typename WebGLObjectType>
  1165. void DeleteWebGLObjectsArray(nsTArray<WebGLObjectType>& array);
  1166. GLuint mActiveTexture;
  1167. GLenum mDefaultFB_DrawBuffer0;
  1168. // glGetError sources:
  1169. bool mEmitContextLostErrorOnce;
  1170. GLenum mWebGLError;
  1171. GLenum mUnderlyingGLError;
  1172. GLenum GetAndFlushUnderlyingGLErrors();
  1173. bool mBypassShaderValidation;
  1174. webgl::ShaderValidator* CreateShaderValidator(GLenum shaderType) const;
  1175. // some GL constants
  1176. uint32_t mGLMaxVertexAttribs;
  1177. int32_t mGLMaxTextureUnits;
  1178. int32_t mGLMaxTextureImageUnits;
  1179. int32_t mGLMaxVertexTextureImageUnits;
  1180. int32_t mGLMaxVaryingVectors;
  1181. int32_t mGLMaxFragmentUniformVectors;
  1182. int32_t mGLMaxVertexUniformVectors;
  1183. uint32_t mGLMaxTransformFeedbackSeparateAttribs;
  1184. GLuint mGLMaxUniformBufferBindings;
  1185. // What is supported:
  1186. uint32_t mGLMaxColorAttachments;
  1187. uint32_t mGLMaxDrawBuffers;
  1188. // What we're allowing:
  1189. uint32_t mImplMaxColorAttachments;
  1190. uint32_t mImplMaxDrawBuffers;
  1191. uint32_t mImplMaxViewportDims[2];
  1192. public:
  1193. GLenum LastColorAttachmentEnum() const {
  1194. return LOCAL_GL_COLOR_ATTACHMENT0 + mImplMaxColorAttachments - 1;
  1195. }
  1196. const decltype(mOptions)& Options() const { return mOptions; }
  1197. protected:
  1198. // Texture sizes are often not actually the GL values. Let's be explicit that these
  1199. // are implementation limits.
  1200. uint32_t mImplMaxTextureSize;
  1201. uint32_t mImplMaxCubeMapTextureSize;
  1202. uint32_t mImplMax3DTextureSize;
  1203. uint32_t mImplMaxArrayTextureLayers;
  1204. uint32_t mImplMaxRenderbufferSize;
  1205. public:
  1206. GLuint MaxVertexAttribs() const {
  1207. return mGLMaxVertexAttribs;
  1208. }
  1209. GLuint GLMaxTextureUnits() const {
  1210. return mGLMaxTextureUnits;
  1211. }
  1212. bool IsFormatValidForFB(TexInternalFormat format) const;
  1213. protected:
  1214. // Represents current status of the context with respect to context loss.
  1215. // That is, whether the context is lost, and what part of the context loss
  1216. // process we currently are at.
  1217. // This is used to support the WebGL spec's asyncronous nature in handling
  1218. // context loss.
  1219. enum ContextStatus {
  1220. // The context is stable; there either are none or we don't know of any.
  1221. ContextNotLost,
  1222. // The context has been lost, but we have not yet sent an event to the
  1223. // script informing it of this.
  1224. ContextLostAwaitingEvent,
  1225. // The context has been lost, and we have sent the script an event
  1226. // informing it of this.
  1227. ContextLost,
  1228. // The context is lost, an event has been sent to the script, and the
  1229. // script correctly handled the event. We are waiting for the context to
  1230. // be restored.
  1231. ContextLostAwaitingRestore
  1232. };
  1233. // -------------------------------------------------------------------------
  1234. // WebGL extensions (implemented in WebGLContextExtensions.cpp)
  1235. typedef EnumeratedArray<WebGLExtensionID, WebGLExtensionID::Max,
  1236. RefPtr<WebGLExtensionBase>> ExtensionsArrayType;
  1237. ExtensionsArrayType mExtensions;
  1238. // enable an extension. the extension should not be enabled before.
  1239. void EnableExtension(WebGLExtensionID ext);
  1240. // Enable an extension if it's supported. Return the extension on success.
  1241. WebGLExtensionBase* EnableSupportedExtension(dom::CallerType callerType,
  1242. WebGLExtensionID ext);
  1243. public:
  1244. // returns true if the extension has been enabled by calling getExtension.
  1245. bool IsExtensionEnabled(WebGLExtensionID ext) const;
  1246. protected:
  1247. // returns true if the extension is supported for this caller type (this decides what getSupportedExtensions exposes)
  1248. bool IsExtensionSupported(dom::CallerType callerType,
  1249. WebGLExtensionID ext) const;
  1250. bool IsExtensionSupported(WebGLExtensionID ext) const;
  1251. static const char* GetExtensionString(WebGLExtensionID ext);
  1252. nsTArray<GLenum> mCompressedTextureFormats;
  1253. // -------------------------------------------------------------------------
  1254. // WebGL 2 specifics (implemented in WebGL2Context.cpp)
  1255. public:
  1256. virtual bool IsWebGL2() const = 0;
  1257. struct FailureReason {
  1258. nsCString key; // For reporting.
  1259. nsCString info;
  1260. FailureReason() { }
  1261. template<typename A, typename B>
  1262. FailureReason(const A& _key, const B& _info)
  1263. : key(nsCString(_key))
  1264. , info(nsCString(_info))
  1265. { }
  1266. };
  1267. protected:
  1268. bool InitWebGL2(FailureReason* const out_failReason);
  1269. bool CreateAndInitGL(bool forceEnabled,
  1270. std::vector<FailureReason>* const out_failReasons);
  1271. bool ResizeBackbuffer(uint32_t width, uint32_t height);
  1272. typedef already_AddRefed<gl::GLContext> FnCreateGL_T(const gl::SurfaceCaps& caps,
  1273. gl::CreateContextFlags flags,
  1274. WebGLContext* webgl,
  1275. std::vector<FailureReason>* const out_failReasons);
  1276. bool CreateAndInitGLWith(FnCreateGL_T fnCreateGL, const gl::SurfaceCaps& baseCaps,
  1277. gl::CreateContextFlags flags,
  1278. std::vector<FailureReason>* const out_failReasons);
  1279. void ThrowEvent_WebGLContextCreationError(const nsACString& text);
  1280. // -------------------------------------------------------------------------
  1281. // Validation functions (implemented in WebGLContextValidate.cpp)
  1282. bool InitAndValidateGL(FailureReason* const out_failReason);
  1283. bool ValidateBlendEquationEnum(GLenum cap, const char* info);
  1284. bool ValidateBlendFuncDstEnum(GLenum mode, const char* info);
  1285. bool ValidateBlendFuncSrcEnum(GLenum mode, const char* info);
  1286. bool ValidateBlendFuncEnumsCompatibility(GLenum sfactor, GLenum dfactor,
  1287. const char* info);
  1288. bool ValidateComparisonEnum(GLenum target, const char* info);
  1289. bool ValidateStencilOpEnum(GLenum action, const char* info);
  1290. bool ValidateFaceEnum(GLenum face, const char* info);
  1291. bool ValidateTexInputData(GLenum type, js::Scalar::Type jsArrayType,
  1292. WebGLTexImageFunc func, WebGLTexDimensions dims);
  1293. bool ValidateDrawModeEnum(GLenum mode, const char* info);
  1294. bool ValidateAttribIndex(GLuint index, const char* info);
  1295. bool ValidateAttribPointer(bool integerMode, GLuint index, GLint size, GLenum type,
  1296. WebGLboolean normalized, GLsizei stride,
  1297. WebGLintptr byteOffset, const char* info);
  1298. bool ValidateStencilParamsForDrawCall();
  1299. bool ValidateCopyTexImage(TexInternalFormat srcFormat, TexInternalFormat dstformat,
  1300. WebGLTexImageFunc func, WebGLTexDimensions dims);
  1301. bool ValidateTexImage(TexImageTarget texImageTarget,
  1302. GLint level, GLenum internalFormat,
  1303. GLint xoffset, GLint yoffset, GLint zoffset,
  1304. GLint width, GLint height, GLint depth,
  1305. GLint border, GLenum format, GLenum type,
  1306. WebGLTexImageFunc func, WebGLTexDimensions dims);
  1307. bool ValidateTexImageFormat(GLenum internalFormat, WebGLTexImageFunc func,
  1308. WebGLTexDimensions dims);
  1309. bool ValidateTexImageType(GLenum type, WebGLTexImageFunc func,
  1310. WebGLTexDimensions dims);
  1311. bool ValidateTexImageFormatAndType(GLenum format, GLenum type,
  1312. WebGLTexImageFunc func,
  1313. WebGLTexDimensions dims);
  1314. bool ValidateCompTexImageInternalFormat(GLenum format,
  1315. WebGLTexImageFunc func,
  1316. WebGLTexDimensions dims);
  1317. bool ValidateCopyTexImageInternalFormat(GLenum format,
  1318. WebGLTexImageFunc func,
  1319. WebGLTexDimensions dims);
  1320. bool ValidateTexImageSize(TexImageTarget texImageTarget, GLint level,
  1321. GLint width, GLint height, GLint depth,
  1322. WebGLTexImageFunc func, WebGLTexDimensions dims);
  1323. bool ValidateTexSubImageSize(GLint x, GLint y, GLint z, GLsizei width,
  1324. GLsizei height, GLsizei depth,
  1325. GLsizei baseWidth, GLsizei baseHeight,
  1326. GLsizei baseDepth, WebGLTexImageFunc func,
  1327. WebGLTexDimensions dims);
  1328. bool ValidateCompTexImageSize(GLint level, GLenum internalFormat,
  1329. GLint xoffset, GLint yoffset, GLsizei width,
  1330. GLsizei height, GLsizei levelWidth,
  1331. GLsizei levelHeight, WebGLTexImageFunc func,
  1332. WebGLTexDimensions dims);
  1333. bool ValidateCompTexImageDataSize(GLint level, GLenum internalFormat,
  1334. GLsizei width, GLsizei height,
  1335. uint32_t byteLength,
  1336. WebGLTexImageFunc func,
  1337. WebGLTexDimensions dims);
  1338. bool ValidateUniformLocationForProgram(WebGLUniformLocation* location,
  1339. WebGLProgram* program,
  1340. const char* funcName);
  1341. bool ValidateCurFBForRead(const char* funcName,
  1342. const webgl::FormatUsageInfo** const out_format,
  1343. uint32_t* const out_width, uint32_t* const out_height);
  1344. bool HasDrawBuffers() const {
  1345. return IsWebGL2() ||
  1346. IsExtensionEnabled(WebGLExtensionID::WEBGL_draw_buffers);
  1347. }
  1348. WebGLRefPtr<WebGLBuffer>* ValidateBufferSlot(const char* funcName, GLenum target);
  1349. public:
  1350. WebGLBuffer* ValidateBufferSelection(const char* funcName, GLenum target);
  1351. protected:
  1352. IndexedBufferBinding* ValidateIndexedBufferSlot(const char* funcName, GLenum target,
  1353. GLuint index);
  1354. bool ValidateIndexedBufferBinding(const char* funcName, GLenum target, GLuint index,
  1355. WebGLRefPtr<WebGLBuffer>** const out_genericBinding,
  1356. IndexedBufferBinding** const out_indexedBinding);
  1357. bool ValidateNonNegative(const char* funcName, const char* argName, int64_t val) {
  1358. if (MOZ_UNLIKELY(val < 0)) {
  1359. ErrorInvalidValue("%s: `%s` must be non-negative.", funcName, argName);
  1360. return false;
  1361. }
  1362. return true;
  1363. }
  1364. public:
  1365. template<typename T>
  1366. bool ValidateNonNull(const char* funcName, const dom::Nullable<T>& maybe) {
  1367. if (maybe.IsNull()) {
  1368. ErrorInvalidValue("%s: `null` is invalid.", funcName);
  1369. return false;
  1370. }
  1371. return true;
  1372. }
  1373. bool ValidateArrayBufferView(const char* funcName, const dom::ArrayBufferView& view,
  1374. GLuint elemOffset, GLuint elemCountOverride,
  1375. uint8_t** const out_bytes, size_t* const out_byteLen);
  1376. protected:
  1377. ////
  1378. void Invalidate();
  1379. void DestroyResourcesAndContext();
  1380. void MakeContextCurrent() const;
  1381. // helpers
  1382. bool ConvertImage(size_t width, size_t height, size_t srcStride,
  1383. size_t dstStride, const uint8_t* src, uint8_t* dst,
  1384. WebGLTexelFormat srcFormat, bool srcPremultiplied,
  1385. WebGLTexelFormat dstFormat, bool dstPremultiplied,
  1386. size_t dstTexelSize);
  1387. //////
  1388. public:
  1389. bool ValidateObjectAllowDeleted(const char* funcName,
  1390. const WebGLContextBoundObject& object)
  1391. {
  1392. if (!object.IsCompatibleWithContext(this)) {
  1393. ErrorInvalidOperation("%s: Object from different WebGL context (or older"
  1394. " generation of this one) passed as argument.",
  1395. funcName);
  1396. return false;
  1397. }
  1398. return true;
  1399. }
  1400. bool ValidateObject(const char* funcName, const WebGLDeletableObject& object,
  1401. bool isShaderOrProgram = false)
  1402. {
  1403. if (!ValidateObjectAllowDeleted(funcName, object))
  1404. return false;
  1405. if (isShaderOrProgram) {
  1406. /* GLES 3.0.5 p45:
  1407. * "Commands that accept shader or program object names will generate the
  1408. * error INVALID_VALUE if the provided name is not the name of either a
  1409. * shader or program object[.]"
  1410. * Further, shaders and programs appear to be different from other objects,
  1411. * in that their lifetimes are better defined. However, they also appear to
  1412. * allow use of objects marked for deletion, and only reject
  1413. * actually-destroyed objects.
  1414. */
  1415. if (object.IsDeleted()) {
  1416. ErrorInvalidValue("%s: Shader or program object argument cannot have been"
  1417. " deleted.",
  1418. funcName);
  1419. return false;
  1420. }
  1421. } else {
  1422. if (object.IsDeleteRequested()) {
  1423. ErrorInvalidOperation("%s: Object argument cannot have been marked for"
  1424. " deletion.",
  1425. funcName);
  1426. return false;
  1427. }
  1428. }
  1429. return true;
  1430. }
  1431. ////
  1432. bool ValidateObject(const char* funcName, const WebGLProgram& object);
  1433. bool ValidateObject(const char* funcName, const WebGLShader& object);
  1434. ////
  1435. bool ValidateIsObject(const char* funcName,
  1436. const WebGLDeletableObject* object) const
  1437. {
  1438. if (IsContextLost())
  1439. return false;
  1440. if (!object)
  1441. return false;
  1442. if (!object->IsCompatibleWithContext(this))
  1443. return false;
  1444. if (object->IsDeleted())
  1445. return false;
  1446. return true;
  1447. }
  1448. bool ValidateDeleteObject(const char* funcName, const WebGLDeletableObject* object) {
  1449. if (IsContextLost())
  1450. return false;
  1451. if (!object)
  1452. return false;
  1453. if (!ValidateObjectAllowDeleted(funcName, *object))
  1454. return false;
  1455. if (object->IsDeleteRequested())
  1456. return false;
  1457. return true;
  1458. }
  1459. ////
  1460. private:
  1461. // -------------------------------------------------------------------------
  1462. // Context customization points
  1463. virtual WebGLVertexArray* CreateVertexArrayImpl();
  1464. virtual bool ValidateAttribPointerType(bool integerMode, GLenum type, uint32_t* alignment, const char* info) = 0;
  1465. virtual bool ValidateUniformMatrixTranspose(bool transpose, const char* info) = 0;
  1466. public:
  1467. void ForceLoseContext(bool simulateLoss = false);
  1468. protected:
  1469. void ForceRestoreContext();
  1470. nsTArray<WebGLRefPtr<WebGLTexture> > mBound2DTextures;
  1471. nsTArray<WebGLRefPtr<WebGLTexture> > mBoundCubeMapTextures;
  1472. nsTArray<WebGLRefPtr<WebGLTexture> > mBound3DTextures;
  1473. nsTArray<WebGLRefPtr<WebGLTexture> > mBound2DArrayTextures;
  1474. nsTArray<WebGLRefPtr<WebGLSampler> > mBoundSamplers;
  1475. void ResolveTexturesForDraw() const;
  1476. WebGLRefPtr<WebGLProgram> mCurrentProgram;
  1477. RefPtr<const webgl::LinkedProgramInfo> mActiveProgramLinkInfo;
  1478. bool ValidateFramebufferTarget(GLenum target, const char* const info);
  1479. bool ValidateInvalidateFramebuffer(const char* funcName, GLenum target,
  1480. const dom::Sequence<GLenum>& attachments,
  1481. ErrorResult* const out_rv,
  1482. std::vector<GLenum>* const scopedVector,
  1483. GLsizei* const out_glNumAttachments,
  1484. const GLenum** const out_glAttachments);
  1485. WebGLRefPtr<WebGLFramebuffer> mBoundDrawFramebuffer;
  1486. WebGLRefPtr<WebGLFramebuffer> mBoundReadFramebuffer;
  1487. WebGLRefPtr<WebGLRenderbuffer> mBoundRenderbuffer;
  1488. WebGLRefPtr<WebGLTransformFeedback> mBoundTransformFeedback;
  1489. WebGLRefPtr<WebGLVertexArray> mBoundVertexArray;
  1490. LinkedList<WebGLBuffer> mBuffers;
  1491. LinkedList<WebGLFramebuffer> mFramebuffers;
  1492. LinkedList<WebGLProgram> mPrograms;
  1493. LinkedList<WebGLQuery> mQueries;
  1494. LinkedList<WebGLRenderbuffer> mRenderbuffers;
  1495. LinkedList<WebGLSampler> mSamplers;
  1496. LinkedList<WebGLShader> mShaders;
  1497. LinkedList<WebGLSync> mSyncs;
  1498. LinkedList<WebGLTexture> mTextures;
  1499. LinkedList<WebGLTransformFeedback> mTransformFeedbacks;
  1500. LinkedList<WebGLVertexArray> mVertexArrays;
  1501. WebGLRefPtr<WebGLTransformFeedback> mDefaultTransformFeedback;
  1502. WebGLRefPtr<WebGLVertexArray> mDefaultVertexArray;
  1503. // PixelStore parameters
  1504. uint32_t mPixelStore_UnpackImageHeight;
  1505. uint32_t mPixelStore_UnpackSkipImages;
  1506. uint32_t mPixelStore_UnpackRowLength;
  1507. uint32_t mPixelStore_UnpackSkipRows;
  1508. uint32_t mPixelStore_UnpackSkipPixels;
  1509. uint32_t mPixelStore_UnpackAlignment;
  1510. uint32_t mPixelStore_PackRowLength;
  1511. uint32_t mPixelStore_PackSkipRows;
  1512. uint32_t mPixelStore_PackSkipPixels;
  1513. uint32_t mPixelStore_PackAlignment;
  1514. CheckedUint32 GetUnpackSize(bool isFunc3D, uint32_t width, uint32_t height,
  1515. uint32_t depth, uint8_t bytesPerPixel);
  1516. bool ValidatePackSize(const char* funcName, uint32_t width, uint32_t height,
  1517. uint8_t bytesPerPixel, uint32_t* const out_rowStride,
  1518. uint32_t* const out_endOffset);
  1519. GLenum mPixelStore_ColorspaceConversion;
  1520. bool mPixelStore_FlipY;
  1521. bool mPixelStore_PremultiplyAlpha;
  1522. ////////////////////////////////////
  1523. class FakeBlackTexture {
  1524. public:
  1525. static UniquePtr<FakeBlackTexture> Create(gl::GLContext* gl,
  1526. TexTarget target,
  1527. FakeBlackType type);
  1528. gl::GLContext* const mGL;
  1529. const GLuint mGLName;
  1530. ~FakeBlackTexture();
  1531. protected:
  1532. explicit FakeBlackTexture(gl::GLContext* gl);
  1533. };
  1534. UniquePtr<FakeBlackTexture> mFakeBlack_2D_0000;
  1535. UniquePtr<FakeBlackTexture> mFakeBlack_2D_0001;
  1536. UniquePtr<FakeBlackTexture> mFakeBlack_CubeMap_0000;
  1537. UniquePtr<FakeBlackTexture> mFakeBlack_CubeMap_0001;
  1538. UniquePtr<FakeBlackTexture> mFakeBlack_3D_0000;
  1539. UniquePtr<FakeBlackTexture> mFakeBlack_3D_0001;
  1540. UniquePtr<FakeBlackTexture> mFakeBlack_2D_Array_0000;
  1541. UniquePtr<FakeBlackTexture> mFakeBlack_2D_Array_0001;
  1542. bool BindFakeBlack(uint32_t texUnit, TexTarget target, FakeBlackType fakeBlack);
  1543. ////////////////////////////////////
  1544. protected:
  1545. GLuint mEmptyTFO;
  1546. // Generic Vertex Attributes
  1547. // Though CURRENT_VERTEX_ATTRIB is listed under "Vertex Shader State" in the spec
  1548. // state tables, this isn't vertex shader /object/ state. This array is merely state
  1549. // useful to vertex shaders, but is global state.
  1550. UniquePtr<GLenum[]> mGenericVertexAttribTypes;
  1551. uint8_t mGenericVertexAttrib0Data[sizeof(float) * 4];
  1552. GLuint mFakeVertexAttrib0BufferObject;
  1553. size_t mFakeVertexAttrib0BufferObjectSize;
  1554. bool mFakeVertexAttrib0DataDefined;
  1555. uint8_t mFakeVertexAttrib0Data[sizeof(float) * 4];
  1556. JSObject* GetVertexAttribFloat32Array(JSContext* cx, GLuint index);
  1557. JSObject* GetVertexAttribInt32Array(JSContext* cx, GLuint index);
  1558. JSObject* GetVertexAttribUint32Array(JSContext* cx, GLuint index);
  1559. GLint mStencilRefFront;
  1560. GLint mStencilRefBack;
  1561. GLuint mStencilValueMaskFront;
  1562. GLuint mStencilValueMaskBack;
  1563. GLuint mStencilWriteMaskFront;
  1564. GLuint mStencilWriteMaskBack;
  1565. realGLboolean mColorWriteMask[4];
  1566. realGLboolean mDepthWriteMask;
  1567. GLfloat mColorClearValue[4];
  1568. GLint mStencilClearValue;
  1569. GLfloat mDepthClearValue;
  1570. GLint mViewportX;
  1571. GLint mViewportY;
  1572. GLsizei mViewportWidth;
  1573. GLsizei mViewportHeight;
  1574. bool mAlreadyWarnedAboutViewportLargerThanDest;
  1575. GLfloat mLineWidth;
  1576. WebGLContextLossHandler mContextLossHandler;
  1577. bool mAllowContextRestore;
  1578. bool mLastLossWasSimulated;
  1579. ContextStatus mContextStatus;
  1580. bool mContextLostErrorSet;
  1581. // Used for some hardware (particularly Tegra 2 and 4) that likes to
  1582. // be Flushed while doing hundreds of draw calls.
  1583. int mDrawCallsSinceLastFlush;
  1584. int mAlreadyGeneratedWarnings;
  1585. int mMaxWarnings;
  1586. bool mAlreadyWarnedAboutFakeVertexAttrib0;
  1587. bool ShouldGenerateWarnings() const;
  1588. uint64_t mLastUseIndex;
  1589. bool mNeedsFakeNoAlpha;
  1590. bool mNeedsFakeNoDepth;
  1591. bool mNeedsFakeNoStencil;
  1592. bool mNeedsEmulatedLoneDepthStencil;
  1593. const bool mAllowFBInvalidation;
  1594. bool Has64BitTimestamps() const;
  1595. struct ScopedDrawCallWrapper final {
  1596. WebGLContext& mWebGL;
  1597. const bool mFakeNoAlpha;
  1598. const bool mFakeNoDepth;
  1599. const bool mFakeNoStencil;
  1600. static bool ShouldFakeNoAlpha(WebGLContext& webgl) {
  1601. // We should only be doing this if we're about to draw to the backbuffer, but
  1602. // the backbuffer needs to have this fake-no-alpha workaround.
  1603. return !webgl.mBoundDrawFramebuffer &&
  1604. webgl.mNeedsFakeNoAlpha &&
  1605. webgl.mColorWriteMask[3] != false;
  1606. }
  1607. static bool ShouldFakeNoDepth(WebGLContext& webgl) {
  1608. // We should only be doing this if we're about to draw to the backbuffer.
  1609. return !webgl.mBoundDrawFramebuffer &&
  1610. webgl.mNeedsFakeNoDepth &&
  1611. webgl.mDepthTestEnabled;
  1612. }
  1613. static bool HasDepthButNoStencil(const WebGLFramebuffer* fb);
  1614. static bool ShouldFakeNoStencil(WebGLContext& webgl) {
  1615. if (!webgl.mStencilTestEnabled)
  1616. return false;
  1617. if (!webgl.mBoundDrawFramebuffer) {
  1618. if (webgl.mNeedsFakeNoStencil)
  1619. return true;
  1620. if (webgl.mNeedsEmulatedLoneDepthStencil &&
  1621. webgl.mOptions.depth && !webgl.mOptions.stencil)
  1622. {
  1623. return true;
  1624. }
  1625. return false;
  1626. }
  1627. if (webgl.mNeedsEmulatedLoneDepthStencil &&
  1628. HasDepthButNoStencil(webgl.mBoundDrawFramebuffer))
  1629. {
  1630. return true;
  1631. }
  1632. return false;
  1633. }
  1634. ////
  1635. explicit ScopedDrawCallWrapper(WebGLContext& webgl);
  1636. ~ScopedDrawCallWrapper();
  1637. };
  1638. void OnBeforeReadCall();
  1639. void LoseOldestWebGLContextIfLimitExceeded();
  1640. void UpdateLastUseIndex();
  1641. template <typename WebGLObjectType>
  1642. JS::Value WebGLObjectAsJSValue(JSContext* cx, const WebGLObjectType*,
  1643. ErrorResult& rv) const;
  1644. template <typename WebGLObjectType>
  1645. JSObject* WebGLObjectAsJSObject(JSContext* cx, const WebGLObjectType*,
  1646. ErrorResult& rv) const;
  1647. public:
  1648. // console logging helpers
  1649. void GenerateWarning(const char* fmt, ...);
  1650. void GenerateWarning(const char* fmt, va_list ap);
  1651. public:
  1652. UniquePtr<webgl::FormatUsageAuthority> mFormatUsage;
  1653. virtual UniquePtr<webgl::FormatUsageAuthority>
  1654. CreateFormatUsage(gl::GLContext* gl) const = 0;
  1655. const decltype(mBound2DTextures)* TexListForElemType(GLenum elemType) const;
  1656. // Friend list
  1657. friend class ScopedCopyTexImageSource;
  1658. friend class ScopedResolveTexturesForDraw;
  1659. friend class ScopedUnpackReset;
  1660. friend class webgl::TexUnpackBlob;
  1661. friend class webgl::TexUnpackBytes;
  1662. friend class webgl::TexUnpackImage;
  1663. friend class webgl::TexUnpackSurface;
  1664. friend struct webgl::UniformInfo;
  1665. friend class WebGLTexture;
  1666. friend class WebGLFBAttachPoint;
  1667. friend class WebGLFramebuffer;
  1668. friend class WebGLRenderbuffer;
  1669. friend class WebGLProgram;
  1670. friend class WebGLQuery;
  1671. friend class WebGLBuffer;
  1672. friend class WebGLSampler;
  1673. friend class WebGLShader;
  1674. friend class WebGLSync;
  1675. friend class WebGLTransformFeedback;
  1676. friend class WebGLUniformLocation;
  1677. friend class WebGLVertexArray;
  1678. friend class WebGLVertexArrayFake;
  1679. friend class WebGLVertexArrayGL;
  1680. };
  1681. // used by DOM bindings in conjunction with GetParentObject
  1682. inline nsISupports*
  1683. ToSupports(WebGLContext* webgl)
  1684. {
  1685. return static_cast<nsIDOMWebGLRenderingContext*>(webgl);
  1686. }
  1687. // Returns `value` rounded to the next highest multiple of `multiple`.
  1688. // AKA PadToAlignment, StrideForAlignment.
  1689. template<typename V, typename M>
  1690. V
  1691. RoundUpToMultipleOf(const V& value, const M& multiple)
  1692. {
  1693. return ((value + multiple - 1) / multiple) * multiple;
  1694. }
  1695. bool
  1696. ValidateTexTarget(WebGLContext* webgl, const char* funcName, uint8_t funcDims,
  1697. GLenum rawTexTarget, TexTarget* const out_texTarget,
  1698. WebGLTexture** const out_tex);
  1699. bool
  1700. ValidateTexImageTarget(WebGLContext* webgl, const char* funcName, uint8_t funcDims,
  1701. GLenum rawTexImageTarget, TexImageTarget* const out_texImageTarget,
  1702. WebGLTexture** const out_tex);
  1703. class UniqueBuffer
  1704. {
  1705. // Like UniquePtr<>, but for void* and malloc/calloc/free.
  1706. void* mBuffer;
  1707. public:
  1708. UniqueBuffer()
  1709. : mBuffer(nullptr)
  1710. { }
  1711. MOZ_IMPLICIT UniqueBuffer(void* buffer)
  1712. : mBuffer(buffer)
  1713. { }
  1714. ~UniqueBuffer() {
  1715. free(mBuffer);
  1716. }
  1717. UniqueBuffer(UniqueBuffer&& other) {
  1718. this->mBuffer = other.mBuffer;
  1719. other.mBuffer = nullptr;
  1720. }
  1721. UniqueBuffer& operator =(UniqueBuffer&& other) {
  1722. free(this->mBuffer);
  1723. this->mBuffer = other.mBuffer;
  1724. other.mBuffer = nullptr;
  1725. return *this;
  1726. }
  1727. UniqueBuffer& operator =(void* newBuffer) {
  1728. free(this->mBuffer);
  1729. this->mBuffer = newBuffer;
  1730. return *this;
  1731. }
  1732. explicit operator bool() const { return bool(mBuffer); }
  1733. void* get() const { return mBuffer; }
  1734. UniqueBuffer(const UniqueBuffer& other) = delete; // construct using Move()!
  1735. void operator =(const UniqueBuffer& other) = delete; // assign using Move()!
  1736. };
  1737. class ScopedUnpackReset final
  1738. : public gl::ScopedGLWrapper<ScopedUnpackReset>
  1739. {
  1740. friend struct gl::ScopedGLWrapper<ScopedUnpackReset>;
  1741. private:
  1742. WebGLContext* const mWebGL;
  1743. public:
  1744. explicit ScopedUnpackReset(WebGLContext* webgl);
  1745. private:
  1746. void UnwrapImpl();
  1747. };
  1748. class ScopedFBRebinder final
  1749. : public gl::ScopedGLWrapper<ScopedFBRebinder>
  1750. {
  1751. friend struct gl::ScopedGLWrapper<ScopedFBRebinder>;
  1752. private:
  1753. WebGLContext* const mWebGL;
  1754. public:
  1755. explicit ScopedFBRebinder(WebGLContext* webgl)
  1756. : ScopedGLWrapper<ScopedFBRebinder>(webgl->gl)
  1757. , mWebGL(webgl)
  1758. { }
  1759. private:
  1760. void UnwrapImpl();
  1761. };
  1762. class ScopedLazyBind final
  1763. : public gl::ScopedGLWrapper<ScopedLazyBind>
  1764. {
  1765. friend struct gl::ScopedGLWrapper<ScopedLazyBind>;
  1766. const GLenum mTarget;
  1767. const WebGLBuffer* const mBuf;
  1768. public:
  1769. ScopedLazyBind(gl::GLContext* gl, GLenum target, const WebGLBuffer* buf);
  1770. private:
  1771. void UnwrapImpl();
  1772. };
  1773. ////
  1774. bool
  1775. Intersect(int32_t srcSize, int32_t read0, int32_t readSize, int32_t* out_intRead0,
  1776. int32_t* out_intWrite0, int32_t* out_intSize);
  1777. ////
  1778. void
  1779. ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& callback,
  1780. const std::vector<IndexedBufferBinding>& field,
  1781. const char* name, uint32_t flags = 0);
  1782. void
  1783. ImplCycleCollectionUnlink(std::vector<IndexedBufferBinding>& field);
  1784. } // namespace mozilla
  1785. #endif