summaryrefslogtreecommitdiffstats
path: root/gfx/config/gfxConfig.cpp
blob: 39ecd30da59f1bbc385c30c6d56a146dc4650a80 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "gfxConfig.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Unused.h"
#include "mozilla/gfx/GPUParent.h"
#include "mozilla/gfx/GraphicsMessages.h"
#include "plstr.h"

namespace mozilla {
namespace gfx {

static StaticAutoPtr<gfxConfig> sConfig;

/* static */ FeatureState& gfxConfig::GetFeature(Feature aFeature) {
  return sConfig->GetState(aFeature);
}

/* static */
bool gfxConfig::IsEnabled(Feature aFeature) {
  const FeatureState& state = sConfig->GetState(aFeature);
  return state.IsEnabled();
}

/* static */
bool gfxConfig::IsDisabledByDefault(Feature aFeature) {
  const FeatureState& state = sConfig->GetState(aFeature);
  return state.DisabledByDefault();
}

/* static */
bool gfxConfig::IsForcedOnByUser(Feature aFeature) {
  const FeatureState& state = sConfig->GetState(aFeature);
  return state.IsForcedOnByUser();
}

/* static */
FeatureStatus gfxConfig::GetValue(Feature aFeature) {
  const FeatureState& state = sConfig->GetState(aFeature);
  return state.GetValue();
}

/* static */
bool gfxConfig::SetDefault(Feature aFeature, bool aEnable,
                           FeatureStatus aDisableStatus,
                           const char* aDisableMessage) {
  FeatureState& state = sConfig->GetState(aFeature);
  return state.SetDefault(aEnable, aDisableStatus, aDisableMessage);
}

/* static */
void gfxConfig::DisableByDefault(Feature aFeature, FeatureStatus aDisableStatus,
                                 const char* aDisableMessage,
                                 const nsACString& aFailureId) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.DisableByDefault(aDisableStatus, aDisableMessage, aFailureId);
}

/* static */
void gfxConfig::EnableByDefault(Feature aFeature) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.EnableByDefault();
}

/* static */
void gfxConfig::SetDefaultFromPref(Feature aFeature, const char* aPrefName,
                                   bool aIsEnablePref, bool aDefaultValue) {
  FeatureState& state = sConfig->GetState(aFeature);
  return state.SetDefaultFromPref(aPrefName, aIsEnablePref, aDefaultValue);
}

/* static */
bool gfxConfig::InitOrUpdate(Feature aFeature, bool aEnable,
                             FeatureStatus aDisableStatus,
                             const char* aDisableMessage) {
  FeatureState& state = sConfig->GetState(aFeature);
  return state.InitOrUpdate(aEnable, aDisableStatus, aDisableMessage);
}

/* static */
void gfxConfig::SetFailed(Feature aFeature, FeatureStatus aStatus,
                          const char* aMessage, const nsACString& aFailureId) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.SetFailed(aStatus, aMessage, aFailureId);
}

/* static */
void gfxConfig::Disable(Feature aFeature, FeatureStatus aStatus,
                        const char* aMessage, const nsACString& aFailureId) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.Disable(aStatus, aMessage, aFailureId);
}

/* static */
void gfxConfig::UserEnable(Feature aFeature, const char* aMessage) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.UserEnable(aMessage);
}

/* static */
void gfxConfig::UserForceEnable(Feature aFeature, const char* aMessage) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.UserForceEnable(aMessage);
}

/* static */
void gfxConfig::UserDisable(Feature aFeature, const char* aMessage,
                            const nsACString& aFailureId) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.UserDisable(aMessage, aFailureId);
}

/* static */
void gfxConfig::Reenable(Feature aFeature, Fallback aFallback) {
  FeatureState& state = sConfig->GetState(aFeature);
  MOZ_ASSERT(IsFeatureStatusFailure(state.GetValue()));

  const char* message = state.GetRuntimeMessage();
  EnableFallback(aFallback, message);
  state.SetRuntime(FeatureStatus::Available, nullptr, nsCString());
}

/* static */
void gfxConfig::Reset(Feature aFeature) {
  FeatureState& state = sConfig->GetState(aFeature);
  state.Reset();
}

/* static */
void gfxConfig::Inherit(Feature aFeature, FeatureStatus aStatus) {
  FeatureState& state = sConfig->GetState(aFeature);

  state.Reset();

  switch (aStatus) {
    case FeatureStatus::Unused:
      break;
    case FeatureStatus::Available:
      gfxConfig::EnableByDefault(aFeature);
      break;
    case FeatureStatus::ForceEnabled:
      gfxConfig::EnableByDefault(aFeature);
      gfxConfig::UserForceEnable(aFeature, "Inherited from parent process");
      break;
    default:
      gfxConfig::SetDefault(aFeature, false, aStatus,
                            "Disabled in parent process");
      break;
  }
}

/* static */
void gfxConfig::Inherit(EnumSet<Feature> aFeatures,
                        const DevicePrefs& aDevicePrefs) {
  for (Feature feature : aFeatures) {
    FeatureStatus status = FeatureStatus::Unused;
    switch (feature) {
      case Feature::HW_COMPOSITING:
        status = aDevicePrefs.hwCompositing();
        break;
      case Feature::D3D11_COMPOSITING:
        status = aDevicePrefs.d3d11Compositing();
        break;
      case Feature::OPENGL_COMPOSITING:
        status = aDevicePrefs.oglCompositing();
        break;
      case Feature::DIRECT2D:
        status = aDevicePrefs.useD2D1();
        break;
      default:
        break;
    }
    gfxConfig::Inherit(feature, status);
  }
}

/* static */
bool gfxConfig::UseFallback(Fallback aFallback) {
  return sConfig->UseFallbackImpl(aFallback);
}

/* static */
void gfxConfig::EnableFallback(Fallback aFallback, const char* aMessage) {
  if (!NS_IsMainThread()) {
    nsCString message(aMessage);
    NS_DispatchToMainThread(
        NS_NewRunnableFunction("gfxConfig::EnableFallback", [=]() -> void {
          gfxConfig::EnableFallback(aFallback, message.get());
        }));
    return;
  }

  if (XRE_IsGPUProcess()) {
    nsCString message(aMessage);
    Unused << GPUParent::GetSingleton()->SendUsedFallback(aFallback, message);
    return;
  }

  sConfig->EnableFallbackImpl(aFallback, aMessage);
}

bool gfxConfig::UseFallbackImpl(Fallback aFallback) const {
  return !!(mFallbackBits & (uint64_t(1) << uint64_t(aFallback)));
}

void gfxConfig::EnableFallbackImpl(Fallback aFallback, const char* aMessage) {
  if (!UseFallbackImpl(aFallback)) {
    MOZ_ASSERT(mNumFallbackLogEntries < kNumFallbacks);

    FallbackLogEntry& entry = mFallbackLog[mNumFallbackLogEntries];
    mNumFallbackLogEntries++;

    entry.mFallback = aFallback;
    PL_strncpyz(entry.mMessage, aMessage, sizeof(entry.mMessage));
  }
  mFallbackBits |= (uint64_t(1) << uint64_t(aFallback));
}

struct FeatureInfo {
  const char* name;
  const char* description;
};
static const FeatureInfo sFeatureInfo[] = {
#define FOR_EACH_FEATURE(name, type, desc) {#name, desc},
    GFX_FEATURE_MAP(FOR_EACH_FEATURE)
#undef FOR_EACH_FEATURE
        {nullptr, nullptr}};

/* static */
void gfxConfig::ForEachFeature(const FeatureIterCallback& aCallback) {
  for (size_t i = 0; i < kNumFeatures; i++) {
    FeatureState& state = GetFeature(static_cast<Feature>(i));
    if (!state.IsInitialized()) {
      continue;
    }

    aCallback(sFeatureInfo[i].name, sFeatureInfo[i].description, state);
  }
}

static const char* sFallbackNames[] = {
#define FOR_EACH_FALLBACK(name) #name,
    GFX_FALLBACK_MAP(FOR_EACH_FALLBACK)
#undef FOR_EACH_FALLBACK
        nullptr};

/* static  */
void gfxConfig::ForEachFallback(const FallbackIterCallback& aCallback) {
  sConfig->ForEachFallbackImpl(aCallback);
}

void gfxConfig::ForEachFallbackImpl(const FallbackIterCallback& aCallback) {
  for (size_t i = 0; i < mNumFallbackLogEntries; i++) {
    const FallbackLogEntry& entry = mFallbackLog[i];
    aCallback(sFallbackNames[size_t(entry.mFallback)], entry.mMessage);
  }
}

/* static */ const nsCString& gfxConfig::GetFailureId(Feature aFeature) {
  const FeatureState& state = sConfig->GetState(aFeature);
  return state.GetFailureId();
}

/* static */
void gfxConfig::ImportChange(Feature aFeature,
                             const Maybe<FeatureFailure>& aChange) {
  if (aChange.isNothing()) {
    return;
  }

  const FeatureFailure& failure = aChange.ref();
  gfxConfig::SetFailed(aFeature, failure.status(), failure.message().get(),
                       failure.failureId());
}

/* static */
void gfxConfig::Init() { sConfig = new gfxConfig(); }

/* static */
void gfxConfig::Shutdown() { sConfig = nullptr; }

}  // namespace gfx
}  // namespace mozilla