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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use super::{gl, BackendAPI, MipMapped};
use crate::prelude::*;
use skia_bindings as sb;
use skia_bindings::{GrBackendFormat, GrBackendRenderTarget, GrBackendTexture, GrMipMapped};

#[cfg(feature = "vulkan")]
use super::vk;

pub type BackendFormat = Handle<GrBackendFormat>;

impl NativeDrop for GrBackendFormat {
    fn drop(&mut self) {
        unsafe { sb::C_GrBackendFormat_destruct(self) }
    }
}

impl NativeClone for GrBackendFormat {
    fn clone(&self) -> Self {
        unsafe { GrBackendFormat::new(self) }
    }
}

impl Default for BackendFormat {
    fn default() -> Self {
        Self::new()
    }
}

impl Handle<GrBackendFormat> {
    pub fn new() -> Self {
        Self::construct(|bf| unsafe { sb::C_GrBackendFormat_Construct(bf) })
    }

    pub fn new_gl(format: gl::Enum, target: gl::Enum) -> Self {
        Self::construct(|bf| unsafe { sb::C_GrBackendFormat_ConstructGL(bf, format, target) })
    }

    #[cfg(feature = "vulkan")]
    pub fn new_vulkan(format: vk::Format) -> Self {
        Self::construct(|bf| unsafe { sb::C_GrBackendFormat_ConstructVk(bf, format) })
    }

    #[cfg(feature = "vulkan")]
    pub fn new_vulkan_ycbcr(conversion_info: &vk::YcbcrConversionInfo) -> Self {
        Self::construct(|bf| unsafe {
            sb::C_GrBackendFormat_ConstructVk2(bf, conversion_info.native())
        })
    }

    pub fn backend_api(&self) -> BackendAPI {
        BackendAPI::from_native(self.native().fBackend)
    }

    pub fn gl_format(&self) -> Option<gl::Enum> {
        unsafe {
            #[allow(clippy::map_clone)]
            self.native()
                .getGLFormat()
                .into_option()
                .map(|format| *format)
        }
    }

    pub fn gl_target(&self) -> Option<gl::Enum> {
        unsafe {
            #[allow(clippy::map_clone)]
            self.native()
                .getGLTarget()
                .into_option()
                .map(|target| *target)
        }
    }

    #[cfg(feature = "vulkan")]
    pub fn vulkan_format(&self) -> Option<vk::Format> {
        unsafe {
            #[allow(clippy::map_clone)]
            self.native()
                .getVkFormat()
                .into_option()
                .map(|format| *format)
        }
    }

    pub fn to_texture_2d(&self) -> Option<Self> {
        let new = Self::from_native(unsafe { self.native().makeTexture2D() });

        new.is_valid().if_true_some(new)
    }

    pub fn is_valid(&self) -> bool {
        self.native().fValid
    }
}

pub type BackendTexture = Handle<GrBackendTexture>;

impl NativeDrop for GrBackendTexture {
    fn drop(&mut self) {
        unsafe { sb::C_GrBackendTexture_destruct(self) }
    }
}

impl NativeClone for GrBackendTexture {
    fn clone(&self) -> Self {
        unsafe { GrBackendTexture::new3(self) }
    }
}

impl Handle<GrBackendTexture> {
    pub unsafe fn new_gl(
        (width, height): (i32, i32),
        mip_mapped: MipMapped,
        gl_info: gl::TextureInfo,
    ) -> BackendTexture {
        Self::from_native_if_valid(GrBackendTexture::new(
            width,
            height,
            mip_mapped.into_native(),
            gl_info.native(),
        ))
        .unwrap()
    }

    #[cfg(feature = "vulkan")]
    pub unsafe fn new_vulkan(
        (width, height): (i32, i32),
        vk_info: &vk::ImageInfo,
    ) -> BackendTexture {
        Self::from_native_if_valid(GrBackendTexture::new1(width, height, vk_info.native())).unwrap()
    }

    pub(crate) unsafe fn from_native_if_valid(
        backend_texture: GrBackendTexture,
    ) -> Option<BackendTexture> {
        backend_texture
            .fIsValid
            .if_true_then_some(|| BackendTexture::from_native(backend_texture))
    }

    pub fn width(&self) -> i32 {
        self.native().fWidth
    }

    pub fn height(&self) -> i32 {
        self.native().fHeight
    }

    pub fn has_mip_maps(&self) -> bool {
        self.native().fMipMapped == GrMipMapped::kYes
    }

    pub fn backend(&self) -> BackendAPI {
        BackendAPI::from_native(self.native().fBackend)
    }

    pub fn gl_texture_info(&self) -> Option<gl::TextureInfo> {
        unsafe {
            let mut texture_info = gl::TextureInfo::default();
            self.native()
                .getGLTextureInfo(texture_info.native_mut())
                .if_true_some(texture_info)
        }
    }

    pub fn gl_texture_parameters_modified(&mut self) {
        unsafe { self.native_mut().glTextureParametersModified() }
    }

    #[cfg(feature = "vulkan")]
    pub fn vulkan_image_info(&self) -> Option<vk::ImageInfo> {
        unsafe {
            // constructor not available.
            let mut image_info = vk::ImageInfo::default();
            self.native()
                .getVkImageInfo(image_info.native_mut())
                .if_true_some(image_info)
        }
    }

    #[cfg(feature = "vulkan")]
    pub fn set_vulkan_image_layout(&mut self, layout: vk::ImageLayout) -> &mut Self {
        unsafe { self.native_mut().setVkImageLayout(layout) }
        self
    }

    pub fn backend_format(&self) -> Option<BackendFormat> {
        let format = BackendFormat::from_native(unsafe { self.native().getBackendFormat() });

        format.is_valid().if_true_some(format)
    }

    pub fn is_protected(&self) -> bool {
        unsafe { self.native().isProtected() }
    }

    pub fn is_valid(&self) -> bool {
        self.native().fIsValid
    }

    #[allow(clippy::wrong_self_convention)]
    pub fn is_same_texture(&mut self, texture: &BackendTexture) -> bool {
        unsafe { self.native_mut().isSameTexture(texture.native()) }
    }
}

pub type BackendRenderTarget = Handle<GrBackendRenderTarget>;

impl NativeDrop for GrBackendRenderTarget {
    fn drop(&mut self) {
        unsafe { sb::C_GrBackendRenderTarget_destruct(self) }
    }
}

impl NativeClone for GrBackendRenderTarget {
    fn clone(&self) -> Self {
        unsafe { GrBackendRenderTarget::new4(self) }
    }
}

impl Handle<GrBackendRenderTarget> {
    pub fn new_gl(
        (width, height): (i32, i32),
        sample_count: impl Into<Option<usize>>,
        stencil_bits: usize,
        info: gl::FramebufferInfo,
    ) -> BackendRenderTarget {
        Self::from_native(unsafe {
            GrBackendRenderTarget::new(
                width,
                height,
                sample_count.into().unwrap_or(0).try_into().unwrap(),
                stencil_bits.try_into().unwrap(),
                info.native(),
            )
        })
    }

    #[cfg(feature = "vulkan")]
    pub fn new_vulkan(
        (width, height): (i32, i32),
        sample_count: impl Into<Option<usize>>,
        info: &vk::ImageInfo,
    ) -> BackendRenderTarget {
        BackendRenderTarget::from_native(unsafe {
            GrBackendRenderTarget::new2(
                width,
                height,
                sample_count.into().unwrap_or(0).try_into().unwrap(),
                info.native(),
            )
        })
    }

    pub(crate) fn from_native_if_valid(
        native: GrBackendRenderTarget,
    ) -> Option<BackendRenderTarget> {
        let backend_render_target = BackendRenderTarget::from_native(native);
        backend_render_target
            .is_valid()
            .if_true_some(backend_render_target)
    }

    pub fn width(&self) -> i32 {
        self.native().fWidth
    }

    pub fn height(&self) -> i32 {
        self.native().fHeight
    }

    pub fn sample_count(&self) -> usize {
        self.native().fSampleCnt.try_into().unwrap()
    }

    pub fn stencil_bits(&self) -> usize {
        self.native().fStencilBits.try_into().unwrap()
    }

    pub fn backend(&self) -> BackendAPI {
        BackendAPI::from_native(self.native().fBackend)
    }

    pub fn gl_framebuffer_info(&self) -> Option<gl::FramebufferInfo> {
        let mut info = gl::FramebufferInfo::default();
        unsafe { self.native().getGLFramebufferInfo(info.native_mut()) }.if_true_some(info)
    }

    #[cfg(feature = "vulkan")]
    pub fn vulkan_image_info(&self) -> Option<vk::ImageInfo> {
        let mut info = vk::ImageInfo::default();
        unsafe { self.native().getVkImageInfo(info.native_mut()) }.if_true_some(info)
    }

    #[cfg(feature = "vulkan")]
    pub fn set_vulkan_image_layout(&mut self, layout: vk::ImageLayout) -> &mut Self {
        unsafe { self.native_mut().setVkImageLayout(layout) }
        self
    }

    pub fn backend_format(&self) -> BackendFormat {
        BackendFormat::from_native(unsafe { self.native().getBackendFormat() })
    }

    pub fn is_protected(&self) -> bool {
        unsafe { self.native().isProtected() }
    }

    pub fn is_valid(&self) -> bool {
        self.native().fIsValid
    }
}