Skip to content

Commit

Permalink
Starboardization skia to prevent API leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sherryzy committed Jan 25, 2024
1 parent 4a95370 commit 4b4b536
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 9 deletions.
10 changes: 8 additions & 2 deletions third_party/skia/include/core/SkTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#ifndef SkTypes_DEFINED
#define SkTypes_DEFINED

#if defined(STARBOARD)
#include "base/logging.h"
#endif

/** \file SkTypes.h
*/

Expand Down Expand Up @@ -278,8 +282,8 @@
# endif
# define SK_ABORT(message, ...) \
do { \
SkDebugf(SK_DUMP_LINE_FORMAT ": fatal error: \"" message "\"\n", \
__FILE__, __LINE__, ##__VA_ARGS__); \
SkDebugf(SK_DUMP_LINE_FORMAT ": fatal error: \"%s\"\n", \
__FILE__, __LINE__, ##__VA_ARGS__); \
SK_DUMP_GOOGLE3_STACK(); \
sk_abort_no_print(); \
} while (false)
Expand Down Expand Up @@ -341,13 +345,15 @@ inline SkPmcolor GetSkPmcolor() {
}
#endif

#if !defined(STARBOARD)
#if defined SK_DEBUG && defined SK_BUILD_FOR_WIN
#ifdef free
#undef free
#endif
#include <crtdbg.h>
#undef free
#endif
#endif

#if !defined(SK_UNUSED)
# if !defined(__clang__) && defined(_MSC_VER)
Expand Down
31 changes: 29 additions & 2 deletions third_party/skia/src/core/SkExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
#include <deque>
#include <thread>

#if defined(STARBOARD)
#include "starboard/thread.h"
#endif

namespace {

#if defined(STARBOARD)
// Starboardize std::thread as thread creation is undefined for stub builds.
class SkThread {
public:
SkThread(SbThreadEntryPoint entry_point, void* context) {
SbThreadCreate(0, // default stack_size.
kSbThreadNoPriority, // default priority.
kSbThreadNoAffinity, // default affinity.
true, // joinable.
"SkThread", entry_point, this);
}

bool join() { return SbThreadJoin((SbThread)this, NULL); }
};
#else
typedef std::thread SkThread;
#endif

} // namespace

#if defined(STARBOARD)
#include "starboard/system.h"
static int num_cores() {
Expand Down Expand Up @@ -129,17 +155,18 @@ class SkThreadPool final : public SkExecutor {
return true;
}

static void Loop(void* ctx) {
static void* Loop(void* ctx) {
auto pool = (SkThreadPool*)ctx;
do {
pool->fWorkAvailable.wait();
} while (pool->do_work());
return nullptr;
}

// Both SkMutex and SkSpinlock can work here.
using Lock = SkMutex;

SkTArray<std::thread> fThreads;
SkTArray<SkThread> fThreads;
WorkList fWork;
Lock fWorkLock;
SkSemaphore fWorkAvailable;
Expand Down
13 changes: 12 additions & 1 deletion third_party/skia/src/core/SkSemaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
#include "include/private/SkSemaphore.h"
#include "src/core/SkLeanWindows.h"

#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#if defined(STARBOARD)
#include "starboard/common/semaphore.h"

struct SkSemaphore::OSSemaphore {
starboard::Semaphore fSemaphore;

OSSemaphore(): fSemaphore(0/*initial count*/) {}

void signal(int n) { while (n -- > 0) { fSemaphore.Put(); } }
void wait() { fSemaphore.Take(); }
};
#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include <dispatch/dispatch.h>

struct SkSemaphore::OSSemaphore {
Expand Down
2 changes: 2 additions & 0 deletions third_party/skia/src/core/SkStrikeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
bool gSkUseThreadLocalStrikeCaches_IAcknowledgeThisIsIncrediblyExperimental = false;

SkStrikeCache* SkStrikeCache::GlobalStrikeCache() {
#if !defined(STARBOARD)
if (gSkUseThreadLocalStrikeCaches_IAcknowledgeThisIsIncrediblyExperimental) {
static thread_local auto* cache = new SkStrikeCache;
return cache;
}
#endif
static auto* cache = new SkStrikeCache;
return cache;
}
Expand Down
5 changes: 4 additions & 1 deletion third_party/skia/src/core/SkThreadID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

#include "include/private/SkThreadID.h"

#ifdef SK_BUILD_FOR_WIN
#if defined(STARBOARD)
#include "starboard/thread.h"
SkThreadID SkGetThreadID() { return SbThreadGetId(); }
#elif defined(SK_BUILD_FOR_WIN)
#include "src/core/SkLeanWindows.h"
SkThreadID SkGetThreadID() { return GetCurrentThreadId(); }
#else
Expand Down
12 changes: 12 additions & 0 deletions third_party/skia/src/gpu/GrAutoLocaleSetter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
#ifndef GrAutoLocaleSetter_DEFINED
#define GrAutoLocaleSetter_DEFINED

#if defined(STARBOARD)

class GrAutoLocaleSetter : public SkNoncopyable {
public:
// Starboard does not support setting locales.
GrAutoLocaleSetter(const char*) {}
};

#else // defined(STARBOARD)

#include "include/gpu/GrTypes.h"
#include "include/private/SkNoncopyable.h"

Expand Down Expand Up @@ -95,4 +105,6 @@ class GrAutoLocaleSetter : public SkNoncopyable {
#undef HAVE_LOCALE_T
#undef HAVE_XLOCALE

#endif // defined(STARBOARD)

#endif
10 changes: 9 additions & 1 deletion third_party/skia/src/gpu/ops/AtlasTextOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,35 @@

namespace skgpu::v1 {

#if !defined(STARBOARD)
// If we have thread local, then cache memory for a single AtlasTextOp.
static thread_local void* gCache = nullptr;
#endif

void* AtlasTextOp::operator new(size_t s) {
#if !defined(STARBOARD)
if (gCache != nullptr) {
return std::exchange(gCache, nullptr);
}

#endif
return ::operator new(s);
}

void AtlasTextOp::operator delete(void* bytes) noexcept {
#if !defined(STARBOARD)
if (gCache == nullptr) {
gCache = bytes;
return;
}
#endif
::operator delete(bytes);
}

void AtlasTextOp::ClearCache() {
#if !defined(STARBOARD)
::operator delete(gCache);
gCache = nullptr;
#endif
}

AtlasTextOp::AtlasTextOp(MaskType maskType,
Expand Down
16 changes: 16 additions & 0 deletions third_party/skia/src/gpu/text/GrAtlasManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "src/gpu/GrImageInfo.h"
#include "src/gpu/text/GrStrikeCache.h"

#if defined(STARBOARD)
#include "starboard/file.h"
#endif

GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider,
size_t maxTextureBytes,
GrDrawOpAtlas::AllowMultitexturing allowMultitexturing)
Expand Down Expand Up @@ -246,19 +250,31 @@ static bool save_pixels(GrDirectContext* dContext, GrSurfaceProxyView view, GrCo
return false;
}

#if !defined(STARBOARD)
// remove any previous version of this file
remove(filename);
#else
SbFileDelete(filename);
#endif

SkFILEWStream file(filename);
if (!file.isValid()) {
SkDebugf("------ failed to create file: %s\n", filename);
#if !defined(STARBOARD)
remove(filename); // remove any partial file
#else
SbFileDelete(filename);
#endif
return false;
}

if (!SkEncodeImage(&file, bm, SkEncodedImageFormat::kPNG, 100)) {
SkDebugf("------ failed to encode %s\n", filename);
#if !defined(STARBOARD)
remove(filename); // remove any partial file
#else
SbFileDelete(filename);
#endif
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion third_party/skia/src/ports/SkFontHost_FreeType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

// SK_FREETYPE_MINIMUM_RUNTIME_VERSION 0x<major><minor><patch><flags>
// Flag SK_FREETYPE_DLOPEN: also try dlopen to get newer features.
#define SK_FREETYPE_DLOPEN (0x1)
#define SK_FREETYPE_DLOPEN (0x0)
#ifndef SK_FREETYPE_MINIMUM_RUNTIME_VERSION
# if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined (SK_BUILD_FOR_GOOGLE3)
# define SK_FREETYPE_MINIMUM_RUNTIME_VERSION (((FREETYPE_MAJOR) << 24) | ((FREETYPE_MINOR) << 16) | ((FREETYPE_PATCH) << 8))
Expand Down
34 changes: 33 additions & 1 deletion third_party/skia/src/sksl/SkSLPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@

#ifndef STARBOARD // Avoid redefining VLOG from base/logging.h
#define VLOG(...) // printf(__VA_ARGS__)
#else
#include "starboard/common/log.h"
#include "starboard/once.h"
#include "starboard/thread.h"
#endif

namespace SkSL {

#if !defined(STARBOARD)
static thread_local MemoryPool* sMemPool = nullptr;

static MemoryPool* get_thread_local_memory_pool() {
Expand All @@ -24,6 +29,33 @@ static MemoryPool* get_thread_local_memory_pool() {
static void set_thread_local_memory_pool(MemoryPool* memPool) {
sMemPool = memPool;
}
#else
namespace {
SbOnceControl s_once_flag = SB_ONCE_INITIALIZER;
SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid;

void InitThreadLocalKey() {
s_thread_local_key = SbThreadCreateLocalKey(nullptr);
SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
SbThreadSetLocalValue(s_thread_local_key, nullptr);
}

void EnsureThreadLocalKeyInited() {
SbOnce(&s_once_flag, InitThreadLocalKey);
SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
}
} // namespace

static MemoryPool* get_thread_local_memory_pool() {
return static_cast<MemoryPool*>(SbThreadGetLocalValue(s_thread_local_key));
}

static void set_thread_local_memory_pool(MemoryPool* memPool) {
EnsureThreadLocalKeyInited();
SbThreadSetLocalValue(s_thread_local_key, memPool);
}
#endif


Pool::~Pool() {
if (get_thread_local_memory_pool() == fMemPool.get()) {
Expand Down Expand Up @@ -56,7 +88,7 @@ void Pool::attachToThread() {
#ifndef STARBOARD
VLOG("ATTACH Pool:0x%016llX\n", (uint64_t)fMemPool.get());
#endif
SkASSERT(get_thread_local_memory_pool() == nullptr);
// SkASSERT(get_thread_local_memory_pool() == nullptr);
set_thread_local_memory_pool(fMemPool.get());
}

Expand Down
35 changes: 35 additions & 0 deletions third_party/skia/src/sksl/SkSLThreadContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ void ThreadContext::ReportErrors(PositionInfo pos) {
GetErrorReporter().reportPendingErrors(pos);
}

#if !defined(STARBOARD)
thread_local ThreadContext* instance = nullptr;

bool ThreadContext::IsActive() {
Expand All @@ -201,5 +202,39 @@ void ThreadContext::SetInstance(std::unique_ptr<ThreadContext> newInstance) {
delete instance;
instance = newInstance.release();
}
#else
#include "starboard/common/log.h"
#include "starboard/once.h"
#include "starboard/thread.h"
namespace {
ThreadContext* instance = nullptr;

SbOnceControl s_once_flag = SB_ONCE_INITIALIZER;
SbThreadLocalKey s_thread_local_key = kSbThreadLocalKeyInvalid;

void InitThreadLocalKey() {
s_thread_local_key = SbThreadCreateLocalKey(nullptr);
SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
SbThreadSetLocalValue(s_thread_local_key, nullptr);
}

void EnsureThreadLocalKeyInited() {
SbOnce(&s_once_flag, InitThreadLocalKey);
SB_DCHECK(SbThreadIsValidLocalKey(s_thread_local_key));
}
} // namespace

bool ThreadContext::IsActive() { return SbThreadGetLocalValue(s_thread_local_key) != nullptr; }

ThreadContext& ThreadContext::Instance() { return *instance; }

void ThreadContext::SetInstance(std::unique_ptr<ThreadContext> newInstance) {
EnsureThreadLocalKeyInited();
delete instance;
SbThreadSetLocalValue(s_thread_local_key, static_cast<void*>(newInstance.release()));
instance = static_cast<ThreadContext*>(SbThreadGetLocalValue(s_thread_local_key));
}

#endif

} // namespace SkSL

0 comments on commit 4b4b536

Please sign in to comment.