The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/Errors.h> |
| 22 | #include <utils/Log.h> |
Mathias Agopian | 310f8da | 2009-05-22 01:27:01 -0700 | [diff] [blame] | 23 | #include <binder/MemoryBase.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IMemory.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 25 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <ui/PixelFormat.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 27 | #include <ui/Surface.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <pixelflinger/pixelflinger.h> |
| 29 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 30 | #include "Buffer.h" |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 | #include "BufferAllocator.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | namespace android { |
| 36 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | // =========================================================================== |
| 38 | // Buffer and implementation of android_native_buffer_t |
| 39 | // =========================================================================== |
| 40 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 41 | Buffer::Buffer() |
| 42 | : SurfaceBuffer(), mInitCheck(NO_ERROR), mVStride(0) |
| 43 | { |
| 44 | } |
| 45 | |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 46 | Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, |
| 47 | uint32_t reqUsage, uint32_t flags) |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 48 | : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 49 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 50 | mInitCheck = initSize(w, h, format, reqUsage, flags); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Buffer::~Buffer() |
| 54 | { |
| 55 | if (handle) { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 56 | BufferAllocator& allocator(BufferAllocator::get()); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 57 | allocator.free(handle); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | status_t Buffer::initCheck() const { |
| 62 | return mInitCheck; |
| 63 | } |
| 64 | |
| 65 | android_native_buffer_t* Buffer::getNativeBuffer() const |
| 66 | { |
| 67 | return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this)); |
| 68 | } |
| 69 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 70 | status_t Buffer::reallocate(uint32_t w, uint32_t h, PixelFormat f, |
| 71 | uint32_t reqUsage, uint32_t flags) |
| 72 | { |
| 73 | if (handle) { |
| 74 | BufferAllocator& allocator(BufferAllocator::get()); |
| 75 | allocator.free(handle); |
| 76 | handle = 0; |
| 77 | } |
| 78 | return initSize(w, h, f, reqUsage, flags); |
| 79 | } |
| 80 | |
| 81 | status_t Buffer::initSize(uint32_t w, uint32_t h, PixelFormat format, |
| 82 | uint32_t reqUsage, uint32_t flags) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 83 | { |
| 84 | status_t err = NO_ERROR; |
| 85 | |
| 86 | BufferAllocator& allocator = BufferAllocator::get(); |
| 87 | |
| 88 | /* |
| 89 | * buffers used for software rendering, but h/w composition |
| 90 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 91 | * |
| 92 | * buffers used for h/w rendering and h/w composition |
| 93 | * are allocated with HW_RENDER | HW_TEXTURE |
| 94 | * |
| 95 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 96 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 97 | * |
| 98 | */ |
| 99 | |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 100 | if (flags & Buffer::SECURE) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 101 | // secure buffer, don't store it into the GPU |
| 102 | usage = BufferAllocator::USAGE_SW_READ_OFTEN | |
| 103 | BufferAllocator::USAGE_SW_WRITE_OFTEN; |
| 104 | } else { |
Mathias Agopian | 5221271 | 2009-08-11 22:34:02 -0700 | [diff] [blame] | 105 | // it's allowed to modify the usage flags here, but generally |
| 106 | // the requested flags should be honored. |
| 107 | usage = reqUsage | BufferAllocator::USAGE_HW_TEXTURE; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | err = allocator.alloc(w, h, format, usage, &handle, &stride); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 111 | if (err == NO_ERROR) { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame^] | 112 | this->width = w; |
| 113 | this->height = h; |
| 114 | this->format = format; |
Mathias Agopian | 5051754 | 2009-08-19 17:10:18 -0700 | [diff] [blame] | 115 | mVStride = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return err; |
| 119 | } |
| 120 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 121 | status_t Buffer::lock(GGLSurface* sur, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 122 | { |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 123 | void* vaddr; |
| 124 | status_t res = SurfaceBuffer::lock(usage, &vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 125 | if (res == NO_ERROR && sur) { |
| 126 | sur->version = sizeof(GGLSurface); |
| 127 | sur->width = width; |
| 128 | sur->height = height; |
| 129 | sur->stride = stride; |
| 130 | sur->format = format; |
| 131 | sur->vstride = mVStride; |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 132 | sur->data = static_cast<GGLubyte*>(vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 133 | } |
| 134 | return res; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | } |
| 136 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | // --------------------------------------------------------------------------- |
| 138 | |
| 139 | }; // namespace android |