2009-12-07 Rolf Bjarne Kvinge <RKvinge@novell.com>
[moon.git] / src / bitmapsource.cpp
blob5c89a8054992f6d73d1f97d5b4d15f8f953488d4
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * bitmapsource.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 <config.h>
16 #include <stdio.h>
18 #include "application.h"
19 #include "bitmapsource.h"
20 #include "runtime.h"
22 BitmapSource::BitmapSource ()
24 SetObjectType (Type::BITMAPSOURCE);
25 image_surface = NULL;
26 native_surface = NULL;
27 data = NULL;
28 own_data = true;
31 BitmapSource::~BitmapSource ()
33 if (image_surface)
34 cairo_surface_destroy (image_surface);
35 if (native_surface)
36 cairo_surface_destroy (native_surface);
38 if (data && own_data)
39 g_free (data);
42 gpointer
43 BitmapSource::GetBitmapData ()
45 return data;
48 void
49 BitmapSource::SetBitmapData (gpointer data, bool own_data)
51 if (data == NULL) {
52 SetPixelWidth (0.0);
53 SetPixelHeight (0.0);
56 if (this->data && this->own_data)
57 g_free (this->data);
58 this->own_data = own_data;
59 this->data = data;
62 void
63 BitmapSource::Invalidate ()
65 if (GetPixelWidth () == 0 || GetPixelHeight () == 0)
66 return;
68 if (native_surface) {
69 cairo_surface_destroy (native_surface);
70 native_surface = NULL;
72 if (image_surface)
73 cairo_surface_destroy (image_surface);
75 image_surface = cairo_image_surface_create_for_data ((unsigned char *) GetBitmapData (), GetPixelFormat () == PixelFormatBgr32 ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32, GetPixelWidth (), GetPixelHeight (), GetPixelWidth ()*4);
77 Emit (BitmapSource::PixelDataChangedEvent);
80 cairo_surface_t *
81 BitmapSource::GetSurface (cairo_t *cr)
83 if (image_surface == NULL)
84 return NULL;
86 if (native_surface)
87 return native_surface;
89 if (cr == NULL)
90 return image_surface;
92 native_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
93 cairo_surface_get_content (image_surface),
94 GetPixelWidth (), GetPixelHeight ());
96 cairo_t *context = cairo_create (native_surface);
98 cairo_set_source_surface (context, image_surface, 0, 0);
99 cairo_pattern_set_filter (cairo_get_source (context), CAIRO_FILTER_FAST);
101 cairo_paint (context);
102 cairo_destroy (context);
104 return native_surface;