chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 1 | |
| 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.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
| 13 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 14 | SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 15 | fDebugger = debugger; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | SkGLWidget::~SkGLWidget() { |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 19 | } |
| 20 | |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 21 | void SkGLWidget::setSampleCount(int sampleCount) { |
commit-bot@chromium.org | fde1e7c | 2013-08-02 13:59:50 +0000 | [diff] [blame] | 22 | QGLFormat currentFormat = format(); |
| 23 | currentFormat.setSampleBuffers(sampleCount > 0); |
| 24 | currentFormat.setSamples(sampleCount); |
| 25 | setFormat(currentFormat); |
| 26 | } |
| 27 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 28 | void SkGLWidget::initializeGL() { |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 29 | if (!fCurIntf) { |
| 30 | fCurIntf.reset(GrGLCreateNativeInterface()); |
| 31 | } |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 32 | if (!fCurIntf) { |
| 33 | return; |
| 34 | } |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 35 | // 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 |
kkinnunen | 9e33d1d | 2015-02-10 22:27:48 -0800 | [diff] [blame] | 37 | // proper resource cleanup could be made. |
| 38 | if (fCurContext) { |
| 39 | fCurContext->abandonContext(); |
| 40 | } |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 41 | |
| 42 | fGpuSurface = nullptr; |
| 43 | fCanvas = nullptr; |
kkinnunen | 9e33d1d | 2015-02-10 22:27:48 -0800 | [diff] [blame] | 44 | |
| 45 | fCurContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf.get())); |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void SkGLWidget::createRenderTarget() { |
| 49 | if (!fCurContext) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | glDisable(GL_SCISSOR_TEST); |
commit-bot@chromium.org | fae599b | 2014-03-10 16:04:47 +0000 | [diff] [blame] | 54 | glStencilMask(0xffffffff); |
| 55 | glClearStencil(0); |
| 56 | glClear(GL_STENCIL_BUFFER_BIT); |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 57 | fCurContext->resetContext(); |
commit-bot@chromium.org | fae599b | 2014-03-10 16:04:47 +0000 | [diff] [blame] | 58 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 59 | GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height()); |
robertphillips@google.com | 6577cd3 | 2013-02-08 21:22:09 +0000 | [diff] [blame] | 60 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 61 | |
Florin Malita | f530586 | 2016-11-15 10:03:32 -0500 | [diff] [blame] | 62 | fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), desc, nullptr); |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 63 | fCanvas = fGpuSurface->getCanvas(); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void SkGLWidget::resizeGL(int w, int h) { |
kkinnunen | 4be0238 | 2014-12-22 06:06:41 -0800 | [diff] [blame] | 67 | SkASSERT(w == this->width() && h == this->height()); |
| 68 | this->createRenderTarget(); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void SkGLWidget::paintGL() { |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 72 | if (!this->isHidden() && fCanvas) { |
kkinnunen | 9e33d1d | 2015-02-10 22:27:48 -0800 | [diff] [blame] | 73 | fCurContext->resetContext(); |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 74 | fDebugger->draw(fCanvas); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 75 | // TODO(chudy): Implement an optional flush button in Gui. |
| 76 | fCanvas->flush(); |
robertphillips | 9ea8acd | 2016-03-01 09:34:38 -0800 | [diff] [blame] | 77 | Q_EMIT drawComplete(); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 78 | } |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 79 | } |
| 80 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 81 | GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) { |
| 82 | GrBackendRenderTargetDesc desc; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 83 | desc.fWidth = SkScalarRoundToInt(this->width()); |
| 84 | desc.fHeight = SkScalarRoundToInt(this->height()); |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 85 | desc.fConfig = kSkia8888_GrPixelConfig; |
Robert Phillips | e070727 | 2017-01-06 10:25:56 -0500 | [diff] [blame] | 86 | GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &desc.fSampleCnt); |
| 87 | GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &desc.fStencilBits); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 88 | GrGLint buffer; |
Robert Phillips | e070727 | 2017-01-06 10:25:56 -0500 | [diff] [blame] | 89 | GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 90 | desc.fRenderTargetHandle = buffer; |
| 91 | |
| 92 | return desc; |
| 93 | } |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 94 | |
| 95 | #endif |