blob: dd61e1a3f111ba272e4324eab064139f57417119 [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
Mathias Agopian52212712009-08-11 22:34:02 -070041Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format,
42 uint32_t reqUsage, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043 : SurfaceBuffer(), mInitCheck(NO_INIT), mFlags(flags),
44 mVStride(0)
45{
46 this->format = format;
47 if (w>0 && h>0) {
Mathias Agopian52212712009-08-11 22:34:02 -070048 mInitCheck = initSize(w, h, reqUsage);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049 }
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
Mathias Agopian52212712009-08-11 22:34:02 -070069status_t Buffer::initSize(uint32_t w, uint32_t h, uint32_t reqUsage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070{
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 {
Mathias Agopian52212712009-08-11 22:34:02 -070092 // it's allowed to modify the usage flags here, but generally
93 // the requested flags should be honored.
94 usage = reqUsage | BufferAllocator::USAGE_HW_TEXTURE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070095 }
96
97 err = allocator.alloc(w, h, format, usage, &handle, &stride);
98
99 if (err == NO_ERROR) {
Mathias Agopian50517542009-08-19 17:10:18 -0700100 width = w;
101 height = h;
102 mVStride = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 }
104
105 return err;
106}
107
Mathias Agopian0926f502009-05-04 14:17:04 -0700108status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700110 void* vaddr;
111 status_t res = SurfaceBuffer::lock(usage, &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700112 if (res == NO_ERROR && sur) {
113 sur->version = sizeof(GGLSurface);
114 sur->width = width;
115 sur->height = height;
116 sur->stride = stride;
117 sur->format = format;
118 sur->vstride = mVStride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700119 sur->data = static_cast<GGLubyte*>(vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700120 }
121 return res;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122}
123
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700124// ===========================================================================
125// LayerBitmap
126// ===========================================================================
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128LayerBitmap::LayerBitmap()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700129 : mInfo(0), mWidth(0), mHeight(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131}
132
133LayerBitmap::~LayerBitmap()
134{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135}
136
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137status_t LayerBitmap::init(surface_info_t* info,
138 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700140 if (info == NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return BAD_VALUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700142
143 mFormat = format;
144 mFlags = flags;
145 mWidth = w;
146 mHeight = h;
147
148 mInfo = info;
149 memset(info, 0, sizeof(surface_info_t));
150 info->flags = surface_info_t::eNeedNewBuffer;
151
152 // init the buffer, but don't trigger an allocation
153 mBuffer = new Buffer(0, 0, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154 return NO_ERROR;
155}
156
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700157status_t LayerBitmap::setSize(uint32_t w, uint32_t h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700159 Mutex::Autolock _l(mLock);
160 if ((w != mWidth) || (h != mHeight)) {
161 mWidth = w;
162 mHeight = h;
163 // this will signal the client that it needs to asks us for a new buffer
164 mInfo->flags = surface_info_t::eNeedNewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 }
166 return NO_ERROR;
167}
168
Mathias Agopian52212712009-08-11 22:34:02 -0700169sp<Buffer> LayerBitmap::allocate(uint32_t reqUsage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 Mutex::Autolock _l(mLock);
Mathias Agopian672136f2009-07-28 19:17:54 -0700172 surface_info_t* info = mInfo;
Mathias Agopiana03f7c92009-07-31 16:44:12 -0700173 mBuffer.clear(); // free buffer before allocating a new one
Mathias Agopian52212712009-08-11 22:34:02 -0700174 sp<Buffer> buffer = new Buffer(mWidth, mHeight, mFormat, reqUsage, mFlags);
Mathias Agopian672136f2009-07-28 19:17:54 -0700175 status_t err = buffer->initCheck();
176 if (LIKELY(err == NO_ERROR)) {
177 info->flags = surface_info_t::eBufferDirty;
178 info->status = NO_ERROR;
179 } else {
180 memset(info, 0, sizeof(surface_info_t));
181 info->status = NO_MEMORY;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 }
Mathias Agopian672136f2009-07-28 19:17:54 -0700183 mBuffer = buffer;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700184 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Mathias Agopian759fdb22009-07-02 17:33:40 -0700187status_t LayerBitmap::free()
188{
189 mBuffer.clear();
190 mWidth = 0;
191 mHeight = 0;
192 return NO_ERROR;
193}
194
195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196// ---------------------------------------------------------------------------
197
198}; // namespace android