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 | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 12 | SkGLWidget::SkGLWidget() : 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;}"); |
| 14 | fTransform.set(0,0); |
| 15 | fScaleFactor = 1.0; |
| 16 | fIndex = 0; |
| 17 | fDebugCanvas = NULL; |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 18 | fCurIntf = NULL; |
| 19 | fCurContext = NULL; |
| 20 | fGpuDevice = NULL; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 21 | fCanvas = NULL; |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | SkGLWidget::~SkGLWidget() { |
| 25 | SkSafeUnref(fCurIntf); |
| 26 | SkSafeUnref(fCurContext); |
| 27 | SkSafeUnref(fGpuDevice); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 28 | SkSafeUnref(fCanvas); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void SkGLWidget::initializeGL() { |
| 32 | fCurIntf = GrGLCreateNativeInterface(); |
| 33 | fCurContext = GrContext::Create(kOpenGL_Shaders_GrEngine, (GrPlatform3DContext) fCurIntf); |
| 34 | GrRenderTarget* curRenderTarget = fCurContext->createPlatformRenderTarget(getDesc(this->width(), this->height())); |
| 35 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 36 | fCanvas = new SkCanvas(fGpuDevice); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 37 | curRenderTarget->unref(); |
| 38 | |
| 39 | glClearColor(1, 1, 1, 0); |
| 40 | glClearStencil(0); |
| 41 | glClear(GL_STENCIL_BUFFER_BIT); |
| 42 | } |
| 43 | |
| 44 | void SkGLWidget::resizeGL(int w, int h) { |
| 45 | GrRenderTarget* curRenderTarget = fCurContext->createPlatformRenderTarget(getDesc(w,h)); |
| 46 | SkSafeUnref(fGpuDevice); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 47 | SkSafeUnref(fCanvas); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 48 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 49 | fCanvas = new SkCanvas(fGpuDevice); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 50 | drawTo(fIndex); |
| 51 | } |
| 52 | |
| 53 | void SkGLWidget::paintGL() { |
| 54 | glClearColor(1, 1, 1, 0); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame^] | 55 | fDebugCanvas->drawTo(fCanvas, fIndex); |
| 56 | // TODO(chudy): Implement an optional flush button in Gui. |
| 57 | fCanvas->flush(); |
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 | } |