Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / display / chromeos / x11 / native_display_delegate_x11.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/display/chromeos/x11/native_display_delegate_x11.h"
6
7 #include <X11/Xatom.h>
8 #include <X11/Xlib.h>
9 #include <X11/extensions/dpms.h>
10 #include <X11/extensions/Xrandr.h>
11 #include <X11/extensions/XInput2.h>
12
13 #include <utility>
14
15 #include "base/logging.h"
16 #include "base/stl_util.h"
17 #include "ui/display/chromeos/x11/display_mode_x11.h"
18 #include "ui/display/chromeos/x11/display_snapshot_x11.h"
19 #include "ui/display/chromeos/x11/display_util_x11.h"
20 #include "ui/display/chromeos/x11/native_display_event_dispatcher_x11.h"
21 #include "ui/display/types/native_display_observer.h"
22 #include "ui/display/util/x11/edid_parser_x11.h"
23 #include "ui/events/platform/platform_event_source.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/x/x11_error_tracker.h"
26 #include "ui/gfx/x/x11_types.h"
27
28 namespace ui {
29
30 namespace {
31
32 // DPI measurements.
33 const float kMmInInch = 25.4;
34 const float kDpi96 = 96.0;
35 const float kPixelsToMmScale = kMmInInch / kDpi96;
36
37 const char kContentProtectionAtomName[] = "Content Protection";
38 const char kProtectionUndesiredAtomName[] = "Undesired";
39 const char kProtectionDesiredAtomName[] = "Desired";
40 const char kProtectionEnabledAtomName[] = "Enabled";
41
42 RRMode GetOutputNativeMode(const XRROutputInfo* output_info) {
43   return output_info->nmode > 0 ? output_info->modes[0] : None;
44 }
45
46 XRRCrtcGamma* ResampleGammaRamp(XRRCrtcGamma* gamma_ramp, int gamma_ramp_size) {
47   if (gamma_ramp->size == gamma_ramp_size)
48     return gamma_ramp;
49
50 #define RESAMPLE(array, i, r) \
51   array[i] + (array[i + 1] - array[i]) * r / gamma_ramp_size
52
53   XRRCrtcGamma* resampled = XRRAllocGamma(gamma_ramp_size);
54   for (int i = 0; i < gamma_ramp_size; ++i) {
55     int base_index = gamma_ramp->size * i / gamma_ramp_size;
56     int remaining = gamma_ramp->size * i % gamma_ramp_size;
57     if (base_index < gamma_ramp->size - 1) {
58       resampled->red[i] = RESAMPLE(gamma_ramp->red, base_index, remaining);
59       resampled->green[i] = RESAMPLE(gamma_ramp->green, base_index, remaining);
60       resampled->blue[i] = RESAMPLE(gamma_ramp->blue, base_index, remaining);
61     } else {
62       resampled->red[i] = gamma_ramp->red[gamma_ramp->size - 1];
63       resampled->green[i] = gamma_ramp->green[gamma_ramp->size - 1];
64       resampled->blue[i] = gamma_ramp->blue[gamma_ramp->size - 1];
65     }
66   }
67
68 #undef RESAMPLE
69   XRRFreeGamma(gamma_ramp);
70   return resampled;
71 }
72
73 }  // namespace
74
75 ////////////////////////////////////////////////////////////////////////////////
76 // NativeDisplayDelegateX11::HelperDelegateX11
77
78 class NativeDisplayDelegateX11::HelperDelegateX11
79     : public NativeDisplayDelegateX11::HelperDelegate {
80  public:
81   HelperDelegateX11(NativeDisplayDelegateX11* delegate) : delegate_(delegate) {}
82   virtual ~HelperDelegateX11() {}
83
84   // NativeDisplayDelegateX11::HelperDelegate overrides:
85   virtual void UpdateXRandRConfiguration(const base::NativeEvent& event)
86       override {
87     XRRUpdateConfiguration(event);
88   }
89   virtual const std::vector<DisplaySnapshot*>& GetCachedDisplays() const
90       override {
91     return delegate_->cached_outputs_.get();
92   }
93   virtual void NotifyDisplayObservers() override {
94     FOR_EACH_OBSERVER(
95         NativeDisplayObserver, delegate_->observers_, OnConfigurationChanged());
96   }
97
98  private:
99   NativeDisplayDelegateX11* delegate_;
100
101   DISALLOW_COPY_AND_ASSIGN(HelperDelegateX11);
102 };
103
104 ////////////////////////////////////////////////////////////////////////////////
105 // NativeDisplayDelegateX11 implementation:
106
107 NativeDisplayDelegateX11::NativeDisplayDelegateX11()
108     : display_(gfx::GetXDisplay()),
109       window_(DefaultRootWindow(display_)),
110       screen_(NULL),
111       background_color_argb_(0) {}
112
113 NativeDisplayDelegateX11::~NativeDisplayDelegateX11() {
114   if (ui::PlatformEventSource::GetInstance()) {
115     ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(
116         platform_event_dispatcher_.get());
117   }
118
119   STLDeleteContainerPairSecondPointers(modes_.begin(), modes_.end());
120 }
121
122 void NativeDisplayDelegateX11::Initialize() {
123   int error_base_ignored = 0;
124   int xrandr_event_base = 0;
125   XRRQueryExtension(display_, &xrandr_event_base, &error_base_ignored);
126
127   helper_delegate_.reset(new HelperDelegateX11(this));
128   platform_event_dispatcher_.reset(new NativeDisplayEventDispatcherX11(
129       helper_delegate_.get(), xrandr_event_base));
130
131   if (ui::PlatformEventSource::GetInstance()) {
132     ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(
133         platform_event_dispatcher_.get());
134   }
135 }
136
137 void NativeDisplayDelegateX11::GrabServer() {
138   CHECK(!screen_) << "Server already grabbed";
139   XGrabServer(display_);
140   screen_ = XRRGetScreenResources(display_, window_);
141   CHECK(screen_);
142 }
143
144 void NativeDisplayDelegateX11::UngrabServer() {
145   CHECK(screen_) << "Server not grabbed";
146   XRRFreeScreenResources(screen_);
147   screen_ = NULL;
148   XUngrabServer(display_);
149   // crbug.com/366125
150   XFlush(display_);
151 }
152
153 bool NativeDisplayDelegateX11::TakeDisplayControl() {
154   NOTIMPLEMENTED();
155   return false;
156 }
157
158 bool NativeDisplayDelegateX11::RelinquishDisplayControl() {
159   NOTIMPLEMENTED();
160   return false;
161 }
162
163 void NativeDisplayDelegateX11::SyncWithServer() { XSync(display_, 0); }
164
165 void NativeDisplayDelegateX11::SetBackgroundColor(uint32_t color_argb) {
166   background_color_argb_ = color_argb;
167 }
168
169 void NativeDisplayDelegateX11::ForceDPMSOn() {
170   CHECK(DPMSEnable(display_));
171   CHECK(DPMSForceLevel(display_, DPMSModeOn));
172 }
173
174 std::vector<DisplaySnapshot*> NativeDisplayDelegateX11::GetDisplays() {
175   CHECK(screen_) << "Server not grabbed";
176
177   cached_outputs_.clear();
178   RRCrtc last_used_crtc = None;
179
180   InitModes();
181   for (int i = 0; i < screen_->noutput && cached_outputs_.size() < 2; ++i) {
182     RROutput output_id = screen_->outputs[i];
183     XRROutputInfo* output_info = XRRGetOutputInfo(display_, screen_, output_id);
184     if (output_info->connection == RR_Connected) {
185       DisplaySnapshotX11* output =
186           InitDisplaySnapshot(output_id, output_info, &last_used_crtc, i);
187       cached_outputs_.push_back(output);
188     }
189     XRRFreeOutputInfo(output_info);
190   }
191
192   return cached_outputs_.get();
193 }
194
195 void NativeDisplayDelegateX11::AddMode(const DisplaySnapshot& output,
196                                        const DisplayMode* mode) {
197   CHECK(screen_) << "Server not grabbed";
198   CHECK(mode) << "Must add valid mode";
199
200   const DisplaySnapshotX11& x11_output =
201       static_cast<const DisplaySnapshotX11&>(output);
202   RRMode mode_id = static_cast<const DisplayModeX11*>(mode)->mode_id();
203
204   VLOG(1) << "AddDisplayMode: output=" << x11_output.output()
205           << " mode=" << mode_id;
206   XRRAddOutputMode(display_, x11_output.output(), mode_id);
207 }
208
209 bool NativeDisplayDelegateX11::Configure(const DisplaySnapshot& output,
210                                          const DisplayMode* mode,
211                                          const gfx::Point& origin) {
212   const DisplaySnapshotX11& x11_output =
213       static_cast<const DisplaySnapshotX11&>(output);
214   RRMode mode_id = None;
215   if (mode)
216     mode_id = static_cast<const DisplayModeX11*>(mode)->mode_id();
217
218   return ConfigureCrtc(
219       x11_output.crtc(), mode_id, x11_output.output(), origin.x(), origin.y());
220 }
221
222 bool NativeDisplayDelegateX11::ConfigureCrtc(RRCrtc crtc,
223                                              RRMode mode,
224                                              RROutput output,
225                                              int x,
226                                              int y) {
227   CHECK(screen_) << "Server not grabbed";
228   VLOG(1) << "ConfigureCrtc: crtc=" << crtc << " mode=" << mode
229           << " output=" << output << " x=" << x << " y=" << y;
230   // Xrandr.h is full of lies. XRRSetCrtcConfig() is defined as returning a
231   // Status, which is typically 0 for failure and 1 for success. In
232   // actuality it returns a RRCONFIGSTATUS, which uses 0 for success.
233   if (XRRSetCrtcConfig(display_,
234                        screen_,
235                        crtc,
236                        CurrentTime,
237                        x,
238                        y,
239                        mode,
240                        RR_Rotate_0,
241                        (output && mode) ? &output : NULL,
242                        (output && mode) ? 1 : 0) != RRSetConfigSuccess) {
243     LOG(WARNING) << "Unable to configure CRTC " << crtc << ":"
244                  << " mode=" << mode << " output=" << output << " x=" << x
245                  << " y=" << y;
246     return false;
247   }
248
249   return true;
250 }
251
252 void NativeDisplayDelegateX11::CreateFrameBuffer(const gfx::Size& size) {
253   CHECK(screen_) << "Server not grabbed";
254   gfx::Size current_screen_size(
255       DisplayWidth(display_, DefaultScreen(display_)),
256       DisplayHeight(display_, DefaultScreen(display_)));
257
258   VLOG(1) << "CreateFrameBuffer: new=" << size.ToString()
259           << " current=" << current_screen_size.ToString();
260
261   DestroyUnusedCrtcs();
262
263   if (size == current_screen_size)
264     return;
265
266   gfx::Size min_screen_size(current_screen_size);
267   min_screen_size.SetToMin(size);
268   UpdateCrtcsForNewFramebuffer(min_screen_size);
269
270   int mm_width = size.width() * kPixelsToMmScale;
271   int mm_height = size.height() * kPixelsToMmScale;
272   XRRSetScreenSize(
273       display_, window_, size.width(), size.height(), mm_width, mm_height);
274   // We don't wait for root window resize, therefore this end up with drawing
275   // in the old window size, which we care during the boot.
276   DrawBackground();
277
278   // Don't redraw the background upon framebuffer change again. This should
279   // happen only once after boot.
280   background_color_argb_ = 0;
281 }
282
283 void NativeDisplayDelegateX11::AddObserver(NativeDisplayObserver* observer) {
284   observers_.AddObserver(observer);
285 }
286
287 void NativeDisplayDelegateX11::RemoveObserver(NativeDisplayObserver* observer) {
288   observers_.RemoveObserver(observer);
289 }
290
291 void NativeDisplayDelegateX11::InitModes() {
292   CHECK(screen_) << "Server not grabbed";
293
294   STLDeleteContainerPairSecondPointers(modes_.begin(), modes_.end());
295   modes_.clear();
296
297   for (int i = 0; i < screen_->nmode; ++i) {
298     const XRRModeInfo& info = screen_->modes[i];
299     float refresh_rate = 0.0f;
300     if (info.hTotal && info.vTotal) {
301       refresh_rate =
302           static_cast<float>(info.dotClock) /
303           (static_cast<float>(info.hTotal) * static_cast<float>(info.vTotal));
304     }
305
306     modes_.insert(
307         std::make_pair(info.id,
308                        new DisplayModeX11(gfx::Size(info.width, info.height),
309                                           info.modeFlags & RR_Interlace,
310                                           refresh_rate,
311                                           info.id)));
312   }
313 }
314
315 DisplaySnapshotX11* NativeDisplayDelegateX11::InitDisplaySnapshot(
316     RROutput output,
317     XRROutputInfo* info,
318     RRCrtc* last_used_crtc,
319     int index) {
320   int64_t display_id = 0;
321   if (!GetDisplayId(output, static_cast<uint8_t>(index), &display_id))
322     display_id = index;
323
324   bool has_overscan = false;
325   GetOutputOverscanFlag(output, &has_overscan);
326
327   DisplayConnectionType type = GetDisplayConnectionTypeFromName(info->name);
328   if (type == DISPLAY_CONNECTION_TYPE_UNKNOWN)
329     LOG(ERROR) << "Unknown link type: " << info->name;
330
331   RRMode native_mode_id = GetOutputNativeMode(info);
332   RRMode current_mode_id = None;
333   gfx::Point origin;
334   if (info->crtc) {
335     XRRCrtcInfo* crtc_info = XRRGetCrtcInfo(display_, screen_, info->crtc);
336     current_mode_id = crtc_info->mode;
337     origin.SetPoint(crtc_info->x, crtc_info->y);
338     XRRFreeCrtcInfo(crtc_info);
339   }
340
341   RRCrtc crtc = None;
342   // Assign a CRTC that isn't already in use.
343   for (int i = 0; i < info->ncrtc; ++i) {
344     if (info->crtcs[i] != *last_used_crtc) {
345       crtc = info->crtcs[i];
346       *last_used_crtc = crtc;
347       break;
348     }
349   }
350
351   const DisplayMode* current_mode = NULL;
352   const DisplayMode* native_mode = NULL;
353   std::vector<const DisplayMode*> display_modes;
354
355   for (int i = 0; i < info->nmode; ++i) {
356     const RRMode mode = info->modes[i];
357     if (modes_.find(mode) != modes_.end()) {
358       display_modes.push_back(modes_.at(mode));
359
360       if (mode == current_mode_id)
361         current_mode = display_modes.back();
362       if (mode == native_mode_id)
363         native_mode = display_modes.back();
364     } else {
365       LOG(WARNING) << "Unable to find XRRModeInfo for mode " << mode;
366     }
367   }
368
369   DisplaySnapshotX11* display_snapshot =
370       new DisplaySnapshotX11(display_id,
371                              origin,
372                              gfx::Size(info->mm_width, info->mm_height),
373                              type,
374                              IsOutputAspectPreservingScaling(output),
375                              has_overscan,
376                              GetDisplayName(output),
377                              display_modes,
378                              current_mode,
379                              native_mode,
380                              output,
381                              crtc,
382                              index);
383
384   VLOG(1) << "Found display " << cached_outputs_.size() << ":"
385           << " output=" << output << " crtc=" << crtc
386           << " current_mode=" << current_mode_id;
387
388   return display_snapshot;
389 }
390
391 bool NativeDisplayDelegateX11::GetHDCPState(const DisplaySnapshot& output,
392                                             HDCPState* state) {
393   unsigned char* values = NULL;
394   int actual_format = 0;
395   unsigned long nitems = 0;
396   unsigned long bytes_after = 0;
397   Atom actual_type = None;
398   int success = 0;
399   RROutput output_id = static_cast<const DisplaySnapshotX11&>(output).output();
400   // TODO(kcwu): Use X11AtomCache to save round trip time of XInternAtom.
401   Atom prop = XInternAtom(display_, kContentProtectionAtomName, False);
402
403   bool ok = true;
404   // TODO(kcwu): Move this to x11_util (similar method calls in this file and
405   // output_util.cc)
406   success = XRRGetOutputProperty(display_,
407                                  output_id,
408                                  prop,
409                                  0,
410                                  100,
411                                  False,
412                                  False,
413                                  AnyPropertyType,
414                                  &actual_type,
415                                  &actual_format,
416                                  &nitems,
417                                  &bytes_after,
418                                  &values);
419   if (actual_type == None) {
420     LOG(ERROR) << "Property '" << kContentProtectionAtomName
421                << "' does not exist";
422     ok = false;
423   } else if (success == Success && actual_type == XA_ATOM &&
424              actual_format == 32 && nitems == 1) {
425     Atom value = reinterpret_cast<Atom*>(values)[0];
426     if (value == XInternAtom(display_, kProtectionUndesiredAtomName, False)) {
427       *state = HDCP_STATE_UNDESIRED;
428     } else if (value ==
429                XInternAtom(display_, kProtectionDesiredAtomName, False)) {
430       *state = HDCP_STATE_DESIRED;
431     } else if (value ==
432                XInternAtom(display_, kProtectionEnabledAtomName, False)) {
433       *state = HDCP_STATE_ENABLED;
434     } else {
435       LOG(ERROR) << "Unknown " << kContentProtectionAtomName
436                  << " value: " << value;
437       ok = false;
438     }
439   } else {
440     LOG(ERROR) << "XRRGetOutputProperty failed";
441     ok = false;
442   }
443   if (values)
444     XFree(values);
445
446   VLOG(3) << "HDCP state: " << ok << "," << *state;
447   return ok;
448 }
449
450 bool NativeDisplayDelegateX11::SetHDCPState(const DisplaySnapshot& output,
451                                             HDCPState state) {
452   Atom name = XInternAtom(display_, kContentProtectionAtomName, False);
453   Atom value = None;
454   switch (state) {
455     case HDCP_STATE_UNDESIRED:
456       value = XInternAtom(display_, kProtectionUndesiredAtomName, False);
457       break;
458     case HDCP_STATE_DESIRED:
459       value = XInternAtom(display_, kProtectionDesiredAtomName, False);
460       break;
461     default:
462       NOTREACHED() << "Invalid HDCP state: " << state;
463       return false;
464   }
465   gfx::X11ErrorTracker err_tracker;
466   unsigned char* data = reinterpret_cast<unsigned char*>(&value);
467   RROutput output_id = static_cast<const DisplaySnapshotX11&>(output).output();
468   XRRChangeOutputProperty(
469       display_, output_id, name, XA_ATOM, 32, PropModeReplace, data, 1);
470   if (err_tracker.FoundNewError()) {
471     LOG(ERROR) << "XRRChangeOutputProperty failed";
472     return false;
473   } else {
474     return true;
475   }
476 }
477
478 void NativeDisplayDelegateX11::DestroyUnusedCrtcs() {
479   CHECK(screen_) << "Server not grabbed";
480
481   for (int i = 0; i < screen_->ncrtc; ++i) {
482     bool in_use = false;
483     for (ScopedVector<DisplaySnapshot>::const_iterator it =
484              cached_outputs_.begin();
485          it != cached_outputs_.end();
486          ++it) {
487       DisplaySnapshotX11* x11_output = static_cast<DisplaySnapshotX11*>(*it);
488       if (screen_->crtcs[i] == x11_output->crtc()) {
489         in_use = true;
490         break;
491       }
492     }
493
494     if (!in_use)
495       ConfigureCrtc(screen_->crtcs[i], None, None, 0, 0);
496   }
497 }
498
499 void NativeDisplayDelegateX11::UpdateCrtcsForNewFramebuffer(
500     const gfx::Size& min_screen_size) {
501   CHECK(screen_) << "Server not grabbed";
502   // Setting the screen size will fail if any CRTC doesn't fit afterwards.
503   // At the same time, turning CRTCs off and back on uses up a lot of time.
504   // This function tries to be smart to avoid too many off/on cycles:
505   // - We set the new modes on CRTCs, if they fit in both the old and new
506   //   FBs, and park them at (0,0)
507   // - We disable the CRTCs we will need but don't fit in the old FB. Those
508   //   will be reenabled after the resize.
509   // We don't worry about the cached state of the outputs here since we are
510   // not interested in the state we are setting - we just try to get the CRTCs
511   // out of the way so we can rebuild the frame buffer.
512   gfx::Rect fb_rect(min_screen_size);
513   for (ScopedVector<DisplaySnapshot>::const_iterator it =
514            cached_outputs_.begin();
515        it != cached_outputs_.end();
516        ++it) {
517     DisplaySnapshotX11* x11_output = static_cast<DisplaySnapshotX11*>(*it);
518     const DisplayMode* mode_info = x11_output->current_mode();
519     RROutput output = x11_output->output();
520     RRMode mode = None;
521
522     if (mode_info) {
523       mode = static_cast<const DisplayModeX11*>(mode_info)->mode_id();
524
525       if (!fb_rect.Contains(gfx::Rect(mode_info->size()))) {
526         // In case our CRTC doesn't fit in common area of our current and about
527         // to be resized framebuffer, disable it.
528         // It'll get reenabled after we resize the framebuffer.
529         mode = None;
530         output = None;
531         mode_info = NULL;
532       }
533     }
534
535     ConfigureCrtc(x11_output->crtc(), mode, output, 0, 0);
536   }
537 }
538
539 bool NativeDisplayDelegateX11::IsOutputAspectPreservingScaling(RROutput id) {
540   bool ret = false;
541
542   Atom scaling_prop = XInternAtom(display_, "scaling mode", False);
543   Atom full_aspect_atom = XInternAtom(display_, "Full aspect", False);
544   if (scaling_prop == None || full_aspect_atom == None)
545     return false;
546
547   int nprop = 0;
548   Atom* props = XRRListOutputProperties(display_, id, &nprop);
549   for (int j = 0; j < nprop && !ret; j++) {
550     Atom prop = props[j];
551     if (scaling_prop == prop) {
552       unsigned char* values = NULL;
553       int actual_format;
554       unsigned long nitems;
555       unsigned long bytes_after;
556       Atom actual_type;
557       int success;
558
559       success = XRRGetOutputProperty(display_,
560                                      id,
561                                      prop,
562                                      0,
563                                      100,
564                                      False,
565                                      False,
566                                      AnyPropertyType,
567                                      &actual_type,
568                                      &actual_format,
569                                      &nitems,
570                                      &bytes_after,
571                                      &values);
572       if (success == Success && actual_type == XA_ATOM && actual_format == 32 &&
573           nitems == 1) {
574         Atom value = reinterpret_cast<Atom*>(values)[0];
575         if (full_aspect_atom == value)
576           ret = true;
577       }
578       if (values)
579         XFree(values);
580     }
581   }
582   if (props)
583     XFree(props);
584
585   return ret;
586 }
587
588
589 std::vector<ColorCalibrationProfile>
590 NativeDisplayDelegateX11::GetAvailableColorCalibrationProfiles(
591     const DisplaySnapshot& output) {
592   // TODO(mukai|marcheu): Checks the system data and fills the result.
593   // Note that the order would be Dynamic -> Standard -> Movie -> Reading.
594   return std::vector<ColorCalibrationProfile>();
595 }
596
597 bool NativeDisplayDelegateX11::SetColorCalibrationProfile(
598     const DisplaySnapshot& output,
599     ColorCalibrationProfile new_profile) {
600   const DisplaySnapshotX11& x11_output =
601       static_cast<const DisplaySnapshotX11&>(output);
602
603   XRRCrtcGamma* gamma_ramp = CreateGammaRampForProfile(x11_output, new_profile);
604
605   if (!gamma_ramp)
606     return false;
607
608   int gamma_ramp_size = XRRGetCrtcGammaSize(display_, x11_output.crtc());
609   XRRSetCrtcGamma(display_,
610                   x11_output.crtc(),
611                   ResampleGammaRamp(gamma_ramp, gamma_ramp_size));
612   XRRFreeGamma(gamma_ramp);
613   return true;
614 }
615
616 XRRCrtcGamma* NativeDisplayDelegateX11::CreateGammaRampForProfile(
617     const DisplaySnapshotX11& x11_output,
618     ColorCalibrationProfile new_profile) {
619   // TODO(mukai|marcheu): Creates the appropriate gamma ramp data from the
620   // profile enum. It would be served by the vendor.
621   return NULL;
622 }
623
624 void NativeDisplayDelegateX11::DrawBackground() {
625   if (!background_color_argb_)
626     return;
627   // Configuring CRTCs/Framebuffer clears the boot screen image.  Paint the
628   // same background color after updating framebuffer to minimize the
629   // duration of black screen at boot time.
630   XColor color;
631   Colormap colormap = DefaultColormap(display_, 0);
632   // XColor uses 16 bits per color.
633   color.red = (background_color_argb_ & 0x00FF0000) >> 8;
634   color.green = (background_color_argb_ & 0x0000FF00);
635   color.blue = (background_color_argb_ & 0x000000FF) << 8;
636   color.flags = DoRed | DoGreen | DoBlue;
637   XAllocColor(display_, colormap, &color);
638
639   GC gc = XCreateGC(display_, window_, 0, 0);
640   XSetForeground(display_, gc, color.pixel);
641   XSetFillStyle(display_, gc, FillSolid);
642   int width = DisplayWidth(display_, DefaultScreen(display_));
643   int height = DisplayHeight(display_, DefaultScreen(display_));
644   XFillRectangle(display_, window_, gc, 0, 0, width, height);
645   XFreeGC(display_, gc);
646   XFreeColors(display_, colormap, &color.pixel, 1, 0);
647 }
648
649 }  // namespace ui