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 #include "EvsGlDisplay.h"
18 
19 #include <ui/GraphicBufferAllocator.h>
20 #include <ui/GraphicBufferMapper.h>
21 #include <utils/SystemClock.h>
22 
23 
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace evs {
28 namespace V1_0 {
29 namespace implementation {
30 
31 static bool sDebugFirstFrameDisplayed = false;
32 
EvsGlDisplay()33 EvsGlDisplay::EvsGlDisplay() {
34     ALOGD("EvsGlDisplay 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 
42 
~EvsGlDisplay()43 EvsGlDisplay::~EvsGlDisplay() {
44     ALOGD("EvsGlDisplay being destroyed");
45 	forceShutdown();
46 }
47 
48 
49 /**
50  * This gets called if another caller "steals" ownership of the display
51  */
forceShutdown()52 void EvsGlDisplay::forceShutdown()
53 {
54     ALOGD("EvsGlDisplay forceShutdown");
55     std::lock_guard<std::mutex> lock(mAccessLock);
56 
57     // If the buffer isn't being held by a remote client, release it now as an
58     // optimization to release the resources more quickly than the destructor might
59     // get called.
60     if (mBuffer.memHandle) {
61         // Report if we're going away while a buffer is outstanding
62         if (mFrameBusy) {
63             ALOGE("EvsGlDisplay going down while client is holding a buffer");
64         }
65 
66         // Drop the graphics buffer we've been using
67         GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
68         alloc.free(mBuffer.memHandle);
69         mBuffer.memHandle = nullptr;
70 
71         mGlWrapper.shutdown();
72     }
73 
74     // Put this object into an unrecoverable error state since somebody else
75     // is going to own the display now.
76     mRequestedState = DisplayState::DEAD;
77 }
78 
79 
80 /**
81  * Returns basic information about the EVS display provided by the system.
82  * See the description of the DisplayDesc structure for details.
83  */
getDisplayInfo(getDisplayInfo_cb _hidl_cb)84 Return<void> EvsGlDisplay::getDisplayInfo(getDisplayInfo_cb _hidl_cb)  {
85     ALOGD("getDisplayInfo");
86 
87     // Send back our self description
88     _hidl_cb(mInfo);
89     return Void();
90 }
91 
92 
93 /**
94  * Clients may set the display state to express their desired state.
95  * The HAL implementation must gracefully accept a request for any state
96  * while in any other state, although the response may be to ignore the request.
97  * The display is defined to start in the NOT_VISIBLE state upon initialization.
98  * The client is then expected to request the VISIBLE_ON_NEXT_FRAME state, and
99  * then begin providing video.  When the display is no longer required, the client
100  * is expected to request the NOT_VISIBLE state after passing the last video frame.
101  */
setDisplayState(DisplayState state)102 Return<EvsResult> EvsGlDisplay::setDisplayState(DisplayState state) {
103     ALOGD("setDisplayState");
104     std::lock_guard<std::mutex> lock(mAccessLock);
105 
106     if (mRequestedState == DisplayState::DEAD) {
107         // This object no longer owns the display -- it's been superceeded!
108         return EvsResult::OWNERSHIP_LOST;
109     }
110 
111     // Ensure we recognize the requested state so we don't go off the rails
112     if (state >= DisplayState::NUM_STATES) {
113         return EvsResult::INVALID_ARG;
114     }
115 
116     switch (state) {
117     case DisplayState::NOT_VISIBLE:
118         mGlWrapper.hideWindow();
119         break;
120     case DisplayState::VISIBLE:
121         mGlWrapper.showWindow();
122         break;
123     default:
124         break;
125     }
126 
127     // Record the requested state
128     mRequestedState = state;
129 
130     return EvsResult::OK;
131 }
132 
133 
134 /**
135  * The HAL implementation should report the actual current state, which might
136  * transiently differ from the most recently requested state.  Note, however, that
137  * the logic responsible for changing display states should generally live above
138  * the device layer, making it undesirable for the HAL implementation to
139  * spontaneously change display states.
140  */
getDisplayState()141 Return<DisplayState> EvsGlDisplay::getDisplayState()  {
142     ALOGD("getDisplayState");
143     std::lock_guard<std::mutex> lock(mAccessLock);
144 
145     return mRequestedState;
146 }
147 
148 
149 /**
150  * This call returns a handle to a frame buffer associated with the display.
151  * This buffer may be locked and written to by software and/or GL.  This buffer
152  * must be returned via a call to returnTargetBufferForDisplay() even if the
153  * display is no longer visible.
154  */
getTargetBuffer(getTargetBuffer_cb _hidl_cb)155 Return<void> EvsGlDisplay::getTargetBuffer(getTargetBuffer_cb _hidl_cb)  {
156     ALOGV("getTargetBuffer");
157     std::lock_guard<std::mutex> lock(mAccessLock);
158 
159     if (mRequestedState == DisplayState::DEAD) {
160         ALOGE("Rejecting buffer request from object that lost ownership of the display.");
161         BufferDesc nullBuff = {};
162         _hidl_cb(nullBuff);
163         return Void();
164     }
165 
166     // If we don't already have a buffer, allocate one now
167     if (!mBuffer.memHandle) {
168         // Initialize our display window
169         // NOTE:  This will cause the display to become "VISIBLE" before a frame is actually
170         // returned, which is contrary to the spec and will likely result in a black frame being
171         // (briefly) shown.
172         if (!mGlWrapper.initialize()) {
173             // Report the failure
174             ALOGE("Failed to initialize GL display");
175             BufferDesc nullBuff = {};
176             _hidl_cb(nullBuff);
177             return Void();
178         }
179 
180         // Assemble the buffer description we'll use for our render target
181         mBuffer.width       = mGlWrapper.getWidth();
182         mBuffer.height      = mGlWrapper.getHeight();
183         mBuffer.format      = HAL_PIXEL_FORMAT_RGBA_8888;
184         mBuffer.usage       = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER;
185         mBuffer.bufferId    = 0x3870;  // Arbitrary magic number for self recognition
186         mBuffer.pixelSize   = 4;
187 
188         // Allocate the buffer that will hold our displayable image
189         buffer_handle_t handle = nullptr;
190         GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
191         status_t result = alloc.allocate(mBuffer.width, mBuffer.height,
192                                          mBuffer.format, 1,
193                                          mBuffer.usage, &handle,
194                                          &mBuffer.stride,
195                                          0, "EvsGlDisplay");
196         if (result != NO_ERROR) {
197             ALOGE("Error %d allocating %d x %d graphics buffer",
198                   result, mBuffer.width, mBuffer.height);
199             BufferDesc nullBuff = {};
200             _hidl_cb(nullBuff);
201             mGlWrapper.shutdown();
202             return Void();
203         }
204         if (!handle) {
205             ALOGE("We didn't get a buffer handle back from the allocator");
206             BufferDesc nullBuff = {};
207             _hidl_cb(nullBuff);
208             mGlWrapper.shutdown();
209             return Void();
210         }
211 
212         mBuffer.memHandle = handle;
213         ALOGD("Allocated new buffer %p with stride %u",
214               mBuffer.memHandle.getNativeHandle(), mBuffer.stride);
215         mFrameBusy = false;
216     }
217 
218     // Do we have a frame available?
219     if (mFrameBusy) {
220         // This means either we have a 2nd client trying to compete for buffers
221         // (an unsupported mode of operation) or else the client hasn't returned
222         // a previously issued buffer yet (they're behaving badly).
223         // NOTE:  We have to make the callback even if we have nothing to provide
224         ALOGE("getTargetBuffer called while no buffers available.");
225         BufferDesc nullBuff = {};
226         _hidl_cb(nullBuff);
227         return Void();
228     } else {
229         // Mark our buffer as busy
230         mFrameBusy = true;
231 
232         // Send the buffer to the client
233         ALOGV("Providing display buffer handle %p as id %d",
234               mBuffer.memHandle.getNativeHandle(), mBuffer.bufferId);
235         _hidl_cb(mBuffer);
236         return Void();
237     }
238 }
239 
240 
241 /**
242  * This call tells the display that the buffer is ready for display.
243  * The buffer is no longer valid for use by the client after this call.
244  */
returnTargetBufferForDisplay(const BufferDesc & buffer)245 Return<EvsResult> EvsGlDisplay::returnTargetBufferForDisplay(const BufferDesc& buffer)  {
246     ALOGV("returnTargetBufferForDisplay %p", buffer.memHandle.getNativeHandle());
247     std::lock_guard<std::mutex> lock(mAccessLock);
248 
249     // Nobody should call us with a null handle
250     if (!buffer.memHandle.getNativeHandle()) {
251         ALOGE ("returnTargetBufferForDisplay called without a valid buffer handle.\n");
252         return EvsResult::INVALID_ARG;
253     }
254     if (buffer.bufferId != mBuffer.bufferId) {
255         ALOGE ("Got an unrecognized frame returned.\n");
256         return EvsResult::INVALID_ARG;
257     }
258     if (!mFrameBusy) {
259         ALOGE ("A frame was returned with no outstanding frames.\n");
260         return EvsResult::BUFFER_NOT_AVAILABLE;
261     }
262 
263     mFrameBusy = false;
264 
265     // If we've been displaced by another owner of the display, then we can't do anything else
266     if (mRequestedState == DisplayState::DEAD) {
267         return EvsResult::OWNERSHIP_LOST;
268     }
269 
270     // If we were waiting for a new frame, this is it!
271     if (mRequestedState == DisplayState::VISIBLE_ON_NEXT_FRAME) {
272         mRequestedState = DisplayState::VISIBLE;
273         mGlWrapper.showWindow();
274     }
275 
276     // Validate we're in an expected state
277     if (mRequestedState != DisplayState::VISIBLE) {
278         // Not sure why a client would send frames back when we're not visible.
279         ALOGW ("Got a frame returned while not visible - ignoring.\n");
280     } else {
281         // Update the texture contents with the provided data
282 // TODO:  Why doesn't it work to pass in the buffer handle we got from HIDL?
283 //        if (!mGlWrapper.updateImageTexture(buffer)) {
284         if (!mGlWrapper.updateImageTexture(mBuffer)) {
285             return EvsResult::UNDERLYING_SERVICE_ERROR;
286         }
287 
288         // Put the image on the screen
289         mGlWrapper.renderImageToScreen();
290         if (!sDebugFirstFrameDisplayed) {
291             ALOGD("EvsFirstFrameDisplayTiming start time: %" PRId64 "ms", elapsedRealtime());
292             sDebugFirstFrameDisplayed = true;
293         }
294 
295     }
296 
297     return EvsResult::OK;
298 }
299 
300 } // namespace implementation
301 } // namespace V1_0
302 } // namespace evs
303 } // namespace automotive
304 } // namespace hardware
305 } // namespace android
306