static int initTextures(void) { struct TexSquare *tsq=0; GLfloat texpercx, texpercy; int s; int x=0, y=0; // textures smaller than 64x64 might not be supported s=64; while (s<image_width) s*=2; texture_width=s; s=64; while (s<image_height) s*=2; texture_height=s; if (!is_yuv) gl_internal_format = getInternalFormat(); /* Test the max texture size */ do { GLint w; glTexImage2D (GL_PROXY_TEXTURE_2D, 0, gl_internal_format, texture_width, texture_height, 0, gl_bitmap_format, gl_bitmap_type, NULL); glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); if (w >= texture_width) break; mp_msg (MSGT_VO, MSGL_V, "[gl_tiled] Needed texture [%dx%d] too big, trying ", texture_width, texture_height); if (texture_width > texture_height) texture_width /= 2; else texture_height /= 2; mp_msg (MSGT_VO, MSGL_V, "[%dx%d] !\n", texture_width, texture_height); if(texture_width < 64 || texture_height < 64) { mp_msg (MSGT_VO, MSGL_FATAL, "[gl_tiled] Give up .. usable texture size not available, or texture config error !\n"); return -1; } } while (texture_width > 1 && texture_height > 1); #ifdef TEXTURE_WIDTH texture_width = TEXTURE_WIDTH; #endif #ifdef TEXTURE_HEIGHT texture_height = TEXTURE_HEIGHT; #endif texnumx = image_width / texture_width; if ((image_width % texture_width) > 0) texnumx++; texnumy = image_height / texture_height; if ((image_height % texture_height) > 0) texnumy++; mp_msg(MSGT_VO, MSGL_V, "[gl_tiled] Creating %dx%d textures of size %dx%d ...\n", texnumx, texnumy, texture_width,texture_height); /* Allocate the texture memory */ texpercx = (GLfloat) texture_width / (GLfloat) image_width; texpercy = (GLfloat) texture_height / (GLfloat) image_height; free(texgrid); texgrid = calloc (texnumx * texnumy, sizeof (struct TexSquare)); raw_line_len = image_width * image_bytes; mp_msg (MSGT_VO, MSGL_DBG2, "[gl_tiled] texture-usage %d*width=%d, %d*height=%d\n", (int) texnumx, (int) texture_width, (int) texnumy, (int) texture_height); tsq = texgrid; for (y = 0; y < texnumy; y++) { for (x = 0; x < texnumx; x++) { tsq->fx = x * texpercx; tsq->fy = y * texpercy; tsq->fw = texpercx; tsq->fh = texpercy; tsq->texobj=0; tsq->uvtexobjs[0] = tsq->uvtexobjs[1] = 0; glGenTextures (1, &(tsq->texobj)); glBindTexture (GL_TEXTURE_2D, tsq->texobj); if (is_yuv) { glGenTextures(2, tsq->uvtexobjs); mpglActiveTexture(GL_TEXTURE1); glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[0]); mpglActiveTexture(GL_TEXTURE2); glBindTexture (GL_TEXTURE_2D, tsq->uvtexobjs[1]); mpglActiveTexture(GL_TEXTURE0); } glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR, texture_width, texture_height, 0); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); if (is_yuv) { int xs, ys, depth; int chroma_clear_val = 128; mp_get_chroma_shift(image_format, &xs, &ys, &depth); chroma_clear_val >>= -depth & 7; mpglActiveTexture(GL_TEXTURE1); glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR, texture_width >> xs, texture_height >> ys, chroma_clear_val); mpglActiveTexture(GL_TEXTURE2); glCreateClearTex(GL_TEXTURE_2D, gl_internal_format, gl_bitmap_format, gl_bitmap_type, GL_LINEAR, texture_width >> xs, texture_height >> ys, chroma_clear_val); mpglActiveTexture(GL_TEXTURE0); } tsq++; } /* for all texnumx */ } /* for all texnumy */ return 0; }
/** * \brief Initialize a (new or reused) OpenGL context. * set global gl-related variables to their default values */ static int initGl(uint32_t d_width, uint32_t d_height) { GLint scale_type = get_scale_type(0); autodetectGlExtensions(); gl_target = use_rectangle == 1 ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D; yuvconvtype = SET_YUV_CONVERSION(use_yuv) | SET_YUV_LUM_SCALER(lscale) | SET_YUV_CHROM_SCALER(cscale); texSize(image_width, image_height, &texture_width, &texture_height); mpglDisable(GL_BLEND); mpglDisable(GL_DEPTH_TEST); mpglDepthMask(GL_FALSE); mpglDisable(GL_CULL_FACE); mpglEnable(gl_target); mpglDrawBuffer(vo_doublebuffering?GL_BACK:GL_FRONT); mpglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); mp_msg(MSGT_VO, MSGL_V, "[gl] Creating %dx%d texture...\n", texture_width, texture_height); glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, scale_type, texture_width, texture_height, 0); if (mipmap_gen) mpglTexParameteri(gl_target, GL_GENERATE_MIPMAP, GL_TRUE); if (is_yuv) { int i; int xs, ys; scale_type = get_scale_type(1); mp_get_chroma_shift(image_format, &xs, &ys); mpglGenTextures(21, default_texs); default_texs[21] = 0; for (i = 0; i < 7; i++) { mpglActiveTexture(GL_TEXTURE1 + i); mpglBindTexture(GL_TEXTURE_2D, default_texs[i]); mpglBindTexture(GL_TEXTURE_RECTANGLE, default_texs[i + 7]); mpglBindTexture(GL_TEXTURE_3D, default_texs[i + 14]); } mpglActiveTexture(GL_TEXTURE1); glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, scale_type, texture_width >> xs, texture_height >> ys, 128); if (mipmap_gen) mpglTexParameteri(gl_target, GL_GENERATE_MIPMAP, GL_TRUE); mpglActiveTexture(GL_TEXTURE2); glCreateClearTex(gl_target, gl_texfmt, gl_format, gl_type, scale_type, texture_width >> xs, texture_height >> ys, 128); if (mipmap_gen) mpglTexParameteri(gl_target, GL_GENERATE_MIPMAP, GL_TRUE); mpglActiveTexture(GL_TEXTURE0); mpglBindTexture(gl_target, 0); } if (is_yuv || custom_prog) { if ((MASK_NOT_COMBINERS & (1 << use_yuv)) || custom_prog) { if (!mpglGenPrograms || !mpglBindProgram) { mp_msg(MSGT_VO, MSGL_ERR, "[gl] fragment program functions missing!\n"); } else { mpglGenPrograms(1, &fragprog); mpglBindProgram(GL_FRAGMENT_PROGRAM, fragprog); } } update_yuvconv(); } resize(d_width, d_height); mpglClearColor( 0.0f,0.0f,0.0f,0.0f ); mpglClear( GL_COLOR_BUFFER_BIT ); if (mpglSwapInterval && swap_interval >= 0) mpglSwapInterval(swap_interval); return 1; }
/** * Creates the textures and the display list needed for displaying * an OSD part. * Callback function for vo_draw_text(). */ static void create_osd_texture(int x0, int y0, int w, int h, unsigned char *src, unsigned char *srca, int stride) { // initialize to 8 to avoid special-casing on alignment int sx = 8, sy = 8; GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST; if (w <= 0 || h <= 0 || stride < w) { mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n"); return; } texSize(w, h, &sx, &sy); if (osdtexCnt >= MAX_OSD_PARTS) { mp_msg(MSGT_VO, MSGL_ERR, "Too many OSD parts, contact the developers!\n"); return; } // create Textures for OSD part mpglGenTextures(1, &osdtex[osdtexCnt]); mpglBindTexture(gl_target, osdtex[osdtexCnt]); glCreateClearTex(gl_target, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0); glUploadTex(gl_target, GL_LUMINANCE, GL_UNSIGNED_BYTE, src, stride, 0, 0, w, h, 0); #ifndef FAST_OSD mpglGenTextures(1, &osdatex[osdtexCnt]); mpglBindTexture(gl_target, osdatex[osdtexCnt]); glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0); { int i; char *tmp = malloc(stride * h); // convert alpha from weird MPlayer scale. // in-place is not possible since it is reused for future OSDs for (i = h * stride - 1; i >= 0; i--) tmp[i] = -srca[i]; glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, tmp, stride, 0, 0, w, h, 0); free(tmp); } #endif mpglBindTexture(gl_target, 0); // Create a list for rendering this OSD part #ifndef FAST_OSD osdaDispList[osdtexCnt] = mpglGenLists(1); mpglNewList(osdaDispList[osdtexCnt], GL_COMPILE); // render alpha mpglBindTexture(gl_target, osdatex[osdtexCnt]); glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0); mpglEndList(); #endif osdDispList[osdtexCnt] = mpglGenLists(1); mpglNewList(osdDispList[osdtexCnt], GL_COMPILE); // render OSD mpglBindTexture(gl_target, osdtex[osdtexCnt]); glDrawTex(x0, y0, w, h, 0, 0, w, h, sx, sy, use_rectangle == 1, 0, 0); mpglEndList(); osdtexCnt++; }
/** * \brief construct display list from ass image list * \param img image list to create OSD from. * A value of NULL has the same effect as clearEOSD() */ static void genEOSD(struct mp_eosd_image_list *imgs) { int sx, sy; int tinytexcur = 0; int smalltexcur = 0; GLuint *curtex; GLint scale_type = scaled_osd ? GL_LINEAR : GL_NEAREST; struct mp_eosd_image *img = eosd_image_first(imgs); struct mp_eosd_image *i; if (imgs->changed == 0) // there are elements, but they are unchanged return; if (img && imgs->changed == 1) // there are elements, but they just moved goto skip_upload; clearEOSD(); if (!img) return; if (!largeeosdtex[0]) { mpglGenTextures(2, largeeosdtex); mpglBindTexture(gl_target, largeeosdtex[0]); glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, LARGE_EOSD_TEX_SIZE, LARGE_EOSD_TEX_SIZE, 0); mpglBindTexture(gl_target, largeeosdtex[1]); glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, LARGE_EOSD_TEX_SIZE, LARGE_EOSD_TEX_SIZE, 0); } for (i = img; i; i = eosd_image_next(imgs)) { if (i->w <= 0 || i->h <= 0 || i->stride < i->w) continue; if (is_tinytex(i, tinytexcur)) tinytexcur++; else if (is_smalltex(i, smalltexcur)) smalltexcur++; else eosdtexCnt++; } mp_msg(MSGT_VO, MSGL_DBG2, "EOSD counts (tiny, small, all): %i, %i, %i\n", tinytexcur, smalltexcur, eosdtexCnt); if (eosdtexCnt) { eosdtex = calloc(eosdtexCnt, sizeof(GLuint)); mpglGenTextures(eosdtexCnt, eosdtex); } tinytexcur = smalltexcur = 0; for (i = eosd_image_first(imgs), curtex = eosdtex; i; i = eosd_image_next(imgs)) { int x = 0, y = 0; if (i->w <= 0 || i->h <= 0 || i->stride < i->w) { mp_msg(MSGT_VO, MSGL_V, "Invalid dimensions OSD for part!\n"); continue; } if (is_tinytex(i, tinytexcur)) { tinytex_pos(tinytexcur, &x, &y); mpglBindTexture(gl_target, largeeosdtex[0]); tinytexcur++; } else if (is_smalltex(i, smalltexcur)) { smalltex_pos(smalltexcur, &x, &y); mpglBindTexture(gl_target, largeeosdtex[1]); smalltexcur++; } else { texSize(i->w, i->h, &sx, &sy); mpglBindTexture(gl_target, *curtex++); glCreateClearTex(gl_target, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, scale_type, sx, sy, 0); } glUploadTex(gl_target, GL_ALPHA, GL_UNSIGNED_BYTE, i->bitmap, i->stride, x, y, i->w, i->h, 0); } eosdDispList = mpglGenLists(1); skip_upload: mpglNewList(eosdDispList, GL_COMPILE); tinytexcur = smalltexcur = 0; for (i = eosd_image_first(imgs), curtex = eosdtex; i; i = eosd_image_next(imgs)) { int x = 0, y = 0; if (i->w <= 0 || i->h <= 0 || i->stride < i->w) continue; mpglColor4ub(i->color >> 24, (i->color >> 16) & 0xff, (i->color >> 8) & 0xff, 255 - (i->color & 0xff)); if (is_tinytex(i, tinytexcur)) { tinytex_pos(tinytexcur, &x, &y); sx = sy = LARGE_EOSD_TEX_SIZE; mpglBindTexture(gl_target, largeeosdtex[0]); tinytexcur++; } else if (is_smalltex(i, smalltexcur)) { smalltex_pos(smalltexcur, &x, &y); sx = sy = LARGE_EOSD_TEX_SIZE; mpglBindTexture(gl_target, largeeosdtex[1]); smalltexcur++; } else { texSize(i->w, i->h, &sx, &sy); mpglBindTexture(gl_target, *curtex++); } glDrawTex(i->dst_x, i->dst_y, i->w, i->h, x, y, i->w, i->h, sx, sy, use_rectangle == 1, 0, 0); } mpglEndList(); mpglBindTexture(gl_target, 0); }