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, |
| 39 | SET_CROP, |
| 40 | SET_TRANSFORM, |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 41 | GET_ALLOCATOR, |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 42 | QUERY, |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 43 | SET_SYNCHRONOUS_MODE, |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 44 | CONNECT, |
| 45 | DISCONNECT, |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 46 | SET_SCALING_MODE, |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | |
| 50 | class BpSurfaceTexture : public BpInterface<ISurfaceTexture> |
| 51 | { |
| 52 | public: |
| 53 | BpSurfaceTexture(const sp<IBinder>& impl) |
| 54 | : BpInterface<ISurfaceTexture>(impl) |
| 55 | { |
| 56 | } |
| 57 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 58 | virtual sp<GraphicBuffer> requestBuffer(int bufferIdx) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 59 | Parcel data, reply; |
| 60 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 61 | data.writeInt32(bufferIdx); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 62 | remote()->transact(REQUEST_BUFFER, data, &reply); |
| 63 | sp<GraphicBuffer> buffer; |
| 64 | bool nonNull = reply.readInt32(); |
| 65 | if (nonNull) { |
| 66 | buffer = new GraphicBuffer(); |
| 67 | reply.read(*buffer); |
| 68 | } |
| 69 | return buffer; |
| 70 | } |
| 71 | |
| 72 | virtual status_t setBufferCount(int bufferCount) |
| 73 | { |
| 74 | Parcel data, reply; |
| 75 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 76 | data.writeInt32(bufferCount); |
| 77 | remote()->transact(SET_BUFFER_COUNT, data, &reply); |
| 78 | status_t err = reply.readInt32(); |
| 79 | return err; |
| 80 | } |
| 81 | |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 82 | virtual status_t dequeueBuffer(int *buf, uint32_t w, uint32_t h, |
| 83 | uint32_t format, uint32_t usage) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 84 | Parcel data, reply; |
| 85 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 86 | data.writeInt32(w); |
| 87 | data.writeInt32(h); |
| 88 | data.writeInt32(format); |
| 89 | data.writeInt32(usage); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 90 | remote()->transact(DEQUEUE_BUFFER, data, &reply); |
| 91 | *buf = reply.readInt32(); |
| 92 | int result = reply.readInt32(); |
| 93 | return result; |
| 94 | } |
| 95 | |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame^] | 96 | virtual status_t queueBuffer(int buf, int64_t timestamp, |
| 97 | uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform) { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 98 | Parcel data, reply; |
| 99 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 100 | data.writeInt32(buf); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 101 | data.writeInt64(timestamp); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 102 | remote()->transact(QUEUE_BUFFER, data, &reply); |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame^] | 103 | *outWidth = reply.readInt32(); |
| 104 | *outHeight = reply.readInt32(); |
| 105 | *outTransform = reply.readInt32(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 106 | status_t result = reply.readInt32(); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | virtual void cancelBuffer(int buf) { |
| 111 | Parcel data, reply; |
| 112 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 113 | data.writeInt32(buf); |
| 114 | remote()->transact(CANCEL_BUFFER, data, &reply); |
| 115 | } |
| 116 | |
| 117 | virtual status_t setCrop(const Rect& reg) { |
| 118 | Parcel data, reply; |
| 119 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 120 | data.writeFloat(reg.left); |
| 121 | data.writeFloat(reg.top); |
| 122 | data.writeFloat(reg.right); |
| 123 | data.writeFloat(reg.bottom); |
| 124 | remote()->transact(SET_CROP, data, &reply); |
| 125 | status_t result = reply.readInt32(); |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | virtual status_t setTransform(uint32_t transform) { |
| 130 | Parcel data, reply; |
| 131 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 132 | data.writeInt32(transform); |
| 133 | remote()->transact(SET_TRANSFORM, data, &reply); |
| 134 | status_t result = reply.readInt32(); |
| 135 | return result; |
| 136 | } |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 137 | |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 138 | virtual status_t setScalingMode(int mode) { |
| 139 | Parcel data, reply; |
| 140 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 141 | data.writeInt32(mode); |
| 142 | remote()->transact(SET_SCALING_MODE, data, &reply); |
| 143 | status_t result = reply.readInt32(); |
| 144 | return result; |
| 145 | } |
| 146 | |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 147 | virtual sp<IBinder> getAllocator() { |
| 148 | Parcel data, reply; |
| 149 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 150 | remote()->transact(GET_ALLOCATOR, data, &reply); |
| 151 | return reply.readStrongBinder(); |
| 152 | } |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 153 | |
| 154 | virtual int query(int what, int* value) { |
| 155 | Parcel data, reply; |
| 156 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 157 | data.writeInt32(what); |
| 158 | remote()->transact(QUERY, data, &reply); |
| 159 | value[0] = reply.readInt32(); |
| 160 | status_t result = reply.readInt32(); |
| 161 | return result; |
| 162 | } |
| 163 | |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 164 | virtual status_t setSynchronousMode(bool enabled) { |
| 165 | Parcel data, reply; |
| 166 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 167 | data.writeInt32(enabled); |
| 168 | remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply); |
| 169 | status_t result = reply.readInt32(); |
| 170 | return result; |
| 171 | } |
| 172 | |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 173 | virtual status_t connect(int api) { |
| 174 | Parcel data, reply; |
| 175 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 176 | data.writeInt32(api); |
| 177 | remote()->transact(CONNECT, data, &reply); |
| 178 | status_t result = reply.readInt32(); |
| 179 | return result; |
| 180 | } |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 181 | |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 182 | virtual status_t disconnect(int api) { |
| 183 | Parcel data, reply; |
| 184 | data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor()); |
| 185 | data.writeInt32(api); |
| 186 | remote()->transact(DISCONNECT, data, &reply); |
| 187 | status_t result = reply.readInt32(); |
| 188 | return result; |
| 189 | } |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | IMPLEMENT_META_INTERFACE(SurfaceTexture, "android.gui.SurfaceTexture"); |
| 193 | |
| 194 | // ---------------------------------------------------------------------- |
| 195 | |
| 196 | status_t BnSurfaceTexture::onTransact( |
| 197 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 198 | { |
| 199 | switch(code) { |
| 200 | case REQUEST_BUFFER: { |
| 201 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 202 | int bufferIdx = data.readInt32(); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 203 | sp<GraphicBuffer> buffer(requestBuffer(bufferIdx)); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 204 | reply->writeInt32(buffer != 0); |
| 205 | if (buffer != 0) { |
| 206 | reply->write(*buffer); |
| 207 | } |
| 208 | return NO_ERROR; |
| 209 | } break; |
| 210 | case SET_BUFFER_COUNT: { |
| 211 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 212 | int bufferCount = data.readInt32(); |
| 213 | int result = setBufferCount(bufferCount); |
| 214 | reply->writeInt32(result); |
| 215 | return NO_ERROR; |
| 216 | } break; |
| 217 | case DEQUEUE_BUFFER: { |
| 218 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 219 | uint32_t w = data.readInt32(); |
| 220 | uint32_t h = data.readInt32(); |
| 221 | uint32_t format = data.readInt32(); |
| 222 | uint32_t usage = data.readInt32(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 223 | int buf; |
Mathias Agopian | c04f153 | 2011-04-25 20:22:14 -0700 | [diff] [blame] | 224 | int result = dequeueBuffer(&buf, w, h, format, usage); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 225 | reply->writeInt32(buf); |
| 226 | reply->writeInt32(result); |
| 227 | return NO_ERROR; |
| 228 | } break; |
| 229 | case QUEUE_BUFFER: { |
| 230 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 231 | int buf = data.readInt32(); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 232 | int64_t timestamp = data.readInt64(); |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame^] | 233 | uint32_t outWidth, outHeight, outTransform; |
| 234 | status_t result = queueBuffer(buf, timestamp, |
| 235 | &outWidth, &outHeight, &outTransform); |
| 236 | reply->writeInt32(outWidth); |
| 237 | reply->writeInt32(outHeight); |
| 238 | reply->writeInt32(outTransform); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 239 | reply->writeInt32(result); |
| 240 | return NO_ERROR; |
| 241 | } break; |
| 242 | case CANCEL_BUFFER: { |
| 243 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 244 | int buf = data.readInt32(); |
| 245 | cancelBuffer(buf); |
| 246 | return NO_ERROR; |
| 247 | } break; |
| 248 | case SET_CROP: { |
| 249 | Rect reg; |
| 250 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 251 | reg.left = data.readFloat(); |
| 252 | reg.top = data.readFloat(); |
| 253 | reg.right = data.readFloat(); |
| 254 | reg.bottom = data.readFloat(); |
| 255 | status_t result = setCrop(reg); |
| 256 | reply->writeInt32(result); |
| 257 | return NO_ERROR; |
| 258 | } break; |
| 259 | case SET_TRANSFORM: { |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 260 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 261 | uint32_t transform = data.readInt32(); |
| 262 | status_t result = setTransform(transform); |
| 263 | reply->writeInt32(result); |
| 264 | return NO_ERROR; |
| 265 | } break; |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 266 | case SET_SCALING_MODE: { |
| 267 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 268 | int mode = data.readInt32(); |
| 269 | status_t result = setScalingMode(mode); |
| 270 | reply->writeInt32(result); |
| 271 | return NO_ERROR; |
| 272 | } break; |
Jamie Gennis | 1b20cde | 2011-02-02 15:31:47 -0800 | [diff] [blame] | 273 | case GET_ALLOCATOR: { |
| 274 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 275 | sp<IBinder> result = getAllocator(); |
| 276 | reply->writeStrongBinder(result); |
| 277 | return NO_ERROR; |
| 278 | } break; |
Mathias Agopian | eafabcd | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 279 | case QUERY: { |
| 280 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 281 | int value; |
| 282 | int what = data.readInt32(); |
| 283 | int res = query(what, &value); |
| 284 | reply->writeInt32(value); |
| 285 | reply->writeInt32(res); |
| 286 | return NO_ERROR; |
| 287 | } break; |
Mathias Agopian | 8072711 | 2011-05-02 19:51:12 -0700 | [diff] [blame] | 288 | case SET_SYNCHRONOUS_MODE: { |
| 289 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 290 | bool enabled = data.readInt32(); |
| 291 | status_t res = setSynchronousMode(enabled); |
| 292 | reply->writeInt32(res); |
| 293 | return NO_ERROR; |
| 294 | } break; |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 295 | case CONNECT: { |
| 296 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 297 | int api = data.readInt32(); |
| 298 | status_t res = connect(api); |
| 299 | reply->writeInt32(res); |
| 300 | return NO_ERROR; |
| 301 | } break; |
| 302 | case DISCONNECT: { |
| 303 | CHECK_INTERFACE(ISurfaceTexture, data, reply); |
| 304 | int api = data.readInt32(); |
Mathias Agopian | 2773004 | 2011-07-14 20:20:58 -0700 | [diff] [blame] | 305 | status_t res = disconnect(api); |
Jamie Gennis | fe0a87b | 2011-07-13 19:12:20 -0700 | [diff] [blame] | 306 | reply->writeInt32(res); |
| 307 | return NO_ERROR; |
| 308 | } break; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 309 | } |
| 310 | return BBinder::onTransact(code, data, reply, flags); |
| 311 | } |
| 312 | |
| 313 | // ---------------------------------------------------------------------------- |
| 314 | |
| 315 | }; // namespace android |