blob: 38d4bcf02f7d72808996f3a30a920703a5d09d72 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdint.h>
19#include <sys/types.h>
20
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/Errors.h>
22#include <utils/Log.h>
23#include <utils/MemoryDealer.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/MemoryBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/IMemory.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/PixelFormat.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <pixelflinger/pixelflinger.h>
30
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "LayerBitmap.h"
33#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35
36namespace android {
37
Mathias Agopian076b1cc2009-04-10 14:24:30 -070038// ===========================================================================
39// Buffer and implementation of android_native_buffer_t
40// ===========================================================================
41
42Buffer::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
52Buffer::~Buffer()
53{
54 if (handle) {
Mathias Agopian0926f502009-05-04 14:17:04 -070055 BufferAllocator& allocator(BufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070056 allocator.free(handle);
57 }
58}
59
60status_t Buffer::initCheck() const {
61 return mInitCheck;
62}
63
64android_native_buffer_t* Buffer::getNativeBuffer() const
65{
66 return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this));
67}
68
69status_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 Agopian076b1cc2009-04-10 14:24:30 -0700107 if (err == NO_ERROR) {
108 width = w;
109 height = h;
110 mVStride = 0;
111 }
112 }
113
114 return err;
115}
116
Mathias Agopian0926f502009-05-04 14:17:04 -0700117status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700118{
Mathias Agopian0926f502009-05-04 14:17:04 -0700119 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 Agopian076b1cc2009-04-10 14:24:30 -0700130}
131
Mathias Agopian0926f502009-05-04 14:17:04 -0700132
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133
134// ===========================================================================
135// LayerBitmap
136// ===========================================================================
137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
139LayerBitmap::LayerBitmap()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 : mInfo(0), mWidth(0), mHeight(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142}
143
144LayerBitmap::~LayerBitmap()
145{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146}
147
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148status_t LayerBitmap::init(surface_info_t* info,
149 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151 if (info == NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 return BAD_VALUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700153
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 Projectedbf3b62009-03-03 19:31:44 -0800165 return NO_ERROR;
166}
167
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700168status_t LayerBitmap::setSize(uint32_t w, uint32_t h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700170 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 Projectedbf3b62009-03-03 19:31:44 -0800176 }
177 return NO_ERROR;
178}
179
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180sp<Buffer> LayerBitmap::allocate()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700182 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 Projectedbf3b62009-03-03 19:31:44 -0800198 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700199 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
202// ---------------------------------------------------------------------------
203
204}; // namespace android