blob: 7c98857fc283613ab80493c000db13058adde7de [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
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
36namespace android {
37
38// ---------------------------------------------------------------------------
39
40LayerBitmap::LayerBitmap()
41 : mAllocFlags(0), mOffset(0), mSize(-1U), mAlignment(2)
42{
43 memset(&mSurface, 0, sizeof(mSurface));
44}
45
46LayerBitmap::~LayerBitmap()
47{
48 mSurface.data = 0;
49}
50
51status_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
59status_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 uint32_t allocFlags = MemoryDealer::PAGE_ALIGNED;
73 const uint32_t align = 4; // must match GL_UNPACK_ALIGNMENT
74 const uint32_t Bpp = bytesPerPixel(format);
75 uint32_t stride = (w + (alignment-1)) & ~(alignment-1);
76 stride = ((stride * Bpp + (align-1)) & ~(align-1)) / Bpp;
77 size_t size = stride * h * Bpp;
78 if (format == PIXEL_FORMAT_YCbCr_422_SP ||
79 format == PIXEL_FORMAT_YCbCr_420_SP) {
80 // in YUV planar, bitsPerPixel is for the Y plane
81 size = (size * bitsPerPixel(format)) / 8;
82 }
83
84 if (allocFlags & MemoryDealer::PAGE_ALIGNED) {
85 size_t pagesize = getpagesize();
86 size = (size + (pagesize-1)) & ~(pagesize-1);
87 }
88
89 /* FIXME: we should be able to have a h/v stride because the user of the
90 * surface might have stride limitation (for instance h/w codecs often do)
91 */
92 int32_t vstride = 0;
93
94 mAlignment = alignment;
95 mAllocFlags = allocFlags;
96 mOffset = 0;
97 if (mSize != size) {
98 // would be nice to have a reallocate() api
99 mBitsMemory.clear(); // free-memory
100 mBitsMemory = allocator->allocate(size, allocFlags);
101 mSize = size;
102 } else {
103 // don't erase memory if we didn't have to reallocate
104 flags &= ~SECURE_BITS;
105 }
106 if (mBitsMemory != 0) {
107 mOffset = mBitsMemory->offset();
108 mSurface.data = static_cast<GGLubyte*>(mBitsMemory->pointer());
109 mSurface.version = sizeof(GGLSurface);
110 mSurface.width = w;
111 mSurface.height = h;
112 mSurface.stride = stride;
113 mSurface.vstride = vstride;
114 mSurface.format = format;
115 if (flags & SECURE_BITS)
116 clear();
117 }
118
119 if (mBitsMemory==0 || mSurface.data==0) {
120 LOGE("not enough memory for layer bitmap size=%u", size);
121 allocator->dump("LayerBitmap");
122 mSurface.data = 0;
123 mSize = -1U;
124 return NO_MEMORY;
125 }
126 return NO_ERROR;
127}
128
129void LayerBitmap::clear()
130{
131 // NOTE: this memset should not be necessary, at least for
132 // opaque surface. However, for security reasons it's better to keep it
133 // (in the case of pmem, it's possible that the memory contains old
134 // data)
135 if (mSurface.data) {
136 memset(mSurface.data, 0, mSize);
137 //if (bytesPerPixel(mSurface.format) == 4) {
138 // android_memset32((uint32_t*)mSurface.data, 0xFF0000FF, mSize);
139 //} else {
140 // android_memset16((uint16_t*)mSurface.data, 0xF800, mSize);
141 //}
142 }
143}
144
145status_t LayerBitmap::getInfo(surface_info_t* info) const
146{
147 if (mSurface.data == 0) {
148 memset(info, 0, sizeof(surface_info_t));
149 info->bits_offset = NO_MEMORY;
150 return NO_MEMORY;
151 }
152 info->w = uint16_t(width());
153 info->h = uint16_t(height());
154 info->stride= uint16_t(stride());
155 info->bpr = uint16_t(stride() * bytesPerPixel(pixelFormat()));
156 info->format= uint8_t(pixelFormat());
157 info->flags = surface_info_t::eBufferDirty;
158 info->bits_offset = ssize_t(mOffset);
159 return NO_ERROR;
160}
161
162status_t LayerBitmap::resize(uint32_t w, uint32_t h)
163{
164 int err = setBits(w, h, mAlignment, pixelFormat(), SECURE_BITS);
165 return err;
166}
167
168size_t LayerBitmap::size() const
169{
170 return mSize;
171}
172
173void LayerBitmap::getBitmapSurface(copybit_image_t* img) const
174{
175 const sp<IMemoryHeap>& mh(getAllocator()->getMemoryHeap());
176 void* sbase = mh->base();
177 const GGLSurface& t(surface());
178 img->w = t.stride ?: t.width;
179 img->h = t.vstride ?: t.height;
180 img->format = t.format;
181 img->offset = intptr_t(t.data) - intptr_t(sbase);
182 img->base = sbase;
183 img->fd = mh->heapID();
184}
185
186// ---------------------------------------------------------------------------
187
188}; // namespace android