5
// Mathijs Dumon <mathijsken@hotmail.com>
7
// Copyright (c) 2010 Mathijs Dumon
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34
public delegate Cairo.ImageSurface GetDefaultSurface ();
36
public class TextureHolder : IDisposable {
38
private int texture_size;
40
protected Cairo.ImageSurface default_surface;
41
public Cairo.ImageSurface DefaultSurface {
43
if (default_surface == null && GetDefaultSurface != null) {
44
default_surface = ClutterFlowActor.MakeReflection (GetDefaultSurface ());
46
return default_surface;
50
protected IntPtr default_texture;
51
public IntPtr DefaultTexture {
53
if (default_texture == IntPtr.Zero) {
54
SetupDefaultTexture ();
56
return default_texture;
60
protected IntPtr shade_texture;
61
public IntPtr ShadeTexture {
63
if (shade_texture == IntPtr.Zero) {
70
private GetDefaultSurface GetDefaultSurface;
73
#region Initialisation
74
public TextureHolder (int texture_size, GetDefaultSurface get_default_surface)
76
this.texture_size = texture_size;
77
this.GetDefaultSurface = get_default_surface;
80
protected bool disposed = false;
81
public virtual void Dispose ()
89
if (default_surface != null) {
90
((IDisposable) default_surface).Dispose ();
92
Cogl.Handle.Unref (default_texture);
93
Cogl.Handle.Unref (shade_texture);
94
default_texture = IntPtr.Zero;
95
shade_texture = IntPtr.Zero;
98
private void SetupDefaultTexture ()
100
if (default_texture == IntPtr.Zero) {
101
if (DefaultSurface != null) {
103
if (DefaultSurface.Format == Cairo.Format.ARGB32) {
104
fm = PixelFormat.Argb8888Pre;
106
else //if (DefaultSurface.Format == Cairo.Format.RGB24)
107
fm = PixelFormat.Rgb888;
110
default_texture = ClutterHelper.cogl_texture_new_from_data((uint) DefaultSurface.Width, (uint) DefaultSurface.Height, Cogl.TextureFlags.None,
111
fm, Cogl.PixelFormat.Any, (uint) DefaultSurface.Stride, DefaultSurface.DataPtr);
114
default_texture = Cogl.Texture.NewWithSize ((uint) texture_size, (uint) texture_size,
115
Cogl.TextureFlags.None, Cogl.PixelFormat.Any);
120
private void SetupShadeTexture ()
122
if (shade_texture==IntPtr.Zero) {
124
Gdk.Pixbuf finalPb = new Gdk.Pixbuf (Colorspace.Rgb, true, 8, texture_size, texture_size);
127
int dst_rowstride = finalPb.Rowstride;
128
int dst_width = finalPb.Width;
129
int shd_width = (int) ((float) dst_width * 0.25f);
130
int dst_height = finalPb.Height;
131
byte * dst_byte = (byte *) finalPb.Pixels;
132
byte * dst_base = dst_byte;
134
for (int j = 0; j < dst_height; j++) {
135
dst_byte = ((byte *) dst_base) + j * dst_rowstride;
136
for (int i = 0; i < dst_width; i++) {
141
*dst_byte++ = (byte) (255 * (float) (i - shd_width) / (float) (dst_width - shd_width));
147
shade_texture = ClutterHelper.cogl_texture_new_from_data((uint) finalPb.Width, (uint) finalPb.Height, Cogl.TextureFlags.None,
148
PixelFormat.Rgba8888, Cogl.PixelFormat.Any, (uint) finalPb.Rowstride, finalPb.Pixels);