~jamespharaoh/ubuntu/oneiric/unity/fix-for-880672

« back to all changes in this revision

Viewing changes to .pc/debian-changes-4.8.2-0ubuntu3/plugins/unityshell/src/BackgroundEffectHelper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-08-19 18:53:28 UTC
  • Revision ID: james.westby@ubuntu.com-20110819185328-qaxk9eepjaghewu9
Tags: 4.8.2-0ubuntu4
Backport another trunk commit to fix an alt-tab segfault

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
2
 
/*
3
 
 * Copyright (C) 2010 Canonical Ltd
4
 
 *
5
 
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 3 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 *
17
 
 * Authored by: Jay Taoko <jay.taoko@canonical.com>
18
 
 */
19
 
 
20
 
#include "BackgroundEffectHelper.h"
21
 
 
22
 
using namespace unity;
23
 
 
24
 
std::list<BackgroundEffectHelper*> BackgroundEffectHelper::registered_list_;
25
 
 
26
 
nux::Property<BlurType> BackgroundEffectHelper::blur_type (BLUR_ACTIVE);
27
 
nux::Property<float> BackgroundEffectHelper::sigma_high (5.0f);
28
 
nux::Property<float> BackgroundEffectHelper::sigma_med  (4.0f);
29
 
nux::Property<float> BackgroundEffectHelper::sigma_low  (1.0f);
30
 
nux::Property<bool> BackgroundEffectHelper::updates_enabled (true);
31
 
 
32
 
 
33
 
BackgroundEffectHelper::BackgroundEffectHelper()
34
 
{
35
 
  noise_texture_ = nux::CreateTextureFromFile(PKGDATADIR"/dash_noise.png");
36
 
  Register(this);
37
 
}
38
 
 
39
 
BackgroundEffectHelper::~BackgroundEffectHelper()
40
 
{
41
 
  Unregister(this);
42
 
}
43
 
 
44
 
void BackgroundEffectHelper::QueueDrawOnOwners ()
45
 
{
46
 
  for (BackgroundEffectHelper* helper : registered_list_)
47
 
  {
48
 
    nux::View *owner = helper->owner();
49
 
    if (owner)
50
 
      owner->QueueDraw();
51
 
  }
52
 
}
53
 
 
54
 
void BackgroundEffectHelper::Register   (BackgroundEffectHelper *self)
55
 
{
56
 
  registered_list_.push_back(self);
57
 
}
58
 
 
59
 
void BackgroundEffectHelper::Unregister (BackgroundEffectHelper *self)
60
 
{
61
 
  registered_list_.remove(self);
62
 
}
63
 
 
64
 
void BackgroundEffectHelper::DirtyCache ()
65
 
{
66
 
  if (blur_texture_.IsValid ())
67
 
    blur_texture_.Release ();
68
 
}
69
 
 
70
 
nux::ObjectPtr<nux::IOpenGLBaseTexture> BackgroundEffectHelper::GetBlurRegion(nux::Geometry geo, bool force_update)
71
 
{
72
 
  nux::GraphicsEngine* graphics_engine = nux::GetGraphicsDisplay()->GetGraphicsEngine();
73
 
 
74
 
  bool should_update = updates_enabled() || force_update;
75
 
 
76
 
  if ((blur_type != BLUR_ACTIVE || !should_update) 
77
 
      && blur_texture_.IsValid() 
78
 
      && (geo == blur_geometry_))
79
 
  {
80
 
    return blur_texture_;
81
 
  }
82
 
 
83
 
  blur_geometry_ =  nux::Geometry(0, 0, graphics_engine->GetWindowWidth(), graphics_engine->GetWindowHeight()).Intersect(geo);
84
 
 
85
 
  if (blur_geometry_.IsNull() || blur_type == BLUR_NONE || !nux::GetGraphicsDisplay()->GetGpuDevice()->backup_texture0_.IsValid())
86
 
  {
87
 
    return nux::ObjectPtr<nux::IOpenGLBaseTexture>(NULL);
88
 
  }
89
 
 
90
 
  // save the current fbo
91
 
  nux::ObjectPtr<nux::IOpenGLFrameBufferObject> current_fbo = nux::GetGraphicsDisplay()->GetGpuDevice()->GetCurrentFrameBufferObject();
92
 
  nux::GetGraphicsDisplay ()->GetGpuDevice ()->DeactivateFrameBuffer ();
93
 
  
94
 
  // Set a viewport to the requested size
95
 
  graphics_engine->SetViewport (0, 0, blur_geometry_.width, blur_geometry_.height);
96
 
  graphics_engine->SetScissor (0, 0, blur_geometry_.width, blur_geometry_.height);
97
 
  // Disable nux scissoring
98
 
  graphics_engine->GetRenderStates ().EnableScissor (false);
99
 
 
100
 
  // The background texture is the same size as the monitor where we are rendering.
101
 
  nux::TexCoordXForm texxform__bg;
102
 
  texxform__bg.flip_v_coord = false;
103
 
  texxform__bg.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
104
 
  texxform__bg.uoffset = ((float) blur_geometry_.x) / graphics_engine->GetWindowWidth ();
105
 
  texxform__bg.voffset = ((float) graphics_engine->GetWindowHeight () - blur_geometry_.y - blur_geometry_.height) / graphics_engine->GetWindowHeight ();
106
 
 
107
 
  int opengl_version = nux::GetGraphicsDisplay()->GetGpuDevice()->GetOpenGLMajorVersion();
108
 
  bool support_frag = nux::GetGraphicsDisplay()->GetGpuDevice()->GetGpuInfo().Support_ARB_Fragment_Shader();
109
 
  bool support_vert = nux::GetGraphicsDisplay()->GetGpuDevice()->GetGpuInfo().Support_ARB_Vertex_Shader();
110
 
 
111
 
  if (support_vert && support_frag && opengl_version >= 2)
112
 
  {
113
 
    float noise_factor = 1.2f;
114
 
    float gaussian_sigma = opengl_version >= 3 ? sigma_high : sigma_med;
115
 
    int blur_passes = 1;
116
 
 
117
 
    nux::ObjectPtr<nux::IOpenGLBaseTexture> device_texture = nux::GetGraphicsDisplay()->GetGpuDevice()->backup_texture0_;
118
 
    nux::ObjectPtr<nux::CachedBaseTexture> noise_device_texture = graphics_engine->CacheResource(noise_texture_);
119
 
 
120
 
    int down_size_factor = 1;
121
 
    unsigned int buffer_width = blur_geometry_.width;
122
 
    unsigned int buffer_height = blur_geometry_.height;
123
 
 
124
 
    int x =  0;
125
 
    int y =  0;
126
 
 
127
 
    unsigned int down_size_width = buffer_width / down_size_factor;
128
 
    unsigned int down_size_height = buffer_height / down_size_factor;
129
 
 
130
 
    nux::TexCoordXForm texxform;
131
 
    nux::TexCoordXForm noise_texxform;
132
 
 
133
 
    texxform.SetFilter(nux::TEXFILTER_LINEAR, nux::TEXFILTER_LINEAR);
134
 
    
135
 
    noise_texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
136
 
    noise_texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
137
 
    noise_texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
138
 
 
139
 
    // Down size
140
 
    graphics_engine->QRP_GetCopyTexture(down_size_width, down_size_height, temp_device_texture0_,
141
 
     device_texture, texxform__bg, nux::color::White);
142
 
 
143
 
    // Blur at a lower resolution (less pixels to process)
144
 
    temp_device_texture1_ = graphics_engine->QRP_GetHQBlur(x, y, down_size_width, down_size_height,
145
 
     temp_device_texture0_, texxform, nux::color::White,
146
 
     gaussian_sigma, blur_passes);
147
 
 
148
 
    // Up size
149
 
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height, temp_device_texture0_,
150
 
     temp_device_texture1_, texxform, nux::color::White);
151
 
 
152
 
    // Add Noise
153
 
    blur_texture_ = graphics_engine->QRP_GLSL_GetDisturbedTexture(
154
 
      0, 0, buffer_width, buffer_height,
155
 
      noise_device_texture->m_Texture, noise_texxform, nux::Color (
156
 
      noise_factor * 1.0f/buffer_width,
157
 
      noise_factor * 1.0f/buffer_height, 1.0f, 1.0f),
158
 
      temp_device_texture0_, texxform, nux::color::White);
159
 
  }
160
 
  else
161
 
  {
162
 
    // GPUs with only ARB support are treated here
163
 
 
164
 
    float gaussian_sigma = sigma_low;
165
 
    int blur_passes = 1;
166
 
 
167
 
    nux::ObjectPtr<nux::IOpenGLBaseTexture> device_texture = nux::GetGraphicsDisplay()->GetGpuDevice()->backup_texture0_;
168
 
    nux::ObjectPtr<nux::CachedBaseTexture> noise_device_texture = graphics_engine->CacheResource(noise_texture_);
169
 
 
170
 
    unsigned int offset = 0;
171
 
    int quad_width = blur_geometry_.width;
172
 
    int quad_height = blur_geometry_.height;
173
 
 
174
 
    int down_size_factor = 4;
175
 
    unsigned int buffer_width = quad_width + 2 * offset;
176
 
    unsigned int buffer_height = quad_height + 2 * offset;
177
 
 
178
 
    int x = (buffer_width - quad_width) / 2;
179
 
    int y = (buffer_height - quad_height) / 2;
180
 
 
181
 
    unsigned int down_size_width = buffer_width / down_size_factor;
182
 
    unsigned int down_size_height = buffer_height / down_size_factor;
183
 
 
184
 
    nux::TexCoordXForm texxform;
185
 
    nux::TexCoordXForm noise_texxform;
186
 
 
187
 
    texxform.SetFilter(nux::TEXFILTER_LINEAR, nux::TEXFILTER_LINEAR);
188
 
 
189
 
    noise_texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
190
 
    noise_texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
191
 
    noise_texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
192
 
 
193
 
    // Copy source texture
194
 
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height, temp_device_texture0_,
195
 
     device_texture, texxform__bg, nux::color::White);
196
 
 
197
 
    // Down size
198
 
    graphics_engine->QRP_GetCopyTexture(down_size_width, down_size_height, ds_temp_device_texture1_,
199
 
     temp_device_texture0_, texxform, nux::color::White);
200
 
 
201
 
    // Blur at a lower resolution (less pixels to process)
202
 
    ds_temp_device_texture0_ = graphics_engine->QRP_GetBlurTexture(x, y, down_size_width, down_size_height,
203
 
     ds_temp_device_texture1_, texxform, nux::color::White,
204
 
     gaussian_sigma, blur_passes);
205
 
 
206
 
    // // Copy to new texture
207
 
    // graphics_engine->QRP_GetCopyTexture(down_size_width, down_size_height, temp_device_texture1_, temp_device_texture0_, texxform, nux::color::White);
208
 
 
209
 
    // Up size
210
 
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height, temp_device_texture1_,
211
 
     ds_temp_device_texture0_, texxform, nux::color::White);
212
 
 
213
 
    blur_texture_ = temp_device_texture1_;      
214
 
  }
215
 
 
216
 
  if (current_fbo.IsValid())
217
 
  {
218
 
    current_fbo->Activate(true);
219
 
    graphics_engine->Push2DWindow(current_fbo->GetWidth(), current_fbo->GetHeight());
220
 
    graphics_engine->GetRenderStates ().EnableScissor (true);
221
 
  }
222
 
  else
223
 
  {
224
 
    graphics_engine->SetViewport(0, 0, graphics_engine->GetWindowWidth(), graphics_engine->GetWindowHeight());
225
 
    graphics_engine->Push2DWindow(graphics_engine->GetWindowWidth(), graphics_engine->GetWindowHeight());
226
 
    graphics_engine->ApplyClippingRectangle();
227
 
  }
228
 
 
229
 
  return blur_texture_;
230
 
}