summaryrefslogtreecommitdiffstats
path: root/libs/ui/SurfaceBuffer.cpp
blob: 0510bc1f9d2cd6ddc76c5a6fc6dda04389e8d779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "SurfaceBuffer"

#include <stdint.h>
#include <errno.h>
#include <sys/types.h>

#include <utils/Errors.h>
#include <utils/Log.h>
#include <binder/Parcel.h>

#include <ui/BufferMapper.h>
#include <ui/Rect.h>
#include <private/ui/SurfaceBuffer.h>

namespace android {

// ============================================================================
//  SurfaceBuffer
// ============================================================================

SurfaceBuffer::SurfaceBuffer() 
    : BASE(), mOwner(false), mBufferMapper(BufferMapper::get()), mIndex(-1)
{
    width  = 
    height = 
    stride = 
    format = 
    usage  = 0;
    handle = NULL;
}

SurfaceBuffer::SurfaceBuffer(const Parcel& data) 
    : BASE(), mOwner(true), mBufferMapper(BufferMapper::get())
{
    // we own the handle in this case
    width  = data.readInt32();
    if (width < 0) {
        width = height = stride = format = usage = 0;
        handle = 0;
    } else {
        height = data.readInt32();
        stride = data.readInt32();
        format = data.readInt32();
        usage  = data.readInt32();
        handle = data.readNativeHandle();
    }
}

SurfaceBuffer::~SurfaceBuffer()
{
    if (handle && mOwner) {
        native_handle_close(handle);
        native_handle_delete(const_cast<native_handle*>(handle));
    }
}

status_t SurfaceBuffer::lock(uint32_t usage, void** vaddr)
{
    const Rect lockBounds(width, height);
    status_t res = lock(usage, lockBounds, vaddr);
    return res;
}

status_t SurfaceBuffer::lock(uint32_t usage, const Rect& rect, void** vaddr)
{
    if (rect.left < 0 || rect.right  > this->width || 
        rect.top  < 0 || rect.bottom > this->height) {
        LOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)",
                rect.left, rect.top, rect.right, rect.bottom, 
                this->width, this->height);
        return BAD_VALUE;
    }
    status_t res = getBufferMapper().lock(handle, usage, rect, vaddr);
    return res;
}

status_t SurfaceBuffer::unlock()
{
    status_t res = getBufferMapper().unlock(handle);
    return res;
}

status_t SurfaceBuffer::writeToParcel(Parcel* reply, 
        android_native_buffer_t const* buffer)
{
    if (buffer == NULL)
        return BAD_VALUE;

    if (buffer->width < 0 || buffer->height < 0)
        return BAD_VALUE;

    status_t err = NO_ERROR;
    if (buffer->handle == NULL) {
        // this buffer doesn't have a handle
        reply->writeInt32(NO_MEMORY);
    } else {
        reply->writeInt32(buffer->width);
        reply->writeInt32(buffer->height);
        reply->writeInt32(buffer->stride);
        reply->writeInt32(buffer->format);
        reply->writeInt32(buffer->usage);
        err = reply->writeNativeHandle(buffer->handle);
    }
    return err;
}


void SurfaceBuffer::setIndex(int index) {
    mIndex = index;
}

int SurfaceBuffer::getIndex() const {
    return mIndex;
}


}; // namespace android