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) {
|
2018-02-03 01:32:49 +00:00
|
|
|
fDisplayParams.fMSAASampleCount = GrNextPow2(fDisplayParams.fMSAASampleCount);
|
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() {
|
2017-07-25 14:05:01 +00:00
|
|
|
SkASSERT(!fContext);
|
2017-02-22 19:00:42 +00:00
|
|
|
|
2017-08-17 18:37:06 +00:00
|
|
|
fBackendContext = this->onInitializeContext();
|
2017-12-07 17:33:05 +00:00
|
|
|
fContext = GrContext::MakeGL(fBackendContext, fDisplayParams.fGrContextOptions);
|
2018-02-03 01:32:49 +00:00
|
|
|
if (!fContext && fDisplayParams.fMSAASampleCount > 1) {
|
2017-02-24 23:04:47 +00:00
|
|
|
fDisplayParams.fMSAASampleCount /= 2;
|
|
|
|
this->initializeContext();
|
|
|
|
return;
|
|
|
|
}
|
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();
|
2017-07-25 14:05:01 +00:00
|
|
|
fContext.reset();
|
2016-05-20 13:01:06 +00:00
|
|
|
}
|
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;
|
2017-12-19 18:15:02 +00:00
|
|
|
fbInfo.fFormat = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
|
|
|
|
? GR_GL_SRGB8_ALPHA8 : GR_GL_RGBA8;
|
2017-05-01 13:50:58 +00:00
|
|
|
|
|
|
|
GrBackendRenderTarget backendRT(fWidth,
|
|
|
|
fHeight,
|
|
|
|
fSampleCount,
|
|
|
|
fStencilBits,
|
|
|
|
fbInfo);
|
|
|
|
|
2017-07-25 14:05:01 +00:00
|
|
|
fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
|
2017-05-01 13:50:58 +00:00
|
|
|
kBottomLeft_GrSurfaceOrigin,
|
2017-12-19 18:15:02 +00:00
|
|
|
kRGBA_8888_SkColorType,
|
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
|