color-source.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <obs-module.h>
  2. struct color_source {
  3. struct vec4 color;
  4. struct vec4 color_srgb;
  5. uint32_t width;
  6. uint32_t height;
  7. obs_source_t *src;
  8. };
  9. static const char *color_source_get_name(void *unused)
  10. {
  11. UNUSED_PARAMETER(unused);
  12. return obs_module_text("ColorSource");
  13. }
  14. static void color_source_update(void *data, obs_data_t *settings)
  15. {
  16. struct color_source *context = data;
  17. uint32_t color = (uint32_t)obs_data_get_int(settings, "color");
  18. uint32_t width = (uint32_t)obs_data_get_int(settings, "width");
  19. uint32_t height = (uint32_t)obs_data_get_int(settings, "height");
  20. vec4_from_rgba(&context->color, color);
  21. vec4_from_rgba_srgb(&context->color_srgb, color);
  22. context->width = width;
  23. context->height = height;
  24. }
  25. static void *color_source_create(obs_data_t *settings, obs_source_t *source)
  26. {
  27. UNUSED_PARAMETER(source);
  28. struct color_source *context = bzalloc(sizeof(struct color_source));
  29. context->src = source;
  30. color_source_update(context, settings);
  31. return context;
  32. }
  33. static void color_source_destroy(void *data)
  34. {
  35. bfree(data);
  36. }
  37. static obs_properties_t *color_source_properties(void *unused)
  38. {
  39. UNUSED_PARAMETER(unused);
  40. obs_properties_t *props = obs_properties_create();
  41. obs_properties_add_color_alpha(props, "color",
  42. obs_module_text("ColorSource.Color"));
  43. obs_properties_add_int(props, "width",
  44. obs_module_text("ColorSource.Width"), 0, 4096,
  45. 1);
  46. obs_properties_add_int(props, "height",
  47. obs_module_text("ColorSource.Height"), 0, 4096,
  48. 1);
  49. return props;
  50. }
  51. static void color_source_render_helper(struct color_source *context,
  52. struct vec4 *colorVal)
  53. {
  54. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  55. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  56. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  57. gs_effect_set_vec4(color, colorVal);
  58. gs_technique_begin(tech);
  59. gs_technique_begin_pass(tech, 0);
  60. gs_draw_sprite(0, 0, context->width, context->height);
  61. gs_technique_end_pass(tech);
  62. gs_technique_end(tech);
  63. }
  64. static void color_source_render(void *data, gs_effect_t *effect)
  65. {
  66. UNUSED_PARAMETER(effect);
  67. struct color_source *context = data;
  68. /* need linear path for correct alpha blending */
  69. const bool linear_srgb = gs_get_linear_srgb() ||
  70. (context->color.w < 1.0f);
  71. const bool previous = gs_framebuffer_srgb_enabled();
  72. gs_enable_framebuffer_srgb(linear_srgb);
  73. if (linear_srgb)
  74. color_source_render_helper(context, &context->color_srgb);
  75. else
  76. color_source_render_helper(context, &context->color);
  77. gs_enable_framebuffer_srgb(previous);
  78. }
  79. static uint32_t color_source_getwidth(void *data)
  80. {
  81. struct color_source *context = data;
  82. return context->width;
  83. }
  84. static uint32_t color_source_getheight(void *data)
  85. {
  86. struct color_source *context = data;
  87. return context->height;
  88. }
  89. static void color_source_defaults_v1(obs_data_t *settings)
  90. {
  91. obs_data_set_default_int(settings, "color", 0xFFFFFFFF);
  92. obs_data_set_default_int(settings, "width", 400);
  93. obs_data_set_default_int(settings, "height", 400);
  94. }
  95. static void color_source_defaults_v2(obs_data_t *settings)
  96. {
  97. obs_data_set_default_int(settings, "color", 0xFFFFFFFF);
  98. obs_data_set_default_int(settings, "width", 1920);
  99. obs_data_set_default_int(settings, "height", 1080);
  100. }
  101. static void color_source_defaults_v3(obs_data_t *settings)
  102. {
  103. obs_data_set_default_int(settings, "color", 0xFFD1D1D1);
  104. obs_data_set_default_int(settings, "width", 1920);
  105. obs_data_set_default_int(settings, "height", 1080);
  106. }
  107. struct obs_source_info color_source_info_v1 = {
  108. .id = "color_source",
  109. .type = OBS_SOURCE_TYPE_INPUT,
  110. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  111. OBS_SOURCE_CAP_OBSOLETE,
  112. .create = color_source_create,
  113. .destroy = color_source_destroy,
  114. .update = color_source_update,
  115. .get_name = color_source_get_name,
  116. .get_defaults = color_source_defaults_v1,
  117. .get_width = color_source_getwidth,
  118. .get_height = color_source_getheight,
  119. .video_render = color_source_render,
  120. .get_properties = color_source_properties,
  121. .icon_type = OBS_ICON_TYPE_COLOR,
  122. };
  123. struct obs_source_info color_source_info_v2 = {
  124. .id = "color_source",
  125. .version = 2,
  126. .type = OBS_SOURCE_TYPE_INPUT,
  127. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  128. OBS_SOURCE_CAP_OBSOLETE,
  129. .create = color_source_create,
  130. .destroy = color_source_destroy,
  131. .update = color_source_update,
  132. .get_name = color_source_get_name,
  133. .get_defaults = color_source_defaults_v2,
  134. .get_width = color_source_getwidth,
  135. .get_height = color_source_getheight,
  136. .video_render = color_source_render,
  137. .get_properties = color_source_properties,
  138. .icon_type = OBS_ICON_TYPE_COLOR,
  139. };
  140. struct obs_source_info color_source_info_v3 = {
  141. .id = "color_source",
  142. .version = 3,
  143. .type = OBS_SOURCE_TYPE_INPUT,
  144. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  145. OBS_SOURCE_SRGB,
  146. .create = color_source_create,
  147. .destroy = color_source_destroy,
  148. .update = color_source_update,
  149. .get_name = color_source_get_name,
  150. .get_defaults = color_source_defaults_v3,
  151. .get_width = color_source_getwidth,
  152. .get_height = color_source_getheight,
  153. .video_render = color_source_render,
  154. .get_properties = color_source_properties,
  155. .icon_type = OBS_ICON_TYPE_COLOR,
  156. };