summaryrefslogtreecommitdiff
path: root/merrifield/test/nv12_ved_test.cpp
blob: 7fb5b368ca27ac1f0e65a23603f0db3cad95da01 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
// Copyright (c) 2014 Intel Corporation 
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#include <gtest/gtest.h>

#include <binder/IMemory.h>

#include <gui/ISurfaceComposer.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <private/gui/ComposerService.h>

#include <utils/String8.h>

using namespace android;
const char * filename = "/data/my_640x480.nv12";
#define PIXEL_FORMAT_NV12 0x7FA00E00

// Fill a YV12 buffer with a multi-colored checkerboard pattern
void fillYUVBuffer(uint8_t* buf, int w, int h, int stride) {
	const int blockWidth = w > 16 ? w / 16 : 1;
	const int blockHeight = h > 16 ? h / 16 : 1;
	const int yuvTexOffsetY = 0;
	int yuvTexStrideY = stride;
	int yuvTexOffsetV = yuvTexStrideY * h;
	int yuvTexStrideV = (yuvTexStrideY / 2 + 0xf) & ~0xf;
	int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h / 2;
	int yuvTexStrideU = yuvTexStrideV;
	for (int x = 0; x < w; x++) {
		for (int y = 0; y < h; y++) {
			int parityX = (x / blockWidth) & 1;
			int parityY = (y / blockHeight) & 1;
			unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
			buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
			if (x < w / 2 && y < h / 2) {
				buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
				if (x * 2 < w / 2 && y * 2 < h / 2) {
					buf[yuvTexOffsetV + (y * 2 * yuvTexStrideV) + x * 2 + 0] =
							buf[yuvTexOffsetV + (y * 2 * yuvTexStrideV) + x * 2
									+ 1] =
									buf[yuvTexOffsetV
											+ ((y * 2 + 1) * yuvTexStrideV)
											+ x * 2 + 0] = buf[yuvTexOffsetV
											+ ((y * 2 + 1) * yuvTexStrideV)
											+ x * 2 + 1] = intensity;
				}
			}
		}
	}
}

void loadYUVBufferFromFile(uint8_t* buf, int w, int h, int stride) {
	FILE *fp = fopen(filename, "r");
	int line = 0;
	int offset = 0;
	int buffer_height = h * 1.5;

	if (!fp) {
		printf("%s: failed to open %s\n", __func__, filename);
		return;
	}

	printf("buf=%p, w=%d,h=%d,stride=%d\n", buf, w, h, stride);

	for (line = 0; line < buffer_height; line++) {
		printf("reading line %d...\n", line);
		offset = line * stride;
		fread(buf + offset, w, 1, fp);
	}

	fclose(fp);
}

int main(int argc, char **argv) {
	sp < SurfaceControl > sc;
	sp < Surface > s;
	sp < ANativeWindow > anw;
	ANativeWindowBuffer *anb;
	uint8_t* img = NULL;
	sp < SurfaceComposerClient > composerClient = new SurfaceComposerClient;
	if (composerClient->initCheck() != NO_ERROR)
		return 0;

	sc = composerClient->createSurface(String8("FG Test Surface"), 640, 480,
			PIXEL_FORMAT_RGBA_8888, 0);
	if (sc == NULL)
		return 0;;
	if (!sc->isValid())
		return 0;

	s = sc->getSurface();
	anw = s.get();
	if (native_window_set_buffers_geometry(anw.get(), 640, 480,
			PIXEL_FORMAT_NV12) != NO_ERROR)
		return 0;
	if (native_window_set_usage(anw.get(),
			GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)
			!= NO_ERROR)
		return 0;
	
	/*
	 * load buffer 
	 */
	if (native_window_dequeue_buffer_and_wait(anw.get(), &anb))
		return 0;
	if (anb == NULL)
		return 0;
	sp < GraphicBuffer > buf(new GraphicBuffer(anb, false));
	//if (anw->lockBuffer(anw.get(), buf->getNativeBuffer()) != NO_ERROR)
	//	return 0;
	buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**) (&img));
	if (!img) {
		printf("failed to lock buffer\n");
		exit(-1);
	}

	loadYUVBufferFromFile(img, 640, 480, buf->getStride());
	buf->unlock();
	printf("querying buffer...\n");
	if (anw->queueBuffer(anw.get(), buf->getNativeBuffer(), -1) != NO_ERROR)
		return 0;

	// loop it to continuously display??
	while (1) {
		SurfaceComposerClient::openGlobalTransaction();
		if (sc->setLayer(INT_MAX - 1) != NO_ERROR)
			return 0;
		if (sc->show() != NO_ERROR)
			return 0;

		SurfaceComposerClient::closeGlobalTransaction();
	}
	return 0;
}