1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkDescriptor_DEFINED
11#define SkDescriptor_DEFINED
12
13#include "SkChecksum.h"
14#include "SkTypes.h"
15
16class SkDescriptor : SkNoncopyable {
17public:
18    static size_t ComputeOverhead(int entryCount) {
19        SkASSERT(entryCount >= 0);
20        return sizeof(SkDescriptor) + entryCount * sizeof(Entry);
21    }
22
23    static SkDescriptor* Alloc(size_t length) {
24        SkASSERT(SkAlign4(length) == length);
25        SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length);
26        return desc;
27    }
28
29    static void Free(SkDescriptor* desc) {
30        sk_free(desc);
31    }
32
33    void init() {
34        fLength = sizeof(SkDescriptor);
35        fCount  = 0;
36    }
37
38    uint32_t getLength() const { return fLength; }
39
40    void* addEntry(uint32_t tag, size_t length, const void* data = NULL) {
41        SkASSERT(tag);
42        SkASSERT(SkAlign4(length) == length);
43        SkASSERT(this->findEntry(tag, NULL) == NULL);
44
45        Entry*  entry = (Entry*)((char*)this + fLength);
46        entry->fTag = tag;
47        entry->fLen = SkToU32(length);
48        if (data) {
49            memcpy(entry + 1, data, length);
50        }
51
52        fCount += 1;
53        fLength += sizeof(Entry) + length;
54        return (entry + 1); // return its data
55    }
56
57    void computeChecksum() {
58        fChecksum = SkDescriptor::ComputeChecksum(this);
59    }
60
61#ifdef SK_DEBUG
62    void assertChecksum() const {
63        SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum);
64    }
65#endif
66
67    const void* findEntry(uint32_t tag, uint32_t* length) const {
68        const Entry* entry = (const Entry*)(this + 1);
69        int          count = fCount;
70
71        while (--count >= 0) {
72            if (entry->fTag == tag) {
73                if (length) {
74                    *length = entry->fLen;
75                }
76                return entry + 1;
77            }
78            entry = (const Entry*)((const char*)(entry + 1) + entry->fLen);
79        }
80        return NULL;
81    }
82
83    SkDescriptor* copy() const {
84        SkDescriptor* desc = SkDescriptor::Alloc(fLength);
85        memcpy(desc, this, fLength);
86        return desc;
87    }
88
89    bool equals(const SkDescriptor& other) const {
90        // probe to see if we have a good checksum algo
91//        SkASSERT(a.fChecksum != b.fChecksum || memcmp(&a, &b, a.fLength) == 0);
92
93        // the first value we should look at is the checksum, so this loop
94        // should terminate early if they descriptors are different.
95        // NOTE: if we wrote a sentinel value at the end of each, we chould
96        //       remove the aa < stop test in the loop...
97        const uint32_t* aa = (const uint32_t*)this;
98        const uint32_t* bb = (const uint32_t*)&other;
99        const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength);
100        do {
101            if (*aa++ != *bb++)
102                return false;
103        } while (aa < stop);
104        return true;
105    }
106
107    uint32_t getChecksum() const { return fChecksum; }
108
109    struct Entry {
110        uint32_t fTag;
111        uint32_t fLen;
112    };
113
114#ifdef SK_DEBUG
115    uint32_t getCount() const { return fCount; }
116#endif
117
118private:
119    uint32_t fChecksum;  // must be first
120    uint32_t fLength;    // must be second
121    uint32_t fCount;
122
123    static uint32_t ComputeChecksum(const SkDescriptor* desc) {
124        const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field
125        size_t len = desc->fLength - sizeof(uint32_t);
126        return SkChecksum::Murmur3(ptr, len);
127    }
128
129    // private so no one can create one except our factories
130    SkDescriptor() {}
131};
132
133#include "SkScalerContext.h"
134
135class SkAutoDescriptor : SkNoncopyable {
136public:
137    SkAutoDescriptor(size_t size) {
138        if (size <= sizeof(fStorage)) {
139            fDesc = (SkDescriptor*)(void*)fStorage;
140        } else {
141            fDesc = SkDescriptor::Alloc(size);
142        }
143    }
144
145    ~SkAutoDescriptor() {
146        if (fDesc != (SkDescriptor*)(void*)fStorage) {
147            SkDescriptor::Free(fDesc);
148        }
149    }
150
151    SkDescriptor* getDesc() const { return fDesc; }
152private:
153    enum {
154        kStorageSize =  sizeof(SkDescriptor)
155                        + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext::Rec)    // for rec
156                        + sizeof(SkDescriptor::Entry) + sizeof(void*)                   // for typeface
157                        + 32   // slop for occational small extras
158    };
159    SkDescriptor*   fDesc;
160    uint32_t        fStorage[(kStorageSize + 3) >> 2];
161};
162#define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor)
163
164
165#endif
166