tizen 2.4 release
[sdk/emulator-yagl.git] / GLESv2 / yagl_gles3_query.c
1 /*
2  * YaGL
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact :
7  * Stanislav Vorobiov <s.vorobiov@samsung.com>
8  * Jinhyung Jo <jinhyung.jo@samsung.com>
9  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  *
29  * Contributors:
30  * - S-Core Co., Ltd
31  *
32  */
33
34 #include "GLES3/gl3.h"
35 #include "yagl_gles3_query.h"
36 #include "yagl_malloc.h"
37 #include "yagl_state.h"
38 #include "yagl_host_gles_calls.h"
39
40 static void yagl_gles3_query_update_result(struct yagl_gles3_query *query, GLenum pname)
41 {
42     if (!query->result_available) {
43         query->result_available =
44             yagl_host_glGetQueryObjectuiv(query->global_name, pname, &query->result);
45     }
46 }
47
48 static void yagl_gles3_query_destroy(struct yagl_ref *ref)
49 {
50     struct yagl_gles3_query *query = (struct yagl_gles3_query*)ref;
51
52     yagl_host_glDeleteObjects(&query->global_name, 1);
53
54     yagl_object_cleanup(&query->base);
55
56     yagl_free(query);
57 }
58
59 struct yagl_gles3_query *yagl_gles3_query_create(void)
60 {
61     struct yagl_gles3_query *query;
62
63     query = yagl_malloc0(sizeof(*query));
64
65     yagl_object_init(&query->base, &yagl_gles3_query_destroy);
66
67     query->global_name = yagl_get_global_name();
68
69     yagl_host_glGenQueries(&query->global_name, 1);
70
71     return query;
72 }
73
74 void yagl_gles3_query_acquire(struct yagl_gles3_query *query)
75 {
76     if (query) {
77         yagl_object_acquire(&query->base);
78     }
79 }
80
81 void yagl_gles3_query_release(struct yagl_gles3_query *query)
82 {
83     if (query) {
84         yagl_object_release(&query->base);
85     }
86 }
87
88 void yagl_gles3_query_begin(struct yagl_gles3_query *query,
89                             GLenum target)
90 {
91     yagl_host_glBeginQuery(target, query->global_name);
92
93     query->active = 1;
94     query->result_available = 0;
95     query->result = 0;
96     query->was_active = 1;
97 }
98
99 void yagl_gles3_query_end(struct yagl_gles3_query *query,
100                           GLenum target)
101 {
102     yagl_host_glEndQuery(target);
103
104     query->active = 0;
105 }
106
107 int yagl_gles3_query_is_result_available(struct yagl_gles3_query *query)
108 {
109     yagl_gles3_query_update_result(query, GL_QUERY_RESULT_AVAILABLE);
110
111     return query->result_available;
112 }
113
114 GLuint yagl_gles3_query_get_result(struct yagl_gles3_query *query)
115 {
116     yagl_gles3_query_update_result(query, GL_QUERY_RESULT);
117
118     return query->result;
119 }
120
121 int yagl_gles3_query_was_active(struct yagl_gles3_query *query)
122 {
123     return query->was_active;
124 }