[M47_2526] Chromium upversion to m47_2526 branch
[platform/framework/web/chromium-efl.git] / tizen_src / chromium_impl / ui / gl / gl_shared_context_efl.cc
1 // Copyright 2014 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/gl/gl_shared_context_efl.h"
6
7 #include <EGL/egl.h>
8 #include <Ecore_Evas.h>
9 #include <Evas_GL.h>
10
11 #include "base/threading/thread_restrictions.h"
12 #include "gpu/command_buffer/service/mailbox_manager_impl.h"
13 #include "ui/gl/gl_context.h"
14 #include "ui/gl/gl_share_group.h"
15 #include "ui/gl/gpu_timing.h"
16 #if defined(TIZEN_DISABLE_GPU_THREAD)
17 #include "base/command_line.h"
18 #endif
19
20 // Defined in gl_current_context_efl.cc because of conflict in chromium
21 // and efl gl includes.
22 extern void* GLGetCurentContext();
23
24 #if defined(TIZEN_DISABLE_GPU_THREAD)
25 class GLSharedContextEflPrivate
26     : public gfx::GLContext,
27       public gfx::GLCustomApi {
28  public:
29 #else
30 struct GLSharedContextEflPrivate : public gfx::GLContext {
31 #endif
32   GLSharedContextEflPrivate(Evas_Object* object)
33       : GLContext(GLSharedContextEfl::GetShareGroup()) {
34     Evas* evas =  evas_object_evas_get(object);
35     evas_gl_config_ = evas_gl_config_new();
36     evas_gl_config_->options_bits = EVAS_GL_OPTIONS_NONE;
37     evas_gl_config_->color_format = EVAS_GL_RGBA_8888;
38     evas_gl_config_->depth_bits = EVAS_GL_DEPTH_BIT_24;
39     evas_gl_config_->stencil_bits = EVAS_GL_STENCIL_BIT_8;
40
41     evas_gl_ = evas_gl_new(evas);
42     evas_gl_context_ = evas_gl_context_create(evas_gl_, 0);
43     if (!evas_gl_context_)
44       LOG(FATAL) << "GLSharedContextEflPrivate(): Create evas gl context Fail";
45
46     evas_gl_surface_ = evas_gl_surface_create(
47         evas_gl_, evas_gl_config_, 1, 1);
48     if (!evas_gl_surface_)
49       LOG(FATAL) << "GLSharedContextEflPrivate(): Create evas gl Surface Fail";
50
51     evas_gl_make_current(evas_gl_, evas_gl_surface_, evas_gl_context_);
52     handle_ = GLGetCurentContext();
53     CHECK(handle_ != EGL_NO_CONTEXT);
54     evas_gl_make_current(evas_gl_, 0, 0);
55
56 #if defined(TIZEN_DISABLE_GPU_THREAD)
57     use_native_surface_ =
58         base::CommandLine::ForCurrentProcess()->HasSwitch("disable-gpu-thread");
59
60     if (use_native_surface_) {
61       evas_gl_api_ = evas_gl_api_get(evas_gl_);
62       GLSharedContextEfl::GetShareGroup()->SetSharedContext(this);
63     }
64     delegate_ = 0;
65 #endif
66   }
67
68   bool Initialize(
69       gfx::GLSurface*, gfx::GpuPreference) override {
70 #if defined(TIZEN_DISABLE_GPU_THREAD)
71     return use_native_surface_;
72 #else
73     NOTREACHED();
74     return false;
75 #endif
76   }
77
78 #if defined(TIZEN_DISABLE_GPU_THREAD)
79   bool MakeCurrent(gfx::GLSurface* surface) override {
80     if (use_native_surface_) {
81       if (delegate_)
82         delegate_->MakeCurrent();
83
84       SetRealGLApi(this);
85       GLContext::SetCurrent(surface);
86       return true;
87     } else
88       return false;
89   }
90
91   void GetIntegerv(GLenum pname, GLint* params) override {
92     evas_gl_api_->glGetIntegerv(pname, params);
93   }
94
95   void Clear(GLbitfield mask) override {
96     evas_gl_api_->glClear(mask);
97   }
98
99   void ClearColor(
100     GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) override {
101     evas_gl_api_->glClearColor(red, green, blue, alpha);
102   }
103
104   void Viewport(GLint x, GLint y, GLsizei width, GLsizei height) override {
105     evas_gl_api_->glViewport(x, y, width, height);
106   }
107
108   void BindFramebufferEXT(GLenum target, GLuint framebuffer) override {
109     evas_gl_api_->glBindFramebuffer(target, framebuffer);
110   }
111
112   void Scissor(GLint x, GLint y, GLsizei width, GLsizei height) override {
113     evas_gl_api_->glScissor(x, y, width, height);
114   }
115
116   void Disable(GLenum cap) {
117     evas_gl_api_->glDisable(cap);
118   }
119
120   void Enable(GLenum cap) {
121     evas_gl_api_->glEnable(cap);
122   }
123 #else
124   bool MakeCurrent(gfx::GLSurface*) override {
125     NOTREACHED();
126     return false;
127   }
128 #endif
129
130   void ReleaseCurrent(gfx::GLSurface*) override {
131     NOTREACHED();
132   }
133
134   bool IsCurrent(gfx::GLSurface*) override {
135     NOTREACHED();
136     return false;
137   }
138
139   void* GetHandle() override {
140     return handle_;
141   }
142
143   virtual scoped_refptr<gfx::GPUTimingClient> CreateGPUTimingClient() override {
144     return 0;
145   }
146
147   virtual void OnSetSwapInterval(int interval) override {
148     NOTREACHED();
149   }
150
151  private:
152   friend struct GLSharedContextEfl;
153
154   ~GLSharedContextEflPrivate() override {
155     evas_gl_surface_destroy(evas_gl_, evas_gl_surface_);
156     evas_gl_context_destroy(evas_gl_, evas_gl_context_);
157     evas_gl_config_free(evas_gl_config_);
158     evas_gl_free(evas_gl_);
159   }
160
161   void* handle_;
162   Evas_GL* evas_gl_;
163   Evas_GL_Context* evas_gl_context_;
164   Evas_GL_Surface* evas_gl_surface_;
165   Evas_GL_Config* evas_gl_config_;
166 #if defined(TIZEN_DISABLE_GPU_THREAD)
167   Evas_GL_API* evas_gl_api_;
168   bool use_native_surface_;
169   GLSharedContextDelegate* delegate_;
170 #endif
171 };
172
173 namespace {
174 static GLSharedContextEflPrivate* g_private_part = NULL;
175 }
176
177 // static
178 void GLSharedContextEfl::Initialize(Evas_Object* object) {
179   if (!g_private_part) {
180     base::ThreadRestrictions::ScopedAllowIO allow_io;
181     g_private_part = new GLSharedContextEflPrivate(object);
182   }
183 }
184
185 // static
186 gfx::GLContext* GLSharedContextEfl::GetInstance() {
187   return g_private_part;
188 }
189
190 // static
191 Evas_GL_Context* GLSharedContextEfl::GetEvasGLContext() {
192   return g_private_part->evas_gl_context_;
193 }
194
195 // static
196 gfx::GLShareGroup* GLSharedContextEfl::GetShareGroup() {
197   static scoped_refptr<gfx::GLShareGroup> share_group_ =
198       new gfx::GLShareGroup();
199   return share_group_.get();
200 }
201
202 #if defined(TIZEN_DISABLE_GPU_THREAD)
203 void GLSharedContextEfl::SetDelegate(GLSharedContextDelegate* delegate) {
204   g_private_part->delegate_ = delegate;
205 }
206 #endif