Coverage Report

Created: 2024-05-20 07:14

/src/skia/src/core/SkPixelRef.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011 Google Inc.
3
 *
4
 * Use of this source code is governed by a BSD-style license that can be
5
 * found in the LICENSE file.
6
 */
7
8
#include "include/core/SkPixelRef.h"
9
10
#include "include/private/base/SkAssert.h"
11
#include "include/private/base/SkDebug.h"
12
#include "src/core/SkBitmapCache.h"
13
#include "src/core/SkNextID.h"
14
#include "src/core/SkPixelRefPriv.h"
15
16
#include <atomic>
17
#include <utility>
18
19
735k
uint32_t SkNextID::ImageID() {
20
    // We never set the low bit.... see SkPixelRef::genIDIsUnique().
21
735k
    static std::atomic<uint32_t> nextID{2};
22
23
735k
    uint32_t id;
24
735k
    do {
25
735k
        id = nextID.fetch_add(2, std::memory_order_relaxed);
26
735k
    } while (id == 0);
27
735k
    return id;
28
735k
}
29
30
///////////////////////////////////////////////////////////////////////////////
31
32
SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes)
33
    : fWidth(width)
34
    , fHeight(height)
35
    , fPixels(pixels)
36
    , fRowBytes(rowBytes)
37
    , fAddedToCache(false)
38
432k
{
39
432k
    this->needsNewGenID();
40
432k
    fMutability = kMutable;
41
432k
}
42
43
432k
SkPixelRef::~SkPixelRef() {
44
432k
    this->callGenIDChangeListeners();
45
432k
}
46
47
// This is undefined if there are clients in-flight trying to use us
48
0
void SkPixelRef::android_only_reset(int width, int height, size_t rowBytes) {
49
0
    fWidth = width;
50
0
    fHeight = height;
51
0
    fRowBytes = rowBytes;
52
    // note: we do not change fPixels
53
54
    // conservative, since its possible the "new" settings are the same as the old.
55
0
    this->notifyPixelsChanged();
56
0
}
57
58
1.08M
void SkPixelRef::needsNewGenID() {
59
1.08M
    fTaggedGenID.store(0);
60
1.08M
    SkASSERT(!this->genIDIsUnique()); // This method isn't threadsafe, so the assert should be fine.
61
1.08M
}
62
63
449k
uint32_t SkPixelRef::getGenerationID() const {
64
449k
    uint32_t id = fTaggedGenID.load();
65
449k
    if (0 == id) {
66
210k
        uint32_t next = SkNextID::ImageID() | 1u;
67
210k
        if (fTaggedGenID.compare_exchange_strong(id, next)) {
68
210k
            id = next;  // There was no race or we won the race.  fTaggedGenID is next now.
69
210k
        } else {
70
            // We lost a race to set fTaggedGenID. compare_exchange() filled id with the winner.
71
0
        }
72
        // We can't quite SkASSERT(this->genIDIsUnique()). It could be non-unique
73
        // if we got here via the else path (pretty unlikely, but possible).
74
210k
    }
75
449k
    return id & ~1u;  // Mask off bottom unique bit.
76
449k
}
77
78
53.3k
void SkPixelRef::addGenIDChangeListener(sk_sp<SkIDChangeListener> listener) {
79
53.3k
    if (!listener || !this->genIDIsUnique()) {
80
        // No point in tracking this if we're not going to call it.
81
0
        return;
82
0
    }
83
53.3k
    SkASSERT(!listener->shouldDeregister());
84
53.3k
    fGenIDChangeListeners.add(std::move(listener));
85
53.3k
}
86
87
// we need to be called *before* the genID gets changed or zerod
88
1.08M
void SkPixelRef::callGenIDChangeListeners() {
89
    // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
90
1.08M
    if (this->genIDIsUnique()) {
91
210k
        fGenIDChangeListeners.changed();
92
210k
        if (fAddedToCache.exchange(false)) {
93
293
            SkNotifyBitmapGenIDIsStale(this->getGenerationID());
94
293
        }
95
879k
    } else {
96
        // Listeners get at most one shot, so even though these weren't triggered or not, blow them
97
        // away.
98
879k
        fGenIDChangeListeners.reset();
99
879k
    }
100
1.08M
}
101
102
657k
void SkPixelRef::notifyPixelsChanged() {
103
#ifdef SK_DEBUG
104
    if (this->isImmutable()) {
105
        SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
106
    }
107
#endif
108
657k
    this->callGenIDChangeListeners();
109
657k
    this->needsNewGenID();
110
657k
}
111
112
247k
void SkPixelRef::setImmutable() {
113
247k
    fMutability = kImmutable;
114
247k
}
115
116
8.96k
void SkPixelRef::setImmutableWithID(uint32_t genID) {
117
    /*
118
     *  We are forcing the genID to match an external value. The caller must ensure that this
119
     *  value does not conflict with other content.
120
     *
121
     *  One use is to force this pixelref's id to match an SkImage's id
122
     */
123
8.96k
    fMutability = kImmutable;
124
8.96k
    fTaggedGenID.store(genID);
125
8.96k
}
126
127
1.77k
void SkPixelRef::setTemporarilyImmutable() {
128
1.77k
    SkASSERT(fMutability != kImmutable);
129
1.77k
    fMutability = kTemporarilyImmutable;
130
1.77k
}
131
132
0
void SkPixelRef::restoreMutability() {
133
0
    SkASSERT(fMutability != kImmutable);
134
0
    fMutability = kMutable;
135
0
}
Unexecuted instantiation: SkPixelRef::restoreMutability()
Unexecuted instantiation: SkPixelRef::restoreMutability()
136
137
sk_sp<SkPixelRef> SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void* addr,
138
150k
                                         void (*releaseProc)(void* addr, void* ctx), void* ctx) {
139
150k
    SkASSERT(width >= 0 && height >= 0);
140
150k
    if (nullptr == releaseProc) {
141
73.6k
        return sk_make_sp<SkPixelRef>(width, height, addr, rowBytes);
142
73.6k
    }
143
76.6k
    struct PixelRef final : public SkPixelRef {
144
76.6k
        void (*fReleaseProc)(void*, void*);
145
76.6k
        void* fReleaseProcContext;
146
76.6k
        PixelRef(int w, int h, void* s, size_t r, void (*proc)(void*, void*), void* ctx)
147
76.6k
            : SkPixelRef(w, h, s, r), fReleaseProc(proc), fReleaseProcContext(ctx) {}
148
76.6k
        ~PixelRef() override { fReleaseProc(this->pixels(), fReleaseProcContext); }
149
76.6k
    };
150
76.6k
    return sk_sp<SkPixelRef>(new PixelRef(width, height, addr, rowBytes, releaseProc, ctx));
151
150k
}