~tatokis/unity/gcc-72-errors

1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
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
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
20
#include <NuxCore/Logger.h>
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
21
#include "BackgroundEffectHelper.h"
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
22
23
#include "TextureCache.h"
1455.3.11 by Tim Penhey
Fix the leaking Regions on early exit.
24
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
25
namespace
26
{
27
DECLARE_LOGGER(logger, "unity.background_effect_helper");
28
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
29
const int BLUR_RADIUS = 3;
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
30
const float sigma_high = 5.0f;
31
const float sigma_med = 3.0f;
32
const float sigma_low = 1.0f;
33
}
1402.1.1 by Jason Smith
dramatically improve blur performance
34
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
35
using namespace unity;
36
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
37
nux::Property<BlurType> BackgroundEffectHelper::blur_type(BLUR_ACTIVE);
38
nux::Geometry BackgroundEffectHelper::monitor_rect_;
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
39
std::list<BackgroundEffectHelper*> BackgroundEffectHelper::registered_list_;
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
40
std::vector<nux::Geometry> BackgroundEffectHelper::blur_geometries_;
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
41
sigc::signal<void, nux::Geometry const&> BackgroundEffectHelper::blur_region_needs_update_;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
42
3347.7.5 by Marco Trevisan (Treviño)
BackgroundEffectHelper: add new constructor that allows to define owner immediately
43
BackgroundEffectHelper::BackgroundEffectHelper(nux::View* view)
44
  : owner(view)
45
  , enabled(false)
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
46
  , cache_dirty(true)
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
47
{
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
48
  enabled.changed.connect(sigc::mem_fun(this, &BackgroundEffectHelper::OnEnabledChanged));
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
49
  owner.changed.connect(sigc::mem_fun(this, &BackgroundEffectHelper::OnOwnerChanged));
4093.2.26 by Marco Trevisan (Treviño)
BackgroundEffectHelper: reload textures on chache themed icons invalidated
50
  TextureCache::GetDefault().themed_invalidated.connect(sigc::mem_fun(this, &BackgroundEffectHelper::LoadTextures));
4093.2.29 by Marco Trevisan (Treviño)
BackgroundEffectHelper: esure we load the textures in first place and inherit from sigc::trackable to disconnect
51
  LoadTextures();
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
52
}
53
3347.7.5 by Marco Trevisan (Treviño)
BackgroundEffectHelper: add new constructor that allows to define owner immediately
54
BackgroundEffectHelper::BackgroundEffectHelper()
55
  : BackgroundEffectHelper(nullptr)
56
{}
57
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
58
BackgroundEffectHelper::~BackgroundEffectHelper()
59
{
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
60
  Unregister(this);
61
}
62
4093.2.26 by Marco Trevisan (Treviño)
BackgroundEffectHelper: reload textures on chache themed icons invalidated
63
void BackgroundEffectHelper::LoadTextures()
64
{
65
  noise_texture_ = TextureCache::GetDefault().FindTexture("dash_noise");
66
}
67
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
68
void BackgroundEffectHelper::OnEnabledChanged(bool enabled)
1402.1.1 by Jason Smith
dramatically improve blur performance
69
{
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
70
  if (enabled)
71
  {
72
    Register(this);
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
73
    SetupOwner(owner());
74
  }
75
  else
76
  {
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
77
    connections_.Clear();
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
78
    Unregister(this);
79
  }
80
}
81
82
void BackgroundEffectHelper::OnOwnerChanged(nux::View* view)
83
{
84
  geo_getter_func_ = nullptr;
85
86
  if (!view)
87
  {
88
    connections_.Clear();
89
    Unregister(this);
90
  }
91
}
92
93
void BackgroundEffectHelper::SetupOwner(nux::View* view)
94
{
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
95
  if (!view)
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
96
    return;
97
98
  auto cb = [this] (nux::Area*, nux::Geometry&) { UpdateOwnerGeometry(); };
99
  connections_.Add(view->geometry_changed.connect(cb));
100
101
  auto* parent = view->GetTopLevelViewWindow();
102
  if (!parent)
103
  {
104
    LOG_ERROR(logger) << "The parent window for the owner must be set!";
105
  }
106
  else
107
  {
108
    connections_.Add(parent->geometry_changed.connect(cb));
109
  }
110
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
111
  if (!UpdateOwnerGeometry())
112
  {
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
113
    DirtyCache();
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
114
    UpdateBlurGeometries();
115
  }
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
116
}
117
118
void BackgroundEffectHelper::SetGeometryGetter(GeometryGetterFunc const& func)
119
{
120
  geo_getter_func_ = func;
121
}
122
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
123
bool BackgroundEffectHelper::UpdateOwnerGeometry()
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
124
{
125
  auto const& geo = geo_getter_func_ ? geo_getter_func_() : owner()->GetAbsoluteGeometry();
126
127
  if (requested_blur_geometry_ != geo)
128
  {
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
129
    // For some reason a view might have this size at show time, let's just ignore them.
130
    if (geo.width != 1 && geo.height != 1)
131
    {
132
      requested_blur_geometry_ = geo;
133
134
      DirtyCache();
135
      UpdateBlurGeometries();
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
136
137
      return true;
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
138
    }
139
  }
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
140
141
  return false;
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
142
}
143
144
void BackgroundEffectHelper::UpdateBlurGeometries()
145
{
4151.1.4 by Eleni Maria Stea
check if we actually have blur before updating blur geometries
146
  if (blur_type == BLUR_NONE)
147
      return;
148
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
149
  int radius = GetBlurRadius();
150
  blur_geometries_.clear();
151
  blur_geometries_.reserve(registered_list_.size());
152
153
  for (BackgroundEffectHelper* bg_effect_helper : registered_list_)
154
  {
155
    /* Use the last requested region. The real region is clipped to the
156
     * monitor geometry, but that is done at paint time */
157
    auto const& blur_geo = bg_effect_helper->requested_blur_geometry_;
158
4151.1.4 by Eleni Maria Stea
check if we actually have blur before updating blur geometries
159
    if (!blur_geo.IsNull())
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
160
      blur_geometries_.push_back(blur_geo.GetExpand(radius, radius));
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
161
  }
1584.1.1 by Jason Smith
merge jays optimized effects branch
162
}
163
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
164
void BackgroundEffectHelper::ProcessDamage(nux::Geometry const& geo)
1826.4.4 by Andrea Azzarone
Ops.
165
{
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
166
  for (BackgroundEffectHelper* bg_effect_helper : registered_list_)
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
167
  {
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
168
    if (bg_effect_helper->cache_dirty)
1402.1.1 by Jason Smith
dramatically improve blur performance
169
      continue;
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
170
3347.7.5 by Marco Trevisan (Treviño)
BackgroundEffectHelper: add new constructor that allows to define owner immediately
171
    if (geo.IsIntersecting(bg_effect_helper->requested_blur_geometry_))
1402.1.1 by Jason Smith
dramatically improve blur performance
172
    {
1584.1.1 by Jason Smith
merge jays optimized effects branch
173
      bg_effect_helper->DirtyCache();
1402.1.1 by Jason Smith
dramatically improve blur performance
174
    }
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
175
  }
176
}
177
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
178
bool BackgroundEffectHelper::HasEnabledHelpers()
179
{
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
180
  return !registered_list_.empty();
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
181
}
182
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
183
float BackgroundEffectHelper::GetBlurSigma()
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
184
{
185
  nux::GpuDevice* gpu_device = nux::GetGraphicsDisplay()->GetGpuDevice();
186
  int opengl_version = gpu_device->GetOpenGLMajorVersion();
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
187
  return (opengl_version >= 3) ? sigma_high : sigma_med;
188
}
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
189
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
190
int BackgroundEffectHelper::GetBlurRadius()
191
{
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
192
  return (blur_type != BLUR_NONE) ? GetBlurSigma() * BLUR_RADIUS : 0;
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
193
}
194
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
195
std::vector<nux::Geometry> const& BackgroundEffectHelper::GetBlurGeometries()
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
196
{
3347.5.22 by Marco Trevisan (Treviño)
BackgroundEffectHelper: Set radius to 0 when using no blur, some cleanup
197
  /* TODO: to reduce useless glCopyTexSubImage2D during paint we should return
198
   * here only damaged geometries */
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
199
  return blur_geometries_;
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
200
}
201
1738.12.1 by Daniel van Vugt
Fix major performance regressions due to unnecessary UnityFBO binding
202
bool BackgroundEffectHelper::HasDirtyHelpers()
203
{
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
204
  for (BackgroundEffectHelper* bg_effect_helper : registered_list_)
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
205
    if (bg_effect_helper->cache_dirty)
1738.12.1 by Daniel van Vugt
Fix major performance regressions due to unnecessary UnityFBO binding
206
      return true;
207
208
  return false;
209
}
210
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
211
void BackgroundEffectHelper::Register(BackgroundEffectHelper* self)
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
212
{
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
213
  auto* view = self->owner();
214
215
  if (!view)
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
216
  {
217
    LOG_ERROR(logger) << "Registering an invalid helper, must set an owner!";
218
    return;
219
  }
220
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
221
  registered_list_.push_back(self);
222
}
223
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
224
void BackgroundEffectHelper::Unregister(BackgroundEffectHelper* self)
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
225
{
226
  registered_list_.remove(self);
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
227
  UpdateBlurGeometries();
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
228
}
229
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
230
void BackgroundEffectHelper::DirtyCache()
1371.1.6 by Jason Smith
more performance improvements
231
{
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
232
  if ((cache_dirty && blur_geometry_ == requested_blur_geometry_) || !owner())
1584.1.1 by Jason Smith
merge jays optimized effects branch
233
    return;
234
1430 by Jason Smith
fix big memory leak on some systems
235
  cache_dirty = true;
3347.5.13 by Marco Trevisan (Treviño)
BackgroundEffectHelper: only register helpers if an owner is set
236
  owner()->QueueDraw();
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
237
3347.5.20 by Marco Trevisan (Treviño)
BackgroundEffectHelper: keep a cached version of blur geometries
238
  int radius = GetBlurRadius();
239
  blur_region_needs_update_.emit(requested_blur_geometry_.GetExpand(radius, radius));
1371.1.6 by Jason Smith
more performance improvements
240
}
1371.1.1 by Jason Smith
centralize control over blur behavior into BackgroundEffectHelper class
241
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
242
nux::ObjectPtr<nux::IOpenGLBaseTexture> BackgroundEffectHelper::GetBlurRegion(bool force_update)
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
243
{
1584.1.1 by Jason Smith
merge jays optimized effects branch
244
  bool should_update = force_update || cache_dirty;
1371.1.5 by Jason Smith
simplify effect helper further
245
1402.2.1 by Sam Spilsbury
Only update the blur texture when the damage bounds actually intersect
246
  /* Static blur: only update when the size changed */
2206.4.1 by Jay Taoko
* Merged with Unity Trunk.
247
  if ((blur_type != BLUR_ACTIVE || !should_update)
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
248
      && blur_texture_.IsValid()
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
249
      && (requested_blur_geometry_ == blur_geometry_))
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
250
  {
251
    return blur_texture_;
252
  }
253
1455.3.11 by Tim Penhey
Fix the leaking Regions on early exit.
254
  nux::GraphicsEngine* graphics_engine = nux::GetGraphicsDisplay()->GetGraphicsEngine();
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
255
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
256
  int monitor_width = BackgroundEffectHelper::monitor_rect_.width;
257
  int monitor_height = BackgroundEffectHelper::monitor_rect_.height;
258
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
259
  nux::Geometry temp = requested_blur_geometry_;
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
260
  temp.OffsetPosition(-monitor_rect_.x, -monitor_rect_.y);
3347.5.14 by Marco Trevisan (Treviño)
BackgroundEffectHelper: connect to owner geometry changes to update the requested blur geo
261
  blur_geometry_ = nux::Geometry(0, 0, monitor_width, monitor_height).Intersect(temp);
1419 by Jason Smith
fix blur not working when damage is null
262
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
263
  nux::GpuDevice* gpu_device = nux::GetGraphicsDisplay()->GetGpuDevice();
264
  if (blur_geometry_.IsNull() || blur_type == BLUR_NONE || !gpu_device->backup_texture0_.IsValid())
1419 by Jason Smith
fix blur not working when damage is null
265
  {
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
266
    return nux::ObjectPtr<nux::IOpenGLBaseTexture>();
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
267
  }
268
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
269
  const int radius = GetBlurRadius();
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
270
271
  // Define a larger region of that account for the blur radius
272
  nux::Geometry larger_blur_geometry;
273
  larger_blur_geometry.x = std::max(blur_geometry_.x - radius, 0);
274
  larger_blur_geometry.y = std::max(blur_geometry_.y - radius, 0);
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
275
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
276
  int xx = std::min(blur_geometry_.x + blur_geometry_.width + radius, monitor_width);
1510.2.1 by Jay Taoko
Merged trunk
277
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
278
  larger_blur_geometry.width = xx - larger_blur_geometry.x;
279
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
280
  int yy = std::min(blur_geometry_.y + blur_geometry_.height + radius, monitor_height);
1510.2.1 by Jay Taoko
Merged trunk
281
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
282
  larger_blur_geometry.height = yy - larger_blur_geometry.y;
283
284
  int dleft     = blur_geometry_.x - larger_blur_geometry.x;
285
  int dbottom   = (larger_blur_geometry.y + larger_blur_geometry.height) - (blur_geometry_.y + blur_geometry_.height);
286
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
287
  // save the current fbo
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
288
  nux::ObjectPtr<nux::IOpenGLFrameBufferObject> current_fbo = gpu_device->GetCurrentFrameBufferObject();
289
  gpu_device->DeactivateFrameBuffer();
1402.2.3 by Sam Spilsbury
Detect occlusions and apply this to the region update logic
290
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
291
  // Set a viewport to the requested size
1402.2.1 by Sam Spilsbury
Only update the blur texture when the damage bounds actually intersect
292
  // FIXME: We need to do multiple passes for the dirty region
293
  // on the underlying backup texture so that we're only updating
294
  // the bits that we need
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
295
  graphics_engine->SetViewport(0, 0, larger_blur_geometry.width, larger_blur_geometry.height);
296
  graphics_engine->SetScissor(0, 0, larger_blur_geometry.width, larger_blur_geometry.height);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
297
  // Disable nux scissoring
2791.7.1 by Nicolas d'Offay
Fixed dodgy maths working out the tex coord for non blurred regions.
298
  graphics_engine->GetRenderStates().EnableScissor(false);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
299
300
  // The background texture is the same size as the monitor where we are rendering.
301
  nux::TexCoordXForm texxform__bg;
302
  texxform__bg.flip_v_coord = false;
303
  texxform__bg.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
304
  texxform__bg.uoffset = ((float) larger_blur_geometry.x) / monitor_width;
305
  texxform__bg.voffset = ((float) monitor_height - larger_blur_geometry.y - larger_blur_geometry.height) / monitor_height;
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
306
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
307
  bool support_frag = gpu_device->GetGpuInfo().Support_ARB_Fragment_Shader();
308
  bool support_vert = gpu_device->GetGpuInfo().Support_ARB_Vertex_Shader();
1371.1.4 by Jason Smith
enable configurable sigma levels for different power GPU's
309
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
310
  if (support_vert && support_frag && gpu_device->GetOpenGLMajorVersion() >= 2)
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
311
  {
1583 by Robert Carr
BackgroundEffectHelper: Change the default noise factor for blur from 1.2 to 1.1
312
    float noise_factor = 1.1f;
3347.5.9 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use GetExpand for computing the blurred area
313
    float gaussian_sigma = GetBlurSigma();
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
314
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
315
    nux::ObjectPtr<nux::IOpenGLBaseTexture> device_texture = gpu_device->backup_texture0_;
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
316
    nux::ObjectPtr<nux::CachedBaseTexture> noise_device_texture = graphics_engine->CacheResource(noise_texture_.GetPointer());
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
317
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
318
    unsigned int buffer_width = larger_blur_geometry.width;
319
    unsigned int buffer_height = larger_blur_geometry.height;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
320
1634.1.1 by Jason Smith
improve blur rendering performance
321
    blur_fx_struct_.src_texture = device_texture;
3212.3.10 by Marco Trevisan (Treviño)
BackgroundEffectHelper: reset changes committed by mistake
322
    graphics_engine->QRP_GLSL_GetLSBlurFx(0, 0, buffer_width, buffer_height,
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
323
                                          &blur_fx_struct_, texxform__bg, nux::color::White,
324
                                          gaussian_sigma);
1576.6.1 by Jay Taoko
Optimized effects creation
325
1634.1.1 by Jason Smith
improve blur rendering performance
326
    nux::TexCoordXForm texxform;
327
    nux::TexCoordXForm noise_texxform;
328
329
    texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
330
331
    noise_texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
332
    noise_texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
333
    noise_texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
334
335
    noise_fx_struct_.src_texture = blur_fx_struct_.dst_texture;
1576.6.1 by Jay Taoko
Optimized effects creation
336
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
337
    // Add Noise
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
338
    nux::Color noise_color(noise_factor * 1.0f/buffer_width,
339
                           noise_factor * 1.0f/buffer_height,
340
                           1.0f, 1.0f);
1576.6.1 by Jay Taoko
Optimized effects creation
341
1634.1.1 by Jason Smith
improve blur rendering performance
342
    texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
343
    texxform.uoffset = (blur_geometry_.x - larger_blur_geometry.x) / (float) buffer_width;
344
    texxform.voffset = (blur_geometry_.y - larger_blur_geometry.y) / (float) buffer_height;
1576.6.1 by Jay Taoko
Optimized effects creation
345
    graphics_engine->QRP_GLSL_GetDisturbedTextureFx(
1634.1.1 by Jason Smith
improve blur rendering performance
346
      0, 0, blur_geometry_.width, blur_geometry_.height,
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
347
      noise_device_texture->m_Texture, noise_texxform, noise_color,
1576.6.1 by Jay Taoko
Optimized effects creation
348
      &noise_fx_struct_, texxform, nux::color::White);
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
349
1634.1.1 by Jason Smith
improve blur rendering performance
350
    blur_texture_ = noise_fx_struct_.dst_texture;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
351
  }
352
  else
353
  {
354
    // GPUs with only ARB support are treated here
355
1371.1.4 by Jason Smith
enable configurable sigma levels for different power GPU's
356
    float gaussian_sigma = sigma_low;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
357
    int blur_passes = 1;
358
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
359
    nux::ObjectPtr<nux::IOpenGLBaseTexture> device_texture = gpu_device->backup_texture0_;
3290.3.18 by Marco Trevisan (Treviño)
BackgroundEffectHelper: use texture cache, unregister clients when unneeded
360
    nux::ObjectPtr<nux::CachedBaseTexture> noise_device_texture = graphics_engine->CacheResource(noise_texture_.GetPointer());
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
361
362
    unsigned int offset = 0;
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
363
    int quad_width = larger_blur_geometry.width;
364
    int quad_height = larger_blur_geometry.height;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
365
366
    int down_size_factor = 4;
367
    unsigned int buffer_width = quad_width + 2 * offset;
368
    unsigned int buffer_height = quad_height + 2 * offset;
369
370
    int x = (buffer_width - quad_width) / 2;
371
    int y = (buffer_height - quad_height) / 2;
372
373
    unsigned int down_size_width = buffer_width / down_size_factor;
374
    unsigned int down_size_height = buffer_height / down_size_factor;
375
376
    nux::TexCoordXForm texxform;
377
    nux::TexCoordXForm noise_texxform;
378
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
379
    texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
380
381
    noise_texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
382
    noise_texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
383
    noise_texxform.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
384
385
    // Copy source texture
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
386
    nux::ObjectPtr<nux::IOpenGLBaseTexture> texture_copy;
387
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height,
388
                                        texture_copy, device_texture,
389
                                        texxform__bg, nux::color::White);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
390
391
    // Down size
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
392
    nux::ObjectPtr<nux::IOpenGLBaseTexture> resized_texture;
393
    graphics_engine->QRP_GetCopyTexture(down_size_width, down_size_height,
394
                                        resized_texture, texture_copy,
395
                                        texxform, nux::color::White);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
396
397
    // Blur at a lower resolution (less pixels to process)
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
398
    nux::ObjectPtr<nux::IOpenGLBaseTexture> low_res_blur;
399
    low_res_blur = graphics_engine->QRP_GetBlurTexture(x, y, down_size_width, down_size_height,
400
                                                       resized_texture, texxform,
401
                                                       nux::color::White,
402
                                                       gaussian_sigma, blur_passes);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
403
404
    // Up size
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
405
    texxform.SetFilter(nux::TEXFILTER_LINEAR, nux::TEXFILTER_LINEAR);
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
406
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height, resized_texture,
407
                                        low_res_blur, texxform, nux::color::White);
408
1424.1.1 by Jay Taoko
Seamless blur transition between the dash, panel and launcher
409
    // Returns a smaller blur region (minus blur radius).
410
    texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
411
    texxform.flip_v_coord = true;
412
    texxform.uoffset = dleft / (float) buffer_width;
413
    texxform.voffset = dbottom / (float) buffer_height;
1455.3.12 by Tim Penhey
Removing member variables where temporaries are fine.
414
    graphics_engine->QRP_GetCopyTexture(blur_geometry_.width, blur_geometry_.height,
415
                                        blur_texture_, resized_texture,
416
                                        texxform, nux::color::White);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
417
  }
418
419
  if (current_fbo.IsValid())
420
  {
421
    current_fbo->Activate(true);
422
    graphics_engine->Push2DWindow(current_fbo->GetWidth(), current_fbo->GetHeight());
1402.2.5 by Sam Spilsbury
Deformattize
423
    graphics_engine->GetRenderStates ().EnableScissor (true);
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
424
  }
425
  else
426
  {
1499.2.1 by Jay Taoko
Fix blur in multimonitor config
427
    graphics_engine->SetViewport(0, 0, monitor_width, monitor_height);
428
    graphics_engine->Push2DWindow(monitor_width, monitor_height);
1510.2.1 by Jay Taoko
Merged trunk
429
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
430
    graphics_engine->ApplyClippingRectangle();
431
  }
432
1430 by Jason Smith
fix big memory leak on some systems
433
  cache_dirty = false;
1323.6.3 by Jay Taoko
Added class BackgroundEffectHelper
434
  return blur_texture_;
435
}
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
436
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
437
nux::ObjectPtr<nux::IOpenGLBaseTexture> BackgroundEffectHelper::GetRegion(bool force_update)
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
438
{
439
  bool should_update = force_update || cache_dirty;
440
441
  /* Static blur: only update when the size changed */
442
  if ((!should_update)
443
      && blur_texture_.IsValid()
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
444
      && (requested_blur_geometry_ == blur_geometry_))
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
445
  {
446
    return blur_texture_;
447
  }
448
449
  nux::GraphicsEngine* graphics_engine = nux::GetGraphicsDisplay()->GetGraphicsEngine();
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
450
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
451
  int monitor_width = BackgroundEffectHelper::monitor_rect_.width;
452
  int monitor_height = BackgroundEffectHelper::monitor_rect_.height;
453
3248.4.5 by Sam Spilsbury
Specify the required blur area before drawing so that we can selectively use glCopyTexSubImage2D.
454
  nux::Geometry temp = requested_blur_geometry_;
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
455
  temp.OffsetPosition(-monitor_rect_.x, -monitor_rect_.y);
2791.7.1 by Nicolas d'Offay
Fixed dodgy maths working out the tex coord for non blurred regions.
456
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
457
  blur_geometry_ =  nux::Geometry(0, 0, monitor_width, monitor_height).Intersect(temp);
458
459
  nux::GpuDevice* gpu_device = nux::GetGraphicsDisplay()->GetGpuDevice();
3347.5.11 by Marco Trevisan (Treviño)
UnityShell: some code cleanup
460
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
461
  if (blur_geometry_.IsNull() || !gpu_device->backup_texture0_.IsValid())
462
  {
463
    return nux::ObjectPtr<nux::IOpenGLBaseTexture>();
464
  }
465
466
  // save the current fbo
467
  nux::ObjectPtr<nux::IOpenGLFrameBufferObject> current_fbo = gpu_device->GetCurrentFrameBufferObject();
468
  gpu_device->DeactivateFrameBuffer();
469
470
  // Set a viewport to the requested size
471
  // FIXME: We need to do multiple passes for the dirty region
472
  // on the underlying backup texture so that we're only updating
473
  // the bits that we need
474
  graphics_engine->SetViewport(0, 0, blur_geometry_.width, blur_geometry_.height);
475
  graphics_engine->SetScissor(0, 0, blur_geometry_.width, blur_geometry_.height);
476
  // Disable nux scissoring
2791.7.1 by Nicolas d'Offay
Fixed dodgy maths working out the tex coord for non blurred regions.
477
  graphics_engine->GetRenderStates().EnableScissor(false);
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
478
479
  // The background texture is the same size as the monitor where we are rendering.
480
  nux::TexCoordXForm texxform__bg;
481
  texxform__bg.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD);
482
  texxform__bg.uoffset = ((float) blur_geometry_.x) / monitor_width;
2791.7.1 by Nicolas d'Offay
Fixed dodgy maths working out the tex coord for non blurred regions.
483
  texxform__bg.voffset = ((float) blur_geometry_.y) / monitor_height;
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
484
485
  {
486
    nux::ObjectPtr<nux::IOpenGLBaseTexture> device_texture = gpu_device->backup_texture0_;
487
488
    unsigned int offset = 0;
489
    int quad_width = blur_geometry_.width;
490
    int quad_height = blur_geometry_.height;
491
492
    unsigned int buffer_width = quad_width + 2 * offset;
493
    unsigned int buffer_height = quad_height + 2 * offset;
494
495
    texxform__bg.SetFilter(nux::TEXFILTER_NEAREST, nux::TEXFILTER_NEAREST);
496
    texxform__bg.flip_v_coord = true;
497
498
    // Copy source texture
499
    graphics_engine->QRP_GetCopyTexture(buffer_width, buffer_height,
500
                                        blur_texture_, device_texture,
501
                                        texxform__bg, nux::color::White);
502
  }
503
504
  if (current_fbo.IsValid())
505
  {
506
    current_fbo->Activate(true);
507
    graphics_engine->Push2DWindow(current_fbo->GetWidth(), current_fbo->GetHeight());
2791.7.1 by Nicolas d'Offay
Fixed dodgy maths working out the tex coord for non blurred regions.
508
    graphics_engine->GetRenderStates().EnableScissor(true);
2160.4.1 by Jay Taoko
* Fixes the rendering of the dash when the "No Blur" option is selected in CCSM.
509
  }
510
  else
511
  {
512
    graphics_engine->SetViewport(0, 0, monitor_width, monitor_height);
513
    graphics_engine->Push2DWindow(monitor_width, monitor_height);
514
515
    graphics_engine->ApplyClippingRectangle();
516
  }
517
518
  cache_dirty = false;
519
  return blur_texture_;
520
}
521