WebTexture2D.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <ThirdParty/CEF/include/cef_render_handler.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/Resource/ResourceCache.h>
  4. #include <Atomic/Graphics/Graphics.h>
  5. #include <Atomic/Graphics/Technique.h>
  6. #include "WebClient.h"
  7. #include "WebTexture2D.h"
  8. namespace Atomic
  9. {
  10. class WebTexture2DPrivate : public CefRenderHandler
  11. {
  12. friend class WebTexture2D;
  13. public:
  14. IMPLEMENT_REFCOUNTING(WebTexture2DPrivate)
  15. WebTexture2DPrivate(WebTexture2D* webTexture2D)
  16. {
  17. webTexture2D_ = webTexture2D;
  18. }
  19. bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
  20. {
  21. rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
  22. return true;
  23. }
  24. void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects,
  25. const void *buffer, int width, int height) OVERRIDE
  26. {
  27. if (type == PET_VIEW)
  28. {
  29. if (width == webTexture2D_->GetWidth() && height == webTexture2D_->GetHeight())
  30. {
  31. Texture2D* tex = webTexture2D_->texture_;
  32. tex->SetData(0, 0, 0, width, height, buffer);
  33. }
  34. }
  35. }
  36. private:
  37. WeakPtr<WebTexture2D> webTexture2D_;
  38. };
  39. WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
  40. {
  41. d_ = new WebTexture2DPrivate(this);
  42. d_->AddRef();
  43. texture_ = new Texture2D(context_);
  44. texture_->SetNumLevels(1);
  45. texture_->SetFilterMode(FILTER_BILINEAR);
  46. ResourceCache* cache = GetSubsystem<ResourceCache>();
  47. material_ = new Material(context_);
  48. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
  49. material_->SetTexture(TU_DIFFUSE, texture_);
  50. }
  51. WebTexture2D::~WebTexture2D()
  52. {
  53. //d_->Release();
  54. }
  55. CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
  56. {
  57. return d_;
  58. }
  59. int WebTexture2D::GetWidth() const
  60. {
  61. return texture_->GetWidth();
  62. }
  63. int WebTexture2D::GetHeight() const
  64. {
  65. return texture_->GetHeight();
  66. }
  67. void WebTexture2D::SetSize(int width, int height)
  68. {
  69. if (width == texture_->GetWidth() && height == texture_->GetHeight())
  70. return;
  71. texture_->SetSize(width, height, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
  72. }
  73. }