@@ -6,6 +6,16 @@ use canvas_core::image_asset::ImageAsset;
66use crate :: context:: Context ;
77
88impl Context {
9+ #[ cfg( feature = "2d" ) ]
10+ fn promote_to_gpu ( & mut self , img : Image ) -> Image {
11+ if let Some ( ctx) = self . direct_context . as_mut ( ) {
12+ img. new_texture_image ( ctx, skia_safe:: gpu:: Mipmapped :: No )
13+ . unwrap_or ( img)
14+ } else {
15+ img
16+ }
17+ }
18+
919 pub fn draw_image_asset_src_xywh_dst_xywh (
1020 & mut self ,
1121 image : & ImageAsset ,
@@ -18,24 +28,33 @@ impl Context {
1828 dst_width : f32 ,
1929 dst_height : f32 ,
2030 ) {
31+ #[ cfg( feature = "2d" ) ]
32+ if let Some ( img) = image. raster_image ( ) {
33+ let img = self . promote_to_gpu ( img) ;
34+ self . draw_image_src_xywh_dst_xywh (
35+ & img, src_x, src_y, src_width, src_height,
36+ dst_x, dst_y, dst_width, dst_height,
37+ ) ;
38+ return ;
39+ }
40+
2141 image. with_skia_image_bytes ( |image, bytes| {
2242 if let Some ( image) = image {
2343 self . draw_image_src_xywh_dst_xywh (
24- & image, src_x, src_y, src_width, src_height, dst_x, dst_y, dst_width, dst_height,
44+ image, src_x, src_y, src_width, src_height,
45+ dst_x, dst_y, dst_width, dst_height,
2546 )
26- } else {
27- if let Some ( ( dimensions, bytes) ) = bytes {
28- let ( width, height) = dimensions;
29- if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
30- bytes,
31- width as i32 ,
32- height as i32 ,
33- ) {
34- self . draw_image_src_xywh_dst_xywh (
35- & image, src_x, src_y, src_width, src_height, dst_x, dst_y, dst_width,
36- dst_height,
37- )
38- }
47+ } else if let Some ( ( dimensions, bytes) ) = bytes {
48+ let ( width, height) = dimensions;
49+ if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
50+ bytes,
51+ width as i32 ,
52+ height as i32 ,
53+ ) {
54+ self . draw_image_src_xywh_dst_xywh (
55+ & image, src_x, src_y, src_width, src_height,
56+ dst_x, dst_y, dst_width, dst_height,
57+ )
3958 }
4059 }
4160 } ) ;
@@ -47,38 +66,51 @@ impl Context {
4766 src_rect : impl Into < Rect > ,
4867 dst_rect : impl Into < Rect > ,
4968 ) {
69+ let src_rect = src_rect. into ( ) ;
70+ let dst_rect = dst_rect. into ( ) ;
71+
72+ #[ cfg( feature = "2d" ) ]
73+ if let Some ( img) = image. raster_image ( ) {
74+ let img = self . promote_to_gpu ( img) ;
75+ self . draw_image ( & img, src_rect, dst_rect) ;
76+ return ;
77+ }
78+
5079 image. with_skia_image_bytes ( |image, bytes| {
5180 if let Some ( image) = image {
52- self . draw_image ( & image, src_rect, dst_rect)
53- } else {
54- if let Some ( ( dimensions, bytes) ) = bytes {
55- let ( width, height) = dimensions;
56- if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
57- bytes,
58- width as i32 ,
59- height as i32 ,
60- ) {
61- self . draw_image ( & image, src_rect, dst_rect)
62- }
81+ self . draw_image ( image, src_rect, dst_rect)
82+ } else if let Some ( ( dimensions, bytes) ) = bytes {
83+ let ( width, height) = dimensions;
84+ if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
85+ bytes,
86+ width as i32 ,
87+ height as i32 ,
88+ ) {
89+ self . draw_image ( & image, src_rect, dst_rect)
6390 }
6491 }
6592 } ) ;
6693 }
6794
6895 pub fn draw_image_asset_dx_dy ( & mut self , image : & ImageAsset , x : f32 , y : f32 ) {
96+ #[ cfg( feature = "2d" ) ]
97+ if let Some ( img) = image. raster_image ( ) {
98+ let img = self . promote_to_gpu ( img) ;
99+ self . draw_image_dx_dy ( & img, x, y) ;
100+ return ;
101+ }
102+
69103 image. with_skia_image_bytes ( |image, bytes| {
70104 if let Some ( image) = image {
71- self . draw_image_dx_dy ( & image, x, y)
72- } else {
73- if let Some ( ( dimensions, bytes) ) = bytes {
74- let ( width, height) = dimensions;
75- if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
76- bytes,
77- width as i32 ,
78- height as i32 ,
79- ) {
80- self . draw_image_dx_dy ( & image, x, y)
81- }
105+ self . draw_image_dx_dy ( image, x, y)
106+ } else if let Some ( ( dimensions, bytes) ) = bytes {
107+ let ( width, height) = dimensions;
108+ if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
109+ bytes,
110+ width as i32 ,
111+ height as i32 ,
112+ ) {
113+ self . draw_image_dx_dy ( & image, x, y)
82114 }
83115 }
84116 } ) ;
@@ -92,36 +124,48 @@ impl Context {
92124 width : f32 ,
93125 height : f32 ,
94126 ) {
127+ #[ cfg( feature = "2d" ) ]
128+ if let Some ( img) = image. raster_image ( ) {
129+ let img = self . promote_to_gpu ( img) ;
130+ self . draw_image_dx_dy_dw_dh ( & img, x, y, width, height) ;
131+ return ;
132+ }
133+
95134 image. with_skia_image_bytes ( |image, bytes| {
96135 if let Some ( image) = image {
97- self . draw_image_dx_dy_dw_dh ( & image, x, y, width, height)
98- } else {
99- if let Some ( ( dimensions, bytes) ) = bytes {
100- let ( w, h) = dimensions;
101- if let Some ( image) =
102- crate :: utils:: image:: from_image_slice_no_copy ( bytes, w as i32 , h as i32 )
103- {
104- self . draw_image_dx_dy_dw_dh ( & image, x, y, width, height)
105- }
136+ self . draw_image_dx_dy_dw_dh ( image, x, y, width, height)
137+ } else if let Some ( ( dimensions, bytes) ) = bytes {
138+ let ( w, h) = dimensions;
139+ if let Some ( image) =
140+ crate :: utils:: image:: from_image_slice_no_copy ( bytes, w as i32 , h as i32 )
141+ {
142+ self . draw_image_dx_dy_dw_dh ( & image, x, y, width, height)
106143 }
107144 }
108145 } ) ;
109146 }
110147
111148 pub fn draw_image_asset_with_rect ( & mut self , image : & ImageAsset , dst_rect : impl Into < Rect > ) {
149+ let dst_rect = dst_rect. into ( ) ;
150+
151+ #[ cfg( feature = "2d" ) ]
152+ if let Some ( img) = image. raster_image ( ) {
153+ let img = self . promote_to_gpu ( img) ;
154+ self . draw_image_with_rect ( & img, dst_rect) ;
155+ return ;
156+ }
157+
112158 image. with_skia_image_bytes ( |image, bytes| {
113159 if let Some ( image) = image {
114- self . draw_image_with_rect ( & image, dst_rect)
115- } else {
116- if let Some ( ( dimensions, bytes) ) = bytes {
117- let ( width, height) = dimensions;
118- if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
119- bytes,
120- width as i32 ,
121- height as i32 ,
122- ) {
123- self . draw_image_with_rect ( & image, dst_rect)
124- }
160+ self . draw_image_with_rect ( image, dst_rect)
161+ } else if let Some ( ( dimensions, bytes) ) = bytes {
162+ let ( width, height) = dimensions;
163+ if let Some ( image) = crate :: utils:: image:: from_image_slice_no_copy (
164+ bytes,
165+ width as i32 ,
166+ height as i32 ,
167+ ) {
168+ self . draw_image_with_rect ( & image, dst_rect)
125169 }
126170 }
127171 } ) ;
@@ -149,7 +193,8 @@ impl Context {
149193 pub fn draw_image_dx_dy ( & mut self , image : & Image , x : f32 , y : f32 ) {
150194 #[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
151195 let _ = unsafe { objc2_foundation:: NSAutoreleasePool :: new ( ) } ;
152- #[ cfg( feature = "gl" ) ] {
196+ #[ cfg( feature = "gl" ) ]
197+ {
153198 if let Some ( ref context) = self . gl_context {
154199 context. make_current ( ) ;
155200 }
@@ -178,7 +223,8 @@ impl Context {
178223 fn draw_image ( & mut self , image : & Image , src_rect : impl Into < Rect > , dst_rect : impl Into < Rect > ) {
179224 #[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
180225 let _ = unsafe { objc2_foundation:: NSAutoreleasePool :: new ( ) } ;
181- #[ cfg( feature = "gl" ) ] {
226+ #[ cfg( feature = "gl" ) ]
227+ {
182228 if let Some ( ref context) = self . gl_context {
183229 context. make_current ( ) ;
184230 }
@@ -215,14 +261,14 @@ impl Context {
215261 fn draw_image_with_rect ( & mut self , image : & Image , dst_rect : impl Into < Rect > ) {
216262 #[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
217263 let _ = unsafe { objc2_foundation:: NSAutoreleasePool :: new ( ) } ;
218- #[ cfg( feature = "gl" ) ] {
264+ #[ cfg( feature = "gl" ) ]
265+ {
219266 if let Some ( ref context) = self . gl_context {
220267 context. make_current ( ) ;
221268 }
222269 }
223270
224271 let dimensions = image. dimensions ( ) ;
225-
226272 let src_rect = Rect :: from_xywh ( 0. , 0. , dimensions. width as f32 , dimensions. height as f32 ) ;
227273 let dst_rect = dst_rect. into ( ) ;
228274
0 commit comments