* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkPixelRef.h"
#include "include/private/SkMutex.h"
#include "src/core/SkBitmapCache.h"
#include "src/core/SkNextID.h"
#include "src/core/SkPixelRefPriv.h"
#include "src/core/SkTraceEvent.h"
#include <atomic>
uint32_t SkNextID::ImageID() {
static std::atomic<uint32_t> nextID{2};
uint32_t id;
do {
id = nextID.fetch_add(2);
} while (id == 0);
return id;
}
SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes)
: fWidth(width)
, fHeight(height)
, fPixels(pixels)
, fRowBytes(rowBytes)
, fAddedToCache(false)
{
this->needsNewGenID();
fMutability = kMutable;
}
SkPixelRef::~SkPixelRef() {
this->callGenIDChangeListeners();
}
void SkPixelRef::android_only_reset(int width, int height, size_t rowBytes) {
fWidth = width;
fHeight = height;
fRowBytes = rowBytes;
this->notifyPixelsChanged();
}
void SkPixelRef::needsNewGenID() {
fTaggedGenID.store(0);
SkASSERT(!this->genIDIsUnique());
}
uint32_t SkPixelRef::getGenerationID() const {
uint32_t id = fTaggedGenID.load();
if (0 == id) {
uint32_t next = SkNextID::ImageID() | 1u;
if (fTaggedGenID.compare_exchange_strong(id, next)) {
id = next;
} else {
}
}
return id & ~1u;
}
void SkPixelRef::addGenIDChangeListener(GenIDChangeListener* listener) {
if (nullptr == listener || !this->genIDIsUnique()) {
delete listener;
return;
}
SkAutoMutexExclusive lock(fGenIDChangeListenersMutex);
*fGenIDChangeListeners.append() = listener;
}
void SkPixelRef::callGenIDChangeListeners() {
SkAutoMutexExclusive lock(fGenIDChangeListenersMutex);
if (this->genIDIsUnique()) {
for (int i = 0; i < fGenIDChangeListeners.count(); i++) {
fGenIDChangeListeners[i]->onChange();
}
if (fAddedToCache.exchange(false)) {
SkNotifyBitmapGenIDIsStale(this->getGenerationID());
}
}
fGenIDChangeListeners.deleteAll();
}
void SkPixelRef::notifyPixelsChanged() {
#ifdef SK_DEBUG
if (this->isImmutable()) {
SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
}
#endif
this->callGenIDChangeListeners();
this->needsNewGenID();
}
void SkPixelRef::setImmutable() {
fMutability = kImmutable;
}
void SkPixelRef::setImmutableWithID(uint32_t genID) {
* We are forcing the genID to match an external value. The caller must ensure that this
* value does not conflict with other content.
*
* One use is to force this pixelref's id to match an SkImage's id
*/
fMutability = kImmutable;
fTaggedGenID.store(genID);
}
void SkPixelRef::setTemporarilyImmutable() {
SkASSERT(fMutability != kImmutable);
fMutability = kTemporarilyImmutable;
}
void SkPixelRef::restoreMutability() {
SkASSERT(fMutability != kImmutable);
fMutability = kMutable;
}
sk_sp<SkPixelRef> SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void* addr,
void (*releaseProc)(void* addr, void* ctx), void* ctx) {
SkASSERT(width >= 0 && height >= 0);
if (nullptr == releaseProc) {
return sk_make_sp<SkPixelRef>(width, height, addr, rowBytes);
}
struct PixelRef final : public SkPixelRef {
void (*fReleaseProc)(void*, void*);
void* fReleaseProcContext;
PixelRef(int w, int h, void* s, size_t r, void (*proc)(void*, void*), void* ctx)
: SkPixelRef(w, h, s, r), fReleaseProc(proc), fReleaseProcContext(ctx) {}
~PixelRef() override { fReleaseProc(this->pixels(), fReleaseProcContext); }
};
return sk_sp<SkPixelRef>(new PixelRef(width, height, addr, rowBytes, releaseProc, ctx));
}