summaryrefslogtreecommitdiff
path: root/boilerplate/cairo-boilerplate-beos.cpp
blob: 8a1b1afb512b868a0103269203312a74ba6d7320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* vim:set ts=8 sw=4 noet cin: */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Takashi Toyoshima <toyoshim@be-in.org>
 *   Fredrik Holmqvist <thesuckiestemail@yahoo.se>
 *   Christian Biesinger <cbiesinger@web.de>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "cairo-boilerplate.h"
#include <cairo-beos.h>

// Part of this code was originally part of
// xpfe/bootstrap/nsNativeAppSupportBeOS.cpp in the Mozilla source code.

#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Bitmap.h>

class CairoTestWindow : public BWindow
{
public:
    CairoTestWindow(BRect frame, const char* title);
    virtual ~CairoTestWindow();
    BView* View() const { return mView; }
private:
    BView* mView;
};

CairoTestWindow::CairoTestWindow(BRect frame, const char* title)
    : BWindow(frame, title, B_TITLED_WINDOW,
	      B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
{
    mView = new BView(frame, "CairoWindowTestView", B_FOLLOW_ALL_SIDES, 0);
    AddChild(mView);
    Show();

    // Make sure the window is actually on screen
    Lock();
    Sync();
    mView->SetViewColor(B_TRANSPARENT_COLOR);
    mView->Sync();
    Unlock();
}

CairoTestWindow::~CairoTestWindow()
{
    RemoveChild(mView);
    delete mView;
}


class nsBeOSApp : public BApplication
{
public:
    nsBeOSApp(sem_id sem) : BApplication(GetAppSig()), init(sem)
    {}

    void ReadyToRun()
    {
	release_sem(init);
    }

    static int32 Main(void *args)
    {
	nsBeOSApp *app = new nsBeOSApp( (sem_id)args );
	if(app == NULL)
	    return B_ERROR;
	return app->Run();
    }

private:

    const char *GetAppSig()
    {
	return "application/x-vnd.cairo-test-app";
    }

    sem_id init;
}; //class nsBeOSApp

class AppRunner
{
    public:
	AppRunner();
	~AppRunner();
};

AppRunner::AppRunner()
{
    if (be_app)
	return;

    sem_id initsem = create_sem(0, "Cairo BApplication init");
    if (initsem < B_OK) {
	fprintf (stderr, "Error creating BeOS initialization semaphore\n");
	return;
    }

    thread_id tid = spawn_thread(nsBeOSApp::Main, "Cairo/BeOS test", B_NORMAL_PRIORITY, (void *)initsem);
    if (tid < B_OK || B_OK != resume_thread(tid)) {
	fprintf (stderr, "Error spawning thread\n");
	return;
    }

    if (B_OK != acquire_sem(initsem)) {
	fprintf (stderr, "Error acquiring semaphore\n");
	return;
    }

    delete_sem(initsem);
    return;
}

AppRunner::~AppRunner()
{
    if (be_app) {
	if (be_app->Lock())
	    be_app->Quit();
	delete be_app;
	be_app = NULL;
    }
}

// Make sure that the BApplication is initialized
static AppRunner sAppRunner;

struct beos_boilerplate_closure {
    BView* view;
    BBitmap* bitmap;
    BWindow* window;
};

// Test a real window
static cairo_surface_t *
_cairo_boilerplate_beos_create_surface (const char		  *name,
					cairo_content_t 	   content,
					double			   width,
					double			   height,
					cairo_boilerplate_mode_t   mode,
					void			 **closure)
{
    float right = width ? width - 1 : 0;
    float bottom = height ? height - 1 : 0;
    BRect rect(0.0, 0.0, right, bottom);
    CairoTestWindow* wnd = new CairoTestWindow(rect, name);

    beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
    bclosure->view = wnd->View();
    bclosure->bitmap = NULL;
    bclosure->window = wnd;

    *closure = bclosure;

    return cairo_beos_surface_create(wnd->View());
}

static void
_cairo_boilerplate_beos_cleanup (void *closure)
{
    beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);

    bclosure->window->Lock();
    bclosure->window->Quit();

    delete bclosure;
}

// Test a bitmap
static cairo_surface_t *
_cairo_boilerplate_beos_create_surface_for_bitmap (const char		     *name,
						   cairo_content_t	      content,
						   double		      width,
						   double		      height,
						   cairo_boilerplate_mode_t   mode,
						   void 		    **closure)
{
    BRect rect(0.0, 0.0, width - 1, height - 1);
    color_space beosformat = (content == CAIRO_CONTENT_COLOR_ALPHA) ? B_RGBA32
								    : B_RGB32;
    BBitmap* bmp = new BBitmap(rect, beosformat, true);
    BView* view = new BView(rect, "Cairo test view", B_FOLLOW_ALL_SIDES, 0);
    bmp->AddChild(view);

    beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
    bclosure->view = view;
    bclosure->bitmap = bmp;
    bclosure->window = NULL;
    *closure = bclosure;

    return cairo_beos_surface_create_for_bitmap(view, bmp);
}

static void
_cairo_boilerplate_beos_cleanup_bitmap (void *closure)
{
    beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);

    bclosure->bitmap->RemoveChild(bclosure->view);


    delete bclosure->view;
    delete bclosure->bitmap;

    delete bclosure;
}

static const cairo_boilerplate_target_t targets[] = {
    /* BeOS sometimes produces a slightly different image. Perhaps this
     * is related to the fact that it doesn't use premultiplied alpha...
     * Just ignore the small difference. */
    {
	"beos", "beos", NULL, NULL,
	CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
	_cairo_boilerplate_beos_create_surface,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_beos_cleanup
    },
    {
	"beos-bitmap", "beos", NULL, NULL,
	CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
	_cairo_boilerplate_beos_create_surface_for_bitmap,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_beos_cleanup_bitmap
    },
    {
	"beos-bitmap", "beos", NULL, NULL,
	CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR_ALPHA, 1,
	_cairo_boilerplate_beos_create_surface_for_bitmap,
	NULL, NULL,
	_cairo_boilerplate_get_image_surface,
	cairo_surface_write_to_png,
	_cairo_boilerplate_beos_cleanup_bitmap
    },
};
CAIRO_BOILERPLATE (beos, targets)