blob: 7049d9e42d1638375118b33a051f79b3bcd55214 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Mathias Agopian1473f462009-04-10 14:24:30 -070017#define LOG_TAG "ISurface"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <stdio.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/Parcel.h>
24#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <ui/Overlay.h>
Mathias Agopian6950e422009-10-05 17:07:12 -070027#include <ui/GraphicBuffer.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Mathias Agopian000479f2010-02-09 17:46:37 -080029#include <surfaceflinger/Surface.h>
30#include <surfaceflinger/ISurface.h>
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032namespace android {
33
Mathias Agopian1473f462009-04-10 14:24:30 -070034// ----------------------------------------------------------------------
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036ISurface::BufferHeap::BufferHeap()
37 : w(0), h(0), hor_stride(0), ver_stride(0), format(0),
38 transform(0), flags(0)
39{
40}
41
42ISurface::BufferHeap::BufferHeap(uint32_t w, uint32_t h,
43 int32_t hor_stride, int32_t ver_stride,
44 PixelFormat format, const sp<IMemoryHeap>& heap)
45 : w(w), h(h), hor_stride(hor_stride), ver_stride(ver_stride),
46 format(format), transform(0), flags(0), heap(heap)
47{
48}
49
50ISurface::BufferHeap::BufferHeap(uint32_t w, uint32_t h,
51 int32_t hor_stride, int32_t ver_stride,
52 PixelFormat format, uint32_t transform, uint32_t flags,
53 const sp<IMemoryHeap>& heap)
54 : w(w), h(h), hor_stride(hor_stride), ver_stride(ver_stride),
55 format(format), transform(transform), flags(flags), heap(heap)
56{
57}
58
59
60ISurface::BufferHeap::~BufferHeap()
61{
62}
63
Mathias Agopian1473f462009-04-10 14:24:30 -070064// ----------------------------------------------------------------------
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066class BpSurface : public BpInterface<ISurface>
67{
68public:
69 BpSurface(const sp<IBinder>& impl)
70 : BpInterface<ISurface>(impl)
71 {
72 }
73
Mathias Agopian2be352a2010-05-21 17:24:35 -070074 virtual sp<GraphicBuffer> requestBuffer(int bufferIdx,
75 uint32_t w, uint32_t h, uint32_t format, uint32_t usage)
Mathias Agopian1473f462009-04-10 14:24:30 -070076 {
77 Parcel data, reply;
78 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
Mathias Agopian9779b2212009-09-07 16:32:45 -070079 data.writeInt32(bufferIdx);
Mathias Agopian2be352a2010-05-21 17:24:35 -070080 data.writeInt32(w);
81 data.writeInt32(h);
82 data.writeInt32(format);
Mathias Agopian5cec4742009-08-11 22:34:02 -070083 data.writeInt32(usage);
Mathias Agopian9779b2212009-09-07 16:32:45 -070084 remote()->transact(REQUEST_BUFFER, data, &reply);
Mathias Agopianc86727f2010-02-11 17:30:52 -080085 sp<GraphicBuffer> buffer = new GraphicBuffer();
86 reply.read(*buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -070087 return buffer;
88 }
89
Mathias Agopian59751db2010-05-07 15:58:44 -070090 virtual status_t setBufferCount(int bufferCount)
91 {
92 Parcel data, reply;
93 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
94 data.writeInt32(bufferCount);
95 remote()->transact(SET_BUFFER_COUNT, data, &reply);
96 status_t err = reply.readInt32();
97 return err;
98 }
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 virtual status_t registerBuffers(const BufferHeap& buffers)
101 {
102 Parcel data, reply;
103 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
104 data.writeInt32(buffers.w);
105 data.writeInt32(buffers.h);
106 data.writeInt32(buffers.hor_stride);
107 data.writeInt32(buffers.ver_stride);
108 data.writeInt32(buffers.format);
109 data.writeInt32(buffers.transform);
110 data.writeInt32(buffers.flags);
111 data.writeStrongBinder(buffers.heap->asBinder());
112 remote()->transact(REGISTER_BUFFERS, data, &reply);
113 status_t result = reply.readInt32();
114 return result;
115 }
116
117 virtual void postBuffer(ssize_t offset)
118 {
119 Parcel data, reply;
120 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
121 data.writeInt32(offset);
122 remote()->transact(POST_BUFFER, data, &reply, IBinder::FLAG_ONEWAY);
123 }
124
125 virtual void unregisterBuffers()
126 {
127 Parcel data, reply;
128 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
129 remote()->transact(UNREGISTER_BUFFERS, data, &reply);
130 }
131
132 virtual sp<OverlayRef> createOverlay(
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800133 uint32_t w, uint32_t h, int32_t format, int32_t orientation)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 {
135 Parcel data, reply;
136 data.writeInterfaceToken(ISurface::getInterfaceDescriptor());
137 data.writeInt32(w);
138 data.writeInt32(h);
139 data.writeInt32(format);
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800140 data.writeInt32(orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 remote()->transact(CREATE_OVERLAY, data, &reply);
142 return OverlayRef::readFromParcel(reply);
143 }
144};
145
146IMPLEMENT_META_INTERFACE(Surface, "android.ui.ISurface");
147
148// ----------------------------------------------------------------------
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150status_t BnSurface::onTransact(
151 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
152{
153 switch(code) {
Mathias Agopian9779b2212009-09-07 16:32:45 -0700154 case REQUEST_BUFFER: {
Mathias Agopian1473f462009-04-10 14:24:30 -0700155 CHECK_INTERFACE(ISurface, data, reply);
Mathias Agopian9779b2212009-09-07 16:32:45 -0700156 int bufferIdx = data.readInt32();
Mathias Agopian2be352a2010-05-21 17:24:35 -0700157 uint32_t w = data.readInt32();
158 uint32_t h = data.readInt32();
159 uint32_t format = data.readInt32();
160 uint32_t usage = data.readInt32();
161 sp<GraphicBuffer> buffer(requestBuffer(bufferIdx, w, h, format, usage));
Mathias Agopianc86727f2010-02-11 17:30:52 -0800162 if (buffer == NULL)
163 return BAD_VALUE;
164 return reply->write(*buffer);
Mathias Agopian1473f462009-04-10 14:24:30 -0700165 }
Mathias Agopian59751db2010-05-07 15:58:44 -0700166 case SET_BUFFER_COUNT: {
167 CHECK_INTERFACE(ISurface, data, reply);
168 int bufferCount = data.readInt32();
169 status_t err = setBufferCount(bufferCount);
170 reply->writeInt32(err);
171 return NO_ERROR;
172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 case REGISTER_BUFFERS: {
174 CHECK_INTERFACE(ISurface, data, reply);
175 BufferHeap buffer;
176 buffer.w = data.readInt32();
177 buffer.h = data.readInt32();
178 buffer.hor_stride = data.readInt32();
179 buffer.ver_stride= data.readInt32();
180 buffer.format = data.readInt32();
181 buffer.transform = data.readInt32();
182 buffer.flags = data.readInt32();
183 buffer.heap = interface_cast<IMemoryHeap>(data.readStrongBinder());
184 status_t err = registerBuffers(buffer);
185 reply->writeInt32(err);
186 return NO_ERROR;
187 } break;
188 case UNREGISTER_BUFFERS: {
189 CHECK_INTERFACE(ISurface, data, reply);
190 unregisterBuffers();
191 return NO_ERROR;
192 } break;
193 case POST_BUFFER: {
194 CHECK_INTERFACE(ISurface, data, reply);
195 ssize_t offset = data.readInt32();
196 postBuffer(offset);
197 return NO_ERROR;
198 } break;
199 case CREATE_OVERLAY: {
200 CHECK_INTERFACE(ISurface, data, reply);
201 int w = data.readInt32();
202 int h = data.readInt32();
203 int f = data.readInt32();
Chih-Chung Change1ceec22010-01-21 17:31:06 -0800204 int orientation = data.readInt32();
205 sp<OverlayRef> o = createOverlay(w, h, f, orientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 return OverlayRef::writeToParcel(reply, o);
207 } break;
208 default:
209 return BBinder::onTransact(code, data, reply, flags);
210 }
211}
212
213}; // namespace android