Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkDescriptor_DEFINED | 10 #ifndef SkDescriptor_DEFINED |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 } | 127 } |
| 128 | 128 |
| 129 // private so no one can create one except our factories | 129 // private so no one can create one except our factories |
| 130 SkDescriptor() {} | 130 SkDescriptor() {} |
| 131 }; | 131 }; |
| 132 | 132 |
| 133 #include "SkScalerContext.h" | 133 #include "SkScalerContext.h" |
| 134 | 134 |
| 135 class SkAutoDescriptor : SkNoncopyable { | 135 class SkAutoDescriptor : SkNoncopyable { |
| 136 public: | 136 public: |
| 137 SkAutoDescriptor(size_t size) { | 137 SkAutoDescriptor() : fDesc(NULL), fHeapAllocSize(0) {} |
| 138 if (size <= sizeof(fStorage)) { | 138 SkAutoDescriptor(size_t size) : fDesc(NULL), fHeapAllocSize(0) { this->reset (size); } |
| 139 | |
| 140 ~SkAutoDescriptor() { this->free(); } | |
| 141 | |
| 142 void reset(size_t size) { | |
| 143 if (this->isHeapAlloc()) { | |
| 144 if (size > fHeapAllocSize) { | |
| 145 this->free(); | |
| 146 this->alloc(size); | |
| 147 } | |
| 148 } else if (size <= sizeof(fStorage)) { | |
| 139 fDesc = (SkDescriptor*)(void*)fStorage; | 149 fDesc = (SkDescriptor*)(void*)fStorage; |
| 140 } else { | 150 } else { |
| 141 fDesc = SkDescriptor::Alloc(size); | 151 SkASSERT(!fDesc); |
| 152 this->alloc(size); | |
| 142 } | 153 } |
| 143 } | 154 } |
| 144 | 155 |
| 145 ~SkAutoDescriptor() { | 156 SkDescriptor* getDesc() const { SkASSERT(fDesc); return fDesc; } |
| 146 if (fDesc != (SkDescriptor*)(void*)fStorage) { | 157 private: |
| 158 void alloc(size_t size) { | |
| 159 fDesc = SkDescriptor::Alloc(size); | |
| 160 fHeapAllocSize = size; | |
| 161 } | |
| 162 bool isHeapAlloc() const { return fDesc && fDesc != (SkDescriptor*)(void*)fS torage; } | |
| 163 void free() { | |
| 164 if (this->isHeapAlloc()) { | |
| 147 SkDescriptor::Free(fDesc); | 165 SkDescriptor::Free(fDesc); |
| 148 } | 166 } |
| 149 } | 167 } |
| 150 | 168 |
| 151 SkDescriptor* getDesc() const { return fDesc; } | |
| 152 private: | |
| 153 enum { | 169 enum { |
| 154 kStorageSize = sizeof(SkDescriptor) | 170 kStorageSize = sizeof(SkDescriptor) |
| 155 + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext:: Rec) // for rec | 171 + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext:: Rec) // for rec |
| 156 + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface | 172 + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface |
| 157 + 32 // slop for occational small extras | 173 + 32 // slop for occational small extras |
| 158 }; | 174 }; |
| 159 SkDescriptor* fDesc; | 175 SkDescriptor* fDesc; |
| 160 uint32_t fStorage[(kStorageSize + 3) >> 2]; | 176 uint32_t fStorage[(kStorageSize + 3) >> 2]; |
| 177 size_t fHeapAllocSize; | |
|
bsalomon
2015/04/01 15:41:45
Is this really worth the complexity? Seems much si
joshualitt
2015/04/01 16:54:23
If I'm going to be reusing the blobs, which I basi
| |
| 161 }; | 178 }; |
| 162 #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor) | 179 #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor) |
| 163 | 180 |
| 164 | 181 |
| 165 #endif | 182 #endif |
| OLD | NEW |