WebTexture2D.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ifdef ATOMIC_PLATFORM_OSX
  2. #include <ThirdParty/GLEW/glew.h>
  3. #endif
  4. #include <include/cef_render_handler.h>
  5. #include <Atomic/Math/Rect.h>
  6. #include <Atomic/IO/Log.h>
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Graphics/Graphics.h>
  9. #include <Atomic/Graphics/GraphicsImpl.h>
  10. #include <Atomic/Graphics/Technique.h>
  11. #include "WebClient.h"
  12. #include "WebTexture2D.h"
  13. namespace Atomic
  14. {
  15. class WebTexture2DPrivate : public CefRenderHandler
  16. {
  17. friend class WebTexture2D;
  18. public:
  19. IMPLEMENT_REFCOUNTING(WebTexture2DPrivate)
  20. WebTexture2DPrivate(WebTexture2D* webTexture2D)
  21. {
  22. webTexture2D_ = webTexture2D;
  23. ClearPopupRects();
  24. }
  25. void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) OVERRIDE
  26. {
  27. if (!show)
  28. {
  29. // Clear the popup rectangle.
  30. ClearPopupRects();
  31. }
  32. }
  33. void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) OVERRIDE
  34. {
  35. if (rect.width <= 0 || rect.height <= 0)
  36. {
  37. ClearPopupRects();
  38. return;
  39. }
  40. popupRectOriginal_ = rect;
  41. popupRect_ = GetPopupRectInWebView(popupRectOriginal_);
  42. }
  43. CefRect GetPopupRectInWebView(const CefRect& original_rect) {
  44. CefRect rc(original_rect);
  45. // if x or y are negative, move them to 0.
  46. if (rc.x < 0)
  47. rc.x = 0;
  48. if (rc.y < 0)
  49. rc.y = 0;
  50. // if popup goes outside the view, try to reposition origin
  51. if (rc.x + rc.width > webTexture2D_->GetWidth())
  52. rc.x = webTexture2D_->GetWidth() - rc.width;
  53. if (rc.y + rc.height > webTexture2D_->GetHeight())
  54. rc.y = webTexture2D_->GetHeight() - rc.height;
  55. // if x or y became negative, move them to 0 again.
  56. if (rc.x < 0)
  57. rc.x = 0;
  58. if (rc.y < 0)
  59. rc.y = 0;
  60. return rc;
  61. }
  62. void ClearPopupRects()
  63. {
  64. popupRect_.Set(0, 0, 0, 0);
  65. popupRectOriginal_.Set(0, 0, 0, 0);
  66. }
  67. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  68. {
  69. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  70. return true;
  71. }
  72. #ifdef ATOMIC_PLATFORM_OSX
  73. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  74. const void *buffer, int width, int height) OVERRIDE
  75. {
  76. glEnable(GL_TEXTURE_2D);
  77. if (type == PET_VIEW)
  78. {
  79. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  80. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  81. if (dirtyRects.size() == 1 &&
  82. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  83. {
  84. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  85. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  86. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  87. }
  88. else
  89. {
  90. // Update just the dirty rectangles.
  91. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  92. for (; i != dirtyRects.end(); ++i)
  93. {
  94. const CefRect& rect = *i;
  95. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  96. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  97. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  98. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  99. buffer);
  100. }
  101. }
  102. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  103. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  104. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  105. glBindTexture(GL_TEXTURE_2D, 0);
  106. }
  107. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  108. {
  109. int skip_pixels = 0, x = popupRect_.x;
  110. int skip_rows = 0, y = popupRect_.y;
  111. int w = width;
  112. int h = height;
  113. int viewwidth = webTexture2D_->GetWidth();
  114. int viewheight = webTexture2D_->GetHeight();
  115. // Adjust the popup to fit inside the view.
  116. if (x < 0)
  117. {
  118. skip_pixels = -x;
  119. x = 0;
  120. }
  121. if (y < 0)
  122. {
  123. skip_rows = -y;
  124. y = 0;
  125. }
  126. if (x + w > viewwidth)
  127. {
  128. w -= x + w - viewwidth;
  129. }
  130. if (y + h > viewheight)
  131. {
  132. h -= y + h - viewheight;
  133. }
  134. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  135. // Update the popup rectangle.
  136. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  137. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  138. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  139. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  140. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  141. glBindTexture(GL_TEXTURE_2D, 0);
  142. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  143. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  144. }
  145. glDisable(GL_TEXTURE_2D);
  146. }
  147. #else
  148. void D3D9Blit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false)
  149. {
  150. RECT d3dRect;
  151. d3dRect.left = dstRect.left_;
  152. d3dRect.top = dstRect.top_;
  153. d3dRect.right = dstRect.right_;
  154. d3dRect.bottom = dstRect.bottom_;
  155. int level = 0;
  156. DWORD flags = discard ? D3DLOCK_DISCARD : 0;
  157. D3DLOCKED_RECT d3dLockedRect;
  158. IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject();
  159. if (FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  160. {
  161. LOGERROR("WebTexture2D - Could not lock texture");
  162. return;
  163. }
  164. int width = dstRect.Width();
  165. int height = dstRect.Height();
  166. for (int j = 0; j < height; ++j)
  167. {
  168. unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch;
  169. memcpy(dst, src, width * 4);
  170. src += srcStride;
  171. }
  172. object->UnlockRect(level);
  173. }
  174. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  175. const void *buffer, int width, int height) OVERRIDE
  176. {
  177. if (type == PET_VIEW)
  178. {
  179. if (dirtyRects.size() == 1 &&
  180. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  181. {
  182. D3D9Blit(IntRect(0, 0, width, height), (unsigned char*) buffer, width * 4, true);
  183. return;
  184. }
  185. // Update just the dirty rectangles
  186. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  187. for (; i != dirtyRects.end(); ++i)
  188. {
  189. const CefRect& rect = *i;
  190. unsigned char* src = (unsigned char*) buffer;
  191. src += rect.y * (width * 4) + (rect.x * 4);
  192. D3D9Blit(IntRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height), src, width * 4, false);
  193. }
  194. }
  195. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  196. {
  197. int x = popupRect_.x;
  198. int y = popupRect_.y;
  199. int w = width;
  200. int h = height;
  201. int viewwidth = webTexture2D_->GetWidth();
  202. int viewheight = webTexture2D_->GetHeight();
  203. // Adjust the popup to fit inside the view.
  204. if (x < 0)
  205. {
  206. x = 0;
  207. }
  208. if (y < 0)
  209. {
  210. y = 0;
  211. }
  212. if (x + w > viewwidth)
  213. {
  214. w -= x + w - viewwidth;
  215. }
  216. if (y + h > viewheight)
  217. {
  218. h -= y + h - viewheight;
  219. }
  220. unsigned char* src = (unsigned char*) buffer;
  221. D3D9Blit(IntRect(x, y, x + w, y + h), src, width * 4, false);
  222. }
  223. }
  224. #endif
  225. private:
  226. CefRect popupRect_;
  227. CefRect popupRectOriginal_;
  228. WeakPtr<WebTexture2D> webTexture2D_;
  229. };
  230. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
  231. {
  232. d_ = new WebTexture2DPrivate(this);
  233. d_->AddRef();
  234. texture_ = new Texture2D(context_);
  235. texture_->SetNumLevels(1);
  236. texture_->SetFilterMode(FILTER_NEAREST);
  237. }
  238. WebTexture2D::~WebTexture2D()
  239. {
  240. //d_->Release();
  241. }
  242. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  243. {
  244. return d_;
  245. }
  246. int WebTexture2D::GetWidth() const
  247. {
  248. return texture_->GetWidth();
  249. }
  250. int WebTexture2D::GetHeight() const
  251. {
  252. return texture_->GetHeight();
  253. }
  254. void WebTexture2D::SetSize(int width, int height)
  255. {
  256. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  257. return;
  258. texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC);
  259. }
  260. }