From f18bdb34f858f6b779ac4a16734c03fa6f9db3f2 Mon Sep 17 00:00:00 2001 From: Robert Fekete Date: Tue, 23 Feb 2010 10:33:24 +0100 Subject: new b2r2lib --- src/b2r2_blt.h | 531 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/blt_b2r2.c | 256 +++++++++++++++++++++++++++ 2 files changed, 787 insertions(+) create mode 100755 src/b2r2_blt.h create mode 100644 src/blt_b2r2.c (limited to 'src') diff --git a/src/b2r2_blt.h b/src/b2r2_blt.h new file mode 100755 index 0000000..2c63b91 --- /dev/null +++ b/src/b2r2_blt.h @@ -0,0 +1,531 @@ +/* + * Copyright (C) ST-Ericsson AB 2009 - All rights reserved + * Reproduction and Communication of this document is strictly prohibited + * unless specifically authorized in writing by ST-Ericsson + * + * \file b2r2_blt.h + * \brief B2R2 Blit kernel API + * \author ST-Ericsson + * + */ + +#ifndef _LINUX_VIDEO_B2R2_BLT_H +#define _LINUX_VIDEO_B2R2_BLT_H + +#include + +/** + * struct b2r2_blt_rect - Specifies a B2R2 rectangle + * + * @left: X-coordinate of top left corner + * @top: Y-coordinate of top left corner + * @width: Rectangle width. Must be >= 0. + * @height: Rectangle height. Must be >= 0. + */ +struct b2r2_blt_rect { + __s32 x; + __s32 y; + __s32 width; + __s32 height; +}; + +/** + * enum b2r2_blt_fmt - Defines the available B2R2 buffer formats + * + * Inspired by Khronos OpenMAX, please see + * OpenMAX IL specification for detailed descriptions of the formats + * + * @B2R2_BLT_FMT_UNUSED: Placeholder value when format is unknown, + * or specified using a vendor-specific means. + * @B2R2_BLT_FMT_16_BIT_ARGB4444: 16 bits per pixel ARGB format with colors + * stored as Alpha 15:12, Red 11:8, Green 7:4, and Blue 3:0. + * @B2R2_BLT_FMT_16_BIT_ARGB1555: 16 bits per pixel ARGB format with colors + * stored as Alpha 15, Red 14:10, Green 9:5, and Blue 4:0. + * @B2R2_BLT_FMT_16_BIT_RGB565: 16 bits per pixel RGB format with colors + * stored as Red 15:11, Green 10:5, and Blue 4:0. + * @B2R2_BLT_FMT_24_BIT_RGB888: 24 bits per pixel RGB format with colors + * stored as Red 23:16, Green 15:8, and Blue 7:0. + * @B2R2_BLT_FMT_32_BIT_ARGB8888: 32 bits per pixel ARGB format with colors + * stored as Alpha 31:24, Red 23:16, Green 15:8, and Blue 7:0. + * @B2R2_BLT_FMT_YUV420_PACKED_PLANAR: YUV planar format, organized with + * three separate planes for each color component, namely Y, U, and V. + * U and V pixels are sub-sampled by a factor of two both horizontally and + * vertically. The buffer shall contain a plane of Y, U, and V data in this + * order + * @B2R2_BLT_FMT_YUV422_PACKED_PLANAR: YUV planar format, organized with + * three separate planes for each color component, namely Y, U, and V. + * U and V pixels are subsampled by a factor of two horizontally. + * The buffer shall contain a plane of Y, U, and V data in this order. + * @B2R2_BLT_FMT_Y_CB_Y_CR: 16 bits per pixel YUV interleaved format organized + * as YUYV (i.e., YCbYCr). + * (Corresponds to YUV422 interleaved) + * @B2R2_BLT_FMT_CB_Y_CR_Y: 16 bits per pixel YUV interleaved format organized + * as UYVY (i.e., CbYCrY). + * (Corresponds to YUV422R) + * @B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR: YUV planar format, organized with + * a first plane containing Y pixels, and a second plane containing U and V + * pixels interleaved with the first U value first. U and V pixels are + * sub-sampled by a factor of two both horizontally and vertically. The buffer + * shall contain a plane of Y, U and V data. + * (Same as B2R2 420 Raster 2 buffer - 420 R2B) + * @B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR: YUV planar format, organized with + * a first plane containing Y pixels, and a second plane containing U and V + * pixels interleaved with the first U value first. U and V pixels are + * sub-sampled by a factor of two horizontally. The buffer shall contain a + * plane of Y, U and V data. + * (Same as B2R2 422 Raster 2 buffer - 422 R2B) + * @B2R2_BLT_FMT_32_BIT_ABGR8888: 32 bits per pixel ABGR format with colors + * stored as Alpha 31:24,Blue 23:16, Green 15:8, and Red 7:0. + * @B2R2_BLT_FMT_24_BIT_ARGB8565: 24 bits per pixel ARGB format with colors + * stored as Alpha 23:16, Red 15:11, Green 10:5, and Blue 4:0. + * @B2R2_BLT_FMT_24_BIT_YUV888: 24 bits per pixel YUV format with colors + * stored as Y 23:16, U 15:8, and V 7:0. + * @B2R2_BLT_FMT_32_BIT_AYUV8888: 32 bits per pixel AYUV format with colors + * stored as Alpha 31:24, Y 23:16, U 15:8, and V 7:0. + * @B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE: Nomadik YUV 420 macro block + * format, see B2R2 spec for details + * @B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE: Nomadik YUV 422 macro block + * format, see B2R2 spec for details + * @B2R2_BLT_FMT_1_BIT_A1: 1 bit per pixel A format, 1 bit alpha + * @B2R2_BLT_FMT_8_BIT_A8: 8 bit per pixel A format, 8 bit alpha + */ +enum b2r2_blt_fmt { + B2R2_BLT_FMT_UNUSED = 0, + B2R2_BLT_FMT_16_BIT_ARGB4444 = 4, + B2R2_BLT_FMT_16_BIT_ARGB1555 = 5, + B2R2_BLT_FMT_16_BIT_RGB565 = 6, // + B2R2_BLT_FMT_24_BIT_RGB888 = 11, + B2R2_BLT_FMT_32_BIT_ARGB8888 = 16, + B2R2_BLT_FMT_YUV420_PACKED_PLANAR = 20, + B2R2_BLT_FMT_YUV422_PACKED_PLANAR = 23, + B2R2_BLT_FMT_Y_CB_Y_CR = 25, + B2R2_BLT_FMT_CB_Y_CR_Y = 27, + B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR=39, + B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR=40, + /* Extensions, non OpenMAX formats */ + B2R2_BLT_FMT_32_BIT_ABGR8888 = 0x7F000000, /* OpenMax vendor start */ + B2R2_BLT_FMT_24_BIT_ARGB8565 = 0x7F000001, + B2R2_BLT_FMT_24_BIT_YUV888 = 0x7F000002, + B2R2_BLT_FMT_32_BIT_AYUV8888 = 0x7F000003, + B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE = 0x7F000004, + B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE = 0x7F000005, + B2R2_BLT_FMT_1_BIT_A1 = 0x7F000006, + B2R2_BLT_FMT_8_BIT_A8 = 0x7F000007, +}; + +/** + * enum b2r2_blt_ptr_type - Specifies a B2R2 buffer pointer type + * + * @B2R2_BLT_PTR_NONE: + * No pointer (NULL). E.g. src fill. + * @B2R2_BLT_PTR_VIRTUAL: + * Use offset as a userspace virtual address + * @B2R2_BLT_PTR_PHYSICAL: + * Use offset as a physical address + * @B2R2_BLT_PTR_FD_OFFSET: + * Use fd + offset to determine the userspace virtual address. + * @B2R2_BLT_PTR_GEM: + * Use fd + offset as a GEM address + */ +enum b2r2_blt_ptr_type { + B2R2_BLT_PTR_NONE, + B2R2_BLT_PTR_VIRTUAL, + B2R2_BLT_PTR_PHYSICAL, + B2R2_BLT_PTR_FD_OFFSET, + B2R2_BLT_PTR_GEM, +}; + +/** + * struct b2r2_blt_buf - Specifies a B2R2 buffer pointer + * + * @type: Buffer pointer type + * @fd: File descriptor (e.g. file handle to pmem or fb device) + * @offset: Offset in file or userspace virtual address + * @len: Size of buffer in bytes + * @bits: Pointer to the bitmap data. This field can be used to specify + * an alternative way to access the buffer. Whenever the 'bits' pointer + * is set to non-NULL, the underlying implementation + * is free to decide whether or not to use it in favor of 'fd' and/or 'offset'. + */ +struct b2r2_blt_buf { + enum b2r2_blt_ptr_type type; + __s32 fd; + __u32 offset; + __u32 len; + void *bits; +}; + + +/** + * struct b2r2_blt_img - Specifies a B2R2 image + * + * @fmt: Pixel format of image + * @buf: Pixel buffer + * @width: Width in pixels + * @height: Height in pixels + * @pitch: Pitch in bytes (from start of one line to start of next) + */ +struct b2r2_blt_img { + enum b2r2_blt_fmt fmt; + struct b2r2_blt_buf buf; + __s32 width; + __s32 height; + __u32 pitch; +}; + + +/** + * enum b2r2_blt_transform- Specifies rotation and flipping, mutually exclusive + * @B2R2_BLT_TRANSFORM_NONE: + * No rotation or flip + * @B2R2_BLT_TRANSFORM_FLIP_H + * Flip horizontally + * @B2R2_BLT_TRANSFORM_FLIP_V + * Flip vertically + * @B2R2_BLT_TRANSFORM_CCW_ROT_90 + * Rotate 90 degrees counter clockwise + * @B2R2_BLT_TRANSFORM_CCW_ROT_180 + * Rotate 180 degrees (same as flip horizontally together with + * flip vertically) + * @B2R2_BLT_TRANSFORM_CCW_ROT_270 + * Rotate 270 degrees counter clockwise + * @B2R2_BLT_TRANSFORM_FLIP_H_CCW_ROT_90 + * Flip horizontally and then rotate 90 degrees counter clockwise + * @B2R2_BLT_TRANSFORM_FLIP_V_CCW_ROT_90 + * Flip vertically and then rotate 90 degrees counter clockwise + */ +enum b2r2_blt_transform { + B2R2_BLT_TRANSFORM_NONE = 0, + B2R2_BLT_TRANSFORM_FLIP_H = 1, + B2R2_BLT_TRANSFORM_FLIP_V = 2, + B2R2_BLT_TRANSFORM_CCW_ROT_90 = 4, + B2R2_BLT_TRANSFORM_CCW_ROT_180 = 3, + B2R2_BLT_TRANSFORM_CCW_ROT_270 = 7, + B2R2_BLT_TRANSFORM_FLIP_H_CCW_ROT_90 = 5, + B2R2_BLT_TRANSFORM_FLIP_V_CCW_ROT_90 = 6, +}; + + +/** + * enum b2r2_blt_flag - Flags that controls the B2R2 request + * + * Can be combined. + * + * @B2R2_BLT_FLAG_ASYNCH: + * Asynchronous request. b2r2_blt will returns when the request + * has been queued. + * @B2R2_BLT_FLAG_DRY_RUN: + * Dry run, just to check if request can be performed. + * @B2R2_BLT_FLAG_PER_PIXEL_ALPHA_BLEND: + * Enable per pixel alpha blend + * @B2R2_BLT_FLAG_GLOBAL_ALPHA_BLEND: + * Enable global alpha blend (alpha value in global_alpha) + * @B2R2_BLT_FLAG_SOURCE_COLOR_KEY: + * Enable source color key (color in src_color). Color should be in raw + * format. + * B2R2_BLT_FLAG_SOURCE_COLOR_KEY, B2R2_BLT_FLAG_SOURCE_FILL and + * B2R2_BLT_FLAG_SOURCE_FILL_RAW cannot be specified at the same time. + * B2R2_BLT_FLAG_SOURCE_COLOR_KEY and B2R2_BLT_FLAG_DEST_COLOR_KEY cannot be + * specified at the same time. + * @B2R2_BLT_FLAG_SOURCE_FILL: + * Enable ARGB/AYUV source fill (color in src_color). Which of ARGB and AYUV + * is determined by the destination format. + * B2R2_BLT_FLAG_SOURCE_COLOR_KEY, B2R2_BLT_FLAG_SOURCE_FILL and + * B2R2_BLT_FLAG_SOURCE_FILL_RAW cannot be specified at the same time + * @B2R2_BLT_FLAG_SOURCE_FILL_RAW: + * Enable raw color source fill (color in src_color) + * B2R2_BLT_FLAG_SOURCE_COLOR_KEY, B2R2_BLT_FLAG_SOURCE_FILL and + * B2R2_BLT_FLAG_SOURCE_FILL_RAW cannot be specified at the same time + * @B2R2_BLT_FLAG_DEST_COLOR_KEY: + * Enable dest color key (color in dst_color). Color in raw format. + * @B2R2_BLT_FLAG_SRC_IS_NOT_PREMULT: + * Source color not premultiplied (Valid for alpha formats only). + * @B2R2_BLT_FLAG_DITHER: + * Enable dithering + * @B2R2_BLT_FLAG_BLUR: + * Enable blur + * @B2R2_BLT_FLAG_SOURCE_MASK: + * Enable source mask + * @B2R2_BLT_FLAG_DESTINATION_CLIP: + * Enable destination clip rectangle + * @B2R2_BLT_FLAG_INHERIT_PRIO + * Inherit process priority + * @B2R2_BLT_FLAG_REPORT_WHEN_DONE + * Report through b2r2_blt file when done. A b2r2_blt_report structure is + * read. Use poll() or select() if anything to read. (i.e. to help user space + * to implement callback functionality) + * @B2R2_BLT_FLAG_REPORT_PERFORMANCE + * Include performance data in the report structure + */ +enum b2r2_blt_flag { + B2R2_BLT_FLAG_ASYNCH = 0x1, + B2R2_BLT_FLAG_DRY_RUN = 0x2, + B2R2_BLT_FLAG_PER_PIXEL_ALPHA_BLEND = 0x4, + B2R2_BLT_FLAG_GLOBAL_ALPHA_BLEND = 0x8, + B2R2_BLT_FLAG_SOURCE_COLOR_KEY = 0x10, + B2R2_BLT_FLAG_SOURCE_FILL = 0x20, + B2R2_BLT_FLAG_SOURCE_FILL_RAW = 0x40, + B2R2_BLT_FLAG_DEST_COLOR_KEY = 0x80, + B2R2_BLT_FLAG_SRC_IS_NOT_PREMULT = 0x100, + B2R2_BLT_FLAG_DITHER = 0x200, + B2R2_BLT_FLAG_BLUR = 0x400, + B2R2_BLT_FLAG_SOURCE_MASK = 0x800, + B2R2_BLT_FLAG_DESTINATION_CLIP = 0x1000, + B2R2_BLT_FLAG_INHERIT_PRIO = 0x2000, + B2R2_BLT_FLAG_REPORT_WHEN_DONE = 0x20000000, + B2R2_BLT_FLAG_REPORT_PERFORMANCE = 0x40000000, +}; + + +/** + * struct b2r2_blt_req - Specifies a request to B2R2 + * + * @size: Size of this structure. Used for versioning. MUST be specified. + * @flags: Flags that control the B2R2 request ORed together + * @tfm: How source should be flipped and rotated when blitting + * @prio: Priority (-20 to 19). Inherits process prio + * if B2R2_BLT_FLAG_INHERIT_PRIO. Given priority is mapped onto B2R2. + * TBD: How? + * @src_img: Source image. Not used if source fill. + * @src_mask: Source mask. Not used if source fill. + * @src_rect: Source area to be blitted. + * @src_color: Source fill color or color key + * @dst_img: Destination image. Not used if source fill. + * @dst_rect: Destination area to be blitted to. + * @dst_color: Destination color key + * @dst_clip_rect: Destination clip rectangle. + * @global_alpha: Global alpha value (0 - 255) + * @report1: Data 1 to report back when request is done. + * See struct b2r2_blt_report. + * @report2: Data 2 to report back when request is done. + * See struct b2r2_blt_report. + * + */ +struct b2r2_blt_req { + __u32 size; + enum b2r2_blt_flag flags; + enum b2r2_blt_transform transform; + __s32 prio; + struct b2r2_blt_img src_img; + struct b2r2_blt_img src_mask; + struct b2r2_blt_rect src_rect; + __u32 src_color; + struct b2r2_blt_img dst_img; + struct b2r2_blt_rect dst_rect; + struct b2r2_blt_rect dst_clip_rect; + __u32 dst_color; + __u8 global_alpha; + __u32 report1; + __u32 report2; +}; + +/** + * enum b2r2_blt_cap - Capabilities that can be queried for. + * + * Capabilities can be queried for a specific format or for formats in + * general. To query for capabilities in general, specify BLT_FMT_UNUSED + * as format. + * + * B2R2_BLT_CAP_UNUSED: Unused/unspecified capability + * B2R2_BLT_CAP_FMT_SOURCE: Is format supported as source? + * B2R2_BLT_CAP_FMT_SOURCE_MASK: Is format supported as source mask? + * B2R2_BLT_CAP_FMT_DEST: Is format supported as dest? + * B2R2_BLT_CAP_PER_PIXEL_ALPHA_BLEND: Is per pixel alpha blending supported + * with format as source + * B2R2_BLT_CAP_GLOBAL_ALPHA_BLEND: Is per global alpha blending supported + * with format as source + * B2R2_BLT_CAP_SOURCE_COLOR_KEY: Is source color key supported with format as + * source + * B2R2_BLT_CAP_SOURCE_FILL: Is source fill supported with format as source + * B2R2_BLT_CAP_SOURCE_FILL_RAW: Is source fill raw supported with format as + * dest + * B2R2_BLT_CAP_DEST_COLOR_KEY: Is dest color key supported with format as dest + * B2R2_BLT_CAP_DITHER: Is dithering supported with format as source + * B2R2_BLT_CAP_BLUR: Is blur supported with format as source + * B2R2_BLT_CAP_MINIFICATION_LIMIT: Minification limit (copybit support) + * B2R2_BLT_CAP_MAGNIFICATION_LIMIT: Magnification limit (copybit support) + * B2R2_BLT_CAP_SCALING_FRAC_BITS: Number of scaling fractional bits (copybit + * support) + * B2R2_BLT_CAP_ROTATION_STEP_DEG: Supported rotation step in degrees (copybit + * support) + */ + +enum b2r2_blt_cap { + B2R2_BLT_CAP_UNUSED = 0, + /** + * @brief Is format supported as source. + */ + B2R2_BLT_CAP_FMT_SOURCE, + /** + * @brief Is format supported as source mask + */ + B2R2_BLT_CAP_FMT_SOURCE_MASK, + /** + * @brief Is format supported as destination + */ + B2R2_BLT_CAP_FMT_DEST, + /** + * @brief Is per pixel alpha blending supported with format as source + */ + B2R2_BLT_CAP_PER_PIXEL_ALPHA_BLEND, + /** + * @brief Is global alpha blending supported with format as source + */ + B2R2_BLT_CAP_GLOBAL_ALPHA_BLEND, + /** + * @brief Is source color key supported with format as source + */ + B2R2_BLT_CAP_SOURCE_COLOR_KEY, + /** + * @brief Is source fill supported with format as source + */ + B2R2_BLT_CAP_SOURCE_FILL, + /** + * @brief Is source fill raw supported with format as dest + */ + B2R2_BLT_CAP_SOURCE_FILL_RAW, + /** + * @brief Is dest color key supported with format as dest + */ + B2R2_BLT_CAP_DEST_COLOR_KEY, + /** + * @brief Is dithering supported with format as source + */ + B2R2_BLT_CAP_DITHER, + /** + * @brief Is blur supported with format as source + */ + B2R2_BLT_CAP_BLUR, + /** + * @brief Minification limit (copybit support) + */ + B2R2_BLT_CAP_MINIFICATION_LIMIT, + /** + * @brief Magnification limit (copybit support) + */ + B2R2_BLT_CAP_MAGNIFICATION_LIMIT, + /** + * @brief Number of scaling fractional bits (copybit support) + */ + B2R2_BLT_CAP_SCALING_FRAC_BITS, + /** + * @brief Supported rotation step in degrees (copybit support) + */ + B2R2_BLT_CAP_ROTATION_STEP_DEG, +}; + +/** + * struct b2r2_blt_query_cap - Query B2R2 capabilities + * + * fmt: Format to query capabilities for or BLT_FMT_UNUSED for all + * cap: Capability to query for + * result: Returned capability. Interpretaion of this variable varies + * with the capability queried + */ +struct b2r2_blt_query_cap { + enum b2r2_blt_fmt fmt; + enum b2r2_blt_cap cap; + __u32 result; +}; + +/** + * struct b2r2_blt_report - Report from B2R2 driver back to user space + * + * This structure can be read from B2R2 driver if B2R2_BLT_FLAG_REPORT_WHEN_DONE + * flag was specified when the request was issued. + * + * @request_id: The id for the request, same as reported from blt_request + * @report1: Client data specified in struct blt_request + * @report2: Client data specified in struct blt_request + * @usec_elapsed: Number of microseconds needed to perform this blit + * if B2R2_BLT_FLAG_REPORT_PERFORMANCE was specified when the + * request was issued. + * + */ +struct b2r2_blt_report { + __u32 request_id; + __u32 report1; + __u32 report2; + __u32 usec_elapsed; +}; + +/** + * B2R2 BLT driver is used in the following way: + * + * Obtain a file descriptor to the driver: + * fd = open("/dev/b2r2_blt", O_RDWR); + * + * Issue requests: + * struct b2r2_blt_request blt_request; + * blt_request.size = sizeof(blt_request); + * ... Fill request with data... + * + * request_id = ioctl(fd, B2R2_BLT_IOC, (__u32) &blt_request); + * + * Wait for a request to finish + * ret = ioctl(fd, B2R2_BLT_SYNCH_IOC, (__u32) request_id); + * + * Wait for all requests from this context to finish + * ret = ioctl(fd, B2R2_BLT_SYNCH_IOC, (__u32) 0); + * + * Wait indefinitely for report data from driver: + * pollfd.fd = fd + * pollfd.events = 0xFFFFFFFF; // ??? + * pollfd.revents = 0; + * ret = poll(&pollfd, 1, -1); // select() can be used too + * + * Read report data from driver + * struct b2r2_blt_report blt_report; + * + * nread = read(fd, &blt_report, sizeof(blt_report)); + * + * Close the driver + * close(fd); + */ + +/* B2R2 BLT IOCTLS */ + +/** + * B2R2_BLT_IOC_MAGIC is ioctl type group for B2R2 driver + */ +#define B2R2_BLT_IOC_MAGIC 0xb2 + +/** + * The B2R2_BLT_IOC ioctl adds a blit request to B2R2. + * + * The ioctl returns when the blit has been performed if not + * asynchronous execution has been specified. If asynchronous, + * control is returned as soon as the request has been queued. + * + * Supplied parameter shall be a pointer to a struct b2r2_blt_req. + * + * Returns an unique request id if >= 0, else a negative error code. + * This request id can be waited for using B2R2_BLT_SYNC_IOC. + * Return values: -ESOMERROR Description of an error + */ +#define B2R2_BLT_IOC _IOW(B2R2_BLT_IOC_MAGIC, 1, struct b2r2_blt_req) + +/** + * The B2R2_BLT_SYNC_IOC waits for all or a specified request to be finished. + * + * Supplied parameter shall be a request id previously returned by + * B2R2_BLT_IOC or 0 for all requests. + * + * Returns 0 if OK, else a negative error code + * Return value: -ESOMERROR Description of an error + */ +#define B2R2_BLT_SYNCH_IOC _IOW(B2R2_BLT_IOC_MAGIC, 2, int) + +/** + * The BLT_QUERY_CAP_IOC returns capability information for all or + * for a certain format + * + * Supplied parameter shall be a pointer to a struct b2r2_blt_query_cap. + * + * @return Returns 0 if OK, else a negative error code + * @retval -ESOMERROR Description of an error + */ +#define B2R2_BLT_QUERY_CAP_IOC _IOWR(B2R2_BLT_IOC_MAGIC, 3, \ + struct b2r2_blt_query_cap) + +#endif /* #ifdef _LINUX_VIDEO_B2R2_BLT_H */ diff --git a/src/blt_b2r2.c b/src/blt_b2r2.c new file mode 100644 index 0000000..7e17d04 --- /dev/null +++ b/src/blt_b2r2.c @@ -0,0 +1,256 @@ +/* + * Copyright (C) ST-Ericsson AB 2009 - All rights reserved + * Reproduction and Communication of this document is strictly prohibited + * unless specifically authorized in writing by ST-Ericsson + * + * \file blt_b2r2.c + * \brief Android driver for B2R2 hardware + * \author ST-Ericsson + * + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "b2r2_blt.h" + +#define LOG_TAG "libblt_hw" + +#ifdef ANDROID +# define LOG_TAG "libblt_hw" +# include +# define LOGE2(...) LOGE(__VA_ARGS__) +# define LOGI2(...) LOGI(__VA_ARGS__) +#else +# define LOGE(format) fprintf(stderr, LOG_TAG format "\n") +# define LOGE2(format, ...) fprintf(stderr, LOG_TAG format "\n", __VA_ARGS__) +# define LOGI2(...) printf(LOG_TAG format "\n", __VA_ARGS__) +#endif + +#define B2R2_BLT_DEV "/dev/b2r2_blt" + +struct blt_b2r2_data { + int fd; + pthread_t callback_thread; +}; + +#define DATAS_START_SIZE 10 +#define DATAS_GROW_SIZE 5 +static struct blt_b2r2_data **datas = NULL; +static int data_count = 0; + +static int grow_datas(void) +{ + struct blt_b2r2_data **new_datas = NULL; + int new_data_count = data_count + DATAS_GROW_SIZE; + + new_datas = malloc(new_data_count*sizeof(*new_datas)); + if (new_datas == NULL) { + LOGE("Out of memory!"); + errno = -ENOMEM; + goto error; + } + + memset(new_datas, 0, new_data_count*sizeof(*new_datas)); + memcpy(new_datas, datas, data_count*sizeof(*datas)); + + free(datas); + + data_count = new_data_count; + datas = new_datas; + + return 0; + +error: + return -1; +} + +static int get_handle(struct blt_b2r2_data *data) { + int handle; + + if (datas == NULL) { + + datas = malloc(DATAS_START_SIZE*sizeof(*datas)); + if (datas == NULL) { + LOGE("Out of memory!\n"); + errno = -ENOMEM; + goto error; + } + + data_count = DATAS_START_SIZE; + memset(datas, 0, data_count*sizeof(*datas)); + } + + for (handle = 0; handle < data_count; handle++) { + if (datas[handle] == NULL) { + datas[handle] = data; + break; + } + + if (handle == data_count - 1) { + if (grow_datas() < 0) + goto error; + } + } + + return handle; + +error: + return -1; +} + +static struct blt_b2r2_data *get_data(int handle) { + if (handle >= data_count || handle < 0) + return NULL; + else + return datas[handle]; +} + +static void free_handle(int handle) { + if (handle < data_count && handle > 0) { + datas[handle] = NULL; + } +} + +static void *callback_thread_run(void *arg) +{ + while (1) { + struct pollfd fds; + struct b2r2_blt_report report; + + fds.fd = (int)arg; + fds.events = POLLIN; + + if (poll(&fds, 1, -1) <= 0) { + /* We assume that this is because the device was closed */ + pthread_exit(NULL); + break; + } else { + ssize_t count; + + memset(&report, 0, sizeof(report)); + count = read(fds.fd, &report, sizeof(report)); + if (count < 0) { + LOGE2("Could not read report from b2r2 device (%s)", + strerror(errno)); + } else if (report.report1 != 0) { + void (*callback)(int, uint32_t) = (void*)report.report1; + callback(report.request_id, (uint32_t)report.report2); + } + } + } + return NULL; +} + +int blt_open(void) +{ + struct blt_b2r2_data *data = NULL; + int fd; + int handle; + + fd = open(B2R2_BLT_DEV, O_RDWR); + if (fd < 0) { + LOGE2("Could not open device %s", B2R2_BLT_DEV); + goto error; + } + + data = malloc(sizeof(struct blt_b2r2_data)); + if (data == NULL) { + LOGE("Out of memory"); + goto error; + } + + memset(data, 0, sizeof(*data)); + + data->fd = fd; + data->callback_thread = -1; + + handle = get_handle(data); + if (handle < 0) + goto error_free; + + LOGI2("Library opened (handle = %d)", handle); + + return handle; + +error_free: + free(data); +error: + return -1; +} + +void blt_close(int blt_handle) +{ + struct blt_b2r2_data *data = get_data(blt_handle); + + if (data == NULL) + goto out; + + close(data->fd); + free(data); + +out: + return; +} + +int blt_request(int blt_handle, struct blt_req *req) +{ + struct blt_b2r2_data *data = get_data(blt_handle); + int ret; + + if (data == NULL) { + errno = EINVAL; + ret = -1; + goto out; + } + + if (req->callback != NULL) { + req->flags |= B2R2_BLT_FLAG_REPORT_WHEN_DONE; +#ifdef BLT_B2R2_DEBUG_PERFORMANCE + req->flags |= B2R2_BLT_FLAG_REPORT_PERFORMANCE; +#endif + + if (data->callback_thread != -1) { + /* Start a thread to wait for the requests to complete */ + pthread_create(&data->callback_thread, NULL, callback_thread_run, + (void *)data->fd); + } + } + + ret = ioctl(data->fd, B2R2_BLT_IOC, (struct b2r2_blt_req *) req); + if (ret < 0) + goto out; + +out: + return ret; +} + +int blt_synch(int blt_handle, int request_id) +{ + struct blt_b2r2_data *data = get_data(blt_handle); + + if (data == NULL) { + errno = EINVAL; + return -1; + } + + return ioctl(data->fd, B2R2_BLT_SYNCH_IOC, request_id); +} + +int blt_query_cap(int blt_handle, + enum blt_fmt fmt, + enum blt_cap capability, + uint32_t *cap) +{ + LOGE("blt_query_cap not implemented yet"); + errno = ENOSYS; + return -1; +} -- cgit v1.2.3