| 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 |  | 
|  | 17 | #define LOG_TAG "SurfaceFlinger" | 
|  | 18 |  | 
|  | 19 | #include <stdlib.h> | 
|  | 20 | #include <stdint.h> | 
|  | 21 | #include <sys/types.h> | 
|  | 22 |  | 
|  | 23 | #include <cutils/memory.h> | 
|  | 24 | #include <utils/Errors.h> | 
|  | 25 | #include <utils/Log.h> | 
|  | 26 | #include <utils/MemoryDealer.h> | 
|  | 27 | #include <utils/IMemory.h> | 
|  | 28 | #include <ui/PixelFormat.h> | 
|  | 29 | #include <pixelflinger/pixelflinger.h> | 
|  | 30 |  | 
|  | 31 | #include "LayerBitmap.h" | 
|  | 32 | #include "SurfaceFlinger.h" | 
|  | 33 | #include "VRamHeap.h" | 
|  | 34 |  | 
|  | 35 |  | 
|  | 36 | namespace android { | 
|  | 37 |  | 
|  | 38 | // --------------------------------------------------------------------------- | 
|  | 39 |  | 
|  | 40 | LayerBitmap::LayerBitmap() | 
|  | 41 | : mAllocFlags(0), mOffset(0), mSize(-1U), mAlignment(2) | 
|  | 42 | { | 
|  | 43 | memset(&mSurface, 0, sizeof(mSurface)); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | LayerBitmap::~LayerBitmap() | 
|  | 47 | { | 
|  | 48 | mSurface.data = 0; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | status_t LayerBitmap::init(const sp<MemoryDealer>& allocator) | 
|  | 52 | { | 
|  | 53 | if (mAllocator != NULL) | 
|  | 54 | return BAD_VALUE; | 
|  | 55 | mAllocator = allocator; | 
|  | 56 | return NO_ERROR; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | status_t LayerBitmap::setBits(uint32_t w, uint32_t h, uint32_t alignment, | 
|  | 60 | PixelFormat format, uint32_t flags) | 
|  | 61 | { | 
|  | 62 | const sp<MemoryDealer>& allocator(mAllocator); | 
|  | 63 | if (allocator == NULL) | 
|  | 64 | return NO_INIT; | 
|  | 65 |  | 
|  | 66 | if (UNLIKELY(w == mSurface.width && h == mSurface.height && | 
|  | 67 | format == mSurface.format)) | 
|  | 68 | { // same format and size, do nothing. | 
|  | 69 | return NO_ERROR; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | PixelFormatInfo info; | 
|  | 73 | getPixelFormatInfo(format, &info); | 
|  | 74 |  | 
|  | 75 | uint32_t allocFlags = MemoryDealer::PAGE_ALIGNED; | 
|  | 76 | const uint32_t align = 4; // must match GL_UNPACK_ALIGNMENT | 
|  | 77 | const uint32_t Bpp = info.bytesPerPixel; | 
|  | 78 | uint32_t stride = (w + (alignment-1)) & ~(alignment-1); | 
|  | 79 | stride = ((stride * Bpp + (align-1)) & ~(align-1)) / Bpp; | 
|  | 80 | size_t size = info.getScanlineSize(stride) * h; | 
|  | 81 | if (allocFlags & MemoryDealer::PAGE_ALIGNED) { | 
|  | 82 | size_t pagesize = getpagesize(); | 
|  | 83 | size = (size + (pagesize-1)) & ~(pagesize-1); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | /* FIXME: we should be able to have a h/v stride because the user of the | 
|  | 87 | * surface might have stride limitation (for instance h/w codecs often do) | 
|  | 88 | */ | 
|  | 89 | int32_t vstride = 0; | 
|  | 90 |  | 
|  | 91 | mAlignment = alignment; | 
|  | 92 | mAllocFlags = allocFlags; | 
|  | 93 | mOffset = 0; | 
|  | 94 | if (mSize != size) { | 
|  | 95 | // would be nice to have a reallocate() api | 
|  | 96 | mBitsMemory.clear(); // free-memory | 
|  | 97 | mBitsMemory = allocator->allocate(size, allocFlags); | 
|  | 98 | mSize = size; | 
|  | 99 | } else { | 
|  | 100 | // don't erase memory if we didn't have to reallocate | 
|  | 101 | flags &= ~SECURE_BITS; | 
|  | 102 | } | 
|  | 103 | if (mBitsMemory != 0) { | 
|  | 104 | mOffset = mBitsMemory->offset(); | 
|  | 105 | mSurface.data = static_cast<GGLubyte*>(mBitsMemory->pointer()); | 
|  | 106 | mSurface.version = sizeof(GGLSurface); | 
|  | 107 | mSurface.width  = w; | 
|  | 108 | mSurface.height = h; | 
|  | 109 | mSurface.stride = stride; | 
|  | 110 | mSurface.vstride = vstride; | 
|  | 111 | mSurface.format = format; | 
|  | 112 | if (flags & SECURE_BITS) | 
|  | 113 | clear(); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (mBitsMemory==0 || mSurface.data==0) { | 
|  | 117 | LOGE("not enough memory for layer bitmap size=%u", size); | 
|  | 118 | allocator->dump("LayerBitmap"); | 
|  | 119 | mSurface.data = 0; | 
|  | 120 | mSize = -1U; | 
|  | 121 | return NO_MEMORY; | 
|  | 122 | } | 
|  | 123 | return NO_ERROR; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | void LayerBitmap::clear() | 
|  | 127 | { | 
|  | 128 | // NOTE: this memset should not be necessary, at least for | 
|  | 129 | // opaque surface. However, for security reasons it's better to keep it | 
|  | 130 | // (in the case of pmem, it's possible that the memory contains old | 
|  | 131 | // data) | 
|  | 132 | if (mSurface.data) { | 
|  | 133 | memset(mSurface.data, 0, mSize); | 
|  | 134 | //if (bytesPerPixel(mSurface.format) == 4) { | 
|  | 135 | //    android_memset32((uint32_t*)mSurface.data, 0xFF0000FF, mSize); | 
|  | 136 | //} else  { | 
|  | 137 | //    android_memset16((uint16_t*)mSurface.data, 0xF800, mSize); | 
|  | 138 | //} | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | status_t LayerBitmap::getInfo(surface_info_t* info) const | 
|  | 143 | { | 
|  | 144 | if (mSurface.data == 0) { | 
|  | 145 | memset(info, 0, sizeof(surface_info_t)); | 
|  | 146 | info->bits_offset = NO_MEMORY; | 
|  | 147 | return NO_MEMORY; | 
|  | 148 | } | 
|  | 149 | info->w     = uint16_t(width()); | 
|  | 150 | info->h     = uint16_t(height()); | 
|  | 151 | info->stride= uint16_t(stride()); | 
|  | 152 | info->bpr   = uint16_t(stride() * bytesPerPixel(pixelFormat())); | 
|  | 153 | info->format= uint8_t(pixelFormat()); | 
|  | 154 | info->flags = surface_info_t::eBufferDirty; | 
|  | 155 | info->bits_offset = ssize_t(mOffset); | 
|  | 156 | return NO_ERROR; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | status_t LayerBitmap::resize(uint32_t w, uint32_t h) | 
|  | 160 | { | 
|  | 161 | int err = setBits(w, h, mAlignment, pixelFormat(), SECURE_BITS); | 
|  | 162 | return err; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | size_t LayerBitmap::size() const | 
|  | 166 | { | 
|  | 167 | return mSize; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | void LayerBitmap::getBitmapSurface(copybit_image_t* img) const | 
|  | 171 | { | 
|  | 172 | const sp<IMemoryHeap>& mh(getAllocator()->getMemoryHeap()); | 
|  | 173 | void* sbase = mh->base(); | 
|  | 174 | const GGLSurface& t(surface()); | 
|  | 175 | img->w = t.stride  ?: t.width; | 
|  | 176 | img->h = t.vstride ?: t.height; | 
|  | 177 | img->format = t.format; | 
|  | 178 | img->offset = intptr_t(t.data) - intptr_t(sbase); | 
|  | 179 | img->base = sbase; | 
|  | 180 | img->fd = mh->heapID(); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | // --------------------------------------------------------------------------- | 
|  | 184 |  | 
|  | 185 | }; // namespace android |