blob: 5221fed56f4dbbc0a8768fd60227df8716e5fd42 [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 Agopian310f8da2009-05-22 01:27:01 -070023#include <binder/MemoryBase.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/IMemory.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070025
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/PixelFormat.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <ui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <pixelflinger/pixelflinger.h>
29
Mathias Agopian076b1cc2009-04-10 14:24:30 -070030#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031#include "LayerBitmap.h"
32#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34
35namespace android {
36
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037// ===========================================================================
38// Buffer and implementation of android_native_buffer_t
39// ===========================================================================
40
Fred Quintanab2fd4662009-08-11 20:49:35 -070041Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070042 : SurfaceBuffer(), mInitCheck(NO_INIT), mFlags(flags),
43 mVStride(0)
44{
45 this->format = format;
46 if (w>0 && h>0) {
Fred Quintanab2fd4662009-08-11 20:49:35 -070047 mInitCheck = initSize(w, h);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070048 }
49}
50
51Buffer::~Buffer()
52{
53 if (handle) {
Mathias Agopian0926f502009-05-04 14:17:04 -070054 BufferAllocator& allocator(BufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070055 allocator.free(handle);
56 }
57}
58
59status_t Buffer::initCheck() const {
60 return mInitCheck;
61}
62
63android_native_buffer_t* Buffer::getNativeBuffer() const
64{
65 return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this));
66}
67
Fred Quintanab2fd4662009-08-11 20:49:35 -070068status_t Buffer::initSize(uint32_t w, uint32_t h)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070069{
70 status_t err = NO_ERROR;
71
72 BufferAllocator& allocator = BufferAllocator::get();
73
74 /*
75 * buffers used for software rendering, but h/w composition
76 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
77 *
78 * buffers used for h/w rendering and h/w composition
79 * are allocated with HW_RENDER | HW_TEXTURE
80 *
81 * buffers used with h/w rendering and either NPOT or no egl_image_ext
82 * are allocated with SW_READ_RARELY | HW_RENDER
83 *
84 */
85
86 if (mFlags & Buffer::SECURE) {
87 // secure buffer, don't store it into the GPU
88 usage = BufferAllocator::USAGE_SW_READ_OFTEN |
89 BufferAllocator::USAGE_SW_WRITE_OFTEN;
90 } else {
Fred Quintanab2fd4662009-08-11 20:49:35 -070091 if (mFlags & Buffer::GPU) {
92 // the client wants to do GL rendering
93 usage = BufferAllocator::USAGE_HW_RENDER |
94 BufferAllocator::USAGE_HW_TEXTURE;
95 } else {
96 // software rendering-client, h/w composition
97 usage = BufferAllocator::USAGE_SW_READ_OFTEN |
98 BufferAllocator::USAGE_SW_WRITE_OFTEN |
99 BufferAllocator::USAGE_HW_TEXTURE;
100 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 }
102
103 err = allocator.alloc(w, h, format, usage, &handle, &stride);
104
105 if (err == NO_ERROR) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700106 if (err == NO_ERROR) {
107 width = w;
108 height = h;
109 mVStride = 0;
110 }
111 }
112
113 return err;
114}
115
Mathias Agopian0926f502009-05-04 14:17:04 -0700116status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700117{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700118 void* vaddr;
119 status_t res = SurfaceBuffer::lock(usage, &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700120 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;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700127 sur->data = static_cast<GGLubyte*>(vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700128 }
129 return res;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130}
131
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132// ===========================================================================
133// LayerBitmap
134// ===========================================================================
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136LayerBitmap::LayerBitmap()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 : mInfo(0), mWidth(0), mHeight(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
141LayerBitmap::~LayerBitmap()
142{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700145status_t LayerBitmap::init(surface_info_t* info,
146 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700148 if (info == NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 return BAD_VALUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700150
151 mFormat = format;
152 mFlags = flags;
153 mWidth = w;
154 mHeight = h;
155
156 mInfo = info;
157 memset(info, 0, sizeof(surface_info_t));
158 info->flags = surface_info_t::eNeedNewBuffer;
159
160 // init the buffer, but don't trigger an allocation
161 mBuffer = new Buffer(0, 0, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 return NO_ERROR;
163}
164
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700165status_t LayerBitmap::setSize(uint32_t w, uint32_t h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700167 Mutex::Autolock _l(mLock);
168 if ((w != mWidth) || (h != mHeight)) {
169 mWidth = w;
170 mHeight = h;
171 // this will signal the client that it needs to asks us for a new buffer
172 mInfo->flags = surface_info_t::eNeedNewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 }
174 return NO_ERROR;
175}
176
Fred Quintanab2fd4662009-08-11 20:49:35 -0700177sp<Buffer> LayerBitmap::allocate()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700179 Mutex::Autolock _l(mLock);
Mathias Agopian672136f2009-07-28 19:17:54 -0700180 surface_info_t* info = mInfo;
Mathias Agopiana03f7c92009-07-31 16:44:12 -0700181 mBuffer.clear(); // free buffer before allocating a new one
Fred Quintanab2fd4662009-08-11 20:49:35 -0700182 sp<Buffer> buffer = new Buffer(mWidth, mHeight, mFormat, mFlags);
Mathias Agopian672136f2009-07-28 19:17:54 -0700183 status_t err = buffer->initCheck();
184 if (LIKELY(err == NO_ERROR)) {
185 info->flags = surface_info_t::eBufferDirty;
186 info->status = NO_ERROR;
187 } else {
188 memset(info, 0, sizeof(surface_info_t));
189 info->status = NO_MEMORY;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 }
Mathias Agopian672136f2009-07-28 19:17:54 -0700191 mBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700192 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
Mathias Agopian759fdb22009-07-02 17:33:40 -0700195status_t LayerBitmap::free()
196{
197 mBuffer.clear();
198 mWidth = 0;
199 mHeight = 0;
200 return NO_ERROR;
201}
202
203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204// ---------------------------------------------------------------------------
205
206}; // namespace android