blob: 9dd546d5350808b2b3e0db96391740b1968918cc [file] [log] [blame]
Scott Randolph5c99d852016-11-15 17:01:23 -08001/*
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 Randolph83422792017-03-01 20:32:59 -080017#define LOG_TAG "android.hardware.automotive.evs@1.0-service"
Scott Randolph5c99d852016-11-15 17:01:23 -080018
19#include "EvsDisplay.h"
20
21#include <ui/GraphicBufferAllocator.h>
22#include <ui/GraphicBufferMapper.h>
23
24
25namespace android {
26namespace hardware {
Scott Randolph83422792017-03-01 20:32:59 -080027namespace automotive {
Scott Randolph5c99d852016-11-15 17:01:23 -080028namespace evs {
29namespace V1_0 {
30namespace implementation {
31
32
Scott Randolph5c99d852016-11-15 17:01:23 -080033EvsDisplay::EvsDisplay() {
34 ALOGD("EvsDisplay instantiated");
35
36 // Set up our self description
Scott Randolphdb5a5982017-01-23 12:35:05 -080037 // NOTE: These are arbitrary values chosen for testing
Scott Randolph5c99d852016-11-15 17:01:23 -080038 mInfo.displayId = "Mock Display";
39 mInfo.vendorFlags = 3870;
Scott Randolphde9880e2017-03-30 14:04:12 -070040
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 Randolph5c99d852016-11-15 17:01:23 -080048}
49
50
51EvsDisplay::~EvsDisplay() {
52 ALOGD("EvsDisplay being destroyed");
Scott Randolphde9880e2017-03-30 14:04:12 -070053 forceShutdown();
54}
55
56
57/**
58 * This gets called if another caller "steals" ownership of the display
59 */
60void EvsDisplay::forceShutdown()
61{
62 ALOGD("EvsDisplay forceShutdown");
Scott Randolph5c99d852016-11-15 17:01:23 -080063 std::lock_guard<std::mutex> lock(mAccessLock);
64
Scott Randolphde9880e2017-03-30 14:04:12 -070065 // 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 Randolphdb5a5982017-01-23 12:35:05 -080068 if (mBuffer.memHandle) {
Scott Randolphde9880e2017-03-30 14:04:12 -070069 // 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 Randolph5c99d852016-11-15 17:01:23 -080074 // Drop the graphics buffer we've been using
75 GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Scott Randolphdb5a5982017-01-23 12:35:05 -080076 alloc.free(mBuffer.memHandle);
77 mBuffer.memHandle = nullptr;
Scott Randolph5c99d852016-11-15 17:01:23 -080078 }
Scott Randolphde9880e2017-03-30 14:04:12 -070079
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 Randolph5c99d852016-11-15 17:01:23 -080083}
84
85
86/**
87 * Returns basic information about the EVS display provided by the system.
Scott Randolphde9880e2017-03-30 14:04:12 -070088 * See the description of the DisplayDesc structure for details.
Scott Randolph5c99d852016-11-15 17:01:23 -080089 */
90Return<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 */
108Return<EvsResult> EvsDisplay::setDisplayState(DisplayState state) {
109 ALOGD("setDisplayState");
110 std::lock_guard<std::mutex> lock(mAccessLock);
111
Scott Randolphde9880e2017-03-30 14:04:12 -0700112 if (mRequestedState == DisplayState::DEAD) {
113 // This object no longer owns the display -- it's been superceeded!
114 return EvsResult::OWNERSHIP_LOST;
115 }
116
Scott Randolph5c99d852016-11-15 17:01:23 -0800117 // 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 */
138Return<DisplayState> EvsDisplay::getDisplayState() {
139 ALOGD("getDisplayState");
140 std::lock_guard<std::mutex> lock(mAccessLock);
141
Scott Randolphde9880e2017-03-30 14:04:12 -0700142 return mRequestedState;
Scott Randolph5c99d852016-11-15 17:01:23 -0800143}
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)
153Return<void> EvsDisplay::getTargetBuffer(getTargetBuffer_cb _hidl_cb) {
154 ALOGD("getTargetBuffer");
155 std::lock_guard<std::mutex> lock(mAccessLock);
156
Scott Randolphde9880e2017-03-30 14:04:12 -0700157 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 Randolph5c99d852016-11-15 17:01:23 -0800164 // If we don't already have a buffer, allocate one now
Scott Randolphdb5a5982017-01-23 12:35:05 -0800165 if (!mBuffer.memHandle) {
Scott Randolphde9880e2017-03-30 14:04:12 -0700166 // Allocate the buffer that will hold our displayable image
Scott Randolphdb5a5982017-01-23 12:35:05 -0800167 buffer_handle_t handle = nullptr;
Scott Randolph5c99d852016-11-15 17:01:23 -0800168 GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Chris Forbesf90642b2017-04-19 16:09:51 -0700169 status_t result = alloc.allocate(
170 mBuffer.width, mBuffer.height, mBuffer.format, 1, mBuffer.usage,
171 &handle, &mBuffer.stride, 0, "EvsDisplay");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800172 if (result != NO_ERROR) {
173 ALOGE("Error %d allocating %d x %d graphics buffer",
174 result, mBuffer.width, mBuffer.height);
175 BufferDesc nullBuff = {};
176 _hidl_cb(nullBuff);
177 return Void();
178 }
179 if (!handle) {
180 ALOGE("We didn't get a buffer handle back from the allocator");
181 BufferDesc nullBuff = {};
182 _hidl_cb(nullBuff);
183 return Void();
184 }
185
186 mBuffer.memHandle = handle;
Scott Randolph5c99d852016-11-15 17:01:23 -0800187 mFrameBusy = false;
Scott Randolphdb5a5982017-01-23 12:35:05 -0800188 ALOGD("Allocated new buffer %p with stride %u",
Scott Randolph9a773c72017-02-15 16:25:48 -0800189 mBuffer.memHandle.getNativeHandle(), mBuffer.stride);
Scott Randolph5c99d852016-11-15 17:01:23 -0800190 }
191
192 // Do we have a frame available?
193 if (mFrameBusy) {
194 // This means either we have a 2nd client trying to compete for buffers
195 // (an unsupported mode of operation) or else the client hasn't returned
Scott Randolphdb5a5982017-01-23 12:35:05 -0800196 // a previously issued buffer yet (they're behaving badly).
197 // NOTE: We have to make the callback even if we have nothing to provide
Scott Randolph5c99d852016-11-15 17:01:23 -0800198 ALOGE("getTargetBuffer called while no buffers available.");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800199 BufferDesc nullBuff = {};
200 _hidl_cb(nullBuff);
201 return Void();
202 } else {
Scott Randolph5c99d852016-11-15 17:01:23 -0800203 // Mark our buffer as busy
204 mFrameBusy = true;
205
206 // Send the buffer to the client
Scott Randolphdb5a5982017-01-23 12:35:05 -0800207 ALOGD("Providing display buffer handle %p as id %d",
208 mBuffer.memHandle.getNativeHandle(), mBuffer.bufferId);
Scott Randolph5c99d852016-11-15 17:01:23 -0800209 _hidl_cb(mBuffer);
Scott Randolphdb5a5982017-01-23 12:35:05 -0800210 return Void();
Scott Randolph5c99d852016-11-15 17:01:23 -0800211 }
Scott Randolph5c99d852016-11-15 17:01:23 -0800212}
213
214
215/**
216 * This call tells the display that the buffer is ready for display.
217 * The buffer is no longer valid for use by the client after this call.
218 */
Scott Randolphdb5a5982017-01-23 12:35:05 -0800219Return<EvsResult> EvsDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer) {
220 ALOGD("returnTargetBufferForDisplay %p", buffer.memHandle.getNativeHandle());
Scott Randolph5c99d852016-11-15 17:01:23 -0800221 std::lock_guard<std::mutex> lock(mAccessLock);
222
Scott Randolphdb5a5982017-01-23 12:35:05 -0800223 // Nobody should call us with a null handle
224 if (!buffer.memHandle.getNativeHandle()) {
Scott Randolph5c99d852016-11-15 17:01:23 -0800225 ALOGE ("returnTargetBufferForDisplay called without a valid buffer handle.\n");
226 return EvsResult::INVALID_ARG;
227 }
Scott Randolphdb5a5982017-01-23 12:35:05 -0800228 if (buffer.bufferId != mBuffer.bufferId) {
Scott Randolph5c99d852016-11-15 17:01:23 -0800229 ALOGE ("Got an unrecognized frame returned.\n");
230 return EvsResult::INVALID_ARG;
231 }
Scott Randolph5c99d852016-11-15 17:01:23 -0800232 if (!mFrameBusy) {
233 ALOGE ("A frame was returned with no outstanding frames.\n");
234 return EvsResult::BUFFER_NOT_AVAILABLE;
235 }
236
237 mFrameBusy = false;
238
Scott Randolphde9880e2017-03-30 14:04:12 -0700239 // If we've been displaced by another owner of the display, then we can't do anything else
240 if (mRequestedState == DisplayState::DEAD) {
241 return EvsResult::OWNERSHIP_LOST;
242 }
243
Scott Randolph5c99d852016-11-15 17:01:23 -0800244 // If we were waiting for a new frame, this is it!
245 if (mRequestedState == DisplayState::VISIBLE_ON_NEXT_FRAME) {
246 mRequestedState = DisplayState::VISIBLE;
247 }
248
249 // Validate we're in an expected state
250 if (mRequestedState != DisplayState::VISIBLE) {
251 // We shouldn't get frames back when we're not visible.
252 ALOGE ("Got an unexpected frame returned while not visible - ignoring.\n");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800253 } else {
254 // This is where the buffer would be made visible.
255 // For now we simply validate it has the data we expect in it by reading it back
256
257 // Lock our display buffer for reading
258 uint32_t* pixels = nullptr;
259 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
260 mapper.lock(mBuffer.memHandle,
261 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_NEVER,
262 android::Rect(mBuffer.width, mBuffer.height),
263 (void **)&pixels);
264
265 // If we failed to lock the pixel buffer, we're about to crash, but log it first
266 if (!pixels) {
Scott Randolph83422792017-03-01 20:32:59 -0800267 ALOGE("Display failed to gain access to image buffer for reading");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800268 }
269
270 // Check the test pixels
271 bool frameLooksGood = true;
Scott Randolphde9880e2017-03-30 14:04:12 -0700272 for (unsigned row = 0; row < mBuffer.height; row++) {
273 for (unsigned col = 0; col < mBuffer.width; col++) {
Scott Randolphdb5a5982017-01-23 12:35:05 -0800274 // Index into the row to check the pixel at this column.
275 // We expect 0xFF in the LSB channel, a vertical gradient in the
276 // second channel, a horitzontal gradient in the third channel, and
277 // 0xFF in the MSB.
278 // The exception is the very first 32 bits which is used for the
279 // time varying frame signature to avoid getting fooled by a static image.
280 uint32_t expectedPixel = 0xFF0000FF | // MSB and LSB
281 ((row & 0xFF) << 8) | // vertical gradient
282 ((col & 0xFF) << 16); // horizontal gradient
283 if ((row | col) == 0) {
284 // we'll check the "uniqueness" of the frame signature below
285 continue;
286 }
287 // Walk across this row (we'll step rows below)
Scott Randolph9a773c72017-02-15 16:25:48 -0800288 uint32_t receivedPixel = pixels[col];
289 if (receivedPixel != expectedPixel) {
Scott Randolphdb5a5982017-01-23 12:35:05 -0800290 ALOGE("Pixel check mismatch in frame buffer");
291 frameLooksGood = false;
292 break;
293 }
294 }
295
296 if (!frameLooksGood) {
297 break;
298 }
299
Scott Randolph9a773c72017-02-15 16:25:48 -0800300 // Point to the next row (NOTE: gralloc reports stride in units of pixels)
301 pixels = pixels + mBuffer.stride;
Scott Randolphdb5a5982017-01-23 12:35:05 -0800302 }
303
304 // Ensure we don't see the same buffer twice without it being rewritten
305 static uint32_t prevSignature = ~0;
306 uint32_t signature = pixels[0] & 0xFF;
307 if (prevSignature == signature) {
308 frameLooksGood = false;
309 ALOGE("Duplicate, likely stale frame buffer detected");
310 }
311
312
313 // Release our output buffer
314 mapper.unlock(mBuffer.memHandle);
315
316 if (!frameLooksGood) {
317 return EvsResult::UNDERLYING_SERVICE_ERROR;
318 }
Scott Randolph5c99d852016-11-15 17:01:23 -0800319 }
320
321 return EvsResult::OK;
322}
323
324} // namespace implementation
325} // namespace V1_0
326} // namespace evs
Scott Randolph83422792017-03-01 20:32:59 -0800327} // namespace automotive
Scott Randolph5c99d852016-11-15 17:01:23 -0800328} // namespace hardware
329} // namespace android