blob: 397ddc82e12a137665453d76e92c8a7b80714877 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 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) {
Mathias Agopian3c4d8c82009-05-29 14:55:06 -0700117 LOGE("not enough memory for layer bitmap "
118 "size=%u (w=%d, h=%d, stride=%d, format=%d)",
119 size, int(w), int(h), int(stride), int(format));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 allocator->dump("LayerBitmap");
121 mSurface.data = 0;
122 mSize = -1U;
123 return NO_MEMORY;
124 }
125 return NO_ERROR;
126}
127
128void LayerBitmap::clear()
129{
130 // NOTE: this memset should not be necessary, at least for
131 // opaque surface. However, for security reasons it's better to keep it
132 // (in the case of pmem, it's possible that the memory contains old
133 // data)
134 if (mSurface.data) {
135 memset(mSurface.data, 0, mSize);
136 //if (bytesPerPixel(mSurface.format) == 4) {
137 // android_memset32((uint32_t*)mSurface.data, 0xFF0000FF, mSize);
138 //} else {
139 // android_memset16((uint16_t*)mSurface.data, 0xF800, mSize);
140 //}
141 }
142}
143
144status_t LayerBitmap::getInfo(surface_info_t* info) const
145{
146 if (mSurface.data == 0) {
147 memset(info, 0, sizeof(surface_info_t));
148 info->bits_offset = NO_MEMORY;
149 return NO_MEMORY;
150 }
151 info->w = uint16_t(width());
152 info->h = uint16_t(height());
153 info->stride= uint16_t(stride());
154 info->bpr = uint16_t(stride() * bytesPerPixel(pixelFormat()));
155 info->format= uint8_t(pixelFormat());
156 info->flags = surface_info_t::eBufferDirty;
157 info->bits_offset = ssize_t(mOffset);
158 return NO_ERROR;
159}
160
161status_t LayerBitmap::resize(uint32_t w, uint32_t h)
162{
163 int err = setBits(w, h, mAlignment, pixelFormat(), SECURE_BITS);
164 return err;
165}
166
167size_t LayerBitmap::size() const
168{
169 return mSize;
170}
171
172void LayerBitmap::getBitmapSurface(copybit_image_t* img) const
173{
174 const sp<IMemoryHeap>& mh(getAllocator()->getMemoryHeap());
175 void* sbase = mh->base();
176 const GGLSurface& t(surface());
177 img->w = t.stride ?: t.width;
178 img->h = t.vstride ?: t.height;
179 img->format = t.format;
180 img->offset = intptr_t(t.data) - intptr_t(sbase);
181 img->base = sbase;
182 img->fd = mh->heapID();
183}
184
185// ---------------------------------------------------------------------------
186
187}; // namespace android