=====================================================
SDL源代碼分析系列文章列表:
SDL2源代碼分析1:初始化(SDL_Init())
SDL2源代碼分析2:窗口(SDL_Window)
SDL2源代碼分析3:渲染器(SDL_Renderer)
SDL2源代碼分析4:紋理(SDL_Texture)
SDL2源代碼分析5:更新紋理(SDL_UpdateTexture())
SDL2源代碼分析6:複製到渲染器(SDL_RenderCopy())
SDL2源代碼分析7:顯示(SDL_RenderPresent())
SDL2源代碼分析8:視頻顯示總結
=====================================================
上一篇文章分析了SDL的創建紋理函數SDL_CreateTexture()。這篇文章繼續分析SDL的源代碼。本文分析SDL更新紋理數據函數SDL_UpdateTexture()。
SDL播放視頻的代碼流程如下所示。
初始化:
SDL_Init(): 初始化SDL。
SDL_CreateWindow(): 創建窗口(Window)。
SDL_CreateRenderer(): 基於窗口創建渲染器(Render)。
SDL_CreateTexture(): 創建紋理(Texture)。
循環渲染數據:
SDL_UpdateTexture(): 設置紋理的數據。
SDL_RenderCopy(): 紋理複製給渲染器。
SDL_RenderPresent(): 顯示。
上篇文章分析了該流程中的第4個函數SDL_CreateTexture()。本文繼續分析該流程中的第5個函數SDL_UpdateTexture()。
SDL_UpdateTexture()
函數簡介
SDL使用SDL_UpdateTexture()設置紋理的像素數據。SDL_UpdateTexture()的原型如下。
-
int SDLCALL SDL_UpdateTexture(SDL_Texture * texture,
-
const SDL_Rect * rect,
-
const void *pixels, int pitch);
參數的含義如下。
texture:目標紋理。
rect:更新像素的矩形區域。設置爲NULL的時候更新整個區域。
pixels:像素數據。
pitch:一行像素數據的字節數。
成功的話返回0,失敗的話返回-1。
函數調用關係圖
SDL_UpdateTexture()關鍵函數的調用關係可以用下圖表示。
上面的圖片不太清晰,更清晰的圖片上傳到了相冊裏面:
http://my.csdn.net/leixiaohua1020/album/detail/1793769
把相冊裏面的圖片保存下來就可以得到清晰的圖片。
源代碼分析
SDL_UpdateTexture()的源代碼位於render\SDL_render.c中。如下所示。
-
int SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
-
const void *pixels, int pitch)
-
{
-
SDL_Renderer *renderer;
-
SDL_Rect full_rect;
-
-
-
CHECK_TEXTURE_MAGIC(texture, -1);
-
-
-
if (!pixels) {
-
return SDL_InvalidParamError("pixels");
-
}
-
if (!pitch) {
-
return SDL_InvalidParamError("pitch");
-
}
-
-
-
if (!rect) {
-
full_rect.x = 0;
-
full_rect.y = 0;
-
full_rect.w = texture->w;
-
full_rect.h = texture->h;
-
rect = &full_rect;
-
}
-
-
-
if (texture->yuv) {
-
return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
-
} else if (texture->native) {
-
return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
-
} else {
-
renderer = texture->renderer;
-
return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
-
}
-
}
從源代碼中可以看出,SDL_UpdateTexture()的大致流程如下。
1.
檢查輸入參數的合理性。例如像素格式是否支持,寬和高是否小於等於0等等。
2.
如果是一些特殊的格式,進行一定的處理:
a) 如果輸入的像素數據是YUV格式的,則會調用SDL_UpdateTextureYUV()進行處理。
b) 如果輸入的像素數據的像素格式不是渲染器支持的格式,則會調用SDL_UpdateTextureNative()進行處理。
3.
調用SDL_Render的UpdateTexture()方法更新紋理。這一步是整個函數的核心。
下面我們詳細看一下幾種不同的渲染器的UpdateTexture ()的方法。
1.
Direct3D
Direct3D 渲染器中對應UpdateTexture ()的函數是D3D_UpdateTexture(),它的源代碼如下所示(位於render\direct3d\SDL_render_d3d.c)。
-
static int
-
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
-
const SDL_Rect * rect, const void *pixels, int pitch)
-
{
-
D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
-
SDL_bool full_texture = SDL_FALSE;
-
-
-
#ifdef USE_DYNAMIC_TEXTURE
-
if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
-
rect->x == 0 && rect->y == 0 &&
-
rect->w == texture->w && rect->h == texture->h) {
-
full_texture = SDL_TRUE;
-
}
-
#endif
-
-
-
if (!data) {
-
SDL_SetError("Texture is not currently available");
-
return -1;
-
}
-
-
-
if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
-
return -1;
-
}
-
-
-
if (data->yuv) {
-
-
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
-
-
-
if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->vtexture : data->utexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
-
return -1;
-
}
-
-
-
-
pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
-
if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->utexture : data->vtexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
-
return -1;
-
}
-
}
-
return 0;
-
}
從代碼中可以看出,該函數調用了D3D_UpdateTextureInternal()函數。在這裏需要注意,如果輸入像素格式是YUV,就會使用3個紋理,對於多出的那2個紋理會單獨進行處理。調用的函數D3D_UpdateTextureInternal()代碼如下。
-
static int D3D_UpdateTextureInternal(IDirect3DTexture9 *texture, Uint32 format, SDL_bool full_texture, int x, int y, int w, int h, const void *pixels, int pitch)
-
{
-
RECT d3drect;
-
D3DLOCKED_RECT locked;
-
const Uint8 *src;
-
Uint8 *dst;
-
int row, length;
-
HRESULT result;
-
-
-
if (full_texture) {
-
result = IDirect3DTexture9_LockRect(texture, 0, &locked, NULL, D3DLOCK_DISCARD);
-
} else {
-
d3drect.left = x;
-
d3drect.right = x + w;
-
d3drect.top = y;
-
d3drect.bottom = y + h;
-
result = IDirect3DTexture9_LockRect(texture, 0, &locked, &d3drect, 0);
-
}
-
-
-
if (FAILED(result)) {
-
return D3D_SetError("LockRect()", result);
-
}
-
-
-
src = (const Uint8 *)pixels;
-
dst = locked.pBits;
-
length = w * SDL_BYTESPERPIXEL(format);
-
if (length == pitch && length == locked.Pitch) {
-
SDL_memcpy(dst, src, length*h);
-
} else {
-
if (length > pitch) {
-
length = pitch;
-
}
-
if (length > locked.Pitch) {
-
length = locked.Pitch;
-
}
-
for (row = 0; row < h; ++row) {
-
SDL_memcpy(dst, src, length);
-
src += pitch;
-
dst += locked.Pitch;
-
}
-
}
-
IDirect3DTexture9_UnlockRect(texture, 0);
-
-
-
return 0;
-
}
從代碼中可以看出,該函數首先調用IDirect3DTexture9_LockRect()鎖定紋理,然後使用SDL_memcpy()將新的像素數據拷貝至紋理(SDL_memcpy()實際上就是memcpy()), 最後使用IDirect3DTexture9_UnlockRect()解鎖紋理。
2.
OpenGL
OpenGL渲染器中對應UpdateTexture()的函數是GL_UpdateTexture(),它的源代碼如下所示(位於render\opengl\SDL_render_gl.c)。
-
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
-
const SDL_Rect * rect, const void *pixels, int pitch)
-
{
-
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
-
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
-
-
-
GL_ActivateRenderer(renderer);
-
-
-
renderdata->glEnable(data->type);
-
renderdata->glBindTexture(data->type, data->texture);
-
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
-
(pitch / SDL_BYTESPERPIXEL(texture->format)));
-
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
-
rect->h, data->format, data->formattype,
-
pixels);
-
if (data->yuv) {
-
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / 2));
-
-
-
-
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
-
if (texture->format == SDL_PIXELFORMAT_YV12) {
-
renderdata->glBindTexture(data->type, data->vtexture);
-
} else {
-
renderdata->glBindTexture(data->type, data->utexture);
-
}
-
renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
-
rect->w/2, rect->h/2,
-
data->format, data->formattype, pixels);
-
-
-
-
pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
-
if (texture->format == SDL_PIXELFORMAT_YV12) {
-
renderdata->glBindTexture(data->type, data->utexture);
-
} else {
-
renderdata->glBindTexture(data->type, data->vtexture);
-
}
-
renderdata->glTexSubImage2D(data->type, 0, rect->x/2, rect->y/2,
-
rect->w/2, rect->h/2,
-
data->format, data->formattype, pixels);
-
}
-
renderdata->glDisable(data->type);
-
-
-
return GL_CheckError("glTexSubImage2D()", renderer);
-
}
從代碼中可以看出,該函數調用了OpenGL的API函數glBindTexture (),glTexSubImage2D()等更新了一個紋理。
在這裏有一點需要注意,如果輸入像素格式是YUV,就會使用3個紋理,對於多出的那2個紋理會單獨進行處理。
3.
Software
Software渲染器中對應UpdateTexture()的函數是SW_UpdateTexture(),它的源代碼如下所示(位於render\software\SDL_render_sw.c)。
-
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
-
const SDL_Rect * rect, const void *pixels, int pitch)
-
{
-
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
-
Uint8 *src, *dst;
-
int row;
-
size_t length;
-
-
-
if(SDL_MUSTLOCK(surface))
-
SDL_LockSurface(surface);
-
src = (Uint8 *) pixels;
-
dst = (Uint8 *) surface->pixels +
-
rect->y * surface->pitch +
-
rect->x * surface->format->BytesPerPixel;
-
length = rect->w * surface->format->BytesPerPixel;
-
for (row = 0; row < rect->h; ++row) {
-
SDL_memcpy(dst, src, length);
-
src += pitch;
-
dst += surface->pitch;
-
}
-
if(SDL_MUSTLOCK(surface))
-
SDL_UnlockSurface(surface);
-
return 0;
-
}
該函數的源代碼還沒有詳細分析。其中最關鍵的函數要數SDL_memcpy()了,正是這個函數更新了紋理的像素數據。但是Software渲染器紋理修改的時候是否需要Lock()和Unlock()呢?這一點一直也沒太搞清。