+static int copy_to_texture(void *lcd_base)
+{
+ char *dest;
+ int pitch, x, y;
+ int src_pitch;
+ void *pixels;
+ char *src;
+ int ret;
+
+ if (sdl.src_depth == sdl.depth) {
+ SDL_UpdateTexture(sdl.texture, NULL, lcd_base, sdl.pitch);
+ return 0;
+ }
+
+ /*
+ * We only support copying from an 8bpp to a 32bpp texture since the
+ * other cases are supported directly by the texture.
+ */
+ if (sdl.depth != 32 && sdl.src_depth != 8) {
+ printf("Need depth 32bpp for copy\n");
+ return -EINVAL;
+ }
+
+ ret = SDL_LockTexture(sdl.texture, NULL, &pixels, &pitch);
+ if (ret) {
+ printf("SDL lock %d: %s\n", ret, SDL_GetError());
+ return ret;
+ }
+
+ /* Copy the pixels one by one */
+ src_pitch = sdl.width * sdl.src_depth / 8;
+ for (y = 0; y < sdl.height; y++) {
+ char val;
+
+ dest = pixels + y * pitch;
+ src = lcd_base + src_pitch * y;
+ for (x = 0; x < sdl.width; x++, dest += 4) {
+ val = *src++;
+ dest[0] = val;
+ dest[1] = val;
+ dest[2] = val;
+ dest[3] = 0;
+ }
+ }
+ SDL_UnlockTexture(sdl.texture);
+
+ return 0;
+}
+