PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 1ms

/src/backend/android/ftk_display_android_gles.c

http://ftk.googlecode.com/
C | 186 lines | 149 code | 35 blank | 2 comment | 13 complexity | b311f3d3da20187937d34ca040de1235 MD5 | raw file
  1
  2#include "ftk_jni.h"
  3#include "ftk_event.h"
  4#include "ftk_globals.h"
  5#include "ftk_display_android.h"
  6#include <GLES/gl.h>
  7#include <GLES/glext.h>
  8
  9typedef struct _PrivInfo
 10{
 11	FtkBitmap* bitmap;
 12}PrivInfo;
 13
 14static int screen_width = 320;
 15static int screen_height = 480;
 16
 17void Android_SetScreenResolution(int width, int height)
 18{
 19	screen_width = width;
 20	screen_height = height;
 21}
 22
 23static Ret  ftk_display_android_handle_event(FtkDisplay* thiz, FtkEvent* event)
 24{
 25	Ret ret = RET_OK;
 26	DECL_PRIV(thiz, priv);
 27	return_val_if_fail(priv != NULL && event != NULL, RET_FAIL);
 28
 29	switch(event->type)
 30	{
 31		case FTK_EVT_OS_SCREEN_ROTATED:
 32		{
 33			ftk_wnd_manager_set_rotate(ftk_default_wnd_manager(), (FtkRotate)event->u.extra);
 34			ret = RET_REMOVE;
 35			break;
 36		}
 37		default:break;
 38	}
 39
 40	return ret;
 41}
 42
 43static int display_bitmap(PrivInfo* priv, FtkBitmap* bitmap, int x, int y, int width, int height, int xoffset, int yoffset)
 44{
 45	GLint crop[4] = {0};
 46	crop[0] = x;
 47	crop[1] = y;
 48	crop[2] = width;
 49	crop[3] = height;
 50
 51	if(Android_PreRender() != RET_OK)
 52	{
 53		return 0;
 54	}
 55
 56	glColor4f(1,1,1,1);
 57	glBindTexture(GL_TEXTURE_2D, 0);
 58	glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
 59	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 60	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 61	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 62	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 63	glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 64	glEnable(GL_TEXTURE_2D);
 65
 66	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ftk_bitmap_width(bitmap), ftk_bitmap_height(bitmap), 
 67		0, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
 68	glDrawTexiOES(xoffset, yoffset, 0, width, height);
 69
 70	Android_Render();
 71
 72	return 0;
 73}
 74
 75static Ret ftk_display_android_update(FtkDisplay* thiz, FtkBitmap* bitmap, FtkRect* rect, int xoffset, int yoffset)
 76{
 77	int i = 0;
 78	Ret ret = RET_OK;
 79	DECL_PRIV(thiz, priv);
 80	int width = rect->width;
 81	int height = rect->height;
 82	int src_width = ftk_bitmap_width(bitmap);
 83	int src_height = ftk_bitmap_height(bitmap);
 84	int dst_width = ftk_bitmap_width(priv->bitmap);
 85	int dst_height = ftk_bitmap_height(priv->bitmap);
 86	FtkColor* src = ftk_bitmap_lock(bitmap);
 87	FtkColor* dst = ftk_bitmap_lock(priv->bitmap);
 88
 89	return_val_if_fail(rect->x < src_width && rect->y < src_height
 90		&& xoffset < dst_width && yoffset < dst_height, RET_FAIL);
 91
 92	width = (xoffset + width) >= dst_width ? dst_width - xoffset : width;
 93	height = (yoffset + height) >= dst_height ? dst_height - yoffset : height;
 94
 95	yoffset = dst_height  - yoffset - height - 1;
 96	src += rect->y * src_width + rect->x;
 97	dst += (yoffset + height) * dst_width + xoffset;
 98
 99	for(i = 0; i < height; i++)
100	{
101		memcpy(dst, src, sizeof(FtkColor) * width);
102		dst -= dst_width;
103		src += src_width;
104	}
105
106#if 0
107	display_bitmap(priv, priv->bitmap, xoffset, yoffset, width, height, xoffset, yoffset);
108#else
109	display_bitmap(priv, priv->bitmap, 0, 0, screen_width, screen_height, 0, 0);
110#endif
111
112	return ret;
113}
114
115static Ret ftk_display_android_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
116{
117	int i = 0;
118	int xoffset = r->x;
119	int yoffset = r->y;
120	int width = r->width;
121	int height = r->height;
122	DECL_PRIV(thiz, priv);
123	FtkColor* dst = ftk_bitmap_lock(bitmap);
124	FtkColor* src = ftk_bitmap_lock(priv->bitmap);
125
126	/*TODO*/
127	//glReadPixels(r->x, r->y, r->width, r->height, GL_RGBA, GL_UNSIGNED_BYTE, ftk_bitmap_lock(bitmap));
128
129	return RET_OK;
130}
131
132static int ftk_display_android_width(FtkDisplay* thiz)
133{
134	return screen_width;
135}
136
137static int ftk_display_android_height(FtkDisplay* thiz)
138{
139	return screen_height;
140}
141
142static void ftk_display_android_destroy(FtkDisplay* thiz)
143{
144	if(thiz != NULL)
145	{
146		DECL_PRIV(thiz, priv);
147
148		ftk_wnd_manager_remove_global_listener(ftk_default_wnd_manager(),
149			(FtkListener)ftk_display_android_handle_event, thiz);
150
151		ftk_bitmap_unref(priv->bitmap);
152		FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
153	}
154
155	return;
156}
157
158FtkDisplay* ftk_display_android_create(void)
159{
160	FtkDisplay* thiz = NULL;
161
162	thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
163	if(thiz != NULL)
164	{
165		FtkColor bg;
166		DECL_PRIV(thiz, priv);
167		thiz->update   = ftk_display_android_update;
168		thiz->width    = ftk_display_android_width;
169		thiz->height   = ftk_display_android_height;
170		thiz->snap     = ftk_display_android_snap;
171		thiz->destroy  = ftk_display_android_destroy;
172
173		bg.a = 0xff;
174		bg.r = 0xff;
175		bg.g = 0xff;
176		bg.b = 0xff;
177		priv->bitmap = ftk_bitmap_create(screen_width, screen_height, bg);
178
179		ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(),
180			(FtkListener)ftk_display_android_handle_event, thiz);
181
182		Android_InitEGL();
183	}
184
185	return thiz;
186}