Home | History | Annotate | Download | only in QT
      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 }
     17 
     18 SkGLWidget::~SkGLWidget() {
     19 }
     20 
     21 void SkGLWidget::setSampleCount(int sampleCount) {
     22     QGLFormat currentFormat = format();
     23     currentFormat.setSampleBuffers(sampleCount > 1);
     24     currentFormat.setSamples(sampleCount);
     25     setFormat(currentFormat);
     26 }
     27 
     28 void SkGLWidget::initializeGL() {
     29     if (!fCurIntf) {
     30         fCurIntf = GrGLMakeNativeInterface();
     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 = GrContext::MakeGL(fCurIntf.get());
     46 }
     47 
     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     GrBackendRenderTarget backendRenderTarget = this->getBackendRenderTarget();
     59     SkColorType colorType;
     60     if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) {
     61         colorType = kRGBA_8888_SkColorType;
     62     } else {
     63         colorType = kBGRA_8888_SkColorType;
     64     }
     65     fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), backendRenderTarget,
     66                                                          kBottomLeft_GrSurfaceOrigin, colorType,
     67                                                          nullptr, nullptr);
     68     fCanvas = fGpuSurface->getCanvas();
     69 }
     70 
     71 void SkGLWidget::resizeGL(int w, int h) {
     72     SkASSERT(w == this->width() && h == this->height());
     73     this->createRenderTarget();
     74 }
     75 
     76 void SkGLWidget::paintGL() {
     77     if (!this->isHidden() && fCanvas) {
     78         fCurContext->resetContext();
     79         fDebugger->draw(fCanvas);
     80         // TODO(chudy): Implement an optional flush button in Gui.
     81         fCanvas->flush();
     82         Q_EMIT drawComplete();
     83     }
     84 }
     85 
     86 GrBackendRenderTarget SkGLWidget::getBackendRenderTarget() {
     87     GrGLFramebufferInfo info;
     88     int stencilBits;
     89     int sampleCnt;
     90     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &info.fFBOID);
     91     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &sampleCnt);
     92     sampleCnt = SkTMax(sampleCnt, 1);
     93     GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &stencilBits);
     94     // We are on desktop so we assume the internal config is RGBA
     95     info.fFormat = GR_GL_RGBA8;
     96     return GrBackendRenderTarget(SkScalarRoundToInt(this->width()),
     97                                  SkScalarRoundToInt(this->height()),
     98                                  sampleCnt,
     99                                  stencilBits,
    100                                  info);
    101 }
    102 
    103 #endif
    104