2016-06-02 19:16:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
#include <EGL/egl.h>
|
2017-08-17 18:37:06 +00:00
|
|
|
#include <GLES/gl.h>
|
|
|
|
#include "../GLWindowContext.h"
|
|
|
|
#include "WindowContextFactory_android.h"
|
|
|
|
#include "gl/GrGLInterface.h"
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
using sk_app::GLWindowContext;
|
|
|
|
using sk_app::DisplayParams;
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
namespace {
|
|
|
|
class GLWindowContext_android : public GLWindowContext {
|
|
|
|
public:
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
GLWindowContext_android(ANativeWindow*, const DisplayParams&);
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
~GLWindowContext_android() override;
|
|
|
|
|
|
|
|
void onSwapBuffers() override;
|
|
|
|
|
2017-08-17 18:37:06 +00:00
|
|
|
sk_sp<const GrGLInterface> onInitializeContext() override;
|
2016-07-26 19:02:50 +00:00
|
|
|
void onDestroyContext() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
EGLDisplay fDisplay;
|
|
|
|
EGLContext fEGLContext;
|
2017-06-02 14:29:21 +00:00
|
|
|
EGLSurface fSurfaceAndroid;
|
2016-07-26 19:02:50 +00:00
|
|
|
|
|
|
|
// For setDisplayParams and resize which call onInitializeContext with null platformData
|
|
|
|
ANativeWindow* fNativeWindow = nullptr;
|
2017-05-02 20:15:53 +00:00
|
|
|
|
|
|
|
typedef GLWindowContext INHERITED;
|
2016-07-26 19:02:50 +00:00
|
|
|
};
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2017-05-02 20:15:53 +00:00
|
|
|
GLWindowContext_android::GLWindowContext_android(ANativeWindow* window,
|
|
|
|
const DisplayParams& params)
|
|
|
|
: INHERITED(params)
|
2016-06-02 19:16:25 +00:00
|
|
|
, fDisplay(EGL_NO_DISPLAY)
|
|
|
|
, fEGLContext(EGL_NO_CONTEXT)
|
2017-06-02 14:29:21 +00:00
|
|
|
, fSurfaceAndroid(EGL_NO_SURFACE)
|
2016-07-26 19:02:50 +00:00
|
|
|
, fNativeWindow(window) {
|
2016-06-02 19:16:25 +00:00
|
|
|
|
|
|
|
// any config code here (particularly for msaa)?
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
this->initializeContext();
|
2016-06-02 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLWindowContext_android::~GLWindowContext_android() {
|
|
|
|
this->destroyContext();
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:37:06 +00:00
|
|
|
sk_sp<const GrGLInterface> GLWindowContext_android::onInitializeContext() {
|
2016-06-09 19:02:34 +00:00
|
|
|
fWidth = ANativeWindow_getWidth(fNativeWindow);
|
|
|
|
fHeight = ANativeWindow_getHeight(fNativeWindow);
|
2016-06-02 19:16:25 +00:00
|
|
|
|
|
|
|
fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
|
|
|
|
EGLint majorVersion;
|
|
|
|
EGLint minorVersion;
|
|
|
|
eglInitialize(fDisplay, &majorVersion, &minorVersion);
|
|
|
|
|
|
|
|
SkAssertResult(eglBindAPI(EGL_OPENGL_ES_API));
|
|
|
|
|
|
|
|
EGLint numConfigs = 0;
|
2018-02-03 01:32:49 +00:00
|
|
|
EGLint eglSampleCnt = fDisplayParams.fMSAASampleCount > 1 ? fDisplayParams.fMSAASampleCount > 1
|
|
|
|
: 0;
|
2016-06-02 19:16:25 +00:00
|
|
|
const EGLint configAttribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
2017-02-24 23:04:47 +00:00
|
|
|
EGL_STENCIL_SIZE, 8,
|
2018-02-03 01:32:49 +00:00
|
|
|
EGL_SAMPLE_BUFFERS, eglSampleCnt ? 1 : 0,
|
|
|
|
EGL_SAMPLES, eglSampleCnt,
|
2016-06-02 19:16:25 +00:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLConfig surfaceConfig;
|
|
|
|
SkAssertResult(eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs));
|
|
|
|
SkASSERT(numConfigs > 0);
|
|
|
|
|
|
|
|
static const EGLint kEGLContextAttribsForOpenGLES[] = {
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
fEGLContext = eglCreateContext(
|
|
|
|
fDisplay, surfaceConfig, nullptr, kEGLContextAttribsForOpenGLES);
|
|
|
|
SkASSERT(EGL_NO_CONTEXT != fEGLContext);
|
|
|
|
|
2016-06-14 15:23:48 +00:00
|
|
|
// SkDebugf("EGL: %d.%d", majorVersion, minorVersion);
|
|
|
|
// SkDebugf("Vendor: %s", eglQueryString(fDisplay, EGL_VENDOR));
|
|
|
|
// SkDebugf("Extensions: %s", eglQueryString(fDisplay, EGL_EXTENSIONS));
|
|
|
|
|
2018-07-09 14:23:19 +00:00
|
|
|
fSurfaceAndroid = eglCreateWindowSurface(fDisplay, surfaceConfig, fNativeWindow, nullptr);
|
2017-06-02 14:29:21 +00:00
|
|
|
SkASSERT(EGL_NO_SURFACE != fSurfaceAndroid);
|
2016-06-02 19:16:25 +00:00
|
|
|
|
2017-06-02 14:29:21 +00:00
|
|
|
SkAssertResult(eglMakeCurrent(fDisplay, fSurfaceAndroid, fSurfaceAndroid, fEGLContext));
|
2017-12-11 15:06:31 +00:00
|
|
|
// GLWindowContext::initializeContext will call GrGLMakeNativeInterface so we
|
2016-06-02 19:16:25 +00:00
|
|
|
// won't call it here.
|
|
|
|
|
|
|
|
glClearStencil(0);
|
|
|
|
glClearColor(0, 0, 0, 0);
|
|
|
|
glStencilMask(0xffffffff);
|
|
|
|
glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_STENCIL_SIZE, &fStencilBits);
|
|
|
|
eglGetConfigAttrib(fDisplay, surfaceConfig, EGL_SAMPLES, &fSampleCount);
|
2018-02-03 01:32:49 +00:00
|
|
|
fSampleCount = SkTMax(fSampleCount, 1);
|
2017-08-17 18:37:06 +00:00
|
|
|
|
2019-03-27 20:23:26 +00:00
|
|
|
eglSwapInterval(fDisplay, fDisplayParams.fDisableVsync ? 0 : 1);
|
|
|
|
|
2017-12-11 15:06:31 +00:00
|
|
|
return GrGLMakeNativeInterface();
|
2016-06-02 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext_android::onDestroyContext() {
|
2017-06-02 14:29:21 +00:00
|
|
|
if (!fDisplay || !fEGLContext || !fSurfaceAndroid) {
|
2016-06-02 19:16:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
2017-06-02 14:29:21 +00:00
|
|
|
SkAssertResult(eglDestroySurface(fDisplay, fSurfaceAndroid));
|
2016-06-02 19:16:25 +00:00
|
|
|
SkAssertResult(eglDestroyContext(fDisplay, fEGLContext));
|
|
|
|
fEGLContext = EGL_NO_CONTEXT;
|
2017-06-02 14:29:21 +00:00
|
|
|
fSurfaceAndroid = EGL_NO_SURFACE;
|
2016-06-02 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext_android::onSwapBuffers() {
|
2017-06-02 14:29:21 +00:00
|
|
|
if (fDisplay && fEGLContext && fSurfaceAndroid) {
|
|
|
|
eglSwapBuffers(fDisplay, fSurfaceAndroid);
|
2016-06-02 19:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
namespace window_context_factory {
|
|
|
|
|
|
|
|
WindowContext* NewGLForAndroid(ANativeWindow* window, const DisplayParams& params) {
|
|
|
|
WindowContext* ctx = new GLWindowContext_android(window, params);
|
|
|
|
if (!ctx->isValid()) {
|
|
|
|
delete ctx;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ctx;
|
2016-06-02 19:16:25 +00:00
|
|
|
}
|
2016-07-26 19:02:50 +00:00
|
|
|
|
|
|
|
} // namespace window_context_factory
|
|
|
|
} // namespace sk_app
|