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