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