video-play: handle arbitrary cursor sizes

video-play: only allocate the texture when we know the size and
format of the cursor bitmap.
video-src: make cursor size defined with constants.
This commit is contained in:
Wim Taymans 2018-11-30 12:09:31 +01:00
parent 218fd081df
commit 8205486554
2 changed files with 46 additions and 22 deletions

View file

@ -80,6 +80,8 @@ struct data {
SDL_Rect cursor_rect;
};
static Uint32 id_to_sdl_format(struct data *data, uint32_t id);
static void handle_events(struct data *data)
{
SDL_Event event;
@ -146,6 +148,15 @@ on_stream_process(void *_data)
data->cursor_rect.w = mb->width;
data->cursor_rect.h = mb->height;
if (data->cursor == NULL) {
data->cursor = SDL_CreateTexture(data->renderer,
id_to_sdl_format(data, mb->format),
SDL_TEXTUREACCESS_STREAMING,
mb->width, mb->height);
SDL_SetTextureBlendMode(data->cursor, SDL_BLENDMODE_BLEND);
}
if (SDL_LockTexture(data->cursor, NULL, &cdata, &cstride) < 0) {
fprintf(stderr, "Couldn't lock cursor texture: %s\n", SDL_GetError());
goto done;
@ -308,12 +319,6 @@ on_stream_format_changed(void *_data, const struct spa_pod *format)
SDL_LockTexture(data->texture, NULL, &d, &data->stride);
SDL_UnlockTexture(data->texture);
data->cursor = SDL_CreateTexture(data->renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
64, 64);
SDL_SetTextureBlendMode(data->cursor, SDL_BLENDMODE_BLEND);
data->rect.x = 0;
data->rect.y = 0;
data->rect.w = data->format.size.width;
@ -335,12 +340,14 @@ on_stream_format_changed(void *_data, const struct spa_pod *format)
t->param.idMeta, t->param_meta.Meta,
":", t->param_meta.type, "I", t->meta.VideoCrop,
":", t->param_meta.size, "i", sizeof(struct spa_meta_video_crop));
#define CURSOR_META_SIZE(w,h) (sizeof(struct spa_meta_cursor) + \
sizeof(struct spa_meta_bitmap) + w * h * 4)
params[3] = spa_pod_builder_object(&b,
t->param.idMeta, t->param_meta.Meta,
":", t->param_meta.type, "I", data->type.meta_cursor,
":", t->param_meta.size, "i", sizeof(struct spa_meta_cursor) +
sizeof(struct spa_meta_bitmap) +
64 * 64 * 4);
":", t->param_meta.size, "iru", CURSOR_META_SIZE(64,64),
SPA_POD_PROP_MIN_MAX(CURSOR_META_SIZE(1,1),
CURSOR_META_SIZE(256,256)));
pw_stream_finish_format(stream, 0, params, 4);
}
@ -525,7 +532,8 @@ int main(int argc, char *argv[])
pw_main_loop_destroy(data.loop);
SDL_DestroyTexture(data.texture);
SDL_DestroyTexture(data.cursor);
if (data.cursor)
SDL_DestroyTexture(data.cursor);
SDL_DestroyRenderer(data.renderer);
SDL_DestroyWindow(data.window);

View file

@ -51,6 +51,9 @@ static inline void init_type(struct type *type, struct spa_type_map *map)
#define WIDTH 320
#define HEIGHT 200
#define CROP 8
#define CURSOR_WIDTH 64
#define CURSOR_HEIGHT 64
#define CURSOR_BPP 4
#define M_PI_M2 ( M_PI + M_PI )
@ -77,6 +80,24 @@ struct data {
double accumulator;
};
static void draw_elipse(uint32_t *dst, int width, int height, uint32_t color)
{
int i, j, r1, r2, r12, r22, r122;
r1 = width/2;
r12 = r1 * r1;
r2 = height/2;
r22 = r2 * r2;
r122 = r12 * r22;
for (i = -r2; i < r2; i++) {
for (j = -r1; j < r1; j++) {
dst[(i + r2)*width+(j+r1)] =
(i * i * r12 + j * j * r22 <= r122) ? color : 0x00000000;
}
}
}
static void on_timeout(void *userdata, uint64_t expirations)
{
struct data *data = userdata;
@ -129,9 +150,9 @@ static void on_timeout(void *userdata, uint64_t expirations)
mb = SPA_MEMBER(mcs, mcs->bitmap_offset, struct spa_meta_bitmap);
mb->format = data->type.video_format.ARGB;
mb->width = 64;
mb->height = 64;
mb->stride = 64 * 4;
mb->width = CURSOR_WIDTH;
mb->height = CURSOR_HEIGHT;
mb->stride = CURSOR_WIDTH * CURSOR_BPP;
mb->size = mb->stride * mb->height;
mb->offset = sizeof(struct spa_meta_bitmap);
@ -139,12 +160,7 @@ static void on_timeout(void *userdata, uint64_t expirations)
color = (cos(data->accumulator) + 1.0) * (1 << 23);
color |= 0xff000000;
for (i = 0; i < mb->height; i++) {
for (j = 0; j < mb->width; j++) {
int v = (i - 32) * (i - 32) + (j - 32) * (j - 32);
bitmap[i*64+j] = (v <= 32*32) ? color : 0x00000000;
}
}
draw_elipse(bitmap, mb->width, mb->height, color);
}
for (i = 0; i < data->format.size.height; i++) {
@ -227,12 +243,12 @@ on_stream_format_changed(void *_data, const struct spa_pod *format)
t->param.idMeta, t->param_meta.Meta,
":", t->param_meta.type, "I", t->meta.VideoCrop,
":", t->param_meta.size, "i", sizeof(struct spa_meta_video_crop));
#define CURSOR_META_SIZE(w,h) (sizeof(struct spa_meta_cursor) + \
sizeof(struct spa_meta_bitmap) + w * h * CURSOR_BPP)
params[3] = spa_pod_builder_object(&b,
t->param.idMeta, t->param_meta.Meta,
":", t->param_meta.type, "I", data->type.meta_cursor,
":", t->param_meta.size, "i", sizeof(struct spa_meta_cursor) +
sizeof(struct spa_meta_bitmap) +
64 * 64 * 4);
":", t->param_meta.size, "i", CURSOR_META_SIZE(CURSOR_WIDTH,CURSOR_HEIGHT));
pw_stream_finish_format(stream, 0, params, 4);
}