WebTexture2D.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifdef ATOMIC_PLATFORM_OSX
  2. #include <ThirdParty/GLEW/glew.h>
  3. #endif
  4. #include <ThirdParty/CEF/include/cef_render_handler.h>
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Resource/ResourceCache.h>
  7. #include <Atomic/Graphics/Graphics.h>
  8. #include <Atomic/Graphics/Technique.h>
  9. #include "WebClient.h"
  10. #include "WebTexture2D.h"
  11. namespace Atomic
  12. {
  13. class WebTexture2DPrivate : public CefRenderHandler
  14. {
  15. friend class WebTexture2D;
  16. public:
  17. IMPLEMENT_REFCOUNTING(WebTexture2DPrivate)
  18. WebTexture2DPrivate(WebTexture2D* webTexture2D)
  19. {
  20. webTexture2D_ = webTexture2D;
  21. }
  22. void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) OVERRIDE
  23. {
  24. if (!show)
  25. {
  26. // Clear the popup rectangle.
  27. ClearPopupRects();
  28. }
  29. }
  30. void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) OVERRIDE
  31. {
  32. if (rect.width <= 0 || rect.height <= 0)
  33. {
  34. ClearPopupRects();
  35. return;
  36. }
  37. popupRectOriginal_ = rect;
  38. popupRect_ = GetPopupRectInWebView(popupRectOriginal_);
  39. }
  40. CefRect GetPopupRectInWebView(const CefRect& original_rect) {
  41. CefRect rc(original_rect);
  42. // if x or y are negative, move them to 0.
  43. if (rc.x < 0)
  44. rc.x = 0;
  45. if (rc.y < 0)
  46. rc.y = 0;
  47. // if popup goes outside the view, try to reposition origin
  48. if (rc.x + rc.width > webTexture2D_->GetWidth())
  49. rc.x = webTexture2D_->GetWidth() - rc.width;
  50. if (rc.y + rc.height > webTexture2D_->GetHeight())
  51. rc.y = webTexture2D_->GetHeight() - rc.height;
  52. // if x or y became negative, move them to 0 again.
  53. if (rc.x < 0)
  54. rc.x = 0;
  55. if (rc.y < 0)
  56. rc.y = 0;
  57. return rc;
  58. }
  59. void ClearPopupRects()
  60. {
  61. popupRect_.Set(0, 0, 0, 0);
  62. popupRectOriginal_.Set(0, 0, 0, 0);
  63. }
  64. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  65. {
  66. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  67. return true;
  68. }
  69. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  70. const void *buffer, int width, int height) OVERRIDE
  71. {
  72. glEnable(GL_TEXTURE_2D);
  73. if (type == PET_VIEW)
  74. {
  75. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  76. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  77. if (dirtyRects.size() == 1 &&
  78. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  79. {
  80. Texture2D* tex = webTexture2D_->texture_;
  81. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  82. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  83. tex->SetData(0, 0, 0, width, height, buffer);
  84. }
  85. else
  86. {
  87. // Update just the dirty rectangles.
  88. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  89. for (; i != dirtyRects.end(); ++i)
  90. {
  91. const CefRect& rect = *i;
  92. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  93. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  94. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  95. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  96. buffer);
  97. }
  98. }
  99. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  100. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  101. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  102. glBindTexture(GL_TEXTURE_2D, 0);
  103. }
  104. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  105. {
  106. int skip_pixels = 0, x = popupRect_.x;
  107. int skip_rows = 0, y = popupRect_.y;
  108. int w = width;
  109. int h = height;
  110. int viewwidth = webTexture2D_->GetWidth();
  111. int viewheight = webTexture2D_->GetHeight();
  112. // Adjust the popup to fit inside the view.
  113. if (x < 0)
  114. {
  115. skip_pixels = -x;
  116. x = 0;
  117. }
  118. if (y < 0)
  119. {
  120. skip_rows = -y;
  121. y = 0;
  122. }
  123. if (x + w > viewwidth)
  124. {
  125. w -= x + w - viewwidth;
  126. }
  127. if (y + h > viewheight)
  128. {
  129. h -= y + h - viewheight;
  130. }
  131. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  132. // Update the popup rectangle.
  133. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  134. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  135. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  136. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  137. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  138. glBindTexture(GL_TEXTURE_2D, 0);
  139. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  140. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  141. }
  142. glDisable(GL_TEXTURE_2D);
  143. }
  144. private:
  145. CefRect popupRect_;
  146. CefRect popupRectOriginal_;
  147. WeakPtr<WebTexture2D> webTexture2D_;
  148. };
  149. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
  150. {
  151. d_ = new WebTexture2DPrivate(this);
  152. d_->AddRef();
  153. texture_ = new Texture2D(context_);
  154. texture_->SetNumLevels(1);
  155. texture_->SetFilterMode(FILTER_BILINEAR);
  156. ResourceCache* cache = GetSubsystem<ResourceCache>();
  157. material_ = new Material(context_);
  158. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
  159. material_->SetTexture(TU_DIFFUSE, texture_);
  160. }
  161. WebTexture2D::~WebTexture2D()
  162. {
  163. //d_->Release();
  164. }
  165. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  166. {
  167. return d_;
  168. }
  169. int WebTexture2D::GetWidth() const
  170. {
  171. return texture_->GetWidth();
  172. }
  173. int WebTexture2D::GetHeight() const
  174. {
  175. return texture_->GetHeight();
  176. }
  177. void WebTexture2D::SetSize(int width, int height)
  178. {
  179. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  180. return;
  181. texture_->SetSize(width, height, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
  182. }
  183. }