1 /**
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <gui/BufferQueue.h>
17 #include <gui/IGraphicBufferProducer.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <ui/Fence.h>
23 #include <utils/String8.h>
24 
25 using namespace android;
26 
27 #define MAX_TRY 5000  // based on experiments
28 volatile int quit = 1;
29 
start2(void * args)30 static void *start2(void *args) {
31   sp<IGraphicBufferProducer> bufferProducer =
32       *(sp<IGraphicBufferProducer> *)args;
33 
34   /*
35    * It will end when ever the main thread exits due to
36    * two conditions.
37    * 1. count value reaches less than 0
38    * 2. Transact failed
39    */
40   while (quit) {
41     int buffer;
42     sp<Fence> fence;
43     bufferProducer->dequeueBuffer(&buffer, &fence, 800, 600, 1, 0, nullptr,
44                                   nullptr);
45   }
46   return NULL;
47 }
48 
main(int argc,char * const argv[])49 int main(__attribute__((unused)) int argc,
50          __attribute__((unused)) char *const argv[]) {
51   int count = MAX_TRY;
52   int result = EXIT_SUCCESS;
53   sp<IGraphicBufferProducer> bufferProducer = NULL;
54   sp<IGraphicBufferConsumer> bufferConsumer = NULL;
55 
56   pthread_t thread;
57   pthread_create(&thread, NULL, start2, &bufferProducer);
58 
59   while (quit) {
60     bufferConsumer->setConsumerName(String8("dddddddddddddddd"));
61     String8 str = bufferProducer->getConsumerName();
62     if (count < 0) {
63       quit = 0;
64     }
65     if (!strcmp("TransactFailed", str.string())) {
66       result = EXIT_FAILURE;
67       quit = 0;
68     }
69     count--;
70   }
71   pthread_join(thread, NULL);
72 
73   return result;
74 }
75