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 | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include "BufferAllocator.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include "LayerBitmap.h" |
| 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 | |
| 41 | Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) |
| 42 | : SurfaceBuffer(), mInitCheck(NO_INIT), mFlags(flags), |
| 43 | mVStride(0) |
| 44 | { |
| 45 | this->format = format; |
| 46 | if (w>0 && h>0) { |
| 47 | mInitCheck = initSize(w, h); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Buffer::~Buffer() |
| 52 | { |
| 53 | if (handle) { |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 54 | BufferAllocator& allocator(BufferAllocator::get()); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 55 | allocator.free(handle); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | status_t Buffer::initCheck() const { |
| 60 | return mInitCheck; |
| 61 | } |
| 62 | |
| 63 | android_native_buffer_t* Buffer::getNativeBuffer() const |
| 64 | { |
| 65 | return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this)); |
| 66 | } |
| 67 | |
| 68 | status_t Buffer::initSize(uint32_t w, uint32_t h) |
| 69 | { |
| 70 | status_t err = NO_ERROR; |
| 71 | |
| 72 | BufferAllocator& allocator = BufferAllocator::get(); |
| 73 | |
| 74 | /* |
| 75 | * buffers used for software rendering, but h/w composition |
| 76 | * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE |
| 77 | * |
| 78 | * buffers used for h/w rendering and h/w composition |
| 79 | * are allocated with HW_RENDER | HW_TEXTURE |
| 80 | * |
| 81 | * buffers used with h/w rendering and either NPOT or no egl_image_ext |
| 82 | * are allocated with SW_READ_RARELY | HW_RENDER |
| 83 | * |
| 84 | */ |
| 85 | |
| 86 | if (mFlags & Buffer::SECURE) { |
| 87 | // secure buffer, don't store it into the GPU |
| 88 | usage = BufferAllocator::USAGE_SW_READ_OFTEN | |
| 89 | BufferAllocator::USAGE_SW_WRITE_OFTEN; |
| 90 | } else { |
| 91 | if (mFlags & Buffer::GPU) { |
| 92 | // the client wants to do GL rendering |
| 93 | usage = BufferAllocator::USAGE_HW_RENDER | |
| 94 | BufferAllocator::USAGE_HW_TEXTURE; |
| 95 | } else { |
| 96 | // software rendering-client, h/w composition |
| 97 | usage = BufferAllocator::USAGE_SW_READ_OFTEN | |
| 98 | BufferAllocator::USAGE_SW_WRITE_OFTEN | |
| 99 | BufferAllocator::USAGE_HW_TEXTURE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | err = allocator.alloc(w, h, format, usage, &handle, &stride); |
| 104 | |
| 105 | if (err == NO_ERROR) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 106 | if (err == NO_ERROR) { |
| 107 | width = w; |
| 108 | height = h; |
| 109 | mVStride = 0; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return err; |
| 114 | } |
| 115 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 116 | status_t Buffer::lock(GGLSurface* sur, uint32_t usage) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 117 | { |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 118 | void* vaddr; |
| 119 | status_t res = SurfaceBuffer::lock(usage, &vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 120 | if (res == NO_ERROR && sur) { |
| 121 | sur->version = sizeof(GGLSurface); |
| 122 | sur->width = width; |
| 123 | sur->height = height; |
| 124 | sur->stride = stride; |
| 125 | sur->format = format; |
| 126 | sur->vstride = mVStride; |
Mathias Agopian | e71212b | 2009-05-05 00:37:46 -0700 | [diff] [blame] | 127 | sur->data = static_cast<GGLubyte*>(vaddr); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 128 | } |
| 129 | return res; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 132 | // =========================================================================== |
| 133 | // LayerBitmap |
| 134 | // =========================================================================== |
| 135 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | LayerBitmap::LayerBitmap() |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 137 | : mInfo(0), mWidth(0), mHeight(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | LayerBitmap::~LayerBitmap() |
| 142 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 145 | status_t LayerBitmap::init(surface_info_t* info, |
| 146 | uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 148 | if (info == NULL) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | return BAD_VALUE; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 150 | |
| 151 | mFormat = format; |
| 152 | mFlags = flags; |
| 153 | mWidth = w; |
| 154 | mHeight = h; |
| 155 | |
| 156 | mInfo = info; |
| 157 | memset(info, 0, sizeof(surface_info_t)); |
| 158 | info->flags = surface_info_t::eNeedNewBuffer; |
| 159 | |
| 160 | // init the buffer, but don't trigger an allocation |
| 161 | mBuffer = new Buffer(0, 0, format, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | return NO_ERROR; |
| 163 | } |
| 164 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 165 | status_t LayerBitmap::setSize(uint32_t w, uint32_t h) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 167 | Mutex::Autolock _l(mLock); |
| 168 | if ((w != mWidth) || (h != mHeight)) { |
| 169 | mWidth = w; |
| 170 | mHeight = h; |
| 171 | // this will signal the client that it needs to asks us for a new buffer |
| 172 | mInfo->flags = surface_info_t::eNeedNewBuffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | } |
| 174 | return NO_ERROR; |
| 175 | } |
| 176 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 177 | sp<Buffer> LayerBitmap::allocate() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 179 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 672136f | 2009-07-28 19:17:54 -0700 | [diff] [blame^] | 180 | surface_info_t* info = mInfo; |
| 181 | sp<Buffer> buffer = new Buffer(mWidth, mHeight, mFormat, mFlags); |
| 182 | status_t err = buffer->initCheck(); |
| 183 | if (LIKELY(err == NO_ERROR)) { |
| 184 | info->flags = surface_info_t::eBufferDirty; |
| 185 | info->status = NO_ERROR; |
| 186 | } else { |
| 187 | memset(info, 0, sizeof(surface_info_t)); |
| 188 | info->status = NO_MEMORY; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | } |
Mathias Agopian | 672136f | 2009-07-28 19:17:54 -0700 | [diff] [blame^] | 190 | mBuffer = buffer; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 191 | return buffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 194 | status_t LayerBitmap::free() |
| 195 | { |
| 196 | mBuffer.clear(); |
| 197 | mWidth = 0; |
| 198 | mHeight = 0; |
| 199 | return NO_ERROR; |
| 200 | } |
| 201 | |
| 202 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | // --------------------------------------------------------------------------- |
| 204 | |
| 205 | }; // namespace android |