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

Commit 30c6ac31 authored by Arthur Hung's avatar Arthur Hung Committed by Android (Google) Code Review
Browse files

Merge "Move drag event to InputDispatcher (1/n)" into sc-dev

parents 36e94b41 7632c339
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -166,6 +166,9 @@ enum {


    /** Capture event */
    /** Capture event */
    AINPUT_EVENT_TYPE_CAPTURE = 4,
    AINPUT_EVENT_TYPE_CAPTURE = 4,

    /** Drag event */
    AINPUT_EVENT_TYPE_DRAG = 5,
};
};


/**
/**
+29 −0
Original line number Original line Diff line number Diff line
@@ -792,6 +792,30 @@ protected:
    bool mPointerCaptureEnabled;
    bool mPointerCaptureEnabled;
};
};


/*
 * Drag events.
 */
class DragEvent : public InputEvent {
public:
    virtual ~DragEvent() {}

    virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_DRAG; }

    inline bool isExiting() const { return mIsExiting; }

    inline float getX() const { return mX; }

    inline float getY() const { return mY; }

    void initialize(int32_t id, float x, float y, bool isExiting);

    void initialize(const DragEvent& from);

protected:
    bool mIsExiting;
    float mX, mY;
};

/**
/**
 * Base class for verified events.
 * Base class for verified events.
 * Do not create a VerifiedInputEvent explicitly.
 * Do not create a VerifiedInputEvent explicitly.
@@ -855,6 +879,7 @@ public:
    virtual MotionEvent* createMotionEvent() = 0;
    virtual MotionEvent* createMotionEvent() = 0;
    virtual FocusEvent* createFocusEvent() = 0;
    virtual FocusEvent* createFocusEvent() = 0;
    virtual CaptureEvent* createCaptureEvent() = 0;
    virtual CaptureEvent* createCaptureEvent() = 0;
    virtual DragEvent* createDragEvent() = 0;
};
};


/*
/*
@@ -870,12 +895,14 @@ public:
    virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
    virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
    virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
    virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
    virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
    virtual CaptureEvent* createCaptureEvent() override { return &mCaptureEvent; }
    virtual DragEvent* createDragEvent() override { return &mDragEvent; }


private:
private:
    KeyEvent mKeyEvent;
    KeyEvent mKeyEvent;
    MotionEvent mMotionEvent;
    MotionEvent mMotionEvent;
    FocusEvent mFocusEvent;
    FocusEvent mFocusEvent;
    CaptureEvent mCaptureEvent;
    CaptureEvent mCaptureEvent;
    DragEvent mDragEvent;
};
};


/*
/*
@@ -890,6 +917,7 @@ public:
    virtual MotionEvent* createMotionEvent() override;
    virtual MotionEvent* createMotionEvent() override;
    virtual FocusEvent* createFocusEvent() override;
    virtual FocusEvent* createFocusEvent() override;
    virtual CaptureEvent* createCaptureEvent() override;
    virtual CaptureEvent* createCaptureEvent() override;
    virtual DragEvent* createDragEvent() override;


    void recycle(InputEvent* event);
    void recycle(InputEvent* event);


@@ -900,6 +928,7 @@ private:
    std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
    std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
    std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
    std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
    std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
    std::queue<std::unique_ptr<CaptureEvent>> mCaptureEventPool;
    std::queue<std::unique_ptr<DragEvent>> mDragEventPool;
};
};


} // namespace android
} // namespace android
+21 −0
Original line number Original line Diff line number Diff line
@@ -69,6 +69,7 @@ struct InputMessage {
        FINISHED,
        FINISHED,
        FOCUS,
        FOCUS,
        CAPTURE,
        CAPTURE,
        DRAG,
    };
    };


    struct Header {
    struct Header {
@@ -183,6 +184,16 @@ struct InputMessage {


            inline size_t size() const { return sizeof(Capture); }
            inline size_t size() const { return sizeof(Capture); }
        } capture;
        } capture;

        struct Drag {
            int32_t eventId;
            float x;
            float y;
            bool isExiting;
            uint8_t empty[3];

            inline size_t size() const { return sizeof(Drag); }
        } drag;
    } __attribute__((aligned(8))) body;
    } __attribute__((aligned(8))) body;


    bool isValid(size_t actualSize) const;
    bool isValid(size_t actualSize) const;
@@ -354,6 +365,15 @@ public:
     */
     */
    status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled);
    status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled);


    /* Publishes a drag event to the input channel.
     *
     * Returns OK on success.
     * Returns WOULD_BLOCK if the channel is full.
     * Returns DEAD_OBJECT if the channel's peer has been closed.
     * Other errors probably indicate that the channel is broken.
     */
    status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting);

    /* Receives the finished signal from the consumer in reply to the original dispatch signal.
    /* Receives the finished signal from the consumer in reply to the original dispatch signal.
     * If a signal was received, returns the message sequence number,
     * If a signal was received, returns the message sequence number,
     * whether the consumer handled the message, and the time the event was first read by the
     * whether the consumer handled the message, and the time the event was first read by the
@@ -601,6 +621,7 @@ private:
    static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
    static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
    static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
    static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
    static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg);
    static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg);
    static void initializeDragEvent(DragEvent* event, const InputMessage* msg);
    static void addSample(MotionEvent* event, const InputMessage* msg);
    static void addSample(MotionEvent* event, const InputMessage* msg);
    static bool canAddSample(const Batch& batch, const InputMessage* msg);
    static bool canAddSample(const Batch& batch, const InputMessage* msg);
    static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
    static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
+35 −0
Original line number Original line Diff line number Diff line
@@ -92,6 +92,9 @@ const char* inputEventTypeToString(int32_t type) {
        case AINPUT_EVENT_TYPE_CAPTURE: {
        case AINPUT_EVENT_TYPE_CAPTURE: {
            return "CAPTURE";
            return "CAPTURE";
        }
        }
        case AINPUT_EVENT_TYPE_DRAG: {
            return "DRAG";
        }
    }
    }
    return "UNKNOWN";
    return "UNKNOWN";
}
}
@@ -770,6 +773,23 @@ void CaptureEvent::initialize(const CaptureEvent& from) {
    mPointerCaptureEnabled = from.mPointerCaptureEnabled;
    mPointerCaptureEnabled = from.mPointerCaptureEnabled;
}
}


// --- DragEvent ---

void DragEvent::initialize(int32_t id, float x, float y, bool isExiting) {
    InputEvent::initialize(id, ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID, AINPUT_SOURCE_UNKNOWN,
                           ADISPLAY_ID_NONE, INVALID_HMAC);
    mIsExiting = isExiting;
    mX = x;
    mY = y;
}

void DragEvent::initialize(const DragEvent& from) {
    InputEvent::initialize(from);
    mIsExiting = from.mIsExiting;
    mX = from.mX;
    mY = from.mY;
}

// --- PooledInputEventFactory ---
// --- PooledInputEventFactory ---


PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
PooledInputEventFactory::PooledInputEventFactory(size_t maxPoolSize) :
@@ -815,6 +835,15 @@ CaptureEvent* PooledInputEventFactory::createCaptureEvent() {
    return event;
    return event;
}
}


DragEvent* PooledInputEventFactory::createDragEvent() {
    if (mDragEventPool.empty()) {
        return new DragEvent();
    }
    DragEvent* event = mDragEventPool.front().release();
    mDragEventPool.pop();
    return event;
}

void PooledInputEventFactory::recycle(InputEvent* event) {
void PooledInputEventFactory::recycle(InputEvent* event) {
    switch (event->getType()) {
    switch (event->getType()) {
    case AINPUT_EVENT_TYPE_KEY:
    case AINPUT_EVENT_TYPE_KEY:
@@ -842,6 +871,12 @@ void PooledInputEventFactory::recycle(InputEvent* event) {
            return;
            return;
        }
        }
        break;
        break;
    case AINPUT_EVENT_TYPE_DRAG:
        if (mDragEventPool.size() < mMaxPoolSize) {
            mDragEventPool.push(std::unique_ptr<DragEvent>(static_cast<DragEvent*>(event)));
            return;
        }
        break;
    }
    }
    delete event;
    delete event;
}
}
+51 −0
Original line number Original line Diff line number Diff line
@@ -108,6 +108,8 @@ bool InputMessage::isValid(size_t actualSize) const {
                return true;
                return true;
            case Type::CAPTURE:
            case Type::CAPTURE:
                return true;
                return true;
            case Type::DRAG:
                return true;
        }
        }
    }
    }
    return false;
    return false;
@@ -125,6 +127,8 @@ size_t InputMessage::size() const {
            return sizeof(Header) + body.focus.size();
            return sizeof(Header) + body.focus.size();
        case Type::CAPTURE:
        case Type::CAPTURE:
            return sizeof(Header) + body.capture.size();
            return sizeof(Header) + body.capture.size();
        case Type::DRAG:
            return sizeof(Header) + body.drag.size();
    }
    }
    return sizeof(Header);
    return sizeof(Header);
}
}
@@ -249,6 +253,13 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
            msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
            break;
            break;
        }
        }
        case InputMessage::Type::DRAG: {
            msg->body.drag.eventId = body.drag.eventId;
            msg->body.drag.x = body.drag.x;
            msg->body.drag.y = body.drag.y;
            msg->body.drag.isExiting = body.drag.isExiting;
            break;
        }
    }
    }
}
}


@@ -599,6 +610,25 @@ status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
    return mChannel->sendMessage(&msg);
    return mChannel->sendMessage(&msg);
}
}


status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
                                          bool isExiting) {
    if (ATRACE_ENABLED()) {
        std::string message =
                StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
                             mChannel->getName().c_str(), x, y, toString(isExiting));
        ATRACE_NAME(message.c_str());
    }

    InputMessage msg;
    msg.header.type = InputMessage::Type::DRAG;
    msg.header.seq = seq;
    msg.body.drag.eventId = eventId;
    msg.body.drag.isExiting = isExiting;
    msg.body.drag.x = x;
    msg.body.drag.y = y;
    return mChannel->sendMessage(&msg);
}

status_t InputPublisher::receiveFinishedSignal(
status_t InputPublisher::receiveFinishedSignal(
        const std::function<void(uint32_t seq, bool handled, nsecs_t consumeTime)>& callback) {
        const std::function<void(uint32_t seq, bool handled, nsecs_t consumeTime)>& callback) {
    if (DEBUG_TRANSPORT_ACTIONS) {
    if (DEBUG_TRANSPORT_ACTIONS) {
@@ -779,6 +809,16 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consum
                *outEvent = captureEvent;
                *outEvent = captureEvent;
                break;
                break;
            }
            }

            case InputMessage::Type::DRAG: {
                DragEvent* dragEvent = factory->createDragEvent();
                if (!dragEvent) return NO_MEMORY;

                initializeDragEvent(dragEvent, &mMsg);
                *outSeq = mMsg.header.seq;
                *outEvent = dragEvent;
                break;
            }
        }
        }
    }
    }
    return OK;
    return OK;
@@ -1236,6 +1276,11 @@ void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessa
    event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
    event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
}
}


void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
    event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
                      msg->body.drag.isExiting);
}

void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
    uint32_t pointerCount = msg->body.motion.pointerCount;
    uint32_t pointerCount = msg->body.motion.pointerCount;
    PointerProperties pointerProperties[pointerCount];
    PointerProperties pointerProperties[pointerCount];
@@ -1346,6 +1391,12 @@ std::string InputConsumer::dump() const {
                                                                        .pointerCaptureEnabled));
                                                                        .pointerCaptureEnabled));
                    break;
                    break;
                }
                }
                case InputMessage::Type::DRAG: {
                    out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
                                                       msg.body.drag.x, msg.body.drag.y,
                                                       toString(msg.body.drag.isExiting));
                    break;
                }
            }
            }
            out += "\n";
            out += "\n";
        }
        }
Loading