blob: a60ae0f0ba7529856e380514d13e39803d44fa07 [file] [log] [blame]
chudy@google.comea5488b2012-07-26 19:38:22 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkGLWidget.h"
11
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +000012#if SK_SUPPORT_GPU
13
chudy@google.com607357f2012-08-07 16:12:23 +000014SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() {
chudy@google.com607357f2012-08-07 16:12:23 +000015 fDebugger = debugger;
chudy@google.comea5488b2012-07-26 19:38:22 +000016}
17
18SkGLWidget::~SkGLWidget() {
chudy@google.comea5488b2012-07-26 19:38:22 +000019}
20
kkinnunen4be02382014-12-22 06:06:41 -080021void SkGLWidget::setSampleCount(int sampleCount) {
commit-bot@chromium.orgfde1e7c2013-08-02 13:59:50 +000022 QGLFormat currentFormat = format();
23 currentFormat.setSampleBuffers(sampleCount > 0);
24 currentFormat.setSamples(sampleCount);
25 setFormat(currentFormat);
26}
27
chudy@google.comea5488b2012-07-26 19:38:22 +000028void SkGLWidget::initializeGL() {
kkinnunen4be02382014-12-22 06:06:41 -080029 if (!fCurIntf) {
30 fCurIntf.reset(GrGLCreateNativeInterface());
31 }
bungeman@google.com0b4d6b22013-07-01 13:54:10 +000032 if (!fCurIntf) {
33 return;
34 }
kkinnunen4be02382014-12-22 06:06:41 -080035 // The call may come multiple times, for example after setSampleCount(). The QGLContext will be
36 // different, but we do not have a mechanism to catch the destroying of QGLContext, so that
kkinnunen9e33d1d2015-02-10 22:27:48 -080037 // proper resource cleanup could be made.
38 if (fCurContext) {
39 fCurContext->abandonContext();
40 }
robertphillips12e96622016-08-01 05:53:23 -070041
42 fGpuSurface = nullptr;
43 fCanvas = nullptr;
kkinnunen9e33d1d2015-02-10 22:27:48 -080044
45 fCurContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf.get()));
kkinnunen4be02382014-12-22 06:06:41 -080046}
47
48void SkGLWidget::createRenderTarget() {
49 if (!fCurContext) {
50 return;
51 }
52
53 glDisable(GL_SCISSOR_TEST);
commit-bot@chromium.orgfae599b2014-03-10 16:04:47 +000054 glStencilMask(0xffffffff);
55 glClearStencil(0);
56 glClear(GL_STENCIL_BUFFER_BIT);
kkinnunen4be02382014-12-22 06:06:41 -080057 fCurContext->resetContext();
commit-bot@chromium.orgfae599b2014-03-10 16:04:47 +000058
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000059 GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height());
robertphillips@google.com6577cd32013-02-08 21:22:09 +000060 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
robertphillips12e96622016-08-01 05:53:23 -070061
Florin Malitaf5305862016-11-15 10:03:32 -050062 fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), desc, nullptr);
robertphillips12e96622016-08-01 05:53:23 -070063 fCanvas = fGpuSurface->getCanvas();
chudy@google.comea5488b2012-07-26 19:38:22 +000064}
65
66void SkGLWidget::resizeGL(int w, int h) {
kkinnunen4be02382014-12-22 06:06:41 -080067 SkASSERT(w == this->width() && h == this->height());
68 this->createRenderTarget();
chudy@google.comea5488b2012-07-26 19:38:22 +000069}
70
71void SkGLWidget::paintGL() {
bungeman@google.com0b4d6b22013-07-01 13:54:10 +000072 if (!this->isHidden() && fCanvas) {
kkinnunen9e33d1d2015-02-10 22:27:48 -080073 fCurContext->resetContext();
robertphillips12e96622016-08-01 05:53:23 -070074 fDebugger->draw(fCanvas);
chudy@google.com607357f2012-08-07 16:12:23 +000075 // TODO(chudy): Implement an optional flush button in Gui.
76 fCanvas->flush();
robertphillips9ea8acd2016-03-01 09:34:38 -080077 Q_EMIT drawComplete();
chudy@google.com607357f2012-08-07 16:12:23 +000078 }
chudy@google.comea5488b2012-07-26 19:38:22 +000079}
80
bsalomon@google.com16e3dde2012-10-25 18:43:28 +000081GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) {
82 GrBackendRenderTargetDesc desc;
reed@google.come1ca7052013-12-17 19:22:07 +000083 desc.fWidth = SkScalarRoundToInt(this->width());
84 desc.fHeight = SkScalarRoundToInt(this->height());
bsalomon@google.comfec0bc32013-02-07 14:43:04 +000085 desc.fConfig = kSkia8888_GrPixelConfig;
Robert Phillipse0707272017-01-06 10:25:56 -050086 GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &desc.fSampleCnt);
87 GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &desc.fStencilBits);
chudy@google.comea5488b2012-07-26 19:38:22 +000088 GrGLint buffer;
Robert Phillipse0707272017-01-06 10:25:56 -050089 GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
chudy@google.comea5488b2012-07-26 19:38:22 +000090 desc.fRenderTargetHandle = buffer;
91
92 return desc;
93}
robertphillips@google.come8fe4bc2013-02-13 13:26:13 +000094
95#endif