Mathias Agopian | 7460160 | 2009-10-01 15:45:37 -0700 | [diff] [blame] | 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 <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include <ui/PixelFormat.h> |
| 25 | #include <pixelflinger/pixelflinger.h> |
| 26 | |
| 27 | #include "Buffer.h" |
| 28 | #include "BufferAllocator.h" |
| 29 | |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // =========================================================================== |
| 34 | // Buffer and implementation of android_native_buffer_t |
| 35 | // =========================================================================== |
| 36 | |
| 37 | Buffer::Buffer() |
| 38 | : SurfaceBuffer(), mInitCheck(NO_ERROR), mVStride(0) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, |
| 43 | uint32_t reqUsage, uint32_t flags) |
| 44 | : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0) |
| 45 | { |
| 46 | mInitCheck = initSize(w, h, format, reqUsage, flags); |
| 47 | } |
| 48 | |
| 49 | Buffer::~Buffer() |
| 50 | { |
| 51 | if (handle) { |
| 52 | BufferAllocator& allocator(BufferAllocator::get()); |
| 53 | allocator.free(handle); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | status_t Buffer::initCheck() const { |
| 58 | return mInitCheck; |
| 59 | } |
| 60 | |
| 61 | android_native_buffer_t* Buffer::getNativeBuffer() const |
| 62 | { |
| 63 | return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this)); |
| 64 | } |
| 65 | |
| 66 | status_t Buffer::reallocate(uint32_t w, uint32_t h, PixelFormat f, |
| 67 | uint32_t reqUsage, uint32_t flags) |
| 68 | { |
| 69 | if (handle) { |
| 70 | BufferAllocator& allocator(BufferAllocator::get()); |
| 71 | allocator.free(handle); |
| 72 | handle = 0; |
| 73 | } |
| 74 | return initSize(w, h, f, reqUsage, flags); |
| 75 | } |
| 76 | |
| 77 | status_t Buffer::initSize(uint32_t w, uint32_t h, PixelFormat format, |
| 78 | uint32_t reqUsage, uint32_t flags) |
| 79 | { |
| 80 | status_t err = NO_ERROR; |
| 81 | BufferAllocator& allocator = BufferAllocator::get(); |
| 82 | err = allocator.alloc(w, h, format, reqUsage, &handle, &stride); |
| 83 | if (err == NO_ERROR) { |
| 84 | this->width = w; |
| 85 | this->height = h; |
| 86 | this->format = format; |
| 87 | mVStride = 0; |
| 88 | } |
| 89 | |
| 90 | return err; |
| 91 | } |
| 92 | |
| 93 | status_t Buffer::lock(GGLSurface* sur, uint32_t usage) |
| 94 | { |
| 95 | void* vaddr; |
| 96 | status_t res = SurfaceBuffer::lock(usage, &vaddr); |
| 97 | if (res == NO_ERROR && sur) { |
| 98 | sur->version = sizeof(GGLSurface); |
| 99 | sur->width = width; |
| 100 | sur->height = height; |
| 101 | sur->stride = stride; |
| 102 | sur->format = format; |
| 103 | sur->vstride = mVStride; |
| 104 | sur->data = static_cast<GGLubyte*>(vaddr); |
| 105 | } |
| 106 | return res; |
| 107 | } |
| 108 | |
| 109 | // --------------------------------------------------------------------------- |
| 110 | |
| 111 | }; // namespace android |