Commit: 85739dabefa9a28701e24f1ac33c5c00b1952af1
Author: Bastien Montagne
Date:   Sat Aug 13 18:40:40 2016 +0200
Branches: asset-engine
https://developer.blender.org/rB85739dabefa9a28701e24f1ac33c5c00b1952af1

Add initial support for previews to AssetEngine.

Code not really tested (it just builds and does not crash :P ).

Will also have to rethink a bit API and data structs imho... AssetUUID is 
starting to become a bit annoying.

===================================================================

M       source/blender/blenkernel/BKE_asset_engine.h
M       source/blender/blenloader/intern/readfile.c
M       source/blender/editors/space_file/filelist.c
M       source/blender/makesdna/DNA_ID.h
M       source/blender/makesdna/DNA_space_types.h
M       source/blender/makesrna/intern/rna_asset.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_asset_engine.h 
b/source/blender/blenkernel/BKE_asset_engine.h
index db6769e..3d7c1d8 100644
--- a/source/blender/blenkernel/BKE_asset_engine.h
+++ b/source/blender/blenkernel/BKE_asset_engine.h
@@ -121,6 +121,10 @@ typedef bool (*ae_entries_block_get)(struct AssetEngine 
*engine, const int start
 typedef bool (*ae_entries_uuid_get)(struct AssetEngine *engine, struct 
AssetUUIDList *uuids,
                                     struct FileDirEntryArr *entries_r);
 
+/* FILEBROWSER - Get previews of given entries.
+ * XXX WARNING! Currently, only asset part of uuids is valid here (because 
fileentries only store this one)... */
+typedef int (*ae_previews_get)(struct AssetEngine *engine, const int job_id, 
struct AssetUUIDList *uuids);
+
 /* 'pre-loading' hook, called before opening/appending/linking/updating given 
entries.
  * Note first given uuid is the one of 'active' entry, and first entry in 
returned list will be considered as such too.
  * E.g. allows the engine to ensure entries' paths are actually valid by 
downloading requested data, etc.
@@ -162,6 +166,8 @@ typedef struct AssetEngineType {
        ae_entries_block_get entries_block_get;
        ae_entries_uuid_get entries_uuid_get;
 
+       ae_previews_get previews_get;
+
        ae_ensure_uuids ensure_uuids;
 
        ae_load_pre load_pre;
diff --git a/source/blender/blenloader/intern/readfile.c 
b/source/blender/blenloader/intern/readfile.c
index ec5a46d..26c402c 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -2167,6 +2167,8 @@ static void direct_link_id(FileData *fd, ID *id)
        }
        if (id->uuid) {
                id->uuid = newdataadr(fd, id->uuid);
+               id->uuid->ibuff = NULL;  /* Just in case... */
+               id->uuid->width = id->uuid->height = 0;
        }
 }
 
@@ -8103,6 +8105,8 @@ static BHead *read_libblock(FileData *fd, Main *main, 
BHead *bhead, const short
                        bhead = read_data_into_oldnewmap(fd, bhead, __func__);
 
                        id->uuid = newdataadr(fd, id->uuid);
+                       id->uuid->ibuff = NULL;  /* Just in case... */
+                       id->uuid->width = id->uuid->height = 0;
 
                        oldnewmap_free_unused(fd->datamap);
                        oldnewmap_clear(fd->datamap);
@@ -9998,6 +10002,8 @@ static ID *link_named_part_ex(
 
                id->uuid = MEM_mallocN(sizeof(*id->uuid), __func__);
                *id->uuid = *uuid;
+               id->uuid->ibuff = NULL;
+               id->uuid->width = id->uuid->height = 0;
                id->flag |= LIB_ASSET;
 
                if (!mainl->curlib->asset_repository) {
diff --git a/source/blender/editors/space_file/filelist.c 
b/source/blender/editors/space_file/filelist.c
index 1ce7f0b..3718486 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -252,9 +252,12 @@ typedef struct FileListEntryCache {
        /* Allows to quickly get a cached entry from its UUID. */
        GHash *uuids;
 
-       /* Previews handling. */
+       /* Previews handling - generic filebrowser. */
        TaskPool *previews_pool;
        ThreadQueue *previews_done;
+       /* Previews handling - Asset engines. */
+       int ae_preview_job;
+       AssetUUIDList ae_preview_uuids;
 } FileListEntryCache;
 
 /* FileListCache.flags */
@@ -365,7 +368,7 @@ static int groupname_to_code(const char *group);
 static unsigned int groupname_to_filter_id(const char *group);
 
 static void filelist_filter_clear(FileList *filelist);
-static void filelist_cache_clear(FileListEntryCache *cache, size_t new_size);
+static void filelist_cache_clear(FileListEntryCache *cache, size_t new_size, 
AssetEngine *ae);
 
 /* ********** Sort helpers ********** */
 
@@ -729,7 +732,7 @@ void filelist_sort_filter(struct FileList *filelist, 
FileSelectParams *params)
                                                 filelist->ae, need_sorting, 
need_filtering, params, &filelist->filelist);
 //                     printf("%s: changed: %d (%d - %d)\n", __func__, 
changed, need_sorting, need_filtering);
                        if (changed) {
-                               filelist_cache_clear(&filelist->filelist_cache, 
filelist->filelist_cache.size);
+                               filelist_cache_clear(&filelist->filelist_cache, 
filelist->filelist_cache.size, filelist->ae);
                        }
                }
        }
@@ -794,7 +797,7 @@ void filelist_sort_filter(struct FileList *filelist, 
FileSelectParams *params)
                        filelist->filelist.nbr_entries_filtered = num_filtered;
                //      printf("Filetered: %d over %d entries\n", num_filtered, 
filelist->filelist.nbr_entries);
 
-                       filelist_cache_clear(&filelist->filelist_cache, 
filelist->filelist_cache.size);
+                       filelist_cache_clear(&filelist->filelist_cache, 
filelist->filelist_cache.size, filelist->ae);
 
                        MEM_freeN(filtered_tmp);
                }
@@ -1164,9 +1167,12 @@ static void filelist_cache_preview_freef(TaskPool * 
__restrict UNUSED(pool), voi
        }
 }
 
-static void filelist_cache_preview_ensure_running(FileListEntryCache *cache)
+static void filelist_cache_preview_ensure_running(FileListEntryCache *cache, 
AssetEngine *ae)
 {
-       if (!cache->previews_pool) {
+       if (ae) {
+               /* Nothing to do... */
+       }
+       else if (!cache->previews_pool) {
                TaskScheduler *scheduler = BLI_task_scheduler_get();
 
                cache->previews_pool = 
BLI_task_pool_create_background(scheduler, cache);
@@ -1176,11 +1182,23 @@ static void 
filelist_cache_preview_ensure_running(FileListEntryCache *cache)
        }
 }
 
-static void filelist_cache_previews_clear(FileListEntryCache *cache)
+static void filelist_cache_previews_clear(FileListEntryCache *cache, 
AssetEngine *ae)
 {
-       FileListEntryPreview *preview;
+       if (ae) {
+               if (cache->ae_preview_job != AE_JOB_ID_UNSET) {
+                       ae->type->kill(ae, cache->ae_preview_job);
+                       cache->ae_preview_job = AE_JOB_ID_UNSET;
+               }
+               for (int i = cache->ae_preview_uuids.nbr_uuids; i--;) {
+                       MEM_SAFE_FREE(cache->ae_preview_uuids.uuids[i].ibuff);
+               }
+               MEM_SAFE_FREE(cache->ae_preview_uuids.uuids);
+               cache->ae_preview_uuids.nbr_uuids = 0;
+       }
 
        if (cache->previews_pool) {
+               FileListEntryPreview *preview;
+
                BLI_task_pool_cancel(cache->previews_pool);
 
                while ((preview = 
BLI_thread_queue_pop_timeout(cache->previews_done, 0))) {
@@ -1193,12 +1211,16 @@ static void 
filelist_cache_previews_clear(FileListEntryCache *cache)
        }
 }
 
-static void filelist_cache_previews_free(FileListEntryCache *cache)
+static void filelist_cache_previews_free(FileListEntryCache *cache, 
AssetEngine *ae)
 {
+       if (ae) {
+               filelist_cache_previews_clear(cache, ae);
+       }
+
        if (cache->previews_pool) {
                BLI_thread_queue_nowait(cache->previews_done);
 
-               filelist_cache_previews_clear(cache);
+               filelist_cache_previews_clear(cache, ae);
 
                BLI_thread_queue_free(cache->previews_done);
                BLI_task_pool_free(cache->previews_pool);
@@ -1214,24 +1236,41 @@ static void 
filelist_cache_previews_free(FileListEntryCache *cache)
 static void filelist_cache_previews_push(FileList *filelist, FileDirEntry 
*entry, const int index)
 {
        FileListEntryCache *cache = &filelist->filelist_cache;
+       const bool do_preview = ((filelist->ae && 
filelist->ae->type->previews_get != NULL) ||
+                                (entry->typeflag & (FILE_TYPE_IMAGE | 
FILE_TYPE_MOVIE | FILE_TYPE_FTFONT |
+                                                    FILE_TYPE_BLENDER | 
FILE_TYPE_BLENDER_BACKUP | FILE_TYPE_BLENDERLIB)));
 
        BLI_assert(cache->flags & FLC_PREVIEWS_ACTIVE);
 
-       if (!entry->image &&
-           !(entry->flags & FILE_ENTRY_INVALID_PREVIEW) &&
-           (entry->typeflag & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | 
FILE_TYPE_FTFONT |
-                               FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP | 
FILE_TYPE_BLENDERLIB)))
-       {
-               FileListEntryPreview *preview = MEM_mallocN(sizeof(*preview), 
__func__);
-               BLI_join_dirfile(preview->path, sizeof(preview->path), 
filelist->filelist.root, entry->relpath);
-               preview->index = index;
-               preview->flags = entry->typeflag;
-               preview->img = NULL;
-//             printf("%s: %d - %s - %p\n", __func__, preview->index, 
preview->path, preview->img);
+       if (do_preview && !entry->image && !(entry->flags & 
FILE_ENTRY_INVALID_PREVIEW)) {
+               if (filelist->ae) {
+                       /* TODO Really have to find better way to handle this 
than realloc... */
+                       AssetUUIDList *uuids = &cache->ae_preview_uuids;
 
-               filelist_cache_preview_ensure_running(cache);
-               BLI_task_pool_push_ex(cache->previews_pool, 
filelist_cache_preview_runf, preview,
-                                     true, filelist_cache_preview_freef, 
TASK_PRIORITY_LOW);
+                       if (uuids->uuids) {
+                               BLI_assert(uuids->nbr_uuids != 0);
+                               uuids->uuids = MEM_recallocN(uuids->uuids, 
++uuids->nbr_uuids * sizeof(*uuids->uuids));
+                       }
+                       else {
+                               BLI_assert(uuids->nbr_uuids == 0);
+                               uuids->uuids = MEM_callocN(++uuids->nbr_uuids * 
sizeof(*uuids->uuids), __func__);
+                       }
+                       memcpy(uuids->uuids[uuids->nbr_uuids - 1].uuid_asset, 
entry->uuid,
+                              sizeof(uuids->uuids[uuids->nbr_uuids - 
1].uuid_asset));
+                       /* No real need to call ae->type->previews_get() here, 
update callback will do so anyway. */
+               }
+               else {
+                       FileListEntryPreview *preview = 
MEM_mallocN(sizeof(*preview), __func__);
+                       BLI_join_dirfile(preview->path, sizeof(preview->path), 
filelist->filelist.root, entry->relpath);
+                       preview->index = index;
+                       preview->flags = entry->typeflag;
+                       preview->img = NULL;
+//                     printf("%s: %d - %s - %p\n", __func__, preview->index, 
preview->path, preview->img);
+
+                       filelist_cache_preview_ensure_running(cache, 
filelist->ae);
+                       BLI_task_pool_push_ex(cache->previews_pool, 
filelist_cache_preview_runf, preview,
+                                                                 true, 
filelist_cache_preview_freef, TASK_PRIORITY_LOW);
+               }
        }
 }
 
@@ -1255,7 +1294,7 @@ static void filelist_cache_init(FileListEntryCache 
*cache, size_t cache_size)
        cache->flags = FLC_IS_INIT;
 }
 
-static void filelist_cache_free(FileListEntryCache *cache)
+static void filelist_cache_free(FileListEntryCache *cache, AssetEngine *ae)
 {
        FileDirEntry *entry, *entry_next;
 
@@ -1263,7 +1302,7 @@ static void filelist_cache_free(FileListEntryCache *cache)
                return;
        }
 
-       filelist_cache_previews_free(cache);
+       filelist_cache_previews_free(cache, ae);
 
        MEM_freeN(cache->block_entries);
 
@@ -1281,7 +1320,7 @@ static void filelist_cache_free(FileListEntryCache *cache)
        BLI_listbase_clear(&cache->cached_entries);
 }
 
-static void filelist_cache_clear(FileListEntryCache *cache, size_t new_size)
+static void filelist_cache_clear(FileListEntryCache *cache, size_t new_size, 
AssetEngine *ae)
 {
        FileDirEntry *entry, *entry_next;
 
@@ -1289,7 +1328,7 @@ static vo

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to