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