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 | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 16 | fCurIntf = NULL; |
| 17 | fCurContext = NULL; |
| 18 | fGpuDevice = NULL; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 19 | fCanvas = NULL; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SkGLWidget::~SkGLWidget() { |
| 23 | SkSafeUnref(fCurIntf); |
| 24 | SkSafeUnref(fCurContext); |
| 25 | SkSafeUnref(fGpuDevice); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 26 | SkSafeUnref(fCanvas); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 27 | } |
| 28 | |
commit-bot@chromium.org | fde1e7c | 2013-08-02 13:59:50 +0000 | [diff] [blame] | 29 | void SkGLWidget::setSampleCount(int sampleCount) |
| 30 | { |
| 31 | QGLFormat currentFormat = format(); |
| 32 | currentFormat.setSampleBuffers(sampleCount > 0); |
| 33 | currentFormat.setSamples(sampleCount); |
| 34 | setFormat(currentFormat); |
| 35 | } |
| 36 | |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 37 | void SkGLWidget::initializeGL() { |
| 38 | fCurIntf = GrGLCreateNativeInterface(); |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 39 | if (!fCurIntf) { |
| 40 | return; |
| 41 | } |
commit-bot@chromium.org | fae599b | 2014-03-10 16:04:47 +0000 | [diff] [blame] | 42 | glStencilMask(0xffffffff); |
| 43 | glClearStencil(0); |
| 44 | glClear(GL_STENCIL_BUFFER_BIT); |
| 45 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 46 | fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf); |
| 47 | GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height()); |
robertphillips@google.com | 6577cd3 | 2013-02-08 21:22:09 +0000 | [diff] [blame] | 48 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 49 | GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(desc); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 50 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 51 | fCanvas = new SkCanvas(fGpuDevice); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 52 | curRenderTarget->unref(); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void SkGLWidget::resizeGL(int w, int h) { |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 56 | if (fCurContext) { |
commit-bot@chromium.org | fae599b | 2014-03-10 16:04:47 +0000 | [diff] [blame] | 57 | glDisable(GL_SCISSOR_TEST); |
| 58 | glStencilMask(0xffffffff); |
| 59 | glClearStencil(0); |
| 60 | glClear(GL_STENCIL_BUFFER_BIT); |
| 61 | fCurContext->resetContext(); |
| 62 | |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 63 | GrBackendRenderTargetDesc desc = this->getDesc(w, h); |
| 64 | desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 65 | GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(desc); |
| 66 | SkSafeUnref(fGpuDevice); |
| 67 | SkSafeUnref(fCanvas); |
| 68 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
| 69 | fCanvas = new SkCanvas(fGpuDevice); |
| 70 | } |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 71 | fDebugger->resize(w, h); |
| 72 | draw(); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void SkGLWidget::paintGL() { |
bungeman@google.com | 0b4d6b2 | 2013-07-01 13:54:10 +0000 | [diff] [blame] | 76 | if (!this->isHidden() && fCanvas) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 77 | fDebugger->draw(fCanvas); |
| 78 | // TODO(chudy): Implement an optional flush button in Gui. |
| 79 | fCanvas->flush(); |
| 80 | emit drawComplete(); |
| 81 | } |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon@google.com | 16e3dde | 2012-10-25 18:43:28 +0000 | [diff] [blame] | 84 | GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) { |
| 85 | GrBackendRenderTargetDesc desc; |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 86 | desc.fWidth = SkScalarRoundToInt(this->width()); |
| 87 | desc.fHeight = SkScalarRoundToInt(this->height()); |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 88 | desc.fConfig = kSkia8888_GrPixelConfig; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 89 | GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); |
| 90 | GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); |
| 91 | GrGLint buffer; |
| 92 | GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 93 | desc.fRenderTargetHandle = buffer; |
| 94 | |
| 95 | return desc; |
| 96 | } |
robertphillips@google.com | e8fe4bc | 2013-02-13 13:26:13 +0000 | [diff] [blame] | 97 | |
| 98 | #endif |