blob: 3713f61dee2298f486f16d2de35f79c0711af627 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
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
Dima Zavin4daf48a2011-08-30 11:59:20 -070017#include <stdbool.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include <stdlib.h>
19#include <unistd.h>
20
Dees Troy62b75ab2014-05-02 13:20:33 +000021#include <errno.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <fcntl.h>
23#include <stdio.h>
24
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <sys/types.h>
28
29#include <linux/fb.h>
30#include <linux/kd.h>
31
Ethan Yonkera33161b2014-11-06 15:11:20 -060032#include <pixelflinger/pixelflinger.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Doug Zongker6fd59ac2013-03-06 15:01:11 -080034#include "font_10x18.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "minui.h"
Ethan Yonkera33161b2014-11-06 15:11:20 -060036
37#if defined(RECOVERY_BGRA)
38#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888
39#define PIXEL_SIZE 4
40#elif defined(RECOVERY_RGBX)
41#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888
42#define PIXEL_SIZE 4
43#else
44#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565
45#define PIXEL_SIZE 2
46#endif
47
48#define NUM_BUFFERS 2
Devin Kim862d0262012-07-19 10:47:34 -070049
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050typedef struct {
Ethan Yonkera33161b2014-11-06 15:11:20 -060051 GGLSurface* texture;
52 unsigned cwidth;
53 unsigned cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054} GRFont;
55
Ethan Yonkera33161b2014-11-06 15:11:20 -060056static GRFont *gr_font = 0;
57static GGLContext *gr_context = 0;
58static GGLSurface gr_font_texture;
59static GGLSurface gr_framebuffer[NUM_BUFFERS];
60static GGLSurface gr_mem_surface;
61static unsigned gr_active_fb = 0;
62static unsigned double_buffering = 0;
Doug Zongkerc560a672012-12-18 16:31:27 -080063static int overscan_percent = OVERSCAN_PERCENT;
64static int overscan_offset_x = 0;
65static int overscan_offset_y = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066
Ethan Yonkera33161b2014-11-06 15:11:20 -060067static int gr_fb_fd = -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068static int gr_vt_fd = -1;
69
70static struct fb_var_screeninfo vi;
Michael Ward9d1bcdf2011-06-22 14:30:34 -070071static struct fb_fix_screeninfo fi;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072
Dees Troy62b75ab2014-05-02 13:20:33 +000073static bool has_overlay = false;
74
75bool target_has_overlay(char *version);
76int free_ion_mem(void);
77int alloc_ion_mem(unsigned int size);
78int allocate_overlay(int fd, GGLSurface gr_fb[]);
79int free_overlay(int fd);
80int overlay_display_frame(int fd, GGLubyte* data, size_t size);
81
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082static int get_framebuffer(GGLSurface *fb)
83{
84 int fd;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 void *bits;
86
87 fd = open("/dev/graphics/fb0", O_RDWR);
88 if (fd < 0) {
89 perror("cannot open fb0");
90 return -1;
91 }
92
Michael Ward3dbe66b2011-06-23 19:28:53 -070093 if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094 perror("failed to get fb0 info");
95 close(fd);
96 return -1;
97 }
98
Michael Ward3dbe66b2011-06-23 19:28:53 -070099 vi.bits_per_pixel = PIXEL_SIZE * 8;
100 if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) {
101 vi.red.offset = 8;
102 vi.red.length = 8;
103 vi.green.offset = 16;
104 vi.green.length = 8;
105 vi.blue.offset = 24;
106 vi.blue.length = 8;
107 vi.transp.offset = 0;
108 vi.transp.length = 8;
109 } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) {
110 vi.red.offset = 24;
111 vi.red.length = 8;
112 vi.green.offset = 16;
113 vi.green.length = 8;
114 vi.blue.offset = 8;
115 vi.blue.length = 8;
116 vi.transp.offset = 0;
117 vi.transp.length = 8;
118 } else { /* RGB565*/
119 vi.red.offset = 11;
120 vi.red.length = 5;
121 vi.green.offset = 5;
122 vi.green.length = 6;
123 vi.blue.offset = 0;
124 vi.blue.length = 5;
125 vi.transp.offset = 0;
126 vi.transp.length = 0;
127 }
128 if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
129 perror("failed to put fb0 info");
130 close(fd);
131 return -1;
132 }
133
134 if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 perror("failed to get fb0 info");
136 close(fd);
137 return -1;
138 }
139
Dees Troy62b75ab2014-05-02 13:20:33 +0000140 has_overlay = target_has_overlay(fi.id);
141
142 if (!has_overlay) {
143 bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
144 if (bits == MAP_FAILED) {
145 perror("failed to mmap framebuffer");
146 close(fd);
147 return -1;
148 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149 }
150
Doug Zongkerc560a672012-12-18 16:31:27 -0800151 overscan_offset_x = vi.xres * overscan_percent / 100;
152 overscan_offset_y = vi.yres * overscan_percent / 100;
153
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 fb->version = sizeof(*fb);
155 fb->width = vi.xres;
156 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700157 fb->stride = fi.line_length/PIXEL_SIZE;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800158 fb->format = PIXEL_FORMAT;
Dees Troy62b75ab2014-05-02 13:20:33 +0000159 if (!has_overlay) {
160 fb->data = bits;
161 memset(fb->data, 0, vi.yres * fi.line_length);
162 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
164 fb++;
165
Octavian Purdila0e348802011-07-01 17:57:45 +0300166 /* check if we can use double buffering */
167 if (vi.yres * fi.line_length * 2 > fi.smem_len)
168 return fd;
169
Ethan Yonkera33161b2014-11-06 15:11:20 -0600170 double_buffering = 1;
171
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172 fb->version = sizeof(*fb);
173 fb->width = vi.xres;
174 fb->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700175 fb->stride = fi.line_length/PIXEL_SIZE;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800176 fb->format = PIXEL_FORMAT;
Dees Troy62b75ab2014-05-02 13:20:33 +0000177 if (!has_overlay) {
Ethan Yonkerbcc502c2014-11-10 11:22:10 -0600178 fb->data = (void*) (((unsigned long) bits) + vi.yres * fi.line_length);
Dees Troy62b75ab2014-05-02 13:20:33 +0000179 memset(fb->data, 0, vi.yres * fi.line_length);
180 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181
182 return fd;
183}
184
185static void get_memory_surface(GGLSurface* ms) {
186 ms->version = sizeof(*ms);
187 ms->width = vi.xres;
188 ms->height = vi.yres;
Michael Ward9d1bcdf2011-06-22 14:30:34 -0700189 ms->stride = fi.line_length/PIXEL_SIZE;
190 ms->data = malloc(fi.line_length * vi.yres);
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800191 ms->format = PIXEL_FORMAT;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192}
193
194static void set_active_framebuffer(unsigned n)
195{
Octavian Purdila0e348802011-07-01 17:57:45 +0300196 if (n > 1 || !double_buffering) return;
Devin Kim862d0262012-07-19 10:47:34 -0700197 vi.yres_virtual = vi.yres * NUM_BUFFERS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198 vi.yoffset = n * vi.yres;
Doug Zongkerbe3e6f12011-01-13 16:43:44 -0800199 vi.bits_per_pixel = PIXEL_SIZE * 8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
201 perror("active fb swap failed");
202 }
203}
204
205void gr_flip(void)
206{
Dees Troy62b75ab2014-05-02 13:20:33 +0000207 if (-EINVAL == overlay_display_frame(gr_fb_fd, gr_mem_surface.data,
208 (fi.line_length * vi.yres))) {
209 GGLContext *gl = gr_context;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210
Dees Troy62b75ab2014-05-02 13:20:33 +0000211 /* swap front and back buffers */
212 if (double_buffering)
213 gr_active_fb = (gr_active_fb + 1) & 1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214
Dees Troy62b75ab2014-05-02 13:20:33 +0000215 /* copy data from the in-memory surface to the buffer we're about
216 * to make active. */
217 memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data,
218 fi.line_length * vi.yres);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219
Dees Troy62b75ab2014-05-02 13:20:33 +0000220 /* inform the display driver */
221 set_active_framebuffer(gr_active_fb);
222 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223}
224
225void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
226{
227 GGLContext *gl = gr_context;
228 GGLint color[4];
229 color[0] = ((r << 8) | r) + 1;
230 color[1] = ((g << 8) | g) + 1;
231 color[2] = ((b << 8) | b) + 1;
232 color[3] = ((a << 8) | a) + 1;
233 gl->color4xv(gl, color);
234}
235
236int gr_measure(const char *s)
237{
238 return gr_font->cwidth * strlen(s);
239}
240
Dima Zavin3c7f00e2011-08-30 11:58:24 -0700241void gr_font_size(int *x, int *y)
242{
243 *x = gr_font->cwidth;
244 *y = gr_font->cheight;
245}
246
Vojtech Bocek65fdcdd2013-09-06 21:32:22 +0200247int gr_text(int x, int y, const char *s, ...)
248{
249 return gr_text_impl(x, y, s, 0);
250}
251
252int gr_text_impl(int x, int y, const char *s, int bold)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253{
Ethan Yonkera33161b2014-11-06 15:11:20 -0600254 GGLContext *gl = gr_context;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 GRFont *font = gr_font;
256 unsigned off;
257
Ethan Yonkera33161b2014-11-06 15:11:20 -0600258 if (!font->texture) return x;
Doug Zongker55a36ac2013-03-04 15:49:02 -0800259
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800260 bold = bold && (font->texture->height != font->cheight);
261
Doug Zongkerc560a672012-12-18 16:31:27 -0800262 x += overscan_offset_x;
263 y += overscan_offset_y;
264
Ethan Yonkera33161b2014-11-06 15:11:20 -0600265 gl->bindTexture(gl, font->texture);
266 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
267 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
268 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
269 gl->enable(gl, GGL_TEXTURE_2D);
270
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 while((off = *s++)) {
272 off -= 32;
273 if (off < 96) {
Ethan Yonkera33161b2014-11-06 15:11:20 -0600274 gl->texCoord2i(gl, (off * font->cwidth) - x,
275 (bold ? font->cheight : 0) - y);
276 gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 }
278 x += font->cwidth;
279 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600280
281 return x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282}
283
Ethan Yonkera33161b2014-11-06 15:11:20 -0600284void gr_texticon(int x, int y, gr_surface icon) {
285 if (gr_context == NULL || icon == NULL) {
Doug Zongker52eeea4f2012-09-04 14:28:25 -0700286 return;
287 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600288 GGLContext* gl = gr_context;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700289
Doug Zongkerc560a672012-12-18 16:31:27 -0800290 x += overscan_offset_x;
291 y += overscan_offset_y;
292
Ethan Yonkera33161b2014-11-06 15:11:20 -0600293 gl->bindTexture(gl, (GGLSurface*) icon);
294 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
295 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
296 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
297 gl->enable(gl, GGL_TEXTURE_2D);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700298
Ethan Yonkera33161b2014-11-06 15:11:20 -0600299 int w = gr_get_width(icon);
300 int h = gr_get_height(icon);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700301
Ethan Yonkera33161b2014-11-06 15:11:20 -0600302 gl->texCoord2i(gl, -x, -y);
303 gl->recti(gl, x, y, x+gr_get_width(icon), y+gr_get_height(icon));
Doug Zongker02ec6b82012-08-22 17:26:40 -0700304}
305
Doug Zongkerc560a672012-12-18 16:31:27 -0800306void gr_fill(int x1, int y1, int x2, int y2)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307{
Doug Zongkerc560a672012-12-18 16:31:27 -0800308 x1 += overscan_offset_x;
309 y1 += overscan_offset_y;
310
311 x2 += overscan_offset_x;
312 y2 += overscan_offset_y;
313
Ethan Yonkera33161b2014-11-06 15:11:20 -0600314 GGLContext *gl = gr_context;
315 gl->disable(gl, GGL_TEXTURE_2D);
316 gl->recti(gl, x1, y1, x2, y2);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317}
318
Ethan Yonkera33161b2014-11-06 15:11:20 -0600319void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) {
320 if (gr_context == NULL || source == NULL) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 return;
322 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600323 GGLContext *gl = gr_context;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800324
Doug Zongkerc560a672012-12-18 16:31:27 -0800325 dx += overscan_offset_x;
326 dy += overscan_offset_y;
327
Ethan Yonkera33161b2014-11-06 15:11:20 -0600328 gl->bindTexture(gl, (GGLSurface*) source);
329 gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE);
330 gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
331 gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE);
332 gl->enable(gl, GGL_TEXTURE_2D);
333 gl->texCoord2i(gl, sx - dx, sy - dy);
334 gl->recti(gl, dx, dy, dx + w, dy + h);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335}
336
Ethan Yonkera33161b2014-11-06 15:11:20 -0600337unsigned int gr_get_width(gr_surface surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 if (surface == NULL) {
339 return 0;
340 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600341 return ((GGLSurface*) surface)->width;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342}
343
Ethan Yonkera33161b2014-11-06 15:11:20 -0600344unsigned int gr_get_height(gr_surface surface) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345 if (surface == NULL) {
346 return 0;
347 }
Ethan Yonkera33161b2014-11-06 15:11:20 -0600348 return ((GGLSurface*) surface)->height;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349}
350
351static void gr_init_font(void)
352{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353 gr_font = calloc(sizeof(*gr_font), 1);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800354
Ethan Yonkera33161b2014-11-06 15:11:20 -0600355 int res = res_create_surface("font", (void**)&(gr_font->texture));
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800356 if (res == 0) {
357 // The font image should be a 96x2 array of character images. The
358 // columns are the printable ASCII characters 0x20 - 0x7f. The
359 // top row is regular text; the bottom row is bold.
360 gr_font->cwidth = gr_font->texture->width / 96;
361 gr_font->cheight = gr_font->texture->height / 2;
362 } else {
Doug Zongker55a36ac2013-03-04 15:49:02 -0800363 printf("failed to read font: res=%d\n", res);
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800364
365 // fall back to the compiled-in font.
366 gr_font->texture = malloc(sizeof(*gr_font->texture));
367 gr_font->texture->width = font.width;
368 gr_font->texture->height = font.height;
Ethan Yonkera33161b2014-11-06 15:11:20 -0600369 gr_font->texture->stride = font.width;
Doug Zongker6fd59ac2013-03-06 15:01:11 -0800370
371 unsigned char* bits = malloc(font.width * font.height);
372 gr_font->texture->data = (void*) bits;
373
374 unsigned char data;
375 unsigned char* in = font.rundata;
376 while((data = *in++)) {
377 memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
378 bits += (data & 0x7f);
379 }
380
381 gr_font->cwidth = font.cwidth;
382 gr_font->cheight = font.cheight;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800383 }
384
Ethan Yonkera33161b2014-11-06 15:11:20 -0600385 // interpret the grayscale as alpha
386 gr_font->texture->format = GGL_PIXEL_FORMAT_A_8;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800387}
388
389int gr_init(void)
390{
Ethan Yonkera33161b2014-11-06 15:11:20 -0600391 gglInit(&gr_context);
392 GGLContext *gl = gr_context;
Doug Zongker16f97c32014-03-06 16:16:05 -0800393
Ethan Yonkera33161b2014-11-06 15:11:20 -0600394 gr_init_font();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800395 gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
396 if (gr_vt_fd < 0) {
397 // This is non-fatal; post-Cupcake kernels don't have tty0.
398 perror("can't open /dev/tty0");
399 } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
400 // However, if we do open tty0, we expect the ioctl to work.
401 perror("failed KDSETMODE to KD_GRAPHICS on tty0");
402 gr_exit();
403 return -1;
404 }
405
Ethan Yonkera33161b2014-11-06 15:11:20 -0600406 gr_fb_fd = get_framebuffer(gr_framebuffer);
407 if (gr_fb_fd < 0) {
408 gr_exit();
409 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800410 }
411
412 get_memory_surface(&gr_mem_surface);
413
Dees Troy62b75ab2014-05-02 13:20:33 +0000414 fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
415 gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800416
Dees Troy62b75ab2014-05-02 13:20:33 +0000417 /* start with 0 as front (displayed) and 1 as back (drawing) */
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800418 gr_active_fb = 0;
Dees Troy62b75ab2014-05-02 13:20:33 +0000419 if (!has_overlay)
420 set_active_framebuffer(0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800421 gl->colorBuffer(gl, &gr_mem_surface);
422
Ethan Yonkera33161b2014-11-06 15:11:20 -0600423 gl->activeTexture(gl, 0);
424 gl->enable(gl, GGL_BLEND);
425 gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800426
Ethan Yonkera33161b2014-11-06 15:11:20 -0600427 gr_fb_blank(true);
428 gr_fb_blank(false);
Doug Zongkerf6abd402011-09-27 13:09:48 -0700429
Dees Troy62b75ab2014-05-02 13:20:33 +0000430 if (!alloc_ion_mem(fi.line_length * vi.yres))
431 allocate_overlay(gr_fb_fd, gr_framebuffer);
432
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800433 return 0;
434}
435
436void gr_exit(void)
437{
Dees Troy62b75ab2014-05-02 13:20:33 +0000438 free_overlay(gr_fb_fd);
439 free_ion_mem();
440
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800441 close(gr_fb_fd);
442 gr_fb_fd = -1;
443
444 free(gr_mem_surface.data);
445
446 ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
447 close(gr_vt_fd);
448 gr_vt_fd = -1;
449}
450
451int gr_fb_width(void)
452{
Ethan Yonkera33161b2014-11-06 15:11:20 -0600453 return gr_framebuffer[0].width - 2*overscan_offset_x;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454}
455
456int gr_fb_height(void)
457{
Ethan Yonkera33161b2014-11-06 15:11:20 -0600458 return gr_framebuffer[0].height - 2*overscan_offset_y;
459}
460
461gr_pixel *gr_fb_data(void)
462{
463 return (unsigned short *) gr_mem_surface.data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464}
Dima Zavin4daf48a2011-08-30 11:59:20 -0700465
466void gr_fb_blank(bool blank)
467{
Matt Mower4a5db2d2014-01-20 16:14:25 -0600468#if defined(TW_NO_SCREEN_BLANK) && defined(TW_BRIGHTNESS_PATH) && defined(TW_MAX_BRIGHTNESS)
Ethan Chen0940e412013-10-22 13:48:50 -0700469 int fd;
Matt Mower4a5db2d2014-01-20 16:14:25 -0600470 char brightness[4];
471 snprintf(brightness, 4, "%03d", TW_MAX_BRIGHTNESS/2);
Ethan Chen0940e412013-10-22 13:48:50 -0700472
Matt Mower4a5db2d2014-01-20 16:14:25 -0600473 fd = open(TW_BRIGHTNESS_PATH, O_RDWR);
Ethan Chen0940e412013-10-22 13:48:50 -0700474 if (fd < 0) {
475 perror("cannot open LCD backlight");
476 return;
477 }
Matt Mower4a5db2d2014-01-20 16:14:25 -0600478 write(fd, blank ? "000" : brightness, 3);
Ethan Chen0940e412013-10-22 13:48:50 -0700479 close(fd);
480#else
Ethan Yonkera33161b2014-11-06 15:11:20 -0600481 int ret;
Dees Troy62b75ab2014-05-02 13:20:33 +0000482 if (blank)
483 free_overlay(gr_fb_fd);
Dima Zavin4daf48a2011-08-30 11:59:20 -0700484
Ethan Yonkera33161b2014-11-06 15:11:20 -0600485 ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
486 if (ret < 0)
487 perror("ioctl(): blank");
488
Dees Troy62b75ab2014-05-02 13:20:33 +0000489 if (!blank)
490 allocate_overlay(gr_fb_fd, gr_framebuffer);
Ethan Chen0940e412013-10-22 13:48:50 -0700491#endif
Dima Zavin4daf48a2011-08-30 11:59:20 -0700492}
Dees Troy62b75ab2014-05-02 13:20:33 +0000493
494void gr_get_memory_surface(gr_surface surface)
495{
496 get_memory_surface( (GGLSurface*) surface);
497}
Ethan Yonker304f32f2014-11-07 10:14:05 -0600498
499// These are new graphics functions from 5.0 that were not available in
500// 4.4 that are required by charger and healthd
501void gr_clear()
502{
503 return;
504}
505