tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gfxSurfaceRefCountTest.cpp (4216B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <stdio.h>
      8 
      9 #include "gtest/gtest.h"
     10 
     11 #include "gfxASurface.h"
     12 #include "gfxImageSurface.h"
     13 
     14 #include "nsISupportsUtils.h"  // for NS_ADDREF
     15 
     16 #include "cairo.h"
     17 
     18 static int GetASurfaceRefCount(gfxASurface* s) {
     19  NS_ADDREF(s);
     20  return s->Release();
     21 }
     22 
     23 static int CheckInt(int value, int expected) {
     24  if (value != expected) {
     25    fprintf(stderr, "Expected %d got %d\n", expected, value);
     26    return 1;
     27  }
     28 
     29  return 0;
     30 }
     31 
     32 static int CheckPointer(void* value, void* expected) {
     33  if (value != expected) {
     34    fprintf(stderr, "Expected %p got %p\n", expected, value);
     35    return 1;
     36  }
     37 
     38  return 0;
     39 }
     40 
     41 static cairo_user_data_key_t destruction_key;
     42 static void SurfaceDestroyNotifier(void* data) { *(int*)data = 1; }
     43 
     44 static int TestNewSurface() {
     45  int failures = 0;
     46  int destroyed = 0;
     47 
     48  RefPtr<gfxASurface> s =
     49      new gfxImageSurface(mozilla::gfx::IntSize(10, 10),
     50                          mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32);
     51  cairo_surface_t* cs = s->CairoSurface();
     52 
     53  cairo_surface_set_user_data(cs, &destruction_key, &destroyed,
     54                              SurfaceDestroyNotifier);
     55 
     56  failures += CheckInt(GetASurfaceRefCount(s.get()), 1);
     57  failures += CheckInt(cairo_surface_get_reference_count(cs), 1);
     58  failures += CheckInt(destroyed, 0);
     59 
     60  cairo_surface_reference(cs);
     61 
     62  failures += CheckInt(GetASurfaceRefCount(s.get()), 2);
     63  failures += CheckInt(cairo_surface_get_reference_count(cs), 2);
     64  failures += CheckInt(destroyed, 0);
     65 
     66  gfxASurface* savedWrapper = s.get();
     67 
     68  s = nullptr;
     69 
     70  failures += CheckInt(cairo_surface_get_reference_count(cs), 1);
     71  failures += CheckInt(destroyed, 0);
     72 
     73  s = gfxASurface::Wrap(cs);
     74 
     75  failures += CheckPointer(s.get(), savedWrapper);
     76  failures += CheckInt(GetASurfaceRefCount(s.get()), 2);
     77  failures += CheckInt(cairo_surface_get_reference_count(cs), 2);
     78  failures += CheckInt(destroyed, 0);
     79 
     80  cairo_surface_destroy(cs);
     81 
     82  failures += CheckInt(GetASurfaceRefCount(s.get()), 1);
     83  failures += CheckInt(cairo_surface_get_reference_count(cs), 1);
     84  failures += CheckInt(destroyed, 0);
     85 
     86  s = nullptr;
     87 
     88  failures += CheckInt(destroyed, 1);
     89 
     90  return failures;
     91 }
     92 
     93 static int TestExistingSurface() {
     94  int failures = 0;
     95  int destroyed = 0;
     96 
     97  cairo_surface_t* cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
     98 
     99  cairo_surface_set_user_data(cs, &destruction_key, &destroyed,
    100                              SurfaceDestroyNotifier);
    101 
    102  failures += CheckInt(cairo_surface_get_reference_count(cs), 1);
    103  failures += CheckInt(destroyed, 0);
    104 
    105  RefPtr<gfxASurface> s = gfxASurface::Wrap(cs);
    106 
    107  failures += CheckInt(GetASurfaceRefCount(s.get()), 2);
    108 
    109  cairo_surface_reference(cs);
    110 
    111  failures += CheckInt(GetASurfaceRefCount(s.get()), 3);
    112  failures += CheckInt(cairo_surface_get_reference_count(cs), 3);
    113  failures += CheckInt(destroyed, 0);
    114 
    115  gfxASurface* savedWrapper = s.get();
    116 
    117  s = nullptr;
    118 
    119  failures += CheckInt(cairo_surface_get_reference_count(cs), 2);
    120  failures += CheckInt(destroyed, 0);
    121 
    122  s = gfxASurface::Wrap(cs);
    123 
    124  failures += CheckPointer(s.get(), savedWrapper);
    125  failures += CheckInt(GetASurfaceRefCount(s.get()), 3);
    126  failures += CheckInt(cairo_surface_get_reference_count(cs), 3);
    127  failures += CheckInt(destroyed, 0);
    128 
    129  cairo_surface_destroy(cs);
    130 
    131  failures += CheckInt(GetASurfaceRefCount(s.get()), 2);
    132  failures += CheckInt(cairo_surface_get_reference_count(cs), 2);
    133  failures += CheckInt(destroyed, 0);
    134 
    135  s = nullptr;
    136 
    137  failures += CheckInt(cairo_surface_get_reference_count(cs), 1);
    138  failures += CheckInt(destroyed, 0);
    139 
    140  cairo_surface_destroy(cs);
    141 
    142  failures += CheckInt(destroyed, 1);
    143 
    144  return failures;
    145 }
    146 
    147 TEST(Gfx, SurfaceRefCount)
    148 {
    149  int fail;
    150 
    151  fail = TestNewSurface();
    152  EXPECT_TRUE(fail == 0) << "TestNewSurface: " << fail << " failures";
    153  fail = TestExistingSurface();
    154  EXPECT_TRUE(fail == 0) << "TestExistingSurface: " << fail << " failures";
    155 }