~ubuntu-branches/debian/jessie/banshee-community-extensions/jessie

« back to all changes in this revision

Viewing changes to src/ClutterFlow/ClutterFlow/TextureHolder.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-09-20 18:45:46 UTC
  • mfrom: (1.2.9 upstream) (5.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20110920184546-3ahue2qplydc4t0e
Tags: 2.2.0-1
* [4940fab] Imported Upstream version 2.2.0
  + Notable bug fixes:
    - Karaoke: Fix crash when switching to Now Playing
    - Lyrics: Fix crash when switching to Now Playing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// TextureHolder.cs
 
3
// 
 
4
// Author:
 
5
//       Mathijs Dumon <mathijsken@hotmail.com>
 
6
//
 
7
// Copyright (c) 2010 Mathijs Dumon
 
8
//
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
 
 
29
using Cogl;
 
30
using Gdk;
 
31
 
 
32
namespace ClutterFlow
 
33
{
 
34
    public delegate Cairo.ImageSurface GetDefaultSurface ();
 
35
 
 
36
    public class TextureHolder : IDisposable {
 
37
        #region Fields
 
38
        private int texture_size;
 
39
 
 
40
        protected Cairo.ImageSurface default_surface;
 
41
        public Cairo.ImageSurface DefaultSurface {
 
42
            get {
 
43
                if (default_surface == null && GetDefaultSurface != null) {
 
44
                    default_surface = ClutterFlowActor.MakeReflection (GetDefaultSurface ());
 
45
                }
 
46
                return default_surface;
 
47
            }
 
48
        }
 
49
 
 
50
        protected IntPtr default_texture;
 
51
        public IntPtr DefaultTexture {
 
52
            get {
 
53
                if (default_texture == IntPtr.Zero) {
 
54
                    SetupDefaultTexture ();
 
55
                }
 
56
                return default_texture;
 
57
            }
 
58
        }
 
59
 
 
60
        protected IntPtr shade_texture;
 
61
        public IntPtr ShadeTexture {
 
62
            get {
 
63
                if (shade_texture == IntPtr.Zero) {
 
64
                    SetupShadeTexture ();
 
65
                }
 
66
                return shade_texture;
 
67
            }
 
68
        }
 
69
 
 
70
        private GetDefaultSurface GetDefaultSurface;
 
71
        #endregion
 
72
 
 
73
        #region Initialisation
 
74
        public TextureHolder (int texture_size, GetDefaultSurface get_default_surface)
 
75
        {
 
76
            this.texture_size = texture_size;
 
77
            this.GetDefaultSurface = get_default_surface;
 
78
        }
 
79
 
 
80
        protected bool disposed = false;
 
81
        public virtual void Dispose ()
 
82
        {
 
83
            if (disposed) {
 
84
                return;
 
85
            }
 
86
            disposed = true;
 
87
 
 
88
 
 
89
            if (default_surface != null) {
 
90
                ((IDisposable) default_surface).Dispose ();
 
91
            }
 
92
            Cogl.Handle.Unref (default_texture);
 
93
            Cogl.Handle.Unref (shade_texture);
 
94
            default_texture = IntPtr.Zero;
 
95
            shade_texture = IntPtr.Zero;
 
96
        }
 
97
 
 
98
        private void SetupDefaultTexture ()
 
99
        {
 
100
            if (default_texture == IntPtr.Zero) {
 
101
                if (DefaultSurface != null) {
 
102
                    Cogl.PixelFormat fm;
 
103
                    if (DefaultSurface.Format == Cairo.Format.ARGB32) {
 
104
                        fm = PixelFormat.Argb8888Pre;
 
105
                    }
 
106
                    else //if (DefaultSurface.Format == Cairo.Format.RGB24)
 
107
                        fm = PixelFormat.Rgb888;
 
108
 
 
109
                    unsafe {
 
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);
 
112
                    }
 
113
                } else {
 
114
                    default_texture = Cogl.Texture.NewWithSize ((uint) texture_size, (uint) texture_size,
 
115
                                                             Cogl.TextureFlags.None, Cogl.PixelFormat.Any);
 
116
                }
 
117
            }
 
118
        }
 
119
 
 
120
        private void SetupShadeTexture ()
 
121
        {
 
122
            if (shade_texture==IntPtr.Zero) {
 
123
 
 
124
                Gdk.Pixbuf finalPb = new Gdk.Pixbuf (Colorspace.Rgb, true, 8, texture_size, texture_size);
 
125
 
 
126
                unsafe {
 
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;
 
133
 
 
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++) {
 
137
                            *dst_byte++ = 0x00;
 
138
                            *dst_byte++ = 0x00;
 
139
                            *dst_byte++ = 0x00;
 
140
                            if (i > shd_width)
 
141
                                *dst_byte++ = (byte) (255 * (float) (i - shd_width) / (float) (dst_width - shd_width));
 
142
                            else
 
143
                                *dst_byte++ = 0x00;
 
144
                        }
 
145
                    }
 
146
 
 
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);
 
149
                }
 
150
            }
 
151
        }
 
152
        #endregion
 
153
    }
 
154
}
 
155