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 | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | SkGLWidget::~SkGLWidget() { |
| 24 | SkSafeUnref(fCurIntf); |
| 25 | SkSafeUnref(fCurContext); |
| 26 | SkSafeUnref(fGpuDevice); |
| 27 | } |
| 28 | |
| 29 | void SkGLWidget::initializeGL() { |
| 30 | fCurIntf = GrGLCreateNativeInterface(); |
| 31 | fCurContext = GrContext::Create(kOpenGL_Shaders_GrEngine, (GrPlatform3DContext) fCurIntf); |
| 32 | GrRenderTarget* curRenderTarget = fCurContext->createPlatformRenderTarget(getDesc(this->width(), this->height())); |
| 33 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
| 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); |
| 44 | fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); |
| 45 | drawTo(fIndex); |
| 46 | } |
| 47 | |
| 48 | void SkGLWidget::paintGL() { |
| 49 | glClearColor(1, 1, 1, 0); |
| 50 | SkCanvas canvas(fGpuDevice); |
| 51 | canvas.translate(fTransform.fX, fTransform.fY); |
| 52 | if(fScaleFactor < 0) { |
| 53 | canvas.scale((1.0 / -fScaleFactor),(1.0 / -fScaleFactor)); |
| 54 | } else if (fScaleFactor > 0) { |
| 55 | canvas.scale(fScaleFactor, fScaleFactor); |
| 56 | } |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 57 | fDebugCanvas->drawTo(&canvas, fIndex); |
chudy@google.com | ea5488b | 2012-07-26 19:38:22 +0000 | [diff] [blame] | 58 | canvas.flush(); |
| 59 | } |
| 60 | |
| 61 | GrPlatformRenderTargetDesc SkGLWidget::getDesc(int w, int h) { |
| 62 | GrPlatformRenderTargetDesc desc; |
| 63 | desc.fWidth = SkScalarRound(this->width()); |
| 64 | desc.fHeight = SkScalarRound(this->height()); |
| 65 | desc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 66 | GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); |
| 67 | GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); |
| 68 | GrGLint buffer; |
| 69 | GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 70 | desc.fRenderTargetHandle = buffer; |
| 71 | |
| 72 | return desc; |
| 73 | } |