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 23, 2024
1 parent 4a95370 commit c9e7965
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
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
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
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 c9e7965

Please sign in to comment.