2020-04-22 06:10:27 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2020-06-02 17:58:19 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Settings;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.SurfaceFlinger.Types;
|
2020-05-15 03:30:08 +02:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Time.Clock;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger
|
|
|
|
|
{
|
|
|
|
|
class BufferQueueProducer : IGraphicBufferProducer
|
|
|
|
|
{
|
|
|
|
|
public BufferQueueCore Core { get; }
|
|
|
|
|
|
|
|
|
|
private uint _stickyTransform;
|
|
|
|
|
|
|
|
|
|
private uint _nextCallbackTicket;
|
|
|
|
|
private uint _currentCallbackTicket;
|
|
|
|
|
private uint _callbackTicket;
|
|
|
|
|
|
|
|
|
|
private readonly object _callbackLock = new object();
|
|
|
|
|
|
|
|
|
|
public BufferQueueProducer(BufferQueueCore core)
|
|
|
|
|
{
|
|
|
|
|
Core = core;
|
|
|
|
|
|
|
|
|
|
_stickyTransform = 0;
|
|
|
|
|
_callbackTicket = 0;
|
|
|
|
|
_nextCallbackTicket = 0;
|
|
|
|
|
_currentCallbackTicket = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status RequestBuffer(int slot, out AndroidStrongPointer<GraphicBuffer> graphicBuffer)
|
|
|
|
|
{
|
|
|
|
|
graphicBuffer = new AndroidStrongPointer<GraphicBuffer>();
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slot < 0 || slot >= Core.Slots.Length || !Core.IsOwnedByProducerLocked(slot))
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graphicBuffer.Set(Core.Slots[slot].GraphicBuffer);
|
|
|
|
|
|
|
|
|
|
Core.Slots[slot].RequestBufferCalled = true;
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status SetBufferCount(int bufferCount)
|
|
|
|
|
{
|
|
|
|
|
IConsumerListener listener = null;
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bufferCount > BufferSlotArray.NumBufferSlots)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int slot = 0; slot < Core.Slots.Length; slot++)
|
|
|
|
|
{
|
|
|
|
|
if (Core.Slots[slot].BufferState == BufferState.Dequeued)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bufferCount == 0)
|
|
|
|
|
{
|
|
|
|
|
Core.OverrideMaxBufferCount = 0;
|
|
|
|
|
Core.SignalDequeueEvent();
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int minBufferSlots = Core.GetMinMaxBufferCountLocked(false);
|
|
|
|
|
|
|
|
|
|
if (bufferCount < minBufferSlots)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
int preallocatedBufferCount = GetPreallocatedBufferCountLocked();
|
|
|
|
|
|
|
|
|
|
if (preallocatedBufferCount <= 0)
|
|
|
|
|
{
|
|
|
|
|
Core.Queue.Clear();
|
|
|
|
|
Core.FreeAllBuffersLocked();
|
|
|
|
|
}
|
|
|
|
|
else if (preallocatedBufferCount < bufferCount)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, "Not enough buffers. Try with more pre-allocated buffers");
|
2020-06-02 17:58:19 +02:00
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.OverrideMaxBufferCount = bufferCount;
|
2020-06-02 17:58:19 +02:00
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.SignalDequeueEvent();
|
2020-05-15 03:30:08 +02:00
|
|
|
|
Core.SignalWaitBufferFreeEvent();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
listener = Core.ConsumerListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listener?.OnBuffersReleased();
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status DequeueBuffer(out int slot,
|
|
|
|
|
out AndroidFence fence,
|
|
|
|
|
bool async,
|
|
|
|
|
uint width,
|
|
|
|
|
uint height,
|
|
|
|
|
PixelFormat format,
|
|
|
|
|
uint usage)
|
|
|
|
|
{
|
|
|
|
|
if ((width == 0 && height != 0) || (height == 0 && width != 0))
|
|
|
|
|
{
|
|
|
|
|
slot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Status returnFlags = Status.Success;
|
|
|
|
|
|
|
|
|
|
bool attachedByConsumer = false;
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (format == PixelFormat.Unknown)
|
|
|
|
|
{
|
|
|
|
|
format = Core.DefaultBufferFormat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usage |= Core.ConsumerUsageBits;
|
|
|
|
|
|
|
|
|
|
Status status = WaitForFreeSlotThenRelock(async, out slot, out returnFlags);
|
|
|
|
|
|
|
|
|
|
if (status != Status.Success)
|
|
|
|
|
{
|
|
|
|
|
slot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slot == BufferSlotArray.InvalidBufferSlot)
|
|
|
|
|
{
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, "No available buffer slots");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return Status.Busy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachedByConsumer = Core.Slots[slot].AttachedByConsumer;
|
|
|
|
|
|
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
|
{
|
|
|
|
|
width = (uint)Core.DefaultWidth;
|
|
|
|
|
height = (uint)Core.DefaultHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GraphicBuffer graphicBuffer = Core.Slots[slot].GraphicBuffer.Object;
|
|
|
|
|
|
|
|
|
|
if (Core.Slots[slot].GraphicBuffer.IsNull
|
|
|
|
|
|| graphicBuffer.Width != width
|
|
|
|
|
|| graphicBuffer.Height != height
|
|
|
|
|
|| graphicBuffer.Format != format
|
|
|
|
|
|| (graphicBuffer.Usage & usage) != usage)
|
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
if (!Core.Slots[slot].IsPreallocated)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
|
|
|
|
slot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return Status.NoMemory;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger,
|
|
|
|
|
$"Preallocated buffer mismatch - slot {slot}\n" +
|
|
|
|
|
$"available: Width = {graphicBuffer.Width} Height = {graphicBuffer.Height} Format = {graphicBuffer.Format} Usage = {graphicBuffer.Usage:x} " +
|
|
|
|
|
$"requested: Width = {width} Height = {height} Format = {format} Usage = {usage:x}");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
slot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Dequeued;
|
|
|
|
|
|
|
|
|
|
Core.UpdateMaxBufferCountCachedLocked(slot);
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
fence = Core.Slots[slot].Fence;
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.Slots[slot].Fence = AndroidFence.NoFence;
|
|
|
|
|
Core.Slots[slot].QueueTime = TimeSpanType.Zero;
|
|
|
|
|
Core.Slots[slot].PresentationTime = TimeSpanType.Zero;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
Core.CheckSystemEventsLocked(Core.GetMaxBufferCountLocked(async));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attachedByConsumer)
|
|
|
|
|
{
|
|
|
|
|
returnFlags |= Status.BufferNeedsReallocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status DetachBuffer(int slot)
|
|
|
|
|
{
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slot < 0 || slot >= Core.Slots.Length || !Core.IsOwnedByProducerLocked(slot))
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Core.Slots[slot].RequestBufferCalled)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Slot {slot} was detached without requesting a buffer");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.FreeBufferLocked(slot);
|
|
|
|
|
Core.SignalDequeueEvent();
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status DetachNextBuffer(out AndroidStrongPointer<GraphicBuffer> graphicBuffer, out AndroidFence fence)
|
|
|
|
|
{
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
Core.WaitWhileAllocatingLocked();
|
|
|
|
|
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
graphicBuffer = default;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nextBufferSlot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
|
|
|
|
|
for (int slot = 0; slot < Core.Slots.Length; slot++)
|
|
|
|
|
{
|
|
|
|
|
if (Core.Slots[slot].BufferState == BufferState.Free && !Core.Slots[slot].GraphicBuffer.IsNull)
|
|
|
|
|
{
|
|
|
|
|
if (nextBufferSlot == BufferSlotArray.InvalidBufferSlot || Core.Slots[slot].FrameNumber < Core.Slots[nextBufferSlot].FrameNumber)
|
|
|
|
|
{
|
|
|
|
|
nextBufferSlot = slot;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nextBufferSlot == BufferSlotArray.InvalidBufferSlot)
|
|
|
|
|
{
|
|
|
|
|
graphicBuffer = default;
|
|
|
|
|
fence = AndroidFence.NoFence;
|
|
|
|
|
|
|
|
|
|
return Status.NoMemory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graphicBuffer = Core.Slots[nextBufferSlot].GraphicBuffer;
|
|
|
|
|
fence = Core.Slots[nextBufferSlot].Fence;
|
|
|
|
|
|
|
|
|
|
Core.FreeBufferLocked(nextBufferSlot);
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status AttachBuffer(out int slot, AndroidStrongPointer<GraphicBuffer> graphicBuffer)
|
|
|
|
|
{
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.WaitWhileAllocatingLocked();
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Status status = WaitForFreeSlotThenRelock(false, out slot, out Status returnFlags);
|
|
|
|
|
|
|
|
|
|
if (status != Status.Success)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slot == BufferSlotArray.InvalidBufferSlot)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, "No available buffer slots");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return Status.Busy;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.UpdateMaxBufferCountCachedLocked(slot);
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.Slots[slot].GraphicBuffer.Set(graphicBuffer);
|
|
|
|
|
|
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Dequeued;
|
|
|
|
|
Core.Slots[slot].Fence = AndroidFence.NoFence;
|
|
|
|
|
Core.Slots[slot].RequestBufferCalled = true;
|
|
|
|
|
|
|
|
|
|
return returnFlags;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status QueueBuffer(int slot, ref QueueBufferInput input, out QueueBufferOutput output)
|
|
|
|
|
{
|
|
|
|
|
output = default;
|
|
|
|
|
|
|
|
|
|
switch (input.ScalingMode)
|
|
|
|
|
{
|
|
|
|
|
case NativeWindowScalingMode.Freeze:
|
|
|
|
|
case NativeWindowScalingMode.ScaleToWindow:
|
|
|
|
|
case NativeWindowScalingMode.ScaleCrop:
|
|
|
|
|
case NativeWindowScalingMode.Unknown:
|
|
|
|
|
case NativeWindowScalingMode.NoScaleCrop:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferItem item = new BufferItem();
|
|
|
|
|
|
|
|
|
|
IConsumerListener frameAvailableListener = null;
|
|
|
|
|
IConsumerListener frameReplaceListener = null;
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxBufferCount = Core.GetMaxBufferCountLocked(input.Async != 0);
|
|
|
|
|
|
|
|
|
|
if (input.Async != 0 && Core.OverrideMaxBufferCount != 0 && Core.OverrideMaxBufferCount < maxBufferCount)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slot < 0 || slot >= Core.Slots.Length || !Core.IsOwnedByProducerLocked(slot))
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Core.Slots[slot].RequestBufferCalled)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Slot {slot} was queued without requesting a buffer");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
input.Crop.Intersect(Core.Slots[slot].GraphicBuffer.Object.ToRect(), out Rect croppedRect);
|
|
|
|
|
|
|
|
|
|
if (croppedRect != input.Crop)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.Slots[slot].Fence = input.Fence;
|
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Queued;
|
|
|
|
|
Core.FrameCounter++;
|
2020-05-15 03:30:08 +02:00
|
|
|
|
Core.Slots[slot].FrameNumber = Core.FrameCounter;
|
|
|
|
|
Core.Slots[slot].QueueTime = TimeSpanType.FromTimeSpan(ARMeilleure.State.ExecutionContext.ElapsedTime);
|
|
|
|
|
Core.Slots[slot].PresentationTime = TimeSpanType.Zero;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
item.AcquireCalled = Core.Slots[slot].AcquireCalled;
|
|
|
|
|
item.Crop = input.Crop;
|
|
|
|
|
item.Transform = input.Transform;
|
|
|
|
|
item.TransformToDisplayInverse = (input.Transform & NativeWindowTransform.InverseDisplay) == NativeWindowTransform.InverseDisplay;
|
|
|
|
|
item.ScalingMode = input.ScalingMode;
|
|
|
|
|
item.Timestamp = input.Timestamp;
|
|
|
|
|
item.IsAutoTimestamp = input.IsAutoTimestamp != 0;
|
|
|
|
|
item.SwapInterval = input.SwapInterval;
|
|
|
|
|
item.FrameNumber = Core.FrameCounter;
|
|
|
|
|
item.Slot = slot;
|
|
|
|
|
item.Fence = input.Fence;
|
|
|
|
|
item.IsDroppable = Core.DequeueBufferCannotBlock || input.Async != 0;
|
|
|
|
|
|
|
|
|
|
item.GraphicBuffer.Set(Core.Slots[slot].GraphicBuffer);
|
|
|
|
|
item.GraphicBuffer.Object.IncrementNvMapHandleRefCount(Core.Owner);
|
|
|
|
|
|
2020-05-15 03:30:08 +02:00
|
|
|
|
Core.BufferHistoryPosition = (Core.BufferHistoryPosition + 1) % BufferQueueCore.BufferHistoryArraySize;
|
|
|
|
|
|
|
|
|
|
Core.BufferHistory[Core.BufferHistoryPosition] = new BufferInfo
|
|
|
|
|
{
|
|
|
|
|
FrameNumber = Core.FrameCounter,
|
|
|
|
|
QueueTime = Core.Slots[slot].QueueTime,
|
|
|
|
|
State = BufferState.Queued
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
_stickyTransform = input.StickyTransform;
|
|
|
|
|
|
|
|
|
|
if (Core.Queue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Core.Queue.Add(item);
|
|
|
|
|
|
|
|
|
|
frameAvailableListener = Core.ConsumerListener;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
BufferItem frontItem = Core.Queue[0];
|
|
|
|
|
|
|
|
|
|
if (frontItem.IsDroppable)
|
|
|
|
|
{
|
|
|
|
|
if (Core.StillTracking(ref frontItem))
|
|
|
|
|
{
|
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Free;
|
|
|
|
|
Core.Slots[slot].FrameNumber = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.Queue.RemoveAt(0);
|
|
|
|
|
Core.Queue.Insert(0, item);
|
|
|
|
|
|
|
|
|
|
frameReplaceListener = Core.ConsumerListener;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Core.Queue.Add(item);
|
|
|
|
|
|
|
|
|
|
frameAvailableListener = Core.ConsumerListener;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.BufferHasBeenQueued = true;
|
|
|
|
|
Core.SignalDequeueEvent();
|
|
|
|
|
|
|
|
|
|
Core.CheckSystemEventsLocked(maxBufferCount);
|
|
|
|
|
|
|
|
|
|
output = new QueueBufferOutput
|
|
|
|
|
{
|
|
|
|
|
Width = (uint)Core.DefaultWidth,
|
|
|
|
|
Height = (uint)Core.DefaultHeight,
|
|
|
|
|
TransformHint = Core.TransformHint,
|
|
|
|
|
NumPendingBuffers = (uint)Core.Queue.Count
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
if ((input.StickyTransform & 8) != 0)
|
|
|
|
|
{
|
|
|
|
|
output.TransformHint |= NativeWindowTransform.ReturnFrameNumber;
|
|
|
|
|
output.FrameNumber = Core.Slots[slot].FrameNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
_callbackTicket = _nextCallbackTicket++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (_callbackLock)
|
|
|
|
|
{
|
|
|
|
|
while (_callbackTicket != _currentCallbackTicket)
|
|
|
|
|
{
|
|
|
|
|
Monitor.Wait(_callbackLock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
frameAvailableListener?.OnFrameAvailable(ref item);
|
|
|
|
|
frameReplaceListener?.OnFrameReplaced(ref item);
|
|
|
|
|
|
|
|
|
|
_currentCallbackTicket++;
|
|
|
|
|
|
|
|
|
|
Monitor.PulseAll(_callbackLock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void CancelBuffer(int slot, ref AndroidFence fence)
|
|
|
|
|
{
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned || slot < 0 || slot >= Core.Slots.Length || !Core.IsOwnedByProducerLocked(slot))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Free;
|
|
|
|
|
Core.Slots[slot].FrameNumber = 0;
|
|
|
|
|
Core.Slots[slot].Fence = fence;
|
|
|
|
|
Core.SignalDequeueEvent();
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.SignalWaitBufferFreeEvent();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status Query(NativeWindowAttribute what, out int outValue)
|
|
|
|
|
{
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
outValue = 0;
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (what)
|
|
|
|
|
{
|
|
|
|
|
case NativeWindowAttribute.Width:
|
|
|
|
|
outValue = Core.DefaultWidth;
|
|
|
|
|
return Status.Success;
|
|
|
|
|
case NativeWindowAttribute.Height:
|
|
|
|
|
outValue = Core.DefaultHeight;
|
|
|
|
|
return Status.Success;
|
|
|
|
|
case NativeWindowAttribute.Format:
|
|
|
|
|
outValue = (int)Core.DefaultBufferFormat;
|
|
|
|
|
return Status.Success;
|
|
|
|
|
case NativeWindowAttribute.MinUnqueuedBuffers:
|
|
|
|
|
outValue = Core.GetMinUndequeuedBufferCountLocked(false);
|
|
|
|
|
return Status.Success;
|
2020-05-15 03:30:08 +02:00
|
|
|
|
case NativeWindowAttribute.ConsumerRunningBehind:
|
|
|
|
|
outValue = Core.Queue.Count > 1 ? 1 : 0;
|
|
|
|
|
return Status.Success;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
case NativeWindowAttribute.ConsumerUsageBits:
|
|
|
|
|
outValue = (int)Core.ConsumerUsageBits;
|
|
|
|
|
return Status.Success;
|
|
|
|
|
case NativeWindowAttribute.MaxBufferCountAsync:
|
|
|
|
|
outValue = Core.GetMaxBufferCountLocked(true);
|
|
|
|
|
return Status.Success;
|
|
|
|
|
default:
|
|
|
|
|
outValue = 0;
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status Connect(IProducerListener listener, NativeWindowApi api, bool producerControlledByApp, out QueueBufferOutput output)
|
|
|
|
|
{
|
|
|
|
|
output = new QueueBufferOutput();
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned || Core.ConsumerListener == null)
|
|
|
|
|
{
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Core.ConnectedApi != NativeWindowApi.NoApi)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.BufferHasBeenQueued = false;
|
|
|
|
|
Core.DequeueBufferCannotBlock = Core.ConsumerControlledByApp && producerControlledByApp;
|
|
|
|
|
|
|
|
|
|
switch (api)
|
|
|
|
|
{
|
|
|
|
|
case NativeWindowApi.NVN:
|
|
|
|
|
case NativeWindowApi.CPU:
|
|
|
|
|
case NativeWindowApi.Media:
|
|
|
|
|
case NativeWindowApi.Camera:
|
|
|
|
|
Core.ProducerListener = listener;
|
|
|
|
|
Core.ConnectedApi = api;
|
|
|
|
|
|
|
|
|
|
output.Width = (uint)Core.DefaultWidth;
|
|
|
|
|
output.Height = (uint)Core.DefaultHeight;
|
|
|
|
|
output.TransformHint = Core.TransformHint;
|
|
|
|
|
output.NumPendingBuffers = (uint)Core.Queue.Count;
|
2020-06-02 17:58:19 +02:00
|
|
|
|
|
|
|
|
|
if (NxSettings.Settings.TryGetValue("nv!nvn_no_vsync_capability", out object noVSyncCapability) && (bool)noVSyncCapability)
|
|
|
|
|
{
|
|
|
|
|
output.TransformHint |= NativeWindowTransform.NoVSyncCapability;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
return Status.Success;
|
|
|
|
|
default:
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Status Disconnect(NativeWindowApi api)
|
|
|
|
|
{
|
|
|
|
|
IProducerListener producerListener = null;
|
|
|
|
|
|
|
|
|
|
Status status = Status.BadValue;
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.WaitWhileAllocatingLocked();
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (api)
|
|
|
|
|
{
|
|
|
|
|
case NativeWindowApi.NVN:
|
|
|
|
|
case NativeWindowApi.CPU:
|
|
|
|
|
case NativeWindowApi.Media:
|
|
|
|
|
case NativeWindowApi.Camera:
|
|
|
|
|
if (Core.ConnectedApi == api)
|
|
|
|
|
{
|
2020-05-15 03:30:08 +02:00
|
|
|
|
Core.Queue.Clear();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.FreeAllBuffersLocked();
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.SignalDequeueEvent();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
producerListener = Core.ProducerListener;
|
|
|
|
|
|
|
|
|
|
Core.ProducerListener = null;
|
|
|
|
|
Core.ConnectedApi = NativeWindowApi.NoApi;
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.SignalWaitBufferFreeEvent();
|
|
|
|
|
Core.SignalFrameAvailableEvent();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
status = Status.Success;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
producerListener?.OnBufferReleased();
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
private int GetPreallocatedBufferCountLocked()
|
|
|
|
|
{
|
|
|
|
|
int bufferCount = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Core.Slots.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Core.Slots[i].IsPreallocated)
|
|
|
|
|
{
|
|
|
|
|
bufferCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bufferCount;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
public override Status SetPreallocatedBuffer(int slot, AndroidStrongPointer<GraphicBuffer> graphicBuffer)
|
|
|
|
|
{
|
|
|
|
|
if (slot < 0 || slot >= Core.Slots.Length)
|
|
|
|
|
{
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
Core.Slots[slot].BufferState = BufferState.Free;
|
|
|
|
|
Core.Slots[slot].Fence = AndroidFence.NoFence;
|
|
|
|
|
Core.Slots[slot].RequestBufferCalled = false;
|
2020-04-22 21:18:53 +02:00
|
|
|
|
Core.Slots[slot].AcquireCalled = false;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.Slots[slot].NeedsCleanupOnRelease = false;
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.Slots[slot].IsPreallocated = !graphicBuffer.IsNull;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.Slots[slot].FrameNumber = 0;
|
|
|
|
|
|
|
|
|
|
Core.Slots[slot].GraphicBuffer.Set(graphicBuffer);
|
|
|
|
|
|
|
|
|
|
if (!Core.Slots[slot].GraphicBuffer.IsNull)
|
|
|
|
|
{
|
|
|
|
|
Core.Slots[slot].GraphicBuffer.Object.Buffer.Usage &= (int)Core.ConsumerUsageBits;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.OverrideMaxBufferCount = GetPreallocatedBufferCountLocked();
|
2020-04-25 12:08:50 +02:00
|
|
|
|
Core.UseAsyncBuffer = false;
|
|
|
|
|
|
2020-04-22 06:10:27 +02:00
|
|
|
|
if (!graphicBuffer.IsNull)
|
|
|
|
|
{
|
|
|
|
|
// NOTE: Nintendo set the default width, height and format from the GraphicBuffer..
|
|
|
|
|
// This is entirely wrong and should only be controlled by the consumer...
|
|
|
|
|
Core.DefaultWidth = graphicBuffer.Object.Width;
|
|
|
|
|
Core.DefaultHeight = graphicBuffer.Object.Height;
|
|
|
|
|
Core.DefaultBufferFormat = graphicBuffer.Object.Format;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
bool allBufferFreed = true;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Core.Slots.Length; i++)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
if (!Core.Slots[i].GraphicBuffer.IsNull)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
allBufferFreed = false;
|
2020-04-22 06:10:27 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
if (allBufferFreed)
|
|
|
|
|
{
|
|
|
|
|
Core.Queue.Clear();
|
|
|
|
|
Core.FreeAllBuffersLocked();
|
|
|
|
|
Core.SignalDequeueEvent();
|
|
|
|
|
Core.SignalWaitBufferFreeEvent();
|
|
|
|
|
Core.SignalFrameAvailableEvent();
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
Core.SignalDequeueEvent();
|
2020-04-22 06:10:27 +02:00
|
|
|
|
Core.SignalWaitBufferFreeEvent();
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Status WaitForFreeSlotThenRelock(bool async, out int freeSlot, out Status returnStatus)
|
|
|
|
|
{
|
|
|
|
|
bool tryAgain = true;
|
|
|
|
|
|
|
|
|
|
freeSlot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
returnStatus = Status.Success;
|
|
|
|
|
|
|
|
|
|
while (tryAgain)
|
|
|
|
|
{
|
|
|
|
|
if (Core.IsAbandoned)
|
|
|
|
|
{
|
|
|
|
|
freeSlot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
|
|
|
|
|
return Status.NoInit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxBufferCount = Core.GetMaxBufferCountLocked(async);
|
|
|
|
|
|
|
|
|
|
if (async && Core.OverrideMaxBufferCount != 0 && Core.OverrideMaxBufferCount < maxBufferCount)
|
|
|
|
|
{
|
|
|
|
|
freeSlot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 17:58:19 +02:00
|
|
|
|
|
|
|
|
|
if (maxBufferCount < Core.MaxBufferCountCached)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
for (int slot = maxBufferCount; slot < Core.MaxBufferCountCached; slot++)
|
2020-04-22 06:10:27 +02:00
|
|
|
|
{
|
2020-06-02 17:58:19 +02:00
|
|
|
|
if (Core.Slots[slot].BufferState == BufferState.Free && !Core.Slots[slot].GraphicBuffer.IsNull && !Core.Slots[slot].IsPreallocated)
|
|
|
|
|
{
|
|
|
|
|
Core.FreeBufferLocked(slot);
|
|
|
|
|
returnStatus |= Status.ReleaseAllBuffers;
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeSlot = BufferSlotArray.InvalidBufferSlot;
|
|
|
|
|
|
|
|
|
|
int dequeuedCount = 0;
|
|
|
|
|
int acquiredCount = 0;
|
|
|
|
|
|
|
|
|
|
for (int slot = 0; slot < maxBufferCount; slot++)
|
|
|
|
|
{
|
|
|
|
|
switch (Core.Slots[slot].BufferState)
|
|
|
|
|
{
|
|
|
|
|
case BufferState.Acquired:
|
|
|
|
|
acquiredCount++;
|
|
|
|
|
break;
|
|
|
|
|
case BufferState.Dequeued:
|
|
|
|
|
dequeuedCount++;
|
|
|
|
|
break;
|
|
|
|
|
case BufferState.Free:
|
|
|
|
|
if (freeSlot == BufferSlotArray.InvalidBufferSlot || Core.Slots[slot].FrameNumber < Core.Slots[freeSlot].FrameNumber)
|
|
|
|
|
{
|
|
|
|
|
freeSlot = slot;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The producer SHOULD call SetBufferCount otherwise it's not allowed to dequeue multiple buffers.
|
|
|
|
|
if (Core.OverrideMaxBufferCount == 0 && dequeuedCount > 0)
|
|
|
|
|
{
|
|
|
|
|
return Status.InvalidOperation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Core.BufferHasBeenQueued)
|
|
|
|
|
{
|
|
|
|
|
int newUndequeuedCount = maxBufferCount - (dequeuedCount + 1);
|
|
|
|
|
int minUndequeuedCount = Core.GetMinUndequeuedBufferCountLocked(async);
|
|
|
|
|
|
|
|
|
|
if (newUndequeuedCount < minUndequeuedCount)
|
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.SurfaceFlinger, $"Min undequeued buffer count ({minUndequeuedCount}) exceeded (dequeued = {dequeuedCount} undequeued = {newUndequeuedCount})");
|
2020-04-22 06:10:27 +02:00
|
|
|
|
|
|
|
|
|
return Status.InvalidOperation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool tooManyBuffers = Core.Queue.Count > maxBufferCount;
|
|
|
|
|
|
|
|
|
|
tryAgain = freeSlot == BufferSlotArray.InvalidBufferSlot || tooManyBuffers;
|
|
|
|
|
|
|
|
|
|
if (tryAgain)
|
|
|
|
|
{
|
|
|
|
|
if (async || (Core.DequeueBufferCannotBlock && acquiredCount < Core.MaxAcquiredBufferCount))
|
|
|
|
|
{
|
|
|
|
|
Core.CheckSystemEventsLocked(maxBufferCount);
|
|
|
|
|
|
|
|
|
|
return Status.WouldBlock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core.WaitDequeueEvent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override KReadableEvent GetWaitBufferFreeEvent()
|
|
|
|
|
{
|
|
|
|
|
return Core.GetWaitBufferFreeEvent();
|
|
|
|
|
}
|
2020-05-15 03:30:08 +02:00
|
|
|
|
|
|
|
|
|
public override Status GetBufferHistory(int bufferHistoryCount, out Span<BufferInfo> bufferInfos)
|
|
|
|
|
{
|
|
|
|
|
if (bufferHistoryCount <= 0)
|
|
|
|
|
{
|
|
|
|
|
bufferInfos = Span<BufferInfo>.Empty;
|
|
|
|
|
|
|
|
|
|
return Status.BadValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock (Core.Lock)
|
|
|
|
|
{
|
|
|
|
|
bufferHistoryCount = Math.Min(bufferHistoryCount, Core.BufferHistory.Length);
|
|
|
|
|
|
|
|
|
|
BufferInfo[] result = new BufferInfo[bufferHistoryCount];
|
|
|
|
|
|
|
|
|
|
uint position = Core.BufferHistoryPosition;
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < bufferHistoryCount; i++)
|
|
|
|
|
{
|
|
|
|
|
result[i] = Core.BufferHistory[(position - i) % Core.BufferHistory.Length];
|
|
|
|
|
|
|
|
|
|
position--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bufferInfos = result;
|
|
|
|
|
|
|
|
|
|
return Status.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 06:10:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|