This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chromeos: Add support for dimming the screen.
This makes Chrome listen for messages from the power manager asking it to dim or undim the screen. We'll use this on desktop devices with external displays to dim the screen just before blanking it when the user is idle. BUG=125176 TEST=added, also did manual testing with powerd changes Review URL: http://codereview.chromium.org/10263011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@134638 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
derat@chromium.org
committed
Apr 30, 2012
1 parent
d2e45cc
commit 0dfd64b
Showing
13 changed files
with
329 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ash/wm/screen_dimmer.h" | ||
|
||
#include "ash/shell.h" | ||
#include "base/time.h" | ||
#include "ui/aura/root_window.h" | ||
#include "ui/gfx/compositor/layer.h" | ||
#include "ui/gfx/compositor/scoped_layer_animation_settings.h" | ||
#include "ui/gfx/rect.h" | ||
#include "ui/gfx/size.h" | ||
|
||
namespace ash { | ||
namespace internal { | ||
|
||
namespace { | ||
|
||
// Opacity for |dimming_layer_| when it's dimming the screen. | ||
const float kDimmingLayerOpacity = 0.4f; | ||
|
||
// Duration for dimming animations, in milliseconds. | ||
const int kDimmingTransitionMs = 200; | ||
|
||
} // namespace | ||
|
||
ScreenDimmer::ScreenDimmer() : currently_dimming_(false) { | ||
Shell::GetInstance()->GetRootWindow()->AddRootWindowObserver(this); | ||
} | ||
|
||
ScreenDimmer::~ScreenDimmer() { | ||
Shell::GetInstance()->GetRootWindow()->RemoveRootWindowObserver(this); | ||
} | ||
|
||
void ScreenDimmer::SetDimming(bool should_dim) { | ||
if (should_dim == currently_dimming_) | ||
return; | ||
|
||
if (!dimming_layer_.get()) { | ||
dimming_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); | ||
dimming_layer_->SetColor(SK_ColorBLACK); | ||
dimming_layer_->SetOpacity(0.0f); | ||
ui::Layer* root_layer = Shell::GetRootWindow()->layer(); | ||
dimming_layer_->SetBounds(root_layer->bounds()); | ||
root_layer->Add(dimming_layer_.get()); | ||
root_layer->StackAtTop(dimming_layer_.get()); | ||
} | ||
|
||
currently_dimming_ = should_dim; | ||
|
||
ui::ScopedLayerAnimationSettings scoped_settings( | ||
dimming_layer_->GetAnimator()); | ||
scoped_settings.SetTransitionDuration( | ||
base::TimeDelta::FromMilliseconds(kDimmingTransitionMs)); | ||
dimming_layer_->SetOpacity(should_dim ? kDimmingLayerOpacity : 0.0f); | ||
} | ||
|
||
void ScreenDimmer::OnRootWindowResized(const aura::RootWindow* root, | ||
const gfx::Size& old_size) { | ||
if (dimming_layer_.get()) | ||
dimming_layer_->SetBounds(gfx::Rect(root->bounds().size())); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace ash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef ASH_WM_SCREEN_DIMMER_H_ | ||
#define ASH_WM_SCREEN_DIMMER_H_ | ||
#pragma once | ||
|
||
#include "ash/ash_export.h" | ||
#include "base/basictypes.h" | ||
#include "base/compiler_specific.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "ui/aura/root_window_observer.h" | ||
|
||
namespace ui { | ||
class Layer; | ||
} | ||
|
||
namespace ash { | ||
namespace internal { | ||
|
||
// ScreenDimmer displays a partially-opaque layer above everything else to | ||
// darken the screen. It shouldn't be used for long-term brightness adjustments | ||
// due to performance considerations -- it's only intended for cases where we | ||
// want to briefly dim the screen (e.g. to indicate to the user that we're about | ||
// to suspend a machine that lacks an internal backlight that can be adjusted). | ||
class ASH_EXPORT ScreenDimmer : public aura::RootWindowObserver { | ||
public: | ||
class TestApi { | ||
public: | ||
explicit TestApi(ScreenDimmer* dimmer) : dimmer_(dimmer) {} | ||
|
||
ui::Layer* layer() { return dimmer_->dimming_layer_.get(); } | ||
|
||
private: | ||
ScreenDimmer* dimmer_; // not owned | ||
|
||
DISALLOW_COPY_AND_ASSIGN(TestApi); | ||
}; | ||
|
||
ScreenDimmer(); | ||
virtual ~ScreenDimmer(); | ||
|
||
// Dim or undim the screen. | ||
void SetDimming(bool should_dim); | ||
|
||
// aura::RootWindowObserver overrides: | ||
virtual void OnRootWindowResized(const aura::RootWindow* root, | ||
const gfx::Size& old_size) OVERRIDE; | ||
|
||
private: | ||
friend class TestApi; | ||
|
||
// Partially-opaque layer that's stacked above all of the root window's | ||
// children and used to dim the screen. NULL until the first time we dim. | ||
scoped_ptr<ui::Layer> dimming_layer_; | ||
|
||
// Are we currently dimming the screen? | ||
bool currently_dimming_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(ScreenDimmer); | ||
}; | ||
|
||
} // namespace internal | ||
} // namespace ash | ||
|
||
#endif // ASH_WM_SCREEN_DIMMER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "ash/wm/screen_dimmer.h" | ||
|
||
#include "ash/shell.h" | ||
#include "ash/test/ash_test_base.h" | ||
#include "base/basictypes.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "ui/aura/root_window.h" | ||
#include "ui/gfx/compositor/layer.h" | ||
|
||
namespace ash { | ||
namespace test { | ||
|
||
class ScreenDimmerTest : public AshTestBase { | ||
public: | ||
ScreenDimmerTest() : dimmer_(NULL) {} | ||
virtual ~ScreenDimmerTest() {} | ||
|
||
void SetUp() OVERRIDE { | ||
AshTestBase::SetUp(); | ||
dimmer_ = Shell::GetInstance()->screen_dimmer(); | ||
test_api_.reset(new internal::ScreenDimmer::TestApi(dimmer_)); | ||
} | ||
|
||
protected: | ||
internal::ScreenDimmer* dimmer_; // not owned | ||
|
||
scoped_ptr<internal::ScreenDimmer::TestApi> test_api_; | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(ScreenDimmerTest); | ||
}; | ||
|
||
TEST_F(ScreenDimmerTest, DimAndUndim) { | ||
// Don't create a layer until we need to. | ||
EXPECT_TRUE(test_api_->layer() == NULL); | ||
dimmer_->SetDimming(false); | ||
EXPECT_TRUE(test_api_->layer() == NULL); | ||
|
||
// When we enable dimming, the layer should be created and stacked at the top | ||
// of the root's children. | ||
dimmer_->SetDimming(true); | ||
ASSERT_TRUE(test_api_->layer() != NULL); | ||
ui::Layer* root_layer = Shell::GetInstance()->GetRootWindow()->layer(); | ||
ASSERT_TRUE(!root_layer->children().empty()); | ||
EXPECT_EQ(test_api_->layer(), root_layer->children().back()); | ||
EXPECT_TRUE(test_api_->layer()->visible()); | ||
EXPECT_GT(test_api_->layer()->GetTargetOpacity(), 0.0f); | ||
|
||
// When we disable dimming, the layer should be animated back to full | ||
// transparency. | ||
dimmer_->SetDimming(false); | ||
ASSERT_TRUE(test_api_->layer() != NULL); | ||
EXPECT_TRUE(test_api_->layer()->visible()); | ||
EXPECT_FLOAT_EQ(0.0f, test_api_->layer()->GetTargetOpacity()); | ||
} | ||
|
||
TEST_F(ScreenDimmerTest, ResizeLayer) { | ||
// The dimming layer should be initially sized to cover the root window. | ||
dimmer_->SetDimming(true); | ||
ui::Layer* dimming_layer = test_api_->layer(); | ||
ASSERT_TRUE(dimming_layer != NULL); | ||
ui::Layer* root_layer = Shell::GetInstance()->GetRootWindow()->layer(); | ||
EXPECT_EQ(gfx::Rect(root_layer->bounds().size()).ToString(), | ||
dimming_layer->bounds().ToString()); | ||
|
||
// When we resize the root window, the dimming layer should be resized to | ||
// match. | ||
gfx::Size kNewSize(400, 300); | ||
Shell::GetInstance()->GetRootWindow()->SetHostSize(kNewSize); | ||
EXPECT_EQ(kNewSize.ToString(), dimming_layer->bounds().size().ToString()); | ||
} | ||
|
||
} // namespace test | ||
} // namespace ash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/chromeos/power/screen_dimming_observer.h" | ||
|
||
#include "ash/shell.h" | ||
#include "ash/wm/screen_dimmer.h" | ||
#include "chromeos/dbus/dbus_thread_manager.h" | ||
|
||
namespace chromeos { | ||
|
||
ScreenDimmingObserver::ScreenDimmingObserver() { | ||
DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | ||
} | ||
|
||
ScreenDimmingObserver::~ScreenDimmingObserver() { | ||
DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | ||
} | ||
|
||
void ScreenDimmingObserver::ScreenDimmingRequested(ScreenDimmingState state) { | ||
ash::Shell::GetInstance()->screen_dimmer()->SetDimming( | ||
state == PowerManagerClient::Observer::SCREEN_DIMMING_IDLE); | ||
} | ||
|
||
} // namespace chromeos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_CHROMEOS_POWER_SCREEN_DIMMING_OBSERVER_H_ | ||
#define CHROME_BROWSER_CHROMEOS_POWER_SCREEN_DIMMING_OBSERVER_H_ | ||
#pragma once | ||
|
||
#include "base/basictypes.h" | ||
#include "base/compiler_specific.h" | ||
#include "chromeos/dbus/power_manager_client.h" | ||
|
||
namespace chromeos { | ||
|
||
// Listens for requests to enable or disable dimming of the screen in software | ||
// (as opposed to by adjusting a backlight). | ||
class ScreenDimmingObserver : public PowerManagerClient::Observer { | ||
public: | ||
// This class registers/unregisters itself as an observer in ctor/dtor. | ||
ScreenDimmingObserver(); | ||
virtual ~ScreenDimmingObserver(); | ||
|
||
private: | ||
// PowerManagerClient::Observer implementation. | ||
virtual void ScreenDimmingRequested(ScreenDimmingState state) OVERRIDE; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(ScreenDimmingObserver); | ||
}; | ||
|
||
} // namespace chromeos | ||
|
||
#endif // CHROME_BROWSER_CHROMEOS_POWER_SCREEN_DIMMING_OBSERVER_H_ |
Oops, something went wrong.