blob: e3c4161cb42dc2a70dc94e130a052f9f21106e2f [file] [log] [blame]
chudy@google.comea5488b2012-07-26 19:38:22 +00001
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.com2d537a12012-07-31 12:49:52 +000012SkGLWidget::SkGLWidget() : QGLWidget() {
chudy@google.comea5488b2012-07-26 19:38:22 +000013 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.com2d537a12012-07-31 12:49:52 +000018 fCurIntf = NULL;
19 fCurContext = NULL;
20 fGpuDevice = NULL;
chudy@google.comea5488b2012-07-26 19:38:22 +000021}
22
23SkGLWidget::~SkGLWidget() {
24 SkSafeUnref(fCurIntf);
25 SkSafeUnref(fCurContext);
26 SkSafeUnref(fGpuDevice);
27}
28
29void 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
41void 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
48void 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.com0b5bbb02012-07-31 19:55:32 +000057 fDebugCanvas->drawTo(&canvas, fIndex);
chudy@google.comea5488b2012-07-26 19:38:22 +000058 canvas.flush();
59}
60
61GrPlatformRenderTargetDesc 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}