Petr Havlena | c928814 | 2012-11-15 14:07:10 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright@ Samsung Electronics Co. LTD |
| 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 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <linux/vt.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <poll.h> |
| 30 | #include <signal.h> |
| 31 | #include <cutils/log.h> |
| 32 | |
| 33 | #include "fimd.h" |
| 34 | |
| 35 | int fb_open(int win) |
| 36 | { |
| 37 | char node[20]; |
| 38 | int fp = -1; |
| 39 | |
| 40 | sprintf(node, "%s%d", PFX_NODE_FB, win); |
| 41 | |
| 42 | fp = open(node, O_RDWR); |
| 43 | if (fp < 0) |
| 44 | ALOGE("%s: fb[%d] open failed", __func__, win); |
| 45 | |
| 46 | return fp; |
| 47 | } |
| 48 | |
| 49 | int fb_close(int fp) |
| 50 | { |
| 51 | if (fp) |
| 52 | close(fp); |
| 53 | else |
| 54 | ALOGE("%s: fb is not allocated %d", __func__, fp); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int fb_get_fscreeninfo(int fp, struct fb_fix_screeninfo *fix) |
| 60 | { |
| 61 | int ret = -1; |
| 62 | |
| 63 | ret = ioctl(fp, FBIOGET_FSCREENINFO, fix); |
| 64 | if (ret) |
| 65 | ALOGE("%s: FBIOGET_FSCREENINFO failed", __func__); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | int fb_get_vscreeninfo(int fp, struct fb_var_screeninfo *var) |
| 71 | { |
| 72 | int ret = -1; |
| 73 | |
| 74 | ret = ioctl(fp, FBIOGET_VSCREENINFO, var); |
| 75 | if (ret) |
| 76 | ALOGE("%s:: FBIOGET_VSCREENINFO failed", __func__); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | int fb_put_vscreeninfo(int fp, struct fb_var_screeninfo *var) |
| 82 | { |
| 83 | int ret = -1; |
| 84 | |
| 85 | ret = ioctl(fp, FBIOPUT_VSCREENINFO, var); |
| 86 | if (ret) |
| 87 | ALOGE("%s:: FBIOPUT_VSCREENINFO failed", __func__); |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | char* fb_mmap(int fp, __u32 size) |
| 93 | { |
| 94 | char *buffer; |
| 95 | |
| 96 | buffer = (char *)mmap(0, size, PROT_READ | PROT_WRITE, |
| 97 | MAP_SHARED, fp, 0); |
| 98 | if (!buffer) { |
| 99 | ALOGE("%s:: mmap failed", __func__); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | return buffer; |
| 104 | } |
| 105 | |
| 106 | int fb_ioctl(int fp, __u32 cmd, void *arg) |
| 107 | { |
| 108 | int ret = -1; |
| 109 | |
| 110 | ret = ioctl(fp, cmd, arg); |
| 111 | if (ret < 0) |
| 112 | ALOGE("%s:: ioctl (%d) failed", __func__, cmd); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | int fb_on(int fp) |
| 118 | { |
| 119 | int ret = -1; |
| 120 | |
| 121 | ret = ioctl(fp, FBIOBLANK, FB_BLANK_UNBLANK); |
| 122 | if (ret) |
| 123 | ALOGE("%s:: FBIOBLANK failed", __func__); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | int fb_off(int fp) |
| 129 | { |
| 130 | int ret = -1; |
| 131 | |
| 132 | ret = ioctl(fp, FBIOBLANK, FB_BLANK_POWERDOWN); |
| 133 | if (ret) |
| 134 | ALOGE("%s:: FBIOBLANK failed", __func__); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | int fb_off_all() |
| 140 | { |
| 141 | int fp, i; |
| 142 | |
| 143 | for (i = 0; i < TOTAL_FB_NUM; i++) { |
| 144 | fp = fb_open(i); |
| 145 | if (fp < 0) |
| 146 | return -1; |
| 147 | |
| 148 | if (ioctl(fp, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) |
| 149 | ALOGE("%s:: FBIOBLANK failed", __func__); |
| 150 | |
| 151 | fb_off(fp); |
| 152 | fb_close(fp); |
| 153 | } |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | char* fb_init_display(int fp, int width, int height, int left_x, int top_y, int bpp) |
| 159 | { |
| 160 | struct fb_var_screeninfo var; |
| 161 | struct s5ptvfb_user_window window; |
| 162 | int fb_size; |
| 163 | char *fb = NULL; |
| 164 | |
| 165 | var.xres = width; |
| 166 | var.yres = height; |
| 167 | var.bits_per_pixel = bpp; |
| 168 | window.x = left_x; |
| 169 | window.y = top_y; |
| 170 | |
| 171 | var.xres_virtual = var.xres; |
| 172 | var.yres_virtual = var.yres; |
| 173 | var.xoffset = 0; |
| 174 | var.yoffset = 0; |
| 175 | var.width = 0; |
| 176 | var.height = 0; |
| 177 | var.transp.length = 0; |
| 178 | var.activate = FB_ACTIVATE_FORCE; |
| 179 | fb_size = var.xres_virtual * var.yres_virtual * bpp / 8; |
| 180 | |
| 181 | /* FBIOPUT_VSCREENINFO should be first */ |
| 182 | fb_put_vscreeninfo(fp, &var); |
| 183 | fb_ioctl(fp, S5PTVFB_WIN_POSITION, &window); |
| 184 | |
| 185 | /* draw image */ |
| 186 | fb = fb_mmap(fb_size, fp); |
| 187 | memset(fb, 0x0, fb_size); |
| 188 | |
| 189 | return fb; |
| 190 | } |
| 191 | |
| 192 | #if 0 |
| 193 | |
| 194 | static int get_bytes_per_pixel(int bits_per_pixel) |
| 195 | { |
| 196 | return (bits_per_pixel == 24 || bits_per_pixel == 25 || |
| 197 | bits_per_pixel == 28) ? 4 : bits_per_pixel / 8; |
| 198 | } |
| 199 | |
| 200 | int simple_draw(char *dest, const char *src, int img_width, |
| 201 | struct fb_var_screeninfo *var) |
| 202 | { |
| 203 | int bytes_per_pixel = get_bytes_per_pixel(var->bits_per_pixel); |
| 204 | unsigned int y; |
| 205 | |
| 206 | for (y = 0; y < var->yres; y++) |
| 207 | memcpy(dest + y * var->xres * bytes_per_pixel, |
| 208 | src + y * img_width * bytes_per_pixel, |
| 209 | var->xres * bytes_per_pixel); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | int draw(char *dest, const char *src, int img_width, |
| 215 | struct fb_var_screeninfo *var) |
| 216 | { |
| 217 | int bytes_per_pixel = get_bytes_per_pixel(var->bits_per_pixel); |
| 218 | unsigned int y; |
| 219 | |
| 220 | if (var->bits_per_pixel == 16) { |
| 221 | memcpy(dest, src, var->xres * var->yres * 2); |
| 222 | } else { |
| 223 | for (y = 0; y < var->yres; y++) |
| 224 | memcpy(dest + y * var->xres * bytes_per_pixel, |
| 225 | src + y * img_width * bytes_per_pixel, |
| 226 | var->xres * bytes_per_pixel); |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | #endif |