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 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 12 | SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() { |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 13 | this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}"); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 14 | fDebugger = debugger; |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 15 | fCurIntf = NULL; |
| 16 | fCurContext = NULL; |
| 17 | fGpuDevice = NULL; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 18 | fCanvas = NULL; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | SkGLWidget::~SkGLWidget() { |
| 22 | SkSafeUnref(fCurIntf); |
| 23 | SkSafeUnref(fCurContext); |
| 24 | SkSafeUnref(fGpuDevice); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 25 | SkSafeUnref(fCanvas); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void SkGLWidget::initializeGL() { |
| 29 | fCurIntf = GrGLCreateNativeInterface(); |
| 30 | fCurContext = GrContext::Create(kOpenGL_Shaders_GrEngine, (GrPlatform3DContext) fCurIntf); |
| 31 | GrRenderTarget* curRenderTarget = fCurContext->createPlatformRenderTarget(getDesc(this->width(), this->height())); |
| 32 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 33 | fCanvas = new SkCanvas(fGpuDevice); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 34 | curRenderTarget->unref(); |
| 35 | |
| 36 | glClearColor(1, 1, 1, 0); |
| 37 | glClearStencil(0); |
| 38 | glClear(GL_STENCIL_BUFFER_BIT); |
| 39 | } |
| 40 | |
| 41 | void SkGLWidget::resizeGL(int w, int h) { |
| 42 | GrRenderTarget* curRenderTarget = fCurContext->createPlatformRenderTarget(getDesc(w,h)); |
| 43 | SkSafeUnref(fGpuDevice); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 44 | SkSafeUnref(fCanvas); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 45 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 46 | fCanvas = new SkCanvas(fGpuDevice); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 47 | fDebugger->resize(w, h); |
| 48 | draw(); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void SkGLWidget::paintGL() { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 52 | if (!this->isHidden()) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 53 | fDebugger->draw(fCanvas); |
| 54 | // TODO(chudy): Implement an optional flush button in Gui. |
| 55 | fCanvas->flush(); |
| 56 | emit drawComplete(); |
| 57 | } |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | GrPlatformRenderTargetDesc SkGLWidget::getDesc(int w, int h) { |
| 61 | GrPlatformRenderTargetDesc desc; |
| 62 | desc.fWidth = SkScalarRound(this->width()); |
| 63 | desc.fHeight = SkScalarRound(this->height()); |
| 64 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 65 | GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); |
| 66 | GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); |
| 67 | GrGLint buffer; |
| 68 | GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 69 | desc.fRenderTargetHandle = buffer; |
| 70 | |
| 71 | return desc; |
| 72 | } |