1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#ifndef UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_
6c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#define UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <stdint.h>
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <map>
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <string>
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/event_types.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/observer_list.h"
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/timer/timer.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/display/display_export.h"
20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ui/display/types/display_constants.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/display/types/native_display_observer.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ui/gfx/geometry/size.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)namespace gfx {
2523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class Point;
2623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class Size;
2723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace ui {
3023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class DisplayMode;
3123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class DisplaySnapshot;
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NativeDisplayDelegate;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// This class interacts directly with the system display configurator.
35c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass DISPLAY_EXPORT DisplayConfigurator : public NativeDisplayObserver {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  typedef uint64_t ContentProtectionClientId;
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  static const ContentProtectionClientId kInvalidClientId = 0;
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  struct DisplayState {
4123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    DisplayState();
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    DisplaySnapshot* display;  // Not owned.
443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // User-selected mode for the display.
4623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    const DisplayMode* selected_mode;
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Mode used when displaying the same desktop on multiple displays.
4923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    const DisplayMode* mirror_mode;
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  typedef std::vector<DisplayState> DisplayStateList;
5323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class Observer {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    virtual ~Observer() {}
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Called after the display mode has been changed. |display| contains the
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // just-applied configuration. Note that the X server is no longer grabbed
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // when this method is called, so the actual configuration could've changed
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // already.
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    virtual void OnDisplayModeChanged(
63c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        const std::vector<DisplayState>& displays) {}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Called after a display mode change attempt failed. |failed_new_state| is
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // the new state which the system failed to enter.
67c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    virtual void OnDisplayModeChangeFailed(
68c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        MultipleDisplayState failed_new_state) {}
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
71c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Interface for classes that make decisions about which display state
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // should be used.
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  class StateController {
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    virtual ~StateController() {}
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Called when displays are detected.
78c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    virtual MultipleDisplayState GetStateForDisplayIds(
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        const std::vector<int64_t>& display_ids) const = 0;
80a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
81c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // Queries the resolution (|size|) in pixels to select display mode for the
8223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    // given display id.
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    virtual bool GetResolutionForDisplayId(int64_t display_id,
8423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                           gfx::Size* size) const = 0;
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  };
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Interface for classes that implement software based mirroring.
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  class SoftwareMirroringController {
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)   public:
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    virtual ~SoftwareMirroringController() {}
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Called when the hardware mirroring failed.
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    virtual void SetSoftwareMirroring(bool enabled) = 0;
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    virtual bool SoftwareMirroringEnabled() const = 0;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Helper class used by tests.
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  class TestApi {
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)   public:
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    TestApi(DisplayConfigurator* configurator) : configurator_(configurator) {}
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ~TestApi() {}
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // If |configure_timer_| is started, stops the timer, runs
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // ConfigureDisplays(), and returns true; returns false otherwise.
1056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool TriggerConfigureTimeout() WARN_UNUSED_RESULT;
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)   private:
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    DisplayConfigurator* configurator_;  // not owned
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(TestApi);
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Flags that can be passed to SetDisplayPower().
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static const int kSetDisplayPowerNoFlags;
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Configure displays even if the passed-in state matches |power_state_|.
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static const int kSetDisplayPowerForceProbe;
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Do not change the state if multiple displays are connected or if the
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // only connected display is external.
1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static const int kSetDisplayPowerOnlyIfSingleInternalDisplay;
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gap between screens so cursor at bottom of active display doesn't
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // partially appear on top of inactive display. Higher numbers guard
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // against larger cursors, but also waste more memory.
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For simplicity, this is hard-coded to avoid the complexity of always
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // determining the DPI of the screen and rationalizing which screen we
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // need to use for the DPI calculation.
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // See crbug.com/130188 for initial discussion.
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static const int kVerticalGap = 60;
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
130c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Returns the mode within |display| that matches the given size with highest
131c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // refresh rate. Returns None if no matching display was found.
13223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  static const DisplayMode* FindDisplayModeMatchingSize(
133c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      const DisplaySnapshot& display,
13423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      const gfx::Size& size);
135d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
136c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DisplayConfigurator();
137c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual ~DisplayConfigurator();
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
139c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MultipleDisplayState display_state() const { return display_state_; }
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  chromeos::DisplayPowerState requested_power_state() const {
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return requested_power_state_;
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const gfx::Size framebuffer_size() const { return framebuffer_size_; }
144c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  const std::vector<DisplayState>& cached_displays() const {
145c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    return cached_displays_;
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void set_state_controller(StateController* controller) {
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    state_controller_ = controller;
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void set_mirroring_controller(SoftwareMirroringController* controller) {
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    mirroring_controller_ = controller;
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Replaces |native_display_delegate_| with the delegate passed in and sets
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // |configure_display_| to true. Should be called before Init().
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void SetDelegateForTesting(
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr<NativeDisplayDelegate> display_delegate);
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
160c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Sets the initial value of |power_state_|.  Must be called before Start().
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void SetInitialDisplayPower(chromeos::DisplayPowerState power_state);
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialization, must be called right after constructor.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |is_panel_fitting_enabled| indicates hardware panel fitting support.
165a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void Init(bool is_panel_fitting_enabled);
166a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
167a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Does initial configuration of displays during startup.
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If |background_color_argb| is non zero and there are multiple displays,
169c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // DisplayConfigurator sets the background color of X's RootWindow to this
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // color.
171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void ForceInitialConfigure(uint32_t background_color_argb);
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Stop handling display configuration events/requests.
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void PrepareForExit();
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when powerd notifies us that some set of displays should be turned
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on or off.  This requires enabling or disabling the CRTC associated with
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the display(s) in question so that the low power state is engaged.
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // |flags| contains bitwise-or-ed kSetDisplayPower* values. Returns true if
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // the system successfully enters (or was already in) |power_state|.
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool SetDisplayPower(chromeos::DisplayPowerState power_state, int flags);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Force switching the display mode to |new_state|. Returns false if
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // switching failed (possibly because |new_state| is invalid for the
185c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // current set of connected displays).
186c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool SetDisplayMode(MultipleDisplayState new_state);
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // NativeDisplayDelegate::Observer overrides:
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnConfigurationChanged() OVERRIDE;
190558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddObserver(Observer* observer);
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveObserver(Observer* observer);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets all the displays into pre-suspend mode; usually this means
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // configure them for their resume state. This allows faster resume on
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // machines where display configuration is slow.
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SuspendDisplays();
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Reprobes displays to handle changes made while the system was
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // suspended.
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ResumeDisplays();
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
203c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Registers a client for display protection and requests a client id. Returns
2044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // 0 if requesting failed.
205c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ContentProtectionClientId RegisterContentProtectionClient();
2064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Unregisters the client.
208c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void UnregisterContentProtectionClient(ContentProtectionClientId client_id);
2094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Queries link status and protection status.
211c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |link_mask| is the type of connected display links, which is a bitmask of
212c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // DisplayConnectionType values. |protection_mask| is the desired protection
213c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // methods, which is a bitmask of the ContentProtectionMethod values.
2144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Returns true on success.
215c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool QueryContentProtectionStatus(ContentProtectionClientId client_id,
216c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    int64_t display_id,
217c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    uint32_t* link_mask,
218c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                    uint32_t* protection_mask);
2194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Requests the desired protection methods.
2214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // |protection_mask| is the desired protection methods, which is a bitmask
222c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // of the ContentProtectionMethod values.
2234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Returns true when the protection request has been made.
224c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool EnableContentProtection(ContentProtectionClientId client_id,
225c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                               int64_t display_id,
226c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                               uint32_t desired_protection_mask);
2274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
228effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Checks the available color profiles for |display_id| and fills the result
229effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // into |profiles|.
230effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  std::vector<ui::ColorCalibrationProfile> GetAvailableColorCalibrationProfiles(
231effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      int64_t display_id);
232effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
23323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Updates the color calibration to |new_profile|.
234c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool SetColorCalibrationProfile(int64_t display_id,
235c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                  ui::ColorCalibrationProfile new_profile);
23623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
237a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) private:
2380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Mapping a display_id to a protection request bitmask.
239c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  typedef std::map<int64_t, uint32_t> ContentProtections;
2400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Mapping a client to its protection request.
241c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  typedef std::map<ContentProtectionClientId, ContentProtections>
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ProtectionRequests;
2434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
244a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Performs platform specific delegate initialization.
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<NativeDisplayDelegate> CreatePlatformNativeDisplayDelegate();
246a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
247c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Updates |cached_displays_| to contain currently-connected displays. Calls
248c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |delegate_->GetDisplays()| and then does additional work, like finding the
24968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // mirror mode and setting user-preferred modes. Note that the server must be
25068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // grabbed via |delegate_->GrabServer()| first.
251c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void UpdateCachedDisplays();
25268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
253c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Helper method for UpdateCachedDisplays() that initializes the passed-in
254c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // displays' |mirror_mode| fields by looking for a mode in |internal_display|
255c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // and |external_display| having the same resolution. Returns false if a
256c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // shared
257d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // mode wasn't found or created.
258d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  //
259d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |try_panel_fitting| allows creating a panel-fitting mode for
260c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |internal_display| instead of only searching for a matching mode (note that
261d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // it may lead to a crash if |internal_info| is not capable of panel fitting).
262d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  //
263d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // |preserve_aspect| limits the search/creation only to the modes having the
264c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // native aspect ratio of |external_display|.
265c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool FindMirrorMode(DisplayState* internal_display,
266c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                      DisplayState* external_display,
267d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                      bool try_panel_fitting,
268d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                      bool preserve_aspect);
269d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
270c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Configures displays.
271c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void ConfigureDisplays();
272a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
2733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Notifies observers about an attempted state change.
274c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void NotifyObservers(bool success, MultipleDisplayState attempted_state);
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
276c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Switches to the state specified in |display_state| and |power_state|.
27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If the hardware mirroring failed and |mirroring_controller_| is set,
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // it switches to |STATE_DUAL_EXTENDED| and calls |SetSoftwareMirroring()|
279ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // to enable software based mirroring.
280c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // On success, updates |display_state_|, |power_state_|, and
281c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |cached_displays_| and returns true.
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool EnterStateOrFallBackToSoftwareMirroring(
283c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      MultipleDisplayState display_state,
284a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      chromeos::DisplayPowerState power_state);
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
286c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Switches to the state specified in |display_state| and |power_state|.
287c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // On success, updates |display_state_|, |power_state_|, and
288c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // |cached_displays_| and returns true.
289c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool EnterState(MultipleDisplayState display_state,
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  chromeos::DisplayPowerState power_state);
29168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
292c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Returns the display state that should be used with |cached_displays_| while
29368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // in |power_state|.
294c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MultipleDisplayState ChooseDisplayState(
295c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      chromeos::DisplayPowerState power_state) const;
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns the ratio between mirrored mode area and native mode area:
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // (mirror_mode_width * mirrow_mode_height) / (native_width * native_height)
299c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  float GetMirroredDisplayAreaRatio(const DisplayState& display);
300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
301cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns true if in either hardware or software mirroring mode.
302cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool IsMirroring() const;
303cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
304c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Applies display protections according to requests.
305c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool ApplyProtections(const ContentProtections& requests);
3060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StateController* state_controller_;
30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  SoftwareMirroringController* mirroring_controller_;
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<NativeDisplayDelegate> native_display_delegate_;
310c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
311d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Used to enable modes which rely on panel fitting.
312d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  bool is_panel_fitting_enabled_;
313d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is detected by the constructor to determine whether or not we should
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be enabled.  If we aren't running on ChromeOS, we can't assume that the
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Xrandr X11 extension is supported.
317c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // If this flag is set to false, any attempts to change the display
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // configuration to immediately fail without changing the state.
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool configure_display_;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The current display state.
322c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  MultipleDisplayState display_state_;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
324cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  gfx::Size framebuffer_size_;
325cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // The last-requested and current power state. These may differ if
3271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // configuration fails: SetDisplayMode() needs the last-requested state while
3281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // SetDisplayPower() needs the current state.
3291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  chromeos::DisplayPowerState requested_power_state_;
3301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  chromeos::DisplayPowerState current_power_state_;
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
332c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Most-recently-used display configuration. Note that the actual
333ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // configuration changes asynchronously.
334c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DisplayStateList cached_displays_;
335ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObserverList<Observer> observers_;
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // The timer to delay configuring displays. This is used to aggregate multiple
3396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // display configuration events when they are reported in short time spans.
3406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // See comment for NativeDisplayEventDispatcherX11 for more details.
3416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::OneShotTimer<DisplayConfigurator> configure_timer_;
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
343c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Id for next display protection client.
344c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ContentProtectionClientId next_display_protection_client_id_;
3454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
346c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Display protection requests of each client.
3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ProtectionRequests client_protection_requests_;
3484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
349c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DISALLOW_COPY_AND_ASSIGN(DisplayConfigurator);
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
352a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace ui
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
354c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#endif  // UI_DISPLAY_CHROMEOS_DISPLAY_CONFIGURATOR_H_
355