github.com/racerxdl/gonx@v0.0.0-20210103083128-c5afc43bcbd2/services/display/buffer.go (about)

     1  package display
     2  
     3  import (
     4  	"github.com/racerxdl/gonx/internal"
     5  	"github.com/racerxdl/gonx/nx/graphics"
     6  	"github.com/racerxdl/gonx/nx/nxerrors"
     7  	"github.com/racerxdl/gonx/nx/nxtypes"
     8  	"github.com/racerxdl/gonx/services/gpu"
     9  	"github.com/racerxdl/gonx/services/nv"
    10  	"github.com/racerxdl/gonx/services/vi"
    11  	"unsafe"
    12  )
    13  
    14  // CompositorTiming
    15  // https://source.android.com/reference/hidl/android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer#compositortiming
    16  type CompositorTiming struct {
    17  	DeadlineNanoseconds       int64
    18  	InternalNanoseconds       int64
    19  	PresentLatencyNanoseconds int64
    20  }
    21  
    22  // FrameEventHistoryDelta
    23  //
    24  // Not fully implemented, just enough that it works.
    25  // https://source.android.com/reference/hidl/android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer#frameeventhistorydelta
    26  type FrameEventHistoryDelta struct {
    27  	CompositorTiming CompositorTiming
    28  }
    29  
    30  // QueueBufferOutput Values received back from queueBuffer
    31  // https://source.android.com/reference/hidl/android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer#queuebufferoutput
    32  type QueueBufferOutput struct {
    33  	Width             uint32
    34  	Height            uint32
    35  	TransformHint     uint32
    36  	NumPendingBuffers uint32
    37  	NextFrameNumber   uint32
    38  	BufferReplaced    bool
    39  	FrameTimestamps   FrameEventHistoryDelta
    40  }
    41  
    42  // GraphicBuffer Graphics Buffer
    43  type GraphicBuffer struct {
    44  	Width             uint32
    45  	Height            uint32
    46  	Stride            uint32
    47  	Format            graphics.PixelFormat
    48  	Length            uint32
    49  	Usage             uint32
    50  	GPUBuffer         *gpu.Buffer
    51  	Index             int32
    52  	PixelBufferOffset uint32
    53  
    54  	NativeHandle *nxtypes.NativeHandle
    55  	GRBuff       *nv.GraphicBuffer
    56  }
    57  
    58  // QueueBufferInput Parameters passed to queueBuffer
    59  // https://source.android.com/reference/hidl/android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer#queuebufferinput
    60  type QueueBufferInput struct {
    61  	Size            uint32
    62  	NumFds          uint32
    63  	Timestamp       int64
    64  	IsAutoTimestamp int32
    65  	Crop            Rect
    66  	ScalingMode     int32
    67  	Transform       uint32
    68  	StickyTransform uint32
    69  	Unknown         [2]uint32
    70  	Fence           Fence
    71  }
    72  
    73  func UnflattenQueueBufferOutput(p *vi.Parcel) (qbo *QueueBufferOutput, err error) {
    74  	if p.Remaining() < 4*4 { // 4 uint32
    75  		return nil, nxerrors.ParcelDataUnderrun
    76  	}
    77  
    78  	qbo = &QueueBufferOutput{
    79  		Width:             p.ReadU32(),
    80  		Height:            p.ReadU32(),
    81  		TransformHint:     p.ReadU32(),
    82  		NumPendingBuffers: p.ReadU32(),
    83  	}
    84  
    85  	return qbo, nil
    86  }
    87  
    88  func (qbi *QueueBufferInput) Flatten(p *vi.Parcel) {
    89  	buff := make([]byte, unsafe.Sizeof(*qbi))
    90  	internal.Memcpy(unsafe.Pointer(&buff[0]), unsafe.Pointer(qbi), uintptr(len(buff)))
    91  	p.WriteInPlace(buff)
    92  }
    93  
    94  func (gb *GraphicBuffer) Flatten(p *vi.Parcel) error {
    95  	buffer := make([]uint32, 0)
    96  
    97  	buffer = append(buffer, 0x47424652) // GBFR (Graphic Buffer)
    98  	buffer = append(buffer, gb.Width)
    99  	buffer = append(buffer, gb.Height)
   100  	buffer = append(buffer, gb.Stride)
   101  
   102  	buffer = append(buffer, uint32(gb.Format))
   103  	buffer = append(buffer, gb.Usage)
   104  	buffer = append(buffer, 42)
   105  	buffer = append(buffer, 0)
   106  
   107  	buffer = append(buffer, 0)
   108  	buffer = append(buffer, uint32(gb.GRBuff.Header.NumInts))
   109  
   110  	buffer = append(buffer, uint32(gb.GRBuff.Unk0))
   111  	buffer = append(buffer, uint32(gb.GRBuff.NVMapID))
   112  	buffer = append(buffer, gb.GRBuff.Unk2)
   113  	buffer = append(buffer, gb.GRBuff.Magic)
   114  	buffer = append(buffer, gb.GRBuff.PID)
   115  	buffer = append(buffer, gb.GRBuff.Type)
   116  	buffer = append(buffer, gb.GRBuff.Usage)
   117  	buffer = append(buffer, gb.GRBuff.Format)
   118  	buffer = append(buffer, gb.GRBuff.ExtFormat)
   119  	buffer = append(buffer, gb.GRBuff.Stride)
   120  	buffer = append(buffer, gb.GRBuff.TotalSize)
   121  	buffer = append(buffer, gb.GRBuff.NumPlanes)
   122  	buffer = append(buffer, gb.GRBuff.Unk12)
   123  
   124  	planeSize := (uint32(unsafe.Sizeof(gb.GRBuff.Planes[0])) / 4) * 3
   125  	planeBuff := make([]uint32, planeSize)
   126  	internal.Memcpy(unsafe.Pointer(&planeBuff[0]), unsafe.Pointer(&gb.GRBuff.Planes), unsafe.Sizeof(gb.GRBuff.Planes))
   127  
   128  	for i := uint32(0); i < planeSize; i++ {
   129  		buffer = append(buffer, planeBuff[i])
   130  	}
   131  	buffer = append(buffer, 0)
   132  	buffer = append(buffer, 0)
   133  
   134  	p.WriteU32(uint32(len(buffer) * 4))
   135  	p.WriteU32(0)
   136  	p.WriteInPlaceU32(buffer)
   137  
   138  	return nil
   139  }