The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2012 The Android Open Source Project |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include "DisplayHardware/DisplayHardwareBase.h" |
| 28 | #include "SurfaceFlinger.h" |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Mathias Agopian | 3d03150 | 2011-04-18 16:25:40 -0700 | [diff] [blame] | 33 | static char const * const kSleepFileName = "/sys/power/wait_for_fb_sleep"; |
| 34 | static char const * const kWakeFileName = "/sys/power/wait_for_fb_wake"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 38 | DisplayHardwareBase::DisplayEventThread::DisplayEventThread( |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | const sp<SurfaceFlinger>& flinger) |
| 40 | : Thread(false), mFlinger(flinger) { |
| 41 | } |
| 42 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 43 | DisplayHardwareBase::DisplayEventThread::~DisplayEventThread() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 46 | void 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 54 | status_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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 59 | bool DisplayHardwareBase::DisplayEventThread::threadLoop() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 61 | if (waitForFbSleep() == NO_ERROR) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | sp<SurfaceFlinger> flinger = mFlinger.promote(); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 63 | ALOGD("About to give-up screen, flinger = %p", flinger.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | if (flinger != 0) { |
| 65 | mBarrier.close(); |
| 66 | flinger->screenReleased(0); |
| 67 | mBarrier.wait(); |
| 68 | } |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 69 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 78 | |
| 79 | // error, exit the thread |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | status_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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | do { |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 89 | err = read(fd, &buf, 1); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } while (err < 0 && errno == EINTR); |
| 91 | close(fd); |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 92 | 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 96 | status_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 | |
| 109 | status_t DisplayHardwareBase::DisplayEventThread::releaseScreen() const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | mBarrier.open(); |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | // ---------------------------------------------------------------------------- |
| 115 | |
| 116 | DisplayHardwareBase::DisplayHardwareBase(const sp<SurfaceFlinger>& flinger, |
| 117 | uint32_t displayIndex) |
Mathias Agopian | f7cdd05 | 2011-10-10 22:18:55 -0700 | [diff] [blame] | 118 | : mScreenAcquired(true) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | { |
| 120 | mDisplayEventThread = new DisplayEventThread(flinger); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 123 | DisplayHardwareBase::~DisplayHardwareBase() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | // request exit |
| 125 | mDisplayEventThread->requestExitAndWait(); |
| 126 | } |
| 127 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 128 | bool DisplayHardwareBase::canDraw() const { |
Mathias Agopian | f7cdd05 | 2011-10-10 22:18:55 -0700 | [diff] [blame] | 129 | return mScreenAcquired; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 132 | void DisplayHardwareBase::releaseScreen() const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | status_t err = mDisplayEventThread->releaseScreen(); |
| 134 | if (err >= 0) { |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 135 | mScreenAcquired = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 139 | void DisplayHardwareBase::acquireScreen() const { |
| 140 | mScreenAcquired = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Mathias Agopian | 67016af | 2012-02-02 15:14:13 -0800 | [diff] [blame^] | 143 | bool DisplayHardwareBase::isScreenAcquired() const { |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 144 | return mScreenAcquired; |
| 145 | } |
| 146 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | }; // namespace android |