blob: 09f190629928fea3aed0120e498f9e3f83056cf5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian67016af2012-02-02 15:14:13 -08002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <errno.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21
22#include <unistd.h>
23#include <fcntl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <utils/Log.h>
26
27#include "DisplayHardware/DisplayHardwareBase.h"
28#include "SurfaceFlinger.h"
29
30// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031namespace android {
32
Mathias Agopian3d031502011-04-18 16:25:40 -070033static char const * const kSleepFileName = "/sys/power/wait_for_fb_sleep";
34static char const * const kWakeFileName = "/sys/power/wait_for_fb_wake";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
36// ----------------------------------------------------------------------------
37
Mathias Agopian67016af2012-02-02 15:14:13 -080038DisplayHardwareBase::DisplayEventThread::DisplayEventThread(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 const sp<SurfaceFlinger>& flinger)
40 : Thread(false), mFlinger(flinger) {
41}
42
Mathias Agopian67016af2012-02-02 15:14:13 -080043DisplayHardwareBase::DisplayEventThread::~DisplayEventThread() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044}
45
Mathias Agopian67016af2012-02-02 15:14:13 -080046void DisplayHardwareBase::DisplayEventThread::onFirstRef() {
47 if (initCheck() == NO_ERROR) {
48 run("DisplayEventThread", PRIORITY_URGENT_DISPLAY);
49 } else {
50 ALOGW("/sys/power/wait_for_fb_{wake|sleep} don't exist");
51 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052}
53
Mathias Agopian67016af2012-02-02 15:14:13 -080054status_t DisplayHardwareBase::DisplayEventThread::initCheck() const {
55 return ((access(kSleepFileName, R_OK) == 0 &&
56 access(kWakeFileName, R_OK) == 0)) ? NO_ERROR : NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057}
58
Mathias Agopian67016af2012-02-02 15:14:13 -080059bool DisplayHardwareBase::DisplayEventThread::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopian67016af2012-02-02 15:14:13 -080061 if (waitForFbSleep() == NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 sp<SurfaceFlinger> flinger = mFlinger.promote();
Steve Block9d453682011-12-20 16:23:08 +000063 ALOGD("About to give-up screen, flinger = %p", flinger.get());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 if (flinger != 0) {
65 mBarrier.close();
66 flinger->screenReleased(0);
67 mBarrier.wait();
68 }
Mathias Agopian67016af2012-02-02 15:14:13 -080069 if (waitForFbWake() == NO_ERROR) {
70 sp<SurfaceFlinger> flinger = mFlinger.promote();
71 ALOGD("Screen about to return, flinger = %p", flinger.get());
72 if (flinger != 0) {
73 flinger->screenAcquired(0);
74 }
75 return true;
76 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 }
Mathias Agopian67016af2012-02-02 15:14:13 -080078
79 // error, exit the thread
80 return false;
81}
82
83status_t DisplayHardwareBase::DisplayEventThread::waitForFbSleep() {
84 int err = 0;
85 char buf;
86 int fd = open(kSleepFileName, O_RDONLY, 0);
87 // if the file doesn't exist, the error will be caught in read() below
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 do {
Mathias Agopian67016af2012-02-02 15:14:13 -080089 err = read(fd, &buf, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 } while (err < 0 && errno == EINTR);
91 close(fd);
Mathias Agopian67016af2012-02-02 15:14:13 -080092 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
93 return err < 0 ? -errno : int(NO_ERROR);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094}
95
Mathias Agopian67016af2012-02-02 15:14:13 -080096status_t DisplayHardwareBase::DisplayEventThread::waitForFbWake() {
97 int err = 0;
98 char buf;
99 int fd = open(kWakeFileName, O_RDONLY, 0);
100 // if the file doesn't exist, the error will be caught in read() below
101 do {
102 err = read(fd, &buf, 1);
103 } while (err < 0 && errno == EINTR);
104 close(fd);
105 ALOGE_IF(err<0, "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
106 return err < 0 ? -errno : int(NO_ERROR);
107}
108
109status_t DisplayHardwareBase::DisplayEventThread::releaseScreen() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 mBarrier.open();
111 return NO_ERROR;
112}
113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114// ----------------------------------------------------------------------------
115
116DisplayHardwareBase::DisplayHardwareBase(const sp<SurfaceFlinger>& flinger,
117 uint32_t displayIndex)
Mathias Agopianf7cdd052011-10-10 22:18:55 -0700118 : mScreenAcquired(true)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119{
120 mDisplayEventThread = new DisplayEventThread(flinger);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
Mathias Agopian67016af2012-02-02 15:14:13 -0800123DisplayHardwareBase::~DisplayHardwareBase() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 // request exit
125 mDisplayEventThread->requestExitAndWait();
126}
127
Mathias Agopian67016af2012-02-02 15:14:13 -0800128bool DisplayHardwareBase::canDraw() const {
Mathias Agopianf7cdd052011-10-10 22:18:55 -0700129 return mScreenAcquired;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130}
131
Mathias Agopian67016af2012-02-02 15:14:13 -0800132void DisplayHardwareBase::releaseScreen() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 status_t err = mDisplayEventThread->releaseScreen();
134 if (err >= 0) {
Mathias Agopian59119e62010-10-11 12:37:43 -0700135 mScreenAcquired = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
137}
138
Mathias Agopian67016af2012-02-02 15:14:13 -0800139void DisplayHardwareBase::acquireScreen() const {
140 mScreenAcquired = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}
142
Mathias Agopian67016af2012-02-02 15:14:13 -0800143bool DisplayHardwareBase::isScreenAcquired() const {
Mathias Agopian59119e62010-10-11 12:37:43 -0700144 return mScreenAcquired;
145}
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}; // namespace android