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();
|
|
|
|
}
|
2014-12-22 14:06:41 +00:00
|
|
|
fGpuDevice.reset(NULL);
|
|
|
|
fCanvas.reset(NULL);
|
2015-02-11 06:27:48 +00:00
|
|
|
|
|
|
|
fCurContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) 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();
|
|
|
|
|
|
|
|
fGpuDevice.reset(NULL);
|
|
|
|
fCanvas.reset(NULL);
|
2014-03-10 16:04:47 +00:00
|
|
|
|
2012-10-25 18:43:28 +00:00
|
|
|
GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height());
|
2013-02-08 21:22:09 +00:00
|
|
|
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
2014-12-22 14:06:41 +00:00
|
|
|
SkAutoTUnref<GrRenderTarget> curRenderTarget(fCurContext->wrapBackendRenderTarget(desc));
|
2015-01-16 19:01:44 +00:00
|
|
|
SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
|
|
|
|
fGpuDevice.reset(SkGpuDevice::Create(curRenderTarget, &props));
|
2014-12-22 14:06:41 +00:00
|
|
|
fCanvas.reset(new SkCanvas(fGpuDevice));
|
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();
|
2014-12-22 14:06:41 +00:00
|
|
|
fDebugger->draw(fCanvas.get());
|
2012-08-07 16:12:23 +00:00
|
|
|
// TODO(chudy): Implement an optional flush button in Gui.
|
|
|
|
fCanvas->flush();
|
|
|
|
emit drawComplete();
|
|
|
|
}
|
2012-07-26 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-25 18:43:28 +00:00
|
|
|
GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) {
|
|
|
|
GrBackendRenderTargetDesc desc;
|
2013-12-17 19:22:07 +00:00
|
|
|
desc.fWidth = SkScalarRoundToInt(this->width());
|
|
|
|
desc.fHeight = SkScalarRoundToInt(this->height());
|
2013-02-07 14:43:04 +00:00
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
2012-07-26 19:38:22 +00:00
|
|
|
GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt);
|
|
|
|
GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits);
|
|
|
|
GrGLint buffer;
|
|
|
|
GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
|
|
|
|
desc.fRenderTargetHandle = buffer;
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
2013-02-13 13:26:13 +00:00
|
|
|
|
|
|
|
#endif
|