/src/mozilla-central/gfx/thebes/gfxPrefs.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | | * This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "gfxPrefs.h" |
7 | | |
8 | | #include "MainThreadUtils.h" |
9 | | #include "nsXULAppAPI.h" |
10 | | #include "mozilla/Preferences.h" |
11 | | #include "mozilla/Unused.h" |
12 | | #include "mozilla/gfx/gfxVars.h" |
13 | | #include "mozilla/gfx/Logging.h" |
14 | | #include "mozilla/gfx/GPUChild.h" |
15 | | #include "mozilla/gfx/GPUProcessManager.h" |
16 | | #include "VRProcessManager.h" |
17 | | #include "VRChild.h" |
18 | | |
19 | | using namespace mozilla; |
20 | | |
21 | | nsTArray<gfxPrefs::Pref*>* gfxPrefs::sGfxPrefList = nullptr; |
22 | | gfxPrefs* gfxPrefs::sInstance = nullptr; |
23 | | bool gfxPrefs::sInstanceHasBeenDestroyed = false; |
24 | | |
25 | | gfxPrefs& |
26 | 0 | gfxPrefs::CreateAndInitializeSingleton() { |
27 | 0 | MOZ_ASSERT(!sInstanceHasBeenDestroyed, |
28 | 0 | "Should never recreate a gfxPrefs instance!"); |
29 | 0 | sGfxPrefList = new nsTArray<Pref*>(); |
30 | 0 | sInstance = new gfxPrefs; |
31 | 0 | sInstance->Init(); |
32 | 0 | MOZ_ASSERT(SingletonExists()); |
33 | 0 | return *sInstance; |
34 | 0 | } |
35 | | |
36 | | void |
37 | | gfxPrefs::DestroySingleton() |
38 | 0 | { |
39 | 0 | if (sInstance) { |
40 | 0 | delete sInstance; |
41 | 0 | sInstance = nullptr; |
42 | 0 | sInstanceHasBeenDestroyed = true; |
43 | 0 | } |
44 | 0 | MOZ_ASSERT(!SingletonExists()); |
45 | 0 | } |
46 | | |
47 | | bool |
48 | | gfxPrefs::SingletonExists() |
49 | 0 | { |
50 | 0 | return sInstance != nullptr; |
51 | 0 | } |
52 | | |
53 | | gfxPrefs::gfxPrefs() |
54 | 0 | { |
55 | 0 | // UI, content, and plugin processes use XPCOM and should have prefs |
56 | 0 | // ready by the time we initialize gfxPrefs. |
57 | 0 | MOZ_ASSERT_IF(XRE_IsContentProcess() || |
58 | 0 | XRE_IsParentProcess() || |
59 | 0 | XRE_GetProcessType() == GeckoProcessType_Plugin, |
60 | 0 | Preferences::IsServiceAvailable()); |
61 | 0 |
|
62 | 0 | gfxPrefs::AssertMainThread(); |
63 | 0 | } |
64 | | |
65 | | void |
66 | | gfxPrefs::Init() |
67 | 0 | { |
68 | 0 | // Set up Moz2D prefs. |
69 | 0 | SetGfxLoggingLevelChangeCallback([](const GfxPrefValue& aValue) -> void { |
70 | 0 | mozilla::gfx::LoggingPrefs::sGfxLogLevel = aValue.get_int32_t(); |
71 | 0 | }); |
72 | 0 | } |
73 | | |
74 | | gfxPrefs::~gfxPrefs() |
75 | 0 | { |
76 | 0 | gfxPrefs::AssertMainThread(); |
77 | 0 | SetGfxLoggingLevelChangeCallback(nullptr); |
78 | 0 | delete sGfxPrefList; |
79 | 0 | sGfxPrefList = nullptr; |
80 | 0 | } |
81 | | |
82 | | void gfxPrefs::AssertMainThread() |
83 | 0 | { |
84 | 0 | MOZ_ASSERT(NS_IsMainThread(), "this code must be run on the main thread"); |
85 | 0 | } |
86 | | |
87 | | void |
88 | | gfxPrefs::Pref::OnChange() |
89 | 0 | { |
90 | 0 | if (auto gpm = gfx::GPUProcessManager::Get()) { |
91 | 0 | if (gfx::GPUChild* gpu = gpm->GetGPUChild()) { |
92 | 0 | GfxPrefValue value; |
93 | 0 | GetLiveValue(&value); |
94 | 0 | Unused << gpu->SendUpdatePref(gfx::GfxPrefSetting(mIndex, value)); |
95 | 0 | } |
96 | 0 | } |
97 | 0 | if (auto vpm = gfx::VRProcessManager::Get()) { |
98 | 0 | if (gfx::VRChild* vr = vpm->GetVRChild()) { |
99 | 0 | GfxPrefValue value; |
100 | 0 | GetLiveValue(&value); |
101 | 0 | Unused << vr->SendUpdatePref(gfx::GfxPrefSetting(mIndex, value)); |
102 | 0 | } |
103 | 0 | } |
104 | 0 | FireChangeCallback(); |
105 | 0 | } |
106 | | |
107 | | void |
108 | | gfxPrefs::Pref::FireChangeCallback() |
109 | 0 | { |
110 | 0 | if (mChangeCallback) { |
111 | 0 | GfxPrefValue value; |
112 | 0 | GetLiveValue(&value); |
113 | 0 | mChangeCallback(value); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | void |
118 | | gfxPrefs::Pref::SetChangeCallback(ChangeCallback aCallback) |
119 | 0 | { |
120 | 0 | mChangeCallback = aCallback; |
121 | 0 |
|
122 | 0 | if (!IsParentProcess() && IsPrefsServiceAvailable()) { |
123 | 0 | // If we're in the parent process, we watch prefs by default so we can |
124 | 0 | // send changes over to the GPU process. Otherwise, we need to add or |
125 | 0 | // remove a watch for the pref now. |
126 | 0 | if (aCallback) { |
127 | 0 | WatchChanges(Name(), this); |
128 | 0 | } else { |
129 | 0 | UnwatchChanges(Name(), this); |
130 | 0 | } |
131 | 0 | } |
132 | 0 |
|
133 | 0 | // Fire the callback once to make initialization easier for the caller. |
134 | 0 | FireChangeCallback(); |
135 | 0 | } |
136 | | |
137 | | // On lightweight processes such as for GMP and GPU, XPCOM is not initialized, |
138 | | // and therefore we don't have access to Preferences. When XPCOM is not |
139 | | // available we rely on manual synchronization of gfxPrefs values over IPC. |
140 | | /* static */ bool |
141 | | gfxPrefs::IsPrefsServiceAvailable() |
142 | 0 | { |
143 | 0 | return Preferences::IsServiceAvailable(); |
144 | 0 | } |
145 | | |
146 | | /* static */ bool |
147 | | gfxPrefs::IsParentProcess() |
148 | 0 | { |
149 | 0 | return XRE_IsParentProcess(); |
150 | 0 | } |
151 | | |
152 | | void gfxPrefs::PrefAddVarCache(bool* aVariable, |
153 | | const nsACString& aPref, |
154 | | bool aDefault) |
155 | 0 | { |
156 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
157 | 0 | Preferences::AddBoolVarCache(aVariable, aPref, aDefault); |
158 | 0 | } |
159 | | |
160 | | void gfxPrefs::PrefAddVarCache(int32_t* aVariable, |
161 | | const nsACString& aPref, |
162 | | int32_t aDefault) |
163 | 0 | { |
164 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
165 | 0 | Preferences::AddIntVarCache(aVariable, aPref, aDefault); |
166 | 0 | } |
167 | | |
168 | | void gfxPrefs::PrefAddVarCache(uint32_t* aVariable, |
169 | | const nsACString& aPref, |
170 | | uint32_t aDefault) |
171 | 0 | { |
172 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
173 | 0 | Preferences::AddUintVarCache(aVariable, aPref, aDefault); |
174 | 0 | } |
175 | | |
176 | | void gfxPrefs::PrefAddVarCache(float* aVariable, |
177 | | const nsACString& aPref, |
178 | | float aDefault) |
179 | 0 | { |
180 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
181 | 0 | Preferences::AddFloatVarCache(aVariable, aPref, aDefault); |
182 | 0 | } |
183 | | |
184 | | void gfxPrefs::PrefAddVarCache(std::string* aVariable, |
185 | | const nsCString& aPref, |
186 | | std::string aDefault) |
187 | 0 | { |
188 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
189 | 0 | Preferences::SetCString(aPref.get(), aVariable->c_str()); |
190 | 0 | } |
191 | | |
192 | | void gfxPrefs::PrefAddVarCache(AtomicBool* aVariable, |
193 | | const nsACString& aPref, |
194 | | bool aDefault) |
195 | 0 | { |
196 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
197 | 0 | Preferences::AddAtomicBoolVarCache(aVariable, aPref, aDefault); |
198 | 0 | } |
199 | | |
200 | | void gfxPrefs::PrefAddVarCache(AtomicInt32* aVariable, |
201 | | const nsACString& aPref, |
202 | | int32_t aDefault) |
203 | 0 | { |
204 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
205 | 0 | Preferences::AddAtomicIntVarCache(aVariable, aPref, aDefault); |
206 | 0 | } |
207 | | |
208 | | void gfxPrefs::PrefAddVarCache(AtomicUint32* aVariable, |
209 | | const nsACString& aPref, |
210 | | uint32_t aDefault) |
211 | 0 | { |
212 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
213 | 0 | Preferences::AddAtomicUintVarCache(aVariable, aPref, aDefault); |
214 | 0 | } |
215 | | |
216 | | bool gfxPrefs::PrefGet(const char* aPref, bool aDefault) |
217 | 0 | { |
218 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
219 | 0 | return Preferences::GetBool(aPref, aDefault); |
220 | 0 | } |
221 | | |
222 | | int32_t gfxPrefs::PrefGet(const char* aPref, int32_t aDefault) |
223 | 0 | { |
224 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
225 | 0 | return Preferences::GetInt(aPref, aDefault); |
226 | 0 | } |
227 | | |
228 | | uint32_t gfxPrefs::PrefGet(const char* aPref, uint32_t aDefault) |
229 | 0 | { |
230 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
231 | 0 | return Preferences::GetUint(aPref, aDefault); |
232 | 0 | } |
233 | | |
234 | | float gfxPrefs::PrefGet(const char* aPref, float aDefault) |
235 | 0 | { |
236 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
237 | 0 | return Preferences::GetFloat(aPref, aDefault); |
238 | 0 | } |
239 | | |
240 | | std::string gfxPrefs::PrefGet(const char* aPref, std::string aDefault) |
241 | 0 | { |
242 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
243 | 0 |
|
244 | 0 | nsAutoCString result; |
245 | 0 | Preferences::GetCString(aPref, result); |
246 | 0 |
|
247 | 0 | if (result.IsEmpty()) { |
248 | 0 | return aDefault; |
249 | 0 | } |
250 | 0 | |
251 | 0 | return result.get(); |
252 | 0 | } |
253 | | |
254 | | void gfxPrefs::PrefSet(const char* aPref, bool aValue) |
255 | 0 | { |
256 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
257 | 0 | Preferences::SetBool(aPref, aValue); |
258 | 0 | } |
259 | | |
260 | | void gfxPrefs::PrefSet(const char* aPref, int32_t aValue) |
261 | 0 | { |
262 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
263 | 0 | Preferences::SetInt(aPref, aValue); |
264 | 0 | } |
265 | | |
266 | | void gfxPrefs::PrefSet(const char* aPref, uint32_t aValue) |
267 | 0 | { |
268 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
269 | 0 | Preferences::SetUint(aPref, aValue); |
270 | 0 | } |
271 | | |
272 | | void gfxPrefs::PrefSet(const char* aPref, float aValue) |
273 | 0 | { |
274 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
275 | 0 | Preferences::SetFloat(aPref, aValue); |
276 | 0 | } |
277 | | |
278 | | void gfxPrefs::PrefSet(const char* aPref, std::string aValue) |
279 | 0 | { |
280 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
281 | 0 | Preferences::SetCString(aPref, aValue.c_str()); |
282 | 0 | } |
283 | | |
284 | | static void |
285 | | OnGfxPrefChanged(const char* aPrefname, gfxPrefs::Pref* aPref) |
286 | 0 | { |
287 | 0 | aPref->OnChange(); |
288 | 0 | } |
289 | | |
290 | | void gfxPrefs::WatchChanges(const char* aPrefname, Pref* aPref) |
291 | 0 | { |
292 | 0 | MOZ_ASSERT(IsPrefsServiceAvailable()); |
293 | 0 | nsCString name; |
294 | 0 | name.AssignLiteral(aPrefname, strlen(aPrefname)); |
295 | 0 | Preferences::RegisterCallback(OnGfxPrefChanged, name, aPref); |
296 | 0 | } |
297 | | |
298 | | void gfxPrefs::UnwatchChanges(const char* aPrefname, Pref* aPref) |
299 | 0 | { |
300 | 0 | // The Preferences service can go offline before gfxPrefs is destroyed. |
301 | 0 | if (IsPrefsServiceAvailable()) { |
302 | 0 | Preferences::UnregisterCallback(OnGfxPrefChanged, nsDependentCString(aPrefname), |
303 | 0 | aPref); |
304 | 0 | } |
305 | 0 | } |
306 | | |
307 | | void gfxPrefs::CopyPrefValue(const bool* aValue, GfxPrefValue* aOutValue) |
308 | 0 | { |
309 | 0 | *aOutValue = *aValue; |
310 | 0 | } |
311 | | |
312 | | void gfxPrefs::CopyPrefValue(const int32_t* aValue, GfxPrefValue* aOutValue) |
313 | 0 | { |
314 | 0 | *aOutValue = *aValue; |
315 | 0 | } |
316 | | |
317 | | void gfxPrefs::CopyPrefValue(const uint32_t* aValue, GfxPrefValue* aOutValue) |
318 | 0 | { |
319 | 0 | *aOutValue = *aValue; |
320 | 0 | } |
321 | | |
322 | | void gfxPrefs::CopyPrefValue(const float* aValue, GfxPrefValue* aOutValue) |
323 | 0 | { |
324 | 0 | *aOutValue = *aValue; |
325 | 0 | } |
326 | | |
327 | | void gfxPrefs::CopyPrefValue(const std::string* aValue, GfxPrefValue* aOutValue) |
328 | 0 | { |
329 | 0 | *aOutValue = nsCString(aValue->c_str()); |
330 | 0 | } |
331 | | |
332 | | void gfxPrefs::CopyPrefValue(const GfxPrefValue* aValue, bool* aOutValue) |
333 | 0 | { |
334 | 0 | *aOutValue = aValue->get_bool(); |
335 | 0 | } |
336 | | |
337 | | void gfxPrefs::CopyPrefValue(const GfxPrefValue* aValue, int32_t* aOutValue) |
338 | 0 | { |
339 | 0 | *aOutValue = aValue->get_int32_t(); |
340 | 0 | } |
341 | | |
342 | | void gfxPrefs::CopyPrefValue(const GfxPrefValue* aValue, uint32_t* aOutValue) |
343 | 0 | { |
344 | 0 | *aOutValue = aValue->get_uint32_t(); |
345 | 0 | } |
346 | | |
347 | | void gfxPrefs::CopyPrefValue(const GfxPrefValue* aValue, float* aOutValue) |
348 | 0 | { |
349 | 0 | *aOutValue = aValue->get_float(); |
350 | 0 | } |
351 | | |
352 | | void gfxPrefs::CopyPrefValue(const GfxPrefValue* aValue, std::string* aOutValue) |
353 | 0 | { |
354 | 0 | *aOutValue = aValue->get_nsCString().get(); |
355 | 0 | } |
356 | | |
357 | | bool gfxPrefs::OverrideBase_WebRender() |
358 | 0 | { |
359 | 0 | return gfx::gfxVars::UseWebRender(); |
360 | 0 | } |