1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
use std::cell::Cell;

use canvas_traits::webgl::{
    webgl_channel, GlType, InternalFormatIntVec, WebGLCommand, WebGLError, WebGLRenderbufferId,
    WebGLResult, WebGLVersion,
};
use dom_struct::dom_struct;

use crate::dom::bindings::codegen::Bindings::EXTColorBufferHalfFloatBinding::EXTColorBufferHalfFloatConstants;
use crate::dom::bindings::codegen::Bindings::WEBGLColorBufferFloatBinding::WEBGLColorBufferFloatConstants;
use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::webglframebuffer::WebGLFramebuffer;
use crate::dom::webglobject::WebGLObject;
use crate::dom::webglrenderingcontext::{Operation, WebGLRenderingContext};
use crate::script_runtime::CanGc;

#[dom_struct]
pub struct WebGLRenderbuffer {
    webgl_object: WebGLObject,
    #[no_trace]
    id: WebGLRenderbufferId,
    ever_bound: Cell<bool>,
    is_deleted: Cell<bool>,
    size: Cell<Option<(i32, i32)>>,
    internal_format: Cell<Option<u32>>,
    is_initialized: Cell<bool>,
    attached_framebuffer: MutNullableDom<WebGLFramebuffer>,
}

impl WebGLRenderbuffer {
    fn new_inherited(context: &WebGLRenderingContext, id: WebGLRenderbufferId) -> Self {
        Self {
            webgl_object: WebGLObject::new_inherited(context),
            id,
            ever_bound: Cell::new(false),
            is_deleted: Cell::new(false),
            internal_format: Cell::new(None),
            size: Cell::new(None),
            is_initialized: Cell::new(false),
            attached_framebuffer: Default::default(),
        }
    }

    pub fn maybe_new(context: &WebGLRenderingContext) -> Option<DomRoot<Self>> {
        let (sender, receiver) = webgl_channel().unwrap();
        context.send_command(WebGLCommand::CreateRenderbuffer(sender));
        receiver
            .recv()
            .unwrap()
            .map(|id| WebGLRenderbuffer::new(context, id))
    }

    pub fn new(context: &WebGLRenderingContext, id: WebGLRenderbufferId) -> DomRoot<Self> {
        reflect_dom_object(
            Box::new(WebGLRenderbuffer::new_inherited(context, id)),
            &*context.global(),
            CanGc::note(),
        )
    }
}

impl WebGLRenderbuffer {
    pub fn id(&self) -> WebGLRenderbufferId {
        self.id
    }

    pub fn size(&self) -> Option<(i32, i32)> {
        self.size.get()
    }

    pub fn internal_format(&self) -> u32 {
        self.internal_format.get().unwrap_or(constants::RGBA4)
    }

    pub fn mark_initialized(&self) {
        self.is_initialized.set(true);
    }

    pub fn is_initialized(&self) -> bool {
        self.is_initialized.get()
    }

    pub fn bind(&self, target: u32) {
        self.ever_bound.set(true);
        self.upcast::<WebGLObject>()
            .context()
            .send_command(WebGLCommand::BindRenderbuffer(target, Some(self.id)));
    }

    pub fn delete(&self, operation_fallibility: Operation) {
        if !self.is_deleted.get() {
            self.is_deleted.set(true);

            let context = self.upcast::<WebGLObject>().context();

            /*
            If a renderbuffer object is deleted while its image is attached to one or more
            attachment points in a currently bound framebuffer object, then it is as if
            FramebufferRenderbuffer had been called, with a renderbuffer of zero, for each
            attachment point to which this image was attached in that framebuffer object.
            In other words,the renderbuffer image is first detached from all attachment points
            in that frame-buffer object.
            - GLES 3.0, 4.4.2.3, "Attaching Renderbuffer Images to a Framebuffer"
            */
            if let Some(fb) = context.get_draw_framebuffer_slot().get() {
                let _ = fb.detach_renderbuffer(self);
            }
            if let Some(fb) = context.get_read_framebuffer_slot().get() {
                let _ = fb.detach_renderbuffer(self);
            }

            let cmd = WebGLCommand::DeleteRenderbuffer(self.id);
            match operation_fallibility {
                Operation::Fallible => context.send_command_ignored(cmd),
                Operation::Infallible => context.send_command(cmd),
            }
        }
    }

    pub fn is_deleted(&self) -> bool {
        self.is_deleted.get()
    }

    pub fn ever_bound(&self) -> bool {
        self.ever_bound.get()
    }

    pub fn storage(
        &self,
        api_type: GlType,
        sample_count: i32,
        internal_format: u32,
        width: i32,
        height: i32,
    ) -> WebGLResult<()> {
        let is_gles = api_type == GlType::Gles;
        let webgl_version = self.upcast().context().webgl_version();

        // Validate the internal_format, and save it for completeness
        // validation.
        let actual_format = match internal_format {
            constants::RGBA4 | constants::DEPTH_COMPONENT16 | constants::STENCIL_INDEX8 => {
                internal_format
            },
            constants::R8 |
            constants::R8UI |
            constants::R8I |
            constants::R16UI |
            constants::R16I |
            constants::R32UI |
            constants::R32I |
            constants::RG8 |
            constants::RG8UI |
            constants::RG8I |
            constants::RG16UI |
            constants::RG16I |
            constants::RG32UI |
            constants::RG32I |
            constants::RGB8 |
            constants::RGBA8 |
            constants::SRGB8_ALPHA8 |
            constants::RGB10_A2 |
            constants::RGBA8UI |
            constants::RGBA8I |
            constants::RGB10_A2UI |
            constants::RGBA16UI |
            constants::RGBA16I |
            constants::RGBA32I |
            constants::RGBA32UI |
            constants::DEPTH_COMPONENT24 |
            constants::DEPTH_COMPONENT32F |
            constants::DEPTH24_STENCIL8 |
            constants::DEPTH32F_STENCIL8 => match webgl_version {
                WebGLVersion::WebGL1 => return Err(WebGLError::InvalidEnum),
                _ => internal_format,
            },
            // https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.8
            constants::DEPTH_STENCIL => constants::DEPTH24_STENCIL8,
            constants::RGB5_A1 => {
                // 16-bit RGBA formats are not supported on desktop GL.
                if is_gles {
                    constants::RGB5_A1
                } else {
                    constants::RGBA8
                }
            },
            constants::RGB565 => {
                // RGB565 is not supported on desktop GL.
                if is_gles {
                    constants::RGB565
                } else {
                    constants::RGB8
                }
            },
            EXTColorBufferHalfFloatConstants::RGBA16F_EXT |
            EXTColorBufferHalfFloatConstants::RGB16F_EXT => {
                if !self
                    .upcast()
                    .context()
                    .extension_manager()
                    .is_half_float_buffer_renderable()
                {
                    return Err(WebGLError::InvalidEnum);
                }
                internal_format
            },
            WEBGLColorBufferFloatConstants::RGBA32F_EXT => {
                if !self
                    .upcast()
                    .context()
                    .extension_manager()
                    .is_float_buffer_renderable()
                {
                    return Err(WebGLError::InvalidEnum);
                }
                internal_format
            },
            _ => return Err(WebGLError::InvalidEnum),
        };

        if webgl_version != WebGLVersion::WebGL1 {
            let (sender, receiver) = webgl_channel().unwrap();
            self.upcast::<WebGLObject>().context().send_command(
                WebGLCommand::GetInternalFormatIntVec(
                    constants::RENDERBUFFER,
                    internal_format,
                    InternalFormatIntVec::Samples,
                    sender,
                ),
            );
            let samples = receiver.recv().unwrap();
            if sample_count < 0 || sample_count > samples.first().cloned().unwrap_or(0) {
                return Err(WebGLError::InvalidOperation);
            }
        }

        self.internal_format.set(Some(internal_format));
        self.is_initialized.set(false);

        if let Some(fb) = self.attached_framebuffer.get() {
            fb.update_status();
        }

        let command = match sample_count {
            0 => WebGLCommand::RenderbufferStorage(
                constants::RENDERBUFFER,
                actual_format,
                width,
                height,
            ),
            _ => WebGLCommand::RenderbufferStorageMultisample(
                constants::RENDERBUFFER,
                sample_count,
                actual_format,
                width,
                height,
            ),
        };
        self.upcast::<WebGLObject>().context().send_command(command);

        self.size.set(Some((width, height)));
        Ok(())
    }

    pub fn attach_to_framebuffer(&self, fb: &WebGLFramebuffer) {
        self.attached_framebuffer.set(Some(fb));
    }

    pub fn detach_from_framebuffer(&self) {
        self.attached_framebuffer.set(None);
    }
}

impl Drop for WebGLRenderbuffer {
    fn drop(&mut self) {
        self.delete(Operation::Fallible);
    }
}