Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | #include <utils/RefBase.h> |
| 22 | #include <utils/Vector.h> |
| 23 | #include <utils/Timers.h> |
| 24 | |
| 25 | #include <binder/Parcel.h> |
| 26 | #include <binder/IInterface.h> |
| 27 | |
| 28 | #include <gui/ISurfaceTexture.h> |
| 29 | |
| 30 | namespace android { |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | enum { |
| 34 | REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION, |
| 35 | SET_BUFFER_COUNT, |
| 36 | DEQUEUE_BUFFER, |
| 37 | QUEUE_BUFFER, |
| 38 | CANCEL_BUFFER, |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 39 | QUERY, |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 40 | SET_SYNCHRONOUS_MODE, |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 41 | CONNECT, |
| 42 | DISCONNECT, |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | |
| 46 | class BpSurfaceTexture : public BpInterface<ISurfaceTexture> |
| 47 | { |
| 48 | public: |
| 49 | BpSurfaceTexture(const sp<IBinder>& impl) |
| 50 | : BpInterface<ISurfaceTexture>(impl) |
| 51 | { |
| 52 | } |
| 53 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 54 | virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 55 | Parcel data, reply; |
| 56 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 57 | data.writeInt32(bufferIdx); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 58 | status_t result =remote()->transact(REQUEST_BUFFER, data, &reply); |
| 59 | if (result != NO_ERROR) { |
| 60 | return result; |
| 61 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 62 | bool nonNull = reply.readInt32(); |
| 63 | if (nonNull) { |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 64 | *buf = new GraphicBuffer(); |
| 65 | reply.read(**buf); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 66 | } |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 67 | result = reply.readInt32(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 68 | return result; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | virtual status_t setBufferCount(int bufferCount) |
| 72 | { |
| 73 | Parcel data, reply; |
| 74 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 75 | data.writeInt32(bufferCount); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 76 | status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply); |
| 77 | if (result != NO_ERROR) { |
| 78 | return result; |
| 79 | } |
| 80 | result = reply.readInt32(); |
| 81 | return result; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 84 | virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence, |
| 85 | uint32_t w, uint32_t h, uint32_t format, uint32_t usage) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 86 | Parcel data, reply; |
| 87 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 88 | data.writeInt32(w); |
| 89 | data.writeInt32(h); |
| 90 | data.writeInt32(format); |
| 91 | data.writeInt32(usage); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 92 | status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply); |
| 93 | if (result != NO_ERROR) { |
| 94 | return result; |
| 95 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 96 | *buf = reply.readInt32(); |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 97 | fence.clear(); |
| 98 | bool hasFence = reply.readInt32(); |
| 99 | if (hasFence) { |
| 100 | fence = new Fence(); |
| 101 | reply.read(*fence.get()); |
| 102 | } |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 103 | result = reply.readInt32(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 104 | return result; |
| 105 | } |
| 106 | |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 107 | virtual status_t queueBuffer(int buf, |
| 108 | const QueueBufferInput& input, QueueBufferOutput* output) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 109 | Parcel data, reply; |
| 110 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 111 | data.writeInt32(buf); |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 112 | memcpy(data.writeInplace(sizeof(input)), &input, sizeof(input)); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 113 | status_t result = remote()->transact(QUEUE_BUFFER, data, &reply); |
| 114 | if (result != NO_ERROR) { |
| 115 | return result; |
| 116 | } |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 117 | memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output)); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 118 | result = reply.readInt32(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 119 | return result; |
| 120 | } |
| 121 | |
| 122 | virtual void cancelBuffer(int buf) { |
| 123 | Parcel data, reply; |
| 124 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 125 | data.writeInt32(buf); |
| 126 | remote()->transact(CANCEL_BUFFER, data, &reply); |
| 127 | } |
| 128 | |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 129 | virtual int query(int what, int* value) { |
| 130 | Parcel data, reply; |
| 131 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 132 | data.writeInt32(what); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 133 | status_t result = remote()->transact(QUERY, data, &reply); |
| 134 | if (result != NO_ERROR) { |
| 135 | return result; |
| 136 | } |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 137 | value[0] = reply.readInt32(); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 138 | result = reply.readInt32(); |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 139 | return result; |
| 140 | } |
| 141 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 142 | virtual status_t setSynchronousMode(bool enabled) { |
| 143 | Parcel data, reply; |
| 144 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 145 | data.writeInt32(enabled); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 146 | status_t result = remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply); |
| 147 | if (result != NO_ERROR) { |
| 148 | return result; |
| 149 | } |
| 150 | result = reply.readInt32(); |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 151 | return result; |
| 152 | } |
| 153 | |
Mathias Agopian | 24202f5 | 2012-04-23 14:28:58 -0700 | [diff] [blame] | 154 | virtual status_t connect(int api, QueueBufferOutput* output) { |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 155 | Parcel data, reply; |
| 156 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 157 | data.writeInt32(api); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 158 | status_t result = remote()->transact(CONNECT, data, &reply); |
| 159 | if (result != NO_ERROR) { |
| 160 | return result; |
| 161 | } |
Mathias Agopian | 24202f5 | 2012-04-23 14:28:58 -0700 | [diff] [blame] | 162 | memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output)); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 163 | result = reply.readInt32(); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 164 | return result; |
| 165 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 166 | |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 167 | virtual status_t disconnect(int api) { |
| 168 | Parcel data, reply; |
| 169 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 170 | data.writeInt32(api); |
Jamie Gennis | 8a29ff2 | 2011-10-14 15:03:17 -0700 | [diff] [blame] | 171 | status_t result =remote()->transact(DISCONNECT, data, &reply); |
| 172 | if (result != NO_ERROR) { |
| 173 | return result; |
| 174 | } |
| 175 | result = reply.readInt32(); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 176 | return result; |
| 177 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | IMPLEMENT_META_INTERFACE(SurfaceTexture, "android.gui.SurfaceTexture"); |
| 181 | |
| 182 | // ---------------------------------------------------------------------- |
| 183 | |
| 184 | status_t BnSurfaceTexture::onTransact( |
| 185 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 186 | { |
| 187 | switch(code) { |
| 188 | case REQUEST_BUFFER: { |
| 189 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 190 | int bufferIdx = data.readInt32(); |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 191 | sp<GraphicBuffer> buffer; |
| 192 | int result = requestBuffer(bufferIdx, &buffer); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 193 | reply->writeInt32(buffer != 0); |
| 194 | if (buffer != 0) { |
| 195 | reply->write(*buffer); |
| 196 | } |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame] | 197 | reply->writeInt32(result); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 198 | return NO_ERROR; |
| 199 | } break; |
| 200 | case SET_BUFFER_COUNT: { |
| 201 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 202 | int bufferCount = data.readInt32(); |
| 203 | int result = setBufferCount(bufferCount); |
| 204 | reply->writeInt32(result); |
| 205 | return NO_ERROR; |
| 206 | } break; |
| 207 | case DEQUEUE_BUFFER: { |
| 208 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 209 | uint32_t w = data.readInt32(); |
| 210 | uint32_t h = data.readInt32(); |
| 211 | uint32_t format = data.readInt32(); |
| 212 | uint32_t usage = data.readInt32(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 213 | int buf; |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 214 | sp<Fence> fence; |
| 215 | int result = dequeueBuffer(&buf, fence, w, h, format, usage); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 216 | reply->writeInt32(buf); |
Jesse Hall | f785754 | 2012-06-14 15:26:33 -0700 | [diff] [blame] | 217 | reply->writeInt32(fence.get() != NULL); |
| 218 | if (fence.get() != NULL) { |
| 219 | reply->write(*fence.get()); |
| 220 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 221 | reply->writeInt32(result); |
| 222 | return NO_ERROR; |
| 223 | } break; |
| 224 | case QUEUE_BUFFER: { |
| 225 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 226 | int buf = data.readInt32(); |
Mathias Agopian | f0bc2f1 | 2012-04-09 16:14:01 -0700 | [diff] [blame] | 227 | QueueBufferInput const* const input = |
| 228 | reinterpret_cast<QueueBufferInput const *>( |
| 229 | data.readInplace(sizeof(QueueBufferInput))); |
| 230 | QueueBufferOutput* const output = |
| 231 | reinterpret_cast<QueueBufferOutput *>( |
| 232 | reply->writeInplace(sizeof(QueueBufferOutput))); |
| 233 | status_t result = queueBuffer(buf, *input, output); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 234 | reply->writeInt32(result); |
| 235 | return NO_ERROR; |
| 236 | } break; |
| 237 | case CANCEL_BUFFER: { |
| 238 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 239 | int buf = data.readInt32(); |
| 240 | cancelBuffer(buf); |
| 241 | return NO_ERROR; |
| 242 | } break; |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 243 | case QUERY: { |
| 244 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 245 | int value; |
| 246 | int what = data.readInt32(); |
| 247 | int res = query(what, &value); |
| 248 | reply->writeInt32(value); |
| 249 | reply->writeInt32(res); |
| 250 | return NO_ERROR; |
| 251 | } break; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 252 | case SET_SYNCHRONOUS_MODE: { |
| 253 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 254 | bool enabled = data.readInt32(); |
| 255 | status_t res = setSynchronousMode(enabled); |
| 256 | reply->writeInt32(res); |
| 257 | return NO_ERROR; |
| 258 | } break; |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 259 | case CONNECT: { |
| 260 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 261 | int api = data.readInt32(); |
Mathias Agopian | 24202f5 | 2012-04-23 14:28:58 -0700 | [diff] [blame] | 262 | QueueBufferOutput* const output = |
| 263 | reinterpret_cast<QueueBufferOutput *>( |
| 264 | reply->writeInplace(sizeof(QueueBufferOutput))); |
| 265 | status_t res = connect(api, output); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 266 | reply->writeInt32(res); |
| 267 | return NO_ERROR; |
| 268 | } break; |
| 269 | case DISCONNECT: { |
| 270 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 271 | int api = data.readInt32(); |
Mathias Agopian | 2773004 | 2011-07-14 20:20:58 -0700 | [diff] [blame] | 272 | status_t res = disconnect(api); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 273 | reply->writeInt32(res); |
| 274 | return NO_ERROR; |
| 275 | } break; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 276 | } |
| 277 | return BBinder::onTransact(code, data, reply, flags); |
| 278 | } |
| 279 | |
| 280 | // ---------------------------------------------------------------------------- |
| 281 | |
| 282 | }; // namespace android |