NDK图形函数在某些机型下显示花屏的问题

NDK使用ANativeWindow渲染surface,

大致代码如下:

ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);
if (nativeWindow == 0) {
	LOGE("ANativeWindow_window_from_surface error;env[0x%x] surface[0x%x]", env, surface);
	return NULL;
}

int err = ANativeWindow_setBuffersGeometry(nativeWindow, width, height, WINDOW_FORMAT_RGB_565);
if ( err < 0 ) {
	LOGE("ANativeWindow_setBuffersGeometry error");
	return NULL;
}


LOGI("AnativeSurface_Draw param invalid. %d | %d | %d | %d | %d | %d", env, NativeSurface, ImgData, dataLen, width, height);
if (env == NULL || NativeSurface == NULL || ImgData == NULL
			|| dataLen == 0 || width == 0 || height == 0) {
	LOGE("AnativeSurface_Draw param invalid. %d | %d | %d | %d | %d | %d", env, NativeSurface, ImgData, dataLen, width, height);
	return ;
}

ANativeWindow_Buffer windowBuffer;
int nlocked = ANativeWindow_lock((ANativeWindow*)NativeSurface, &windowBuffer, 0);
if (nlocked < 0) {
	LOGW("AnativeSurface lock error:%d", nlocked);
	return;
}

if (windowBuffer.bits != NULL) {
	memcpy(windowBuffer.bits, ImgData, dataLen);
}

ANativeWindow_unlockAndPost((ANativeWindow*)NativeSurface);

ANativeWindow_release((ANativeWindow*)NativeSurface);

在测试的过程中,发现有些机型上出现花屏的现象,经内存拷贝测试,发现这些机型上,windowbuffer.bits的大小不等于 width * height * RGBSIZE,因此会导致数据错位而无法正常显示。


翻了下native_window.h(development/ndk/platforms/android-x/include/android目录下),对于windowBuffer的定义如下:

typedef struct ANativeWindow_Buffer {
    // The number of pixels that are show horizontally.
    int32_t width;

    // The number of pixels that are shown vertically.
    int32_t height;

    // The number of *pixels* that a line in the buffer takes in
    // memory.  This may be >= width.
    int32_t stride;

    // The format of the buffer.  One of WINDOW_FORMAT_*
    int32_t format;

    // The actual bits.
    void* bits;
    
    // Do not touch.
    uint32_t reserved[6];
} ANativeWindow_Buffer;

其中一个叫stride的项,表示在内存中每一行包含的像素数量,这个值可能会大于width。


再次测试,发现所有有问题的机型,果然stride大于width。因此在拷贝的时候需要注意内存对齐。。


最后解决方案如下:

    if (windowBuffer.bits != NULL) {
    	//memcpy(windowBuffer.bits, ImgData, dataLen);
    	if (windowBuffer.width == windowBuffer.stride) {
    		memcpy(windowBuffer.bits, ImgData, dataLen);
    	} else {
    		for (int ii=0; ii < windowBuffer.height; ii++) {
    			char *srcPointer = ImgData + windowBuffer.width * ii * 2;
    			char *dstPointer = ((char *)windowBuffer.bits) + windowBuffer.stride * ii * 2;

    			memcpy(dstPointer, srcPointer, windowBuffer.width * 2);
    		}
    	}
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值