1 /*
2  * Copyright 2022 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 "BufferProducerThread.h"
18 
19 namespace android {
20 
BufferProducerThread(tv_input_device_t * device,int deviceId,const tv_stream_t * stream)21 BufferProducerThread::BufferProducerThread(tv_input_device_t* device, int deviceId,
22                                            const tv_stream_t* stream)
23       : Thread(false),
24         mDevice(device),
25         mDeviceId(deviceId),
26         mBuffer(NULL),
27         mBufferState(RELEASED),
28         mSeq(0u),
29         mShutdown(false) {
30     memcpy(&mStream, stream, sizeof(mStream));
31 }
32 
readyToRun()33 status_t BufferProducerThread::readyToRun() {
34     sp<ANativeWindow> anw(mSurface);
35     status_t err = native_window_set_usage(anw.get(), mStream.buffer_producer.usage);
36     if (err != NO_ERROR) {
37         return err;
38     }
39     err = native_window_set_buffers_dimensions(anw.get(), mStream.buffer_producer.width,
40                                                mStream.buffer_producer.height);
41     if (err != NO_ERROR) {
42         return err;
43     }
44     err = native_window_set_buffers_format(anw.get(), mStream.buffer_producer.format);
45     if (err != NO_ERROR) {
46         return err;
47     }
48     return NO_ERROR;
49 }
50 
setSurface(const sp<Surface> & surface)51 void BufferProducerThread::setSurface(const sp<Surface>& surface) {
52     Mutex::Autolock autoLock(&mLock);
53     setSurfaceLocked(surface);
54 }
55 
setSurfaceLocked(const sp<Surface> & surface)56 void BufferProducerThread::setSurfaceLocked(const sp<Surface>& surface) {
57     if (surface == mSurface) {
58         return;
59     }
60 
61     if (mBufferState == CAPTURING) {
62         mDevice->cancel_capture(mDevice, mDeviceId, mStream.stream_id, mSeq);
63     }
64     while (mBufferState == CAPTURING) {
65         status_t err = mCondition.waitRelative(mLock, s2ns(1));
66         if (err != NO_ERROR) {
67             ALOGE("error %d while wating for buffer state to change.", err);
68             break;
69         }
70     }
71     mBuffer.clear();
72     mBufferState = RELEASED;
73 
74     mSurface = surface;
75     mCondition.broadcast();
76 }
77 
onCaptured(uint32_t seq,bool succeeded)78 void BufferProducerThread::onCaptured(uint32_t seq, bool succeeded) {
79     Mutex::Autolock autoLock(&mLock);
80     if (seq != mSeq) {
81         ALOGW("Incorrect sequence value: expected %u actual %u", mSeq, seq);
82     }
83     if (mBufferState != CAPTURING) {
84         ALOGW("mBufferState != CAPTURING : instead %d", mBufferState);
85     }
86     if (succeeded) {
87         mBufferState = CAPTURED;
88     } else {
89         mBuffer.clear();
90         mBufferState = RELEASED;
91     }
92     mCondition.broadcast();
93 }
94 
shutdown()95 void BufferProducerThread::shutdown() {
96     Mutex::Autolock autoLock(&mLock);
97     mShutdown = true;
98     setSurfaceLocked(NULL);
99     requestExitAndWait();
100 }
101 
threadLoop()102 bool BufferProducerThread::threadLoop() {
103     Mutex::Autolock autoLock(&mLock);
104 
105     status_t err = NO_ERROR;
106     if (mSurface == NULL) {
107         err = mCondition.waitRelative(mLock, s2ns(1));
108         // It's OK to time out here.
109         if (err != NO_ERROR && err != TIMED_OUT) {
110             ALOGE("error %d while wating for non-null surface to be set", err);
111             return false;
112         }
113         return true;
114     }
115     sp<ANativeWindow> anw(mSurface);
116     while (mBufferState == CAPTURING) {
117         err = mCondition.waitRelative(mLock, s2ns(1));
118         if (err != NO_ERROR) {
119             ALOGE("error %d while wating for buffer state to change.", err);
120             return false;
121         }
122     }
123     if (mBufferState == CAPTURED && anw != NULL) {
124         err = anw->queueBuffer(anw.get(), mBuffer.get(), -1);
125         if (err != NO_ERROR) {
126             ALOGE("error %d while queueing buffer to surface", err);
127             return false;
128         }
129         mBuffer.clear();
130         mBufferState = RELEASED;
131     }
132     if (mBuffer == NULL && !mShutdown && anw != NULL) {
133         ANativeWindowBuffer_t* buffer = NULL;
134         err = native_window_dequeue_buffer_and_wait(anw.get(), &buffer);
135         if (err != NO_ERROR) {
136             ALOGE("error %d while dequeueing buffer to surface", err);
137             return false;
138         }
139         mBuffer = buffer;
140         mBufferState = CAPTURING;
141         mDevice->request_capture(mDevice, mDeviceId, mStream.stream_id, buffer->handle, ++mSeq);
142     }
143 
144     return true;
145 }
146 
147 } // namespace android
148