blob: 6190cd82b98abddf296f2d7f47d663ca090423f5 [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 Agopian076b1cc2009-04-10 14:24:30 -070023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <ui/PixelFormat.h>
25#include <pixelflinger/pixelflinger.h>
26
Mathias Agopiancbb288b2009-09-07 16:32:45 -070027#include "Buffer.h"
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include "BufferAllocator.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
30namespace android {
31
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032// ===========================================================================
33// Buffer and implementation of android_native_buffer_t
34// ===========================================================================
35
Mathias Agopiancbb288b2009-09-07 16:32:45 -070036Buffer::Buffer()
37 : SurfaceBuffer(), mInitCheck(NO_ERROR), mVStride(0)
38{
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 Agopiancbb288b2009-09-07 16:32:45 -070043 : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070044{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070045 mInitCheck = initSize(w, h, format, reqUsage, flags);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046}
47
48Buffer::~Buffer()
49{
50 if (handle) {
Mathias Agopian0926f502009-05-04 14:17:04 -070051 BufferAllocator& allocator(BufferAllocator::get());
Mathias Agopian076b1cc2009-04-10 14:24:30 -070052 allocator.free(handle);
53 }
54}
55
56status_t Buffer::initCheck() const {
57 return mInitCheck;
58}
59
60android_native_buffer_t* Buffer::getNativeBuffer() const
61{
62 return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this));
63}
64
Mathias Agopiancbb288b2009-09-07 16:32:45 -070065status_t Buffer::reallocate(uint32_t w, uint32_t h, PixelFormat f,
66 uint32_t reqUsage, uint32_t flags)
67{
68 if (handle) {
69 BufferAllocator& allocator(BufferAllocator::get());
70 allocator.free(handle);
71 handle = 0;
72 }
73 return initSize(w, h, f, reqUsage, flags);
74}
75
76status_t Buffer::initSize(uint32_t w, uint32_t h, PixelFormat format,
77 uint32_t reqUsage, uint32_t flags)
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078{
79 status_t err = NO_ERROR;
80
81 BufferAllocator& allocator = BufferAllocator::get();
82
83 /*
84 * buffers used for software rendering, but h/w composition
85 * are allocated with SW_READ_OFTEN | SW_WRITE_OFTEN | HW_TEXTURE
86 *
87 * buffers used for h/w rendering and h/w composition
88 * are allocated with HW_RENDER | HW_TEXTURE
89 *
90 * buffers used with h/w rendering and either NPOT or no egl_image_ext
91 * are allocated with SW_READ_RARELY | HW_RENDER
92 *
93 */
94
Mathias Agopiancbb288b2009-09-07 16:32:45 -070095 if (flags & Buffer::SECURE) {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 // secure buffer, don't store it into the GPU
97 usage = BufferAllocator::USAGE_SW_READ_OFTEN |
98 BufferAllocator::USAGE_SW_WRITE_OFTEN;
99 } else {
Mathias Agopian52212712009-08-11 22:34:02 -0700100 // it's allowed to modify the usage flags here, but generally
101 // the requested flags should be honored.
102 usage = reqUsage | BufferAllocator::USAGE_HW_TEXTURE;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 }
Mathias Agopian816d7d02009-09-14 18:10:30 -0700104
105 if (format == PIXEL_FORMAT_RGBX_8888)
106 format = PIXEL_FORMAT_RGBA_8888;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700107
108 err = allocator.alloc(w, h, format, usage, &handle, &stride);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700109 if (err == NO_ERROR) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700110 this->width = w;
111 this->height = h;
112 this->format = format;
Mathias Agopian50517542009-08-19 17:10:18 -0700113 mVStride = 0;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700114 }
115
116 return err;
117}
118
Mathias Agopian0926f502009-05-04 14:17:04 -0700119status_t Buffer::lock(GGLSurface* sur, uint32_t usage)
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700120{
Mathias Agopiane71212b2009-05-05 00:37:46 -0700121 void* vaddr;
122 status_t res = SurfaceBuffer::lock(usage, &vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700123 if (res == NO_ERROR && sur) {
124 sur->version = sizeof(GGLSurface);
125 sur->width = width;
126 sur->height = height;
127 sur->stride = stride;
128 sur->format = format;
129 sur->vstride = mVStride;
Mathias Agopiane71212b2009-05-05 00:37:46 -0700130 sur->data = static_cast<GGLubyte*>(vaddr);
Mathias Agopian0926f502009-05-04 14:17:04 -0700131 }
132 return res;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133}
134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135// ---------------------------------------------------------------------------
136
137}; // namespace android