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