Skip to content

Commit

Permalink
Zero copy preview made optional
Browse files Browse the repository at this point in the history
  • Loading branch information
live committed Dec 15, 2019
1 parent 89d8f7b commit 94cfa91
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ MainWindow::MainWindow(QWidget *parent)
&settings, &Settings::setIndentWithSpaces);
connect(&settings, &Settings::darkThemeChanging,
this, &MainWindow::handleDarkThemeChanging);
connect(mUi->actionZeroCopyPreview, &QAction::toggled,
[&](bool enabled) {
if (mEditorManager.closeAllImageEditors())
settings.setZeroCopyPreview(enabled);
});

connect(mUi->actionEvalManual, &QAction::triggered,
this, &MainWindow::updateEvaluationMode);
Expand Down Expand Up @@ -322,6 +327,7 @@ void MainWindow::readSettings()
mUi->actionSyntaxHighlighting->setChecked(settings.syntaxHighlighting());
mUi->actionDarkTheme->setChecked(settings.darkTheme());
mUi->actionLineWrapping->setChecked(settings.lineWrap());
mUi->actionZeroCopyPreview->setChecked(settings.zeroCopyPreview());
}

void MainWindow::closeEvent(QCloseEvent *event)
Expand Down
13 changes: 13 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
<addaction name="actionEvalManual"/>
<addaction name="actionEvalAuto"/>
<addaction name="actionEvalSteady"/>
<addaction name="separator"/>
<addaction name="actionZeroCopyPreview"/>
</widget>
<addaction name="menuEvaluation"/>
<addaction name="separator"/>
Expand Down Expand Up @@ -539,6 +541,17 @@
<string>&amp;Dark Theme</string>
</property>
</action>
<action name="actionZeroCopyPreview">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>&amp;Zero Copy Preview (experimental)</string>
</property>
<property name="toolTip">
<string>Zero Copy Preview</string>
</property>
</action>
</widget>
<resources>
<include location="resources.qrc"/>
Expand Down
16 changes: 16 additions & 0 deletions src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <QFontDialog>
#include <QFontDatabase>

bool gZeroCopyPreview = true;

Settings::Settings(QObject *parent) : QSettings(parent)
{
mFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
Expand All @@ -13,6 +15,7 @@ Settings::Settings(QObject *parent) : QSettings(parent)
setAutoIndentation(value("autoIndentation", "true").toBool());
setSyntaxHighlighting(value("syntaxHighlighting", "true").toBool());
setDarkTheme(value("darkTheme", "false").toBool());
setZeroCopyPreview(value("zeroCopyPreview", "false").toBool());

auto fontSettings = value("font").toString();
if (!fontSettings.isEmpty()) {
Expand All @@ -31,6 +34,7 @@ Settings::~Settings()
setValue("syntaxHighlighting", syntaxHighlighting());
setValue("darkTheme", darkTheme());
setValue("font", font().toString());
setValue("zeroCopyPreview", zeroCopyPreview());
endGroup();
}

Expand Down Expand Up @@ -89,3 +93,15 @@ void Settings::setDarkTheme(bool enabled)
emit darkThemeChanging(enabled);
emit darkThemeChanged(enabled);
}

void Settings::setZeroCopyPreview(bool enabled)
{
gZeroCopyPreview = enabled;
gZeroCopyPreview = enabled;
emit zeroCopyPreviewChanged(enabled);
}

bool Settings::zeroCopyPreview() const
{
return gZeroCopyPreview;
}
3 changes: 3 additions & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Settings : public QSettings
bool syntaxHighlighting() const { return mSyntaxHighlighting; }
void setDarkTheme(bool enabled);
bool darkTheme() const { return mDarkTheme; }
void setZeroCopyPreview(bool enabled);
bool zeroCopyPreview() const;

signals:
void tabSizeChanged(int tabSize);
Expand All @@ -36,6 +38,7 @@ class Settings : public QSettings
void syntaxHighlightingChanged(bool enabled);
void darkThemeChanging(bool enabled);
void darkThemeChanged(bool enabled);
void zeroCopyPreviewChanged(bool enabled);

private:
int mTabSize{ 4 };
Expand Down
9 changes: 9 additions & 0 deletions src/editors/EditorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ bool EditorManager::closeAllEditors()
return true;
}

bool EditorManager::closeAllImageEditors()
{
foreach (QDockWidget* dock, mDocks.keys())
if (qobject_cast<ImageEditor*>(dock->widget()))
if (!closeDock(dock))
return false;
return true;
}

void EditorManager::addSourceEditor(SourceEditor *editor)
{
mSourceEditors.append(editor);
Expand Down
1 change: 1 addition & 0 deletions src/editors/EditorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class EditorManager : public DockWindow
bool reloadEditor();
bool closeEditor();
bool closeAllEditors();
bool closeAllImageEditors();

signals:
void editorChanged(const QString &fileName);
Expand Down
54 changes: 37 additions & 17 deletions src/editors/ImageEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <QOpenGLShader>
#include <QOpenGLFunctions_3_3_Core>

class ImageItem : public QGraphicsItem
extern bool gZeroCopyPreview;

class ZeroCopyItem : public QGraphicsItem
{
static constexpr auto vertexShader = R"(
#version 330
Expand Down Expand Up @@ -53,9 +55,9 @@ void main() {
QOpenGLTexture *mTexture{ };
QMetaObject::Connection mTextureContextConnection;
QImage mUploadImage;
QRect mBoundingRect;
GLuint mPreviewTextureId{ };
bool mPreviewFlipY{ };
QRect mBoundingRect;
bool mMagnifyLinear{ };

public:
Expand Down Expand Up @@ -192,13 +194,17 @@ ImageEditor::ImageEditor(QString fileName, QWidget *parent)
mImage.fill(Qt::black);
setTransformationAnchor(AnchorUnderMouse);

setViewport(new QOpenGLWidget(this));
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
if (gZeroCopyPreview) {
setViewport(new QOpenGLWidget(this));
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

setScene(new QGraphicsScene(this));

mImageItem = new ImageItem();
scene()->addItem(mImageItem);
if (gZeroCopyPreview) {
mZeroCopyItem = new ZeroCopyItem();
scene()->addItem(mZeroCopyItem);
}

auto color = QColor(Qt::black);
color.setAlphaF(0.2);
Expand All @@ -225,13 +231,15 @@ ImageEditor::ImageEditor(QString fileName, QWidget *parent)

ImageEditor::~ImageEditor()
{
if (auto texture = mImageItem->resetTexture()) {
auto glWidget = qobject_cast<QOpenGLWidget*>(viewport());
auto context = glWidget->context();
auto surface = context->surface();
if (context->makeCurrent(surface)) {
delete texture;
context->doneCurrent();
if (mZeroCopyItem) {
if (auto texture = mZeroCopyItem->resetTexture()) {
auto glWidget = qobject_cast<QOpenGLWidget*>(viewport());
auto context = glWidget->context();
auto surface = context->surface();
if (context->makeCurrent(surface)) {
delete texture;
context->doneCurrent();
}
}
}
delete scene();
Expand Down Expand Up @@ -299,8 +307,18 @@ void ImageEditor::replace(QImage image, bool emitDataChanged)
return;

mImage = image;
mImageItem->setImage(mImage);
setBounds(mImageItem->boundingRect().toRect());
if (mZeroCopyItem) {
mZeroCopyItem->setImage(mImage);
setBounds(mZeroCopyItem->boundingRect().toRect());
}
else {
delete mPixmapItem;
auto pixmap = QPixmap::fromImage(mImage,
Qt::NoOpaqueDetection | Qt::NoFormatConversion);
mPixmapItem = new QGraphicsPixmapItem(pixmap);
scene()->addItem(mPixmapItem);
setBounds(pixmap.rect());
}

if (!FileDialog::isEmptyOrUntitled(mFileName))
setModified(true);
Expand All @@ -311,7 +329,8 @@ void ImageEditor::replace(QImage image, bool emitDataChanged)

void ImageEditor::updatePreviewTexture(unsigned int textureId, bool flipY)
{
mImageItem->setPreviewTexture(textureId, flipY);
if (mZeroCopyItem)
mZeroCopyItem->setPreviewTexture(textureId, flipY);
}

void ImageEditor::setModified(bool modified)
Expand Down Expand Up @@ -403,7 +422,8 @@ void ImageEditor::setZoom(int zoom)
1 << (mZoom));
updateTransform(scale);

mImageItem->setMagnifyLinear(scale <= 4);
if (mZeroCopyItem)
mZeroCopyItem->setMagnifyLinear(scale <= 4);
}

void ImageEditor::updateTransform(double scale)
Expand Down
7 changes: 4 additions & 3 deletions src/editors/ImageEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "IEditor.h"
#include <QGraphicsView>

class ImageItem;
class ZeroCopyItem;

class ImageEditor : public QGraphicsView, public IEditor
{
Expand All @@ -13,7 +13,7 @@ class ImageEditor : public QGraphicsView, public IEditor
static bool load(const QString &fileName, QImage *image);

explicit ImageEditor(QString fileName, QWidget *parent = nullptr);
~ImageEditor();
~ImageEditor() override;

QList<QMetaObject::Connection>
connectEditActions(const EditActions &actions) override;
Expand Down Expand Up @@ -55,7 +55,8 @@ class ImageEditor : public QGraphicsView, public IEditor
int mPanStartY{ };
QGraphicsPathItem *mInside{ };
QGraphicsPathItem *mBorder{ };
ImageItem *mImageItem{ };
ZeroCopyItem *mZeroCopyItem{ };
QGraphicsPixmapItem *mPixmapItem{ };
};

#endif // IMAGEEDITOR_H
6 changes: 4 additions & 2 deletions src/render/RenderSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <QOpenGLVertexArrayObject>
#include <QOpenGLTimerQuery>

extern bool gZeroCopyPreview;

namespace {
struct BindingScope
{
Expand Down Expand Up @@ -409,7 +411,7 @@ void RenderSession::executeCommandQueue()
void RenderSession::downloadModifiedResources()
{
for (auto &[itemId, texture] : mCommandQueue->textures)
if (mItemsChanged || !texture.canUpdatePreview())
if (!gZeroCopyPreview || mItemsChanged || !texture.canUpdatePreview())
mModifiedImages = mModifiedImages.unite(
texture.getModifiedImages());

Expand Down Expand Up @@ -453,7 +455,7 @@ void RenderSession::finish()

// keep updating preview texture
for (auto& [itemId, texture] : mCommandQueue->textures)
if (!mItemsChanged && texture.canUpdatePreview())
if (gZeroCopyPreview && !mItemsChanged && texture.canUpdatePreview())
if (auto fileItem = castItem<FileItem>(session.findItem(itemId)))
if (auto editor = editors.getImageEditor(fileItem->fileName))
editor->updatePreviewTexture(texture.getReadOnlyTextureId(),
Expand Down

0 comments on commit 94cfa91

Please sign in to comment.