WebGLContextState.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. #include "WebGLContext.h"
  6. #include "GLContext.h"
  7. #include "GLScreenBuffer.h"
  8. #include "mozilla/dom/ToJSValue.h"
  9. #include "mozilla/Preferences.h"
  10. #include "nsString.h"
  11. #include "WebGLBuffer.h"
  12. #include "WebGLContextUtils.h"
  13. #include "WebGLFramebuffer.h"
  14. #include "WebGLProgram.h"
  15. #include "WebGLRenderbuffer.h"
  16. #include "WebGLShader.h"
  17. #include "WebGLTexture.h"
  18. #include "WebGLVertexArray.h"
  19. namespace mozilla {
  20. void
  21. WebGLContext::Disable(GLenum cap)
  22. {
  23. if (IsContextLost())
  24. return;
  25. if (!ValidateCapabilityEnum(cap, "disable"))
  26. return;
  27. realGLboolean* trackingSlot = GetStateTrackingSlot(cap);
  28. if (trackingSlot)
  29. {
  30. *trackingSlot = 0;
  31. }
  32. MakeContextCurrent();
  33. gl->fDisable(cap);
  34. }
  35. void
  36. WebGLContext::Enable(GLenum cap)
  37. {
  38. if (IsContextLost())
  39. return;
  40. if (!ValidateCapabilityEnum(cap, "enable"))
  41. return;
  42. realGLboolean* trackingSlot = GetStateTrackingSlot(cap);
  43. if (trackingSlot)
  44. {
  45. *trackingSlot = 1;
  46. }
  47. MakeContextCurrent();
  48. gl->fEnable(cap);
  49. }
  50. bool
  51. WebGLContext::GetStencilBits(GLint* const out_stencilBits)
  52. {
  53. *out_stencilBits = 0;
  54. if (mBoundDrawFramebuffer) {
  55. if (mBoundDrawFramebuffer->StencilAttachment().IsDefined() &&
  56. mBoundDrawFramebuffer->DepthStencilAttachment().IsDefined())
  57. {
  58. // Error, we don't know which stencil buffer's bits to use
  59. ErrorInvalidFramebufferOperation("getParameter: framebuffer has two stencil buffers bound");
  60. return false;
  61. }
  62. if (mBoundDrawFramebuffer->StencilAttachment().IsDefined() ||
  63. mBoundDrawFramebuffer->DepthStencilAttachment().IsDefined())
  64. {
  65. *out_stencilBits = 8;
  66. }
  67. } else if (mOptions.stencil) {
  68. *out_stencilBits = 8;
  69. }
  70. return true;
  71. }
  72. bool
  73. WebGLContext::GetChannelBits(const char* funcName, GLenum pname, GLint* const out_val)
  74. {
  75. if (mBoundDrawFramebuffer) {
  76. if (!mBoundDrawFramebuffer->ValidateAndInitAttachments(funcName))
  77. return false;
  78. }
  79. if (!mBoundDrawFramebuffer) {
  80. switch (pname) {
  81. case LOCAL_GL_RED_BITS:
  82. case LOCAL_GL_GREEN_BITS:
  83. case LOCAL_GL_BLUE_BITS:
  84. *out_val = 8;
  85. break;
  86. case LOCAL_GL_ALPHA_BITS:
  87. *out_val = (mOptions.alpha ? 8 : 0);
  88. break;
  89. case LOCAL_GL_DEPTH_BITS:
  90. if (mOptions.depth) {
  91. *out_val = gl->Screen()->DepthBits();
  92. } else {
  93. *out_val = 0;
  94. }
  95. break;
  96. case LOCAL_GL_STENCIL_BITS:
  97. *out_val = (mOptions.stencil ? 8 : 0);
  98. break;
  99. default:
  100. MOZ_CRASH("GFX: bad pname");
  101. }
  102. return true;
  103. }
  104. if (!gl->IsCoreProfile()) {
  105. gl->fGetIntegerv(pname, out_val);
  106. return true;
  107. }
  108. GLenum fbAttachment = 0;
  109. GLenum fbPName = 0;
  110. switch (pname) {
  111. case LOCAL_GL_RED_BITS:
  112. fbAttachment = LOCAL_GL_COLOR_ATTACHMENT0;
  113. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE;
  114. break;
  115. case LOCAL_GL_GREEN_BITS:
  116. fbAttachment = LOCAL_GL_COLOR_ATTACHMENT0;
  117. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE;
  118. break;
  119. case LOCAL_GL_BLUE_BITS:
  120. fbAttachment = LOCAL_GL_COLOR_ATTACHMENT0;
  121. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE;
  122. break;
  123. case LOCAL_GL_ALPHA_BITS:
  124. fbAttachment = LOCAL_GL_COLOR_ATTACHMENT0;
  125. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE;
  126. break;
  127. case LOCAL_GL_DEPTH_BITS:
  128. fbAttachment = LOCAL_GL_DEPTH_ATTACHMENT;
  129. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE;
  130. break;
  131. case LOCAL_GL_STENCIL_BITS:
  132. fbAttachment = LOCAL_GL_STENCIL_ATTACHMENT;
  133. fbPName = LOCAL_GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE;
  134. break;
  135. default:
  136. MOZ_CRASH("GFX: bad pname");
  137. }
  138. gl->fGetFramebufferAttachmentParameteriv(LOCAL_GL_DRAW_FRAMEBUFFER, fbAttachment,
  139. fbPName, out_val);
  140. return true;
  141. }
  142. JS::Value
  143. WebGLContext::GetParameter(JSContext* cx, GLenum pname, ErrorResult& rv)
  144. {
  145. const char funcName[] = "getParameter";
  146. if (IsContextLost())
  147. return JS::NullValue();
  148. MakeContextCurrent();
  149. if (MinCapabilityMode()) {
  150. switch(pname) {
  151. ////////////////////////////
  152. // Single-value params
  153. // int
  154. case LOCAL_GL_MAX_VERTEX_ATTRIBS:
  155. return JS::Int32Value(MINVALUE_GL_MAX_VERTEX_ATTRIBS);
  156. case LOCAL_GL_MAX_FRAGMENT_UNIFORM_VECTORS:
  157. return JS::Int32Value(MINVALUE_GL_MAX_FRAGMENT_UNIFORM_VECTORS);
  158. case LOCAL_GL_MAX_VERTEX_UNIFORM_VECTORS:
  159. return JS::Int32Value(MINVALUE_GL_MAX_VERTEX_UNIFORM_VECTORS);
  160. case LOCAL_GL_MAX_VARYING_VECTORS:
  161. return JS::Int32Value(MINVALUE_GL_MAX_VARYING_VECTORS);
  162. case LOCAL_GL_MAX_TEXTURE_SIZE:
  163. return JS::Int32Value(MINVALUE_GL_MAX_TEXTURE_SIZE);
  164. case LOCAL_GL_MAX_CUBE_MAP_TEXTURE_SIZE:
  165. return JS::Int32Value(MINVALUE_GL_MAX_CUBE_MAP_TEXTURE_SIZE);
  166. case LOCAL_GL_MAX_TEXTURE_IMAGE_UNITS:
  167. return JS::Int32Value(MINVALUE_GL_MAX_TEXTURE_IMAGE_UNITS);
  168. case LOCAL_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
  169. return JS::Int32Value(MINVALUE_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
  170. case LOCAL_GL_MAX_RENDERBUFFER_SIZE:
  171. return JS::Int32Value(MINVALUE_GL_MAX_RENDERBUFFER_SIZE);
  172. default:
  173. // Return the real value; we're not overriding this one
  174. break;
  175. }
  176. }
  177. if (IsWebGL2() || IsExtensionEnabled(WebGLExtensionID::WEBGL_draw_buffers)) {
  178. if (pname == LOCAL_GL_MAX_COLOR_ATTACHMENTS) {
  179. return JS::Int32Value(mImplMaxColorAttachments);
  180. } else if (pname == LOCAL_GL_MAX_DRAW_BUFFERS) {
  181. return JS::Int32Value(mImplMaxDrawBuffers);
  182. } else if (pname >= LOCAL_GL_DRAW_BUFFER0 &&
  183. pname < GLenum(LOCAL_GL_DRAW_BUFFER0 + mImplMaxDrawBuffers))
  184. {
  185. GLint ret = LOCAL_GL_NONE;
  186. if (!mBoundDrawFramebuffer) {
  187. if (pname == LOCAL_GL_DRAW_BUFFER0) {
  188. ret = gl->Screen()->GetDrawBufferMode();
  189. }
  190. } else {
  191. gl->fGetIntegerv(pname, &ret);
  192. }
  193. return JS::Int32Value(ret);
  194. }
  195. }
  196. if (IsWebGL2() || IsExtensionEnabled(WebGLExtensionID::OES_vertex_array_object)) {
  197. if (pname == LOCAL_GL_VERTEX_ARRAY_BINDING) {
  198. WebGLVertexArray* vao =
  199. (mBoundVertexArray != mDefaultVertexArray) ? mBoundVertexArray.get() : nullptr;
  200. return WebGLObjectAsJSValue(cx, vao, rv);
  201. }
  202. }
  203. if (IsExtensionEnabled(WebGLExtensionID::EXT_disjoint_timer_query)) {
  204. switch (pname) {
  205. case LOCAL_GL_TIMESTAMP_EXT:
  206. {
  207. uint64_t val = 0;
  208. if (Has64BitTimestamps()) {
  209. gl->fGetInteger64v(pname, (GLint64*)&val);
  210. } else {
  211. gl->fGetIntegerv(pname, (GLint*)&val);
  212. }
  213. // TODO: JS doesn't support 64-bit integers. Be lossy and
  214. // cast to double (53 bits)
  215. return JS::NumberValue(val);
  216. }
  217. case LOCAL_GL_GPU_DISJOINT_EXT:
  218. {
  219. realGLboolean val = false; // Not disjoint by default.
  220. if (gl->IsExtensionSupported(gl::GLContext::EXT_disjoint_timer_query)) {
  221. gl->fGetBooleanv(pname, &val);
  222. }
  223. return JS::BooleanValue(val);
  224. }
  225. default:
  226. break;
  227. }
  228. }
  229. // Privileged string params exposed by WEBGL_debug_renderer_info.
  230. // The privilege check is done in WebGLContext::IsExtensionSupported.
  231. // So here we just have to check that the extension is enabled.
  232. if (IsExtensionEnabled(WebGLExtensionID::WEBGL_debug_renderer_info)) {
  233. switch (pname) {
  234. case UNMASKED_VENDOR_WEBGL:
  235. case UNMASKED_RENDERER_WEBGL:
  236. {
  237. const char* overridePref = nullptr;
  238. GLenum driverEnum = LOCAL_GL_NONE;
  239. switch (pname) {
  240. case UNMASKED_RENDERER_WEBGL:
  241. overridePref = "webgl.renderer-string-override";
  242. driverEnum = LOCAL_GL_RENDERER;
  243. break;
  244. case UNMASKED_VENDOR_WEBGL:
  245. overridePref = "webgl.vendor-string-override";
  246. driverEnum = LOCAL_GL_VENDOR;
  247. break;
  248. default:
  249. MOZ_CRASH("GFX: bad `pname`");
  250. }
  251. bool hasRetVal = false;
  252. nsAutoString ret;
  253. if (overridePref) {
  254. nsresult res = Preferences::GetString(overridePref, &ret);
  255. if (NS_SUCCEEDED(res) && ret.Length() > 0)
  256. hasRetVal = true;
  257. }
  258. if (!hasRetVal) {
  259. const char* chars = reinterpret_cast<const char*>(gl->fGetString(driverEnum));
  260. ret = NS_ConvertASCIItoUTF16(chars);
  261. hasRetVal = true;
  262. }
  263. return StringValue(cx, ret, rv);
  264. }
  265. }
  266. }
  267. if (IsWebGL2() || IsExtensionEnabled(WebGLExtensionID::OES_standard_derivatives)) {
  268. if (pname == LOCAL_GL_FRAGMENT_SHADER_DERIVATIVE_HINT) {
  269. GLint i = 0;
  270. gl->fGetIntegerv(pname, &i);
  271. return JS::Int32Value(i);
  272. }
  273. }
  274. if (IsExtensionEnabled(WebGLExtensionID::EXT_texture_filter_anisotropic)) {
  275. if (pname == LOCAL_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT) {
  276. GLfloat f = 0.f;
  277. gl->fGetFloatv(pname, &f);
  278. return JS::NumberValue(f);
  279. }
  280. }
  281. switch (pname) {
  282. //
  283. // String params
  284. //
  285. case LOCAL_GL_VENDOR:
  286. case LOCAL_GL_RENDERER:
  287. return StringValue(cx, "Mozilla", rv);
  288. case LOCAL_GL_VERSION:
  289. return StringValue(cx, "WebGL 1.0", rv);
  290. case LOCAL_GL_SHADING_LANGUAGE_VERSION:
  291. return StringValue(cx, "WebGL GLSL ES 1.0", rv);
  292. ////////////////////////////////
  293. // Single-value params
  294. // unsigned int
  295. case LOCAL_GL_CULL_FACE_MODE:
  296. case LOCAL_GL_FRONT_FACE:
  297. case LOCAL_GL_ACTIVE_TEXTURE:
  298. case LOCAL_GL_STENCIL_FUNC:
  299. case LOCAL_GL_STENCIL_FAIL:
  300. case LOCAL_GL_STENCIL_PASS_DEPTH_FAIL:
  301. case LOCAL_GL_STENCIL_PASS_DEPTH_PASS:
  302. case LOCAL_GL_STENCIL_BACK_FUNC:
  303. case LOCAL_GL_STENCIL_BACK_FAIL:
  304. case LOCAL_GL_STENCIL_BACK_PASS_DEPTH_FAIL:
  305. case LOCAL_GL_STENCIL_BACK_PASS_DEPTH_PASS:
  306. case LOCAL_GL_DEPTH_FUNC:
  307. case LOCAL_GL_BLEND_SRC_RGB:
  308. case LOCAL_GL_BLEND_SRC_ALPHA:
  309. case LOCAL_GL_BLEND_DST_RGB:
  310. case LOCAL_GL_BLEND_DST_ALPHA:
  311. case LOCAL_GL_BLEND_EQUATION_RGB:
  312. case LOCAL_GL_BLEND_EQUATION_ALPHA: {
  313. GLint i = 0;
  314. gl->fGetIntegerv(pname, &i);
  315. return JS::NumberValue(uint32_t(i));
  316. }
  317. case LOCAL_GL_GENERATE_MIPMAP_HINT:
  318. return JS::NumberValue(mGenerateMipmapHint);
  319. case LOCAL_GL_IMPLEMENTATION_COLOR_READ_FORMAT:
  320. case LOCAL_GL_IMPLEMENTATION_COLOR_READ_TYPE: {
  321. const webgl::FormatUsageInfo* usage;
  322. uint32_t width, height;
  323. if (!ValidateCurFBForRead(funcName, &usage, &width, &height))
  324. return JS::NullValue();
  325. const auto implPI = ValidImplementationColorReadPI(usage);
  326. GLenum ret;
  327. if (pname == LOCAL_GL_IMPLEMENTATION_COLOR_READ_FORMAT) {
  328. ret = implPI.format;
  329. } else {
  330. ret = implPI.type;
  331. }
  332. return JS::NumberValue(uint32_t(ret));
  333. }
  334. // int
  335. case LOCAL_GL_STENCIL_REF:
  336. case LOCAL_GL_STENCIL_BACK_REF: {
  337. GLint stencilBits = 0;
  338. if (!GetStencilBits(&stencilBits))
  339. return JS::NullValue();
  340. // Assuming stencils have 8 bits
  341. const GLint stencilMask = (1 << stencilBits) - 1;
  342. GLint refValue = 0;
  343. gl->fGetIntegerv(pname, &refValue);
  344. return JS::Int32Value(refValue & stencilMask);
  345. }
  346. case LOCAL_GL_STENCIL_CLEAR_VALUE:
  347. case LOCAL_GL_UNPACK_ALIGNMENT:
  348. case LOCAL_GL_PACK_ALIGNMENT:
  349. case LOCAL_GL_SUBPIXEL_BITS:
  350. case LOCAL_GL_SAMPLE_BUFFERS:
  351. case LOCAL_GL_SAMPLES:
  352. case LOCAL_GL_MAX_VERTEX_ATTRIBS:
  353. case LOCAL_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
  354. case LOCAL_GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
  355. case LOCAL_GL_MAX_TEXTURE_IMAGE_UNITS: {
  356. GLint i = 0;
  357. gl->fGetIntegerv(pname, &i);
  358. return JS::Int32Value(i);
  359. }
  360. case LOCAL_GL_RED_BITS:
  361. case LOCAL_GL_GREEN_BITS:
  362. case LOCAL_GL_BLUE_BITS:
  363. case LOCAL_GL_ALPHA_BITS:
  364. case LOCAL_GL_DEPTH_BITS:
  365. case LOCAL_GL_STENCIL_BITS: {
  366. // Deprecated and removed in GL Core profiles, so special handling required.
  367. GLint val;
  368. if (!GetChannelBits(funcName, pname, &val))
  369. return JS::NullValue();
  370. return JS::Int32Value(val);
  371. }
  372. case LOCAL_GL_MAX_TEXTURE_SIZE:
  373. return JS::Int32Value(mImplMaxTextureSize);
  374. case LOCAL_GL_MAX_CUBE_MAP_TEXTURE_SIZE:
  375. return JS::Int32Value(mImplMaxCubeMapTextureSize);
  376. case LOCAL_GL_MAX_RENDERBUFFER_SIZE:
  377. return JS::Int32Value(mImplMaxRenderbufferSize);
  378. case LOCAL_GL_MAX_VERTEX_UNIFORM_VECTORS:
  379. return JS::Int32Value(mGLMaxVertexUniformVectors);
  380. case LOCAL_GL_MAX_FRAGMENT_UNIFORM_VECTORS:
  381. return JS::Int32Value(mGLMaxFragmentUniformVectors);
  382. case LOCAL_GL_MAX_VARYING_VECTORS:
  383. return JS::Int32Value(mGLMaxVaryingVectors);
  384. case LOCAL_GL_COMPRESSED_TEXTURE_FORMATS: {
  385. uint32_t length = mCompressedTextureFormats.Length();
  386. JSObject* obj = dom::Uint32Array::Create(cx, this, length,
  387. mCompressedTextureFormats.Elements());
  388. if (!obj) {
  389. rv = NS_ERROR_OUT_OF_MEMORY;
  390. }
  391. return JS::ObjectOrNullValue(obj);
  392. }
  393. // unsigned int. here we may have to return very large values like 2^32-1 that can't be represented as
  394. // javascript integer values. We just return them as doubles and javascript doesn't care.
  395. case LOCAL_GL_STENCIL_BACK_VALUE_MASK:
  396. return JS::DoubleValue(mStencilValueMaskBack); // pass as FP value to allow large values such as 2^32-1.
  397. case LOCAL_GL_STENCIL_BACK_WRITEMASK:
  398. return JS::DoubleValue(mStencilWriteMaskBack);
  399. case LOCAL_GL_STENCIL_VALUE_MASK:
  400. return JS::DoubleValue(mStencilValueMaskFront);
  401. case LOCAL_GL_STENCIL_WRITEMASK:
  402. return JS::DoubleValue(mStencilWriteMaskFront);
  403. // float
  404. case LOCAL_GL_LINE_WIDTH:
  405. return JS::DoubleValue(mLineWidth);
  406. case LOCAL_GL_DEPTH_CLEAR_VALUE:
  407. case LOCAL_GL_POLYGON_OFFSET_FACTOR:
  408. case LOCAL_GL_POLYGON_OFFSET_UNITS:
  409. case LOCAL_GL_SAMPLE_COVERAGE_VALUE: {
  410. GLfloat f = 0.f;
  411. gl->fGetFloatv(pname, &f);
  412. return JS::DoubleValue(f);
  413. }
  414. // bool
  415. case LOCAL_GL_BLEND:
  416. case LOCAL_GL_DEPTH_TEST:
  417. case LOCAL_GL_STENCIL_TEST:
  418. case LOCAL_GL_CULL_FACE:
  419. case LOCAL_GL_DITHER:
  420. case LOCAL_GL_POLYGON_OFFSET_FILL:
  421. case LOCAL_GL_SCISSOR_TEST:
  422. case LOCAL_GL_SAMPLE_COVERAGE_INVERT:
  423. case LOCAL_GL_DEPTH_WRITEMASK: {
  424. realGLboolean b = 0;
  425. gl->fGetBooleanv(pname, &b);
  426. return JS::BooleanValue(bool(b));
  427. }
  428. // bool, WebGL-specific
  429. case UNPACK_FLIP_Y_WEBGL:
  430. return JS::BooleanValue(mPixelStore_FlipY);
  431. case UNPACK_PREMULTIPLY_ALPHA_WEBGL:
  432. return JS::BooleanValue(mPixelStore_PremultiplyAlpha);
  433. // uint, WebGL-specific
  434. case UNPACK_COLORSPACE_CONVERSION_WEBGL:
  435. return JS::NumberValue(uint32_t(mPixelStore_ColorspaceConversion));
  436. ////////////////////////////////
  437. // Complex values
  438. // 2 floats
  439. case LOCAL_GL_DEPTH_RANGE:
  440. case LOCAL_GL_ALIASED_POINT_SIZE_RANGE:
  441. case LOCAL_GL_ALIASED_LINE_WIDTH_RANGE: {
  442. GLenum driverPName = pname;
  443. if (gl->IsCoreProfile() &&
  444. driverPName == LOCAL_GL_ALIASED_POINT_SIZE_RANGE)
  445. {
  446. driverPName = LOCAL_GL_POINT_SIZE_RANGE;
  447. }
  448. GLfloat fv[2] = { 0 };
  449. gl->fGetFloatv(driverPName, fv);
  450. JSObject* obj = dom::Float32Array::Create(cx, this, 2, fv);
  451. if (!obj) {
  452. rv = NS_ERROR_OUT_OF_MEMORY;
  453. }
  454. return JS::ObjectOrNullValue(obj);
  455. }
  456. // 4 floats
  457. case LOCAL_GL_COLOR_CLEAR_VALUE:
  458. case LOCAL_GL_BLEND_COLOR: {
  459. GLfloat fv[4] = { 0 };
  460. gl->fGetFloatv(pname, fv);
  461. JSObject* obj = dom::Float32Array::Create(cx, this, 4, fv);
  462. if (!obj) {
  463. rv = NS_ERROR_OUT_OF_MEMORY;
  464. }
  465. return JS::ObjectOrNullValue(obj);
  466. }
  467. // 2 ints
  468. case LOCAL_GL_MAX_VIEWPORT_DIMS: {
  469. GLint iv[2] = { 0 };
  470. gl->fGetIntegerv(pname, iv);
  471. JSObject* obj = dom::Int32Array::Create(cx, this, 2, iv);
  472. if (!obj) {
  473. rv = NS_ERROR_OUT_OF_MEMORY;
  474. }
  475. return JS::ObjectOrNullValue(obj);
  476. }
  477. // 4 ints
  478. case LOCAL_GL_SCISSOR_BOX:
  479. case LOCAL_GL_VIEWPORT: {
  480. GLint iv[4] = { 0 };
  481. gl->fGetIntegerv(pname, iv);
  482. JSObject* obj = dom::Int32Array::Create(cx, this, 4, iv);
  483. if (!obj) {
  484. rv = NS_ERROR_OUT_OF_MEMORY;
  485. }
  486. return JS::ObjectOrNullValue(obj);
  487. }
  488. // 4 bools
  489. case LOCAL_GL_COLOR_WRITEMASK: {
  490. realGLboolean gl_bv[4] = { 0 };
  491. gl->fGetBooleanv(pname, gl_bv);
  492. bool vals[4] = { bool(gl_bv[0]), bool(gl_bv[1]),
  493. bool(gl_bv[2]), bool(gl_bv[3]) };
  494. JS::Rooted<JS::Value> arr(cx);
  495. if (!dom::ToJSValue(cx, vals, &arr)) {
  496. rv = NS_ERROR_OUT_OF_MEMORY;
  497. }
  498. return arr;
  499. }
  500. case LOCAL_GL_ARRAY_BUFFER_BINDING: {
  501. return WebGLObjectAsJSValue(cx, mBoundArrayBuffer.get(), rv);
  502. }
  503. case LOCAL_GL_ELEMENT_ARRAY_BUFFER_BINDING: {
  504. return WebGLObjectAsJSValue(cx, mBoundVertexArray->mElementArrayBuffer.get(), rv);
  505. }
  506. case LOCAL_GL_RENDERBUFFER_BINDING: {
  507. return WebGLObjectAsJSValue(cx, mBoundRenderbuffer.get(), rv);
  508. }
  509. // DRAW_FRAMEBUFFER_BINDING is the same as FRAMEBUFFER_BINDING.
  510. case LOCAL_GL_FRAMEBUFFER_BINDING: {
  511. return WebGLObjectAsJSValue(cx, mBoundDrawFramebuffer.get(), rv);
  512. }
  513. case LOCAL_GL_CURRENT_PROGRAM: {
  514. return WebGLObjectAsJSValue(cx, mCurrentProgram.get(), rv);
  515. }
  516. case LOCAL_GL_TEXTURE_BINDING_2D: {
  517. return WebGLObjectAsJSValue(cx, mBound2DTextures[mActiveTexture].get(), rv);
  518. }
  519. case LOCAL_GL_TEXTURE_BINDING_CUBE_MAP: {
  520. return WebGLObjectAsJSValue(cx, mBoundCubeMapTextures[mActiveTexture].get(), rv);
  521. }
  522. default:
  523. break;
  524. }
  525. ErrorInvalidEnumInfo("getParameter: parameter", pname);
  526. return JS::NullValue();
  527. }
  528. void
  529. WebGLContext::GetParameterIndexed(JSContext* cx, GLenum pname, GLuint index,
  530. JS::MutableHandle<JS::Value> retval)
  531. {
  532. if (IsContextLost()) {
  533. retval.setNull();
  534. return;
  535. }
  536. MakeContextCurrent();
  537. switch (pname) {
  538. case LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
  539. {
  540. if (index >= mGLMaxTransformFeedbackSeparateAttribs) {
  541. ErrorInvalidValue("getParameterIndexed: index should be less than MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS", index);
  542. retval.setNull();
  543. return;
  544. }
  545. retval.setNull(); // See bug 903594
  546. return;
  547. }
  548. default:
  549. break;
  550. }
  551. ErrorInvalidEnumInfo("getParameterIndexed: parameter", pname);
  552. retval.setNull();
  553. }
  554. bool
  555. WebGLContext::IsEnabled(GLenum cap)
  556. {
  557. if (IsContextLost())
  558. return false;
  559. if (!ValidateCapabilityEnum(cap, "isEnabled"))
  560. return false;
  561. MakeContextCurrent();
  562. return gl->fIsEnabled(cap);
  563. }
  564. bool
  565. WebGLContext::ValidateCapabilityEnum(GLenum cap, const char* info)
  566. {
  567. switch (cap) {
  568. case LOCAL_GL_BLEND:
  569. case LOCAL_GL_CULL_FACE:
  570. case LOCAL_GL_DEPTH_TEST:
  571. case LOCAL_GL_DITHER:
  572. case LOCAL_GL_POLYGON_OFFSET_FILL:
  573. case LOCAL_GL_SAMPLE_ALPHA_TO_COVERAGE:
  574. case LOCAL_GL_SAMPLE_COVERAGE:
  575. case LOCAL_GL_SCISSOR_TEST:
  576. case LOCAL_GL_STENCIL_TEST:
  577. return true;
  578. case LOCAL_GL_RASTERIZER_DISCARD:
  579. return IsWebGL2();
  580. default:
  581. ErrorInvalidEnumInfo(info, cap);
  582. return false;
  583. }
  584. }
  585. realGLboolean*
  586. WebGLContext::GetStateTrackingSlot(GLenum cap)
  587. {
  588. switch (cap) {
  589. case LOCAL_GL_DEPTH_TEST:
  590. return &mDepthTestEnabled;
  591. case LOCAL_GL_DITHER:
  592. return &mDitherEnabled;
  593. case LOCAL_GL_RASTERIZER_DISCARD:
  594. return &mRasterizerDiscardEnabled;
  595. case LOCAL_GL_SCISSOR_TEST:
  596. return &mScissorTestEnabled;
  597. case LOCAL_GL_STENCIL_TEST:
  598. return &mStencilTestEnabled;
  599. }
  600. return nullptr;
  601. }
  602. } // namespace mozilla