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. Texture2D* tex = webTexture2D_->texture_;
  85. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  86. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  87. tex->SetData(0, 0, 0, width, height, buffer);
  88. }
  89. else
  90. {
  91. // Update just the dirty rectangles.
  92. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  93. for (; i != dirtyRects.end(); ++i)
  94. {
  95. const CefRect& rect = *i;
  96. glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect.x);
  97. glPixelStorei(GL_UNPACK_SKIP_ROWS, rect.y);
  98. glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width,
  99. rect.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  100. buffer);
  101. }
  102. }
  103. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  104. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  105. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  106. glBindTexture(GL_TEXTURE_2D, 0);
  107. }
  108. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  109. {
  110. int skip_pixels = 0, x = popupRect_.x;
  111. int skip_rows = 0, y = popupRect_.y;
  112. int w = width;
  113. int h = height;
  114. int viewwidth = webTexture2D_->GetWidth();
  115. int viewheight = webTexture2D_->GetHeight();
  116. // Adjust the popup to fit inside the view.
  117. if (x < 0)
  118. {
  119. skip_pixels = -x;
  120. x = 0;
  121. }
  122. if (y < 0)
  123. {
  124. skip_rows = -y;
  125. y = 0;
  126. }
  127. if (x + w > viewwidth)
  128. {
  129. w -= x + w - viewwidth;
  130. }
  131. if (y + h > viewheight)
  132. {
  133. h -= y + h - viewheight;
  134. }
  135. glBindTexture(GL_TEXTURE_2D, webTexture2D_->GetTexture2D()->GetGPUObject());
  136. // Update the popup rectangle.
  137. glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
  138. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
  139. glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
  140. glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA,
  141. GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
  142. glBindTexture(GL_TEXTURE_2D, 0);
  143. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  144. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  145. }
  146. glDisable(GL_TEXTURE_2D);
  147. }
  148. #else
  149. void D3D9Blit(const IntRect& dstRect, unsigned char* src, unsigned srcStride, bool discard = false)
  150. {
  151. RECT d3dRect;
  152. d3dRect.left = dstRect.left_;
  153. d3dRect.top = dstRect.top_;
  154. d3dRect.right = dstRect.right_;
  155. d3dRect.bottom = dstRect.bottom_;
  156. int level = 0;
  157. DWORD flags = discard ? D3DLOCK_DISCARD : 0;
  158. D3DLOCKED_RECT d3dLockedRect;
  159. IDirect3DTexture9* object = (IDirect3DTexture9*) webTexture2D_->GetTexture2D()->GetGPUObject();
  160. if (FAILED(object->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  161. {
  162. LOGERROR("WebTexture2D - Could not lock texture");
  163. return;
  164. }
  165. int width = dstRect.Width();
  166. int height = dstRect.Height();
  167. for (int j = 0; j < height; ++j)
  168. {
  169. unsigned char* dst = (unsigned char*) d3dLockedRect.pBits + j * d3dLockedRect.Pitch;
  170. memcpy(dst, src, width * 4);
  171. src += srcStride;
  172. }
  173. object->UnlockRect(level);
  174. }
  175. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  176. const void *buffer, int width, int height) OVERRIDE
  177. {
  178. if (type == PET_VIEW)
  179. {
  180. if (dirtyRects.size() == 1 &&
  181. dirtyRects[0] == CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight()))
  182. {
  183. D3D9Blit(IntRect(0, 0, width, height), (unsigned char*) buffer, width * 4, true);
  184. return;
  185. }
  186. // Update just the dirty rectangles
  187. CefRenderHandler::RectList::const_iterator i = dirtyRects.begin();
  188. for (; i != dirtyRects.end(); ++i)
  189. {
  190. const CefRect& rect = *i;
  191. unsigned char* src = (unsigned char*) buffer;
  192. src += rect.y * (width * 4) + (rect.x * 4);
  193. D3D9Blit(IntRect(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height), src, width * 4, false);
  194. }
  195. }
  196. else if (type == PET_POPUP && popupRect_.width > 0 && popupRect_.height > 0)
  197. {
  198. int x = popupRect_.x;
  199. int y = popupRect_.y;
  200. int w = width;
  201. int h = height;
  202. int viewwidth = webTexture2D_->GetWidth();
  203. int viewheight = webTexture2D_->GetHeight();
  204. // Adjust the popup to fit inside the view.
  205. if (x < 0)
  206. {
  207. x = 0;
  208. }
  209. if (y < 0)
  210. {
  211. y = 0;
  212. }
  213. if (x + w > viewwidth)
  214. {
  215. w -= x + w - viewwidth;
  216. }
  217. if (y + h > viewheight)
  218. {
  219. h -= y + h - viewheight;
  220. }
  221. unsigned char* src = (unsigned char*) buffer;
  222. D3D9Blit(IntRect(x, y, x + w, y + h), src, width * 4, false);
  223. }
  224. }
  225. #endif
  226. private:
  227. CefRect popupRect_;
  228. CefRect popupRectOriginal_;
  229. WeakPtr<WebTexture2D> webTexture2D_;
  230. };
  231. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
  232. {
  233. d_ = new WebTexture2DPrivate(this);
  234. d_->AddRef();
  235. texture_ = new Texture2D(context_);
  236. texture_->SetNumLevels(1);
  237. texture_->SetFilterMode(FILTER_NEAREST);
  238. }
  239. WebTexture2D::~WebTexture2D()
  240. {
  241. //d_->Release();
  242. }
  243. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  244. {
  245. return d_;
  246. }
  247. int WebTexture2D::GetWidth() const
  248. {
  249. return texture_->GetWidth();
  250. }
  251. int WebTexture2D::GetHeight() const
  252. {
  253. return texture_->GetHeight();
  254. }
  255. void WebTexture2D::SetSize(int width, int height)
  256. {
  257. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  258. return;
  259. texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC);
  260. }
  261. }