blob: 0510bc1f9d2cd6ddc76c5a6fc6dda04389e8d779 [file] [log] [blame]
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001/*
2 * Copyright (C) 2009 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#define LOG_TAG "SurfaceBuffer"
18
19#include <stdint.h>
20#include <errno.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/Log.h>
25#include <binder/Parcel.h>
26
27#include <ui/BufferMapper.h>
28#include <ui/Rect.h>
29#include <private/ui/SurfaceBuffer.h>
30
31namespace android {
32
33// ============================================================================
34// SurfaceBuffer
35// ============================================================================
36
37SurfaceBuffer::SurfaceBuffer()
38 : BASE(), mOwner(false), mBufferMapper(BufferMapper::get()), mIndex(-1)
39{
40 width =
41 height =
42 stride =
43 format =
44 usage = 0;
45 handle = NULL;
46}
47
48SurfaceBuffer::SurfaceBuffer(const Parcel& data)
49 : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
50{
51 // we own the handle in this case
52 width = data.readInt32();
53 if (width < 0) {
54 width = height = stride = format = usage = 0;
55 handle = 0;
56 } else {
57 height = data.readInt32();
58 stride = data.readInt32();
59 format = data.readInt32();
60 usage = data.readInt32();
61 handle = data.readNativeHandle();
62 }
63}
64
65SurfaceBuffer::~SurfaceBuffer()
66{
67 if (handle && mOwner) {
68 native_handle_close(handle);
69 native_handle_delete(const_cast<native_handle*>(handle));
70 }
71}
72
73status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
74{
75 const Rect lockBounds(width, height);
76 status_t res = lock(usage, lockBounds, vaddr);
77 return res;
78}
79
80status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
81{
82 if (rect.left < 0 || rect.right > this->width ||
83 rect.top < 0 || rect.bottom > this->height) {
84 LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
85 rect.left, rect.top, rect.right, rect.bottom,
86 this->width, this->height);
87 return BAD_VALUE;
88 }
89 status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
90 return res;
91}
92
93status_t SurfaceBuffer::unlock()
94{
95 status_t res = getBufferMapper().unlock(handle);
96 return res;
97}
98
99status_t SurfaceBuffer::writeToParcel(Parcel* reply,
100 android_native_buffer_t const* buffer)
101{
102 if (buffer == NULL)
103 return BAD_VALUE;
104
105 if (buffer->width < 0 || buffer->height < 0)
106 return BAD_VALUE;
107
108 status_t err = NO_ERROR;
109 if (buffer->handle == NULL) {
110 // this buffer doesn't have a handle
111 reply->writeInt32(NO_MEMORY);
112 } else {
113 reply->writeInt32(buffer->width);
114 reply->writeInt32(buffer->height);
115 reply->writeInt32(buffer->stride);
116 reply->writeInt32(buffer->format);
117 reply->writeInt32(buffer->usage);
118 err = reply->writeNativeHandle(buffer->handle);
119 }
120 return err;
121}
122
123
124void SurfaceBuffer::setIndex(int index) {
125 mIndex = index;
126}
127
128int SurfaceBuffer::getIndex() const {
129 return mIndex;
130}
131
132
133}; // namespace android
134