WebTexture2D.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #ifdef ATOMIC_PLATFORM_OSX
  2. #include <ThirdParty/GLEW/glew.h>
  3. #endif
  4. #include <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. #ifdef ATOMIC_PLATFORM_OSX
  70. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  71. const void *buffer, int width, int height) OVERRIDE
  72. {
  73. glEnable(GL_TEXTURE_2D);
  74. if (type == PET_VIEW)
  75. {
  76. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  77. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  78. if (dirtyRects.size() == 1 &&
  79. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  80. {
  81. Texture2D* tex = webTexture2D_->texture_;
  82. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  83. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  84. tex->SetData(0, 0, 0, width, height, buffer);
  85. }
  86. else
  87. {
  88. // Update just the dirty rectangles.
  89. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  90. for (; i != dirtyRects.end(); ++i)
  91. {
  92. const CefRect& rect = *i;
  93. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  94. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  95. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  96. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  97. buffer);
  98. }
  99. }
  100. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  101. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  102. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  103. glBindTexture(GL_TEXTURE_2D, 0);
  104. }
  105. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  106. {
  107. int skip_pixels = 0, x = popupRect_.x;
  108. int skip_rows = 0, y = popupRect_.y;
  109. int w = width;
  110. int h = height;
  111. int viewwidth = webTexture2D_->GetWidth();
  112. int viewheight = webTexture2D_->GetHeight();
  113. // Adjust the popup to fit inside the view.
  114. if (x < 0)
  115. {
  116. skip_pixels = -x;
  117. x = 0;
  118. }
  119. if (y < 0)
  120. {
  121. skip_rows = -y;
  122. y = 0;
  123. }
  124. if (x + w > viewwidth)
  125. {
  126. w -= x + w - viewwidth;
  127. }
  128. if (y + h > viewheight)
  129. {
  130. h -= y + h - viewheight;
  131. }
  132. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  133. // Update the popup rectangle.
  134. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  135. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  136. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  137. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  138. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  139. glBindTexture(GL_TEXTURE_2D, 0);
  140. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  141. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  142. }
  143. glDisable(GL_TEXTURE_2D);
  144. }
  145. #else
  146. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  147. const void *buffer, int width, int height) OVERRIDE
  148. {
  149. webTexture2D_->GetTexture2D()->SetData(0, 0, 0, width, height, buffer);
  150. }
  151. #endif
  152. private:
  153. CefRect popupRect_;
  154. CefRect popupRectOriginal_;
  155. WeakPtr<WebTexture2D> webTexture2D_;
  156. };
  157. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
  158. {
  159. d_ = new WebTexture2DPrivate(this);
  160. d_->AddRef();
  161. texture_ = new Texture2D(context_);
  162. texture_->SetNumLevels(1);
  163. texture_->SetFilterMode(FILTER_BILINEAR);
  164. ResourceCache* cache = GetSubsystem<ResourceCache>();
  165. material_ = new Material(context_);
  166. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
  167. material_->SetTexture(TU_DIFFUSE, texture_);
  168. }
  169. WebTexture2D::~WebTexture2D()
  170. {
  171. //d_->Release();
  172. }
  173. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  174. {
  175. return d_;
  176. }
  177. int WebTexture2D::GetWidth() const
  178. {
  179. return texture_->GetWidth();
  180. }
  181. int WebTexture2D::GetHeight() const
  182. {
  183. return texture_->GetHeight();
  184. }
  185. void WebTexture2D::SetSize(int width, int height)
  186. {
  187. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  188. return;
  189. texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC);
  190. }
  191. }