blob: e0984fe48c7cef7aa014c01bbe160de94f165017 [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>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/MemoryDealer.h>
Mathias Agopian310f8da2009-05-22 01:27:01 -070024#include <binder/MemoryBase.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/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 Agopiane71212b2009-05-05 00:37:46 -0700119 void* vaddr;
120 status_t res = SurfaceBuffer::lock(usage, &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700121 if (res == NO_ERROR && sur) {
122 sur->version = sizeof(GGLSurface);
123 sur->width = width;
124 sur->height = height;
125 sur->stride = stride;
126 sur->format = format;
127 sur->vstride = mVStride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700128 sur->data = static_cast<GGLubyte*>(vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700129 }
130 return res;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131}
132
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133// ===========================================================================
134// LayerBitmap
135// ===========================================================================
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137LayerBitmap::LayerBitmap()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 : mInfo(0), mWidth(0), mHeight(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
142LayerBitmap::~LayerBitmap()
143{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}
145
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700146status_t LayerBitmap::init(surface_info_t* info,
147 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149 if (info == NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 return BAD_VALUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700151
152 mFormat = format;
153 mFlags = flags;
154 mWidth = w;
155 mHeight = h;
156
157 mInfo = info;
158 memset(info, 0, sizeof(surface_info_t));
159 info->flags = surface_info_t::eNeedNewBuffer;
160
161 // init the buffer, but don't trigger an allocation
162 mBuffer = new Buffer(0, 0, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 return NO_ERROR;
164}
165
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700166status_t LayerBitmap::setSize(uint32_t w, uint32_t h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700168 Mutex::Autolock _l(mLock);
169 if ((w != mWidth) || (h != mHeight)) {
170 mWidth = w;
171 mHeight = h;
172 // this will signal the client that it needs to asks us for a new buffer
173 mInfo->flags = surface_info_t::eNeedNewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 }
175 return NO_ERROR;
176}
177
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700178sp<Buffer> LayerBitmap::allocate()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700180 Mutex::Autolock _l(mLock);
181 sp<Buffer> buffer(mBuffer);
182 const uint32_t w = mWidth;
183 const uint32_t h = mHeight;
Mathias Agopian759fdb22009-07-02 17:33:40 -0700184 if (buffer!=0 && (w != buffer->getWidth() || h != buffer->getHeight())) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700185 surface_info_t* info = mInfo;
186 buffer = new Buffer(w, h, mFormat, mFlags);
187 status_t err = buffer->initCheck();
188 if (LIKELY(err == NO_ERROR)) {
189 info->flags = surface_info_t::eBufferDirty;
190 info->status = NO_ERROR;
191 } else {
192 memset(info, 0, sizeof(surface_info_t));
193 info->status = NO_MEMORY;
194 }
195 mBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700197 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
Mathias Agopian759fdb22009-07-02 17:33:40 -0700200status_t LayerBitmap::free()
201{
202 mBuffer.clear();
203 mWidth = 0;
204 mHeight = 0;
205 return NO_ERROR;
206}
207
208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209// ---------------------------------------------------------------------------
210
211}; // namespace android