PageRenderTime 327ms CodeModel.GetById 162ms app.highlight 79ms RepoModel.GetById 82ms app.codeStats 0ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2010-09-27 Li XianJing <xianjimli@hotmail.com> created.
 29 *
 30 */
 31
 32#ifdef ANDROID
 33#include <EGL/egl.h>
 34#include <GLES/gl.h>
 35#include <GLES/glext.h>
 36
 37#include <ui/FramebufferNativeWindow.h>
 38#include <ui/EGLUtils.h>
 39using namespace android;
 40#define GLES_CREATE_WINDOW android_createDisplaySurface
 41#else
 42#include <egl.h>
 43#include <gl.h>
 44#include <glext.h>
 45#endif
 46
 47#include "ftk_log.h"
 48#include "ftk_display_gles.h"
 49
 50#ifndef EGLNativeWindowType
 51#define EGLNativeWindowType NativeWindowType
 52#endif
 53
 54typedef struct _PrivInfo
 55{
 56	int width;
 57	int height;
 58	EGLDisplay dpy;
 59	EGLContext context;
 60	EGLSurface surface;
 61	FtkBitmap* bitmap;
 62}PrivInfo;
 63
 64int opengles_init(PrivInfo* priv)
 65{
 66	int n = 0;
 67	EGLConfig config;
 68	EGLint majorVersion = 0;
 69	EGLint minorVersion = 0;
 70	
 71	const EGLint config_attr[] =
 72	{
 73		EGL_RED_SIZE,       5,
 74		EGL_GREEN_SIZE,     6,
 75		EGL_BLUE_SIZE,      5,
 76
 77		EGL_ALPHA_SIZE,     EGL_DONT_CARE,
 78		EGL_DEPTH_SIZE,     16,
 79		EGL_STENCIL_SIZE,   EGL_DONT_CARE,
 80		EGL_SURFACE_TYPE,   EGL_WINDOW_BIT,
 81		EGL_NONE
 82	};
 83
 84
 85	EGLNativeWindowType window = GLES_CREATE_WINDOW();
 86	priv->dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 87	eglInitialize(priv->dpy, &majorVersion, &minorVersion);
 88
 89	if (!eglChooseConfig(priv->dpy, config_attr, &config, 1, &n))
 90	{
 91		printf("%s: eglChooseConfig failed.\n", __func__);
 92	}
 93
 94    priv->surface = eglCreateWindowSurface(priv->dpy, config, window, NULL);
 95    priv->context = eglCreateContext(priv->dpy, config, NULL, NULL);
 96	eglMakeCurrent(priv->dpy, priv->surface, priv->surface, priv->context); 
 97	eglQuerySurface(priv->dpy, priv->surface, EGL_WIDTH,  &priv->width);
 98	eglQuerySurface(priv->dpy, priv->surface, EGL_HEIGHT, &priv->height);
 99
100	return 0;
101}
102
103int opengles_snap_bitmap(PrivInfo* priv, FtkBitmap* bitmap, 
104	int xoffset, int yoffset, int width, int height)
105{
106	glReadPixels(xoffset, yoffset, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
107
108	return 0;
109}
110
111int opengles_display_bitmap(PrivInfo* priv, FtkBitmap* bitmap, 
112	int x, int y, int width, int height, int xoffset, int yoffset)
113{
114	GLint crop[4] = {0};
115	crop[0] = x;
116	crop[1] = y;
117	crop[2] = width;
118	crop[3] = height;
119
120	glColor4f(1,1,1,1);
121	glBindTexture(GL_TEXTURE_2D, 0);
122	glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
123	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
125	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
126	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
127	glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
128	glEnable(GL_TEXTURE_2D);
129
130	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ftk_bitmap_width(bitmap), ftk_bitmap_height(bitmap), 
131		0, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
132	glDrawTexiOES(xoffset, yoffset, 0, width, height);
133
134	eglSwapBuffers(priv->dpy, priv->surface);
135
136	printf("rect(%d %d %d %d) offset(%d %d) bitmap(%d %d)\n",
137		x, y, width, height, xoffset, yoffset, ftk_bitmap_width(bitmap), ftk_bitmap_height(bitmap));
138
139	return 0;
140}
141
142static int ftk_display_gles_width(FtkDisplay* thiz)
143{
144	DECL_PRIV(thiz, priv);
145	return_val_if_fail(priv != NULL, 0);
146
147	return priv->width;
148}
149
150static int ftk_display_gles_height(FtkDisplay* thiz)
151{
152	DECL_PRIV(thiz, priv);
153	return_val_if_fail(priv != NULL, 0);
154
155	return priv->height;
156}
157
158static Ret ftk_display_gles_update(FtkDisplay* thiz, FtkBitmap* bitmap, 
159	FtkRect* rect, int xoffset, int yoffset)
160{
161	int i = 0;
162	Ret ret = RET_OK;
163	DECL_PRIV(thiz, priv);
164	int width = rect->width;
165	int height = rect->height;
166	int src_width = ftk_bitmap_width(bitmap);
167	int src_height = ftk_bitmap_height(bitmap);
168	int dst_width = ftk_bitmap_width(priv->bitmap);
169	int dst_height = ftk_bitmap_height(priv->bitmap);
170	FtkColor* src = ftk_bitmap_lock(bitmap);
171	FtkColor* dst = ftk_bitmap_lock(priv->bitmap);
172	
173	return_val_if_fail(rect->x < src_width && rect->y < src_height
174		&& xoffset < dst_width && yoffset < dst_height, RET_FAIL);
175
176	width = (xoffset + width) >= dst_width ? dst_width - xoffset : width;
177	height = (yoffset + height) >= dst_height ? dst_height - yoffset : height;
178
179	yoffset = dst_height  - yoffset - height - 1;
180	src += rect->y * src_width + rect->x;
181	dst += (yoffset + height) * dst_width + xoffset;
182
183	for(i = 0; i < height; i++)
184	{
185		memcpy(dst, src, sizeof(FtkColor) * width);
186		dst -= dst_width;
187		src += src_width;
188	}
189
190	opengles_display_bitmap(priv, priv->bitmap, xoffset, yoffset, 
191		width, height, xoffset, yoffset);
192	
193	return ret;
194}
195
196static Ret ftk_display_gles_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
197{
198	int i = 0;
199	int xoffset = r->x;
200	int yoffset = r->y;
201	int width = r->width;
202	int height = r->height;
203	DECL_PRIV(thiz, priv);
204	FtkColor* dst = ftk_bitmap_lock(bitmap);
205	FtkColor* src = ftk_bitmap_lock(priv->bitmap);
206
207	/*TODO*/
208	opengles_snap_bitmap(priv, bitmap, r->x, r->y, r->width, r->height);
209
210	return RET_OK;
211}
212
213static void ftk_display_gles_destroy(FtkDisplay* thiz)
214{
215	if(thiz != NULL)
216	{
217		DECL_PRIV(thiz, priv);
218		
219		eglTerminate(priv->dpy);
220		FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
221	}
222
223	return;
224}
225
226extern "C" FtkDisplay* ftk_display_gles_create(void)
227{
228	FtkDisplay* thiz = NULL;
229
230	thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
231	if(thiz != NULL)
232	{
233		FtkColor bg;
234		DECL_PRIV(thiz, priv);
235		thiz->update   = ftk_display_gles_update;
236		thiz->width    = ftk_display_gles_width;
237		thiz->height   = ftk_display_gles_height;
238		thiz->snap     = ftk_display_gles_snap;
239		thiz->destroy  = ftk_display_gles_destroy;
240
241		bg.a = 0xff;
242		bg.r = 0xff;
243		bg.g = 0xff;
244		bg.b = 0xff;
245		opengles_init(priv);
246		priv->bitmap = ftk_bitmap_create(priv->width, priv->height, bg);
247	}
248		
249	return thiz;
250}
251
252#ifdef FTK_DISPLAY_GLES_TEST
253#include "ftk.h"
254
255void red_bitmap(FtkBitmap* bitmap)
256{
257	int x = 0;
258	int y = 0;
259	int w = ftk_bitmap_width(bitmap);
260	int h = ftk_bitmap_height(bitmap);
261	FtkColor* bits = ftk_bitmap_lock(bitmap);
262
263	for(y = 0; y < h; y++)
264	{
265		for(x = 0; x < w; x++, bits++)
266		{
267			bits->g = 0;
268			bits->b = 0;
269			bits->r = 0xff;
270		}
271	}
272
273	return;
274}
275
276void green_bitmap(FtkBitmap* bitmap)
277{
278	int x = 0;
279	int y = 0;
280	int w = ftk_bitmap_width(bitmap);
281	int h = ftk_bitmap_height(bitmap);
282	FtkColor* bits = ftk_bitmap_lock(bitmap);
283
284	for(y = 0; y < h; y++)
285	{
286		for(x = 0; x < w; x++, bits++)
287		{
288			bits->r = 0;
289			bits->b = 0;
290			bits->g = 0xff;
291		}
292	}
293
294	return;
295}
296
297void blue_bitmap(FtkBitmap* bitmap)
298{
299	int x = 0;
300	int y = 0;
301	int w = ftk_bitmap_width(bitmap);
302	int h = ftk_bitmap_height(bitmap);
303	FtkColor* bits = ftk_bitmap_lock(bitmap);
304
305	for(y = 0; y < h; y++)
306	{
307		for(x = 0; x < w; x++, bits++)
308		{
309			bits->g = 0;
310			bits->r = 0;
311			bits->b = 0xff;
312		}
313	}
314
315	return;
316}
317
318void mire_bitmap(FtkBitmap* bitmap)
319{
320	int x = 0;
321	int y = 0;
322	int w = ftk_bitmap_width(bitmap);
323	int h = ftk_bitmap_height(bitmap);
324	FtkColor* bits = ftk_bitmap_lock(bitmap);
325
326	for (y = 0; y < h; y++)
327	{
328		for (x=0; x < w; x++, bits++)
329		{
330			unsigned int color = ((x-w/2)*(x-w/2) + (y-h/2)*(y-h/2))/64;
331			bits->r = (color/8) % 256;
332			bits->g = (color/4) % 256;
333			bits->b = (color/2) % 256;
334	//		bits->a = (color*2) % 256;
335		}
336	}
337
338	return;
339}
340
341int main(int argc, char* argv[])
342{
343	FtkDisplay* thiz = NULL;
344
345	ftk_init(argc, argv);
346
347	thiz = ftk_default_display();
348	if(thiz != NULL)
349	{
350		FtkBitmap* bitmap = NULL;
351		FtkColor color;
352		FtkRect rect = {0, 0, 0, 0};
353		color.a = 0xff;
354
355		rect.width = ftk_display_width(thiz);
356		rect.height = ftk_display_height(thiz);
357
358		bitmap = ftk_bitmap_create(rect.width, rect.height, color);
359		red_bitmap(bitmap);
360		ftk_display_update(thiz, bitmap, &rect, 0, 0);
361		sleep(3);
362		green_bitmap(bitmap);
363		ftk_display_update(thiz, bitmap, &rect, 0, 0);
364		sleep(3);
365		blue_bitmap(bitmap);
366		ftk_display_update(thiz, bitmap, &rect, 0, 0);
367		sleep(3);
368		mire_bitmap(bitmap);
369		ftk_display_update(thiz, bitmap, &rect, 0, 0);
370		sleep(3);
371
372		ftk_display_destroy(thiz);
373		ftk_bitmap_unref(bitmap);
374	}
375
376	return 0;
377}
378#endif
379