tb_renderer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_RENDERER_H
  6. #define TB_RENDERER_H
  7. #include "tb_core.h"
  8. #include "tb_geometry.h"
  9. #include "tb_color.h"
  10. #include "tb_linklist.h"
  11. namespace tb {
  12. class TBBitmapFragment;
  13. /** TBRendererListener is a listener for TBRenderer. */
  14. class TBRendererListener : public TBLinkOf<TBRendererListener>
  15. {
  16. public:
  17. virtual ~TBRendererListener() {}
  18. /** Called when the context has been lost and all TBBitmaps need to be deleted.
  19. NOTE: Only do cleanup here. It's not safe to do work on any bitmap since the
  20. context is already lost. */
  21. virtual void OnContextLost() = 0;
  22. /** Called when the context has been restored again, and new TBBitmaps can be created
  23. again. */
  24. virtual void OnContextRestored() = 0;
  25. };
  26. /** TBBitmap is a minimal interface for bitmap to be painted by TBRenderer. */
  27. class TBBitmap
  28. {
  29. public:
  30. /** Note: Implementations for batched renderers should call TBRenderer::FlushBitmap
  31. to make sure any active batch is being flushed before the bitmap is deleted. */
  32. virtual ~TBBitmap() {}
  33. virtual int Width() = 0;
  34. virtual int Height() = 0;
  35. /** Update the bitmap with the given data (in BGRA32 format).
  36. Note: Implementations for batched renderers should call TBRenderer::FlushBitmap
  37. to make sure any active batch is being flushed before the bitmap is changed. */
  38. virtual void SetData(uint32 *data) = 0;
  39. };
  40. /** TBRenderer is a minimal interface for painting strings and bitmaps. */
  41. class TBRenderer
  42. {
  43. public:
  44. virtual ~TBRenderer() {}
  45. /** Should be called before invoking paint on any widget.
  46. render_target_w and render_target_h should be the size of the render target
  47. that the renderer renders to. I.e window size, screen size or frame buffer object. */
  48. virtual void BeginPaint(int render_target_w, int render_target_h) = 0;
  49. virtual void EndPaint() = 0;
  50. /** Translate all drawing with the given offset */
  51. virtual void Translate(int dx, int dy) = 0;
  52. /** Set the current opacity that should apply to all drawing (0.f-1.f). */
  53. virtual void SetOpacity(float opacity) = 0;
  54. virtual float GetOpacity() = 0;
  55. /** Set a clip rect to the renderer. add_to_current should be true when
  56. pushing a new cliprect that should clip inside the last clip rect,
  57. and false when restoring.
  58. It will return the clip rect that was in use before this call. */
  59. virtual TBRect SetClipRect(const TBRect &rect, bool add_to_current) = 0;
  60. /** Get the current clip rect. Note: This may be different from the rect
  61. sent to SetClipRect, due to intersecting with the previous cliprect! */
  62. virtual TBRect GetClipRect() = 0;
  63. /** Draw the src_rect part of the fragment stretched to dst_rect.
  64. dst_rect or src_rect can have negative width and height to achieve horizontal and vertical flip. */
  65. virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmapFragment *bitmap_fragment) = 0;
  66. /** Draw the src_rect part of the bitmap stretched to dst_rect.
  67. dst_rect or src_rect can have negative width and height to achieve horizontal and vertical flip. */
  68. virtual void DrawBitmap(const TBRect &dst_rect, const TBRect &src_rect, TBBitmap *bitmap) = 0;
  69. /** Draw the src_rect part of the fragment stretched to dst_rect.
  70. The bitmap will be used as a mask for the color.
  71. dst_rect or src_rect can have negative width and height to achieve horizontal and vertical flip. */
  72. virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmapFragment *bitmap_fragment) = 0;
  73. /** Draw the src_rect part of the bitmap stretched to dst_rect.
  74. The bitmap will be used as a mask for the color.
  75. dst_rect or src_rect can have negative width and height to achieve horizontal and vertical flip. */
  76. virtual void DrawBitmapColored(const TBRect &dst_rect, const TBRect &src_rect, const TBColor &color, TBBitmap *bitmap) = 0;
  77. /** Draw the bitmap tiled into dst_rect. */
  78. virtual void DrawBitmapTile(const TBRect &dst_rect, TBBitmap *bitmap) = 0;
  79. /** Draw a 1px thick rectangle outline. */
  80. virtual void DrawRect(const TBRect &dst_rect, const TBColor &color) = 0;
  81. /** Draw a filled rectangle. */
  82. virtual void DrawRectFill(const TBRect &dst_rect, const TBColor &color) = 0;
  83. /** Make sure the given bitmap fragment is flushed from any batching, because it may
  84. be changed or deleted after this call. */
  85. virtual void FlushBitmapFragment(TBBitmapFragment *bitmap_fragment) = 0;
  86. /** Create a new TBBitmap from the given data (in BGRA32 format).
  87. Width and height must be a power of two.
  88. Return nullptr if fail. */
  89. virtual TBBitmap *CreateBitmap(int width, int height, uint32 *data) = 0;
  90. /** Add a listener to this renderer. Does not take ownership. */
  91. void AddListener(TBRendererListener *listener) { m_listeners.AddLast(listener); }
  92. /** Remove a listener from this renderer. */
  93. void RemoveListener(TBRendererListener *listener) { m_listeners.Remove(listener); }
  94. /** Invoke OnContextLost on all listeners.
  95. Call when bitmaps should be forgotten. */
  96. void InvokeContextLost();
  97. /** Invoke OnContextRestored on all listeners.
  98. Call when bitmaps can safely be restored. */
  99. void InvokeContextRestored();
  100. /** Defines the hint given to BeginBatchHint. */
  101. enum BATCH_HINT {
  102. /** All calls are either DrawBitmap or DrawBitmapColored with the same bitmap
  103. fragment. */
  104. BATCH_HINT_DRAW_BITMAP_FRAGMENT
  105. };
  106. /** A hint to batching renderers that the following set of draw calls are of the
  107. same type so batching might be optimized.
  108. The hint defines what operations are allowed between BeginBatchHint
  109. until EndBatchHint is called. All other draw operations are invalid.
  110. It's not valid to nest calls to BeginBatchHint. */
  111. virtual void BeginBatchHint(BATCH_HINT hint) {}
  112. /** End the hint scope started with BeginBatchHint. */
  113. virtual void EndBatchHint() {}
  114. private:
  115. TBLinkListOf<TBRendererListener> m_listeners;
  116. };
  117. }; // namespace tb
  118. #endif // TB_RENDERER_H