Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 17 | #define LOG_TAG "android.hardware.automotive.evs@1.0-service" |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 18 | |
| 19 | #include "EvsDisplay.h" |
| 20 | |
| 21 | #include <ui/GraphicBufferAllocator.h> |
| 22 | #include <ui/GraphicBufferMapper.h> |
| 23 | |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 27 | namespace automotive { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 28 | namespace evs { |
| 29 | namespace V1_0 { |
| 30 | namespace implementation { |
| 31 | |
| 32 | |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 33 | EvsDisplay::EvsDisplay() { |
| 34 | ALOGD("EvsDisplay instantiated"); |
| 35 | |
| 36 | // Set up our self description |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 37 | // NOTE: These are arbitrary values chosen for testing |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 38 | mInfo.displayId = "Mock Display"; |
| 39 | mInfo.vendorFlags = 3870; |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 40 | |
| 41 | // Assemble the buffer description we'll use for our render target |
| 42 | mBuffer.width = 320; |
| 43 | mBuffer.height = 240; |
| 44 | mBuffer.format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 45 | mBuffer.usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER; |
| 46 | mBuffer.bufferId = 0x3870; // Arbitrary magic number for self recognition |
| 47 | mBuffer.pixelSize = 4; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
| 51 | EvsDisplay::~EvsDisplay() { |
| 52 | ALOGD("EvsDisplay being destroyed"); |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 53 | forceShutdown(); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * This gets called if another caller "steals" ownership of the display |
| 59 | */ |
| 60 | void EvsDisplay::forceShutdown() |
| 61 | { |
| 62 | ALOGD("EvsDisplay forceShutdown"); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 63 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 64 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 65 | // If the buffer isn't being held by a remote client, release it now as an |
| 66 | // optimization to release the resources more quickly than the destructor might |
| 67 | // get called. |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 68 | if (mBuffer.memHandle) { |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 69 | // Report if we're going away while a buffer is outstanding |
| 70 | if (mFrameBusy) { |
| 71 | ALOGE("EvsDisplay going down while client is holding a buffer"); |
| 72 | } |
| 73 | |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 74 | // Drop the graphics buffer we've been using |
| 75 | GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 76 | alloc.free(mBuffer.memHandle); |
| 77 | mBuffer.memHandle = nullptr; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 78 | } |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 79 | |
| 80 | // Put this object into an unrecoverable error state since somebody else |
| 81 | // is going to own the display now. |
| 82 | mRequestedState = DisplayState::DEAD; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Returns basic information about the EVS display provided by the system. |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 88 | * See the description of the DisplayDesc structure for details. |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 89 | */ |
| 90 | Return<void> EvsDisplay::getDisplayInfo(getDisplayInfo_cb _hidl_cb) { |
| 91 | ALOGD("getDisplayInfo"); |
| 92 | |
| 93 | // Send back our self description |
| 94 | _hidl_cb(mInfo); |
| 95 | return Void(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Clients may set the display state to express their desired state. |
| 101 | * The HAL implementation must gracefully accept a request for any state |
| 102 | * while in any other state, although the response may be to ignore the request. |
| 103 | * The display is defined to start in the NOT_VISIBLE state upon initialization. |
| 104 | * The client is then expected to request the VISIBLE_ON_NEXT_FRAME state, and |
| 105 | * then begin providing video. When the display is no longer required, the client |
| 106 | * is expected to request the NOT_VISIBLE state after passing the last video frame. |
| 107 | */ |
| 108 | Return<EvsResult> EvsDisplay::setDisplayState(DisplayState state) { |
| 109 | ALOGD("setDisplayState"); |
| 110 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 111 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 112 | if (mRequestedState == DisplayState::DEAD) { |
| 113 | // This object no longer owns the display -- it's been superceeded! |
| 114 | return EvsResult::OWNERSHIP_LOST; |
| 115 | } |
| 116 | |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 117 | // Ensure we recognize the requested state so we don't go off the rails |
| 118 | if (state < DisplayState::NUM_STATES) { |
| 119 | // Record the requested state |
| 120 | mRequestedState = state; |
| 121 | return EvsResult::OK; |
| 122 | } |
| 123 | else { |
| 124 | // Turn off the display if asked for an unrecognized state |
| 125 | mRequestedState = DisplayState::NOT_VISIBLE; |
| 126 | return EvsResult::INVALID_ARG; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * The HAL implementation should report the actual current state, which might |
| 133 | * transiently differ from the most recently requested state. Note, however, that |
| 134 | * the logic responsible for changing display states should generally live above |
| 135 | * the device layer, making it undesirable for the HAL implementation to |
| 136 | * spontaneously change display states. |
| 137 | */ |
| 138 | Return<DisplayState> EvsDisplay::getDisplayState() { |
| 139 | ALOGD("getDisplayState"); |
| 140 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 141 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 142 | return mRequestedState; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | /** |
| 147 | * This call returns a handle to a frame buffer associated with the display. |
| 148 | * This buffer may be locked and written to by software and/or GL. This buffer |
| 149 | * must be returned via a call to returnTargetBufferForDisplay() even if the |
| 150 | * display is no longer visible. |
| 151 | */ |
| 152 | // TODO: We need to know if/when our client dies so we can get the buffer back! (blocked b/31632518) |
| 153 | Return<void> EvsDisplay::getTargetBuffer(getTargetBuffer_cb _hidl_cb) { |
| 154 | ALOGD("getTargetBuffer"); |
| 155 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 156 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 157 | if (mRequestedState == DisplayState::DEAD) { |
| 158 | ALOGE("Rejecting buffer request from object that lost ownership of the display."); |
| 159 | BufferDesc nullBuff = {}; |
| 160 | _hidl_cb(nullBuff); |
| 161 | return Void(); |
| 162 | } |
| 163 | |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 164 | // If we don't already have a buffer, allocate one now |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 165 | if (!mBuffer.memHandle) { |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 166 | // Allocate the buffer that will hold our displayable image |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 167 | buffer_handle_t handle = nullptr; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 168 | GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 169 | status_t result = alloc.allocate(mBuffer.width, mBuffer.height, |
| 170 | mBuffer.format, 1, mBuffer.usage, |
Craig Donner | 7babeec | 2017-02-06 10:00:22 -0800 | [diff] [blame] | 171 | mBuffer.usage, &handle, |
| 172 | &mBuffer.stride, |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 173 | 0, "EvsDisplay"); |
| 174 | if (result != NO_ERROR) { |
| 175 | ALOGE("Error %d allocating %d x %d graphics buffer", |
| 176 | result, mBuffer.width, mBuffer.height); |
| 177 | BufferDesc nullBuff = {}; |
| 178 | _hidl_cb(nullBuff); |
| 179 | return Void(); |
| 180 | } |
| 181 | if (!handle) { |
| 182 | ALOGE("We didn't get a buffer handle back from the allocator"); |
| 183 | BufferDesc nullBuff = {}; |
| 184 | _hidl_cb(nullBuff); |
| 185 | return Void(); |
| 186 | } |
| 187 | |
| 188 | mBuffer.memHandle = handle; |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 189 | mFrameBusy = false; |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 190 | ALOGD("Allocated new buffer %p with stride %u", |
Scott Randolph | 9a773c7 | 2017-02-15 16:25:48 -0800 | [diff] [blame] | 191 | mBuffer.memHandle.getNativeHandle(), mBuffer.stride); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Do we have a frame available? |
| 195 | if (mFrameBusy) { |
| 196 | // This means either we have a 2nd client trying to compete for buffers |
| 197 | // (an unsupported mode of operation) or else the client hasn't returned |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 198 | // a previously issued buffer yet (they're behaving badly). |
| 199 | // NOTE: We have to make the callback even if we have nothing to provide |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 200 | ALOGE("getTargetBuffer called while no buffers available."); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 201 | BufferDesc nullBuff = {}; |
| 202 | _hidl_cb(nullBuff); |
| 203 | return Void(); |
| 204 | } else { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 205 | // Mark our buffer as busy |
| 206 | mFrameBusy = true; |
| 207 | |
| 208 | // Send the buffer to the client |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 209 | ALOGD("Providing display buffer handle %p as id %d", |
| 210 | mBuffer.memHandle.getNativeHandle(), mBuffer.bufferId); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 211 | _hidl_cb(mBuffer); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 212 | return Void(); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 213 | } |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
| 217 | /** |
| 218 | * This call tells the display that the buffer is ready for display. |
| 219 | * The buffer is no longer valid for use by the client after this call. |
| 220 | */ |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 221 | Return<EvsResult> EvsDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer) { |
| 222 | ALOGD("returnTargetBufferForDisplay %p", buffer.memHandle.getNativeHandle()); |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 223 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 224 | |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 225 | // Nobody should call us with a null handle |
| 226 | if (!buffer.memHandle.getNativeHandle()) { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 227 | ALOGE ("returnTargetBufferForDisplay called without a valid buffer handle.\n"); |
| 228 | return EvsResult::INVALID_ARG; |
| 229 | } |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 230 | if (buffer.bufferId != mBuffer.bufferId) { |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 231 | ALOGE ("Got an unrecognized frame returned.\n"); |
| 232 | return EvsResult::INVALID_ARG; |
| 233 | } |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 234 | if (!mFrameBusy) { |
| 235 | ALOGE ("A frame was returned with no outstanding frames.\n"); |
| 236 | return EvsResult::BUFFER_NOT_AVAILABLE; |
| 237 | } |
| 238 | |
| 239 | mFrameBusy = false; |
| 240 | |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 241 | // If we've been displaced by another owner of the display, then we can't do anything else |
| 242 | if (mRequestedState == DisplayState::DEAD) { |
| 243 | return EvsResult::OWNERSHIP_LOST; |
| 244 | } |
| 245 | |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 246 | // If we were waiting for a new frame, this is it! |
| 247 | if (mRequestedState == DisplayState::VISIBLE_ON_NEXT_FRAME) { |
| 248 | mRequestedState = DisplayState::VISIBLE; |
| 249 | } |
| 250 | |
| 251 | // Validate we're in an expected state |
| 252 | if (mRequestedState != DisplayState::VISIBLE) { |
| 253 | // We shouldn't get frames back when we're not visible. |
| 254 | ALOGE ("Got an unexpected frame returned while not visible - ignoring.\n"); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 255 | } else { |
| 256 | // This is where the buffer would be made visible. |
| 257 | // For now we simply validate it has the data we expect in it by reading it back |
| 258 | |
| 259 | // Lock our display buffer for reading |
| 260 | uint32_t* pixels = nullptr; |
| 261 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 262 | mapper.lock(mBuffer.memHandle, |
| 263 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_NEVER, |
| 264 | android::Rect(mBuffer.width, mBuffer.height), |
| 265 | (void **)&pixels); |
| 266 | |
| 267 | // If we failed to lock the pixel buffer, we're about to crash, but log it first |
| 268 | if (!pixels) { |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 269 | ALOGE("Display failed to gain access to image buffer for reading"); |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Check the test pixels |
| 273 | bool frameLooksGood = true; |
Scott Randolph | de9880e | 2017-03-30 14:04:12 -0700 | [diff] [blame] | 274 | for (unsigned row = 0; row < mBuffer.height; row++) { |
| 275 | for (unsigned col = 0; col < mBuffer.width; col++) { |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 276 | // Index into the row to check the pixel at this column. |
| 277 | // We expect 0xFF in the LSB channel, a vertical gradient in the |
| 278 | // second channel, a horitzontal gradient in the third channel, and |
| 279 | // 0xFF in the MSB. |
| 280 | // The exception is the very first 32 bits which is used for the |
| 281 | // time varying frame signature to avoid getting fooled by a static image. |
| 282 | uint32_t expectedPixel = 0xFF0000FF | // MSB and LSB |
| 283 | ((row & 0xFF) << 8) | // vertical gradient |
| 284 | ((col & 0xFF) << 16); // horizontal gradient |
| 285 | if ((row | col) == 0) { |
| 286 | // we'll check the "uniqueness" of the frame signature below |
| 287 | continue; |
| 288 | } |
| 289 | // Walk across this row (we'll step rows below) |
Scott Randolph | 9a773c7 | 2017-02-15 16:25:48 -0800 | [diff] [blame] | 290 | uint32_t receivedPixel = pixels[col]; |
| 291 | if (receivedPixel != expectedPixel) { |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 292 | ALOGE("Pixel check mismatch in frame buffer"); |
| 293 | frameLooksGood = false; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (!frameLooksGood) { |
| 299 | break; |
| 300 | } |
| 301 | |
Scott Randolph | 9a773c7 | 2017-02-15 16:25:48 -0800 | [diff] [blame] | 302 | // Point to the next row (NOTE: gralloc reports stride in units of pixels) |
| 303 | pixels = pixels + mBuffer.stride; |
Scott Randolph | db5a598 | 2017-01-23 12:35:05 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Ensure we don't see the same buffer twice without it being rewritten |
| 307 | static uint32_t prevSignature = ~0; |
| 308 | uint32_t signature = pixels[0] & 0xFF; |
| 309 | if (prevSignature == signature) { |
| 310 | frameLooksGood = false; |
| 311 | ALOGE("Duplicate, likely stale frame buffer detected"); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | // Release our output buffer |
| 316 | mapper.unlock(mBuffer.memHandle); |
| 317 | |
| 318 | if (!frameLooksGood) { |
| 319 | return EvsResult::UNDERLYING_SERVICE_ERROR; |
| 320 | } |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return EvsResult::OK; |
| 324 | } |
| 325 | |
| 326 | } // namespace implementation |
| 327 | } // namespace V1_0 |
| 328 | } // namespace evs |
Scott Randolph | 8342279 | 2017-03-01 20:32:59 -0800 | [diff] [blame] | 329 | } // namespace automotive |
Scott Randolph | 5c99d85 | 2016-11-15 17:01:23 -0800 | [diff] [blame] | 330 | } // namespace hardware |
| 331 | } // namespace android |