summaryrefslogtreecommitdiffstats
path: root/ui/gfx/gl/gl_surface_android.cc
blob: 7f66fd867effe144583ad610bc0c7788b9844309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/gl/gl_surface_android.h"

#include <EGL/egl.h>

#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "ui/gfx/gl/egl_util.h"
#include "ui/gfx/gl/gl_bindings.h"
#include "ui/gfx/gl/gl_context.h"
#include "ui/gfx/gl/gl_implementation.h"
#include "ui/gfx/gl/android_native_window.h"

namespace gfx {

bool GLSurface::InitializeOneOffInternal() {
  static bool initialized = false;
  if (initialized)
    return true;

  switch (GetGLImplementation()) {
    case kGLImplementationEGLGLES2:
      if (!GLSurfaceEGL::InitializeOneOff()) {
        LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
        return false;
      }
      break;
    default:
      NOTREACHED();
      break;
  }

  initialized = true;
  return true;
}
// static
scoped_refptr<GLSurface>
GLSurface::CreateViewGLSurface(bool software, gfx::AcceleratedWidget window) {
  if (software)
    return NULL;

  switch (GetGLImplementation()) {
    case kGLImplementationEGLGLES2: {
      // window is unused
      scoped_refptr<AndroidViewSurface> surface(new AndroidViewSurface());
      if (!surface->Initialize())
        return NULL;
      return surface;
    }
    default:
      NOTREACHED();
      return NULL;
  }
}

// static
scoped_refptr<GLSurface>
GLSurface::CreateOffscreenGLSurface(bool software, const gfx::Size& size) {
  if (software)
    return NULL;

  switch (GetGLImplementation()) {
    case kGLImplementationEGLGLES2: {
      scoped_refptr<PbufferGLSurfaceEGL> surface(
          new PbufferGLSurfaceEGL(false, size));
      if (!surface->Initialize())
        return NULL;
      return surface;
    }
    default:
      NOTREACHED();
      return NULL;
  }
}

AndroidViewSurface::AndroidViewSurface()
  : NativeViewGLSurfaceEGL(false, 0),
    pbuffer_surface_(new PbufferGLSurfaceEGL(false, Size(1, 1))),
    window_(NULL) {
}

AndroidViewSurface::~AndroidViewSurface() {
}

bool AndroidViewSurface::Initialize() {
  DCHECK(pbuffer_surface_.get());
  return pbuffer_surface_->Initialize();
}

void AndroidViewSurface::Destroy() {
  if (pbuffer_surface_.get()) {
    pbuffer_surface_->Destroy();
  } else {
    window_ = NULL;
  }
  NativeViewGLSurfaceEGL::Destroy();
}

bool AndroidViewSurface::IsOffscreen() {
  return false;
}

bool AndroidViewSurface::SwapBuffers() {
  if (!pbuffer_surface_.get())
    return NativeViewGLSurfaceEGL::SwapBuffers();
  return true;
}

gfx::Size AndroidViewSurface::GetSize() {
  if (pbuffer_surface_.get())
    return pbuffer_surface_->GetSize();
  else
    return NativeViewGLSurfaceEGL::GetSize();
}

EGLSurface AndroidViewSurface::GetHandle() {
  if (pbuffer_surface_.get())
    return pbuffer_surface_->GetHandle();
  else
    return NativeViewGLSurfaceEGL::GetHandle();
}

bool AndroidViewSurface::Resize(const gfx::Size& size) {
  if (pbuffer_surface_.get())
    return pbuffer_surface_->Resize(size);
  else if (GetHandle()) {
    DCHECK(window_ && window_->GetNativeHandle());
    // Deactivate and restore any currently active context.
    EGLContext context = eglGetCurrentContext();
    if (context != EGL_NO_CONTEXT) {
      eglMakeCurrent(GetDisplay(), EGL_NO_SURFACE, EGL_NO_SURFACE,
          EGL_NO_CONTEXT);
    }
    NativeViewGLSurfaceEGL::Destroy();
    if (CreateWindowSurface(window_)) {
      if (context != EGL_NO_CONTEXT)
        eglMakeCurrent(GetDisplay(), GetHandle(), GetHandle(), context);
    }
  }
  return true;
}

bool AndroidViewSurface::CreateWindowSurface(AndroidNativeWindow* window) {
  DCHECK(window->GetNativeHandle());
  window_ = window;
  EGLSurface surface = eglCreateWindowSurface(GetDisplay(),
                                              GetConfig(),
                                              window->GetNativeHandle(),
                                              NULL);
  if (surface == EGL_NO_SURFACE) {
    LOG(ERROR) << "eglCreateWindowSurface failed with error "
               << GetLastEGLErrorString();
    Destroy();
    return false;
  }

  SetHandle(surface);
  return true;
}

void AndroidViewSurface::SetNativeWindow(AndroidNativeWindow* window) {
  if (window->GetNativeHandle()) {
    DCHECK(pbuffer_surface_.get());
    pbuffer_surface_->Destroy();
    pbuffer_surface_ = NULL;

    CreateWindowSurface(window);
  } else {
    DCHECK(GetHandle());
    NativeViewGLSurfaceEGL::Destroy();
    window_ = NULL;

    pbuffer_surface_ = new PbufferGLSurfaceEGL(false, Size(1,1));
    pbuffer_surface_->Initialize();
  }
}

}  // namespace gfx