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 12 #if SK_SUPPORT_GPU 13 14 SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() { 15 fDebugger = debugger; 16 fCurIntf = NULL; 17 fCurContext = NULL; 18 fGpuDevice = NULL; 19 fCanvas = NULL; 20 } 21 22 SkGLWidget::~SkGLWidget() { 23 SkSafeUnref(fCurIntf); 24 SkSafeUnref(fCurContext); 25 SkSafeUnref(fGpuDevice); 26 SkSafeUnref(fCanvas); 27 } 28 29 void SkGLWidget::setSampleCount(int sampleCount) 30 { 31 QGLFormat currentFormat = format(); 32 currentFormat.setSampleBuffers(sampleCount > 0); 33 currentFormat.setSamples(sampleCount); 34 setFormat(currentFormat); 35 } 36 37 void SkGLWidget::initializeGL() { 38 fCurIntf = GrGLCreateNativeInterface(); 39 if (!fCurIntf) { 40 return; 41 } 42 glStencilMask(0xffffffff); 43 glClearStencil(0); 44 glClear(GL_STENCIL_BUFFER_BIT); 45 46 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf); 47 GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height()); 48 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; 49 GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(desc); 50 fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); 51 fCanvas = new SkCanvas(fGpuDevice); 52 curRenderTarget->unref(); 53 } 54 55 void SkGLWidget::resizeGL(int w, int h) { 56 if (fCurContext) { 57 glDisable(GL_SCISSOR_TEST); 58 glStencilMask(0xffffffff); 59 glClearStencil(0); 60 glClear(GL_STENCIL_BUFFER_BIT); 61 fCurContext->resetContext(); 62 63 GrBackendRenderTargetDesc desc = this->getDesc(w, h); 64 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; 65 GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(desc); 66 SkSafeUnref(fGpuDevice); 67 SkSafeUnref(fCanvas); 68 fGpuDevice = new SkGpuDevice(fCurContext, curRenderTarget); 69 fCanvas = new SkCanvas(fGpuDevice); 70 } 71 fDebugger->resize(w, h); 72 draw(); 73 } 74 75 void SkGLWidget::paintGL() { 76 if (!this->isHidden() && fCanvas) { 77 fDebugger->draw(fCanvas); 78 // TODO(chudy): Implement an optional flush button in Gui. 79 fCanvas->flush(); 80 emit drawComplete(); 81 } 82 } 83 84 GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) { 85 GrBackendRenderTargetDesc desc; 86 desc.fWidth = SkScalarRoundToInt(this->width()); 87 desc.fHeight = SkScalarRoundToInt(this->height()); 88 desc.fConfig = kSkia8888_GrPixelConfig; 89 GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); 90 GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); 91 GrGLint buffer; 92 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); 93 desc.fRenderTargetHandle = buffer; 94 95 return desc; 96 } 97 98 #endif 99