hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.rockchip.gpadc.demo;
 
 
 
 
/**
 * Created by randall on 18-4-27.
 */
 
 
/*
* 一个简单的bufferqueue实现
*
* */
public class ImageBufferQueue {
    private ImageBuffer[] mQueueBuffer;
    private int mQueueBufferSize;
    private int mCurrentFreeBufferIndex;
    private int mCurrentUsingBufferIndex;
 
    public ImageBufferQueue(int bufferSize, int width, int height) {
        mCurrentFreeBufferIndex = -1;
        mCurrentUsingBufferIndex = -1;
        mQueueBufferSize = bufferSize;
        mQueueBuffer = new ImageBuffer[mQueueBufferSize];
 
        for (int i=0; i<mQueueBufferSize; ++i) {
            mQueueBuffer[i] = new ImageBuffer(width, height);
        }
    }
 
    public void release() {
        for (int i=0; i<mQueueBufferSize; ++i) {
            mQueueBuffer[i] = null;
        }
 
        mQueueBuffer = null;
    }
 
    //获取待处理的数据
    public synchronized ImageBuffer getReadyBuffer() {
 
        int index = mCurrentUsingBufferIndex;
 
        for (int i=0; i<mQueueBufferSize; ++i) {
            ++index;
 
            if (index >= mQueueBufferSize) {
                index = 0;
            }
 
            if (mQueueBuffer[index].mStatus == ImageBuffer.STATUS_READY) {
                break;
            }
        }
 
        if ((index != mCurrentUsingBufferIndex) && (mQueueBuffer[index].mStatus == ImageBuffer.STATUS_READY)) {
            mCurrentUsingBufferIndex = index;
            mQueueBuffer[index].mStatus = ImageBuffer.STATUS_USING;
 
            return mQueueBuffer[index];
        }
 
        return null;
    }
 
    public synchronized void releaseBuffer(ImageBuffer buffer) {
        buffer.mStatus = ImageBuffer.STATUS_INVAILD;
    }
 
    public  synchronized ImageBuffer getFreeBuffer() {
 
        int index = mCurrentFreeBufferIndex;
 
        for (int i=0; i<mQueueBufferSize; ++i) {
            ++index;
 
            if (index >= mQueueBufferSize) {
                index = 0;
            }
 
            if (mQueueBuffer[index].mStatus != ImageBuffer.STATUS_USING) {
                break;
            }
        }
 
        mCurrentFreeBufferIndex = index;
 
        mQueueBuffer[index].mStatus = ImageBuffer.STATUS_INVAILD;
        return mQueueBuffer[index];
    }
    public synchronized void postBuffer(ImageBuffer buffer) {
        buffer.mStatus = ImageBuffer.STATUS_READY;
    }
 
    public class ImageBuffer {
        static public final int STATUS_INVAILD = 0;
        static public final int STATUS_READY = 1;
        static public final int STATUS_USING = 2;
 
 
        public int mStatus;
        public int mWidth;
        public int mHeight;
        public int mBufferSize; //nv21
        public byte[] mImage;
 
 
        public ImageBuffer(int width, int height) {
            mStatus = STATUS_INVAILD;
            mWidth = width;
            mHeight = height;
            mBufferSize = mWidth * mHeight * 4;
            mImage = new byte[mBufferSize];
 
        }
 
        public void finalize() {
 
        }
 
    }
}