• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SkGLWidget(SkDebugger * debugger)14 SkGLWidget::SkGLWidget(SkDebugger* debugger) : QGLWidget() {
15     fDebugger = debugger;
16 }
17 
~SkGLWidget()18 SkGLWidget::~SkGLWidget() {
19 }
20 
setSampleCount(int sampleCount)21 void SkGLWidget::setSampleCount(int sampleCount) {
22     QGLFormat currentFormat = format();
23     currentFormat.setSampleBuffers(sampleCount > 0);
24     currentFormat.setSamples(sampleCount);
25     setFormat(currentFormat);
26 }
27 
initializeGL()28 void SkGLWidget::initializeGL() {
29     if (!fCurIntf) {
30         fCurIntf.reset(GrGLCreateNativeInterface());
31     }
32     if (!fCurIntf) {
33         return;
34     }
35     // The call may come multiple times, for example after setSampleCount().  The QGLContext will be
36     // different, but we do not have a mechanism to catch the destroying of QGLContext, so that
37     // proper resource cleanup could be made.
38     if (fCurContext) {
39         fCurContext->abandonContext();
40     }
41 
42     fGpuSurface = nullptr;
43     fCanvas = nullptr;
44 
45     fCurContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) fCurIntf.get()));
46 }
47 
createRenderTarget()48 void SkGLWidget::createRenderTarget() {
49     if (!fCurContext) {
50         return;
51     }
52 
53     glDisable(GL_SCISSOR_TEST);
54     glStencilMask(0xffffffff);
55     glClearStencil(0);
56     glClear(GL_STENCIL_BUFFER_BIT);
57     fCurContext->resetContext();
58 
59     GrBackendRenderTargetDesc desc = this->getDesc(this->width(), this->height());
60     desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
61 
62     fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), desc, nullptr);
63     fCanvas = fGpuSurface->getCanvas();
64 }
65 
resizeGL(int w,int h)66 void SkGLWidget::resizeGL(int w, int h) {
67     SkASSERT(w == this->width() && h == this->height());
68     this->createRenderTarget();
69 }
70 
paintGL()71 void SkGLWidget::paintGL() {
72     if (!this->isHidden() && fCanvas) {
73         fCurContext->resetContext();
74         fDebugger->draw(fCanvas);
75         // TODO(chudy): Implement an optional flush button in Gui.
76         fCanvas->flush();
77         Q_EMIT drawComplete();
78     }
79 }
80 
getDesc(int w,int h)81 GrBackendRenderTargetDesc SkGLWidget::getDesc(int w, int h) {
82     GrBackendRenderTargetDesc desc;
83     desc.fWidth = SkScalarRoundToInt(this->width());
84     desc.fHeight = SkScalarRoundToInt(this->height());
85     desc.fConfig = kSkia8888_GrPixelConfig;
86     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &desc.fSampleCnt);
87     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &desc.fStencilBits);
88     GrGLint buffer;
89     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
90     desc.fRenderTargetHandle = buffer;
91 
92     return desc;
93 }
94 
95 #endif
96