1/*
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
17#include <binder/IMemory.h>
18#include <binder/Parcel.h>
19#include <utils/Errors.h>
20#include <binder/MemoryHeapBase.h>
21
22#include <ui/IOverlay.h>
23#include <ui/Overlay.h>
24
25#include <hardware/overlay.h>
26
27namespace android {
28
29Overlay::Overlay(const sp<OverlayRef>& overlayRef)
30    : mOverlayRef(overlayRef), mOverlayData(0), mStatus(NO_INIT)
31{
32    mOverlayData = NULL;
33    hw_module_t const* module;
34    if (overlayRef != 0) {
35        if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
36            if (overlay_data_open(module, &mOverlayData) == NO_ERROR) {
37                mStatus = mOverlayData->initialize(mOverlayData,
38                        overlayRef->mOverlayHandle);
39            }
40        }
41    }
42}
43
44Overlay::~Overlay() {
45    if (mOverlayData) {
46        overlay_data_close(mOverlayData);
47    }
48}
49
50status_t Overlay::dequeueBuffer(overlay_buffer_t* buffer)
51{
52    if (mStatus != NO_ERROR) return mStatus;
53    return  mOverlayData->dequeueBuffer(mOverlayData, buffer);
54}
55
56status_t Overlay::queueBuffer(overlay_buffer_t buffer)
57{
58    if (mStatus != NO_ERROR) return mStatus;
59    return mOverlayData->queueBuffer(mOverlayData, buffer);
60}
61
62status_t Overlay::resizeInput(uint32_t width, uint32_t height)
63{
64    if (mStatus != NO_ERROR) return mStatus;
65    return mOverlayData->resizeInput(mOverlayData, width, height);
66}
67
68status_t Overlay::setParameter(int param, int value)
69{
70    if (mStatus != NO_ERROR) return mStatus;
71    return mOverlayData->setParameter(mOverlayData, param, value);
72}
73
74status_t Overlay::setCrop(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
75{
76    if (mStatus != NO_ERROR) return mStatus;
77    return mOverlayData->setCrop(mOverlayData, x, y, w, h);
78}
79
80status_t Overlay::getCrop(uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h)
81{
82    if (mStatus != NO_ERROR) return mStatus;
83    return mOverlayData->getCrop(mOverlayData, x, y, w, h);
84}
85
86int32_t Overlay::getBufferCount() const
87{
88    if (mStatus != NO_ERROR) return mStatus;
89    return mOverlayData->getBufferCount(mOverlayData);
90}
91
92void* Overlay::getBufferAddress(overlay_buffer_t buffer)
93{
94    if (mStatus != NO_ERROR) return NULL;
95    return mOverlayData->getBufferAddress(mOverlayData, buffer);
96}
97
98void Overlay::destroy() {
99    if (mStatus != NO_ERROR) return;
100
101    // Must delete the objects in reverse creation order, thus the
102    //  data side must be closed first and then the destroy send to
103    //  the control side.
104    if (mOverlayData) {
105        overlay_data_close(mOverlayData);
106        mOverlayData = NULL;
107    }
108
109    mOverlayRef->mOverlayChannel->destroy();
110}
111
112status_t Overlay::getStatus() const {
113    return mStatus;
114}
115
116overlay_handle_t Overlay::getHandleRef() const {
117    if (mStatus != NO_ERROR) return NULL;
118    return mOverlayRef->mOverlayHandle;
119}
120
121uint32_t Overlay::getWidth() const {
122    if (mStatus != NO_ERROR) return 0;
123    return mOverlayRef->mWidth;
124}
125
126uint32_t Overlay::getHeight() const {
127    if (mStatus != NO_ERROR) return 0;
128    return mOverlayRef->mHeight;
129}
130
131int32_t Overlay::getFormat() const {
132    if (mStatus != NO_ERROR) return -1;
133    return mOverlayRef->mFormat;
134}
135
136int32_t Overlay::getWidthStride() const {
137    if (mStatus != NO_ERROR) return 0;
138    return mOverlayRef->mWidthStride;
139}
140
141int32_t Overlay::getHeightStride() const {
142    if (mStatus != NO_ERROR) return 0;
143    return mOverlayRef->mHeightStride;
144}
145// ----------------------------------------------------------------------------
146
147OverlayRef::OverlayRef()
148 : mOverlayHandle(0),
149    mWidth(0), mHeight(0), mFormat(0), mWidthStride(0), mHeightStride(0),
150    mOwnHandle(true)
151{
152}
153
154OverlayRef::OverlayRef(overlay_handle_t handle, const sp<IOverlay>& channel,
155         uint32_t w, uint32_t h, int32_t f, uint32_t ws, uint32_t hs)
156    : mOverlayHandle(handle), mOverlayChannel(channel),
157    mWidth(w), mHeight(h), mFormat(f), mWidthStride(ws), mHeightStride(hs),
158    mOwnHandle(false)
159{
160}
161
162OverlayRef::~OverlayRef()
163{
164    if (mOwnHandle) {
165        native_handle_close(mOverlayHandle);
166        native_handle_delete(const_cast<native_handle*>(mOverlayHandle));
167    }
168}
169
170sp<OverlayRef> OverlayRef::readFromParcel(const Parcel& data) {
171    sp<OverlayRef> result;
172    sp<IOverlay> overlay = IOverlay::asInterface(data.readStrongBinder());
173    if (overlay != NULL) {
174        uint32_t w = data.readInt32();
175        uint32_t h = data.readInt32();
176        uint32_t f = data.readInt32();
177        uint32_t ws = data.readInt32();
178        uint32_t hs = data.readInt32();
179        native_handle* handle = data.readNativeHandle();
180
181        result = new OverlayRef();
182        result->mOverlayHandle = handle;
183        result->mOverlayChannel = overlay;
184        result->mWidth = w;
185        result->mHeight = h;
186        result->mFormat = f;
187        result->mWidthStride = ws;
188        result->mHeightStride = hs;
189    }
190    return result;
191}
192
193status_t OverlayRef::writeToParcel(Parcel* reply, const sp<OverlayRef>& o) {
194    if (o != NULL) {
195        reply->writeStrongBinder(o->mOverlayChannel->asBinder());
196        reply->writeInt32(o->mWidth);
197        reply->writeInt32(o->mHeight);
198        reply->writeInt32(o->mFormat);
199        reply->writeInt32(o->mWidthStride);
200        reply->writeInt32(o->mHeightStride);
201        reply->writeNativeHandle(o->mOverlayHandle);
202    } else {
203        reply->writeStrongBinder(NULL);
204    }
205    return NO_ERROR;
206}
207
208// ----------------------------------------------------------------------------
209
210}; // namespace android
211