diff options
author | Mathias Agopian <mathias@google.com> | 2009-10-05 17:07:12 -0700 |
---|---|---|
committer | Mathias Agopian <mathias@google.com> | 2009-10-06 17:00:25 -0700 |
commit | 6950e428feaccc8164b989ef64e771a99948797a (patch) | |
tree | f6520613c5355eef110f3820469d7dc477598629 /opengl | |
parent | 521f4f3af94e3a093a37af887d3e317a526b61a1 (diff) | |
download | frameworks_base-6950e428feaccc8164b989ef64e771a99948797a.zip frameworks_base-6950e428feaccc8164b989ef64e771a99948797a.tar.gz frameworks_base-6950e428feaccc8164b989ef64e771a99948797a.tar.bz2 |
fix [2167050] glTexImage2D code path buggy in SurfaceFlinger
When EGLImage extension is not available, SurfaceFlinger will fallback to using
glTexImage2D and glTexSubImage2D instead, which requires 50% more memory and an
extra copy. However this code path has never been exercised and had some bugs
which this patch fix.
Mainly the scale factor wasn't computed right when falling back on glDrawElements.
We also fallback to this mode of operation if a buffer doesn't have the adequate
usage bits for EGLImage usage.
This changes only code that is currently not executed. Some refactoring was needed to
keep the change clean. This doesn't change anything functionaly.
Diffstat (limited to 'opengl')
-rw-r--r-- | opengl/tests/copybits/Android.mk | 18 | ||||
-rw-r--r-- | opengl/tests/copybits/copybits.cpp | 726 | ||||
-rw-r--r-- | opengl/tests/gralloc/Android.mk | 4 | ||||
-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 | 8 |
8 files changed, 5 insertions, 1179 deletions
diff --git a/opengl/tests/copybits/Android.mk b/opengl/tests/copybits/Android.mk deleted file mode 100644 index d5ded42..0000000 --- a/opengl/tests/copybits/Android.mk +++ /dev/null @@ -1,18 +0,0 @@ -LOCAL_PATH:= $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_SRC_FILES:= \ - copybits.cpp - -LOCAL_SHARED_LIBRARIES := \ - libcutils \ - libEGL \ - libGLESv1_CM \ - libui - -LOCAL_MODULE:= test-opengl-copybits - -LOCAL_MODULE_TAGS := tests - -include $(BUILD_EXECUTABLE) - diff --git a/opengl/tests/copybits/copybits.cpp b/opengl/tests/copybits/copybits.cpp deleted file mode 100644 index 11dfb6e..0000000 --- a/opengl/tests/copybits/copybits.cpp +++ /dev/null @@ -1,726 +0,0 @@ -// Test software OpenGL hardware accelleration using copybits. - -#define LOG_TAG "copybits_test" - -#include <stdio.h> -#include <stdlib.h> -#include <math.h> -#include <sys/types.h> -#include <stdlib.h> -#include <string.h> -#include <sys/time.h> - -#include <ui/PixelFormat.h> - -#include <cutils/log.h> -#include <cutils/native_handle.h> - -#include <utils/Atomic.h> - -#include <private/ui/SurfaceBuffer.h> -#include <pixelflinger/pixelflinger.h> - -#include <hardware/gralloc.h> -#include <hardware/hardware.h> - -#include <ui/FramebufferNativeWindow.h> -#include <ui/EGLUtils.h> - -#define EGL_EGLEXT_PROTOTYPES -#define GL_GLEXT_PROTOTYPES - -#include <EGL/egl.h> -#include <EGL/eglext.h> - -#include <GLES/gl.h> -#include <GLES/glext.h> - -using namespace android; - -EGLDisplay eglDisplay; -EGLSurface eglSurface; -EGLContext eglContext; -GLuint texture; - -hw_module_t const* gralloc_module; -alloc_device_t *sAllocDev; - -#define FIXED_ONE 0x10000 /* 1.0 in 16.16 fixed point. */ - -int init_gl_surface(); -void free_gl_surface(); -void init_scene(); - -int create_physical_texture(); -int readTimer(); - -// =========================================================================== -// Buffer an implementation of android_native_buffer_t -// =========================================================================== - -class NativeBuffer; - -class Buffer : public android::SurfaceBuffer -{ -public: - // creates w * h buffer - Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage); - - // 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; } - - - android_native_buffer_t* getNativeBuffer() const; - - void setPixel(int x, int y, int r, int g, int b, int a); - - status_t lock(GGLSurface* surface, uint32_t usage); - void lock() { - GGLSurface s; - lock(&s, GRALLOC_USAGE_SW_WRITE_OFTEN); - mData = (void*)s.data; - } - -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); - - ssize_t mInitCheck; - void* mData; -}; - -Buffer::Buffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) - : SurfaceBuffer(), mInitCheck(NO_INIT) -{ - this->usage = usage; - this->format = format; - if (w>0 && h>0) { - mInitCheck = initSize(w, h); - } -} - -Buffer::~Buffer() -{ - if (handle) { - sAllocDev->free(sAllocDev, 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::initSize(uint32_t w, uint32_t h) -{ - status_t err = NO_ERROR; - - err = sAllocDev->alloc(sAllocDev, w, h, format, usage, &handle, &stride); - - if (err == NO_ERROR) { - if (err == NO_ERROR) { - width = w; - height = h; - } - } - - 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->data = static_cast<GGLubyte*>(vaddr); - } - return res; -} - - -void Buffer::setPixel(int x, int y, int r, int g, int b, int a) { - if (x < 0 || (unsigned int) x >= width - || y < 0 || (unsigned int) y >= height) { - // clipped - return; - } - int index = stride * y + x; - switch (format) { - case HAL_PIXEL_FORMAT_RGB_565: { - unsigned short val = (unsigned short) ( - ((0x1f & (r >> 3)) << 11) - | ((0x3f & (g >> 2)) << 5) - | (0x1f & (b >> 3))); - ((unsigned short*) mData)[index]= val; - } - break; - case HAL_PIXEL_FORMAT_RGBA_8888: { // ABGR - unsigned int val = (unsigned int) - (((a & 0xff) << 24) - | ((b & 0xff) << 16) - | ((g & 0xff) << 8) - | (r & 0xff)); - ((unsigned int*) mData)[index] = val; - } - break; - default: - // Unsupported pixel format - break; - } -} - - -static void gluLookAt(float eyeX, float eyeY, float eyeZ, - float centerX, float centerY, float centerZ, float upX, float upY, - float upZ) -{ - // See the OpenGL GLUT documentation for gluLookAt for a description - // of the algorithm. We implement it in a straightforward way: - - float fx = centerX - eyeX; - float fy = centerY - eyeY; - float fz = centerZ - eyeZ; - - // Normalize f - float rlf = 1.0f / sqrtf(fx*fx + fy*fy + fz*fz); - fx *= rlf; - fy *= rlf; - fz *= rlf; - - // Normalize up - float rlup = 1.0f / sqrtf(upX*upX + upY*upY + upZ*upZ); - upX *= rlup; - upY *= rlup; - upZ *= rlup; - - // compute s = f x up (x means "cross product") - - float sx = fy * upZ - fz * upY; - float sy = fz * upX - fx * upZ; - float sz = fx * upY - fy * upX; - - // compute u = s x f - float ux = sy * fz - sz * fy; - float uy = sz * fx - sx * fz; - float uz = sx * fy - sy * fx; - - float m[16] ; - m[0] = sx; - m[1] = ux; - m[2] = -fx; - m[3] = 0.0f; - - m[4] = sy; - m[5] = uy; - m[6] = -fy; - m[7] = 0.0f; - - m[8] = sz; - m[9] = uz; - m[10] = -fz; - m[11] = 0.0f; - - m[12] = 0.0f; - m[13] = 0.0f; - m[14] = 0.0f; - m[15] = 1.0f; - - glMultMatrixf(m); - glTranslatef(-eyeX, -eyeY, -eyeZ); -} - -int init_gralloc() { - int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &gralloc_module); - LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); - - if (err == 0) { - gralloc_open(gralloc_module, &sAllocDev); - } - return err; -} - -int init_gl_surface(void) -{ - EGLint numConfigs = 1; - EGLConfig myConfig = {0}; - EGLint attrib[] = - { - EGL_DEPTH_SIZE, 16, - EGL_NONE - }; - - EGLNativeWindowType window = android_createDisplaySurface(); - - printf("init_gl_surface\n"); - if ( (eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY ) - { - printf("eglGetDisplay failed\n"); - return 0; - } - - if ( eglInitialize(eglDisplay, NULL, NULL) != EGL_TRUE ) - { - printf("eglInitialize failed\n"); - return 0; - } - - if ( EGLUtils::selectConfigForNativeWindow(eglDisplay, attrib, window, &myConfig) != 0) - { - printf("EGLUtils::selectConfigForNativeWindow failed\n"); - return 0; - } - - - if ( (eglSurface = eglCreateWindowSurface(eglDisplay, myConfig, - window, 0)) == EGL_NO_SURFACE ) - { - printf("eglCreateWindowSurface failed\n"); - return 0; - } - - if ( (eglContext = eglCreateContext(eglDisplay, myConfig, 0, 0)) == EGL_NO_CONTEXT ) - { - printf("eglCreateContext failed\n"); - return 0; - } - - if ( eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE ) - { - printf("eglMakeCurrent failed\n"); - return 0; - } - -#if EGL_ANDROID_swap_rectangle - eglSetSwapRectangleANDROID(eglDisplay, eglSurface, 0, 0, 320, 480); -#endif - - return 1; -} - -void free_gl_surface(void) -{ - if (eglDisplay != EGL_NO_DISPLAY) - { - eglMakeCurrent( eglDisplay, EGL_NO_SURFACE, - EGL_NO_SURFACE, EGL_NO_CONTEXT ); - eglDestroyContext( eglDisplay, eglContext ); - eglDestroySurface( eglDisplay, eglSurface ); - eglTerminate( eglDisplay ); - eglDisplay = EGL_NO_DISPLAY; - } -} - -void init_scene(void) -{ - glDisable(GL_DITHER); - glEnable(GL_CULL_FACE); - float ratio = 320.0f / 480.0f; - glViewport(0, 0, 320, 480); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glFrustumf(-ratio, ratio, -1, 1, 1, 10); - - glMatrixMode(GL_MODELVIEW); - - glLoadIdentity(); - gluLookAt( - 0, 0, 3, // eye - 0, 0, 0, // center - 0, 1, 0); // up - - glEnable(GL_TEXTURE_2D); -} - -// #define USE_ALPHA_COLOR - -#define USE_GL_REPLACE -//#define USE_GL_MODULATE - -// #define USE_BLEND - -#define USE_565 -// #define USE_8888 - -// #define USE_NEAREST -#define USE_LINEAR - -#define USE_SCALE - -void setSmoothGradient(Buffer* bufferObject) { - int pixels = bufferObject->getHeight() * bufferObject->getWidth(); - int step = 0; - for (unsigned int y = 0; y < bufferObject->getHeight(); y++) { - for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) { - int grey = step * 255 / pixels; - bufferObject->setPixel(x, y, grey, grey, grey, 255); - ++step; - } - } -} - -void setSmoothAlphaGradient(Buffer* bufferObject) { - int pixels = bufferObject->getHeight() * bufferObject->getWidth(); - int step = 0; - for (unsigned int y = 0; y < bufferObject->getHeight(); y++) { - for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) { - int grey = step * 255 / pixels; - bufferObject->setPixel(x, y, 255, 255, 255, grey); - ++step; - } - } -} - -void setOrientedCheckerboard(Buffer* bufferObject) { - bufferObject->setPixel(0, 0, 0, 0, 0, 255); - for(unsigned int x = 1; x < bufferObject->getWidth() ; x++) { - bufferObject->setPixel(x, 0, 0, 255, 0, 255); - } - for (unsigned int y = 1; y < bufferObject->getHeight(); y++) { - for(unsigned int x = 0; x < bufferObject->getWidth() ; x++) { - if ((x ^ y ) & 1) { - bufferObject->setPixel(x, y, 255, 255, 255, 255); - } else { - bufferObject->setPixel(x, y, 255, 0, 0, 255); - } - } - } -} - -int create_physical_texture(unsigned int w, unsigned int h) -{ - -#ifdef USE_565 - PixelFormat format = HAL_PIXEL_FORMAT_RGB_565; -#else - PixelFormat format = HAL_PIXEL_FORMAT_RGBA_8888; -#endif - int usage = GRALLOC_USAGE_SW_READ_OFTEN | - GRALLOC_USAGE_SW_WRITE_OFTEN | - GRALLOC_USAGE_HW_TEXTURE | - GRALLOC_USAGE_HW_2D; /* This is the key to allocating the texture in pmem. */ - int32_t stride; - buffer_handle_t handle; - - // Allocate the hardware buffer - Buffer* bufferObject = new Buffer(w, h, format, usage); - - android_native_buffer_t* buffer = bufferObject->getNativeBuffer(); - - buffer->common.incRef(&buffer->common); - - // create the new EGLImageKHR - EGLint attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_NONE }; - EGLDisplay dpy = eglGetCurrentDisplay(); - EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, - (EGLClientBuffer)buffer, attrs); - if (image == EGL_NO_IMAGE_KHR) { - printf("Could not create an image %d\n", eglGetError()); - return -1; - } - - bufferObject->lock(); - setOrientedCheckerboard(bufferObject); - // setSmoothGradient(bufferObject); - // setSmoothAlphaGradient(bufferObject); - bufferObject->unlock(); - - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); -#ifdef USE_LINEAR - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); -#elif defined(USE_NEAREST) - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); -#endif - -#ifdef USE_GL_REPLACE - glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); -#elif defined(USE_GL_MODULATE) - glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); -#endif - -#ifdef USE_ALPHA_COLOR - glColor4f(1.0f, 1.0f, 1.0f, 0.4f); -#else - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); -#endif - -#ifdef USE_BLEND - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -#endif - return 0; -} - -static const int SCALE_COUNT = 12; - -int scale(int base, int factor) { - static const float kTable[SCALE_COUNT] = { - 0.24f, 0.25f, 0.5f, 0.75f, 1.0f, - 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 5.0f - }; - return base * kTable[factor]; -} - -class Timer { - struct timeval first; - double elapsedSeconds; - -public: - Timer() {} - void start() { - gettimeofday(&first, NULL); - } - - void stop() { - struct timeval second, - elapsed; - gettimeofday(&second, NULL); - - if (first.tv_usec > second.tv_usec) { - second.tv_usec += 1000000; - second.tv_sec--; - } - - elapsedSeconds = (second.tv_sec - first.tv_sec) + - (second.tv_usec - first.tv_usec) / 1000000.0; - } - - double getElapsedSeconds() { - return elapsedSeconds; - } - - double getElapsedMs() { - return elapsedSeconds* 1000.0f; - } -}; - -int testTime() -{ - static const int WIDTH = 320; - static const int HEIGHT = 480; - static const int SCALE = 8; - - if (create_physical_texture(WIDTH, HEIGHT) != 0) { - return -1; - } - // Need to do a dummy eglSwapBuffers first. Don't know why. - glClearColor(0.4, 1.0, 0.4, 0.4); - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - eglSwapBuffers(eglDisplay, eglSurface); - - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - -#if defined(USE_SCALE) - static const int scaleOffset = 0; -#else - static const int scaleOffset = 1; -#endif - printf("ms\n"); - for(int j = 0; j < SCALE; j++) { - int w = WIDTH >> (j + scaleOffset); - int h = HEIGHT >> j; - int cropRect[4] = {0,h,w,-h}; // Left bottom width height. Width and Height can be neg to flip. - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); - Timer timer; - timer.start(); - - int copyCount = 1000; - for (int i = 0; i < copyCount; i++) { - glDrawTexiOES(0, 0, 0, w, h); - } - - timer.stop(); - printf("%g\n", timer.getElapsedMs() / copyCount); - } - - eglSwapBuffers(eglDisplay, eglSurface); - return 0; -} - -int testStretch() -{ - static const int WIDTH = 8; - static const int HEIGHT = 8; - - if (create_physical_texture(WIDTH, HEIGHT) != 0) { - return -1; - } - // Need to do a dummy eglSwapBuffers first. Don't know why. - glClearColor(0.4, 1.0, 0.4, 1.0); - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - eglSwapBuffers(eglDisplay, eglSurface); - - int cropRect[4] = {0,HEIGHT,WIDTH,-HEIGHT}; // Left bottom width height. Width and Height can be neg to flip. - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); - - for(int frame = 0; frame < 2; frame++) { - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - int baseX = 10; - for (int x = 0; x < SCALE_COUNT; x++) { - int baseY = 10; - int width = scale(WIDTH, x); - for (int y = 0; y < SCALE_COUNT; y++) { - int height = scale(HEIGHT, y); - glDrawTexxOES(baseX << 16, baseY << 16, 0, width << 16, height << 16); - baseY += height + 10; - } - baseX += width + 10; - } - - eglSwapBuffers(eglDisplay, eglSurface); - LOGD("wait 1s"); - usleep(1000000); - } - return 0; -} - -int testRot90() -{ - static const int WIDTH = 8; - static const int HEIGHT = 8; - - if (create_physical_texture(WIDTH, HEIGHT) != 0) { - return -1; - } - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrthof(0, 320, 480, 0, 0, 1); - - glMatrixMode(GL_MODELVIEW); - - glLoadIdentity(); - - // Need to do a dummy eglSwapBuffers first. Don't know why. - glClearColor(0.4, 0.4, 0.4, 0.4); - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - eglSwapBuffers(eglDisplay, eglSurface); - - glEnable(GL_TEXTURE_2D); - glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glColor4x(0x10000, 0x10000, 0x10000, 0x10000); - glDisable(GL_BLEND); - glShadeModel(GL_FLAT); - glDisable(GL_DITHER); - glDisable(GL_CULL_FACE); - - for(int frame = 0; frame < 2; frame++) { - LOGD("frame = %d", frame); - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - int baseX = 10; - for (int x = 0; x < SCALE_COUNT; x++) { - int baseY = 10; - int width = scale(WIDTH, x); - for (int y = 0; y < SCALE_COUNT; y++) { - int height = scale(HEIGHT, y); - - // Code copied from SurfaceFlinger LayerBase.cpp - - const GLfixed texCoords[4][2] = { - { 0, 0 }, - { 0, 0x10000 }, - { 0x10000, 0x10000 }, - { 0x10000, 0 } - }; - - GLfixed fx = baseX << 16; - GLfixed fy = baseY << 16; - GLfixed fw = width << 16; - GLfixed fh = height << 16; - - /* - * Vertex pattern: - * (2)--(3) - * |\ | - * | \ | - * | \ | - * | \| - * (1)--(0) - * - */ - - const GLfixed vertices[4][2] = { - {fx + fw, fy}, - {fx, fy}, - {fx, fy + fh}, - {fx + fw, fy + fh} - }; - - static const bool rotate90 = true; - - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(2, GL_FIXED, 0, vertices); - glTexCoordPointer(2, GL_FIXED, 0, texCoords); - - LOGD("testRot90 %d, %d %d, %d", baseX, baseY, width, height); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - - baseY += height + 10; - } - baseX += width + 10; - } - - eglSwapBuffers(eglDisplay, eglSurface); - } - return 0; -} - -int main(int argc, char **argv) -{ - - int q; - int start, end; - - if (init_gralloc()) { - printf("gralloc initialization failed - exiting\n"); - return 0; - } - - printf("Initializing EGL...\n"); - - if(!init_gl_surface()) - { - printf("GL initialisation failed - exiting\n"); - return 0; - } - - init_scene(); - - printf("Start test...\n"); - // testTime(); - testStretch(); - //testRot90(); - free_gl_surface(); - - return 0; -} diff --git a/opengl/tests/gralloc/Android.mk b/opengl/tests/gralloc/Android.mk index a86c8ec..d43c39a 100644 --- a/opengl/tests/gralloc/Android.mk +++ b/opengl/tests/gralloc/Android.mk @@ -2,9 +2,7 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ - gralloc.cpp \ - Buffer.cpp \ - BufferAllocator.cpp + gralloc.cpp LOCAL_SHARED_LIBRARIES := \ libcutils \ diff --git a/opengl/tests/gralloc/Buffer.cpp b/opengl/tests/gralloc/Buffer.cpp deleted file mode 100644 index 3920ff0..0000000 --- a/opengl/tests/gralloc/Buffer.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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 deleted file mode 100644 index 8a32153..0000000 --- a/opengl/tests/gralloc/Buffer.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 deleted file mode 100644 index caf9bec..0000000 --- a/opengl/tests/gralloc/BufferAllocator.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* -** -** 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 deleted file mode 100644 index a279ded..0000000 --- a/opengl/tests/gralloc/BufferAllocator.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -** -** 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 index d989bb2..8987040 100644 --- a/opengl/tests/gralloc/gralloc.cpp +++ b/opengl/tests/gralloc/gralloc.cpp @@ -22,8 +22,8 @@ #include <utils/StopWatch.h> #include <utils/Log.h> -#include "Buffer.h" -#include <ui/BufferMapper.h> +#include <ui/GraphicBuffer.h> +#include <ui/GraphicBufferMapper.h> using namespace android; @@ -46,7 +46,7 @@ int main(int argc, char** argv) memset(temp2, 0, size); - sp<Buffer> buffer = new Buffer(128, 256, HAL_PIXEL_FORMAT_RGBA_8888, + sp<GraphicBuffer> buffer = new GraphicBuffer(128, 256, HAL_PIXEL_FORMAT_RGBA_8888, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN); @@ -57,7 +57,7 @@ int main(int argc, char** argv) } void* vaddr; - buffer->SurfaceBuffer::lock( + buffer->lock( GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN, &vaddr); |