Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPixelRef.cpp
Go to the documentation of this file.
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
9
13#include "src/core/SkNextID.h"
15
16#include <atomic>
17#include <utility>
18
20 // We never set the low bit.... see SkPixelRef::genIDIsUnique().
21 static std::atomic<uint32_t> nextID{2};
22
23 uint32_t id;
24 do {
25 id = nextID.fetch_add(2, std::memory_order_relaxed);
26 } while (id == 0);
27 return id;
28}
29
30///////////////////////////////////////////////////////////////////////////////
31
32SkPixelRef::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{
39 this->needsNewGenID();
40 fMutability = kMutable;
41}
42
44 this->callGenIDChangeListeners();
45}
46
47// This is undefined if there are clients in-flight trying to use us
48void SkPixelRef::android_only_reset(int width, int height, size_t rowBytes) {
49 fWidth = width;
50 fHeight = height;
51 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 this->notifyPixelsChanged();
56}
57
58void SkPixelRef::needsNewGenID() {
59 fTaggedGenID.store(0);
60 SkASSERT(!this->genIDIsUnique()); // This method isn't threadsafe, so the assert should be fine.
61}
62
64 uint32_t id = fTaggedGenID.load();
65 if (0 == id) {
66 uint32_t next = SkNextID::ImageID() | 1u;
67 if (fTaggedGenID.compare_exchange_strong(id, next)) {
68 id = next; // There was no race or we won the race. fTaggedGenID is next now.
69 } else {
70 // We lost a race to set fTaggedGenID. compare_exchange() filled id with the winner.
71 }
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 }
75 return id & ~1u; // Mask off bottom unique bit.
76}
77
79 if (!listener || !this->genIDIsUnique()) {
80 // No point in tracking this if we're not going to call it.
81 return;
82 }
83 SkASSERT(!listener->shouldDeregister());
84 fGenIDChangeListeners.add(std::move(listener));
85}
86
87// we need to be called *before* the genID gets changed or zerod
88void SkPixelRef::callGenIDChangeListeners() {
89 // We don't invalidate ourselves if we think another SkPixelRef is sharing our genID.
90 if (this->genIDIsUnique()) {
91 fGenIDChangeListeners.changed();
92 if (fAddedToCache.exchange(false)) {
94 }
95 } else {
96 // Listeners get at most one shot, so even though these weren't triggered or not, blow them
97 // away.
98 fGenIDChangeListeners.reset();
99 }
100}
101
103#ifdef SK_DEBUG
104 if (this->isImmutable()) {
105 SkDebugf("========== notifyPixelsChanged called on immutable pixelref");
106 }
107#endif
108 this->callGenIDChangeListeners();
109 this->needsNewGenID();
110}
111
113 fMutability = kImmutable;
114}
115
116void 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 fMutability = kImmutable;
124 fTaggedGenID.store(genID);
125}
126
127void SkPixelRef::setTemporarilyImmutable() {
128 SkASSERT(fMutability != kImmutable);
129 fMutability = kTemporarilyImmutable;
130}
131
132void SkPixelRef::restoreMutability() {
133 SkASSERT(fMutability != kImmutable);
134 fMutability = kMutable;
135}
136
137sk_sp<SkPixelRef> SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void* addr,
138 void (*releaseProc)(void* addr, void* ctx), void* ctx) {
139 SkASSERT(width >= 0 && height >= 0);
140 if (nullptr == releaseProc) {
141 return sk_make_sp<SkPixelRef>(width, height, addr, rowBytes);
142 }
143 struct PixelRef final : public SkPixelRef {
144 void (*fReleaseProc)(void*, void*);
145 void* fReleaseProcContext;
146 PixelRef(int w, int h, void* s, size_t r, void (*proc)(void*, void*), void* ctx)
147 : SkPixelRef(w, h, s, r), fReleaseProc(proc), fReleaseProcContext(ctx) {}
148 ~PixelRef() override { fReleaseProc(this->pixels(), fReleaseProcContext); }
149 };
150 return sk_sp<SkPixelRef>(new PixelRef(width, height, addr, rowBytes, releaseProc, ctx));
151}
static float next(float f)
#define SkASSERT(cond)
Definition SkAssert.h:116
void SkNotifyBitmapGenIDIsStale(uint32_t bitmapGenID)
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static void releaseProc(const void *ptr, void *context)
sk_sp< SkPixelRef > SkMakePixelRefWithProc(int width, int height, size_t rowBytes, void *addr, void(*releaseProc)(void *addr, void *ctx), void *ctx)
void reset() SK_EXCLUDES(fMutex)
void changed() SK_EXCLUDES(fMutex)
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
static uint32_t ImageID()
~SkPixelRef() override
void notifyPixelsChanged()
void * pixels() const
Definition SkPixelRef.h:36
uint32_t getGenerationID() const
void setImmutable()
bool isImmutable() const
Definition SkPixelRef.h:55
void addGenIDChangeListener(sk_sp< SkIDChangeListener > listener)
int width() const
Definition SkPixelRef.h:34
SkPixelRef(int width, int height, void *addr, size_t rowBytes)
void android_only_reset(int width, int height, size_t rowBytes)
int height() const
Definition SkPixelRef.h:35
size_t rowBytes() const
Definition SkPixelRef.h:37
struct MyStruct s
SkScalar w
SkScalar h
int32_t height
int32_t width
const uintptr_t id