1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2019
6  *			All rights reserved
7  *
8  *  This file is part of GPAC / WebGL JavaScript extension
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 #include <gpac/setup.h>
27 
28 #ifdef GPAC_HAS_QJS
29 
30 #include "../scenegraph/qjs_common.h"
31 #include "../compositor/gl_inc.h"
32 #include <gpac/filters.h>
33 
34 typedef enum {
35 	WGL_POWER_DEFAULT=0,
36 	WGL_POWER_LOWPOWER,
37 	WGL_POWER_HIGHPERF,
38 } WebGLPowerPreference;
39 
40 typedef enum {
41 	WGL_DEPTH_NO=0,
42 	WGL_DEPTH_YES,
43 	WGL_DEPTH_TEXTURE,
44 } WebGLDepthAttach;
45 
46 typedef struct
47 {
48 	Bool alpha;
49     WebGLDepthAttach depth;
50     Bool stencil;
51     Bool antialias;
52     Bool premultipliedAlpha;
53     Bool preserveDrawingBuffer;
54     WebGLPowerPreference powerPreference;
55     Bool failIfMajorPerformanceCaveat;
56     Bool desynchronized;
57     Bool primary;
58 } GF_WebGLContextAttributes;
59 
60 typedef struct
61 {
62 	GF_FilterFrameInterface tex_f_ifce;
63 	GF_FilterFrameInterface depth_f_ifce;
64 	/*internal*/
65 	GF_WebGLContextAttributes creation_attrs;
66 	GF_WebGLContextAttributes actual_attrs;
67 	GLint tex_id, depth_id, stencil_id, fbo_id;
68 	u32 width, height;
69 
70 	JSValue canvas;
71 
72 	JSContext *ctx;
73 	JSValue tex_frame_flush;
74 	JSValue depth_frame_flush;
75 
76 	GF_List *all_objects;
77 	GF_List *named_textures;
78 	GLint active_program;
79 	GLint active_texture;
80 
81 	struct _wgl_named_texture *bound_named_texture;
82 } GF_WebGLContext;
83 
84 typedef struct
85 {
86 	GLuint gl_id;
87 	GF_WebGLContext *par_ctx;
88 	JSValue obj; //object reference
89 	JSClassID class_id; //object class
90 } GF_WebGLObject;
91 
92 
93 typedef struct _wgl_named_texture
94 {
95 	Bool shader_attached;
96 	GF_WebGLContext *par_ctx;
97 	char *tx_name;
98 
99 	GF_GLTextureWrapper tx;
100 } GF_WebGLNamedTexture;
101 
102 Bool js_evg_get_texture_info(JSContext *ctx, JSValue this_obj, u32 *width, u32 *height, u32 *pixfmt, u8 **p_data, u32 *stride, u8 **p_u, u8 **p_v, u32 *stride_uv, u8 **p_a);
103 
104 Bool js_evg_is_texture(JSContext *ctx, JSValue this_obj);
105 
106 
107 typedef GLuint WebGLContextAttributes;
108 typedef GF_WebGLObject *WebGLProgram;
109 typedef GF_WebGLObject *WebGLShader;
110 typedef GF_WebGLObject *WebGLBuffer;
111 typedef GF_WebGLObject *WebGLFramebuffer;
112 typedef GF_WebGLObject *WebGLRenderbuffer;
113 typedef GF_WebGLObject *WebGLTexture;
114 typedef GF_WebGLObject *WebGLUniformLocation;
115 
116 #define WGL_NO_ERROR	0
117 #define WGL_INVALID_ENUM 0x0500
118 #define WGL_INVALID_VALUE 0x0501
119 #define WGL_INVALID_OPERATION 0x0502
120 #define WGL_OUT_OF_MEMORY 0x0505
121 
122 
123 enum
124 {
125 	WebGLRenderingContextBase_PROP_canvas,
126 	WebGLRenderingContextBase_PROP_drawingBufferWidth,
127 	WebGLRenderingContextBase_PROP_drawingBufferHeight,
128 };
129 
130 
131 #define WGL_GET_BOOL(_name, _jsval) _name = JS_ToBool(ctx, _jsval)
132 #define WGL_GET_U32(_name, _jsval) if (JS_ToInt32(ctx, &_name, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE)
133 #define WGL_GET_S32(_name, _jsval) if (JS_ToInt32(ctx, &_name, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE)
134 #define WGL_GET_U64(_name, _jsval) if (JS_ToInt64(ctx, &_name, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE)
135 #define WGL_GET_S64(_name, _jsval) if (JS_ToInt64(ctx, &_name, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE)
136 #define WGL_GET_S16(_name, _jsval) { s32 _v; if (JS_ToInt32(ctx, &_v, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE); _name = (s16) _v; }
137 #define WGL_GET_U16(_name, _jsval) { s32 _v; if (JS_ToInt32(ctx, &_v, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE); _name = (u16) _v; }
138 #define WGL_GET_FLOAT(_name, _jsval) { Double _v; if (JS_ToFloat64(ctx, &_v, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE); _name = (Float) _v; }
139 #define WGL_GET_FLOAT_CLAMP(_name, _jsval) { Double _v; if (JS_ToFloat64(ctx, &_v, _jsval)) return js_throw_err(ctx, WGL_INVALID_VALUE); _name = (Float) (_v>1.0 ? 1.0 : (_v<0 ? 0.0 : _v) ); }
140 
141 #define WGL_GET_GLID(_name, _jsval, _class_id) {\
142 	if (!JS_IsNull(_jsval)) {\
143 		GF_WebGLObject *_gl_obj = JS_GetOpaque(_jsval, _class_id);\
144 		if (!_gl_obj) return js_throw_err(ctx, WGL_INVALID_VALUE);\
145 		if (_gl_obj->par_ctx != JS_GetOpaque(this_val, WebGLRenderingContextBase_class_id)) return js_throw_err(ctx, WGL_INVALID_OPERATION);\
146 		_name = _gl_obj->gl_id;\
147 	} else {\
148 		_name = 0;\
149 	}\
150 }
151 
152 #define WGL_GET_STRING(_name, _jsval) _name = JS_ToCString(ctx, _jsval)
153 
154 #define WGL_CHECK_CONTEXT \
155 	if (!JS_GetOpaque(this_val, WebGLRenderingContextBase_class_id)) return js_throw_err(ctx, WGL_INVALID_OPERATION);
156 
157 
158 #endif
159 
160