protected:
~SkMallocPixelRef() override;
- size_t getAllocatedSizeInBytes() const override;
-
private:
// Uses alloc to implement NewAllocate or NewZeroed.
static sk_sp<SkPixelRef> MakeUsing(void*(*alloc)(size_t),
class SK_API SkPixelRef : public SkRefCnt {
public:
SkPixelRef(const SkImageInfo&, void* addr, size_t rowBytes, sk_sp<SkColorTable> = nullptr);
- ~SkPixelRef() override;
const SkImageInfo& info() const {
return fInfo;
}
+#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
+ // This is undefined if there are clients in-flight trying to use us
+ void android_only_reset(const SkImageInfo&, size_t rowBytes, sk_sp<SkColorTable>);
+#endif
+
+ /**
+ * Change the info's AlphaType. Note that this does not automatically
+ * invalidate the generation ID. If the pixel values themselves have
+ * changed, then you must explicitly call notifyPixelsChanged() as well.
+ */
+ void changeAlphaType(SkAlphaType at);
+
+ /**
+ * Returns the size (in bytes) of the internally allocated memory.
+ * This should be implemented in all serializable SkPixelRef derived classes.
+ * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value,
+ * otherwise the rendering code may attempt to read memory out of bounds.
+ *
+ * @return default impl returns 0.
+ */
+ virtual size_t getAllocatedSizeInBytes() const { return 0; }
+
+ SkPixelRef(int width, int height, void* addr, size_t rowBytes, sk_sp<SkColorTable> = nullptr);
+
+ ~SkPixelRef() override;
+
int width() const { return fInfo.width(); }
int height() const { return fInfo.height(); }
void* pixels() const { return fPixels; }
*/
void notifyPixelsChanged();
- /**
- * Change the info's AlphaType. Note that this does not automatically
- * invalidate the generation ID. If the pixel values themselves have
- * changed, then you must explicitly call notifyPixelsChanged() as well.
- */
- void changeAlphaType(SkAlphaType at);
-
/** 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.
*/
// default impl does nothing.
virtual void onNotifyPixelsChanged();
- /**
- * Returns the size (in bytes) of the internally allocated memory.
- * This should be implemented in all serializable SkPixelRef derived classes.
- * SkBitmap::fPixelRefOffset + SkBitmap::getSafeSize() should never overflow this value,
- * otherwise the rendering code may attempt to read memory out of bounds.
- *
- * @return default impl returns 0.
- */
- virtual size_t getAllocatedSizeInBytes() const;
-
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
// This is undefined if there are clients in-flight trying to use us
- void android_only_reset(const SkImageInfo&, size_t rowBytes, sk_sp<SkColorTable>);
+ void android_only_reset(int width, int height, size_t rowBytes, sk_sp<SkColorTable>);
#endif
private:
- // mostly const. fInfo.fAlpahType can be changed at runtime.
- const SkImageInfo fInfo;
+ // TODO (msarett): After we remove legacy APIs, we should replace |fInfo| with just a width
+ // and height.
+ const SkImageInfo fInfo;
sk_sp<SkColorTable> fCTable;
- void* fPixels;
- size_t fRowBytes;
+ void* fPixels;
+ size_t fRowBytes;
// Bottom bit indicates the Gen ID is unique.
bool genIDIsUnique() const { return SkToBool(fTaggedGenID.load() & 1); }
#ifdef SK_DEBUG
if (pr) {
if (kUnknown_SkColorType != fInfo.colorType()) {
- const SkImageInfo& prInfo = pr->info();
- SkASSERT(fInfo.width() <= prInfo.width());
- SkASSERT(fInfo.height() <= prInfo.height());
- SkASSERT(fInfo.colorType() == prInfo.colorType());
- switch (prInfo.alphaType()) {
- case kUnknown_SkAlphaType:
- SkASSERT(fInfo.alphaType() == kUnknown_SkAlphaType);
- break;
- case kOpaque_SkAlphaType:
- case kPremul_SkAlphaType:
- SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
- fInfo.alphaType() == kPremul_SkAlphaType);
- break;
- case kUnpremul_SkAlphaType:
- SkASSERT(fInfo.alphaType() == kOpaque_SkAlphaType ||
- fInfo.alphaType() == kUnpremul_SkAlphaType);
- break;
- }
+ SkASSERT(fInfo.width() + dx <= pr->width());
+ SkASSERT(fInfo.height() + dy <= pr->height());
}
}
#endif
fPixelRef = std::move(pr);
if (fPixelRef) {
- const SkImageInfo& info = fPixelRef->info();
- fPixelRefOrigin.set(SkTPin(dx, 0, info.width()), SkTPin(dy, 0, info.height()));
+ fPixelRefOrigin.set(SkTPin(dx, 0, fPixelRef->width()), SkTPin(dy, 0, fPixelRef->height()));
this->updatePixelsFromRef();
} else {
// ignore dx,dy if there is no pixelref
if (!pr) {
return false;
}
- bitmap->setInfo(pr->info());
+ bitmap->setInfo(info);
bitmap->setPixelRef(std::move(pr), 0, 0);
return true;
}
SkASSERT(fPixelRef->rowBytes() == fRowBytes);
SkASSERT(fPixelRefOrigin.fX >= 0);
SkASSERT(fPixelRefOrigin.fY >= 0);
- SkASSERT(fPixelRef->info().width() >= (int)this->width() + fPixelRefOrigin.fX);
- SkASSERT(fPixelRef->info().height() >= (int)this->height() + fPixelRefOrigin.fY);
+ SkASSERT(fPixelRef->width() >= (int)this->width() + fPixelRefOrigin.fX);
+ SkASSERT(fPixelRef->height() >= (int)this->height() + fPixelRefOrigin.fY);
SkASSERT(fPixelRef->rowBytes() >= fInfo.minRowBytes());
}
}
fReleaseProc(this->pixels(), fReleaseProcContext);
}
}
-
-size_t SkMallocPixelRef::getAllocatedSizeInBytes() const {
- return this->info().getSafeSize(this->rowBytes());
-}
///////////////////////////////////////////////////////////////////////////////
+#ifdef SK_TRACE_PIXELREF_LIFETIME
+ static int32_t gInstCounter;
+#endif
+
static SkImageInfo validate_info(const SkImageInfo& info) {
SkAlphaType newAlphaType = info.alphaType();
SkAssertResult(SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAlphaType));
}
}
-#ifdef SK_TRACE_PIXELREF_LIFETIME
- static int32_t gInstCounter;
-#endif
-
SkPixelRef::SkPixelRef(const SkImageInfo& info, void* pixels, size_t rowBytes,
sk_sp<SkColorTable> ctable)
: fInfo(validate_info(info))
fAddedToCache.store(false);
}
+SkPixelRef::SkPixelRef(int width, int height, void* pixels, size_t rowBytes,
+ sk_sp<SkColorTable> ctable)
+ : fInfo(SkImageInfo::MakeUnknown(width, height))
+ , fCTable(std::move(ctable))
+ , fPixels(pixels)
+ , fRowBytes(rowBytes)
+#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
+ , fStableID(SkNextID::ImageID())
+#endif
+{
+#ifdef SK_TRACE_PIXELREF_LIFETIME
+ SkDebugf(" pixelref %d\n", sk_atomic_inc(&gInstCounter));
+#endif
+
+ this->needsNewGenID();
+ fMutability = kMutable;
+ fAddedToCache.store(false);
+}
+
SkPixelRef::~SkPixelRef() {
#ifdef SK_TRACE_PIXELREF_LIFETIME
SkDebugf("~pixelref %d\n", sk_atomic_dec(&gInstCounter) - 1);
}
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
+
// This is undefined if there are clients in-flight trying to use us
void SkPixelRef::android_only_reset(const SkImageInfo& info, size_t rowBytes,
sk_sp<SkColorTable> ctable) {
- validate_pixels_ctable(info, ctable.get());
-
*const_cast<SkImageInfo*>(&fInfo) = info;
fRowBytes = rowBytes;
fCTable = std::move(ctable);
// conservative, since its possible the "new" settings are the same as the old.
this->notifyPixelsChanged();
}
+
+// This is undefined if there are clients in-flight trying to use us
+void SkPixelRef::android_only_reset(int width, int height, size_t rowBytes,
+ sk_sp<SkColorTable> ctable) {
+ *const_cast<SkImageInfo*>(&fInfo) = fInfo.makeWH(width, height);
+ fRowBytes = rowBytes;
+ fCTable = std::move(ctable);
+ // note: we do not change fPixels
+
+ // conservative, since its possible the "new" settings are the same as the old.
+ this->notifyPixelsChanged();
+}
+
#endif
void SkPixelRef::needsNewGenID() {
}
void SkPixelRef::onNotifyPixelsChanged() { }
-
-size_t SkPixelRef::getAllocatedSizeInBytes() const {
- return 0;
-}