1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 use font::{FontInstanceKey, FontKey, FontTemplate};
6 use std::sync::Arc;
7 use {DevicePoint, DeviceUintRect, IdNamespace, TileOffset, TileSize};
8 
9 #[repr(C)]
10 #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
11 pub struct ImageKey(pub IdNamespace, pub u32);
12 
13 impl ImageKey {
14     pub const DUMMY: Self = ImageKey(IdNamespace(0), 0);
15 
new(namespace: IdNamespace, key: u32) -> Self16     pub fn new(namespace: IdNamespace, key: u32) -> Self {
17         ImageKey(namespace, key)
18     }
19 }
20 
21 /// An arbitrary identifier for an external image provided by the
22 /// application. It must be a unique identifier for each external
23 /// image.
24 #[repr(C)]
25 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
26 pub struct ExternalImageId(pub u64);
27 
28 #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
29 pub enum TextureTarget {
30     Default = 0,
31     Array = 1,
32     Rect = 2,
33     External = 3,
34 }
35 
36 #[repr(u32)]
37 #[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
38 pub enum ExternalImageType {
39     TextureHandle(TextureTarget),
40     Buffer,
41 }
42 
43 #[repr(C)]
44 #[derive(Debug, Copy, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
45 pub struct ExternalImageData {
46     pub id: ExternalImageId,
47     pub channel_index: u8,
48     pub image_type: ExternalImageType,
49 }
50 
51 #[repr(u32)]
52 #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
53 pub enum ImageFormat {
54     R8 = 1,
55     BGRA8 = 3,
56     RGBAF32 = 4,
57     RG8 = 5,
58 }
59 
60 impl ImageFormat {
bytes_per_pixel(self) -> u3261     pub fn bytes_per_pixel(self) -> u32 {
62         match self {
63             ImageFormat::R8 => 1,
64             ImageFormat::BGRA8 => 4,
65             ImageFormat::RGBAF32 => 16,
66             ImageFormat::RG8 => 2,
67         }
68     }
69 }
70 
71 #[derive(Copy, Clone, Debug, Deserialize, PartialEq, Serialize)]
72 pub struct ImageDescriptor {
73     pub format: ImageFormat,
74     pub width: u32,
75     pub height: u32,
76     pub stride: Option<u32>,
77     pub offset: u32,
78     pub is_opaque: bool,
79 }
80 
81 impl ImageDescriptor {
new(width: u32, height: u32, format: ImageFormat, is_opaque: bool) -> Self82     pub fn new(width: u32, height: u32, format: ImageFormat, is_opaque: bool) -> Self {
83         ImageDescriptor {
84             width,
85             height,
86             format,
87             stride: None,
88             offset: 0,
89             is_opaque,
90         }
91     }
92 
compute_stride(&self) -> u3293     pub fn compute_stride(&self) -> u32 {
94         self.stride
95             .unwrap_or(self.width * self.format.bytes_per_pixel())
96     }
97 
compute_total_size(&self) -> u3298     pub fn compute_total_size(&self) -> u32 {
99         self.compute_stride() * self.height
100     }
101 }
102 
103 #[derive(Clone, Debug, Serialize, Deserialize)]
104 pub enum ImageData {
105     Raw(Arc<Vec<u8>>),
106     Blob(BlobImageData),
107     External(ExternalImageData),
108 }
109 
110 impl ImageData {
new(bytes: Vec<u8>) -> Self111     pub fn new(bytes: Vec<u8>) -> Self {
112         ImageData::Raw(Arc::new(bytes))
113     }
114 
new_shared(bytes: Arc<Vec<u8>>) -> Self115     pub fn new_shared(bytes: Arc<Vec<u8>>) -> Self {
116         ImageData::Raw(bytes)
117     }
118 
new_blob_image(commands: Vec<u8>) -> Self119     pub fn new_blob_image(commands: Vec<u8>) -> Self {
120         ImageData::Blob(commands)
121     }
122 
123     #[inline]
is_blob(&self) -> bool124     pub fn is_blob(&self) -> bool {
125         match self {
126             &ImageData::Blob(_) => true,
127             _ => false,
128         }
129     }
130 
131     #[inline]
uses_texture_cache(&self) -> bool132     pub fn uses_texture_cache(&self) -> bool {
133         match *self {
134             ImageData::External(ref ext_data) => match ext_data.image_type {
135                 ExternalImageType::TextureHandle(_) => false,
136                 ExternalImageType::Buffer => true,
137             },
138             ImageData::Blob(_) => true,
139             ImageData::Raw(_) => true,
140         }
141     }
142 }
143 
144 pub trait BlobImageResources {
get_font_data(&self, key: FontKey) -> &FontTemplate145     fn get_font_data(&self, key: FontKey) -> &FontTemplate;
get_image(&self, key: ImageKey) -> Option<(&ImageData, &ImageDescriptor)>146     fn get_image(&self, key: ImageKey) -> Option<(&ImageData, &ImageDescriptor)>;
147 }
148 
149 pub trait BlobImageRenderer: Send {
add(&mut self, key: ImageKey, data: BlobImageData, tiling: Option<TileSize>)150     fn add(&mut self, key: ImageKey, data: BlobImageData, tiling: Option<TileSize>);
151 
update(&mut self, key: ImageKey, data: BlobImageData, dirty_rect: Option<DeviceUintRect>)152     fn update(&mut self, key: ImageKey, data: BlobImageData, dirty_rect: Option<DeviceUintRect>);
153 
delete(&mut self, key: ImageKey)154     fn delete(&mut self, key: ImageKey);
155 
request( &mut self, services: &BlobImageResources, key: BlobImageRequest, descriptor: &BlobImageDescriptor, dirty_rect: Option<DeviceUintRect>, )156     fn request(
157         &mut self,
158         services: &BlobImageResources,
159         key: BlobImageRequest,
160         descriptor: &BlobImageDescriptor,
161         dirty_rect: Option<DeviceUintRect>,
162     );
163 
resolve(&mut self, key: BlobImageRequest) -> BlobImageResult164     fn resolve(&mut self, key: BlobImageRequest) -> BlobImageResult;
165 
delete_font(&mut self, key: FontKey)166     fn delete_font(&mut self, key: FontKey);
167 
delete_font_instance(&mut self, key: FontInstanceKey)168     fn delete_font_instance(&mut self, key: FontInstanceKey);
169 }
170 
171 pub type BlobImageData = Vec<u8>;
172 
173 pub type BlobImageResult = Result<RasterizedBlobImage, BlobImageError>;
174 
175 #[repr(C)]
176 #[derive(Copy, Clone, Debug)]
177 pub struct BlobImageDescriptor {
178     pub width: u32,
179     pub height: u32,
180     pub offset: DevicePoint,
181     pub format: ImageFormat,
182 }
183 
184 pub struct RasterizedBlobImage {
185     pub width: u32,
186     pub height: u32,
187     pub data: Vec<u8>,
188 }
189 
190 #[derive(Clone, Debug)]
191 pub enum BlobImageError {
192     Oom,
193     InvalidKey,
194     InvalidData,
195     Other(String),
196 }
197 
198 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
199 pub struct BlobImageRequest {
200     pub key: ImageKey,
201     pub tile: Option<TileOffset>,
202 }
203