/src/backend/native/ftk_display_gles.cpp

http://ftk.googlecode.com/ · C++ · 379 lines · 284 code · 64 blank · 31 comment · 21 complexity · c4e2201910daf7c792bce2061718a158 MD5 · raw file

  1. /*
  2. * File: ftk_display_gles.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: opengles based display.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-09-27 Li XianJing <xianjimli@hotmail.com> created.
  28. *
  29. */
  30. #ifdef ANDROID
  31. #include <EGL/egl.h>
  32. #include <GLES/gl.h>
  33. #include <GLES/glext.h>
  34. #include <ui/FramebufferNativeWindow.h>
  35. #include <ui/EGLUtils.h>
  36. using namespace android;
  37. #define GLES_CREATE_WINDOW android_createDisplaySurface
  38. #else
  39. #include <egl.h>
  40. #include <gl.h>
  41. #include <glext.h>
  42. #endif
  43. #include "ftk_log.h"
  44. #include "ftk_display_gles.h"
  45. #ifndef EGLNativeWindowType
  46. #define EGLNativeWindowType NativeWindowType
  47. #endif
  48. typedef struct _PrivInfo
  49. {
  50. int width;
  51. int height;
  52. EGLDisplay dpy;
  53. EGLContext context;
  54. EGLSurface surface;
  55. FtkBitmap* bitmap;
  56. }PrivInfo;
  57. int opengles_init(PrivInfo* priv)
  58. {
  59. int n = 0;
  60. EGLConfig config;
  61. EGLint majorVersion = 0;
  62. EGLint minorVersion = 0;
  63. const EGLint config_attr[] =
  64. {
  65. EGL_RED_SIZE, 5,
  66. EGL_GREEN_SIZE, 6,
  67. EGL_BLUE_SIZE, 5,
  68. EGL_ALPHA_SIZE, EGL_DONT_CARE,
  69. EGL_DEPTH_SIZE, 16,
  70. EGL_STENCIL_SIZE, EGL_DONT_CARE,
  71. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  72. EGL_NONE
  73. };
  74. EGLNativeWindowType window = GLES_CREATE_WINDOW();
  75. priv->dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  76. eglInitialize(priv->dpy, &majorVersion, &minorVersion);
  77. if (!eglChooseConfig(priv->dpy, config_attr, &config, 1, &n))
  78. {
  79. printf("%s: eglChooseConfig failed.\n", __func__);
  80. }
  81. priv->surface = eglCreateWindowSurface(priv->dpy, config, window, NULL);
  82. priv->context = eglCreateContext(priv->dpy, config, NULL, NULL);
  83. eglMakeCurrent(priv->dpy, priv->surface, priv->surface, priv->context);
  84. eglQuerySurface(priv->dpy, priv->surface, EGL_WIDTH, &priv->width);
  85. eglQuerySurface(priv->dpy, priv->surface, EGL_HEIGHT, &priv->height);
  86. return 0;
  87. }
  88. int opengles_snap_bitmap(PrivInfo* priv, FtkBitmap* bitmap,
  89. int xoffset, int yoffset, int width, int height)
  90. {
  91. glReadPixels(xoffset, yoffset, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
  92. return 0;
  93. }
  94. int opengles_display_bitmap(PrivInfo* priv, FtkBitmap* bitmap,
  95. int x, int y, int width, int height, int xoffset, int yoffset)
  96. {
  97. GLint crop[4] = {0};
  98. crop[0] = x;
  99. crop[1] = y;
  100. crop[2] = width;
  101. crop[3] = height;
  102. glColor4f(1,1,1,1);
  103. glBindTexture(GL_TEXTURE_2D, 0);
  104. glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
  105. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  106. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  107. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  108. glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  109. glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  110. glEnable(GL_TEXTURE_2D);
  111. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ftk_bitmap_width(bitmap), ftk_bitmap_height(bitmap),
  112. 0, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
  113. glDrawTexiOES(xoffset, yoffset, 0, width, height);
  114. eglSwapBuffers(priv->dpy, priv->surface);
  115. printf("rect(%d %d %d %d) offset(%d %d) bitmap(%d %d)\n",
  116. x, y, width, height, xoffset, yoffset, ftk_bitmap_width(bitmap), ftk_bitmap_height(bitmap));
  117. return 0;
  118. }
  119. static int ftk_display_gles_width(FtkDisplay* thiz)
  120. {
  121. DECL_PRIV(thiz, priv);
  122. return_val_if_fail(priv != NULL, 0);
  123. return priv->width;
  124. }
  125. static int ftk_display_gles_height(FtkDisplay* thiz)
  126. {
  127. DECL_PRIV(thiz, priv);
  128. return_val_if_fail(priv != NULL, 0);
  129. return priv->height;
  130. }
  131. static Ret ftk_display_gles_update(FtkDisplay* thiz, FtkBitmap* bitmap,
  132. FtkRect* rect, int xoffset, int yoffset)
  133. {
  134. int i = 0;
  135. Ret ret = RET_OK;
  136. DECL_PRIV(thiz, priv);
  137. int width = rect->width;
  138. int height = rect->height;
  139. int src_width = ftk_bitmap_width(bitmap);
  140. int src_height = ftk_bitmap_height(bitmap);
  141. int dst_width = ftk_bitmap_width(priv->bitmap);
  142. int dst_height = ftk_bitmap_height(priv->bitmap);
  143. FtkColor* src = ftk_bitmap_lock(bitmap);
  144. FtkColor* dst = ftk_bitmap_lock(priv->bitmap);
  145. return_val_if_fail(rect->x < src_width && rect->y < src_height
  146. && xoffset < dst_width && yoffset < dst_height, RET_FAIL);
  147. width = (xoffset + width) >= dst_width ? dst_width - xoffset : width;
  148. height = (yoffset + height) >= dst_height ? dst_height - yoffset : height;
  149. yoffset = dst_height - yoffset - height - 1;
  150. src += rect->y * src_width + rect->x;
  151. dst += (yoffset + height) * dst_width + xoffset;
  152. for(i = 0; i < height; i++)
  153. {
  154. memcpy(dst, src, sizeof(FtkColor) * width);
  155. dst -= dst_width;
  156. src += src_width;
  157. }
  158. opengles_display_bitmap(priv, priv->bitmap, xoffset, yoffset,
  159. width, height, xoffset, yoffset);
  160. return ret;
  161. }
  162. static Ret ftk_display_gles_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
  163. {
  164. int i = 0;
  165. int xoffset = r->x;
  166. int yoffset = r->y;
  167. int width = r->width;
  168. int height = r->height;
  169. DECL_PRIV(thiz, priv);
  170. FtkColor* dst = ftk_bitmap_lock(bitmap);
  171. FtkColor* src = ftk_bitmap_lock(priv->bitmap);
  172. /*TODO*/
  173. opengles_snap_bitmap(priv, bitmap, r->x, r->y, r->width, r->height);
  174. return RET_OK;
  175. }
  176. static void ftk_display_gles_destroy(FtkDisplay* thiz)
  177. {
  178. if(thiz != NULL)
  179. {
  180. DECL_PRIV(thiz, priv);
  181. eglTerminate(priv->dpy);
  182. FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
  183. }
  184. return;
  185. }
  186. extern "C" FtkDisplay* ftk_display_gles_create(void)
  187. {
  188. FtkDisplay* thiz = NULL;
  189. thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
  190. if(thiz != NULL)
  191. {
  192. FtkColor bg;
  193. DECL_PRIV(thiz, priv);
  194. thiz->update = ftk_display_gles_update;
  195. thiz->width = ftk_display_gles_width;
  196. thiz->height = ftk_display_gles_height;
  197. thiz->snap = ftk_display_gles_snap;
  198. thiz->destroy = ftk_display_gles_destroy;
  199. bg.a = 0xff;
  200. bg.r = 0xff;
  201. bg.g = 0xff;
  202. bg.b = 0xff;
  203. opengles_init(priv);
  204. priv->bitmap = ftk_bitmap_create(priv->width, priv->height, bg);
  205. }
  206. return thiz;
  207. }
  208. #ifdef FTK_DISPLAY_GLES_TEST
  209. #include "ftk.h"
  210. void red_bitmap(FtkBitmap* bitmap)
  211. {
  212. int x = 0;
  213. int y = 0;
  214. int w = ftk_bitmap_width(bitmap);
  215. int h = ftk_bitmap_height(bitmap);
  216. FtkColor* bits = ftk_bitmap_lock(bitmap);
  217. for(y = 0; y < h; y++)
  218. {
  219. for(x = 0; x < w; x++, bits++)
  220. {
  221. bits->g = 0;
  222. bits->b = 0;
  223. bits->r = 0xff;
  224. }
  225. }
  226. return;
  227. }
  228. void green_bitmap(FtkBitmap* bitmap)
  229. {
  230. int x = 0;
  231. int y = 0;
  232. int w = ftk_bitmap_width(bitmap);
  233. int h = ftk_bitmap_height(bitmap);
  234. FtkColor* bits = ftk_bitmap_lock(bitmap);
  235. for(y = 0; y < h; y++)
  236. {
  237. for(x = 0; x < w; x++, bits++)
  238. {
  239. bits->r = 0;
  240. bits->b = 0;
  241. bits->g = 0xff;
  242. }
  243. }
  244. return;
  245. }
  246. void blue_bitmap(FtkBitmap* bitmap)
  247. {
  248. int x = 0;
  249. int y = 0;
  250. int w = ftk_bitmap_width(bitmap);
  251. int h = ftk_bitmap_height(bitmap);
  252. FtkColor* bits = ftk_bitmap_lock(bitmap);
  253. for(y = 0; y < h; y++)
  254. {
  255. for(x = 0; x < w; x++, bits++)
  256. {
  257. bits->g = 0;
  258. bits->r = 0;
  259. bits->b = 0xff;
  260. }
  261. }
  262. return;
  263. }
  264. void mire_bitmap(FtkBitmap* bitmap)
  265. {
  266. int x = 0;
  267. int y = 0;
  268. int w = ftk_bitmap_width(bitmap);
  269. int h = ftk_bitmap_height(bitmap);
  270. FtkColor* bits = ftk_bitmap_lock(bitmap);
  271. for (y = 0; y < h; y++)
  272. {
  273. for (x=0; x < w; x++, bits++)
  274. {
  275. unsigned int color = ((x-w/2)*(x-w/2) + (y-h/2)*(y-h/2))/64;
  276. bits->r = (color/8) % 256;
  277. bits->g = (color/4) % 256;
  278. bits->b = (color/2) % 256;
  279. // bits->a = (color*2) % 256;
  280. }
  281. }
  282. return;
  283. }
  284. int main(int argc, char* argv[])
  285. {
  286. FtkDisplay* thiz = NULL;
  287. ftk_init(argc, argv);
  288. thiz = ftk_default_display();
  289. if(thiz != NULL)
  290. {
  291. FtkBitmap* bitmap = NULL;
  292. FtkColor color;
  293. FtkRect rect = {0, 0, 0, 0};
  294. color.a = 0xff;
  295. rect.width = ftk_display_width(thiz);
  296. rect.height = ftk_display_height(thiz);
  297. bitmap = ftk_bitmap_create(rect.width, rect.height, color);
  298. red_bitmap(bitmap);
  299. ftk_display_update(thiz, bitmap, &rect, 0, 0);
  300. sleep(3);
  301. green_bitmap(bitmap);
  302. ftk_display_update(thiz, bitmap, &rect, 0, 0);
  303. sleep(3);
  304. blue_bitmap(bitmap);
  305. ftk_display_update(thiz, bitmap, &rect, 0, 0);
  306. sleep(3);
  307. mire_bitmap(bitmap);
  308. ftk_display_update(thiz, bitmap, &rect, 0, 0);
  309. sleep(3);
  310. ftk_display_destroy(thiz);
  311. ftk_bitmap_unref(bitmap);
  312. }
  313. return 0;
  314. }
  315. #endif