18ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis/*
28ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Copyright (C) 2010 The Android Open Source Project
38ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
48ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Licensed under the Apache License, Version 2.0 (the "License");
58ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * you may not use this file except in compliance with the License.
68ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * You may obtain a copy of the License at
78ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
88ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *      http://www.apache.org/licenses/LICENSE-2.0
98ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis *
108ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * Unless required by applicable law or agreed to in writing, software
118ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * distributed under the License is distributed on an "AS IS" BASIS,
128ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * See the License for the specific language governing permissions and
148ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis * limitations under the License.
158ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis */
168ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
178ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <stdint.h>
188ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <sys/types.h>
198ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
208ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <utils/Errors.h>
218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <utils/RefBase.h>
228ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <utils/Vector.h>
238ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <utils/Timers.h>
248ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <binder/Parcel.h>
268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <binder/IInterface.h>
278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis#include <gui/ISurfaceTexture.h>
298ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
308ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisnamespace android {
318ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
328ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
338ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisenum {
348ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    REQUEST_BUFFER = IBinder::FIRST_CALL_TRANSACTION,
358ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    SET_BUFFER_COUNT,
368ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    DEQUEUE_BUFFER,
378ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    QUEUE_BUFFER,
388ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    CANCEL_BUFFER,
39eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    QUERY,
408072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    SET_SYNCHRONOUS_MODE,
41fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    CONNECT,
42fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    DISCONNECT,
438ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis};
448ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
458ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
468ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisclass BpSurfaceTexture : public BpInterface<ISurfaceTexture>
478ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis{
488ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennispublic:
498ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    BpSurfaceTexture(const sp<IBinder>& impl)
508ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        : BpInterface<ISurfaceTexture>(impl)
518ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    {
528ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
538ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
547b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis    virtual status_t requestBuffer(int bufferIdx, sp<GraphicBuffer>* buf) {
558ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        Parcel data, reply;
568ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
578ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInt32(bufferIdx);
588a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result =remote()->transact(REQUEST_BUFFER, data, &reply);
598a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
608a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
618a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
628ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        bool nonNull = reply.readInt32();
638ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        if (nonNull) {
647b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis            *buf = new GraphicBuffer();
657b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis            reply.read(**buf);
668ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        }
678a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
687b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis        return result;
698ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
708ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
718ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    virtual status_t setBufferCount(int bufferCount)
728ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    {
738ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        Parcel data, reply;
748ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
758ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInt32(bufferCount);
768a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result =remote()->transact(SET_BUFFER_COUNT, data, &reply);
778a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
788a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
798a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
808a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
818a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        return result;
828ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
838ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
84f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    virtual status_t dequeueBuffer(int *buf, sp<Fence>& fence,
85f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
868ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        Parcel data, reply;
878ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
88c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian        data.writeInt32(w);
89c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian        data.writeInt32(h);
90c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian        data.writeInt32(format);
91c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian        data.writeInt32(usage);
928a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result = remote()->transact(DEQUEUE_BUFFER, data, &reply);
938a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
948a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
958a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
968ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        *buf = reply.readInt32();
97f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall        fence.clear();
98f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall        bool hasFence = reply.readInt32();
99f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall        if (hasFence) {
100f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            fence = new Fence();
101f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            reply.read(*fence.get());
102f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall        }
1038a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
1048ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        return result;
1058ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
1068ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
107f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian    virtual status_t queueBuffer(int buf,
108f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian            const QueueBufferInput& input, QueueBufferOutput* output) {
1098ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        Parcel data, reply;
1108ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
1118ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInt32(buf);
112c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        data.write(input);
1138a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result = remote()->transact(QUEUE_BUFFER, data, &reply);
1148a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
1158a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
1168a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
117f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian        memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
1188a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
1198ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        return result;
1208ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
1218ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
122c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    virtual void cancelBuffer(int buf, sp<Fence> fence) {
1238ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        Parcel data, reply;
124c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        bool hasFence = fence.get() && fence->isValid();
1258ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
1268ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        data.writeInt32(buf);
127c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        data.writeInt32(hasFence);
128c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        if (hasFence) {
129c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            data.write(*fence.get());
130c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        }
1318ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        remote()->transact(CANCEL_BUFFER, data, &reply);
1328ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
1338ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
134eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    virtual int query(int what, int* value) {
135eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        Parcel data, reply;
136eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
137eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        data.writeInt32(what);
1388a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result = remote()->transact(QUERY, data, &reply);
1398a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
1408a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
1418a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
142eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        value[0] = reply.readInt32();
1438a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
144eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        return result;
145eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian    }
146eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian
1478072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    virtual status_t setSynchronousMode(bool enabled) {
1488072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        Parcel data, reply;
1498072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
1508072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        data.writeInt32(enabled);
1518a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result = remote()->transact(SET_SYNCHRONOUS_MODE, data, &reply);
1528a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
1538a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
1548a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
1558a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
1568072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        return result;
1578072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian    }
1588072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
15924202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian    virtual status_t connect(int api, QueueBufferOutput* output) {
160fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        Parcel data, reply;
161fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
162fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        data.writeInt32(api);
1638a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result = remote()->transact(CONNECT, data, &reply);
1648a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
1658a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
1668a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
16724202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian        memcpy(output, reply.readInplace(sizeof(*output)), sizeof(*output));
1688a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
169fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        return result;
170fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    }
1718072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian
172fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    virtual status_t disconnect(int api) {
173fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        Parcel data, reply;
174fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        data.writeInterfaceToken(ISurfaceTexture::getInterfaceDescriptor());
175fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        data.writeInt32(api);
1768a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        status_t result =remote()->transact(DISCONNECT, data, &reply);
1778a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        if (result != NO_ERROR) {
1788a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis            return result;
1798a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        }
1808a29ff2a356816694c695db70460f93c7fb4ae6fJamie Gennis        result = reply.readInt32();
181fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        return result;
182fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis    }
1838ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis};
1848ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
1858ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie GennisIMPLEMENT_META_INTERFACE(SurfaceTexture, "android.gui.SurfaceTexture");
1868ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
1878ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------
1888ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
1898ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennisstatus_t BnSurfaceTexture::onTransact(
1908ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1918ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis{
1928ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    switch(code) {
1938ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        case REQUEST_BUFFER: {
1948ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
1958ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int bufferIdx   = data.readInt32();
1967b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis            sp<GraphicBuffer> buffer;
1977b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis            int result = requestBuffer(bufferIdx, &buffer);
1988ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            reply->writeInt32(buffer != 0);
1998ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            if (buffer != 0) {
2008ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis                reply->write(*buffer);
2018ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            }
2027b305fffc39d0fe0926e7fd2d7f6a524fbce62b7Jamie Gennis            reply->writeInt32(result);
2038ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            return NO_ERROR;
2048ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        } break;
2058ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        case SET_BUFFER_COUNT: {
2068ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
2078ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int bufferCount = data.readInt32();
2088ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int result = setBufferCount(bufferCount);
2098ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            reply->writeInt32(result);
2108ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            return NO_ERROR;
2118ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        } break;
2128ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        case DEQUEUE_BUFFER: {
2138ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
214c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian            uint32_t w      = data.readInt32();
215c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian            uint32_t h      = data.readInt32();
216c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian            uint32_t format = data.readInt32();
217c04f153353cdb0d291297d10452239f791d3fd2bMathias Agopian            uint32_t usage  = data.readInt32();
2188ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int buf;
219f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            sp<Fence> fence;
220f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            int result = dequeueBuffer(&buf, fence, w, h, format, usage);
221c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            bool hasFence = fence.get() && fence->isValid();
2228ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            reply->writeInt32(buf);
223c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            reply->writeInt32(hasFence);
224c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            if (hasFence) {
225f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall                reply->write(*fence.get());
226f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            }
2278ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            reply->writeInt32(result);
2288ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            return NO_ERROR;
2298ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        } break;
2308ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        case QUEUE_BUFFER: {
2318ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
2328ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int buf = data.readInt32();
233c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            QueueBufferInput input(data);
234f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian            QueueBufferOutput* const output =
235f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                    reinterpret_cast<QueueBufferOutput *>(
236f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian                            reply->writeInplace(sizeof(QueueBufferOutput)));
237c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            status_t result = queueBuffer(buf, input, output);
2388ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            reply->writeInt32(result);
2398ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            return NO_ERROR;
2408ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        } break;
2418ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        case CANCEL_BUFFER: {
2428ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
2438ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            int buf = data.readInt32();
244c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            sp<Fence> fence;
245c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            bool hasFence = data.readInt32();
246c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            if (hasFence) {
247c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall                fence = new Fence();
248c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall                data.read(*fence.get());
249c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            }
250c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall            cancelBuffer(buf, fence);
2518ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis            return NO_ERROR;
2528ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis        } break;
253eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        case QUERY: {
254eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            CHECK_INTERFACE(ISurfaceTexture, data, reply);
255eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            int value;
256eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            int what = data.readInt32();
257eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            int res = query(what, &value);
258eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            reply->writeInt32(value);
259eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            reply->writeInt32(res);
260eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian            return NO_ERROR;
261eafabcdc1639fb96062d9e3c39b0ae27b0238ae1Mathias Agopian        } break;
2628072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        case SET_SYNCHRONOUS_MODE: {
2638072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian            CHECK_INTERFACE(ISurfaceTexture, data, reply);
2648072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian            bool enabled = data.readInt32();
2658072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian            status_t res = setSynchronousMode(enabled);
2668072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian            reply->writeInt32(res);
2678072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian            return NO_ERROR;
2688072711307aa98ee5ee6f7369860ae38c3e19656Mathias Agopian        } break;
269fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        case CONNECT: {
270fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
271fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            int api = data.readInt32();
27224202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian            QueueBufferOutput* const output =
27324202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian                    reinterpret_cast<QueueBufferOutput *>(
27424202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian                            reply->writeInplace(sizeof(QueueBufferOutput)));
27524202f5676c32edeef6544cf36e06b9fc970dbdeMathias Agopian            status_t res = connect(api, output);
276fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            reply->writeInt32(res);
277fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            return NO_ERROR;
278fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        } break;
279fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        case DISCONNECT: {
280fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            CHECK_INTERFACE(ISurfaceTexture, data, reply);
281fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            int api = data.readInt32();
2822773004a83624ed41cf3f94f2b11878cd8521812Mathias Agopian            status_t res = disconnect(api);
283fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            reply->writeInt32(res);
284fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis            return NO_ERROR;
285fe0a87b54654a1392650e7f1862df473287d8332Jamie Gennis        } break;
2868ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    }
2878ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis    return BBinder::onTransact(code, data, reply, flags);
2888ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}
2898ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
2908ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis// ----------------------------------------------------------------------------
2918ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis
292c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallstatic bool isValid(const sp<Fence>& fence) {
293c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    return fence.get() && fence->isValid();
294c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
295c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
296c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse HallISurfaceTexture::QueueBufferInput::QueueBufferInput(const Parcel& parcel) {
297c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    parcel.read(*this);
298c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
299c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
300c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallsize_t ISurfaceTexture::QueueBufferInput::getFlattenedSize() const
301c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall{
302c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    return sizeof(timestamp)
303c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall         + sizeof(crop)
304c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall         + sizeof(scalingMode)
305c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall         + sizeof(transform)
306c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall         + sizeof(bool)
307c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall         + (isValid(fence) ? fence->getFlattenedSize() : 0);
308c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
309c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
310c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallsize_t ISurfaceTexture::QueueBufferInput::getFdCount() const
311c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall{
312c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    return isValid(fence) ? fence->getFdCount() : 0;
313c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
314c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
315c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallstatus_t ISurfaceTexture::QueueBufferInput::flatten(void* buffer, size_t size,
316c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        int fds[], size_t count) const
317c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall{
318c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    status_t err = NO_ERROR;
319c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    bool haveFence = isValid(fence);
320c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    char* p = (char*)buffer;
321c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(p, &timestamp,   sizeof(timestamp));   p += sizeof(timestamp);
322c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(p, &crop,        sizeof(crop));        p += sizeof(crop);
323c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(p, &scalingMode, sizeof(scalingMode)); p += sizeof(scalingMode);
324c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(p, &transform,   sizeof(transform));   p += sizeof(transform);
325c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(p, &haveFence,   sizeof(haveFence));   p += sizeof(haveFence);
326c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    if (haveFence) {
327c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        err = fence->flatten(p, size - (p - (char*)buffer), fds, count);
328c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    }
329c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    return err;
330c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
331c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
332c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hallstatus_t ISurfaceTexture::QueueBufferInput::unflatten(void const* buffer,
333c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        size_t size, int fds[], size_t count)
334c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall{
335c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    status_t err = NO_ERROR;
336c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    bool haveFence;
337c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    const char* p = (const char*)buffer;
338c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(&timestamp,   p, sizeof(timestamp));   p += sizeof(timestamp);
339c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(&crop,        p, sizeof(crop));        p += sizeof(crop);
340c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(&scalingMode, p, sizeof(scalingMode)); p += sizeof(scalingMode);
341c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(&transform,   p, sizeof(transform));   p += sizeof(transform);
342c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    memcpy(&haveFence,   p, sizeof(haveFence));   p += sizeof(haveFence);
343c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    if (haveFence) {
344c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        fence = new Fence();
345c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        err = fence->unflatten(p, size - (p - (const char*)buffer), fds, count);
346c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    }
347c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall    return err;
348c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall}
349c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
3508ba32fade11abb73f3fd47ea0953c9528eb5b91fJamie Gennis}; // namespace android
351