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 
17 #define LOG_TAG "android.hardware.automotive.evs@1.0-service"
18 
19 #include "EvsDisplay.h"
20 
21 #include <ui/GraphicBufferAllocator.h>
22 #include <ui/GraphicBufferMapper.h>
23 
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace evs {
29 namespace V1_0 {
30 namespace implementation {
31 
32 
EvsDisplay()33 EvsDisplay::EvsDisplay() {
34     ALOGD("EvsDisplay instantiated");
35 
36     // Set up our self description
37     // NOTE:  These are arbitrary values chosen for testing
38     mInfo.displayId             = "Mock Display";
39     mInfo.vendorFlags           = 3870;
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;
48 }
49 
50 
~EvsDisplay()51 EvsDisplay::~EvsDisplay() {
52     ALOGD("EvsDisplay being destroyed");
53     forceShutdown();
54 }
55 
56 
57 /**
58  * This gets called if another caller "steals" ownership of the display
59  */
forceShutdown()60 void EvsDisplay::forceShutdown()
61 {
62     ALOGD("EvsDisplay forceShutdown");
63     std::lock_guard<std::mutex> lock(mAccessLock);
64 
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.
68     if (mBuffer.memHandle) {
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 
74         // Drop the graphics buffer we've been using
75         GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
76         alloc.free(mBuffer.memHandle);
77         mBuffer.memHandle = nullptr;
78     }
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;
83 }
84 
85 
86 /**
87  * Returns basic information about the EVS display provided by the system.
88  * See the description of the DisplayDesc structure for details.
89  */
getDisplayInfo(getDisplayInfo_cb _hidl_cb)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  */
setDisplayState(DisplayState state)108 Return<EvsResult> EvsDisplay::setDisplayState(DisplayState state) {
109     ALOGD("setDisplayState");
110     std::lock_guard<std::mutex> lock(mAccessLock);
111 
112     if (mRequestedState == DisplayState::DEAD) {
113         // This object no longer owns the display -- it's been superceeded!
114         return EvsResult::OWNERSHIP_LOST;
115     }
116 
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  */
getDisplayState()138 Return<DisplayState> EvsDisplay::getDisplayState()  {
139     ALOGD("getDisplayState");
140     std::lock_guard<std::mutex> lock(mAccessLock);
141 
142     return mRequestedState;
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)
getTargetBuffer(getTargetBuffer_cb _hidl_cb)153 Return<void> EvsDisplay::getTargetBuffer(getTargetBuffer_cb _hidl_cb)  {
154     ALOGD("getTargetBuffer");
155     std::lock_guard<std::mutex> lock(mAccessLock);
156 
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 
164     // If we don't already have a buffer, allocate one now
165     if (!mBuffer.memHandle) {
166         // Allocate the buffer that will hold our displayable image
167         buffer_handle_t handle = nullptr;
168         GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
169         status_t result = alloc.allocate(
170             mBuffer.width, mBuffer.height, mBuffer.format, 1, mBuffer.usage,
171             &handle, &mBuffer.stride, 0, "EvsDisplay");
172         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;
187         mFrameBusy = false;
188         ALOGD("Allocated new buffer %p with stride %u",
189               mBuffer.memHandle.getNativeHandle(), mBuffer.stride);
190     }
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
196         // a previously issued buffer yet (they're behaving badly).
197         // NOTE:  We have to make the callback even if we have nothing to provide
198         ALOGE("getTargetBuffer called while no buffers available.");
199         BufferDesc nullBuff = {};
200         _hidl_cb(nullBuff);
201         return Void();
202     } else {
203         // Mark our buffer as busy
204         mFrameBusy = true;
205 
206         // Send the buffer to the client
207         ALOGD("Providing display buffer handle %p as id %d",
208               mBuffer.memHandle.getNativeHandle(), mBuffer.bufferId);
209         _hidl_cb(mBuffer);
210         return Void();
211     }
212 }
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  */
returnTargetBufferForDisplay(const BufferDesc & buffer)219 Return<EvsResult> EvsDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer)  {
220     ALOGD("returnTargetBufferForDisplay %p", buffer.memHandle.getNativeHandle());
221     std::lock_guard<std::mutex> lock(mAccessLock);
222 
223     // Nobody should call us with a null handle
224     if (!buffer.memHandle.getNativeHandle()) {
225         ALOGE ("returnTargetBufferForDisplay called without a valid buffer handle.\n");
226         return EvsResult::INVALID_ARG;
227     }
228     if (buffer.bufferId != mBuffer.bufferId) {
229         ALOGE ("Got an unrecognized frame returned.\n");
230         return EvsResult::INVALID_ARG;
231     }
232     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 
239     // 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 
244     // 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");
253     } 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) {
267             ALOGE("Display failed to gain access to image buffer for reading");
268         }
269 
270         // Check the test pixels
271         bool frameLooksGood = true;
272         for (unsigned row = 0; row < mBuffer.height; row++) {
273             for (unsigned col = 0; col < mBuffer.width; col++) {
274                 // 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)
288                 uint32_t receivedPixel = pixels[col];
289                 if (receivedPixel != expectedPixel) {
290                     ALOGE("Pixel check mismatch in frame buffer");
291                     frameLooksGood = false;
292                     break;
293                 }
294             }
295 
296             if (!frameLooksGood) {
297                 break;
298             }
299 
300             // Point to the next row (NOTE:  gralloc reports stride in units of pixels)
301             pixels = pixels + mBuffer.stride;
302         }
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         }
319     }
320 
321     return EvsResult::OK;
322 }
323 
324 } // namespace implementation
325 } // namespace V1_0
326 } // namespace evs
327 } // namespace automotive
328 } // namespace hardware
329 } // namespace android
330