| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkGLWidget.h" | 10 #include "SkGLWidget.h" |
| 11 | 11 |
| 12 #if SK_SUPPORT_GPU | 12 #if SK_SUPPORT_GPU |
| 13 | 13 |
| 14 SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() { | 14 SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() { |
| 15 fDebugger = debugger; | 15 fDebugger = debugger; |
| 16 fCurIntf = NULL; | |
| 17 fCurContext = NULL; | |
| 18 fGpuDevice = NULL; | |
| 19 fCanvas = NULL; | |
| 20 } | 16 } |
| 21 | 17 |
| 22 SkGLWidget::~SkGLWidget() { | 18 SkGLWidget::~SkGLWidget() { |
| 23 SkSafeUnref(fCurIntf); | |
| 24 SkSafeUnref(fCurContext); | |
| 25 SkSafeUnref(fGpuDevice); | |
| 26 SkSafeUnref(fCanvas); | |
| 27 } | 19 } |
| 28 | 20 |
| 29 void SkGLWidget::setSampleCount(int sampleCount) | 21 void SkGLWidget::setSampleCount(int sampleCount) { |
| 30 { | |
| 31 QGLFormat currentFormat = format(); | 22 QGLFormat currentFormat = format(); |
| 32 currentFormat.setSampleBuffers(sampleCount > 0); | 23 currentFormat.setSampleBuffers(sampleCount > 0); |
| 33 currentFormat.setSamples(sampleCount); | 24 currentFormat.setSamples(sampleCount); |
| 34 setFormat(currentFormat); | 25 setFormat(currentFormat); |
| 35 } | 26 } |
| 36 | 27 |
| 37 void SkGLWidget::initializeGL() { | 28 void SkGLWidget::initializeGL() { |
| 38 fCurIntf = GrGLCreateNativeInterface(); | 29 if (!fCurIntf) { |
| 30 fCurIntf.reset(GrGLCreateNativeInterface()); |
| 31 } |
| 39 if (!fCurIntf) { | 32 if (!fCurIntf) { |
| 40 return; | 33 return; |
| 41 } | 34 } |
| 35 if (!fCurContext) { |
| 36 fCurContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext
) fCurIntf.get())); |
| 37 } |
| 38 if (!fCurContext) { |
| 39 return; |
| 40 } |
| 41 |
| 42 // The call may come multiple times, for example after setSampleCount(). Th
e QGLContext will be |
| 43 // different, but we do not have a mechanism to catch the destroying of QGLC
ontext, so that |
| 44 // proper resource cleanup could be made. Instead, we assume that the underl
ying GL context |
| 45 // never actually changes. If it would, we could not destroy the resources. |
| 46 fGpuDevice.reset(NULL); |
| 47 fCanvas.reset(NULL); |
| 48 } |
| 49 |
| 50 void SkGLWidget::createRenderTarget() { |
| 51 if (!fCurContext) { |
| 52 return; |
| 53 } |
| 54 |
| 55 glDisable(GL_SCISSOR_TEST); |
| 42 glStencilMask(0xffffffff); | 56 glStencilMask(0xffffffff); |
| 43 glClearStencil(0); | 57 glClearStencil(0); |
| 44 glClear(GL_STENCIL_BUFFER_BIT); | 58 glClear(GL_STENCIL_BUFFER_BIT); |
| 59 fCurContext->resetContext(); |
| 45 | 60 |
| 46 fCurContext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIn
tf); | 61 fGpuDevice.reset(NULL); |
| 62 fCanvas.reset(NULL); |
| 63 |
| 47 GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height()
); | 64 GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height()
); |
| 48 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; | 65 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 49 GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(desc)
; | 66 SkAutoTUnref<GrRenderTarget> curRenderTarget(fCurContext->wrapBackendRenderT
arget(desc)); |
| 50 fGpuDevice = SkGpuDevice::Create(curRenderTarget, | 67 fGpuDevice.reset(SkGpuDevice::Create(curRenderTarget.get(), |
| 51 SkSurfaceProps(SkSurfaceProps::kLegacyFontH
ost_InitType)); | 68 SkSurfaceProps(SkSurfaceProps::kLegacyF
ontHost_InitType))); |
| 52 fCanvas = new SkCanvas(fGpuDevice); | 69 fCanvas.reset(new SkCanvas(fGpuDevice)); |
| 53 curRenderTarget->unref(); | |
| 54 } | 70 } |
| 55 | 71 |
| 56 void SkGLWidget::resizeGL(int w, int h) { | 72 void SkGLWidget::resizeGL(int w, int h) { |
| 57 if (fCurContext) { | 73 SkASSERT(w == this->width() && h == this->height()); |
| 58 glDisable(GL_SCISSOR_TEST); | 74 this->createRenderTarget(); |
| 59 glStencilMask(0xffffffff); | |
| 60 glClearStencil(0); | |
| 61 glClear(GL_STENCIL_BUFFER_BIT); | |
| 62 fCurContext->resetContext(); | |
| 63 | |
| 64 GrBackendRenderTargetDesc desc = this->getDesc(w, h); | |
| 65 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; | |
| 66 GrRenderTarget* curRenderTarget = fCurContext->wrapBackendRenderTarget(d
esc); | |
| 67 SkSafeUnref(fGpuDevice); | |
| 68 SkSafeUnref(fCanvas); | |
| 69 fGpuDevice = SkGpuDevice::Create(curRenderTarget, | |
| 70 SkSurfaceProps(SkSurfaceProps::kLegacyF
ontHost_InitType)); | |
| 71 fCanvas = new SkCanvas(fGpuDevice); | |
| 72 } | |
| 73 fDebugger->setWindowSize(w, h); | 75 fDebugger->setWindowSize(w, h); |
| 74 draw(); | 76 draw(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void SkGLWidget::paintGL() { | 79 void SkGLWidget::paintGL() { |
| 78 if (!this->isHidden() && fCanvas) { | 80 if (!this->isHidden() && fCanvas) { |
| 79 fDebugger->draw(fCanvas); | 81 fDebugger->draw(fCanvas.get()); |
| 80 // TODO(chudy): Implement an optional flush button in Gui. | 82 // TODO(chudy): Implement an optional flush button in Gui. |
| 81 fCanvas->flush(); | 83 fCanvas->flush(); |
| 82 emit drawComplete(); | 84 emit drawComplete(); |
| 83 } | 85 } |
| 84 } | 86 } |
| 85 | 87 |
| 86 GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) { | 88 GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) { |
| 87 GrBackendRenderTargetDesc desc; | 89 GrBackendRenderTargetDesc desc; |
| 88 desc.fWidth = SkScalarRoundToInt(this->width()); | 90 desc.fWidth = SkScalarRoundToInt(this->width()); |
| 89 desc.fHeight = SkScalarRoundToInt(this->height()); | 91 desc.fHeight = SkScalarRoundToInt(this->height()); |
| 90 desc.fConfig = kSkia8888_GrPixelConfig; | 92 desc.fConfig = kSkia8888_GrPixelConfig; |
| 91 GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); | 93 GR_GL_GetIntegerv(fCurIntf, GR_GL_SAMPLES, &desc.fSampleCnt); |
| 92 GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); | 94 GR_GL_GetIntegerv(fCurIntf, GR_GL_STENCIL_BITS, &desc.fStencilBits); |
| 93 GrGLint buffer; | 95 GrGLint buffer; |
| 94 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); | 96 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer); |
| 95 desc.fRenderTargetHandle = buffer; | 97 desc.fRenderTargetHandle = buffer; |
| 96 | 98 |
| 97 return desc; | 99 return desc; |
| 98 } | 100 } |
| 99 | 101 |
| 100 #endif | 102 #endif |
| OLD | NEW |