@@ -26,6 +26,15 @@ use webrender_traits::{GlyphDimensions, PipelineId, WebGLContextId};
26
26
27
27
thread_local ! ( pub static FONT_CONTEXT : RefCell <FontContext > = RefCell :: new( FontContext :: new( ) ) ) ;
28
28
29
+ // These coordinates are always in texels.
30
+ // They are converted to normalized ST
31
+ // values in the vertex shader. The reason
32
+ // for this is that the texture may change
33
+ // dimensions (e.g. the pages in a texture
34
+ // atlas can grow). When this happens, by
35
+ // storing the coordinates as texel values
36
+ // we don't need to go through and update
37
+ // various CPU-side structures.
29
38
pub struct CacheItem {
30
39
pub texture_id : TextureId ,
31
40
pub uv0 : Point2D < f32 > ,
@@ -129,11 +138,6 @@ impl<K,V> ResourceClassCache<K,V> where K: Clone + Hash + Eq + Debug, V: Resourc
129
138
}
130
139
}
131
140
132
- struct ImageResourceRequest {
133
- key : ImageKey ,
134
- rendering : ImageRendering ,
135
- }
136
-
137
141
struct TextRunResourceRequest {
138
142
key : FontKey ,
139
143
size : Au ,
@@ -142,16 +146,21 @@ struct TextRunResourceRequest {
142
146
}
143
147
144
148
enum ResourceRequest {
145
- Image ( ImageResourceRequest ) ,
149
+ Image ( ImageKey , ImageRendering ) ,
146
150
TextRun ( TextRunResourceRequest ) ,
147
151
}
148
152
153
+ struct WebGLTexture {
154
+ id : TextureId ,
155
+ size : Size2D < i32 > ,
156
+ }
157
+
149
158
pub struct ResourceCache {
150
159
cached_glyphs : ResourceClassCache < GlyphKey , Option < TextureCacheItemId > > ,
151
160
cached_images : ResourceClassCache < ( ImageKey , ImageRendering ) , CachedImageInfo > ,
152
161
153
162
// TODO(pcwalton): Figure out the lifecycle of these.
154
- webgl_textures : HashMap < WebGLContextId , ( TextureId , Size2D < i32 > ) , BuildHasherDefault < FnvHasher > > ,
163
+ webgl_textures : HashMap < WebGLContextId , WebGLTexture , BuildHasherDefault < FnvHasher > > ,
155
164
156
165
draw_lists : FreeList < DrawList > ,
157
166
font_templates : HashMap < FontKey , FontTemplate , BuildHasherDefault < FnvHasher > > ,
@@ -246,18 +255,18 @@ impl ResourceCache {
246
255
}
247
256
248
257
pub fn add_webgl_texture ( & mut self , id : WebGLContextId , texture_id : TextureId , size : Size2D < i32 > ) {
249
- self . webgl_textures . insert ( id, ( texture_id, size) ) ;
258
+ self . webgl_textures . insert ( id, WebGLTexture {
259
+ id : texture_id,
260
+ size : size,
261
+ } ) ;
250
262
self . texture_cache . add_raw_update ( texture_id, size) ;
251
263
}
252
264
253
265
pub fn request_image ( & mut self ,
254
266
key : ImageKey ,
255
267
rendering : ImageRendering ) {
256
268
debug_assert ! ( self . state == State :: AddResources ) ;
257
- self . pending_requests . push ( ResourceRequest :: Image ( ImageResourceRequest {
258
- key : key,
259
- rendering : rendering,
260
- } ) ) ;
269
+ self . pending_requests . push ( ResourceRequest :: Image ( key, rendering) ) ;
261
270
}
262
271
263
272
pub fn request_glyphs ( & mut self ,
@@ -430,11 +439,11 @@ impl ResourceCache {
430
439
431
440
#[ inline]
432
441
pub fn get_webgl_texture ( & self , context_id : & WebGLContextId ) -> CacheItem {
433
- let ( id , size ) = * self . webgl_textures . get ( context_id) . unwrap ( ) ;
442
+ let webgl_texture = & self . webgl_textures [ context_id] ;
434
443
CacheItem {
435
- texture_id : id,
436
- uv0 : Point2D :: new ( 0.0 , size. height as f32 ) ,
437
- uv1 : Point2D :: new ( size. width as f32 , 0.0 ) ,
444
+ texture_id : webgl_texture . id ,
445
+ uv0 : Point2D :: new ( 0.0 , webgl_texture . size . height as f32 ) ,
446
+ uv1 : Point2D :: new ( webgl_texture . size . width as f32 , 0.0 ) ,
438
447
}
439
448
}
440
449
@@ -455,12 +464,11 @@ impl ResourceCache {
455
464
456
465
for request in self . pending_requests . drain ( ..) {
457
466
match request {
458
- ResourceRequest :: Image ( ref image_request ) => {
467
+ ResourceRequest :: Image ( key , rendering ) => {
459
468
let cached_images = & mut self . cached_images ;
460
- let image_template = & self . image_templates [ & image_request. key ] ;
461
- let key = ( image_request. key , image_request. rendering ) ;
469
+ let image_template = & self . image_templates [ & key] ;
462
470
463
- match cached_images. entry ( key, self . current_frame_id ) {
471
+ match cached_images. entry ( ( key, rendering ) , self . current_frame_id ) {
464
472
Occupied ( entry) => {
465
473
let image_id = entry. get ( ) . texture_cache_id ;
466
474
@@ -482,7 +490,7 @@ impl ResourceCache {
482
490
Vacant ( entry) => {
483
491
let image_id = self . texture_cache . new_item_id ( ) ;
484
492
485
- let filter = match image_request . rendering {
493
+ let filter = match rendering {
486
494
ImageRendering :: Pixelated => TextureFilter :: Nearest ,
487
495
ImageRendering :: Auto | ImageRendering :: CrispEdges => TextureFilter :: Linear ,
488
496
} ;
0 commit comments