~3v1n0/unity/static-cairo-text-rewrite

« back to all changes in this revision

Viewing changes to unity-shared/StaticCairoText.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-03-15 18:30:18 UTC
  • Revision ID: mail@3v1n0.net-20130315183018-mmumwiuqmagxgmmb
StaticCairoText: reduce the textures/extents updates

And do them when really needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 
57
57
  glib::Object<PangoLayout> GetLayoutForContext(CairoGraphics& cairo);
58
58
  void SetAttributes(glib::Object<PangoLayout> const& layout);
59
 
  void UpdateTexture();
 
59
  void UpdateTexExtents(bool force = false);
 
60
  void UpdateTexture(bool force = false);
60
61
  void OnFontChanged();
61
62
 
62
63
  static void FontChanged(GObject* gobject, GParamSpec* pspec, gpointer data);
63
64
 
64
65
  StaticCairoText* parent_;
65
66
  bool accept_key_nav_focus_;
66
 
  bool need_new_extent_cache_;
67
67
  Size cached_extent_;
68
68
  int baseline_;
69
69
 
88
88
StaticCairoText::Impl::Impl(StaticCairoText* parent, std::string const& text)
89
89
  : parent_(parent)
90
90
  , accept_key_nav_focus_(false)
91
 
  , need_new_extent_cache_(true)
92
91
  , baseline_(0)
93
92
  , text_(text)
94
93
  , text_color_(color::White)
190
189
 
191
190
void StaticCairoText::SetLines(int lines)
192
191
{
 
192
  if (pimpl->lines_ == lines)
 
193
    return;
 
194
 
193
195
  pimpl->lines_ = lines;
 
196
  pimpl->UpdateTexExtents(true);
194
197
  pimpl->UpdateTexture();
195
198
  QueueDraw();
196
199
}
197
200
 
198
201
void StaticCairoText::SetLineSpacing(float line_spacing)
199
202
{
 
203
  if (pimpl->lines_ == line_spacing)
 
204
    return;
 
205
 
200
206
  pimpl->line_spacing_ = line_spacing;
 
207
  pimpl->UpdateTexExtents(true);
201
208
  pimpl->UpdateTexture();
202
209
  QueueDraw();
203
210
}
297
304
 
298
305
void StaticCairoText::SetText(std::string const& text, bool escape_text)
299
306
{
300
 
  std::string tmp_text = escape_text ? GetEscapedText(text) : text;
 
307
  std::string const& tmp_text = escape_text ? GetEscapedText(text) : text;
301
308
 
302
309
  if (pimpl->text_ != tmp_text)
303
310
  {
304
311
    pimpl->text_ = tmp_text;
305
 
    pimpl->need_new_extent_cache_ = true;
 
312
    pimpl->UpdateTexExtents(true);
306
313
    pimpl->UpdateTexture();
307
314
    sigTextChanged.emit(this);
308
315
  }
313
320
  if (pimpl->text_color_.alpha != alpha)
314
321
  {
315
322
    pimpl->text_color_.alpha = alpha;
316
 
    pimpl->UpdateTexture();
 
323
    pimpl->UpdateTexture(true);
317
324
    QueueDraw();
318
325
  }
319
326
}
322
329
{
323
330
  if (w != GetMaximumWidth())
324
331
  {
325
 
    pimpl->need_new_extent_cache_ = true;
326
332
    View::SetMaximumSize(w, h);
 
333
    pimpl->UpdateTexExtents(true);
327
334
    pimpl->UpdateTexture();
328
335
    return;
329
336
  }
335
342
{
336
343
  if (w != GetMaximumWidth())
337
344
  {
338
 
    pimpl->need_new_extent_cache_ = true;
339
345
    View::SetMaximumWidth(w);
 
346
    pimpl->UpdateTexExtents(true);
340
347
    pimpl->UpdateTexture();
341
348
  }
342
349
}
361
368
  if (pimpl->text_color_ != textColor)
362
369
  {
363
370
    pimpl->text_color_ = textColor;
364
 
    pimpl->UpdateTexture();
 
371
    pimpl->UpdateTexture(true);
365
372
    QueueDraw();
366
373
 
367
374
    sigTextColorChanged.emit(this);
373
380
  if (pimpl->font_ != font)
374
381
  {
375
382
    pimpl->font_ = font;
376
 
    pimpl->need_new_extent_cache_ = true;
377
 
    Size s = GetTextExtents();
378
 
    SetMinimumHeight(s.height);
379
 
    NeedRedraw();
 
383
    pimpl->UpdateTexture(true);
 
384
    pimpl->UpdateTexture();
 
385
    SetMinimumHeight(GetTextExtents().height);
 
386
    QueueDraw();
 
387
 
380
388
    sigFontChanged.emit(this);
381
389
  }
382
390
}
391
399
  if (pimpl->underline_ != underline)
392
400
  {
393
401
    pimpl->underline_ = underline;
394
 
    pimpl->need_new_extent_cache_ = true;
395
 
    Size s = GetTextExtents();
396
 
    SetMinimumHeight(s.height);
397
 
    NeedRedraw();
 
402
    pimpl->UpdateTexture(true);
 
403
    pimpl->UpdateTexture();
 
404
    SetMinimumHeight(GetTextExtents().height);
 
405
 
 
406
    QueueDraw();
398
407
  }
399
408
}
400
409
 
528
537
  return layout;
529
538
}
530
539
 
531
 
void StaticCairoText::Impl::UpdateTexture()
 
540
void StaticCairoText::Impl::UpdateTexExtents(bool force)
532
541
{
533
 
  auto old_extents = cached_extent_;
534
 
 
535
 
  if (need_new_extent_cache_)
536
 
  {
537
 
    CairoGraphics dummy_cg(CAIRO_FORMAT_A1, 1, 1);
538
 
    auto const& layout = GetLayoutForContext(dummy_cg);
539
 
 
540
 
    pango_layout_get_pixel_size(layout, &cached_extent_.width, &cached_extent_.height);
541
 
    baseline_ = PANGO_PIXELS(pango_layout_get_baseline(layout));
542
 
    actual_lines_ = pango_layout_get_line_count(layout);
543
 
    need_new_extent_cache_ = false;
544
 
    UpdateBaseSize();
545
 
  }
546
 
 
547
 
  if (texture_ && old_extents == cached_extent_)
548
 
  {
549
 
    // We're confident that nothing changed. No need to generate the texture again.
 
542
  if (texture_ && !force)
550
543
    return;
 
544
 
 
545
  CairoGraphics dummy_cg(CAIRO_FORMAT_A1, 1, 1);
 
546
  auto const& layout = GetLayoutForContext(dummy_cg);
 
547
 
 
548
  pango_layout_get_pixel_size(layout, &cached_extent_.width, &cached_extent_.height);
 
549
  baseline_ = PANGO_PIXELS(pango_layout_get_baseline(layout));
 
550
  actual_lines_ = pango_layout_get_line_count(layout);
 
551
  UpdateBaseSize();
 
552
}
 
553
 
 
554
void StaticCairoText::Impl::UpdateTexture(bool force)
 
555
{
 
556
  UpdateTexExtents();
 
557
 
 
558
  if (texture_ && !force)
 
559
  {
 
560
    nux::Size old_extents(texture_->GetWidth(), texture_->GetHeight());
 
561
 
 
562
    if (old_extents == cached_extent_)
 
563
    {
 
564
      // We're confident that nothing changed. No need to generate the texture again.
 
565
      return;
 
566
    }
551
567
  }
552
568
 
553
569
  CairoGraphics cg(CAIRO_FORMAT_ARGB32, cached_extent_.width, cached_extent_.height);
578
594
 
579
595
void StaticCairoText::Impl::OnFontChanged()
580
596
{
581
 
  need_new_extent_cache_ = true;
 
597
  UpdateTexExtents(true);
582
598
  UpdateTexture();
583
599
  parent_->sigFontChanged.emit(parent_);
584
600
}