2009-11-12 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / writeablebitmap.cpp
blob027a4bc00b89e59e53c5da01837948c40bcfcc6d
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * writeablebitmap.cpp
5 * Contact:
6 * Moonlight List (moonlight-list@lists.ximian.com)
8 * Copyright 2007-2008 Novell, Inc. (http://www.novell.com)
10 * See the LICENSE file included with the distribution for details.
14 #include "runtime.h"
15 #include "transform.h"
16 #include "rect.h"
17 #include "region.h"
18 #include "panel.h"
19 #include "writeablebitmap.h"
21 WriteableBitmap::WriteableBitmap ()
23 SetObjectType (Type::WRITEABLEBITMAP);
24 pthread_mutex_init (&surface_mutex, NULL);
27 gpointer
28 WriteableBitmap::InitializeFromBitmapSource (BitmapSource *source)
30 cairo_t *cr;
32 if (!source)
33 return NULL;
35 SetPixelHeight (source->GetPixelHeight ());
36 SetPixelWidth (source->GetPixelWidth ());
37 SetPixelFormat (PixelFormatPbgra32);
39 image_surface = cairo_image_surface_create (GetPixelFormat () == PixelFormatBgr32 ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32, GetPixelWidth (), GetPixelHeight ());
40 cr = cairo_create (image_surface);
41 cairo_set_source_surface (cr, source->GetImageSurface (), 0, 0);
42 cairo_paint (cr);
43 cairo_destroy (cr);
45 SetBitmapData (cairo_image_surface_get_data (image_surface));
47 return GetBitmapData ();
50 WriteableBitmap::~WriteableBitmap ()
52 pthread_mutex_destroy (&surface_mutex);
55 void
56 WriteableBitmap::Lock ()
58 pthread_mutex_lock (&surface_mutex);
61 void
62 WriteableBitmap::Unlock ()
64 pthread_mutex_unlock (&surface_mutex);
67 cairo_surface_t *
68 WriteableBitmap::GetSurface (cairo_t *cr)
70 return image_surface;
73 void
74 WriteableBitmap::Render (UIElement *element, Transform *transform)
76 cairo_t *cr;
77 Region *region;
78 Rect bounds;
80 if (!element)
81 return;
83 if (!GetSurface (NULL)) {
84 Invalidate ();
87 cr = cairo_create (GetSurface (NULL));
89 // Region is the entire WB size
90 region = new Region (Rect (0, 0, GetPixelWidth (), GetPixelHeight ()));
92 // FIXME is this supposed to clear the surface?
93 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
94 cairo_paint (cr);
95 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
97 cairo_matrix_t xform;
98 if (transform)
99 transform->GetTransform (&xform);
101 element->Paint (cr, region, &xform);
103 cairo_destroy (cr);
104 cairo_surface_flush (image_surface);