mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
Merge upstream main into unified input profiles
Integrate screen keys, portrait controls, and Home/Back shortcuts with routed input. Preserve remapped axes, neutral gating, and explicit Back bindings; discard stale queued chord reports when routing changes. Validation: 2,632 debug JVM tests across 385 suites pass with Java 17 and skipRust. Device testing and latency measurements were not repeated for this merge.
This commit is contained in:
commit
92b4361100
525 files changed
+40912
-3336
No files matched your search
@@ -184,8 +184,9 @@ bool FrameGen::onResize(VkExtent2D extent, VkFormat , bool force) {
|
||||
VkExtent2D e = levelExtent(extent, l);
|
||||
if (!makeImage(pyrA_[l], e, VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
|
||||
if (!makeImage(pyrB_[l], e, VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
|
||||
if (!makeImage(flowLvl_[l], e, VK_FORMAT_R16G16B16A16_SFLOAT, flowUsage)) return false;
|
||||
}
|
||||
for (int l = flowFinest_; l < kLevels; ++l)
|
||||
if (!makeImage(flowLvl_[l], levelExtent(extent, l), VK_FORMAT_R16G16B16A16_SFLOAT, flowUsage)) return false;
|
||||
pyrAs_.resize(kLevels);
|
||||
for (int l = flowFinest_; l < kLevels; ++l)
|
||||
if (!makeImage(pyrAs_[l], levelExtent(extent, l), VK_FORMAT_R32_SFLOAT, lumaUsage)) return false;
|
||||
|
||||
@@ -439,7 +439,7 @@ static bool ensureScratch(FrameGen* fg, const DeviceDispatch* dd, VkDevice dev,
|
||||
makeUbo(dd, dev, mp, sizeof(FlowRegUBO), S.flowRegUbo, S.flowRegUboMem, S.flowRegUboPtr);
|
||||
if (S.flowRegUboPtr) { FlowRegUBO fu{cfg.frDt, cfg.frLambda, cfg.frEdge, cfg.frEps}; std::memcpy(S.flowRegUboPtr, &fu, sizeof(fu)); }
|
||||
S.flowUbo.resize(kLevels); S.flowUboMem.resize(kLevels); S.flowUboPtr.resize(kLevels);
|
||||
for (int l = 0; l < kLevels; ++l) {
|
||||
for (int l = fg->flowFinest(); l < kLevels; ++l) {
|
||||
makeUbo(dd, dev, mp, sizeof(FlowUBO), S.flowUbo[l], S.flowUboMem[l], S.flowUboPtr[l]);
|
||||
if (S.flowUboPtr[l]) { FlowUBO u{cfg.flowScale, (uint32_t)l, 0.6f, 1.6f}; std::memcpy(S.flowUboPtr[l], &u, sizeof(u)); }
|
||||
}
|
||||
@@ -464,13 +464,12 @@ static bool ensureScratch(FrameGen* fg, const DeviceDispatch* dd, VkDevice dev,
|
||||
}
|
||||
|
||||
S.sFlow.resize(kLevels);
|
||||
for (int l = 0; l < kLevels; ++l) {
|
||||
for (int l = fg->flowFinest(); l < kLevels; ++l) {
|
||||
S.sFlow[l] = allocSet(dd, dev, pool, pFlow.setLayout);
|
||||
int pred = (l + 1 < kLevels) ? l + 1 : l;
|
||||
VkImageView currLvl = (l >= fg->flowFinest()) ? pyrAs[l].view : pyrA[l].view;
|
||||
wUbo(dd, dev, S.sFlow[l], 0, S.flowUbo[l], sizeof(FlowUBO));
|
||||
wImg(dd, dev, S.sFlow[l], 32, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrB[l].view, samp, GEN);
|
||||
wImg(dd, dev, S.sFlow[l], 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, currLvl, samp, GEN);
|
||||
wImg(dd, dev, S.sFlow[l], 33, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, pyrAs[l].view, samp, GEN);
|
||||
wImg(dd, dev, S.sFlow[l], 34, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, flowLvl[pred].view, samp, GEN);
|
||||
wImg(dd, dev, S.sFlow[l], 48, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, flowLvl[l].view, VK_NULL_HANDLE, GEN);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#define VENC_MAX_BUFFERS 16
|
||||
|
||||
enum venc_msg { VENC_CONFIG = 1, VENC_FRAME = 2, VENC_DONE = 3, VENC_BITRATE = 4, VENC_TOUCH = 5 };
|
||||
enum venc_msg { VENC_CONFIG = 1, VENC_FRAME = 2, VENC_DONE = 3, VENC_BITRATE = 4, VENC_TOUCH = 5, VDEC_CONFIG = 6, VDEC_CAPTURE = 7, VDEC_PACKET = 8, VDEC_RESULT = 9 };
|
||||
enum venc_touch_action { VENC_TOUCH_DOWN = 0, VENC_TOUCH_MOVE = 1, VENC_TOUCH_UP = 2 };
|
||||
enum venc_codec { VENC_H264 = 0, VENC_HEVC = 1 };
|
||||
|
||||
@@ -52,4 +52,30 @@ struct venc_touch {
|
||||
uint32_t x, y;
|
||||
};
|
||||
|
||||
struct vdec_config {
|
||||
uint32_t type;
|
||||
uint32_t codec;
|
||||
uint32_t width, height;
|
||||
uint32_t stride, rows;
|
||||
uint32_t bitstreams, bitstream_size;
|
||||
};
|
||||
|
||||
struct vdec_capture {
|
||||
uint32_t type;
|
||||
uint32_t index;
|
||||
uint32_t size;
|
||||
};
|
||||
|
||||
struct vdec_packet {
|
||||
uint32_t type;
|
||||
uint32_t input, bytes, capture;
|
||||
int64_t pts_us;
|
||||
};
|
||||
|
||||
struct vdec_result {
|
||||
uint32_t type;
|
||||
int32_t status;
|
||||
uint32_t frame;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <jni.h>
|
||||
#include <media/NdkMediaCodec.h>
|
||||
#include <media/NdkMediaFormat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -11,6 +12,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
@@ -45,6 +47,218 @@ int32_t avc_profile(int32_t v4l2) {
|
||||
}
|
||||
}
|
||||
|
||||
struct DmaBufSync {
|
||||
uint64_t flags;
|
||||
};
|
||||
constexpr unsigned long kDmaBufIoctlSync = _IOW('b', 0, DmaBufSync);
|
||||
constexpr uint64_t kDmaBufSyncWrite = 2, kDmaBufSyncStart = 0, kDmaBufSyncEnd = 4;
|
||||
constexpr int32_t kColorFormatYuv420Planar = 19;
|
||||
|
||||
class Decoder {
|
||||
public:
|
||||
~Decoder() { release(); }
|
||||
|
||||
void release() {
|
||||
if (codec_) {
|
||||
AMediaCodec_stop(codec_);
|
||||
AMediaCodec_delete(codec_);
|
||||
codec_ = nullptr;
|
||||
}
|
||||
if (bitstream_) munmap((void *)bitstream_, bitstream_size_);
|
||||
bitstream_ = nullptr;
|
||||
for (auto &c : captures_) drop(c);
|
||||
}
|
||||
|
||||
bool configure(const vdec_config &c, int fd) {
|
||||
release();
|
||||
if (c.width < 16 || c.height < 16 || c.stride < c.width || c.rows < c.height || c.bitstreams == 0 ||
|
||||
c.bitstreams > VENC_MAX_BUFFERS || c.bitstream_size == 0) {
|
||||
LOG("bad decoder config: %ux%u stride %u rows %u, %u bitstreams", c.width, c.height, c.stride, c.rows,
|
||||
c.bitstreams);
|
||||
return false;
|
||||
}
|
||||
bitstream_size_ = (size_t)c.bitstreams * c.bitstream_size;
|
||||
void *p = mmap(nullptr, bitstream_size_, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED) {
|
||||
LOG("could not map the bitstream buffers: %s", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
bitstream_ = (const uint8_t *)p;
|
||||
config_ = c;
|
||||
codec_ = create(c, true);
|
||||
if (!codec_) codec_ = create(c, false);
|
||||
if (!codec_) {
|
||||
LOG("no %s decoder for %ux%u", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height);
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
src_stride_ = (int32_t)c.width;
|
||||
src_rows_ = (int32_t)c.height;
|
||||
src_width_ = (int32_t)c.width;
|
||||
src_height_ = (int32_t)c.height;
|
||||
src_planar_ = false;
|
||||
shown_ = false;
|
||||
using GetName = media_status_t (*)(AMediaCodec *, char **);
|
||||
using ReleaseName = void (*)(AMediaCodec *, char *);
|
||||
static auto get_name = (GetName)dlsym(RTLD_DEFAULT, "AMediaCodec_getName");
|
||||
static auto release_name = (ReleaseName)dlsym(RTLD_DEFAULT, "AMediaCodec_releaseName");
|
||||
char *name = nullptr;
|
||||
if (get_name && get_name(codec_, &name) != AMEDIA_OK) name = nullptr;
|
||||
LOG("decoding %s %ux%u with %s", c.codec == VENC_HEVC ? "HEVC" : "H.264", c.width, c.height,
|
||||
name ? name : "MediaCodec");
|
||||
if (name && release_name) release_name(codec_, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void capture(const vdec_capture &c, int fd) {
|
||||
if (c.index >= VENC_MAX_BUFFERS) {
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
Capture &cap = captures_[c.index];
|
||||
drop(cap);
|
||||
void *p = mmap(nullptr, c.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (p == MAP_FAILED) {
|
||||
LOG("could not map capture buffer %u: %s", c.index, strerror(errno));
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
cap = {fd, (uint8_t *)p, c.size};
|
||||
}
|
||||
|
||||
bool decode(const vdec_packet &p, bool *frame) {
|
||||
*frame = false;
|
||||
if (!codec_ || p.capture >= VENC_MAX_BUFFERS || !captures_[p.capture].data || p.input >= config_.bitstreams ||
|
||||
p.bytes == 0 || p.bytes > config_.bitstream_size)
|
||||
return false;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
ssize_t in = AMediaCodec_dequeueInputBuffer(codec_, 20000);
|
||||
if (in < 0) return false;
|
||||
size_t capacity = 0;
|
||||
uint8_t *dst = AMediaCodec_getInputBuffer(codec_, (size_t)in, &capacity);
|
||||
if (!dst || capacity < p.bytes) {
|
||||
AMediaCodec_queueInputBuffer(codec_, (size_t)in, 0, 0, 0, 0);
|
||||
return false;
|
||||
}
|
||||
memcpy(dst, bitstream_ + (size_t)p.input * config_.bitstream_size, p.bytes);
|
||||
if (AMediaCodec_queueInputBuffer(codec_, (size_t)in, 0, p.bytes, (uint64_t)std::max<int64_t>(p.pts_us, 0), 0) !=
|
||||
AMEDIA_OK)
|
||||
return false;
|
||||
auto wait = std::chrono::milliseconds(shown_ ? 25 : 250);
|
||||
while (std::chrono::steady_clock::now() - start < wait) {
|
||||
AMediaCodecBufferInfo info;
|
||||
ssize_t o = AMediaCodec_dequeueOutputBuffer(codec_, &info, 2000);
|
||||
if (o == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
|
||||
read_format();
|
||||
continue;
|
||||
}
|
||||
if (o < 0) continue;
|
||||
size_t size = 0;
|
||||
uint8_t *data = AMediaCodec_getOutputBuffer(codec_, (size_t)o, &size);
|
||||
if (data && info.size > 0 && (size_t)info.offset + (size_t)info.size <= size)
|
||||
*frame = copy(data + info.offset, (size_t)info.size, captures_[p.capture]) || *frame;
|
||||
AMediaCodec_releaseOutputBuffer(codec_, (size_t)o, false);
|
||||
if (info.presentationTimeUs >= p.pts_us) break;
|
||||
}
|
||||
shown_ = shown_ || *frame;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Capture {
|
||||
int fd = -1;
|
||||
uint8_t *data = nullptr;
|
||||
size_t size = 0;
|
||||
};
|
||||
|
||||
static void drop(Capture &c) {
|
||||
if (c.data) munmap(c.data, c.size);
|
||||
if (c.fd >= 0) close(c.fd);
|
||||
c = Capture();
|
||||
}
|
||||
|
||||
AMediaCodec *create(const vdec_config &c, bool low_latency) {
|
||||
const char *mime = c.codec == VENC_HEVC ? "video/hevc" : "video/avc";
|
||||
AMediaCodec *codec = AMediaCodec_createDecoderByType(mime);
|
||||
if (!codec) return nullptr;
|
||||
AMediaFormat *f = AMediaFormat_new();
|
||||
AMediaFormat_setString(f, AMEDIAFORMAT_KEY_MIME, mime);
|
||||
AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_WIDTH, (int32_t)c.width);
|
||||
AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_HEIGHT, (int32_t)c.height);
|
||||
AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_COLOR_FORMAT, kColorFormatYuv420SemiPlanar);
|
||||
AMediaFormat_setInt32(f, AMEDIAFORMAT_KEY_MAX_INPUT_SIZE, (int32_t)c.bitstream_size);
|
||||
if (low_latency) {
|
||||
AMediaFormat_setInt32(f, "low-latency", 1);
|
||||
AMediaFormat_setInt32(f, "vendor.qti-ext-dec-low-latency.enable", 1);
|
||||
AMediaFormat_setInt32(f, "vendor.qti-ext-dec-picture-order.enable", 1);
|
||||
AMediaFormat_setInt32(f, "priority", 0);
|
||||
AMediaFormat_setFloat(f, "operating-rate", 120.0f);
|
||||
}
|
||||
media_status_t s = AMediaCodec_configure(codec, f, nullptr, nullptr, 0);
|
||||
AMediaFormat_delete(f);
|
||||
if (s == AMEDIA_OK) s = AMediaCodec_start(codec);
|
||||
if (s != AMEDIA_OK) {
|
||||
AMediaCodec_delete(codec);
|
||||
return nullptr;
|
||||
}
|
||||
return codec;
|
||||
}
|
||||
|
||||
void read_format() {
|
||||
AMediaFormat *f = AMediaCodec_getOutputFormat(codec_);
|
||||
if (!f) return;
|
||||
int32_t v;
|
||||
if (AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_WIDTH, &v) && v > 0) src_width_ = v;
|
||||
if (AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_HEIGHT, &v) && v > 0) src_height_ = v;
|
||||
src_stride_ = AMediaFormat_getInt32(f, "stride", &v) && v >= src_width_ ? v : src_width_;
|
||||
src_rows_ = AMediaFormat_getInt32(f, "slice-height", &v) && v >= src_height_ ? v : src_height_;
|
||||
src_planar_ = AMediaFormat_getInt32(f, AMEDIAFORMAT_KEY_COLOR_FORMAT, &v) && v == kColorFormatYuv420Planar;
|
||||
LOG("decoder output %dx%d stride %d rows %d %s", src_width_, src_height_, src_stride_, src_rows_,
|
||||
src_planar_ ? "planar" : "semi-planar");
|
||||
AMediaFormat_delete(f);
|
||||
}
|
||||
|
||||
bool copy(const uint8_t *src, size_t size, const Capture &cap) {
|
||||
size_t luma = (size_t)src_stride_ * src_rows_;
|
||||
size_t chroma = src_planar_ ? 2 * (size_t)(src_stride_ / 2) * (src_rows_ / 2) : (size_t)src_stride_ * (src_rows_ / 2);
|
||||
if (size < luma + chroma) return false;
|
||||
uint32_t w = std::min<uint32_t>((uint32_t)src_width_, config_.stride);
|
||||
uint32_t h = std::min<uint32_t>((uint32_t)src_height_, config_.rows);
|
||||
if ((size_t)config_.stride * config_.rows * 3 / 2 > cap.size) return false;
|
||||
DmaBufSync sync = {kDmaBufSyncStart | kDmaBufSyncWrite};
|
||||
ioctl(cap.fd, kDmaBufIoctlSync, &sync);
|
||||
uint8_t *y = cap.data, *uv = cap.data + (size_t)config_.stride * config_.rows;
|
||||
for (uint32_t row = 0; row < h; row++)
|
||||
memcpy(y + (size_t)row * config_.stride, src + (size_t)row * src_stride_, w);
|
||||
const uint8_t *su = src + luma;
|
||||
for (uint32_t row = 0; row < h / 2; row++) {
|
||||
uint8_t *d = uv + (size_t)row * config_.stride;
|
||||
if (!src_planar_) {
|
||||
memcpy(d, su + (size_t)row * src_stride_, w & ~1u);
|
||||
continue;
|
||||
}
|
||||
const uint8_t *u = su + (size_t)row * (src_stride_ / 2);
|
||||
const uint8_t *vv = su + (size_t)(src_stride_ / 2) * (src_rows_ / 2) + (size_t)row * (src_stride_ / 2);
|
||||
for (uint32_t x = 0; x < w / 2; x++) {
|
||||
d[2 * x] = u[x];
|
||||
d[2 * x + 1] = vv[x];
|
||||
}
|
||||
}
|
||||
sync.flags = kDmaBufSyncEnd | kDmaBufSyncWrite;
|
||||
ioctl(cap.fd, kDmaBufIoctlSync, &sync);
|
||||
return true;
|
||||
}
|
||||
|
||||
AMediaCodec *codec_ = nullptr;
|
||||
vdec_config config_ = {};
|
||||
const uint8_t *bitstream_ = nullptr;
|
||||
size_t bitstream_size_ = 0;
|
||||
Capture captures_[VENC_MAX_BUFFERS];
|
||||
int32_t src_width_ = 0, src_height_ = 0, src_stride_ = 0, src_rows_ = 0;
|
||||
bool src_planar_ = false;
|
||||
bool shown_ = false;
|
||||
};
|
||||
|
||||
class Session {
|
||||
public:
|
||||
explicit Session(int fd) : fd_(fd) {}
|
||||
@@ -85,6 +299,24 @@ public:
|
||||
memcpy(&b, buf, sizeof b);
|
||||
set_bitrate(b.bitrate);
|
||||
for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
} else if (type == VDEC_CONFIG && n == (ssize_t)sizeof(vdec_config)) {
|
||||
vdec_config c;
|
||||
memcpy(&c, buf, sizeof c);
|
||||
bool ok = nfds == 1 && decoder_.configure(c, fds[0]);
|
||||
for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
decoded(ok ? 0 : -1, false);
|
||||
} else if (type == VDEC_CAPTURE && n == (ssize_t)sizeof(vdec_capture)) {
|
||||
vdec_capture c;
|
||||
memcpy(&c, buf, sizeof c);
|
||||
if (nfds == 1) decoder_.capture(c, fds[0]);
|
||||
else for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
} else if (type == VDEC_PACKET && n == (ssize_t)sizeof(vdec_packet)) {
|
||||
vdec_packet p;
|
||||
memcpy(&p, buf, sizeof p);
|
||||
bool frame = false;
|
||||
bool ok = decoder_.decode(p, &frame);
|
||||
for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
decoded(ok ? 0 : -1, frame);
|
||||
} else {
|
||||
LOG("unexpected message %u of %zd bytes; closing", type, n);
|
||||
for (int i = 0; i < nfds; i++) close(fds[i]);
|
||||
@@ -117,7 +349,13 @@ private:
|
||||
send(fd_, &d, sizeof d, MSG_NOSIGNAL);
|
||||
}
|
||||
|
||||
void decoded(int32_t status, bool frame) {
|
||||
vdec_result r = {VDEC_RESULT, status, frame ? 1u : 0u};
|
||||
send(fd_, &r, sizeof r, MSG_NOSIGNAL);
|
||||
}
|
||||
|
||||
void release() {
|
||||
decoder_.release();
|
||||
if (codec_) {
|
||||
AMediaCodec_stop(codec_);
|
||||
AMediaCodec_delete(codec_);
|
||||
@@ -292,6 +530,7 @@ private:
|
||||
int32_t dst_stride_ = 0, dst_rows_ = 0;
|
||||
std::vector<Mapping> inputs_, outputs_;
|
||||
std::vector<uint8_t> header_;
|
||||
Decoder decoder_;
|
||||
};
|
||||
|
||||
std::atomic<int> g_listener{-1};
|
||||
|
||||
@@ -95,4 +95,5 @@ target_link_libraries(droiddeckwayland
|
||||
dl
|
||||
log
|
||||
android
|
||||
nativewindow
|
||||
)
|
||||
@@ -371,8 +371,8 @@ void ahb_swapchain_set_mode(int on, int live) {
|
||||
: "; gralloc frames still in flight stay on the layer (or take the copy path) until then");
|
||||
else
|
||||
droiddeck_log("layer", "zero-copy: %s at launch (%s)", on ? "on" : "off",
|
||||
on ? "DROIDDECK_WAYLAND_ZERO_COPY=1: games present their own gralloc buffers on the display layer"
|
||||
: "the drawer's Zero-copy presentation switch turns it on live");
|
||||
on ? "HDR session: games present their own gralloc buffers on the display layer"
|
||||
: "only HDR sessions turn it on");
|
||||
if (live) wl_display_flush_clients(droiddeck_get_display());
|
||||
droiddeck_request_redraw();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <android/log.h>
|
||||
#include <android/hardware_buffer.h>
|
||||
#include <wayland-server.h>
|
||||
|
||||
#include "xdg-shell-server-protocol.h"
|
||||
@@ -185,6 +186,9 @@ static void on_client_created(struct wl_listener *l, void *data) {
|
||||
|
||||
enum surface_role { ROLE_NONE, ROLE_TOPLEVEL, ROLE_SUBSURFACE, ROLE_DESKTOP, ROLE_CURSOR };
|
||||
|
||||
#define DAMAGE_RECTS 8
|
||||
struct damage_list { int32_t r[DAMAGE_RECTS][4]; int n; };
|
||||
|
||||
struct surface {
|
||||
struct wl_resource *resource;
|
||||
struct wl_list link;
|
||||
@@ -206,6 +210,7 @@ struct surface {
|
||||
int buf_w, buf_h, has_content;
|
||||
int buf_alpha;
|
||||
int opaque[4], pending_opaque[4], pending_opaque_set;
|
||||
struct damage_list damage_buf, pending_damage_buf, damage_local, pending_damage_local;
|
||||
int src_set, dst_set;
|
||||
float src[4];
|
||||
int dst[2];
|
||||
@@ -263,6 +268,8 @@ static struct wl_event_source *g_fallback_timer;
|
||||
static int g_fallback_armed;
|
||||
|
||||
volatile int g_fps_limit;
|
||||
/* fps limit -1 (phone output, not mirrored): replaced buffers go back to the client at once instead of once per refresh. */
|
||||
volatile int g_release_unpaced;
|
||||
|
||||
volatile int g_hide_shell;
|
||||
|
||||
@@ -370,7 +377,7 @@ static int on_release_timer(int fd, uint32_t mask, void *data) {
|
||||
static void release_buffer(struct surface *s, struct wl_resource *buffer, int64_t since_ns) {
|
||||
const int limit = g_fps_limit;
|
||||
const int64_t interval = limit > 0 ? 1000000000LL / limit : g_refresh_ns;
|
||||
if (interval <= 0 || g_release_timer_fd < 0) { wl_buffer_send_release(buffer); perf_note_release(since_ns); return; }
|
||||
if (g_release_unpaced || interval <= 0 || g_release_timer_fd < 0) { wl_buffer_send_release(buffer); perf_note_release(since_ns); return; }
|
||||
int64_t now = now_ns();
|
||||
int64_t at = s->next_release_ns + interval;
|
||||
int64_t latest = now + interval * (int64_t)(s->releases_pending + 1);
|
||||
@@ -621,6 +628,68 @@ static void fire_frames(struct wl_list *frames) {
|
||||
}
|
||||
}
|
||||
|
||||
static void scene_size(int *w, int *h);
|
||||
static int64_t damage_area(const struct damage_list *d, int bound_w, int bound_h);
|
||||
static struct surface *g_hud_surface, *g_hud_shm;
|
||||
static int64_t g_hud_shm_ns;
|
||||
static int64_t g_hud_last_ns, g_hud_alive_ns;
|
||||
static int g_hud_fresh;
|
||||
static int g_hud_new;
|
||||
#define HUD_TIMES 256
|
||||
static pthread_mutex_t g_hud_times_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static float g_hud_times[HUD_TIMES];
|
||||
static int64_t g_hud_times_total, g_hud_shown_ns;
|
||||
|
||||
static void hud_frame_shown(int64_t t) {
|
||||
const int64_t previous = g_hud_shown_ns;
|
||||
g_hud_shown_ns = t;
|
||||
if (!previous || t <= previous) return;
|
||||
pthread_mutex_lock(&g_hud_times_lock);
|
||||
g_hud_times[g_hud_times_total % HUD_TIMES] = (float)((t - previous) / 1e6);
|
||||
g_hud_times_total++;
|
||||
pthread_mutex_unlock(&g_hud_times_lock);
|
||||
}
|
||||
|
||||
static int tree_rank(const struct surface *s, const struct surface *want, int *rank, int depth) {
|
||||
const struct surface *c;
|
||||
if (depth > 8) return 0;
|
||||
wl_list_for_each(c, &s->children, child_link)
|
||||
if (c->below_parent && tree_rank(c, want, rank, depth + 1)) return 1;
|
||||
if (s == want) return 1;
|
||||
(*rank)++;
|
||||
wl_list_for_each(c, &s->children, child_link)
|
||||
if (!c->below_parent && tree_rank(c, want, rank, depth + 1)) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int render_rank(const struct surface *want) {
|
||||
const struct surface *s;
|
||||
int rank = 0;
|
||||
if (g_desktop && tree_rank(g_desktop, want, &rank, 0)) return rank;
|
||||
wl_list_for_each(s, &g_toplevels, toplevel_link)
|
||||
if (tree_rank(s, want, &rank, 0)) return rank;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int below_surface(const struct surface *s, const struct surface *other) {
|
||||
const int a = render_rank(s), b = render_rank(other);
|
||||
return a >= 0 && b >= 0 && a < b;
|
||||
}
|
||||
|
||||
int droiddeck_hud_frame_times(float *out, int len, int64_t *total) {
|
||||
pthread_mutex_lock(&g_hud_times_lock);
|
||||
*total = g_hud_times_total;
|
||||
int64_t n = *total < len ? *total : len;
|
||||
if (n > HUD_TIMES) n = HUD_TIMES;
|
||||
for (int64_t i = 0; i < n; i++) out[i] = g_hud_times[(*total - n + i) % HUD_TIMES];
|
||||
pthread_mutex_unlock(&g_hud_times_lock);
|
||||
return (int)n;
|
||||
}
|
||||
static uint64_t g_game_frames;
|
||||
extern void droiddeck_on_game_surface(const char *window, const char *gpu);
|
||||
extern void droiddeck_on_game_frame(void);
|
||||
extern void droiddeck_on_game_program(int pid, const char *program);
|
||||
|
||||
static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_resource *buffer) {
|
||||
int32_t w = wl_shm_buffer_get_width(shm), h = wl_shm_buffer_get_height(shm);
|
||||
int32_t stride = wl_shm_buffer_get_stride(shm);
|
||||
@@ -643,6 +712,33 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res
|
||||
g_stat_shm++;
|
||||
struct client_info *ci = client_info_of(wl_resource_get_client(s->resource));
|
||||
if (ci && ci->asked_feedback) ci->shm_frames++;
|
||||
int local_w, local_h;
|
||||
surface_size(s, &local_w, &local_h);
|
||||
const double region = s->src_set ? (double)s->src[2] * s->src[3] : (double)w * h;
|
||||
const int64_t local_max = damage_area(&s->damage_local, local_w, local_h);
|
||||
const int64_t local = local_w > 0 && local_h > 0
|
||||
? (int64_t)((double)local_max * region / ((double)local_w * local_h)) : local_max;
|
||||
const int64_t buffer_max = damage_area(&s->damage_buf, w, h);
|
||||
const int64_t area = (int64_t)w * h;
|
||||
int64_t damaged = buffer_max > local ? buffer_max : local;
|
||||
if (damaged > area) damaged = area;
|
||||
const int64_t now = now_ns();
|
||||
if (damaged * 16 >= area && (!g_hud_surface || now - g_hud_last_ns > 1000000000LL) &&
|
||||
(s == g_hud_shm || !g_hud_shm || now - g_hud_shm_ns > 1000000000LL)) {
|
||||
const struct surface *top = s;
|
||||
int scene_w, scene_h, top_w, top_h, depth = 0;
|
||||
while (top->parent && depth++ < 8) top = top->parent;
|
||||
scene_size(&scene_w, &scene_h);
|
||||
surface_size(top, &top_w, &top_h);
|
||||
int64_t shown = (int64_t)local_w * local_h;
|
||||
if ((int64_t)top_w * top_h > shown) shown = (int64_t)top_w * top_h;
|
||||
if (top->role == ROLE_TOPLEVEL && top->mapped && shown * 2 >= (int64_t)scene_w * scene_h) {
|
||||
g_hud_shm = s;
|
||||
g_hud_shm_ns = now;
|
||||
g_hud_new = 1;
|
||||
droiddeck_on_game_frame();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resource *buffer) {
|
||||
@@ -659,14 +755,6 @@ static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resou
|
||||
s->has_content = s->shm_img != NULL;
|
||||
}
|
||||
|
||||
static struct surface *g_hud_surface;
|
||||
static int64_t g_hud_last_ns, g_hud_alive_ns;
|
||||
static int g_hud_fresh;
|
||||
static uint64_t g_game_frames;
|
||||
extern void droiddeck_on_game_surface(const char *window, const char *gpu);
|
||||
extern void droiddeck_on_game_frame(void);
|
||||
extern void droiddeck_on_game_program(int pid, const char *program);
|
||||
|
||||
static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_resource *buffer) {
|
||||
const int repeat = s->dmabuf == buffer && s->dmabuf_buf == b;
|
||||
if (!repeat) {
|
||||
@@ -721,8 +809,9 @@ static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_re
|
||||
name, b->width, b->height, (unsigned long long)b->modifier);
|
||||
}
|
||||
if (s != g_hud_surface && (b->img || ahb_swapchain_has_ahb(b)) &&
|
||||
(!g_hud_surface || now_ns() - g_hud_alive_ns > 1000000000LL ||
|
||||
(announced_now && (int64_t)b->width * b->height >= (int64_t)g_hud_surface->buf_w * g_hud_surface->buf_h))) {
|
||||
(!g_hud_surface || now_ns() - g_hud_alive_ns > 1000000000LL || (!repeat && below_surface(s, g_hud_surface)) ||
|
||||
(announced_now && !below_surface(g_hud_surface, s) &&
|
||||
(int64_t)b->width * b->height >= (int64_t)g_hud_surface->buf_w * g_hud_surface->buf_h))) {
|
||||
char name[160];
|
||||
describe(s, name, sizeof(name));
|
||||
g_hud_surface = s;
|
||||
@@ -746,7 +835,7 @@ static void take_dmabuf(struct surface *s, struct dmabuf_buffer *b, struct wl_re
|
||||
}
|
||||
if (s == g_hud_surface) {
|
||||
g_hud_alive_ns = now_ns();
|
||||
if (!repeat) { g_hud_last_ns = g_hud_alive_ns; g_hud_fresh = 1; g_game_frames++; droiddeck_on_game_frame(); }
|
||||
if (!repeat) { g_hud_last_ns = g_hud_alive_ns; g_hud_fresh = 1; g_hud_new = 1; g_game_frames++; droiddeck_on_game_frame(); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -783,8 +872,45 @@ static void surface_attach(struct wl_client *c, struct wl_resource *r,
|
||||
wl_resource_add_destroy_listener(buffer, &s->pending_buffer_destroy);
|
||||
}
|
||||
}
|
||||
static int64_t rect_area(const int32_t *r, int bound_w, int bound_h) {
|
||||
int64_t x0 = r[0], y0 = r[1], x1 = (int64_t)r[0] + r[2], y1 = (int64_t)r[1] + r[3];
|
||||
if (bound_w > 0 && bound_h > 0) {
|
||||
if (x0 < 0) x0 = 0;
|
||||
if (y0 < 0) y0 = 0;
|
||||
if (x1 > bound_w) x1 = bound_w;
|
||||
if (y1 > bound_h) y1 = bound_h;
|
||||
}
|
||||
if (x1 <= x0 || y1 <= y0) return 0;
|
||||
const int64_t w = x1 - x0, h = y1 - y0;
|
||||
return (w < 65536 ? w : 65536) * (h < 65536 ? h : 65536);
|
||||
}
|
||||
static void note_damage(struct damage_list *d, int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
if (w <= 0 || h <= 0) return;
|
||||
const int32_t rect[4] = { x, y, w, h };
|
||||
int slot = d->n;
|
||||
if (slot == DAMAGE_RECTS) {
|
||||
slot = 0;
|
||||
for (int i = 1; i < DAMAGE_RECTS; i++)
|
||||
if (rect_area(d->r[i], 0, 0) < rect_area(d->r[slot], 0, 0)) slot = i;
|
||||
if (rect_area(rect, 0, 0) <= rect_area(d->r[slot], 0, 0)) return;
|
||||
} else {
|
||||
d->n++;
|
||||
}
|
||||
memcpy(d->r[slot], rect, sizeof(rect));
|
||||
}
|
||||
static int64_t damage_area(const struct damage_list *d, int bound_w, int bound_h) {
|
||||
int64_t max = 0;
|
||||
for (int i = 0; i < d->n; i++) {
|
||||
const int64_t area = rect_area(d->r[i], bound_w, bound_h);
|
||||
if (area > max) max = area;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
static void surface_damage(struct wl_client *c, struct wl_resource *r,
|
||||
int32_t x, int32_t y, int32_t w, int32_t h) {}
|
||||
int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
struct surface *s = wl_resource_get_user_data(r);
|
||||
note_damage(&s->pending_damage_local, x, y, w, h);
|
||||
}
|
||||
static void frame_callback_destroy(struct wl_resource *r) {
|
||||
wl_list_remove(wl_resource_get_link(r));
|
||||
}
|
||||
@@ -984,6 +1110,9 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) {
|
||||
s->pending_opaque_set = 0;
|
||||
}
|
||||
|
||||
s->damage_buf = s->pending_damage_buf;
|
||||
s->damage_local = s->pending_damage_local;
|
||||
s->pending_damage_buf.n = s->pending_damage_local.n = 0;
|
||||
if (s->pending_attach) {
|
||||
struct wl_resource *buffer = s->pending_buffer;
|
||||
struct dmabuf_buffer *db = get_dmabuf(buffer);
|
||||
@@ -1048,7 +1177,10 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) {
|
||||
static void surface_set_buffer_transform(struct wl_client *c, struct wl_resource *r, int32_t t) {}
|
||||
static void surface_set_buffer_scale(struct wl_client *c, struct wl_resource *r, int32_t s) {}
|
||||
static void surface_damage_buffer(struct wl_client *c, struct wl_resource *r,
|
||||
int32_t x, int32_t y, int32_t w, int32_t h) {}
|
||||
int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
struct surface *s = wl_resource_get_user_data(r);
|
||||
note_damage(&s->pending_damage_buf, x, y, w, h);
|
||||
}
|
||||
static void surface_offset(struct wl_client *c, struct wl_resource *r, int32_t x, int32_t y) {}
|
||||
|
||||
static const struct wl_surface_interface surface_impl = {
|
||||
@@ -1091,6 +1223,7 @@ static void surface_resource_destroy(struct wl_resource *r) {
|
||||
droiddeck_text_input_surface_gone(r);
|
||||
if (g_desktop == s) { g_desktop = NULL; droiddeck_log("desktop", "the desktop closed"); }
|
||||
if (g_hud_surface == s) { g_hud_surface = NULL; droiddeck_on_game_surface(NULL, NULL); }
|
||||
if (g_hud_shm == s) g_hud_shm = NULL;
|
||||
constraints_surface_gone(s);
|
||||
|
||||
unmap_toplevel(s);
|
||||
@@ -1845,7 +1978,121 @@ static void hdr_count_copied(const struct draw_list *dl, const char *reason) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Stream mode (the Xbox Cloud Gaming native player, in its own process): no Wayland clients, the scene is one
|
||||
* picture handed over as AHardwareBuffers (ImageReader images of the decoded video). Each buffer is imported once
|
||||
* and kept while the reader cycles through its few buffers; the newest frame is the scene, so the screen effects
|
||||
* and frame generation apply to it as to a game.
|
||||
*/
|
||||
#define STREAM_SLOTS 6
|
||||
static pthread_mutex_t g_stream_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static atomic_int g_stream_on;
|
||||
static AHardwareBuffer *g_stream_pending;
|
||||
static struct { AHardwareBuffer *ahb; struct vkp_image *img; uint64_t used; } g_stream_slots[STREAM_SLOTS];
|
||||
static struct vkp_image *g_stream_img;
|
||||
static uint64_t g_stream_seq;
|
||||
static atomic_uint g_stream_frames;
|
||||
|
||||
static void stream_forget_all(void) {
|
||||
vkp_wait_idle();
|
||||
for (int i = 0; i < STREAM_SLOTS; i++) {
|
||||
if (g_stream_slots[i].img) vkp_image_destroy(g_stream_slots[i].img);
|
||||
if (g_stream_slots[i].ahb) AHardwareBuffer_release(g_stream_slots[i].ahb);
|
||||
g_stream_slots[i].img = NULL; g_stream_slots[i].ahb = NULL; g_stream_slots[i].used = 0;
|
||||
}
|
||||
g_stream_img = NULL;
|
||||
}
|
||||
|
||||
/* Compositor thread: the newest handed-over buffer becomes the scene picture. */
|
||||
static void stream_take(void) {
|
||||
pthread_mutex_lock(&g_stream_lock);
|
||||
AHardwareBuffer *ahb = g_stream_pending;
|
||||
g_stream_pending = NULL;
|
||||
pthread_mutex_unlock(&g_stream_lock);
|
||||
if (!ahb) return;
|
||||
if (!atomic_load(&g_stream_on)) { AHardwareBuffer_release(ahb); return; }
|
||||
int slot = -1, oldest = 0;
|
||||
for (int i = 0; i < STREAM_SLOTS; i++) {
|
||||
if (g_stream_slots[i].ahb == ahb) { slot = i; break; }
|
||||
if (g_stream_slots[i].used < g_stream_slots[oldest].used) oldest = i;
|
||||
}
|
||||
if (slot >= 0) {
|
||||
AHardwareBuffer_release(ahb); /* the slot keeps its own reference */
|
||||
} else {
|
||||
struct vkp_image *img = vkp_image_from_ahb(ahb);
|
||||
if (!img) {
|
||||
static int said;
|
||||
if (!said) { said = 1; droiddeck_log("stream", "a decoded frame could not be imported: nothing to show"); }
|
||||
AHardwareBuffer_release(ahb);
|
||||
return;
|
||||
}
|
||||
droiddeck_log("stream", "decoded frame buffer imported (%dx%d)", vkp_image_width(img), vkp_image_height(img));
|
||||
slot = oldest;
|
||||
if (g_stream_slots[slot].img) {
|
||||
vkp_wait_idle(); /* the GPU may still read the picture being replaced */
|
||||
if (g_stream_img == g_stream_slots[slot].img) g_stream_img = NULL;
|
||||
vkp_image_destroy(g_stream_slots[slot].img);
|
||||
}
|
||||
if (g_stream_slots[slot].ahb) AHardwareBuffer_release(g_stream_slots[slot].ahb);
|
||||
g_stream_slots[slot].ahb = ahb;
|
||||
g_stream_slots[slot].img = img;
|
||||
}
|
||||
g_stream_slots[slot].used = ++g_stream_seq;
|
||||
g_stream_img = g_stream_slots[slot].img;
|
||||
atomic_fetch_add(&g_stream_frames, 1);
|
||||
schedule_render();
|
||||
}
|
||||
|
||||
static void render_stream(void) {
|
||||
g_dirty = 0;
|
||||
struct vkp_image *img = g_stream_img;
|
||||
if (!img) return;
|
||||
int w = vkp_image_width(img), h = vkp_image_height(img);
|
||||
if (w != g_scene_w || h != g_scene_h) {
|
||||
droiddeck_log("stream", "picture %dx%d", w, h);
|
||||
g_scene_w = w;
|
||||
g_scene_h = h;
|
||||
}
|
||||
struct vkp_draw d = {.img = img, .sx = 0, .sy = 0, .sw = (float)w, .sh = (float)h, .dx = 0, .dy = 0, .dw = w, .dh = h, .blend = 0};
|
||||
vkp_apply_window_request();
|
||||
sc_layer_hide();
|
||||
int r = vkp_render(w, h, &d, 1);
|
||||
if (r == 0) {
|
||||
g_stat_frames++;
|
||||
static int first;
|
||||
if (!first) { first = 1; droiddeck_log("stream", "first stream frame presented"); }
|
||||
} else {
|
||||
static int failed;
|
||||
if (failed++ % 600 == 0) droiddeck_log("stream", "presenting a stream frame failed (%d, %d times)", r, failed);
|
||||
}
|
||||
}
|
||||
|
||||
void droiddeck_stream_set(int on) {
|
||||
atomic_store(&g_stream_on, on ? 1 : 0);
|
||||
if (g_input_pipe[1] < 0) return;
|
||||
struct input_msg m = { 21, on ? 1 : 0, 0, 0, 0 };
|
||||
ssize_t n = write(g_input_pipe[1], &m, sizeof(m));
|
||||
(void)n;
|
||||
}
|
||||
|
||||
/* Any thread: hands over one decoded frame (acquires its own reference). */
|
||||
void droiddeck_stream_frame(AHardwareBuffer *ahb) {
|
||||
if (!ahb || !atomic_load(&g_stream_on) || g_input_pipe[1] < 0) return;
|
||||
AHardwareBuffer_acquire(ahb);
|
||||
pthread_mutex_lock(&g_stream_lock);
|
||||
AHardwareBuffer *dropped = g_stream_pending;
|
||||
g_stream_pending = ahb;
|
||||
pthread_mutex_unlock(&g_stream_lock);
|
||||
if (dropped) { AHardwareBuffer_release(dropped); return; } /* a wake-up is already queued */
|
||||
struct input_msg m = { 20, 0, 0, 0, 0 };
|
||||
ssize_t n = write(g_input_pipe[1], &m, sizeof(m));
|
||||
(void)n;
|
||||
}
|
||||
|
||||
unsigned droiddeck_stream_frames(void) { return atomic_load(&g_stream_frames); }
|
||||
|
||||
static void render_scene(void) {
|
||||
if (atomic_load(&g_stream_on)) { render_stream(); return; }
|
||||
struct draw_list dl = {0};
|
||||
struct surface *s;
|
||||
int w, h;
|
||||
@@ -1986,6 +2233,10 @@ static void render_scene(void) {
|
||||
if (rendered) {
|
||||
int64_t t = presented_ns(t_scene);
|
||||
g_stat_frames++;
|
||||
if (g_hud_new) {
|
||||
g_hud_new = 0;
|
||||
hud_frame_shown(t);
|
||||
}
|
||||
wl_list_for_each(s, &g_surfaces, link) {
|
||||
if (s->drawn) feedback_present_all(&s->feedback, t);
|
||||
else feedback_discard_all(&s->feedback);
|
||||
@@ -3005,6 +3256,8 @@ static int on_input_readable(int fd, uint32_t mask, void *data) {
|
||||
break;
|
||||
case 10: break;
|
||||
case 11: axis_event(m.p1, m.p2); break;
|
||||
case 20: stream_take(); break;
|
||||
case 21: if (!m.p1) stream_forget_all(); schedule_render(); break;
|
||||
default: deliver_pointer(&m); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
X(BindImageMemory) X(DestroyImage) X(FreeMemory) X(AcquireNextImageKHR) \
|
||||
X(ResetCommandBuffer) X(BeginCommandBuffer) X(CmdPipelineBarrier) X(CmdBlitImage) \
|
||||
X(EndCommandBuffer) X(ResetFences) X(QueueSubmit) X(QueuePresentKHR) X(WaitForFences) \
|
||||
X(QueueWaitIdle) X(GetMemoryFdPropertiesKHR) \
|
||||
X(QueueWaitIdle) X(GetMemoryFdPropertiesKHR) X(GetAndroidHardwareBufferPropertiesANDROID) \
|
||||
X(GetImageSubresourceLayout) X(MapMemory) X(UnmapMemory) X(CmdClearColorImage) \
|
||||
\
|
||||
X(CreateShaderModule) X(DestroyShaderModule) X(CreatePipelineLayout) X(DestroyPipelineLayout) \
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include <android/log.h>
|
||||
#include <android/hardware_buffer.h>
|
||||
|
||||
#define TAG "DroidDeckWayland"
|
||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
|
||||
@@ -39,6 +40,7 @@ struct vkp_image {
|
||||
};
|
||||
|
||||
static ANativeWindow *g_window;
|
||||
static int g_ahb_import;
|
||||
static char *g_driver_path, *g_library_name, *g_native_lib_dir;
|
||||
static int g_dev_state;
|
||||
static VkInstance g_inst;
|
||||
@@ -459,17 +461,19 @@ static int dev_init(void) {
|
||||
}
|
||||
g_vk.GetPhysicalDeviceMemoryProperties(g_pd, &g_memprops);
|
||||
|
||||
const char *dev_exts[7] = {VK_KHR_SWAPCHAIN_EXTENSION_NAME, "VK_KHR_external_memory_fd",
|
||||
"VK_EXT_external_memory_dma_buf", "VK_EXT_image_drm_format_modifier",
|
||||
"VK_KHR_image_format_list", NULL, NULL};
|
||||
uint32_t n_dev_exts = 5;
|
||||
static const char *const import_exts[4] = {"VK_KHR_external_memory_fd", "VK_EXT_external_memory_dma_buf",
|
||||
"VK_EXT_image_drm_format_modifier", "VK_KHR_image_format_list"};
|
||||
const char *dev_exts[9] = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
|
||||
uint32_t n_dev_exts = 1;
|
||||
uint32_t ne = 0;
|
||||
g_vk.EnumerateDeviceExtensionProperties(g_pd, NULL, &ne, NULL);
|
||||
VkExtensionProperties *exts = calloc(ne ? ne : 1, sizeof(*exts));
|
||||
g_vk.EnumerateDeviceExtensionProperties(g_pd, NULL, &ne, exts);
|
||||
for (unsigned i = 0; i < 5; i++)
|
||||
if (!has_ext(exts, ne, dev_exts[i]))
|
||||
LOGE("present: driver MISSING %s (dmabuf import will fail)", dev_exts[i]);
|
||||
/* Only what the driver has: the system driver lacks the dma-buf ones, which the cloud player never needs. */
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
if (has_ext(exts, ne, import_exts[i])) dev_exts[n_dev_exts++] = import_exts[i];
|
||||
else LOGE("present: driver MISSING %s (dmabuf import will fail)", import_exts[i]);
|
||||
}
|
||||
int want_hdr_md = 0;
|
||||
if (droiddeck_color_requested()) {
|
||||
want_hdr_md = has_ext(exts, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME);
|
||||
@@ -477,6 +481,13 @@ static int dev_init(void) {
|
||||
}
|
||||
const int want_sem_fd = has_ext(exts, ne, VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME);
|
||||
if (want_sem_fd) dev_exts[n_dev_exts++] = VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME;
|
||||
/* The cloud player's decoded frames arrive as AHardwareBuffers (vkp_image_from_ahb). */
|
||||
g_ahb_import = has_ext(exts, ne, VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME) &&
|
||||
has_ext(exts, ne, VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME);
|
||||
if (g_ahb_import) {
|
||||
dev_exts[n_dev_exts++] = VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME;
|
||||
dev_exts[n_dev_exts++] = VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME;
|
||||
}
|
||||
free(exts);
|
||||
|
||||
float prio = 1.0f;
|
||||
@@ -987,6 +998,60 @@ struct vkp_image *vkp_image_import_dmabuf(int fd, uint32_t drm_format, uint64_t
|
||||
return img;
|
||||
}
|
||||
|
||||
void vkp_wait_idle(void) {
|
||||
if (g_dev && g_vk.DeviceWaitIdle) g_vk.DeviceWaitIdle(g_dev);
|
||||
}
|
||||
|
||||
struct vkp_image *vkp_image_from_ahb(AHardwareBuffer *ahb) {
|
||||
if (!ahb || dev_init() != 0) return NULL;
|
||||
if (!g_ahb_import || !g_vk.GetAndroidHardwareBufferPropertiesANDROID) {
|
||||
static int said;
|
||||
if (!said) { said = 1; LOGE("ahb: the driver cannot import AHardwareBuffers (VK_ANDROID_external_memory_android_hardware_buffer)"); }
|
||||
return NULL;
|
||||
}
|
||||
AHardwareBuffer_Desc desc;
|
||||
AHardwareBuffer_describe(ahb, &desc);
|
||||
VkAndroidHardwareBufferFormatPropertiesANDROID fp = {
|
||||
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID};
|
||||
VkAndroidHardwareBufferPropertiesANDROID props = {
|
||||
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID, .pNext = &fp};
|
||||
VkResult pr = g_vk.GetAndroidHardwareBufferPropertiesANDROID(g_dev, ahb, &props);
|
||||
if (pr != VK_SUCCESS || fp.format == VK_FORMAT_UNDEFINED) {
|
||||
LOGE("ahb: %ux%u format %u: properties %s, Vulkan format %d", desc.width, desc.height, desc.format,
|
||||
vk_result_name(pr), (int)fp.format);
|
||||
return NULL;
|
||||
}
|
||||
struct vkp_image *img = calloc(1, sizeof(*img));
|
||||
if (!img) return NULL;
|
||||
img->w = (int)desc.width; img->h = (int)desc.height; img->dmabuf = 1; img->fmt = fp.format; img->sampled = 1;
|
||||
VkExternalMemoryImageCreateInfo ext = {
|
||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
|
||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID};
|
||||
VkImageCreateInfo ici = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .pNext = &ext,
|
||||
.imageType = VK_IMAGE_TYPE_2D, .format = fp.format, .extent = {desc.width, desc.height, 1},
|
||||
.mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL,
|
||||
.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
|
||||
VkResult cr = g_vk.CreateImage(g_dev, &ici, NULL, &img->image);
|
||||
if (cr != VK_SUCCESS) { LOGE("ahb: vkCreateImage %ux%u -> %s", desc.width, desc.height, vk_result_name(cr)); free(img); return NULL; }
|
||||
int idx = -1;
|
||||
for (int i = 0; i < 32; i++) if (props.memoryTypeBits & (1u << i)) { idx = i; break; }
|
||||
VkImportAndroidHardwareBufferInfoANDROID imp = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID, .buffer = ahb};
|
||||
VkMemoryDedicatedAllocateInfo ded = {.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
|
||||
.pNext = &imp, .image = img->image};
|
||||
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .pNext = &ded,
|
||||
.allocationSize = props.allocationSize, .memoryTypeIndex = (uint32_t)(idx < 0 ? 0 : idx)};
|
||||
VkResult mr = idx < 0 ? VK_ERROR_FEATURE_NOT_PRESENT : g_vk.AllocateMemory(g_dev, &mai, NULL, &img->mem);
|
||||
if (mr != VK_SUCCESS || g_vk.BindImageMemory(g_dev, img->image, img->mem, 0) != VK_SUCCESS) {
|
||||
LOGE("ahb: importing %ux%u -> %s", desc.width, desc.height, vk_result_name(mr));
|
||||
if (img->mem) g_vk.FreeMemory(g_dev, img->mem, NULL);
|
||||
g_vk.DestroyImage(g_dev, img->image, NULL); free(img); return NULL;
|
||||
}
|
||||
return img;
|
||||
}
|
||||
|
||||
static int shm_sampled(void) {
|
||||
static int known;
|
||||
if (!known) {
|
||||
|
||||
@@ -34,6 +34,9 @@ struct vkp_image *vkp_image_import_dmabuf(int fd, uint32_t drm_format, uint64_t
|
||||
int vkp_image_is_dmabuf(const struct vkp_image *img);
|
||||
|
||||
struct vkp_image *vkp_image_create_shm(int w, int h);
|
||||
struct AHardwareBuffer;
|
||||
struct vkp_image *vkp_image_from_ahb(struct AHardwareBuffer *ahb);
|
||||
void vkp_wait_idle(void);
|
||||
void vkp_image_upload_shm(struct vkp_image *img, const void *data, int stride);
|
||||
|
||||
int vkp_image_width(const struct vkp_image *img);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <sys/resource.h>
|
||||
#include <android/log.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <android/hardware_buffer_jni.h>
|
||||
#include "vk_present.h"
|
||||
#include "droiddeck_ext.h"
|
||||
#include "ahb_swapchain.h"
|
||||
@@ -24,12 +25,17 @@ extern void droiddeck_wayland_send_pointer(int action, int x, int y);
|
||||
extern void droiddeck_wayland_send_touch(int action, int pointer_id, int x, int y);
|
||||
extern int droiddeck_cursor_snapshot(int *out, int cap);
|
||||
extern int droiddeck_cursor_serial(void);
|
||||
extern int droiddeck_hud_frame_times(float *out, int len, int64_t *total);
|
||||
extern void droiddeck_wayland_send_key(int evdev, int state);
|
||||
extern void droiddeck_wayland_send_scene_input(int type, int a, int b);
|
||||
extern void droiddeck_wayland_vsync(int64_t frame_time_ns);
|
||||
extern void droiddeck_stream_set(int on);
|
||||
extern void droiddeck_stream_frame(AHardwareBuffer *ahb);
|
||||
extern unsigned droiddeck_stream_frames(void);
|
||||
extern void droiddeck_wayland_output_resized(void);
|
||||
extern void droiddeck_wayland_set_output_refresh_rate(int mhz);
|
||||
extern volatile int g_fps_limit;
|
||||
extern volatile int g_release_unpaced;
|
||||
extern volatile int g_hide_shell;
|
||||
extern volatile int g_zero_copy;
|
||||
extern volatile unsigned g_zero_copy_last;
|
||||
@@ -263,6 +269,20 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeCursorSnapshot(
|
||||
return n;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeHudFrameTimes(JNIEnv *env, jclass clazz, jfloatArray out) {
|
||||
(void)clazz;
|
||||
if (!out) return 0;
|
||||
jint cap = (*env)->GetArrayLength(env, out);
|
||||
float *buf = cap > 0 ? malloc(sizeof(float) * (size_t)cap) : NULL;
|
||||
if (!buf) return 0;
|
||||
int64_t total = 0;
|
||||
int n = droiddeck_hud_frame_times(buf, cap, &total);
|
||||
if (n > 0) (*env)->SetFloatArrayRegion(env, out, 0, n, buf);
|
||||
free(buf);
|
||||
return (jlong)total;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeCursorSerial(JNIEnv *env, jclass clazz) {
|
||||
(void)env; (void)clazz;
|
||||
@@ -570,6 +590,7 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetLookName(JNIEnv *
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetFpsLimit(JNIEnv *env, jclass clazz, jint fps) {
|
||||
g_fps_limit = fps > 0 ? fps : 0;
|
||||
g_release_unpaced = fps < 0;
|
||||
__android_log_print(ANDROID_LOG_INFO, TAG, "fps limit %d", fps);
|
||||
}
|
||||
|
||||
@@ -667,3 +688,21 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetCardboardSpatialP
|
||||
(*env)->GetFloatArrayRegion(env, pos, 0, 3, p);
|
||||
vkp_cardboard_set_spatial_pose(m, p, live ? 1 : 0);
|
||||
}
|
||||
|
||||
/* The Xbox Cloud Gaming native player: its decoded frames are the whole scene (no Wayland clients). */
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeStreamMode(JNIEnv *env, jclass clazz, jboolean on) {
|
||||
droiddeck_stream_set(on ? 1 : 0);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeStreamFrame(JNIEnv *env, jclass clazz, jobject hardwareBuffer) {
|
||||
if (!hardwareBuffer) return;
|
||||
AHardwareBuffer *ahb = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
|
||||
if (ahb) droiddeck_stream_frame(ahb);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeStreamFrames(JNIEnv *env, jclass clazz) {
|
||||
return (jint)droiddeck_stream_frames();
|
||||
}
|
||||
Reference in new issue
Block a user