tizen 2.4 release
[sdk/emulator-yagl.git] / GLES_common / yagl_gles_image.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 "GL/gl.h"
35 #include "yagl_gles_image.h"
36 #include "yagl_gles_texture.h"
37 #include "yagl_client_context.h"
38 #include "yagl_sharegroup.h"
39 #include "yagl_malloc.h"
40 #include "yagl_host_gles_calls.h"
41
42 static void yagl_gles_image_update(struct yagl_client_image *image,
43                                    uint32_t width,
44                                    uint32_t height,
45                                    uint32_t bpp,
46                                    const void *pixels)
47 {
48     struct yagl_gles_image *gles_image = (struct yagl_gles_image*)image;
49
50     yagl_host_glUpdateOffscreenImageYAGL(gles_image->tex_global_name,
51                                          width,
52                                          height,
53                                          bpp,
54                                          pixels,
55                                          width * height * bpp);
56 }
57
58 static void yagl_gles_image_destroy(struct yagl_ref *ref)
59 {
60     struct yagl_gles_image *image = (struct yagl_gles_image*)ref;
61
62     if (image->texture_obj) {
63         yagl_gles_texture_release(image->texture_obj);
64     } else {
65         yagl_host_glDeleteObjects(&image->tex_global_name, 1);
66     }
67
68     yagl_client_image_cleanup(&image->base);
69
70     yagl_free(image);
71 }
72
73 struct yagl_gles_image *yagl_gles_image_create(yagl_object_name tex_global_name)
74 {
75     struct yagl_gles_image *image;
76
77     image = yagl_malloc0(sizeof(*image));
78
79     yagl_client_image_init(&image->base, &yagl_gles_image_destroy);
80
81     image->base.update = &yagl_gles_image_update;
82
83     image->tex_global_name = tex_global_name;
84
85     return image;
86 }
87
88 struct yagl_gles_image *yagl_gles_image_wrap_tex(struct yagl_client_context *ctx,
89                                                  yagl_object_name tex_local_name)
90 {
91     struct yagl_gles_texture *texture_obj;
92     struct yagl_gles_image *image;
93
94     texture_obj = (struct yagl_gles_texture *)yagl_sharegroup_acquire_object(ctx->sg,
95                                                                              YAGL_NS_TEXTURE,
96                                                                              tex_local_name);
97
98     if (!texture_obj) {
99         goto fail;
100     }
101
102     image = yagl_malloc0(sizeof(*image));
103
104     yagl_client_image_init(&image->base, &yagl_gles_image_destroy);
105
106     image->base.update = &yagl_gles_image_update;
107
108     image->tex_global_name = texture_obj->global_name;
109     image->texture_obj = texture_obj;
110
111     return image;
112
113 fail:
114     yagl_gles_texture_release(texture_obj);
115
116     return NULL;
117 }
118
119 void yagl_gles_image_acquire(struct yagl_gles_image *image)
120 {
121     if (image) {
122         yagl_client_image_acquire(&image->base);
123     }
124 }
125
126 void yagl_gles_image_release(struct yagl_gles_image *image)
127 {
128     if (image) {
129         yagl_client_image_release(&image->base);
130     }
131 }