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

Commit 4b4bbe7b authored by Andrew Wolfers's avatar Andrew Wolfers
Browse files

Use BGRA format for sprite

This change converts the SpriteController pixel format from RGBA
to BGRA, and changes the bitmap icon format similarly. This
conversion affects the format of the buffer that is allocated
for the SpriteController's surface, but does not result in any
user-facing changes to the appearance of the sprite. The BGRA
format is required for the cursor to be presented on the cursor
plane (instead of e.g. an overlay plane.)

To support the BGRA format, this change also introduces
ANDROID_BITMAP_FORMAT_BGRA_8888 as an anonymous enum member,
and cases are added where appropriate. It is not directly
added as a member of AndroidBitmapFormat because it is only
inteded for internal usage, and doing so in this manner prevents
it from being exposed in the Android API.

Bug: b/371003205
Flag: com.android.graphics.libgui.flags.cursor_plane_compatibility
(cherry picked from https://android-review.googlesource.com/q/commit:a22a92b690324a6d7abb52d60130e37454426940)

Change-Id: I58911d7ab40ff0637efb47a7115c84eb03a2d824
parent d6ed8077
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ void ABitmap_releaseRef(ABitmap* bitmap) {
    SkSafeUnref(TypeCast::toBitmap(bitmap));
}

static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
static uint32_t getFormat(const SkImageInfo& info) {
    switch (info.colorType()) {
        case kN32_SkColorType:
            return ANDROID_BITMAP_FORMAT_RGBA_8888;
@@ -62,12 +62,14 @@ static AndroidBitmapFormat getFormat(const SkImageInfo& info) {
            return ANDROID_BITMAP_FORMAT_RGBA_F16;
        case kRGBA_1010102_SkColorType:
            return ANDROID_BITMAP_FORMAT_RGBA_1010102;
        case kBGRA_8888_SkColorType:
            return ANDROID_BITMAP_FORMAT_BGRA_8888;
        default:
            return ANDROID_BITMAP_FORMAT_NONE;
    }
}

static SkColorType getColorType(AndroidBitmapFormat format) {
static SkColorType getColorType(uint32_t format) {
    switch (format) {
        case ANDROID_BITMAP_FORMAT_RGBA_8888:
            return kN32_SkColorType;
@@ -81,6 +83,8 @@ static SkColorType getColorType(AndroidBitmapFormat format) {
            return kRGBA_F16_SkColorType;
        case ANDROID_BITMAP_FORMAT_RGBA_1010102:
            return kRGBA_1010102_SkColorType;
        case ANDROID_BITMAP_FORMAT_BGRA_8888:
            return kBGRA_8888_SkColorType;
        default:
            return kUnknown_SkColorType;
    }
@@ -108,7 +112,7 @@ static uint32_t getInfoFlags(const SkImageInfo& info, bool isHardware) {
    return flags;
}

ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, AndroidBitmapFormat dstFormat) {
ABitmap* ABitmap_copy(ABitmap* srcBitmapHandle, uint32_t dstFormat) {
    SkColorType dstColorType = getColorType(dstFormat);
    if (srcBitmapHandle && dstColorType != kUnknown_SkColorType) {
        SkBitmap srcBitmap;
+7 −4
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ ANDROID_API AndroidBitmapInfo ABitmap_getInfoFromJava(JNIEnv* env, jobject bitma
 */
ANDROID_API ABitmap* ABitmap_acquireBitmapFromJava(JNIEnv* env, jobject bitmapObj);

ANDROID_API ABitmap* ABitmap_copy(ABitmap* srcBitmap, AndroidBitmapFormat dstFormat);
ANDROID_API ABitmap* ABitmap_copy(ABitmap* srcBitmap, uint32_t dstFormat);

ANDROID_API void ABitmap_acquireRef(ABitmap* bitmap);
ANDROID_API void ABitmap_releaseRef(ABitmap* bitmap);
@@ -88,6 +88,11 @@ ANDROID_API AHardwareBuffer* ABitmap_getHardwareBuffer(ABitmap* bitmap);
__END_DECLS

#ifdef	__cplusplus
// Internal extension to AndroidBitmapFormat.
enum {
    ANDROID_BITMAP_FORMAT_BGRA_8888 = 5,
};

namespace android {
namespace graphics {
    class Bitmap {
@@ -118,9 +123,7 @@ namespace graphics {
            return *this;
        }

        Bitmap copy(AndroidBitmapFormat dstFormat) const {
            return Bitmap(ABitmap_copy(mBitmap, dstFormat));
        }
        Bitmap copy(uint32_t dstFormat) const { return Bitmap(ABitmap_copy(mBitmap, dstFormat)); }

        bool isValid() const { return mBitmap != nullptr; }
        bool isEmpty() const {
+9 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <android/native_window.h>
#include <ui/ColorSpace.h>
#include <utils/Log.h>
#include <vndk/hardware_buffer.h>

#include <algorithm>
#include <cmath>
@@ -63,6 +64,10 @@ static inline SkImageInfo createImageInfo(int32_t width, int32_t height, int32_t
            colorType = kAlpha_8_SkColorType;
            alphaType = kPremul_SkAlphaType;
            break;
        case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM:
            colorType = kBGRA_8888_SkColorType;
            alphaType = kPremul_SkAlphaType;
            break;
        default:
            ALOGV("Unsupported format: %d, return unknown by default", format);
            break;
@@ -100,6 +105,8 @@ uint32_t ColorTypeToBufferFormat(SkColorType colorType) {
            return kRGBA4444;
        case kAlpha_8_SkColorType:
              return AHARDWAREBUFFER_FORMAT_R8_UNORM;
        case kBGRA_8888_SkColorType:
            return AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM;
        default:
            ALOGV("Unsupported colorType: %d, return RGBA_8888 by default", (int)colorType);
            return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
@@ -122,6 +129,8 @@ SkColorType BufferFormatToColorType(uint32_t format) {
            return kRGBA_F16_SkColorType;
        case AHARDWAREBUFFER_FORMAT_R8_UNORM:
            return kAlpha_8_SkColorType;
        case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM:
            return kBGRA_8888_SkColorType;
        default:
            ALOGV("Unsupported format: %d, return unknown by default", format);
            return kUnknown_SkColorType;
+18 −2
Original line number Diff line number Diff line
@@ -356,7 +356,7 @@ sp<SurfaceControl> SpriteController::obtainSurface(int32_t width, int32_t height
    }
    const sp<SurfaceControl> surfaceControl =
            mSurfaceComposerClient->createSurface(String8("Sprite"), width, height,
                                                  PIXEL_FORMAT_RGBA_8888, createFlags,
                                                  getPixelFormat(), createFlags,
                                                  parent ? parent->getHandle() : nullptr);
    if (surfaceControl == nullptr || !surfaceControl->isValid()) {
        ALOGE("Error creating sprite surface.");
@@ -403,7 +403,7 @@ void SpriteController::SpriteImpl::setIcon(const SpriteIcon& icon) {

    uint32_t dirty;
    if (icon.isValid()) {
        mLocked.state.icon.bitmap = icon.bitmap.copy(ANDROID_BITMAP_FORMAT_RGBA_8888);
        mLocked.state.icon.bitmap = icon.bitmap.copy(getBitmapFormat());
        if (!mLocked.state.icon.isValid() || mLocked.state.icon.hotSpotX != icon.hotSpotX ||
            mLocked.state.icon.hotSpotY != icon.hotSpotY ||
            mLocked.state.icon.drawNativeDropShadow != icon.drawNativeDropShadow) {
@@ -503,4 +503,20 @@ void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
    }
}

uint32_t SpriteController::getBitmapFormat() {
    if (Surface::IsCursorPlaneCompatibilitySupported()) {
        return ANDROID_BITMAP_FORMAT_BGRA_8888;
    }

    return ANDROID_BITMAP_FORMAT_RGBA_8888;
}

PixelFormat SpriteController::getPixelFormat() {
    if (Surface::IsCursorPlaneCompatibilitySupported()) {
        return PIXEL_FORMAT_BGRA_8888;
    }

    return PIXEL_FORMAT_RGBA_8888;
}

} // namespace android
+3 −0
Original line number Diff line number Diff line
@@ -136,6 +136,9 @@ public:
    virtual void openTransaction();
    virtual void closeTransaction();

    static uint32_t getBitmapFormat();
    static PixelFormat getPixelFormat();

private:
    class Handler : public virtual android::MessageHandler {
    public:
Loading