2012-07-26 19:38:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "SkGLWidget.h"
|
|
|
|
|
2013-02-13 13:26:13 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
2012-08-07 16:12:23 +00:00
|
|
|
SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() {
|
|
|
|
fDebugger = debugger;
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkGLWidget::~SkGLWidget() {
|
|
|
|
}
|
|
|
|
|
2014-12-22 14:06:41 +00:00
|
|
|
void SkGLWidget::setSampleCount(int sampleCount) {
|
2013-08-02 13:59:50 +00:00
|
|
|
QGLFormat currentFormat = format();
|
|
|
|
currentFormat.setSampleBuffers(sampleCount > 0);
|
|
|
|
currentFormat.setSamples(sampleCount);
|
|
|
|
setFormat(currentFormat);
|
|
|
|
}
|
|
|
|
|
2012-07-26 19:38:22 +00:00
|
|
|
void SkGLWidget::initializeGL() {
|
2014-12-22 14:06:41 +00:00
|
|
|
if (!fCurIntf) {
|
|
|
|
fCurIntf.reset(GrGLCreateNativeInterface());
|
|
|
|
}
|
2013-07-01 13:54:10 +00:00
|
|
|
if (!fCurIntf) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-22 14:06:41 +00:00
|
|
|
// The call may come multiple times, for example after setSampleCount(). The QGLContext will be
|
|
|
|
// different, but we do not have a mechanism to catch the destroying of QGLContext, so that
|
2015-02-11 06:27:48 +00:00
|
|
|
// proper resource cleanup could be made.
|
|
|
|
if (fCurContext) {
|
|
|
|
fCurContext->abandonContext();
|
|
|
|
}
|
2016-08-01 12:53:23 +00:00
|
|
|
|
|
|
|
fGpuSurface = nullptr;
|
|
|
|
fCanvas = nullptr;
|
2015-02-11 06:27:48 +00:00
|
|
|
|
2017-07-25 14:05:01 +00:00
|
|
|
fCurContext = GrContext::MakeGL(fCurIntf.get());
|
2014-12-22 14:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkGLWidget::createRenderTarget() {
|
|
|
|
if (!fCurContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
2014-03-10 16:04:47 +00:00
|
|
|
glStencilMask(0xffffffff);
|
|
|
|
glClearStencil(0);
|
|
|
|
glClear(GL_STENCIL_BUFFER_BIT);
|
2014-12-22 14:06:41 +00:00
|
|
|
fCurContext->resetContext();
|
2017-08-31 20:04:00 +00:00
|
|
|
GrBackendRenderTarget backendRenderTarget = this->getBackendRenderTarget();
|
|
|
|
fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), backendRenderTarget,
|
|
|
|
kBottomLeft_GrSurfaceOrigin,
|
|
|
|
nullptr, nullptr);
|
2016-08-01 12:53:23 +00:00
|
|
|
fCanvas = fGpuSurface->getCanvas();
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkGLWidget::resizeGL(int w, int h) {
|
2014-12-22 14:06:41 +00:00
|
|
|
SkASSERT(w == this->width() && h == this->height());
|
|
|
|
this->createRenderTarget();
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkGLWidget::paintGL() {
|
2013-07-01 13:54:10 +00:00
|
|
|
if (!this->isHidden() && fCanvas) {
|
2015-02-11 06:27:48 +00:00
|
|
|
fCurContext->resetContext();
|
2016-08-01 12:53:23 +00:00
|
|
|
fDebugger->draw(fCanvas);
|
2012-08-07 16:12:23 +00:00
|
|
|
// TODO(chudy): Implement an optional flush button in Gui.
|
|
|
|
fCanvas->flush();
|
2016-03-01 17:34:38 +00:00
|
|
|
Q_EMIT drawComplete();
|
2012-08-07 16:12:23 +00:00
|
|
|
}
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:04:00 +00:00
|
|
|
GrBackendRenderTarget SkGLWidget::getBackendRenderTarget() {
|
|
|
|
GrGLFramebufferInfo info;
|
|
|
|
int stencilBits;
|
|
|
|
int sampleCnt;
|
|
|
|
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &info.fFBOID);
|
|
|
|
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &sampleCnt);
|
|
|
|
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &stencilBits);
|
|
|
|
return GrBackendRenderTarget(SkScalarRoundToInt(this->width()),
|
|
|
|
SkScalarRoundToInt(this->height()),
|
|
|
|
sampleCnt,
|
|
|
|
stencilBits,
|
|
|
|
kSkia8888_GrPixelConfig,
|
|
|
|
info);
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
2013-02-13 13:26:13 +00:00
|
|
|
|
|
|
|
#endif
|