Changeset View
Standalone View
source/blender/modifiers/intern/MOD_gpenciltint.c
- This file was added.
| /* | |||||
| * ***** BEGIN GPL LICENSE BLOCK ***** | |||||
| * | |||||
| * This program is free software; you can redistribute it and/or | |||||
| * modify it under the terms of the GNU General Public License | |||||
| * as published by the Free Software Foundation; either version 2 | |||||
| * of the License, or (at your option) any later version. | |||||
| * | |||||
| * This program is distributed in the hope that it will be useful, | |||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
| * GNU General Public License for more details. | |||||
| * | |||||
| * You should have received a copy of the GNU General Public License | |||||
| * along with this program; if not, write to the Free Software Foundation, | |||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||||
| * | |||||
| * The Original Code is Copyright (C) 2005 by the Blender Foundation. | |||||
| * All rights reserved. | |||||
| * | |||||
| * Contributor(s): Antonio Vazquez | |||||
| * | |||||
| * ***** END GPL LICENSE BLOCK ***** | |||||
| * | |||||
| */ | |||||
| /** \file blender/modifiers/intern/MOD_gpenciltint.c | |||||
| * \ingroup modifiers | |||||
| */ | |||||
| #include <stdio.h> | |||||
| #include "DNA_scene_types.h" | |||||
| #include "DNA_object_types.h" | |||||
| #include "DNA_gpencil_types.h" | |||||
| #include "BLI_utildefines.h" | |||||
| #include "BLI_ghash.h" | |||||
| #include "BKE_global.h" | |||||
| #include "BKE_DerivedMesh.h" | |||||
| #include "BKE_gpencil.h" | |||||
| #include "BKE_paint.h" | |||||
| #include "MOD_modifiertypes.h" | |||||
| static void initData(ModifierData *md) | |||||
| { | |||||
| GpencilTintModifierData *gpmd = (GpencilTintModifierData *)md; | |||||
| gpmd->passindex = 0; | |||||
| gpmd->factor = 0; | |||||
| gpmd->layername[0] = '\0'; | |||||
| gpmd->flag |= GP_TINT_CREATE_COLORS; | |||||
| BKE_gpencil_batch_cache_alldirty(); | |||||
| } | |||||
campbellbarton: This had bad code smell, doing global call (which accesses `G.main`) within local modifier… | |||||
| static void copyData(ModifierData *md, ModifierData *target) | |||||
| { | |||||
| modifier_copyData_generic(md, target); | |||||
| } | |||||
| static DerivedMesh *applyModifier(ModifierData *md, const struct EvaluationContext *UNUSED(eval_ctx), Object *ob, | |||||
| DerivedMesh *UNUSED(dm), | |||||
| ModifierApplyFlag UNUSED(flag)) | |||||
| { | |||||
| GpencilTintModifierData *mmd = (GpencilTintModifierData *)md; | |||||
| bGPdata *gpd; | |||||
| Palette *newpalette = NULL; | |||||
| if ((!ob) || (!ob->gpd)) { | |||||
| return NULL; | |||||
| } | |||||
| gpd = ob->gpd; | |||||
| GHash *gh_layer = BLI_ghash_str_new("GP_Tint Layer modifier"); | |||||
| GHash *gh_color; | |||||
| for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) { | |||||
| for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) { | |||||
| for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) { | |||||
| /* look for palette */ | |||||
| gh_color = (GHash *)BLI_ghash_lookup(gh_layer, gps->palette->id.name); | |||||
| if (gh_color == NULL) { | |||||
| gh_color = BLI_ghash_str_new("GP_Tint Color modifier"); | |||||
| BLI_ghash_insert(gh_layer, gps->palette->id.name, gh_color); | |||||
| } | |||||
Not Done Inline ActionsLow priority: for slight performance improvement BLI_ghash_ensure_p can be used here (does the lookup and insertion in a single step). campbellbarton: Low priority: for slight performance improvement `BLI_ghash_ensure_p` can be used here (does… | |||||
| /* look for color */ | |||||
| PaletteColor *newpalcolor = (PaletteColor *)BLI_ghash_lookup(gh_color, gps->palcolor->info); | |||||
| if (newpalcolor == NULL) { | |||||
| if (mmd->flag & GP_TINT_CREATE_COLORS) { | |||||
| if (!newpalette) { | |||||
| newpalette = BKE_palette_add(G.main, "Palette"); | |||||
| } | |||||
| newpalcolor = BKE_palette_color_copy(newpalette, gps->palcolor); | |||||
| BLI_strncpy(gps->colorname, newpalcolor->info, sizeof(gps->colorname)); | |||||
| gps->palcolor = newpalcolor; | |||||
| } | |||||
| else { | |||||
| newpalcolor = gps->palcolor; | |||||
| } | |||||
| BLI_ghash_insert(gh_color, gps->palcolor->info, newpalcolor); | |||||
| BKE_gpencil_tint_modifier(-1, (GpencilTintModifierData *)md, ob, gpl, gps); | |||||
| } | |||||
| else { | |||||
| gps->palcolor = newpalcolor; | |||||
| } | |||||
| } | |||||
| } | |||||
Not Done Inline ActionsModifiers manipulating G.main means these modifiers can't be threaded (or will need to lock). campbellbarton: Modifiers manipulating `G.main` means these modifiers can't be threaded (or will need to lock). | |||||
Not Done Inline ActionsReally we need to thread when apply a modifier? are there any alternatives to use G.main? antoniov: Really we need to thread when apply a modifier? are there any alternatives to use G.main? | |||||
Not Done Inline ActionsThe problem is that with the depsgraph evaluation, multiple datablock get evaluated in parallel. Now, if GP modifiers get run during the standard depsgraph driven evaluation process, you're going to get crashes and other threading bugs. aligorith: The problem is that with the depsgraph evaluation, multiple datablock get evaluated in parallel. | |||||
Not Done Inline ActionsBut this is only evaluated when apply, no? so how the user can apply several at the same time? antoniov: But this is only evaluated when apply, no? so how the user can apply several at the same time? | |||||
Not Done Inline ActionsApply is not just when the user presses apply. - It is to calculate the result which may be animated. So its not unusual for apply to run on every frame-step, for every modifier that has animation. campbellbarton: Apply is not just when the user presses apply. - It is to calculate the result which may be… | |||||
| } | |||||
| /* free hash buffers */ | |||||
| GHashIterator *ihash = BLI_ghashIterator_new(gh_layer); | |||||
| while (!BLI_ghashIterator_done(ihash)) { | |||||
| GHash *gh = BLI_ghashIterator_getValue(ihash); | |||||
| if (gh) { | |||||
| BLI_ghash_free(gh, NULL, NULL); | |||||
| gh = NULL; | |||||
| } | |||||
| BLI_ghashIterator_step(ihash); | |||||
| } | |||||
| BLI_ghashIterator_free(ihash); | |||||
| if (gh_layer) { | |||||
| BLI_ghash_free(gh_layer, NULL, NULL); | |||||
| gh_layer = NULL; | |||||
| } | |||||
| return NULL; | |||||
| } | |||||
| ModifierTypeInfo modifierType_GpencilTint = { | |||||
| /* name */ "Tint", | |||||
| /* structName */ "GpencilTintModifierData", | |||||
| /* structSize */ sizeof(GpencilTintModifierData), | |||||
| /* type */ eModifierTypeType_Gpencil, | |||||
| /* flags */ eModifierTypeFlag_GpencilMod | eModifierTypeFlag_SupportsEditmode, | |||||
| /* copyData */ copyData, | |||||
| /* deformVerts */ NULL, | |||||
| /* deformMatrices */ NULL, | |||||
| /* deformVertsEM */ NULL, | |||||
| /* deformMatricesEM */ NULL, | |||||
| /* applyModifier */ applyModifier, | |||||
| /* applyModifierEM */ NULL, | |||||
| /* initData */ initData, | |||||
| /* requiredDataMask */ NULL, | |||||
| /* freeData */ NULL, | |||||
| /* isDisabled */ NULL, | |||||
| /* updateDepsgraph */ NULL, | |||||
| /* dependsOnTime */ NULL, | |||||
| /* dependsOnNormals */ NULL, | |||||
| /* foreachObjectLink */ NULL, | |||||
| /* foreachIDLink */ NULL, | |||||
| /* foreachTexLink */ NULL, | |||||
| }; | |||||
This had bad code smell, doing global call (which accesses G.main) within local modifier initialization seems like something that could easily backfire later on.
Isn't this something the depsgraph should handle?