diff options
author | Mathias Agopian <mathias@google.com> | 2009-10-01 15:45:37 -0700 |
---|---|---|
committer | Mathias Agopian <mathias@google.com> | 2009-10-01 17:23:05 -0700 |
commit | 15b0ebb925d2d676052a0ef571b435c072b11280 (patch) | |
tree | 1d7222fc9057b0e8d043ed4b39667753c876a451 /opengl | |
parent | 99510681e9f1c76f82719e724990f744cfdbcefd (diff) | |
download | frameworks_base-15b0ebb925d2d676052a0ef571b435c072b11280.zip frameworks_base-15b0ebb925d2d676052a0ef571b435c072b11280.tar.gz frameworks_base-15b0ebb925d2d676052a0ef571b435c072b11280.tar.bz2 |
a simple test app for gralloc
Diffstat (limited to 'opengl')
-rw-r--r-- | opengl/tests/gralloc/Android.mk | 18 | ||||
-rw-r--r-- | opengl/tests/gralloc/Buffer.cpp | 111 | ||||
-rw-r--r-- | opengl/tests/gralloc/Buffer.h | 90 | ||||
-rw-r--r-- | opengl/tests/gralloc/BufferAllocator.cpp | 131 | ||||
-rw-r--r-- | opengl/tests/gralloc/BufferAllocator.h | 96 | ||||
-rw-r--r-- | opengl/tests/gralloc/gralloc.cpp | 110 |
6 files changed, 556 insertions, 0 deletions
diff --git a/opengl/tests/gralloc/Android.mk b/opengl/tests/gralloc/Android.mk new file mode 100644 index 0000000..a86c8ec --- /dev/null +++ b/opengl/tests/gralloc/Android.mk @@ -0,0 +1,18 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + gralloc.cpp \ + Buffer.cpp \ + BufferAllocator.cpp + +LOCAL_SHARED_LIBRARIES := \ + libcutils \ + libutils \ + libui + +LOCAL_MODULE:= test-opengl-gralloc + +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/opengl/tests/gralloc/Buffer.cpp b/opengl/tests/gralloc/Buffer.cpp new file mode 100644 index 0000000..3920ff0 --- /dev/null +++ b/opengl/tests/gralloc/Buffer.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <sys/types.h> + +#include <utils/Errors.h> +#include <utils/Log.h> + +#include <ui/PixelFormat.h> +#include <pixelflinger/pixelflinger.h> + +#include "Buffer.h" +#include "BufferAllocator.h" + + +namespace android { + +// =========================================================================== +// Buffer and implementation of android_native_buffer_t +// =========================================================================== + +Buffer::Buffer() + : SurfaceBuffer(), mInitCheck(NO_ERROR), mVStride(0) +{ +} + +Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, + uint32_t reqUsage, uint32_t flags) + : SurfaceBuffer(), mInitCheck(NO_INIT), mVStride(0) +{ + mInitCheck = initSize(w, h, format, reqUsage, flags); +} + +Buffer::~Buffer() +{ + if (handle) { + BufferAllocator& allocator(BufferAllocator::get()); + allocator.free(handle); + } +} + +status_t Buffer::initCheck() const { + return mInitCheck; +} + +android_native_buffer_t* Buffer::getNativeBuffer() const +{ + return static_cast<android_native_buffer_t*>(const_cast<Buffer*>(this)); +} + +status_t Buffer::reallocate(uint32_t w, uint32_t h, PixelFormat f, + uint32_t reqUsage, uint32_t flags) +{ + if (handle) { + BufferAllocator& allocator(BufferAllocator::get()); + allocator.free(handle); + handle = 0; + } + return initSize(w, h, f, reqUsage, flags); +} + +status_t Buffer::initSize(uint32_t w, uint32_t h, PixelFormat format, + uint32_t reqUsage, uint32_t flags) +{ + status_t err = NO_ERROR; + BufferAllocator& allocator = BufferAllocator::get(); + err = allocator.alloc(w, h, format, reqUsage, &handle, &stride); + if (err == NO_ERROR) { + this->width = w; + this->height = h; + this->format = format; + mVStride = 0; + } + + return err; +} + +status_t Buffer::lock(GGLSurface* sur, uint32_t usage) +{ + void* vaddr; + status_t res = SurfaceBuffer::lock(usage, &vaddr); + if (res == NO_ERROR && sur) { + sur->version = sizeof(GGLSurface); + sur->width = width; + sur->height = height; + sur->stride = stride; + sur->format = format; + sur->vstride = mVStride; + sur->data = static_cast<GGLubyte*>(vaddr); + } + return res; +} + +// --------------------------------------------------------------------------- + +}; // namespace android diff --git a/opengl/tests/gralloc/Buffer.h b/opengl/tests/gralloc/Buffer.h new file mode 100644 index 0000000..8a32153 --- /dev/null +++ b/opengl/tests/gralloc/Buffer.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_LAYER_BITMAP_H +#define ANDROID_LAYER_BITMAP_H + +#include <stdint.h> +#include <sys/types.h> + +#include <hardware/gralloc.h> + +#include <utils/Atomic.h> + +#include <private/ui/SurfaceBuffer.h> +#include <ui/PixelFormat.h> +#include <ui/Rect.h> + +#include <pixelflinger/pixelflinger.h> + +struct android_native_buffer_t; + +namespace android { + +// =========================================================================== +// Buffer +// =========================================================================== + +class NativeBuffer; + +class Buffer : public SurfaceBuffer +{ +public: + enum { + DONT_CLEAR = 0x00000001, + SECURE = 0x00000004 + }; + + Buffer(); + + // creates w * h buffer + Buffer(uint32_t w, uint32_t h, PixelFormat format, + uint32_t reqUsage, uint32_t flags = 0); + + // return status + status_t initCheck() const; + + uint32_t getWidth() const { return width; } + uint32_t getHeight() const { return height; } + uint32_t getStride() const { return stride; } + uint32_t getUsage() const { return usage; } + PixelFormat getPixelFormat() const { return format; } + Rect getBounds() const { return Rect(width, height); } + + status_t lock(GGLSurface* surface, uint32_t usage); + + android_native_buffer_t* getNativeBuffer() const; + + status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, + uint32_t reqUsage, uint32_t flags); + +private: + friend class LightRefBase<Buffer>; + Buffer(const Buffer& rhs); + virtual ~Buffer(); + Buffer& operator = (const Buffer& rhs); + const Buffer& operator = (const Buffer& rhs) const; + + status_t initSize(uint32_t w, uint32_t h, PixelFormat format, + uint32_t reqUsage, uint32_t flags); + + ssize_t mInitCheck; + uint32_t mVStride; +}; + +}; // namespace android + +#endif // ANDROID_LAYER_BITMAP_H diff --git a/opengl/tests/gralloc/BufferAllocator.cpp b/opengl/tests/gralloc/BufferAllocator.cpp new file mode 100644 index 0000000..caf9bec --- /dev/null +++ b/opengl/tests/gralloc/BufferAllocator.cpp @@ -0,0 +1,131 @@ +/* +** +** Copyright 2009, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#include <sys/mman.h> +#include <cutils/ashmem.h> +#include <cutils/log.h> + +#include <utils/Singleton.h> +#include <utils/String8.h> + +#include "BufferAllocator.h" + + +namespace android { +// --------------------------------------------------------------------------- + +ANDROID_SINGLETON_STATIC_INSTANCE( BufferAllocator ) + +Mutex BufferAllocator::sLock; +KeyedVector<buffer_handle_t, BufferAllocator::alloc_rec_t> BufferAllocator::sAllocList; + +BufferAllocator::BufferAllocator() + : mAllocDev(0) +{ + hw_module_t const* module; + int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); + LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); + if (err == 0) { + gralloc_open(module, &mAllocDev); + } +} + +BufferAllocator::~BufferAllocator() +{ + gralloc_close(mAllocDev); +} + +void BufferAllocator::dump(String8& result) const +{ + Mutex::Autolock _l(sLock); + KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); + size_t total = 0; + const size_t SIZE = 512; + char buffer[SIZE]; + snprintf(buffer, SIZE, "Allocated buffers:\n"); + result.append(buffer); + const size_t c = list.size(); + for (size_t i=0 ; i<c ; i++) { + const alloc_rec_t& rec(list.valueAt(i)); + snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u x %4u | %2d | 0x%08x\n", + list.keyAt(i), rec.size/1024.0f, + rec.w, rec.h, rec.format, rec.usage); + result.append(buffer); + total += rec.size; + } + snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f); + result.append(buffer); +} + +static inline uint32_t clamp(uint32_t c) { + return c>0 ? c : 1; +} + +status_t BufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format, + int usage, buffer_handle_t* handle, int32_t* stride) +{ + Mutex::Autolock _l(mLock); + + // make sure to not allocate a 0 x 0 buffer + w = clamp(w); + h = clamp(h); + + // we have a h/w allocator and h/w buffer is requested + status_t err = mAllocDev->alloc(mAllocDev, + w, h, format, usage, handle, stride); + + LOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", + w, h, format, usage, err, strerror(-err)); + + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); + alloc_rec_t rec; + rec.w = w; + rec.h = h; + rec.format = format; + rec.usage = usage; + rec.vaddr = 0; + rec.size = h * stride[0] * bytesPerPixel(format); + list.add(*handle, rec); + } else { + String8 s; + dump(s); + LOGD("%s", s.string()); + } + + return err; +} + +status_t BufferAllocator::free(buffer_handle_t handle) +{ + Mutex::Autolock _l(mLock); + + status_t err = mAllocDev->free(mAllocDev, handle); + LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); + + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList); + list.removeItem(handle); + } + + return err; +} + +// --------------------------------------------------------------------------- +}; // namespace android diff --git a/opengl/tests/gralloc/BufferAllocator.h b/opengl/tests/gralloc/BufferAllocator.h new file mode 100644 index 0000000..a279ded --- /dev/null +++ b/opengl/tests/gralloc/BufferAllocator.h @@ -0,0 +1,96 @@ +/* +** +** Copyright 2009, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#ifndef ANDROID_BUFFER_ALLOCATOR_H +#define ANDROID_BUFFER_ALLOCATOR_H + +#include <stdint.h> + +#include <cutils/native_handle.h> + +#include <utils/Errors.h> +#include <utils/KeyedVector.h> +#include <utils/threads.h> +#include <utils/Singleton.h> + +#include <ui/PixelFormat.h> + +#include <hardware/gralloc.h> + + +namespace android { +// --------------------------------------------------------------------------- + +class String8; + +class BufferAllocator : public Singleton<BufferAllocator> +{ +public: + enum { + USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, + USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, + USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, + USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, + + USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, + USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, + USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, + USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, + + USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, + + USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, + USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, + USAGE_HW_2D = GRALLOC_USAGE_HW_2D, + USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK + }; + + static inline BufferAllocator& get() { return getInstance(); } + + + status_t alloc(uint32_t w, uint32_t h, PixelFormat format, int usage, + buffer_handle_t* handle, int32_t* stride); + + status_t free(buffer_handle_t handle); + + void dump(String8& res) const; + +private: + struct alloc_rec_t { + uint32_t w; + uint32_t h; + PixelFormat format; + uint32_t usage; + void* vaddr; + size_t size; + }; + + static Mutex sLock; + static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList; + + friend class Singleton<BufferAllocator>; + BufferAllocator(); + ~BufferAllocator(); + + mutable Mutex mLock; + alloc_device_t *mAllocDev; +}; + +// --------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_BUFFER_ALLOCATOR_H diff --git a/opengl/tests/gralloc/gralloc.cpp b/opengl/tests/gralloc/gralloc.cpp new file mode 100644 index 0000000..d989bb2 --- /dev/null +++ b/opengl/tests/gralloc/gralloc.cpp @@ -0,0 +1,110 @@ +/* + ** + ** Copyright 2009, The Android Open Source Project + ** + ** Licensed under the Apache License, Version 2.0 (the "License"); + ** you may not use this file except in compliance with the License. + ** You may obtain a copy of the License at + ** + ** http://www.apache.org/licenses/LICENSE-2.0 + ** + ** Unless required by applicable law or agreed to in writing, software + ** distributed under the License is distributed on an "AS IS" BASIS, + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ** See the License for the specific language governing permissions and + ** limitations under the License. + */ + +#define LOG_TAG "StopWatch" + +#include <stdlib.h> +#include <stdio.h> +#include <utils/StopWatch.h> +#include <utils/Log.h> + +#include "Buffer.h" +#include <ui/BufferMapper.h> + +using namespace android; + +void* lamecpy(void* d, void const* s, size_t size) { + char* dst = (char*)d; + char const* src = (char const*)s; + while (size) { + *dst++ = *src++; + size--; + } + return d; +} + +int main(int argc, char** argv) +{ + size_t size = 128*256*4; + void* temp = malloc(size); + void* temp2 = malloc(size); + memset(temp, 0, size); + memset(temp2, 0, size); + + + sp<Buffer> buffer = new Buffer(128, 256, HAL_PIXEL_FORMAT_RGBA_8888, + GRALLOC_USAGE_SW_READ_OFTEN | + GRALLOC_USAGE_SW_WRITE_OFTEN); + + status_t err = buffer->initCheck(); + if (err != NO_ERROR) { + printf("%s\n", strerror(-err)); + return 0; + } + + void* vaddr; + buffer->SurfaceBuffer::lock( + GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, + &vaddr); + + { + StopWatch watch("memset"); + for (int i=0 ; i<10 ; i++) + memset(vaddr, 0, size); + } + + { + StopWatch watch("memcpy baseline"); + for (int i=0 ; i<10 ; i++) + memcpy(temp, temp2, size); + } + + { + StopWatch watch("memcpy from gralloc"); + for (int i=0 ; i<10 ; i++) + memcpy(temp, vaddr, size); + } + + { + StopWatch watch("memcpy into gralloc"); + for (int i=0 ; i<10 ; i++) + memcpy(vaddr, temp, size); + } + + + { + StopWatch watch("lamecpy baseline"); + for (int i=0 ; i<10 ; i++) + lamecpy(temp, temp2, size); + } + + { + StopWatch watch("lamecpy from gralloc"); + for (int i=0 ; i<10 ; i++) + lamecpy(temp, vaddr, size); + } + + { + StopWatch watch("lamecpy into gralloc"); + for (int i=0 ; i<10 ; i++) + lamecpy(vaddr, temp, size); + } + + buffer->unlock(); + + return 0; +} |