~sil2100/nux/precise_sru-1

« back to all changes in this revision

Viewing changes to Nux/CairoWrapper.cpp

  • Committer: Didier Roche
  • Date: 2012-02-17 10:28:35 UTC
  • mfrom: (159.3.34)
  • Revision ID: didier.roche@canonical.com-20120217102835-7fyqcabz0vad239t
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
namespace nux
25
25
{
26
 
  CairoWrapper::CairoWrapper (Geometry const& geom, DrawCanvasCallback callback)
27
 
  {
28
 
    draw_canvas_callback_ = callback;
29
 
        geometry_.x           = geom.x;
30
 
        geometry_.y           = geom.y;
31
 
        geometry_.width       = geom.width;
32
 
        geometry_.height      = geom.height;
33
 
    cr_                   = NULL;
34
 
    surface_              = NULL;
35
 
    bitmap_               = NULL;
36
 
    texture_              = NULL;
37
 
        Recreate ();
38
 
  }
39
 
 
40
 
  CairoWrapper::~CairoWrapper ()
 
26
  struct CairoWrapper::Impl
 
27
  {
 
28
    Impl(CairoWrapper* parent, Geometry const& geo, DrawCanvasCallback callback);
 
29
    ~Impl();
 
30
 
 
31
    bool CreateBitmap ();
 
32
    bool Invalidate (Geometry const& geom);
 
33
    void SetDrawCanvasCallback (DrawCanvasCallback callback);
 
34
    bool DumpToFile (std::string const& filename);
 
35
    bool Recreate ();
 
36
    void DeleteResources();
 
37
 
 
38
    CairoWrapper*      parent_;
 
39
    Geometry           geometry_;
 
40
    DrawCanvasCallback draw_canvas_callback_;
 
41
    cairo_t*           cr_;
 
42
    cairo_surface_t*   surface_;
 
43
    NBitmapData*       bitmap_;
 
44
    BaseTexture*       texture_;
 
45
  };
 
46
 
 
47
  CairoWrapper::Impl::Impl(CairoWrapper* parent, Geometry const& geo, DrawCanvasCallback callback)
 
48
    : parent_(parent)
 
49
    , geometry_(geo)
 
50
    , draw_canvas_callback_(callback)
 
51
    , cr_(0)
 
52
    , surface_(0)
 
53
    , bitmap_(0)
 
54
    , texture_(0)
 
55
  {
 
56
  }
 
57
 
 
58
  CairoWrapper::Impl::~Impl()
 
59
  {
 
60
    DeleteResources();
 
61
  }
 
62
 
 
63
  void CairoWrapper::Impl::DeleteResources()
41
64
  {
42
65
    if (surface_)
 
66
    {
43
67
      cairo_surface_destroy (surface_);
 
68
      surface_ = 0;
 
69
    }
44
70
 
45
71
    if (cr_)
 
72
    {
46
73
      cairo_destroy (cr_);
 
74
      cr_ = 0;
 
75
    }
47
76
 
48
 
        if (bitmap_)
 
77
    if (bitmap_)
 
78
    {
49
79
      delete bitmap_;
 
80
      bitmap_ = 0;
 
81
    }
50
82
 
51
83
    if (texture_)
 
84
    {
52
85
      texture_->UnReference ();
 
86
      texture_ = 0;
 
87
    }
53
88
  }
54
89
 
55
 
  bool CairoWrapper::Recreate ()
 
90
  bool CairoWrapper::Impl::Recreate ()
56
91
  {
57
 
    if (surface_)
58
 
      cairo_surface_destroy (surface_);
59
 
 
60
 
    if (cr_)
61
 
      cairo_destroy (cr_);
 
92
    DeleteResources();
62
93
 
63
94
    surface_ = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
64
95
                                           geometry_.width,
65
96
                                           geometry_.height);
66
97
 
67
98
    if (cairo_surface_status (surface_) != CAIRO_STATUS_SUCCESS)
68
 
        {
 
99
    {
69
100
      g_debug ("Could not create image-surface!");
70
101
      return false;
71
102
    }
84
115
    draw_canvas_callback_ (geometry_, cr_);
85
116
 
86
117
    CreateBitmap ();
87
 
    NBitmapData* bitmap = GetBitmap ();
 
118
    NBitmapData* bitmap = parent_->GetBitmap ();
88
119
    if (texture_)
89
120
        texture_->UnReference ();
90
121
 
91
 
        if (GetGraphicsDisplay()->GetGraphicsEngine() == NULL)
92
 
          return false;
 
122
    if (GetGraphicsDisplay()->GetGraphicsEngine() == 0)
 
123
      return false;
93
124
 
94
125
    texture_ = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
95
126
    texture_->Update (bitmap);
96
127
 
97
 
        return true;
 
128
    return true;
98
129
  }
99
130
 
100
 
  bool CairoWrapper::CreateBitmap ()
 
131
  bool CairoWrapper::Impl::CreateBitmap ()
101
132
  {
102
133
    if (geometry_.width < 1 || geometry_.height < 1)
103
134
    {
108
139
    if (bitmap_)
109
140
    {
110
141
      delete bitmap_;
111
 
          bitmap_ = NULL;
 
142
      bitmap_ = 0;
112
143
    }
113
144
 
114
145
    BitmapFormat bitmap_format = BITFMT_B8G8R8A8;
119
150
    t_u8* ptr = cairo_image_surface_get_data (surface_);
120
151
    int stride = cairo_image_surface_get_stride (surface_);
121
152
 
122
 
    if (ptr == NULL || stride == 0)
 
153
    if (ptr == 0 || stride == 0)
123
154
    {
124
155
      g_debug ("Invalid surface!");
125
156
      return false;
135
166
    return true;
136
167
  }
137
168
 
138
 
  NBitmapData* CairoWrapper::GetBitmap () const
139
 
  {
140
 
    return bitmap_;
141
 
  }
142
 
 
143
 
  void CairoWrapper::SetDrawCanvasCallback (DrawCanvasCallback callback)
 
169
  void CairoWrapper::Impl::SetDrawCanvasCallback (DrawCanvasCallback callback)
144
170
  {
145
171
    if (!callback)
146
172
      return;
147
173
 
148
174
    draw_canvas_callback_ = callback;
149
 
 
150
175
    Recreate (); 
151
176
  }
152
177
 
 
178
  bool CairoWrapper::Impl::DumpToFile (std::string const& filename)
 
179
  {
 
180
    if (!surface_)
 
181
      Recreate();
 
182
 
 
183
    cairo_surface_write_to_png (surface_, filename.c_str ());
 
184
 
 
185
    return true;
 
186
  }
 
187
 
 
188
  bool CairoWrapper::Impl::Invalidate (Geometry const& geom)
 
189
  {
 
190
    if (geometry_.width  == geom.width && geometry_.height == geom.height)
 
191
      return false;
 
192
 
 
193
    geometry_.x      = geom.x;
 
194
    geometry_.y      = geom.y;
 
195
    geometry_.width  = geom.width;
 
196
    geometry_.height = geom.height;
 
197
 
 
198
    DeleteResources(); 
 
199
 
 
200
    return true;
 
201
  }
 
202
 
 
203
  CairoWrapper::CairoWrapper (Geometry const& geom, DrawCanvasCallback callback)
 
204
    : pimpl(new Impl(this, geom, callback))
 
205
  {
 
206
    Recreate ();
 
207
  }
 
208
 
 
209
  CairoWrapper::~CairoWrapper ()
 
210
  {
 
211
    delete pimpl;
 
212
  }
 
213
 
 
214
  bool CairoWrapper::Invalidate (Geometry const& geom)
 
215
  {
 
216
    return pimpl->Invalidate(geom);
 
217
  }
 
218
 
 
219
  void CairoWrapper::SetDrawCanvasCallback (DrawCanvasCallback callback)
 
220
  {
 
221
    pimpl->SetDrawCanvasCallback(callback);
 
222
  }
 
223
 
153
224
  bool CairoWrapper::DumpToFile (std::string const& filename)
154
225
  {
155
 
    cairo_surface_write_to_png (surface_, filename.c_str ());
156
 
 
157
 
    return true;
 
226
    return pimpl->DumpToFile(filename);
 
227
  }
 
228
 
 
229
  bool CairoWrapper::Recreate ()
 
230
  {
 
231
    return pimpl->Recreate();
 
232
  }
 
233
 
 
234
  NBitmapData* CairoWrapper::GetBitmap () const
 
235
  {
 
236
    if (!pimpl->bitmap_)
 
237
      pimpl->Recreate();
 
238
 
 
239
    return pimpl->bitmap_;
158
240
  }
159
241
 
160
242
  BaseTexture* CairoWrapper::GetTexture () const
161
243
  {
162
 
    return texture_;
 
244
    if (!pimpl->texture_)
 
245
      pimpl->Recreate();
 
246
 
 
247
    return pimpl->texture_;
163
248
  }
164
249
 
165
250
  cairo_surface_t* CairoWrapper::GetCairoSurface () const
166
251
  {
167
 
    return surface_;
 
252
    if (!pimpl->surface_)
 
253
      pimpl->Recreate();
 
254
 
 
255
    return pimpl->surface_;
168
256
  }
169
257
 
170
258
  cairo_t* CairoWrapper::GetCairoContext () const
171
259
  {
172
 
    return cr_;
173
 
  }
174
 
 
175
 
  bool CairoWrapper::Invalidate (Geometry const& geom)
176
 
  {
177
 
    if (geometry_.width  == geom.width &&
178
 
            geometry_.height == geom.height)
179
 
      return false;
180
 
 
181
 
        geometry_.x      = geom.x;
182
 
        geometry_.y      = geom.y;
183
 
        geometry_.width  = geom.width;
184
 
        geometry_.height = geom.height;
185
 
 
186
 
    Recreate (); 
187
 
 
188
 
    return true;
 
260
    if (!pimpl->cr_)
 
261
      pimpl->Recreate();
 
262
 
 
263
    return pimpl->cr_;
189
264
  }
190
265
}