From c63e08216c3efd1f60d44744ee6ca01fdfdc0ab2 Mon Sep 17 00:00:00 2001 From: JosePereira Date: Mon, 11 Jun 2012 20:04:30 +0100 Subject: [PATCH] =?UTF-8?q?added=20gralloc=20grabber=20method=C2=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nativeMethods/gralloc/Android.mk | 18 ++++ nativeMethods/gralloc/README | 4 + nativeMethods/gralloc/gralloc.c | 178 +++++++++++++++++++++++++++++++ nativeMethods/gralloc/gralloc.h | 30 ++++++ 4 files changed, 230 insertions(+) create mode 100755 nativeMethods/gralloc/Android.mk create mode 100644 nativeMethods/gralloc/README create mode 100644 nativeMethods/gralloc/gralloc.c create mode 100644 nativeMethods/gralloc/gralloc.h diff --git a/nativeMethods/gralloc/Android.mk b/nativeMethods/gralloc/Android.mk new file mode 100755 index 0000000..d6317cd --- /dev/null +++ b/nativeMethods/gralloc/Android.mk @@ -0,0 +1,18 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES = \ + gralloc.c + +LOCAL_C_INCLUDES += \ + $(LOCAL_PATH)\ + $(LOCAL_PATH)/.. + +LOCAL_PRELINK_MODULE:=false #override prelink map +LOCAL_MODULE := libdvnc_gralloc_sdk$(PLATFORM_SDK_VERSION) +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_PATH := $(LOCAL_PATH)/../lib + +LOCAL_SHARED_LIBRARIES := libhardware libcutils + +include $(BUILD_SHARED_LIBRARY) diff --git a/nativeMethods/gralloc/README b/nativeMethods/gralloc/README new file mode 100644 index 0000000..68258e1 --- /dev/null +++ b/nativeMethods/gralloc/README @@ -0,0 +1,4 @@ +-This method should work for tegra2 devices which not use the linux framebuffer. +- Make sure you have the tegra read() patch applied on your sources. + +http://nv-tegra.nvidia.com/gitweb/?p=android/platform/hardware/libhardware.git;a=commit;h=9e00d0630e6fd8503602389b18f13a8b220728b1 diff --git a/nativeMethods/gralloc/gralloc.c b/nativeMethods/gralloc/gralloc.c new file mode 100644 index 0000000..ef84b51 --- /dev/null +++ b/nativeMethods/gralloc/gralloc.c @@ -0,0 +1,178 @@ +/* +droid vnc server - Android VNC server +Copyright (C) 2011 Jose Pereira + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "gralloc.h" +#include "../common.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define r(fd, ptr, size) (read((fd), (ptr), (size)) != (int)(size)) +#define w(fd, ptr, size) (write((fd), (ptr), (size)) != (int)(size)) + +struct gralloc_module_t *gralloc; +struct framebuffer_device_t *fbdev = 0; +struct alloc_device_t *allocdev = 0; +buffer_handle_t buf = 0; +unsigned char* data = 0; +int stride; + +static int fill_format(int format) +{ + // bpp, red, green, blue, alpha + + static const int format_map[][9] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0}, // INVALID + {32, 0, 8, 8, 8, 16, 8, 24, 8}, // HAL_PIXEL_FORMAT_RGBA_8888 + {32, 0, 8, 8, 8, 16, 8, 0, 0}, // HAL_PIXEL_FORMAT_RGBX_8888 + {24, 16, 8, 8, 8, 0, 8, 0, 0}, // HAL_PIXEL_FORMAT_RGB_888 + {16, 11, 5, 5, 6, 0, 5, 0, 0}, // HAL_PIXEL_FORMAT_RGB_565 + {32, 16, 8, 8, 8, 0, 8, 24, 8}, // HAL_PIXEL_FORMAT_BGRA_8888 + {16, 11, 5, 6, 5, 1, 5, 0, 1}, // HAL_PIXEL_FORMAT_RGBA_5551 + {16, 12, 4, 8, 4, 4, 4, 0, 4} // HAL_PIXEL_FORMAT_RGBA_4444 + }; + const int *p; + + if (format == 0 || format > HAL_PIXEL_FORMAT_RGBA_4444) + return -ENOTSUP; + + p = format_map[format]; + + screenformat.bitsPerPixel = *(p++); + screenformat.redShift = *(p++); + screenformat.redMax = *(p++); + screenformat.greenShift = *(p++); + screenformat.greenMax = *(p++); + screenformat.blueShift = *(p++); + screenformat.blueMax = *(p++); + screenformat.alphaShift = *(p++); + screenformat.alphaMax = *(p++); + + return 0; +} + +#define CHECK_RV if (rv != 0){close_gralloc();return -1;} +#define CHECK_RV_P if (rv != 0){close_gralloc();return NULL;} + +int init_gralloc() +{ + printf("--Initializing gralloc access method--\n"); + + int linebytes; + int rv; + + rv = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t**)&gralloc); + + CHECK_RV; + + rv = framebuffer_open(&gralloc->common, &fbdev); + + CHECK_RV; + + if (!fbdev->read) { + rv = -ENOTSUP; + close_gralloc(); + return rv; + } + + rv = gralloc_open(&gralloc->common, &allocdev); + + CHECK_RV; + + rv = allocdev->alloc(allocdev, fbdev->width, fbdev->height, + fbdev->format, GRALLOC_USAGE_SW_READ_OFTEN, + &buf, &stride); + + + rv = fbdev->read(fbdev, buf); + + CHECK_RV; + + rv = gralloc->lock(gralloc, buf, GRALLOC_USAGE_SW_READ_OFTEN, 0, 0, + fbdev->width, fbdev->height, (void**)&data); + CHECK_RV; + + rv = fill_format(fbdev->format); + + CHECK_RV; + + stride *= (screenformat.bitsPerPixel >> 3); + linebytes = fbdev->width * (screenformat.bitsPerPixel >> 3); + screenformat.size = linebytes * fbdev->height; + screenformat.width = fbdev->width; + screenformat.height = fbdev->height; + + // point of no return: don't attempt alternative means of reading + // after this + rv = 0; + + L("Stride=%d Linebytes=%d %p\n",stride,linebytes,fbdev->setUpdateRect); + + if (data) + gralloc->unlock(gralloc, buf); + + L("Copy %d bytes\n",screenformat.size); + + L("Returning rv=%d\n",rv); + return rv; +} + + +void close_gralloc() +{ + if (buf) + allocdev->free(allocdev, buf); + if (allocdev) + gralloc_close(allocdev); + if (fbdev) + framebuffer_close(fbdev); +} + +screenFormat getscreenformat_gralloc() +{ + return screenformat; +} + +unsigned char *readfb_gralloc () +{ + int rv; + + rv = fbdev->read(fbdev, buf); + + CHECK_RV_P; + + rv = gralloc->lock(gralloc, buf, GRALLOC_USAGE_SW_READ_OFTEN, 0, 0, + fbdev->width, fbdev->height, (void**)&data); + CHECK_RV_P; + + if (data) + gralloc->unlock(gralloc, buf); + + return data; +} + + diff --git a/nativeMethods/gralloc/gralloc.h b/nativeMethods/gralloc/gralloc.h new file mode 100644 index 0000000..392c835 --- /dev/null +++ b/nativeMethods/gralloc/gralloc.h @@ -0,0 +1,30 @@ +/* +droid vnc server - Android VNC server +Copyright (C) 2009 Jose Pereira + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef GRALLOC_H +#define GRALLOC_H + +#include "screenFormat.h" + +int init_gralloc(); +unsigned char *readfb_gralloc(); +void close_gralloc(); +screenFormat getscreenformat_gralloc(); + +#endif