1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 #include "core/GP_Context.h"
26 #include "core/GP_Blit.h"
27 #include "core/GP_Debug.h"
29 #include "GP_BackendVirtual.h"
32 /* Original backend */
38 static void virt_flip(GP_Backend
*self
)
40 struct virt_priv
*virt
= GP_BACKEND_PRIV(self
);
42 /* Convert and copy the buffer */
43 GP_Blit(self
->context
, 0, 0, self
->context
->w
, self
->context
->h
,
44 virt
->backend
->context
, 0, 0);
46 /* Call blit on original backend */
47 virt
->backend
->Flip(virt
->backend
);
50 static void virt_update_rect(GP_Backend
*self
, GP_Coord x0
, GP_Coord y0
,
51 GP_Coord x1
, GP_Coord y1
)
53 struct virt_priv
*virt
= GP_BACKEND_PRIV(self
);
55 /* Convert and copy the buffer */
56 GP_BlitXYXY(self
->context
, x0
, y0
, x1
, y1
,
57 virt
->backend
->context
, x0
, y0
);
59 /* Call blit on original backend */
60 virt
->backend
->UpdateRect(virt
->backend
, x0
, y0
, x1
, y1
);
63 static int virt_set_attributes(struct GP_Backend
*self
,
64 uint32_t w
, uint32_t h
,
67 struct virt_priv
*virt
= GP_BACKEND_PRIV(self
);
70 ret
= virt
->backend
->SetAttributes(virt
->backend
, w
, h
, caption
);
75 /* If backend was resized, update our buffer as well */
77 GP_ContextResize(self
->context
, w
, h
);
82 static void virt_poll(GP_Backend
*self
)
84 struct virt_priv
*virt
= GP_BACKEND_PRIV(self
);
86 virt
->backend
->Poll(virt
->backend
);
89 static void virt_exit(GP_Backend
*self
)
91 struct virt_priv
*virt
= GP_BACKEND_PRIV(self
);
93 GP_ContextFree(self
->context
);
95 if (virt
->flags
& GP_BACKEND_CALL_EXIT
)
96 virt
->backend
->Exit(virt
->backend
);
101 GP_Backend
*GP_BackendVirtualInit(GP_Backend
*backend
,
102 GP_PixelType pixel_type
, int flags
)
105 struct virt_priv
*virt
;
107 self
= malloc(sizeof(GP_Backend
) +
108 sizeof(struct virt_priv
));
111 GP_DEBUG(1, "Malloc failed :(");
115 memset(self
, 0, sizeof(GP_Backend
));
117 /* Create new buffer with different context type */
118 self
->context
= GP_ContextAlloc(backend
->context
->w
, backend
->context
->h
,
121 if (self
->context
== NULL
)
124 virt
= GP_BACKEND_PRIV(self
);
125 virt
->backend
= backend
;
128 /* Initalize new backend */
129 self
->name
= "Virtual Backend";
130 self
->Flip
= virt_flip
;
131 self
->UpdateRect
= virt_update_rect
;
132 self
->Exit
= virt_exit
;
135 self
->Poll
= virt_poll
;
137 if (backend
->SetAttributes
)
138 self
->SetAttributes
= virt_set_attributes
;