2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://www.tizenopensource.org/license
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.
18 #include <unistd.h> /* access */
21 #include <sys/types.h>
32 #include <X11/Xutil.h>
33 #include <X11/Xproto.h>
34 #include <X11/extensions/Xdamage.h>
35 #include <X11/extensions/Xfixes.h>
36 #include <X11/extensions/XShm.h>
40 #include <xf86drmMode.h>
41 #include <tbm_bufmgr.h>
51 #include "client_life.h"
52 #include "client_rpc.h"
53 #include "buffer_handler.h"
58 DESTROYED = 0x00dead00,
60 enum buffer_type type;
67 * \brief Allocate this in the buffer->data.
70 DRI2Buffer *dri2_buffer;
71 unsigned int attachments[1];
79 void *data; /* Gem layer */
82 void *compensate_data; /* Check the pitch value, copy this to data */
90 enum buffer_type type;
97 struct inst_info *inst;
101 tbm_bufmgr slp_bufmgr;
105 Eina_List *pixmap_list;
114 HAPI struct buffer_info *buffer_handler_create(struct inst_info *inst, enum buffer_type type, int w, int h, int pixel_size)
116 struct buffer_info *info;
118 info = malloc(sizeof(*info));
120 ErrPrint("Heap: %s\n", strerror(errno));
125 case BUFFER_TYPE_SHM:
126 info->id = strdup(SCHEMA_SHM "-1");
128 ErrPrint("Heap: %s\n", strerror(errno));
133 case BUFFER_TYPE_FILE:
134 info->id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
136 ErrPrint("Heap: %s\n", strerror(errno));
141 case BUFFER_TYPE_PIXMAP:
142 info->id = strdup(SCHEMA_PIXMAP "0");
144 ErrPrint("Heap: %s\n", strerror(errno));
150 ErrPrint("Invalid type\n");
157 info->pixel_size = pixel_size;
163 DbgPrint("%dx%d size buffer is created\n", w, h);
167 static inline struct buffer *create_pixmap(struct buffer_info *info)
169 struct gem_data *gem;
170 struct buffer *buffer;
174 disp = ecore_x_display_get();
178 parent = DefaultRootWindow(disp);
180 buffer = calloc(1, sizeof(*buffer) + sizeof(*gem));
182 ErrPrint("Heap: %s\n", strerror(errno));
186 gem = (struct gem_data *)buffer->data;
188 buffer->type = BUFFER_TYPE_PIXMAP;
190 buffer->state = CREATED;
192 DbgPrint("Canvas %dx%d - %d is created\n", info->w, info->h, info->pixel_size);
194 gem->attachments[0] = DRI2BufferFrontLeft;
196 gem->w = info->w; /*!< This can be changed by DRI2GetBuffers */
197 gem->h = info->h; /*!< This can be changed by DRI2GetBuffers */
198 gem->depth = info->pixel_size;
202 * 32 Bits is not supported for video playing.
204 gem->pixmap = XCreatePixmap(disp, parent, info->w, info->h, 24 /* (info->pixel_size << 3) */);
205 if (gem->pixmap == (Pixmap)0) {
206 ErrPrint("Failed to create a pixmap\n");
213 DbgPrint("Pixmap:0x%X is created\n", gem->pixmap);
217 static inline int create_gem(struct buffer *buffer)
219 struct gem_data *gem;
222 disp = ecore_x_display_get();
224 ErrPrint("Failed to get display\n");
228 gem = (struct gem_data *)buffer->data;
231 gem->data = calloc(1, gem->w * gem->h * gem->depth);
233 ErrPrint("Heap: %s\n", strerror(errno));
237 DbgPrint("DRI2(gem) is not supported - Fallback to the S/W Backend\n");
241 DRI2CreateDrawable(disp, gem->pixmap);
242 DbgPrint("DRI2CreateDrawable is done\n");
244 gem->dri2_buffer = DRI2GetBuffers(disp, gem->pixmap,
245 &gem->w, &gem->h, gem->attachments, gem->count, &gem->buf_count);
246 if (!gem->dri2_buffer || !gem->dri2_buffer->name) {
247 ErrPrint("Failed to get a gem buffer\n");
248 DRI2DestroyDrawable(disp, gem->pixmap);
251 DbgPrint("dri2_buffer: %p, name: %p, %dx%d\n",
252 gem->dri2_buffer, gem->dri2_buffer->name, gem->w, gem->h);
253 DbgPrint("dri2_buffer->pitch : %d, buf_count: %d\n",
254 gem->dri2_buffer->pitch, gem->buf_count);
257 * \How can I destroy this?
259 gem->pixmap_bo = tbm_bo_import(s_info.slp_bufmgr, gem->dri2_buffer->name);
260 if (!gem->pixmap_bo) {
261 ErrPrint("Failed to import BO\n");
262 DRI2DestroyDrawable(disp, gem->pixmap);
266 if (gem->dri2_buffer->pitch != gem->w * gem->depth) {
267 gem->compensate_data = calloc(1, gem->w * gem->h * gem->depth);
268 if (!gem->compensate_data) {
269 ErrPrint("Failed to allocate heap\n");
271 DbgPrint("Allocate compensate buffer %p(%dx%d %d)\n",
272 gem->compensate_data,
273 gem->w, gem->h, gem->depth);
280 static inline void *acquire_gem(struct buffer *buffer)
282 struct gem_data *gem;
287 gem = (struct gem_data *)buffer->data;
289 DbgPrint("GEM is not supported - Use the fake gem buffer\n");
291 if (!gem->pixmap_bo) {
292 DbgPrint("GEM is not created\n");
297 tbm_bo_handle handle;
300 ErrPrint("Already acquired. but the buffer is not valid\n");
304 handle = tbm_bo_map(gem->pixmap_bo, TBM_DEVICE_CPU, TBM_OPTION_READ | TBM_OPTION_WRITE);
305 gem->data = handle.ptr;
313 * If there is a compensate canvas buffer,
316 return gem->compensate_data ? gem->compensate_data : gem->data;
319 static inline void release_gem(struct buffer *buffer)
321 struct gem_data *gem;
323 gem = (struct gem_data *)buffer->data;
324 if (s_info.fd >= 0 && !gem->pixmap_bo) {
325 DbgPrint("GEM is not created\n");
330 if (gem->refcnt > 0) {
331 ErrPrint("Reference count is not valid %d\n", gem->refcnt);
338 if (gem->refcnt == 0) {
340 DbgPrint("S/W Gem buffer has no reference\n");
344 * Update the gem buffer using compensate data buffer if it is exists
346 if (gem->compensate_data) {
353 pixel = gem->compensate_data;
354 gem_pixel = gem->data;
355 gap = gem->dri2_buffer->pitch - (gem->w * gem->depth);
357 for (y = 0; y < gem->h; y++) {
358 for (x = 0; x < gem->w; x++)
359 *gem_pixel++ = *pixel++;
361 gem_pixel = (int *)(((char *)gem_pixel) + gap);
366 tbm_bo_unmap(gem->pixmap_bo);
370 } else if (gem->refcnt < 0) {
371 DbgPrint("Invalid refcnt: %d (reset)\n", gem->refcnt);
376 static inline int destroy_pixmap(struct buffer *buffer)
378 struct gem_data *gem;
380 gem = (struct gem_data *)buffer->data;
385 disp = ecore_x_display_get();
389 DbgPrint("Free pixmap 0x%X\n", gem->pixmap);
390 XFreePixmap(disp, gem->pixmap);
393 buffer->state = DESTROYED;
398 static inline int destroy_gem(struct buffer *buffer)
400 struct gem_data *gem;
406 * Forcely release the acquire_buffer.
408 gem = (struct gem_data *)buffer->data;
412 if (s_info.fd >= 0) {
413 if (gem->compensate_data) {
414 DbgPrint("Release compensate buffer %p\n", gem->compensate_data);
415 free(gem->compensate_data);
416 gem->compensate_data = NULL;
419 if (gem->pixmap_bo) {
420 DbgPrint("unref pixmap bo\n");
421 tbm_bo_unref(gem->pixmap_bo);
422 gem->pixmap_bo = NULL;
424 DbgPrint("DRI2DestroyDrawable\n");
425 DRI2DestroyDrawable(ecore_x_display_get(), gem->pixmap);
427 } else if (gem->data) {
428 DbgPrint("Release fake gem buffer\n");
436 static inline int load_file_buffer(struct buffer_info *info)
438 struct buffer *buffer;
444 len = strlen(IMAGE_PATH) + 40;
445 new_id = malloc(len);
447 ErrPrint("Heap: %s\n", strerror(errno));
451 timestamp = util_timestamp();
452 snprintf(new_id, len, SCHEMA_FILE "%s%lf", IMAGE_PATH, timestamp);
454 size = sizeof(*buffer) + info->w * info->h * info->pixel_size;
456 ErrPrint("Canvas buffer size is ZERO\n");
461 buffer = calloc(1, size);
463 ErrPrint("Failed to allocate buffer\n");
468 buffer->type = BUFFER_TYPE_FILE;
470 buffer->state = CREATED;
475 info->buffer = buffer;
478 DbgPrint("FILE type %d created\n", size);
482 static inline int load_shm_buffer(struct buffer_info *info)
486 struct buffer *buffer; /* Just for getting a size */
490 size = info->w * info->h * info->pixel_size;
492 ErrPrint("Invalid buffer size\n");
496 id = shmget(IPC_PRIVATE, size + sizeof(*buffer), IPC_CREAT | 0666);
498 ErrPrint("shmget: %s\n", strerror(errno));
502 buffer = shmat(id, NULL, 0);
503 if (buffer == (void *)-1) {
504 ErrPrint("%s shmat: %s\n", info->id, strerror(errno));
506 if (shmctl(id, IPC_RMID, 0) < 0)
507 ErrPrint("%s shmctl: %s\n", info->id, strerror(errno));
512 buffer->type = BUFFER_TYPE_SHM;
514 buffer->state = CREATED; /*!< Needless */
515 buffer->info = NULL; /*!< This has not to be used, every process will see this. So, don't try to save anything on here */
517 len = strlen(SCHEMA_SHM) + 30; /* strlen("shm://") + 30 */
519 new_id = malloc(len);
521 ErrPrint("Heap: %s\n", strerror(errno));
522 if (shmdt(buffer) < 0)
523 ErrPrint("shmdt: %s\n", strerror(errno));
525 if (shmctl(id, IPC_RMID, 0) < 0)
526 ErrPrint("shmctl: %s\n", strerror(errno));
531 snprintf(new_id, len, SCHEMA_SHM "%d", id);
535 info->buffer = buffer;
540 static inline int load_pixmap_buffer(struct buffer_info *info)
542 struct buffer *buffer;
543 struct gem_data *gem;
549 * Before call the buffer_handler_pixmap_ref function,
550 * You should make sure that the is_loaded value is toggled (1)
551 * Or the buffer_handler_pixmap_ref function will return NULL
556 DbgPrint("Buffer is already exists, but override it with new one\n");
558 buffer = buffer_handler_pixmap_ref(info);
560 DbgPrint("Failed to make a reference of a pixmap\n");
565 len = strlen(SCHEMA_PIXMAP) + 30; /* strlen("pixmap://") + 30 */
566 new_id = malloc(len);
569 ErrPrint("Heap: %s\n", strerror(errno));
570 buffer_handler_pixmap_unref(buffer);
574 DbgPrint("Releaseo old id (%s)\n", info->id);
578 gem = (struct gem_data *)buffer->data;
579 DbgPrint("gem pointer: %p\n", gem);
581 snprintf(info->id, len, SCHEMA_PIXMAP "%d", (int)gem->pixmap);
582 DbgPrint("info->id: %s\n", info->id);
587 HAPI int buffer_handler_load(struct buffer_info *info)
592 DbgPrint("buffer handler is nil\n");
596 if (info->is_loaded) {
597 DbgPrint("Buffer is already loaded\n");
601 switch (info->type) {
602 case BUFFER_TYPE_FILE:
603 ret = load_file_buffer(info);
605 case BUFFER_TYPE_SHM:
606 ret = load_shm_buffer(info);
608 case BUFFER_TYPE_PIXMAP:
609 ret = load_pixmap_buffer(info);
612 ErrPrint("Invalid buffer\n");
620 static inline int unload_file_buffer(struct buffer_info *info)
625 new_id = strdup(SCHEMA_FILE "/tmp/.live.undefined");
627 ErrPrint("Heap: %s\n", strerror(errno));
631 DbgFree(info->buffer);
634 path = util_uri_to_path(info->id);
635 if (path && unlink(path) < 0)
636 ErrPrint("unlink: %s\n", strerror(errno));
643 static inline int unload_shm_buffer(struct buffer_info *info)
648 new_id = strdup(SCHEMA_SHM "-1");
650 ErrPrint("Heap: %s\n", strerror(errno));
654 if (sscanf(info->id, SCHEMA_SHM "%d", &id) != 1) {
655 ErrPrint("%s Invalid ID\n", info->id);
661 ErrPrint("(%s) Invalid id: %d\n", info->id, id);
666 if (shmdt(info->buffer) < 0)
667 ErrPrint("Detach shm: %s\n", strerror(errno));
669 if (shmctl(id, IPC_RMID, 0) < 0)
670 ErrPrint("Remove shm: %s\n", strerror(errno));
679 static inline int unload_pixmap_buffer(struct buffer_info *info)
684 new_id = strdup(SCHEMA_PIXMAP "0");
686 ErrPrint("Heap: %s\n", strerror(errno));
690 if (sscanf(info->id, SCHEMA_PIXMAP "%d", &id) != 1) {
691 ErrPrint("Invalid ID (%s)\n", info->id);
697 ErrPrint("(%s) Invalid id: %d\n", info->id, id);
703 * Decrease the reference counter.
705 buffer_handler_pixmap_unref(info->buffer);
709 * Just clear the info->buffer.
710 * It will be reallocated again.
719 HAPI int buffer_handler_unload(struct buffer_info *info)
724 DbgPrint("buffer handler is nil\n");
728 if (!info->is_loaded) {
729 ErrPrint("Buffer is not loaded\n");
733 switch (info->type) {
734 case BUFFER_TYPE_FILE:
735 ret = unload_file_buffer(info);
737 case BUFFER_TYPE_SHM:
738 ret = unload_shm_buffer(info);
740 case BUFFER_TYPE_PIXMAP:
741 ret = unload_pixmap_buffer(info);
744 ErrPrint("Invalid buffer\n");
755 HAPI int buffer_handler_destroy(struct buffer_info *info)
758 struct buffer *buffer;
761 DbgPrint("Buffer is not created yet. info is nil\n");
765 EINA_LIST_FOREACH(s_info.pixmap_list, l, buffer) {
766 if (buffer->info == info)
770 buffer_handler_unload(info);
776 HAPI const char *buffer_handler_id(const struct buffer_info *info)
778 return info ? info->id : "";
781 HAPI enum buffer_type buffer_handler_type(const struct buffer_info *info)
783 return info ? info->type : BUFFER_TYPE_ERROR;
786 HAPI void *buffer_handler_fb(struct buffer_info *info)
788 struct buffer *buffer;
793 buffer = info->buffer;
795 if (info->type == BUFFER_TYPE_PIXMAP) {
801 canvas = buffer_handler_pixmap_acquire_buffer(info);
802 ret = buffer_handler_pixmap_release_buffer(canvas);
803 DbgPrint("Canvas %p(%d) (released but still in use)\n", canvas, ret);
810 HAPI int buffer_handler_pixmap(const struct buffer_info *info)
813 struct gem_data *gem;
816 ErrPrint("Inavlid buffer handler\n");
820 if (info->type != BUFFER_TYPE_PIXMAP) {
821 ErrPrint("Invalid buffer type\n");
825 buf = (struct buffer *)info->buffer;
827 ErrPrint("Invalid buffer data\n");
831 gem = (struct gem_data *)buf->data;
835 HAPI void *buffer_handler_pixmap_acquire_buffer(struct buffer_info *info)
837 struct buffer *buffer;
839 if (!info || !info->is_loaded) {
840 ErrPrint("Buffer is not loaded\n");
844 buffer = buffer_handler_pixmap_ref(info);
848 return acquire_gem(buffer);
851 HAPI void *buffer_handler_pixmap_buffer(struct buffer_info *info)
853 struct buffer *buffer;
854 struct gem_data *gem;
859 if (!info->is_loaded) {
860 ErrPrint("Buffer is not loaded\n");
864 buffer = info->buffer;
868 gem = (struct gem_data *)buffer->data;
869 return gem->compensate_data ? gem->compensate_data : gem->data;
873 * \return "buffer" object (Not the buffer_info)
875 HAPI void *buffer_handler_pixmap_ref(struct buffer_info *info)
877 struct buffer *buffer;
879 if (!info->is_loaded) {
880 ErrPrint("Buffer is not loaded\n");
884 if (info->type != BUFFER_TYPE_PIXMAP) {
885 ErrPrint("Buffer type is not matched\n");
889 buffer = info->buffer;
893 buffer = create_pixmap(info);
895 DbgPrint("Failed to create a pixmap\n");
899 info->buffer = buffer;
902 struct pkg_info *pkg;
904 if (instance_lb_buffer(info->inst) == info) {
905 pkg = instance_package(info->inst);
906 if (package_lb_type(pkg) == LB_TYPE_BUFFER) {
907 DbgPrint("Doesn't need to create gem for LB\n");
910 } else if (instance_pd_buffer(info->inst) == info) {
911 pkg = instance_package(info->inst);
912 if (package_pd_type(pkg) == PD_TYPE_BUFFER) {
913 DbgPrint("Doesn't need to create gem for PD\n");
922 } else if (buffer->state != CREATED || buffer->type != BUFFER_TYPE_PIXMAP) {
923 ErrPrint("Invalid buffer\n");
925 } else if (buffer->refcnt > 0) {
930 s_info.pixmap_list = eina_list_append(s_info.pixmap_list, buffer);
937 HAPI void *buffer_handler_pixmap_find(int pixmap)
939 struct buffer *buffer;
940 struct gem_data *gem;
947 EINA_LIST_FOREACH_SAFE(s_info.pixmap_list, l, n, buffer) {
948 if (!buffer || buffer->state != CREATED || buffer->type != BUFFER_TYPE_PIXMAP) {
949 s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
950 DbgPrint("Invalid buffer (List Removed: %p)\n", buffer);
954 gem = (struct gem_data *)buffer->data;
955 if (gem->pixmap == pixmap)
962 HAPI int buffer_handler_pixmap_release_buffer(void *canvas)
964 struct buffer *buffer;
965 struct gem_data *gem;
973 EINA_LIST_FOREACH_SAFE(s_info.pixmap_list, l, n, buffer) {
974 if (!buffer || buffer->state != CREATED || buffer->type != BUFFER_TYPE_PIXMAP) {
975 s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
979 gem = (struct gem_data *)buffer->data;
980 _ptr = gem->compensate_data ? gem->compensate_data : gem->data;
985 if (_ptr == canvas) {
987 buffer_handler_pixmap_unref(buffer);
998 * \return Return NULL if the buffer is in still uses.
999 * Return buffer_ptr if it needs to destroy
1001 HAPI int buffer_handler_pixmap_unref(void *buffer_ptr)
1003 struct buffer *buffer = buffer_ptr;
1004 struct buffer_info *info;
1007 if (buffer->refcnt > 0)
1008 return 0; /* Return NULL means, gem buffer still in use */
1010 s_info.pixmap_list = eina_list_remove(s_info.pixmap_list, buffer);
1012 if (destroy_gem(buffer) < 0)
1013 ErrPrint("Failed to destroy gem buffer\n");
1015 if (destroy_pixmap(buffer) < 0)
1016 ErrPrint("Failed to destroy pixmap\n");
1018 info = buffer->info;
1019 if (info && info->buffer == buffer)
1020 info->buffer = NULL;
1025 HAPI int buffer_handler_is_loaded(const struct buffer_info *info)
1027 return info ? info->is_loaded : 0;
1030 HAPI void buffer_handler_update_size(struct buffer_info *info, int w, int h)
1039 HAPI int buffer_handler_resize(struct buffer_info *info, int w, int h)
1044 ErrPrint("Invalid handler\n");
1048 if (info->w == w && info->h == h) {
1049 DbgPrint("No changes\n");
1053 buffer_handler_update_size(info, w, h);
1055 if (!info->is_loaded) {
1056 DbgPrint("Not yet loaded, just update the size [%dx%d]\n", w, h);
1060 ret = buffer_handler_unload(info);
1062 ErrPrint("Unload: %d\n", ret);
1064 ret = buffer_handler_load(info);
1066 ErrPrint("Load: %d\n", ret);
1071 HAPI int buffer_handler_get_size(struct buffer_info *info, int *w, int *h)
1084 HAPI struct inst_info *buffer_handler_instance(struct buffer_info *info)
1091 * Only for used S/W Backend
1093 static inline int sync_for_pixmap(struct buffer *buffer)
1099 struct gem_data *gem;
1103 if (buffer->state != CREATED) {
1104 ErrPrint("Invalid state of a FB\n");
1108 if (buffer->type != BUFFER_TYPE_PIXMAP) {
1109 DbgPrint("Invalid buffer\n");
1113 disp = ecore_x_display_get();
1115 ErrPrint("Failed to get a display\n");
1119 gem = (struct gem_data *)buffer->data;
1120 if (gem->w == 0 || gem->h == 0) {
1121 DbgPrint("Nothing can be sync\n");
1125 si.shmid = shmget(IPC_PRIVATE, gem->w * gem->h * gem->depth, IPC_CREAT | 0666);
1127 ErrPrint("shmget: %s\n", strerror(errno));
1131 si.readOnly = False;
1132 si.shmaddr = shmat(si.shmid, NULL, 0);
1133 if (si.shmaddr == (void *)-1) {
1134 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
1135 ErrPrint("shmctl: %s\n", strerror(errno));
1139 screen = DefaultScreenOfDisplay(disp);
1140 visual = DefaultVisualOfScreen(screen);
1143 * XCreatePixmap can only uses 24 bits depth only.
1145 xim = XShmCreateImage(disp, visual, 24 /* (gem->depth << 3) */, ZPixmap, NULL, &si, gem->w, gem->h);
1147 if (shmdt(si.shmaddr) < 0)
1148 ErrPrint("shmdt: %s\n", strerror(errno));
1150 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
1151 ErrPrint("shmctl: %s\n", strerror(errno));
1155 xim->data = si.shmaddr;
1156 XShmAttach(disp, &si);
1159 gc = XCreateGC(disp, gem->pixmap, 0, NULL);
1161 XShmDetach(disp, &si);
1164 if (shmdt(si.shmaddr) < 0)
1165 ErrPrint("shmdt: %s\n", strerror(errno));
1167 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
1168 ErrPrint("shmctl: %s\n", strerror(errno));
1173 memcpy(xim->data, gem->data, gem->w * gem->h * gem->depth);
1176 * \note Do not send the event.
1177 * Instead of X event, master will send the updated event to the viewer
1179 XShmPutImage(disp, gem->pixmap, gc, xim, 0, 0, 0, 0, gem->w, gem->h, False);
1183 XShmDetach(disp, &si);
1186 if (shmdt(si.shmaddr) < 0)
1187 ErrPrint("shmdt: %s\n", strerror(errno));
1189 if (shmctl(si.shmid, IPC_RMID, 0) < 0)
1190 ErrPrint("shmctl: %s\n", strerror(errno));
1195 HAPI void buffer_handler_flush(struct buffer_info *info)
1199 struct buffer *buffer;
1201 if (!info || !info->buffer)
1204 buffer = info->buffer;
1206 if (buffer->type == BUFFER_TYPE_PIXMAP) {
1207 if (s_info.fd > 0) {
1209 XserverRegion region;
1213 rect.width = info->w;
1214 rect.height = info->h;
1216 region = XFixesCreateRegion(ecore_x_display_get(), &rect, 1);
1217 XDamageAdd(ecore_x_display_get(), buffer_handler_pixmap(info), region);
1218 XFixesDestroyRegion(ecore_x_display_get(), region);
1219 XFlush(ecore_x_display_get());
1221 if (sync_for_pixmap(buffer) < 0)
1222 ErrPrint("Failed to sync via S/W Backend\n");
1224 } else if (buffer->type == BUFFER_TYPE_FILE) {
1225 fd = open(util_uri_to_path(info->id), O_WRONLY | O_CREAT, 0644);
1227 ErrPrint("%s open falied: %s\n", util_uri_to_path(info->id), strerror(errno));
1231 size = info->w * info->h * info->pixel_size;
1232 if (write(fd, info->buffer, size) != size)
1233 ErrPrint("Write is not completed: %s\n", strerror(errno));
1237 DbgPrint("Flush nothing\n");
1241 HAPI int buffer_handler_init(void)
1243 int dri2Major, dri2Minor;
1244 char *driverName, *deviceName;
1247 if (!DRI2QueryExtension(ecore_x_display_get(), &s_info.evt_base, &s_info.err_base)) {
1248 DbgPrint("DRI2 is not supported\n");
1252 if (!DRI2QueryVersion(ecore_x_display_get(), &dri2Major, &dri2Minor)) {
1253 DbgPrint("DRI2 is not supported\n");
1254 s_info.evt_base = 0;
1255 s_info.err_base = 0;
1259 if (!DRI2Connect(ecore_x_display_get(), DefaultRootWindow(ecore_x_display_get()), &driverName, &deviceName)) {
1260 DbgPrint("DRI2 is not supported\n");
1261 s_info.evt_base = 0;
1262 s_info.err_base = 0;
1266 DbgPrint("Open: %s (driver: %s)", deviceName, driverName);
1268 if (USE_SW_BACKEND) {
1269 DbgPrint("Fallback to the S/W Backend\n");
1270 s_info.evt_base = 0;
1271 s_info.err_base = 0;
1272 DbgFree(deviceName);
1273 DbgFree(driverName);
1277 s_info.fd = open(deviceName, O_RDWR);
1278 DbgFree(deviceName);
1279 DbgFree(driverName);
1280 if (s_info.fd < 0) {
1281 DbgPrint("Failed to open a drm device: (%s)\n", strerror(errno));
1282 s_info.evt_base = 0;
1283 s_info.err_base = 0;
1287 drmGetMagic(s_info.fd, &magic);
1288 DbgPrint("DRM Magic: 0x%X\n", magic);
1289 if (!DRI2Authenticate(ecore_x_display_get(), DefaultRootWindow(ecore_x_display_get()), (unsigned int)magic)) {
1290 DbgPrint("Failed to do authenticate for DRI2\n");
1293 s_info.evt_base = 0;
1294 s_info.err_base = 0;
1298 s_info.slp_bufmgr = tbm_bufmgr_init(s_info.fd);
1299 if (!s_info.slp_bufmgr) {
1300 DbgPrint("Failed to init bufmgr\n");
1303 s_info.evt_base = 0;
1304 s_info.err_base = 0;
1311 HAPI int buffer_handler_fini(void)
1313 if (s_info.fd >= 0) {
1318 if (s_info.slp_bufmgr) {
1319 tbm_bufmgr_deinit(s_info.slp_bufmgr);
1320 s_info.slp_bufmgr = NULL;