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