Move to SkDataTable::MakeXXX and sk_sp.

Change SkDataTable::NewXXX to SkDataTable::MakeXXX and return sk_sp.
This updates users of SkDataTable to sk_sp as well.
There do not appear to be any external users of these methods.

Review-Url: https://codereview.chromium.org/2211143002
This commit is contained in:
bungeman 2016-08-05 06:51:50 -07:00 committed by Commit bot
parent 4c35a3f905
commit feb3c1a57f
12 changed files with 60 additions and 70 deletions

View File

@ -63,7 +63,7 @@ public:
typedef void (*FreeProc)(void* context);
static SkDataTable* NewEmpty();
static sk_sp<SkDataTable> MakeEmpty();
/**
* Return a new DataTable that contains a copy of the data stored in each
@ -74,8 +74,8 @@ public:
* ptrs[] array.
* @param count the number of array elements in ptrs[] and sizes[] to copy.
*/
static SkDataTable* NewCopyArrays(const void * const * ptrs,
const size_t sizes[], int count);
static sk_sp<SkDataTable> MakeCopyArrays(const void * const * ptrs,
const size_t sizes[], int count);
/**
* Return a new table that contains a copy of the data in array.
@ -85,11 +85,10 @@ public:
* @param count the number of entries to be copied out of array. The number
* of bytes that will be copied is count * elemSize.
*/
static SkDataTable* NewCopyArray(const void* array, size_t elemSize,
int count);
static sk_sp<SkDataTable> MakeCopyArray(const void* array, size_t elemSize, int count);
static SkDataTable* NewArrayProc(const void* array, size_t elemSize,
int count, FreeProc proc, void* context);
static sk_sp<SkDataTable> MakeArrayProc(const void* array, size_t elemSize, int count,
FreeProc proc, void* context);
private:
struct Dir {
@ -164,7 +163,7 @@ public:
* calls to append(). This call also clears any accumluated entries from
* this builder, so its count() will be 0 after this call.
*/
SkDataTable* detachDataTable();
sk_sp<SkDataTable> detachDataTable();
private:
SkTDArray<SkDataTable::Dir> fDir;

View File

@ -111,7 +111,7 @@ public:
// New APIS, which have default impls for now (which do nothing)
virtual SkDataTable* getFamilyNames() { return SkDataTable::NewEmpty(); }
virtual sk_sp<SkDataTable> getFamilyNames() { return SkDataTable::MakeEmpty(); }
typedef SkRefCnt INHERITED;
};

View File

@ -95,7 +95,7 @@ private:
mutable SkTArray<DataEntry> fDataCache;
mutable SkMutex fDataCacheMutex;
mutable SkAutoTUnref<SkDataTable> fFamilyNames;
mutable sk_sp<SkDataTable> fFamilyNames;
mutable SkOnce fFamilyNamesInitOnce;
static void set_up_family_names(const SkFontMgr_Indirect* self);

View File

@ -61,10 +61,8 @@ public:
*
* The indexes may be used with getIndex(int) and
* matchIndexStyle(int, SkFontStyle).
*
* The caller must unref() the returned object.
*/
virtual SkDataTable* getFamilyNames() const = 0;
virtual sk_sp<SkDataTable> getFamilyNames() const = 0;
/**
* Returns all of the fonts with the given familyIndex.

View File

@ -7,6 +7,7 @@
#include "SkData.h"
#include "SkDataTable.h"
#include "SkOnce.h"
static void malloc_freeproc(void* context) {
sk_free(context);
@ -76,19 +77,17 @@ const void* SkDataTable::at(int index, size_t* size) const {
///////////////////////////////////////////////////////////////////////////////
SkDataTable* SkDataTable::NewEmpty() {
static SkDataTable* gEmpty;
if (nullptr == gEmpty) {
gEmpty = new SkDataTable;
}
gEmpty->ref();
return gEmpty;
sk_sp<SkDataTable> SkDataTable::MakeEmpty() {
static SkDataTable* singleton;
static SkOnce once;
once([]{ singleton = new SkDataTable(); });
return sk_ref_sp(singleton);
}
SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs,
const size_t sizes[], int count) {
sk_sp<SkDataTable> SkDataTable::MakeCopyArrays(const void * const * ptrs,
const size_t sizes[], int count) {
if (count <= 0) {
return SkDataTable::NewEmpty();
return SkDataTable::MakeEmpty();
}
size_t dataSize = 0;
@ -108,28 +107,27 @@ SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs,
elem += sizes[i];
}
return new SkDataTable(dir, count, malloc_freeproc, buffer);
return sk_sp<SkDataTable>(new SkDataTable(dir, count, malloc_freeproc, buffer));
}
SkDataTable* SkDataTable::NewCopyArray(const void* array, size_t elemSize,
int count) {
sk_sp<SkDataTable> SkDataTable::MakeCopyArray(const void* array, size_t elemSize, int count) {
if (count <= 0) {
return SkDataTable::NewEmpty();
return SkDataTable::MakeEmpty();
}
size_t bufferSize = elemSize * count;
void* buffer = sk_malloc_throw(bufferSize);
memcpy(buffer, array, bufferSize);
return new SkDataTable(buffer, elemSize, count, malloc_freeproc, buffer);
return sk_sp<SkDataTable>(new SkDataTable(buffer, elemSize, count, malloc_freeproc, buffer));
}
SkDataTable* SkDataTable::NewArrayProc(const void* array, size_t elemSize,
int count, FreeProc proc, void* ctx) {
sk_sp<SkDataTable> SkDataTable::MakeArrayProc(const void* array, size_t elemSize, int count,
FreeProc proc, void* ctx) {
if (count <= 0) {
return SkDataTable::NewEmpty();
return SkDataTable::MakeEmpty();
}
return new SkDataTable(array, elemSize, count, proc, ctx);
return sk_sp<SkDataTable>(new SkDataTable(array, elemSize, count, proc, ctx));
}
///////////////////////////////////////////////////////////////////////////////
@ -164,18 +162,18 @@ void SkDataTableBuilder::append(const void* src, size_t size) {
dir->fSize = size;
}
SkDataTable* SkDataTableBuilder::detachDataTable() {
sk_sp<SkDataTable> SkDataTableBuilder::detachDataTable() {
const int count = fDir.count();
if (0 == count) {
return SkDataTable::NewEmpty();
return SkDataTable::MakeEmpty();
}
// Copy the dir into the heap;
void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir),
SkChunkAlloc::kThrow_AllocFailType);
void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir), SkChunkAlloc::kThrow_AllocFailType);
memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir));
SkDataTable* table = new SkDataTable((SkDataTable::Dir*)dir, count, chunkalloc_freeproc, fHeap);
sk_sp<SkDataTable> table(
new SkDataTable((SkDataTable::Dir*)dir, count, chunkalloc_freeproc, fHeap));
// we have to detach our fHeap, since we are giving that to the table
fHeap = nullptr;
fDir.reset();

View File

@ -61,7 +61,7 @@ private:
};
void SkFontMgr_Indirect::set_up_family_names(const SkFontMgr_Indirect* self) {
self->fFamilyNames.reset(self->fProxy->getFamilyNames());
self->fFamilyNames = self->fProxy->getFamilyNames();
}
int SkFontMgr_Indirect::onCountFamilies() const {

View File

@ -698,7 +698,7 @@ static bool find_name(const SkTDArray<const char*>& list, const char* str) {
return false;
}
SkDataTable* SkFontConfigInterfaceDirect::getFamilyNames() {
sk_sp<SkDataTable> SkFontConfigInterfaceDirect::getFamilyNames() {
FCLocker lock;
FcPattern* pat = FcPatternCreate();
@ -730,6 +730,6 @@ SkDataTable* SkFontConfigInterfaceDirect::getFamilyNames() {
}
}
return SkDataTable::NewCopyArrays((const void*const*)names.begin(),
sizes.begin(), names.count());
return SkDataTable::MakeCopyArrays((const void*const*)names.begin(),
sizes.begin(), names.count());
}

View File

@ -25,7 +25,7 @@ public:
SkStreamAsset* openStream(const FontIdentity&) override;
// new APIs
SkDataTable* getFamilyNames() override;
sk_sp<SkDataTable> getFamilyNames() override;
protected:
virtual bool isAccessible(const char* filename);

View File

@ -139,7 +139,7 @@ static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
class SkFontMgr_FCI : public SkFontMgr {
SkAutoTUnref<SkFontConfigInterface> fFCI;
SkAutoTUnref<SkDataTable> fFamilyNames;
sk_sp<SkDataTable> fFamilyNames;
SkTypeface_FreeType::Scanner fScanner;
mutable SkMutex fMutex;

View File

@ -503,7 +503,7 @@ private:
class SkFontMgr_fontconfig : public SkFontMgr {
mutable SkAutoFcConfig fFC;
SkAutoTUnref<SkDataTable> fFamilyNames;
sk_sp<SkDataTable> fFamilyNames;
SkTypeface_FreeType::Scanner fScanner;
class StyleSet : public SkFontStyleSet {
@ -579,7 +579,7 @@ class SkFontMgr_fontconfig : public SkFontMgr {
return false;
}
static SkDataTable* GetFamilyNames(FcConfig* fcconfig) {
static sk_sp<SkDataTable> GetFamilyNames(FcConfig* fcconfig) {
FCLocker lock;
SkTDArray<const char*> names;
@ -613,8 +613,8 @@ class SkFontMgr_fontconfig : public SkFontMgr {
}
}
return SkDataTable::NewCopyArrays((void const *const *)names.begin(),
sizes.begin(), names.count());
return SkDataTable::MakeCopyArrays((void const *const *)names.begin(),
sizes.begin(), names.count());
}
static bool FindByFcPattern(SkTypeface* cached, void* ctx) {

View File

@ -90,7 +90,7 @@ public:
memcpy(fLocaleName.get(), localeName, localeNameLength * sizeof(WCHAR));
}
SkDataTable* getFamilyNames() const override {
sk_sp<SkDataTable> getFamilyNames() const override {
int count = fFontCollection->GetFontFamilyCount();
SkDataTableBuilder names(1024);

View File

@ -25,35 +25,31 @@ static void test_is_equal(skiatest::Reporter* reporter,
}
}
static void test_datatable_is_empty(skiatest::Reporter* reporter,
SkDataTable* table) {
static void test_datatable_is_empty(skiatest::Reporter* reporter, SkDataTable* table) {
REPORTER_ASSERT(reporter, table->isEmpty());
REPORTER_ASSERT(reporter, 0 == table->count());
}
static void test_emptytable(skiatest::Reporter* reporter) {
SkAutoTUnref<SkDataTable> table0(SkDataTable::NewEmpty());
SkAutoTUnref<SkDataTable> table1(SkDataTable::NewCopyArrays(nullptr, nullptr, 0));
SkAutoTUnref<SkDataTable> table2(SkDataTable::NewCopyArray(nullptr, 0, 0));
SkAutoTUnref<SkDataTable> table3(SkDataTable::NewArrayProc(nullptr, 0, 0,
nullptr, nullptr));
sk_sp<SkDataTable> table0(SkDataTable::MakeEmpty());
sk_sp<SkDataTable> table1(SkDataTable::MakeCopyArrays(nullptr, nullptr, 0));
sk_sp<SkDataTable> table2(SkDataTable::MakeCopyArray(nullptr, 0, 0));
sk_sp<SkDataTable> table3(SkDataTable::MakeArrayProc(nullptr, 0, 0, nullptr, nullptr));
test_datatable_is_empty(reporter, table0);
test_datatable_is_empty(reporter, table1);
test_datatable_is_empty(reporter, table2);
test_datatable_is_empty(reporter, table3);
test_datatable_is_empty(reporter, table0.get());
test_datatable_is_empty(reporter, table1.get());
test_datatable_is_empty(reporter, table2.get());
test_datatable_is_empty(reporter, table3.get());
test_is_equal(reporter, table0, table1);
test_is_equal(reporter, table0, table2);
test_is_equal(reporter, table0, table3);
test_is_equal(reporter, table0.get(), table1.get());
test_is_equal(reporter, table0.get(), table2.get());
test_is_equal(reporter, table0.get(), table3.get());
}
static void test_simpletable(skiatest::Reporter* reporter) {
const int idata[] = { 1, 4, 9, 16, 25, 63 };
int icount = SK_ARRAY_COUNT(idata);
SkAutoTUnref<SkDataTable> itable(SkDataTable::NewCopyArray(idata,
sizeof(idata[0]),
icount));
sk_sp<SkDataTable> itable(SkDataTable::MakeCopyArray(idata, sizeof(idata[0]), icount));
REPORTER_ASSERT(reporter, itable->count() == icount);
for (int i = 0; i < icount; ++i) {
size_t size;
@ -73,8 +69,7 @@ static void test_vartable(skiatest::Reporter* reporter) {
sizes[i] = strlen(str[i]) + 1;
}
SkAutoTUnref<SkDataTable> table(SkDataTable::NewCopyArrays(
(const void*const*)str, sizes, count));
sk_sp<SkDataTable> table(SkDataTable::MakeCopyArrays((const void*const*)str, sizes, count));
REPORTER_ASSERT(reporter, table->count() == count);
for (int i = 0; i < count; ++i) {
@ -100,7 +95,7 @@ static void test_tablebuilder(skiatest::Reporter* reporter) {
for (int i = 0; i < count; ++i) {
builder.append(str[i], strlen(str[i]) + 1);
}
SkAutoTUnref<SkDataTable> table(builder.detachDataTable());
sk_sp<SkDataTable> table(builder.detachDataTable());
REPORTER_ASSERT(reporter, table->count() == count);
for (int i = 0; i < count; ++i) {
@ -121,8 +116,8 @@ static void test_globaltable(skiatest::Reporter* reporter) {
};
int count = SK_ARRAY_COUNT(gData);
SkAutoTUnref<SkDataTable> table(SkDataTable::NewArrayProc(gData,
sizeof(gData[0]), count, nullptr, nullptr));
sk_sp<SkDataTable> table(
SkDataTable::MakeArrayProc(gData, sizeof(gData[0]), count, nullptr, nullptr));
REPORTER_ASSERT(reporter, table->count() == count);
for (int i = 0; i < count; ++i) {