diff --git a/libs/hwui/hwui/Bitmap.cpp b/libs/hwui/hwui/Bitmap.cpp index 929d16067f4c2..b11362698d18e 100644 --- a/libs/hwui/hwui/Bitmap.cpp +++ b/libs/hwui/hwui/Bitmap.cpp @@ -317,7 +317,17 @@ sk_sp Bitmap::createFrom(sp graphicBuffer) { } void Bitmap::setColorSpace(sk_sp colorSpace) { - reconfigure(info().makeColorSpace(std::move(colorSpace)), rowBytes(), sk_ref_sp(colorTable())); + mInfo = mInfo.makeColorSpace(std::move(colorSpace)); +} + +static SkImageInfo validateAlpha(const SkImageInfo& info) { + // Need to validate the alpha type to filter against the color type + // to prevent things like a non-opaque RGB565 bitmap + SkAlphaType alphaType; + LOG_ALWAYS_FATAL_IF(!SkColorTypeValidateAlphaType( + info.colorType(), info.alphaType(), &alphaType), + "Failed to validate alpha type!"); + return info.makeAlphaType(alphaType); } void Bitmap::reconfigure(const SkImageInfo& newInfo, size_t rowBytes, sk_sp ctable) { @@ -325,19 +335,14 @@ void Bitmap::reconfigure(const SkImageInfo& newInfo, size_t rowBytes, sk_spandroid_only_reset(newInfo.makeAlphaType(alphaType), rowBytes, std::move(ctable)); + this->android_only_reset(mInfo.width(), mInfo.height(), rowBytes, std::move(ctable)); } static sk_sp sanitize(const SkImageInfo& info, sk_sp ctable) { @@ -347,8 +352,11 @@ static sk_sp sanitize(const SkImageInfo& info, sk_sp } return nullptr; // drop the ctable if we're not indexed } -Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBytes, sk_sp ctable) - : SkPixelRef(info, address, rowBytes, sanitize(info, std::move(ctable))) +Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBytes, + sk_sp ctable) + : SkPixelRef(info.width(), info.height(), address, rowBytes, + sanitize(info, std::move(ctable))) + , mInfo(validateAlpha(info)) , mPixelStorageType(PixelStorageType::Heap) { mPixelStorage.heap.address = address; mPixelStorage.heap.size = size; @@ -356,7 +364,9 @@ Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBy Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc, const SkImageInfo& info, size_t rowBytes, sk_sp ctable) - : SkPixelRef(info, address, rowBytes, sanitize(info, std::move(ctable))) + : SkPixelRef(info.width(), info.height(), address, rowBytes, + sanitize(info, std::move(ctable))) + , mInfo(validateAlpha(info)) , mPixelStorageType(PixelStorageType::External) { mPixelStorage.external.address = address; mPixelStorage.external.context = context; @@ -365,7 +375,9 @@ Bitmap::Bitmap(void* address, void* context, FreeFunc freeFunc, Bitmap::Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes, sk_sp ctable) - : SkPixelRef(info, address, rowBytes, sanitize(info, std::move(ctable))) + : SkPixelRef(info.width(), info.height(), address, rowBytes, + sanitize(info, std::move(ctable))) + , mInfo(validateAlpha(info)) , mPixelStorageType(PixelStorageType::Ashmem) { mPixelStorage.ashmem.address = address; mPixelStorage.ashmem.fd = fd; @@ -373,9 +385,10 @@ Bitmap::Bitmap(void* address, int fd, size_t mappedSize, } Bitmap::Bitmap(GraphicBuffer* buffer, const SkImageInfo& info) - : SkPixelRef(info, nullptr, + : SkPixelRef(info.width(), info.height(), nullptr, bytesPerPixel(buffer->getPixelFormat()) * buffer->getStride(), nullptr) + , mInfo(validateAlpha(info)) , mPixelStorageType(PixelStorageType::Hardware) { mPixelStorage.hardware.buffer = buffer; buffer->incStrong(buffer); @@ -428,10 +441,6 @@ void* Bitmap::getStorage() const { } } -size_t Bitmap::getAllocatedSizeInBytes() const { - return info().getSafeSize(this->rowBytes()); -} - int Bitmap::getAshmemFd() const { switch (mPixelStorageType) { case PixelStorageType::Ashmem: @@ -459,7 +468,7 @@ void Bitmap::setAlphaType(SkAlphaType alphaType) { return; } - changeAlphaType(alphaType); + mInfo = mInfo.makeAlphaType(alphaType); } void Bitmap::getSkBitmap(SkBitmap* outBitmap) { @@ -470,19 +479,19 @@ void Bitmap::getSkBitmap(SkBitmap* outBitmap) { uirenderer::renderthread::RenderProxy::copyGraphicBufferInto(graphicBuffer(), outBitmap); return; } - outBitmap->setInfo(info(), rowBytes()); + outBitmap->setInfo(mInfo, rowBytes()); outBitmap->setPixelRef(sk_ref_sp(this), 0, 0); } void Bitmap::getSkBitmapForShaders(SkBitmap* outBitmap) { - outBitmap->setInfo(info(), rowBytes()); + outBitmap->setInfo(mInfo, rowBytes()); outBitmap->setPixelRef(sk_ref_sp(this), 0, 0); outBitmap->setHasHardwareMipMap(mHasHardwareMipMap); } void Bitmap::getBounds(SkRect* bounds) const { SkASSERT(bounds); - bounds->set(0, 0, SkIntToScalar(info().width()), SkIntToScalar(info().height())); + bounds->set(0, 0, SkIntToScalar(width()), SkIntToScalar(height())); } GraphicBuffer* Bitmap::graphicBuffer() { diff --git a/libs/hwui/hwui/Bitmap.h b/libs/hwui/hwui/Bitmap.h index 701b9db8d0b1b..9a767152ea4e4 100644 --- a/libs/hwui/hwui/Bitmap.h +++ b/libs/hwui/hwui/Bitmap.h @@ -67,11 +67,8 @@ public: Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes, sk_sp ctable); - int width() const { return info().width(); } - int height() const { return info().height(); } - int rowBytesAsPixels() const { - return rowBytes() >> info().shiftPerPixel(); + return rowBytes() >> SkColorTypeShiftPerPixel(mInfo.colorType()); } void reconfigure(const SkImageInfo& info, size_t rowBytes, sk_sp ctable); @@ -91,8 +88,12 @@ public: void setHasHardwareMipMap(bool hasMipMap); bool hasHardwareMipMap() const; - bool isOpaque() const {return info().isOpaque(); } - SkColorType colorType() const { return info().colorType(); } + bool isOpaque() const { return mInfo.isOpaque(); } + SkColorType colorType() const { return mInfo.colorType(); } + const SkImageInfo& info() const { + return mInfo; + } + void getBounds(SkRect* bounds) const; bool readyToDraw() const { @@ -104,13 +105,13 @@ public: } GraphicBuffer* graphicBuffer(); -protected: - virtual size_t getAllocatedSizeInBytes() const override; private: Bitmap(GraphicBuffer* buffer, const SkImageInfo& info); virtual ~Bitmap(); void* getStorage() const; + SkImageInfo mInfo; + const PixelStorageType mPixelStorageType; bool mHasHardwareMipMap = false; @@ -136,4 +137,4 @@ private: } mPixelStorage; }; -} //namespace android \ No newline at end of file +} //namespace android