/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFontConfigInterface.h"
#include "SkFontMgr.h"
#include "SkMutex.h"
#include "SkRefCnt.h"

SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
static SkFontConfigInterface* gFontConfigInterface;

sk_sp<SkFontConfigInterface> SkFontConfigInterface::RefGlobal() {
    SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);

    if (gFontConfigInterface) {
        return sk_ref_sp(gFontConfigInterface);
    }
    return sk_ref_sp(SkFontConfigInterface::GetSingletonDirectInterface());
}

void SkFontConfigInterface::SetGlobal(sk_sp<SkFontConfigInterface> fc) {
    SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);

    SkSafeUnref(gFontConfigInterface);
    gFontConfigInterface = fc.release();
}

/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFontConfigInterface.h"
#include "SkFontMgr.h"
#include "SkMutex.h"
#include "SkRefCnt.h"

SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
static sk_sp<SkFontConfigInterface> gFontConfigInterface(nullptr);

sk_sp<SkFontConfigInterface> SkFontConfigInterface::RefGlobal() {
    SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);

    if (gFontConfigInterface) {
        return gFontConfigInterface;
    }
    return sk_ref_sp(SkFontConfigInterface::GetSingletonDirectInterface());
}

void SkFontConfigInterface::SetGlobal(sk_sp<SkFontConfigInterface> fc) {
    SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);

    gFontConfigInterface = std::move(fc);
}

--- 
+++ 
@@ -11,13 +11,13 @@
 #include "SkRefCnt.h"
 
 SK_DECLARE_STATIC_MUTEX(gFontConfigInterfaceMutex);
-static sk_sp<SkFontConfigInterface> gFontConfigInterface(nullptr);
+static SkFontConfigInterface* gFontConfigInterface;
 
 sk_sp<SkFontConfigInterface> SkFontConfigInterface::RefGlobal() {
     SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
 
     if (gFontConfigInterface) {
-        return gFontConfigInterface;
+        return sk_ref_sp(gFontConfigInterface);
     }
     return sk_ref_sp(SkFontConfigInterface::GetSingletonDirectInterface());
 }
@@ -25,5 +25,6 @@
 void SkFontConfigInterface::SetGlobal(sk_sp<SkFontConfigInterface> fc) {
     SkAutoMutexAcquire ac(gFontConfigInterfaceMutex);
 
-    gFontConfigInterface = std::move(fc);
+    SkSafeUnref(gFontConfigInterface);
+    gFontConfigInterface = fc.release();
 }
Remove static initializer to register destructor.

Skia cannot have global sk_sp objects because they will register an exit
time destructor which will be done through a static initializer.

BUG=chromium:843858

Change-Id: I43e3d18c9d8b50ff067414148c7524bffd3523da
Reviewed-on: https://skia-review.googlesource.com/128843
Auto-Submit: Ben Wagner <53965154ffeecc4a10886eab9259e95965f2979a@google.com>
Commit-Queue: Herb Derby <9e12a3d2bbf73546e47cc9958f6ba8f3215b7eaf@google.com>
Reviewed-by: Herb Derby <9e12a3d2bbf73546e47cc9958f6ba8f3215b7eaf@google.com>
