30天自制操作系统-第十天

叠加处理
        学习目标:1、内存管理;2、叠加处理;
        1、内存管理      
         memman_alloc和memman_free能够以1字节为单位进行内存管理,这种方式虽然不错,但是有一点不足:在反复进行内存分配和内存释放之后,内存中就会出现很多不连续的小段未使用空间,这样就会把memman->frees消耗殆尽。
        因此,我们要编写一些总是以0x1000字节为单位进行内存分配和内存释放的函数,它们会把指定的内存大小按照0x1000字节为单位进行内存分配和释放的函数,它们会把指定的内存大小按0x1000字节为单位向上舍入,而之所以要以0x1000字节为单位,是因为大小正好为4KB,比较规整。
unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size)
{
	unsigned int a;
	size = (size + 0xfff) & 0xfffff000;
	a = memman_alloc(man, size);
	return a;
}

int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size)
{
	int i;
	size = (size + 0xfff) & 0xfffff000;
	i = memman_free(man, addr, size);
	return i;
}
        向下舍入的式子:i=i&0xfffff000; 向上舍入:if((i&0xfff)!=0){i=(i&0xfffff000)+0x1000;}     实际上向上舍入还有改进的“窍门”,那就是:i=(i+0xfff)&0xfffff000;
        在以2的n次以外的数为单位进行向下舍入和向上舍入处理时,都必须使用除法命令,例如:i=(i/100)*100;   i=i-(i%100);
        2、叠加处理
        窗口的叠加问题:实际上,我们并不是像上面那样仅仅把两张大小相同的图层重叠在一起,而是要从大到小准备很多张图层。最上面的小图层用来描绘鼠标指针,它下面的几张图层是用来存放窗口的,而最下面的一张图是存放桌面壁纸。同时,我们还要通过移动图层的方法实现鼠标指针的移动以及窗口的移动。
        首先来考虑如何将一个图层的信息编成程序。
 
struct SHEET {
	unsigned char *buf; //用来记录图层上所描画内容的地址
	int bxsize, bysize, vx0, vy0, col_inv, height, flags;
};
struct SHTCTL {
	unsigned char *vram;
	int xsize, ysize, top; //TOP代表最上面图层的高度
	struct SHEET *sheets[MAX_SHEETS]; //记忆地址变量的区域
	struct SHEET sheets0[MAX_SHEETS]; //按照高度进行升序排序
};
      图层控制变量,需要比较大的空间,因此在这里使用memman_alloc_4K来分配内存空间,所以就编写了对内存进行分配和初始化的函数。
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)
{
	struct SHTCTL *ctl;
	int i;
	ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof (struct SHTCTL));
	if (ctl == 0) {
		goto err;
	}
	ctl->vram = vram;
	ctl->xsize = xsize;
	ctl->ysize = ysize;
	ctl->top = -1; /* 僔乕僩偼堦枃傕側偄 */
	for (i = 0; i < MAX_SHEETS; i++) {
		ctl->sheets0[i].flags = 0; /* 枹巊梡儅乕僋 */
	}
err:
	return ctl;
}
        接着,我们给控制变量赋值,给其下的所有图层变量加上“未使用”标签。做完这个函数就完成了。
struct SHEET *sheet_alloc(struct SHTCTL *ctl)
{
	struct SHEET *sht;
	int i;
	for (i = 0; i < MAX_SHEETS; i++) {
		if (ctl->sheets0[i].flags == 0) {
			sht = &ctl->sheets0[i];
			sht->flags = SHEET_USE; /* 找到了将其标记为正在使用*/
			sht->height = -1; /* 隐藏 */
			return sht;
		}
	}
	return 0;	/* 慡偰偺僔乕僩偑巊梡拞偩偭偨 */
}
        接下来,设定缓冲区大小和透明色的函数。
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)
{
	sht->buf = buf;
	sht->bxsize = xsize;
	sht->bysize = ysize;
	sht->col_inv = col_inv;
	return;
}
        接下来我们设定板底高度的函数。
void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height)
{
	int h, old = sht->height; /* 愝掕慜偺崅偝傪婰壇偡傞 */

	/* 巜掕偑掅偡偓傗崅偡偓偩偭偨傜丄廋惓偡傞 */
	if (height > ctl->top + 1) {
		height = ctl->top + 1;
	}
	if (height < -1) {
		height = -1;
	}
	sht->height = height; /* 崅偝傪愝掕 */

	/* 埲壓偼庡偵sheets[]偺暲傋懼偊 */
	if (old > height) {	/* 埲慜傛傝傕掅偔側傞 */
		if (height >= 0) {
			/* 娫偺傕偺傪堷偒忋偘傞 */
			for (h = old; h > height; h--) {
				ctl->sheets[h] = ctl->sheets[h - 1];
				ctl->sheets[h]->height = h;
			}
			ctl->sheets[height] = sht;
		} else {	/* 旕昞帵壔 */
			if (ctl->top > old) {
				/* 忋偵側偭偰偄傞傕偺傪偍傠偡 */
				for (h = old; h < ctl->top; h++) {
					ctl->sheets[h] = ctl->sheets[h + 1];
					ctl->sheets[h]->height = h;
				}
			}
			ctl->top--; /* 昞帵拞偺壓偠偒偑堦偮尭傞偺偱丄堦斣忋偺崅偝偑尭傞 */
		}
		sheet_refresh(ctl); /* 怴偟偄壓偠偒偺忣曬偵増偭偰夋柺傪昤偒捈偡 */
	} else if (old < height) {	/* 埲慜傛傝傕崅偔側傞 */
		if (old >= 0) {
			/* 娫偺傕偺傪墴偟壓偘傞 */
			for (h = old; h < height; h++) {
				ctl->sheets[h] = ctl->sheets[h + 1];
				ctl->sheets[h]->height = h;
			}
			ctl->sheets[height] = sht;
		} else {	/* 旕昞帵忬懺偐傜昞帵忬懺傊 */
			/* 忋偵側傞傕偺傪帩偪忋偘傞 */
			for (h = ctl->top; h >= height; h--) {
				ctl->sheets[h + 1] = ctl->sheets[h];
				ctl->sheets[h + 1]->height = h + 1;
			}
			ctl->sheets[height] = sht;
			ctl->top++; /* 昞帵拞偺壓偠偒偑堦偮憹偊傞偺偱丄堦斣忋偺崅偝偑憹偊傞 */
		}
		sheet_refresh(ctl); /* 怴偟偄壓偠偒偺忣曬偵増偭偰夋柺傪昤偒捈偡 */
	}
	return;
}
        下面来说说在sheet_updown中使用的sheet_refresh函数。
void sheet_refresh(struct SHTCTL *ctl)
{
	int h, bx, by, vx, vy;
	unsigned char *buf, c, *vram = ctl->vram;
	struct SHEET *sht;
	for (h = 0; h <= ctl->top; h++) {
		sht = ctl->sheets[h];
		buf = sht->buf;
		for (by = 0; by < sht->bysize; by++) {
			vy = sht->vy0 + by;
			for (bx = 0; bx < sht->bxsize; bx++) {
				vx = sht->vx0 + bx;
				c = buf[by * sht->bxsize + bx];
				if (c != sht->col_inv) {
					vram[vy * ctl->xsize + vx] = c;
				}
			}
		}
	}
	return;
}
       现在我们来看一下不改变图层高度而只上下左右移动图层的函数:sheet_slide。
void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)
{
	sht->vx0 = vx0;
	sht->vy0 = vy0;
	if (sht->height >= 0) { /* 傕偟傕昞帵拞側傜 */
		sheet_refresh(ctl); /* 怴偟偄壓偠偒偺忣曬偵増偭偰夋柺傪昤偒捈偡 */
	}
	return;
}
      最后是释放已使用图层的内存的函数sheet_free。
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht)
{
	if (sht->height >= 0) {
		sheet_updown(ctl, sht, -1); /* 昞帵拞側傜傑偢旕昞帵偵偡傞 */
	}
	sht->flags = 0; /* 枹巊梡儅乕僋 */
	return;
}
      那怎样才能提高速度呢?既然其他操作系统都能处理得那么快,就肯定有好的方法。首先,我们从鼠标指针的移动,也就是图层的移动来思考一下。
      实际上,只重新描绘移动相关的部分,也就是移动前后的部分就可以了。现在我们根据这个思路写一下程序。
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
	int h, bx, by, vx, vy;
	unsigned char *buf, c, *vram = ctl->vram;
	struct SHEET *sht;
	for (h = 0; h <= ctl->top; h++) {
		sht = ctl->sheets[h];
		buf = sht->buf;
		for (by = 0; by < sht->bysize; by++) {
			vy = sht->vy0 + by;
			for (bx = 0; bx < sht->bxsize; bx++) {
				vx = sht->vx0 + bx;
				if (vx0 <= vx && vx < vx1 && vy0 <= vy && vy < vy1) {
					c = buf[by * sht->bxsize + bx];
					if (c != sht->col_inv) {
						vram[vy * ctl->xsize + vx] = c;
					}
				}
			}
		}
	}
	return;
}
        现在我们使用这个refreshsub函数来提高sheet_slide的运行速度。
void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)
{
	int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
	sht->vx0 = vx0;
	sht->vy0 = vy0;
	if (sht->height >= 0) { /* 傕偟傕昞帵拞側傜丄怴偟偄壓偠偒偺忣曬偵増偭偰夋柺傪昤偒捈偡 */
sheet_refreshsub(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize);
sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize);
	}
	return;
}
        这段程序所做的事是:首先记住移动前的显示位置,再设定新的显示位置,最后只要重新描绘移动前和后的地方就可以了。
        下面,我们来解决一下图层内文字显示的问题。
void sheet_refresh(struct SHTCTL *ctl, struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
	if (sht->height >= 0) { /* 傕偟傕昞帵拞側傜丄怴偟偄壓偠偒偺忣曬偵増偭偰夋柺傪昤偒捈偡 */
		sheet_refreshsub(ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht->vy0 + by1);
	}
	return;
}
        速度确实比以前快多了,但还是存在很多问题。原来refreshsub还有一些问题。
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
	int h, bx, by, vx, vy, bx0, by0, bx1, by1;
	unsigned char *buf, c, *vram = ctl->vram;
	struct SHEET *sht;
	for (h = 0; h <= ctl->top; h++) {
		sht = ctl->sheets[h];
		buf = sht->buf;
		/* vx0乣vy1傪巊偭偰丄bx0乣by1傪媡嶼偡傞 */
		bx0 = vx0 - sht->vx0;
		by0 = vy0 - sht->vy0;
		bx1 = vx1 - sht->vx0;
		by1 = vy1 - sht->vy0;
		if (bx0 < 0) { bx0 = 0; }
		if (by0 < 0) { by0 = 0; }
		if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
		if (by1 > sht->bysize) { by1 = sht->bysize; }
		for (by = by0; by < by1; by++) {
			vy = sht->vy0 + by;
			for (bx = bx0; bx < bx1; bx++) {
				vx = sht->vx0 + bx;
				c = buf[by * sht->bxsize + bx];
				if (c != sht->col_inv) {
					vram[vy * ctl->xsize + vx] = c;
				}
			}
		}
	}
	return;
}
      改良的关键在于,bx在for语句中并不是在0到bxsize之间循环,而是在bx0到bx1之间循环。而bx0和bx1都是从刷新范围“倒推”求得的。倒推其实就是把公式变形转换了一下。  vx=sht->vx0+bx;  -> bx=vx-sht->vx0;






 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值