blob: a368ca93aaa6d1f42086fc7691553b2b371f39bb [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 Agopian0926f502009-05-04 14:17:04 -0700133
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134
135// ===========================================================================
136// LayerBitmap
137// ===========================================================================
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139
140LayerBitmap::LayerBitmap()
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700141 : mInfo(0), mWidth(0), mHeight(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}
144
145LayerBitmap::~LayerBitmap()
146{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
148
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700149status_t LayerBitmap::init(surface_info_t* info,
150 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700152 if (info == NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 return BAD_VALUE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700154
155 mFormat = format;
156 mFlags = flags;
157 mWidth = w;
158 mHeight = h;
159
160 mInfo = info;
161 memset(info, 0, sizeof(surface_info_t));
162 info->flags = surface_info_t::eNeedNewBuffer;
163
164 // init the buffer, but don't trigger an allocation
165 mBuffer = new Buffer(0, 0, format, flags);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 return NO_ERROR;
167}
168
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700169status_t LayerBitmap::setSize(uint32_t w, uint32_t h)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700171 Mutex::Autolock _l(mLock);
172 if ((w != mWidth) || (h != mHeight)) {
173 mWidth = w;
174 mHeight = h;
175 // this will signal the client that it needs to asks us for a new buffer
176 mInfo->flags = surface_info_t::eNeedNewBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
178 return NO_ERROR;
179}
180
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700181sp<Buffer> LayerBitmap::allocate()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182{
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700183 Mutex::Autolock _l(mLock);
184 sp<Buffer> buffer(mBuffer);
185 const uint32_t w = mWidth;
186 const uint32_t h = mHeight;
187 if (w != buffer->getWidth() || h != buffer->getHeight()) {
188 surface_info_t* info = mInfo;
189 buffer = new Buffer(w, h, mFormat, mFlags);
190 status_t err = buffer->initCheck();
191 if (LIKELY(err == NO_ERROR)) {
192 info->flags = surface_info_t::eBufferDirty;
193 info->status = NO_ERROR;
194 } else {
195 memset(info, 0, sizeof(surface_info_t));
196 info->status = NO_MEMORY;
197 }
198 mBuffer = buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700200 return buffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201}
202
203// ---------------------------------------------------------------------------
204
205}; // namespace android