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
|
/* 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 http://mozilla.org/MPL/2.0/. */
//! Data needed by the layout task.
use css::matching::{ApplicableDeclarationsCache, StyleSharingCandidateCache};
use geom::rect::Rect;
use geom::size::Size2D;
use gfx::display_list::OpaqueNode;
use gfx::font_context::FontContext;
use gfx::font_cache_task::FontCacheTask;
#[cfg(not(target_os="android"))]
use green::task::GreenTask;
use script::layout_interface::LayoutChan;
use servo_msg::constellation_msg::ConstellationChan;
use servo_net::local_image_cache::LocalImageCache;
use servo_util::geometry::Au;
use servo_util::opts::Opts;
use sync::{Arc, Mutex};
use std::mem;
#[cfg(not(target_os="android"))]
use std::ptr;
#[cfg(not(target_os="android"))]
use std::rt::local::Local;
#[cfg(not(target_os="android"))]
use std::rt::task::Task;
use style::Stylist;
use url::Url;
#[cfg(not(target_os="android"))]
#[thread_local]
static mut FONT_CONTEXT: *mut FontContext = 0 as *mut FontContext;
#[cfg(target_os="android")]
local_data_key!(font_context: *mut FontContext)
#[cfg(not(target_os="android"))]
#[thread_local]
static mut APPLICABLE_DECLARATIONS_CACHE: *mut ApplicableDeclarationsCache =
0 as *mut ApplicableDeclarationsCache;
#[cfg(target_os="android")]
local_data_key!(applicable_declarations_cache: *mut ApplicableDeclarationsCache)
#[cfg(not(target_os="android"))]
#[thread_local]
static mut STYLE_SHARING_CANDIDATE_CACHE: *mut StyleSharingCandidateCache =
0 as *mut StyleSharingCandidateCache;
#[cfg(target_os="android")]
local_data_key!(style_sharing_candidate_cache: *mut StyleSharingCandidateCache)
/// Data shared by all layout workers.
#[allow(raw_pointer_deriving)]
#[deriving(Clone)]
pub struct LayoutContext {
/// The local image cache.
pub image_cache: Arc<Mutex<LocalImageCache>>,
/// The current screen size.
pub screen_size: Size2D<Au>,
/// A channel up to the constellation.
pub constellation_chan: ConstellationChan,
/// A channel up to the layout task.
pub layout_chan: LayoutChan,
/// Interface to the font cache task.
pub font_cache_task: FontCacheTask,
/// The CSS selector stylist.
///
/// FIXME(#2604): Make this no longer an unsafe pointer once we have fast `RWArc`s.
pub stylist: *Stylist,
/// The root node at which we're starting the layout.
pub reflow_root: OpaqueNode,
/// The URL.
pub url: Url,
/// The command line options.
pub opts: Opts,
/// The dirty rectangle, used during display list building.
pub dirty: Rect<Au>,
}
#[cfg(not(target_os="android"))]
impl LayoutContext {
pub fn font_context<'a>(&'a mut self) -> &'a mut FontContext {
// Sanity check.
{
let mut task = Local::borrow(None::<Task>);
match task.maybe_take_runtime::<GreenTask>() {
Some(green) => {
task.put_runtime(green);
fail!("can't call this on a green task!")
}
None => {}
}
}
unsafe {
if FONT_CONTEXT == ptr::mut_null() {
let context = box FontContext::new(self.font_cache_task.clone());
FONT_CONTEXT = mem::transmute(context)
}
mem::transmute(FONT_CONTEXT)
}
}
pub fn applicable_declarations_cache<'a>(&'a self) -> &'a mut ApplicableDeclarationsCache {
// Sanity check.
{
let mut task = Local::borrow(None::<Task>);
match task.maybe_take_runtime::<GreenTask>() {
Some(green) => {
task.put_runtime(green);
fail!("can't call this on a green task!")
}
None => {}
}
}
unsafe {
if APPLICABLE_DECLARATIONS_CACHE == ptr::mut_null() {
let cache = box ApplicableDeclarationsCache::new();
APPLICABLE_DECLARATIONS_CACHE = mem::transmute(cache)
}
mem::transmute(APPLICABLE_DECLARATIONS_CACHE)
}
}
pub fn style_sharing_candidate_cache<'a>(&'a self) -> &'a mut StyleSharingCandidateCache {
// Sanity check.
{
let mut task = Local::borrow(None::<Task>);
match task.maybe_take_runtime::<GreenTask>() {
Some(green) => {
task.put_runtime(green);
fail!("can't call this on a green task!")
}
None => {}
}
}
unsafe {
if STYLE_SHARING_CANDIDATE_CACHE == ptr::mut_null() {
let cache = box StyleSharingCandidateCache::new();
STYLE_SHARING_CANDIDATE_CACHE = mem::transmute(cache)
}
mem::transmute(STYLE_SHARING_CANDIDATE_CACHE)
}
}
}
// On Android, we don't have the __tls_* functions emitted by rustc, so we
// need to use the slower local_data functions.
// Making matters worse, the local_data functions are very particular about
// enforcing the lifetimes associated with objects that they hold onto,
// which causes us some trouble we work around as below.
#[cfg(target_os="android")]
impl LayoutContext {
pub fn font_context<'a>(&'a mut self) -> &'a mut FontContext {
unsafe {
let opt = font_context.replace(None);
let mut context;
match opt {
Some(c) => context = mem::transmute(c),
None => {
context = mem::transmute(box FontContext::new(self.font_cache_task.clone()))
}
}
font_context.replace(Some(context));
mem::transmute(context)
}
}
pub fn applicable_declarations_cache<'a>(&'a self) -> &'a mut ApplicableDeclarationsCache {
unsafe {
let opt = applicable_declarations_cache.replace(None);
let mut cache;
match opt {
Some(c) => cache = mem::transmute(c),
None => {
cache = mem::transmute(box ApplicableDeclarationsCache::new());
}
}
applicable_declarations_cache.replace(Some(cache));
mem::transmute(cache)
}
}
pub fn style_sharing_candidate_cache<'a>(&'a self) -> &'a mut StyleSharingCandidateCache {
unsafe {
let opt = style_sharing_candidate_cache.replace(None);
let mut cache;
match opt {
Some(c) => cache = mem::transmute(c),
None => {
cache = mem::transmute(box StyleSharingCandidateCache::new());
}
}
style_sharing_candidate_cache.replace(Some(cache));
mem::transmute(cache)
}
}
}
|