Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c626a8ba authored by Eric Miao's avatar Eric Miao
Browse files

Add support to trace bitmap count/bytes when enabled

Bug: 357871776
Flag: NONE only when tracing and when tracing tag is enabled

Change-Id: I2f9cedd0d0498d600e9b849927ee280a242e239f
parent fddf3266
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -264,6 +264,7 @@ Bitmap::Bitmap(void* address, size_t size, const SkImageInfo& info, size_t rowBy
        , mPixelStorageType(PixelStorageType::Heap) {
    mPixelStorage.heap.address = address;
    mPixelStorage.heap.size = size;
    traceBitmapCreate();
}

Bitmap::Bitmap(SkPixelRef& pixelRef, const SkImageInfo& info)
@@ -272,6 +273,7 @@ Bitmap::Bitmap(SkPixelRef& pixelRef, const SkImageInfo& info)
        , mPixelStorageType(PixelStorageType::WrappedPixelRef) {
    pixelRef.ref();
    mPixelStorage.wrapped.pixelRef = &pixelRef;
    traceBitmapCreate();
}

Bitmap::Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes)
@@ -281,6 +283,7 @@ Bitmap::Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info
    mPixelStorage.ashmem.address = address;
    mPixelStorage.ashmem.fd = fd;
    mPixelStorage.ashmem.size = mappedSize;
    traceBitmapCreate();
}

#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
@@ -297,10 +300,12 @@ Bitmap::Bitmap(AHardwareBuffer* buffer, const SkImageInfo& info, size_t rowBytes
    setImmutable();  // HW bitmaps are always immutable
    mImage = SkImages::DeferredFromAHardwareBuffer(buffer, mInfo.alphaType(),
                                                   mInfo.refColorSpace());
    traceBitmapCreate();
}
#endif

Bitmap::~Bitmap() {
    traceBitmapDelete();
    switch (mPixelStorageType) {
        case PixelStorageType::WrappedPixelRef:
            mPixelStorage.wrapped.pixelRef->unref();
@@ -572,4 +577,28 @@ void Bitmap::setGainmap(sp<uirenderer::Gainmap>&& gainmap) {
    mGainmap = std::move(gainmap);
}

std::mutex Bitmap::mLock{};
size_t Bitmap::mTotalBitmapBytes = 0;
size_t Bitmap::mTotalBitmapCount = 0;

void Bitmap::traceBitmapCreate() {
    if (ATRACE_ENABLED()) {
        std::lock_guard lock{mLock};
        mTotalBitmapBytes += getAllocationByteCount();
        mTotalBitmapCount++;
        ATRACE_INT64("Bitmap Memory", mTotalBitmapBytes);
        ATRACE_INT64("Bitmap Count", mTotalBitmapCount);
    }
}

void Bitmap::traceBitmapDelete() {
    if (ATRACE_ENABLED()) {
        std::lock_guard lock{mLock};
        mTotalBitmapBytes -= getAllocationByteCount();
        mTotalBitmapCount--;
        ATRACE_INT64("Bitmap Memory", mTotalBitmapBytes);
        ATRACE_INT64("Bitmap Count", mTotalBitmapCount);
    }
}

}  // namespace android
+8 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <cutils/compiler.h>
#include <utils/StrongPointer.h>

#include <mutex>
#include <optional>

#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
@@ -227,6 +228,13 @@ private:
    } mPixelStorage;

    sk_sp<SkImage> mImage;  // Cache is used only for HW Bitmaps with Skia pipeline.

    // for tracing total number and memory usage of bitmaps
    static std::mutex mLock;
    static size_t mTotalBitmapBytes;
    static size_t mTotalBitmapCount;
    void traceBitmapCreate();
    void traceBitmapDelete();
};

}  // namespace android