1/*
2 * Copyright (c) 2010, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * *  Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 *
12 * *  Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * *  Neither the name of Texas Instruments Incorporated nor the names of
17 *    its contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <stdlib.h>
34#include <unistd.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#include <time.h>
39#include <semaphore.h>
40#include <pthread.h>
41#include <string.h>
42#include <climits>
43
44#include <gui/GLConsumer.h>
45#include <gui/Surface.h>
46#include <ui/GraphicBuffer.h>
47#include <ui/GraphicBufferMapper.h>
48
49#include <camera/Camera.h>
50#include <camera/ICamera.h>
51#include <media/mediarecorder.h>
52
53#include <binder/IPCThreadState.h>
54#include <binder/ProcessState.h>
55#include <binder/IServiceManager.h>
56#include <cutils/properties.h>
57#include <camera/CameraParameters.h>
58#include <camera/ShotParameters.h>
59#include <camera/CameraMetadata.h>
60#include <system/audio.h>
61#include <system/camera.h>
62
63#include <cutils/memory.h>
64#include <utils/Log.h>
65
66#include <sys/wait.h>
67
68#include <sys/mman.h>
69
70#ifdef ANDROID_API_JB_OR_LATER
71#include <gui/Surface.h>
72#include <gui/ISurface.h>
73#include <gui/ISurfaceComposer.h>
74#include <gui/ISurfaceComposerClient.h>
75#include <gui/SurfaceComposerClient.h>
76#include <ion/ion.h>
77#else
78#include <surfaceflinger/Surface.h>
79#include <surfaceflinger/ISurface.h>
80#include <surfaceflinger/ISurfaceComposer.h>
81#include <surfaceflinger/ISurfaceComposerClient.h>
82#include <surfaceflinger/SurfaceComposerClient.h>
83#include "ion.h"
84#endif
85
86#include "camera_test.h"
87
88#define ASSERT(X) \
89    do { \
90       if(!(X)) { \
91           printf("error: %s():%d", __FUNCTION__, __LINE__); \
92           return; \
93       } \
94    } while(0);
95
96#define ALIGN_DOWN(x, n) ((x) & (~((n) - 1)))
97#define ALIGN_UP(x, n) ((((x) + (n) - 1)) & (~((n) - 1)))
98#define ALIGN_WIDTH 32 // Should be 32...but the calculated dimension causes an ion crash
99#define ALIGN_HEIGHT 2 // Should be 2...but the calculated dimension causes an ion crash
100
101//temporarily define format here
102#define HAL_PIXEL_FORMAT_TI_NV12 0x100
103#define HAL_PIXEL_FORMAT_TI_NV12_1D 0x102
104#define HAL_PIXEL_FORMAT_TI_Y8 0x103
105#define HAL_PIXEL_FORMAT_TI_Y16 0x104
106
107using namespace android;
108
109#define N_BUFFERS 15
110
111static void
112test_format (int format, int page_mode, int width, int height)
113{
114    sp<GLConsumer> st;
115    Surface *stc;
116    GLint tex_id = 0;
117    sp<ANativeWindow> anw;
118    ANativeWindowBuffer* anb[30] = { 0 };
119    int i;
120    unsigned int usage;
121    GraphicBufferMapper &mapper = GraphicBufferMapper::get();
122
123    printf("testing format %x, page_mode %d\n", format, page_mode);
124
125    sp<BufferQueue> bq = new BufferQueue();
126    st = new GLConsumer (bq, tex_id, GL_TEXTURE_EXTERNAL_OES);
127
128    st->setDefaultBufferSize (width, height);
129
130    stc = new Surface(st);
131    anw = stc;
132
133    usage = GRALLOC_USAGE_SW_READ_RARELY |
134            GRALLOC_USAGE_SW_WRITE_NEVER;
135    if (page_mode) {
136        usage |= GRALLOC_USAGE_PRIVATE_0;
137    }
138
139    native_window_set_usage(anw.get(), usage);
140    native_window_set_buffer_count(anw.get(), N_BUFFERS);
141    native_window_set_buffers_geometry(anw.get(),
142            width, height, format);
143
144    for(i=0;i<N_BUFFERS;i++) {
145        void *data = NULL;
146        Rect bounds(width, height);
147
148        anb[i] = NULL;
149        anw->dequeueBuffer(anw.get(), &anb[i]);
150        printf("%d: %p\n", i, anb[i]);
151        if (anb[i] == NULL) {
152            printf ("FAILED: buffer should be non-NULL\n");
153            break;
154        }
155
156        mapper.lock(anb[i]->handle, GRALLOC_USAGE_SW_READ_RARELY,
157                bounds, &data);
158        if (data == NULL) {
159            printf ("FAILED: mapping should be non-NULL\n");
160            break;
161        }
162
163        mapper.unlock(anb[i]->handle);
164    }
165    for(i=0;i<N_BUFFERS;i++) {
166        if (anb[i]) {
167            anw->cancelBuffer (anw.get(), anb[i]);
168        }
169    }
170
171    //delete stc;
172    st.clear();
173}
174
175void
176ion_test (void)
177{
178    struct ion_handle *handle;
179    int fd;
180    int map_fd;
181    unsigned char *ptr;
182    int i;
183    int ret;
184    int share_fd;
185
186    fd = ion_open ();
187
188#define SIZE (10*1024*1024)
189    for(i=0;i<10;i++){
190        handle = NULL;
191        ret = ion_alloc (fd, SIZE, 4096, (1<<0), &handle);
192        if (ret < 0) {
193            printf("ion_alloc returned error %d, %s\n", ret, strerror(errno));
194            break;
195        }
196        printf("ion_alloc returned %d\n", ret);
197
198        ret = ion_share (fd, handle, &share_fd);
199        if (ret < 0) {
200            printf("ion_share returned error %d, %s\n", ret, strerror(errno));
201            break;
202        }
203        printf("ion_share returned %d\n", ret);
204
205        ptr = (unsigned char *)mmap (NULL, SIZE, PROT_READ|PROT_WRITE,
206                MAP_SHARED, share_fd, 0);
207        printf("mmap returned %p\n", ptr);
208
209        ptr = (unsigned char *)mmap (NULL, SIZE, PROT_READ|PROT_WRITE,
210                MAP_SHARED, share_fd, 0);
211        printf("mmap returned %p\n", ptr);
212
213#if 0
214        ret = ion_map (fd, handle, SIZE, PROT_READ, 0, 0, &ptr, &map_fd);
215        if (ret < 0) {
216            printf("ion_map returned error %d, %s\n", ret, strerror(errno));
217            break;
218        }
219        printf("ion_map returned %d\n", ret);
220#endif
221
222        printf("%d: %p\n", i, ptr);
223
224        ion_free (fd, handle);
225    }
226
227}
228
229
230int
231main (int argc, char *argv[])
232{
233    int width, height;
234
235    width = 640;
236    height = 480;
237    test_format (HAL_PIXEL_FORMAT_TI_NV12, 0, width, height);
238    test_format (HAL_PIXEL_FORMAT_TI_NV12, 1, width, height);
239    test_format (HAL_PIXEL_FORMAT_TI_NV12_1D, 0, width, height);
240    test_format (HAL_PIXEL_FORMAT_TI_Y8, 1, width, height);
241    test_format (HAL_PIXEL_FORMAT_TI_Y16, 1, width, height);
242
243    width = 2608;
244    height = 1960;
245    test_format (HAL_PIXEL_FORMAT_TI_NV12, 1, width, height);
246    test_format (HAL_PIXEL_FORMAT_TI_NV12_1D, 0, width, height);
247    test_format (HAL_PIXEL_FORMAT_TI_Y8, 1, width, height);
248    test_format (HAL_PIXEL_FORMAT_TI_Y16, 1, width, height);
249
250    ion_test();
251
252    return 0;
253}
254
255