blob: 4a7c55e56ae997000dff6d39acd960a422cc2517 [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 Agopiancbb288b2009-09-07 16:32:45 -070030#include "Buffer.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#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 Agopiancbb288b2009-09-07 16:32:45 -070041Buffer::Buffer()
42 : SurfaceBuffer(), mInitCheck(NO_ERROR), mVStride(0)
43{
44}
45
Mathias Agopian52212712009-08-11 22:34:02 -070046Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format,
47 uint32_t reqUsage, uint32_t flags)
Mathias Agopiancbb288b2009-09-07 16:32:45 -070048 : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070049{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070050 mInitCheck = initSize(w, h, format, reqUsage, flags);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070051}
52
53Buffer::~Buffer()
54{
55 if (handle) {
Mathias Agopian0926f502009-05-04 14:17:04 -070056 BufferAllocator& allocator(BufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070057 allocator.free(handle);
58 }
59}
60
61status_t Buffer::initCheck() const {
62 return mInitCheck;
63}
64
65android_native_buffer_t* Buffer::getNativeBuffer() const
66{
67 return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this));
68}
69
Mathias Agopiancbb288b2009-09-07 16:32:45 -070070status_t Buffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
71 uint32_t reqUsage, uint32_t flags)
72{
73 if (handle) {
74 BufferAllocator& allocator(BufferAllocator::get());
75 allocator.free(handle);
76 handle = 0;
77 }
78 return initSize(w, h, f, reqUsage, flags);
79}
80
81status_t Buffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
82 uint32_t reqUsage, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070083{
84 status_t err = NO_ERROR;
85
86 BufferAllocator& allocator = BufferAllocator::get();
87
88 /*
89 * buffers used for software rendering, but h/w composition
90 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
91 *
92 * buffers used for h/w rendering and h/w composition
93 * are allocated with HW_RENDER | HW_TEXTURE
94 *
95 * buffers used with h/w rendering and either NPOT or no egl_image_ext
96 * are allocated with SW_READ_RARELY | HW_RENDER
97 *
98 */
99
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700100 if (flags & Buffer::SECURE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 // secure buffer, don't store it into the GPU
102 usage = BufferAllocator::USAGE_SW_READ_OFTEN |
103 BufferAllocator::USAGE_SW_WRITE_OFTEN;
104 } else {
Mathias Agopian52212712009-08-11 22:34:02 -0700105 // it's allowed to modify the usage flags here, but generally
106 // the requested flags should be honored.
107 usage = reqUsage | BufferAllocator::USAGE_HW_TEXTURE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700108 }
109
110 err = allocator.alloc(w, h, format, usage, &handle, &stride);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700111 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700112 this->width = w;
113 this->height = h;
114 this->format = format;
Mathias Agopian50517542009-08-19 17:10:18 -0700115 mVStride = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700116 }
117
118 return err;
119}
120
Mathias Agopian0926f502009-05-04 14:17:04 -0700121status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700122{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700123 void* vaddr;
124 status_t res = SurfaceBuffer::lock(usage, &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700125 if (res == NO_ERROR && sur) {
126 sur->version = sizeof(GGLSurface);
127 sur->width = width;
128 sur->height = height;
129 sur->stride = stride;
130 sur->format = format;
131 sur->vstride = mVStride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700132 sur->data = static_cast<GGLubyte*>(vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700133 }
134 return res;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700135}
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137// ---------------------------------------------------------------------------
138
139}; // namespace android