2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2008 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkPixelRef_DEFINED
|
|
|
|
#define SkPixelRef_DEFINED
|
|
|
|
|
2016-02-18 16:53:33 +00:00
|
|
|
#include "../private/SkMutex.h"
|
|
|
|
#include "../private/SkTDArray.h"
|
2011-12-02 19:11:17 +00:00
|
|
|
#include "SkBitmap.h"
|
2015-05-22 15:06:21 +00:00
|
|
|
#include "SkFilterQuality.h"
|
2015-02-24 22:38:12 +00:00
|
|
|
#include "SkImageInfo.h"
|
2015-05-22 15:06:21 +00:00
|
|
|
#include "SkPixmap.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRefCnt.h"
|
2014-07-21 18:37:39 +00:00
|
|
|
#include "SkSize.h"
|
2015-02-24 22:38:12 +00:00
|
|
|
#include "SkString.h"
|
2018-08-09 16:23:19 +00:00
|
|
|
#include <atomic>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-01 19:05:36 +00:00
|
|
|
struct SkIRect;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-06-26 19:18:23 +00:00
|
|
|
class GrTexture;
|
2015-08-19 19:25:40 +00:00
|
|
|
class SkDiscardableMemory;
|
2010-09-09 16:01:26 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkPixelRef
|
|
|
|
|
2017-04-13 20:02:22 +00:00
|
|
|
This class is the smart container for pixel memory, and is used with SkBitmap.
|
2008-12-17 15:59:43 +00:00
|
|
|
This class can be shared/accessed between multiple threads.
|
|
|
|
*/
|
2014-07-14 16:21:31 +00:00
|
|
|
class SK_API SkPixelRef : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2017-07-18 14:53:11 +00:00
|
|
|
SkPixelRef(int width, int height, void* addr, size_t rowBytes);
|
2017-05-01 14:22:31 +00:00
|
|
|
~SkPixelRef() override;
|
|
|
|
|
2017-05-02 15:41:30 +00:00
|
|
|
int width() const { return fWidth; }
|
|
|
|
int height() const { return fHeight; }
|
2017-04-17 15:02:51 +00:00
|
|
|
void* pixels() const { return fPixels; }
|
|
|
|
size_t rowBytes() const { return fRowBytes; }
|
2014-01-08 15:42:01 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns a non-zero, unique value corresponding to the pixels in this
|
|
|
|
pixelref. Each time the pixels are changed (and notifyPixelsChanged is
|
|
|
|
called), a different generation ID will be returned.
|
|
|
|
*/
|
|
|
|
uint32_t getGenerationID() const;
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2014-11-25 21:42:12 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
|
|
|
|
/** Returns a non-zero, unique value corresponding to this SkPixelRef.
|
|
|
|
Unlike the generation ID, this ID remains the same even when the pixels
|
|
|
|
are changed. IDs are not reused (until uint32_t wraps), so it is safe
|
|
|
|
to consider this ID unique even after this SkPixelRef is deleted.
|
|
|
|
|
|
|
|
Can be used as a key which uniquely identifies this SkPixelRef
|
|
|
|
regardless of changes to its pixels or deletion of this object.
|
|
|
|
*/
|
|
|
|
uint32_t getStableID() const { return fStableID; }
|
|
|
|
#endif
|
|
|
|
|
2014-01-27 15:41:07 +00:00
|
|
|
/**
|
|
|
|
* Call this if you have changed the contents of the pixels. This will in-
|
|
|
|
* turn cause a different generation ID value to be returned from
|
|
|
|
* getGenerationID().
|
|
|
|
*/
|
2014-01-28 16:05:39 +00:00
|
|
|
void notifyPixelsChanged();
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns true if this pixelref is marked as immutable, meaning that the
|
|
|
|
contents of its pixels will not change for the lifetime of the pixelref.
|
|
|
|
*/
|
2015-07-29 18:44:52 +00:00
|
|
|
bool isImmutable() const { return fMutability != kMutable; }
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Marks this pixelref is immutable, meaning that the contents of its
|
|
|
|
pixels will not change for the lifetime of the pixelref. This state can
|
|
|
|
be set on a pixelref, but it cannot be cleared once it is set.
|
|
|
|
*/
|
|
|
|
void setImmutable();
|
|
|
|
|
2013-10-24 17:44:27 +00:00
|
|
|
// Register a listener that may be called the next time our generation ID changes.
|
|
|
|
//
|
|
|
|
// We'll only call the listener if we're confident that we are the only SkPixelRef with this
|
|
|
|
// generation ID. If our generation ID changes and we decide not to call the listener, we'll
|
|
|
|
// never call it: you must add a new listener for each generation ID change. We also won't call
|
|
|
|
// the listener when we're certain no one knows what our generation ID is.
|
|
|
|
//
|
|
|
|
// This can be used to invalidate caches keyed by SkPixelRef generation ID.
|
|
|
|
struct GenIDChangeListener {
|
|
|
|
virtual ~GenIDChangeListener() {}
|
|
|
|
virtual void onChange() = 0;
|
|
|
|
};
|
|
|
|
|
2018-09-17 21:29:39 +00:00
|
|
|
// Takes ownership of listener. Threadsafe.
|
2013-10-24 17:44:27 +00:00
|
|
|
void addGenIDChangeListener(GenIDChangeListener* listener);
|
|
|
|
|
2015-02-25 15:17:11 +00:00
|
|
|
// Call when this pixelref is part of the key to a resourcecache entry. This allows the cache
|
|
|
|
// to know automatically those entries can be purged when this pixelref is changed or deleted.
|
|
|
|
void notifyAddedToCache() {
|
2015-02-25 16:27:41 +00:00
|
|
|
fAddedToCache.store(true);
|
2015-02-25 15:17:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 14:34:05 +00:00
|
|
|
virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
|
2015-08-19 19:25:40 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2017-07-18 14:53:11 +00:00
|
|
|
void android_only_reset(int width, int height, size_t rowBytes);
|
2017-04-12 16:57:07 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2017-05-02 15:41:30 +00:00
|
|
|
int fWidth;
|
|
|
|
int fHeight;
|
2017-05-01 14:22:31 +00:00
|
|
|
void* fPixels;
|
|
|
|
size_t fRowBytes;
|
2015-05-22 15:06:21 +00:00
|
|
|
|
2015-02-25 17:10:57 +00:00
|
|
|
// Bottom bit indicates the Gen ID is unique.
|
|
|
|
bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
|
2018-08-09 16:23:19 +00:00
|
|
|
mutable std::atomic<uint32_t> fTaggedGenID;
|
2015-02-25 17:10:57 +00:00
|
|
|
|
2014-11-25 21:42:12 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
|
|
|
|
const uint32_t fStableID;
|
|
|
|
#endif
|
2011-02-24 18:09:46 +00:00
|
|
|
|
2018-09-17 21:29:39 +00:00
|
|
|
SkMutex fGenIDChangeListenersMutex;
|
|
|
|
SkTDArray<GenIDChangeListener*> fGenIDChangeListeners; // pointers are owned
|
2012-08-22 15:00:05 +00:00
|
|
|
|
2015-02-25 17:10:57 +00:00
|
|
|
// Set true by caches when they cache content that's derived from the current pixels.
|
2018-08-09 16:23:19 +00:00
|
|
|
std::atomic<bool> fAddedToCache;
|
2015-07-29 18:44:52 +00:00
|
|
|
|
2018-04-26 12:32:37 +00:00
|
|
|
enum Mutability {
|
2015-07-29 18:44:52 +00:00
|
|
|
kMutable, // PixelRefs begin mutable.
|
|
|
|
kTemporarilyImmutable, // Considered immutable, but can revert to mutable.
|
|
|
|
kImmutable, // Once set to this state, it never leaves.
|
|
|
|
} fMutability : 8; // easily fits inside a byte
|
|
|
|
|
2013-10-24 17:44:27 +00:00
|
|
|
void needsNewGenID();
|
|
|
|
void callGenIDChangeListeners();
|
|
|
|
|
2015-07-29 18:44:52 +00:00
|
|
|
void setTemporarilyImmutable();
|
|
|
|
void restoreMutability();
|
|
|
|
friend class SkSurface_Raster; // For the two methods above.
|
|
|
|
|
2015-07-31 03:13:43 +00:00
|
|
|
friend class SkImage_Raster;
|
2016-02-04 18:52:42 +00:00
|
|
|
friend class SkSpecialImage_Raster;
|
2015-07-31 03:13:43 +00:00
|
|
|
|
2015-08-04 15:10:13 +00:00
|
|
|
void setImmutableWithID(uint32_t genID);
|
|
|
|
friend class SkImage_Gpu;
|
2017-04-26 20:20:28 +00:00
|
|
|
friend class SkImage_Lazy;
|
2016-04-08 19:10:42 +00:00
|
|
|
friend class SkSpecialImage_Gpu;
|
2017-04-11 16:03:44 +00:00
|
|
|
friend void SkBitmapCache_setImmutableWithID(SkPixelRef*, uint32_t);
|
2015-08-04 15:10:13 +00:00
|
|
|
|
2014-07-14 16:21:31 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|