blob: b236edc290f5834c801da8d75a89b34bf4c26dc6 [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
17#include <utils/IMemory.h>
18#include <utils/Parcel.h>
19#include <utils/Errors.h>
20#include <utils/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
62int32_t Overlay::getBufferCount() const
63{
64 if (mStatus != NO_ERROR) return mStatus;
65 return mOverlayData->getBufferCount(mOverlayData);
66}
67
68void* Overlay::getBufferAddress(overlay_buffer_t buffer)
69{
70 if (mStatus != NO_ERROR) return NULL;
71 return mOverlayData->getBufferAddress(mOverlayData, buffer);
72}
73
74void Overlay::destroy() {
75 if (mStatus != NO_ERROR) return;
76 mOverlayRef->mOverlayChannel->destroy();
77}
78
79status_t Overlay::getStatus() const {
80 return mStatus;
81}
82
83overlay_handle_t Overlay::getHandleRef() const {
84 if (mStatus != NO_ERROR) return NULL;
85 return mOverlayRef->mOverlayHandle;
86}
87
88uint32_t Overlay::getWidth() const {
89 if (mStatus != NO_ERROR) return 0;
90 return mOverlayRef->mWidth;
91}
92
93uint32_t Overlay::getHeight() const {
94 if (mStatus != NO_ERROR) return 0;
95 return mOverlayRef->mHeight;
96}
97
98int32_t Overlay::getFormat() const {
99 if (mStatus != NO_ERROR) return -1;
100 return mOverlayRef->mFormat;
101}
102
103int32_t Overlay::getWidthStride() const {
104 if (mStatus != NO_ERROR) return 0;
105 return mOverlayRef->mWidthStride;
106}
107
108int32_t Overlay::getHeightStride() const {
109 if (mStatus != NO_ERROR) return 0;
110 return mOverlayRef->mHeightStride;
111}
112// ----------------------------------------------------------------------------
113
114OverlayRef::OverlayRef()
115 : mOverlayHandle(0),
116 mWidth(0), mHeight(0), mFormat(0), mWidthStride(0), mHeightStride(0),
117 mOwnHandle(true)
118{
119}
120
121OverlayRef::OverlayRef(overlay_handle_t handle, const sp<IOverlay>& channel,
122 uint32_t w, uint32_t h, int32_t f, uint32_t ws, uint32_t hs)
123 : mOverlayHandle(handle), mOverlayChannel(channel),
124 mWidth(w), mHeight(h), mFormat(f), mWidthStride(ws), mHeightStride(hs),
125 mOwnHandle(false)
126{
127}
128
129OverlayRef::~OverlayRef()
130{
131 if (mOwnHandle) {
132 /* FIXME: handles should be promoted to "real" API and be handled by
133 * the framework */
134 for (int i=0 ; i<mOverlayHandle->numFds ; i++) {
135 close(mOverlayHandle->data[i]);
136 }
137 free((void*)mOverlayHandle);
138 }
139}
140
141sp<OverlayRef> OverlayRef::readFromParcel(const Parcel& data) {
142 sp<OverlayRef> result;
143 sp<IOverlay> overlay = IOverlay::asInterface(data.readStrongBinder());
144 if (overlay != NULL) {
145 uint32_t w = data.readInt32();
146 uint32_t h = data.readInt32();
147 uint32_t f = data.readInt32();
148 uint32_t ws = data.readInt32();
149 uint32_t hs = data.readInt32();
150 native_handle* handle = data.readNativeHandle(NULL, NULL);
151
152 result = new OverlayRef();
153 result->mOverlayHandle = handle;
154 result->mOverlayChannel = overlay;
155 result->mWidth = w;
156 result->mHeight = h;
157 result->mFormat = f;
158 result->mWidthStride = ws;
159 result->mHeightStride = hs;
160 }
161 return result;
162}
163
164status_t OverlayRef::writeToParcel(Parcel* reply, const sp<OverlayRef>& o) {
165 if (o != NULL) {
166 reply->writeStrongBinder(o->mOverlayChannel->asBinder());
167 reply->writeInt32(o->mWidth);
168 reply->writeInt32(o->mHeight);
169 reply->writeInt32(o->mFormat);
170 reply->writeInt32(o->mWidthStride);
171 reply->writeInt32(o->mHeightStride);
172 reply->writeNativeHandle(*(o->mOverlayHandle));
173 } else {
174 reply->writeStrongBinder(NULL);
175 }
176 return NO_ERROR;
177}
178
179// ----------------------------------------------------------------------------
180
181}; // namespace android