2016-05-20 13:01:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-05-01 13:50:58 +00:00
|
|
|
#include "GrBackendSurface.h"
|
2016-05-20 13:01:06 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GLWindowContext.h"
|
|
|
|
|
|
|
|
#include "gl/GrGLDefines.h"
|
|
|
|
#include "gl/GrGLUtil.h"
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImage_Base.h"
|
2017-02-24 23:04:47 +00:00
|
|
|
#include "SkMathPriv.h"
|
2017-05-01 13:50:58 +00:00
|
|
|
#include "SkSurface.h"
|
2016-05-20 13:01:06 +00:00
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
GLWindowContext::GLWindowContext(const DisplayParams& params)
|
2017-05-02 20:15:53 +00:00
|
|
|
: WindowContext(params)
|
2016-05-20 13:01:06 +00:00
|
|
|
, fBackendContext(nullptr)
|
|
|
|
, fSurface(nullptr) {
|
2017-02-24 23:04:47 +00:00
|
|
|
fDisplayParams.fMSAASampleCount = fDisplayParams.fMSAASampleCount ?
|
|
|
|
GrNextPow2(fDisplayParams.fMSAASampleCount) :
|
|
|
|
0;
|
2016-07-26 19:02:50 +00:00
|
|
|
}
|
2016-05-20 13:01:06 +00:00
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
void GLWindowContext::initializeContext() {
|
|
|
|
this->onInitializeContext();
|
2016-05-20 13:01:06 +00:00
|
|
|
SkASSERT(nullptr == fContext);
|
2017-02-22 19:00:42 +00:00
|
|
|
|
|
|
|
fBackendContext.reset(GrGLCreateNativeInterface());
|
|
|
|
fContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fBackendContext.get(),
|
2017-02-28 00:00:53 +00:00
|
|
|
fDisplayParams.fGrContextOptions);
|
2017-02-24 23:04:47 +00:00
|
|
|
if (!fContext && fDisplayParams.fMSAASampleCount) {
|
|
|
|
fDisplayParams.fMSAASampleCount /= 2;
|
|
|
|
this->initializeContext();
|
|
|
|
return;
|
|
|
|
}
|
2016-05-20 13:01:06 +00:00
|
|
|
|
2017-02-24 23:04:47 +00:00
|
|
|
if (fContext) {
|
|
|
|
// We may not have real sRGB support (ANGLE, in particular), so check for
|
|
|
|
// that, and fall back to L32:
|
|
|
|
fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
|
|
|
|
? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
|
|
|
|
} else {
|
|
|
|
fPixelConfig = kUnknown_GrPixelConfig;
|
|
|
|
}
|
2016-05-20 13:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext::destroyContext() {
|
|
|
|
fSurface.reset(nullptr);
|
|
|
|
|
|
|
|
if (fContext) {
|
|
|
|
// in case we have outstanding refs to this guy (lua?)
|
|
|
|
fContext->abandonContext();
|
|
|
|
fContext->unref();
|
|
|
|
fContext = nullptr;
|
|
|
|
}
|
2017-05-01 13:50:58 +00:00
|
|
|
|
2016-05-20 13:01:06 +00:00
|
|
|
fBackendContext.reset(nullptr);
|
|
|
|
|
|
|
|
this->onDestroyContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
|
|
|
|
if (nullptr == fSurface) {
|
|
|
|
if (fContext) {
|
2017-05-01 13:50:58 +00:00
|
|
|
GrGLFramebufferInfo fbInfo;
|
2016-05-20 13:01:06 +00:00
|
|
|
GrGLint buffer;
|
2017-05-01 13:50:58 +00:00
|
|
|
GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
|
|
|
|
&buffer));
|
|
|
|
fbInfo.fFBOID = buffer;
|
|
|
|
|
|
|
|
GrBackendRenderTarget backendRT(fWidth,
|
|
|
|
fHeight,
|
|
|
|
fSampleCount,
|
|
|
|
fStencilBits,
|
|
|
|
fPixelConfig,
|
|
|
|
fbInfo);
|
|
|
|
|
|
|
|
fSurface = SkSurface::MakeFromBackendRenderTarget(fContext, backendRT,
|
|
|
|
kBottomLeft_GrSurfaceOrigin,
|
2017-02-08 15:47:28 +00:00
|
|
|
fDisplayParams.fColorSpace,
|
|
|
|
&fSurfaceProps);
|
2016-05-20 13:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext::swapBuffers() {
|
|
|
|
this->onSwapBuffers();
|
|
|
|
}
|
|
|
|
|
2016-07-27 15:50:12 +00:00
|
|
|
void GLWindowContext::resize(int w, int h) {
|
2016-05-20 13:01:06 +00:00
|
|
|
this->destroyContext();
|
2016-07-26 19:02:50 +00:00
|
|
|
this->initializeContext();
|
2016-05-20 13:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindowContext::setDisplayParams(const DisplayParams& params) {
|
|
|
|
this->destroyContext();
|
2016-07-26 19:02:50 +00:00
|
|
|
fDisplayParams = params;
|
|
|
|
this->initializeContext();
|
2016-05-20 13:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace sk_app
|