color-source.c 5.1 KB

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