blob: 9ad332a49ef1f8f244d6de0f8b18b44d46aff434 [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());
Scott Randolphdb5a5982017-01-23 12:35:05 -0800169 status_t result = alloc.allocate(mBuffer.width, mBuffer.height,
170 mBuffer.format, 1, mBuffer.usage,
Craig Donner7babeec2017-02-06 10:00:22 -0800171 mBuffer.usage, &handle,
172 &mBuffer.stride,
Scott Randolphdb5a5982017-01-23 12:35:05 -0800173 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 Randolph5c99d852016-11-15 17:01:23 -0800189 mFrameBusy = false;
Scott Randolphdb5a5982017-01-23 12:35:05 -0800190 ALOGD("Allocated new buffer %p with stride %u",
Scott Randolph9a773c72017-02-15 16:25:48 -0800191 mBuffer.memHandle.getNativeHandle(), mBuffer.stride);
Scott Randolph5c99d852016-11-15 17:01:23 -0800192 }
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 Randolphdb5a5982017-01-23 12:35:05 -0800198 // 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 Randolph5c99d852016-11-15 17:01:23 -0800200 ALOGE("getTargetBuffer called while no buffers available.");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800201 BufferDesc nullBuff = {};
202 _hidl_cb(nullBuff);
203 return Void();
204 } else {
Scott Randolph5c99d852016-11-15 17:01:23 -0800205 // Mark our buffer as busy
206 mFrameBusy = true;
207
208 // Send the buffer to the client
Scott Randolphdb5a5982017-01-23 12:35:05 -0800209 ALOGD("Providing display buffer handle %p as id %d",
210 mBuffer.memHandle.getNativeHandle(), mBuffer.bufferId);
Scott Randolph5c99d852016-11-15 17:01:23 -0800211 _hidl_cb(mBuffer);
Scott Randolphdb5a5982017-01-23 12:35:05 -0800212 return Void();
Scott Randolph5c99d852016-11-15 17:01:23 -0800213 }
Scott Randolph5c99d852016-11-15 17:01:23 -0800214}
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 Randolphdb5a5982017-01-23 12:35:05 -0800221Return<EvsResult> EvsDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer) {
222 ALOGD("returnTargetBufferForDisplay %p", buffer.memHandle.getNativeHandle());
Scott Randolph5c99d852016-11-15 17:01:23 -0800223 std::lock_guard<std::mutex> lock(mAccessLock);
224
Scott Randolphdb5a5982017-01-23 12:35:05 -0800225 // Nobody should call us with a null handle
226 if (!buffer.memHandle.getNativeHandle()) {
Scott Randolph5c99d852016-11-15 17:01:23 -0800227 ALOGE ("returnTargetBufferForDisplay called without a valid buffer handle.\n");
228 return EvsResult::INVALID_ARG;
229 }
Scott Randolphdb5a5982017-01-23 12:35:05 -0800230 if (buffer.bufferId != mBuffer.bufferId) {
Scott Randolph5c99d852016-11-15 17:01:23 -0800231 ALOGE ("Got an unrecognized frame returned.\n");
232 return EvsResult::INVALID_ARG;
233 }
Scott Randolph5c99d852016-11-15 17:01:23 -0800234 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 Randolphde9880e2017-03-30 14:04:12 -0700241 // 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 Randolph5c99d852016-11-15 17:01:23 -0800246 // 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 Randolphdb5a5982017-01-23 12:35:05 -0800255 } 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 Randolph83422792017-03-01 20:32:59 -0800269 ALOGE("Display failed to gain access to image buffer for reading");
Scott Randolphdb5a5982017-01-23 12:35:05 -0800270 }
271
272 // Check the test pixels
273 bool frameLooksGood = true;
Scott Randolphde9880e2017-03-30 14:04:12 -0700274 for (unsigned row = 0; row < mBuffer.height; row++) {
275 for (unsigned col = 0; col < mBuffer.width; col++) {
Scott Randolphdb5a5982017-01-23 12:35:05 -0800276 // 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 Randolph9a773c72017-02-15 16:25:48 -0800290 uint32_t receivedPixel = pixels[col];
291 if (receivedPixel != expectedPixel) {
Scott Randolphdb5a5982017-01-23 12:35:05 -0800292 ALOGE("Pixel check mismatch in frame buffer");
293 frameLooksGood = false;
294 break;
295 }
296 }
297
298 if (!frameLooksGood) {
299 break;
300 }
301
Scott Randolph9a773c72017-02-15 16:25:48 -0800302 // Point to the next row (NOTE: gralloc reports stride in units of pixels)
303 pixels = pixels + mBuffer.stride;
Scott Randolphdb5a5982017-01-23 12:35:05 -0800304 }
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 Randolph5c99d852016-11-15 17:01:23 -0800321 }
322
323 return EvsResult::OK;
324}
325
326} // namespace implementation
327} // namespace V1_0
328} // namespace evs
Scott Randolph83422792017-03-01 20:32:59 -0800329} // namespace automotive
Scott Randolph5c99d852016-11-15 17:01:23 -0800330} // namespace hardware
331} // namespace android