Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'qtquick2' of scm.dev.nokia.troll.no:qt/qtdeclarative-st…
…aging into qtquick2
  • Loading branch information
Alan Alpert committed May 11, 2011
2 parents faed3a7 + 4ff7297 commit ad5aeed
Show file tree
Hide file tree
Showing 26 changed files with 548 additions and 169 deletions.
15 changes: 14 additions & 1 deletion doc/src/declarative/basictypes.qdoc
Expand Up @@ -439,7 +439,20 @@
}
\endqml

The \c variant type can also hold:
A \c variant type property can also hold an image or pixmap.
A \c variant which contains a QPixmap or QImage is known as a
"scarce resource" and the declarative engine will attempt to
automatically release such resources after evaluation of any JavaScript
expression which requires one to be copied has completed.

Clients may explicitly release such a scarce resource by calling the
"destroy" method on the \c variant property from within JavaScript. They
may also explicitly preserve the scarce resource by calling the
"preserve" method on the \c variant property from within JavaScript.
For more information regarding the usage of a scarce resource, please
see \l{Scarce Resources in JavaScript}.

Finally, the \c variant type can also hold:

\list
\o An array of \l {QML Basic Types}{basic type} values
Expand Down
229 changes: 229 additions & 0 deletions doc/src/declarative/javascriptblocks.qdoc
Expand Up @@ -168,6 +168,44 @@ Notice that calling \l {QML:Qt::include()}{Qt.include()} imports all functions f
\c factorial.js into the \c MyScript namespace, which means the QML component can also
access \c factorial() directly as \c MyScript.factorial().

In QtQuick 2.0, support has been added to allow JavaScript files to import other
JavaScript files and also QML modules using a variation of the standard QML import
syntax (where all of the previously described rules and qualifications apply).

A JavaScript file may import another in the following fashion:
\code
.import "filename.js" as UniqueQualifier
\endcode
For example:
\code
.import "factorial.js" as MathFunctions
\endcode

A JavaScript file may import a QML module in the following fashion:
\code
.import Module.Name MajorVersion.MinorVersion as UniqueQualifier
\endcode
For example:
\code
.import Qt.test 1.0 as JsQtTest
\endcode
In particular, this may be useful in order to access functionality provided
via a module API; see qmlRegisterModuleApi() for more information.

Due to the ability of a JavaScript file to import another script or QML module in
this fashion in QtQuick 2.0, some extra semantics are defined:
\list
\o a script with imports will not inherit imports from the QML file which imported it (so accessing Component.error will fail, for example)
\o a script without imports will inherit imports from the QML file which imported it (so accessing Component.error will succeed, for example)
\o a shared script (i.e., defined as .pragma library) does not inherit imports from any QML file even if it imports no other scripts
\endlist

The first semantic is conceptually correct, given that a particular script
might be imported by any number of QML files. The second semantic is retained
for the purposes of backwards-compatibility. The third semantic remains
unchanged from the current semantics for shared scripts, but is clarified here
in respect to the newly possible case (where the script imports other scripts
or modules).

\section1 Running JavaScript at Startup

Expand Down Expand Up @@ -321,4 +359,195 @@ Item {

\endlist

\section1 Scarce Resources in JavaScript

As described in the documentation for \l{QML Basic Types}, a \c variant type
property may hold a "scarce resource" (image or pixmap). There are several
important semantics of scarce resources which should be noted:

\list
\o By default, a scarce resource is automatically released by the declarative engine as soon as evaluation of the expression in which the scarce resource is allocated is complete if there are no other references to the resource
\o A client may explicitly preserve a scarce resource, which will ensure that the resource will not be released until all references to the resource are released and the JavaScript engine runs its garbage collector
\o A client may explicitly destroy a scarce resource, which will immediately release the resource
\endlist

In most cases, allowing the engine to automatically release the resource is
the correct choice. In some cases, however, this may result in an invalid
variant being returned from a function in JavaScript, and in those cases it
may be necessary for clients to manually preserve or destroy resources for
themselves.

For the following examples, imagine that we have defined the following class:
\code
class AvatarExample : public QObject
{
Q_OBJECT
Q_PROPERTY(QPixmap avatar READ avatar WRITE setAvatar NOTIFY avatarChanged)
public:
AvatarExample(QObject *parent = 0) : QObject(parent), m_value(100, 100) { m_value.fill(Qt::blue); }
~AvatarExample() {}

QPixmap avatar() const { return m_value; }
void setAvatar(QPixmap v) { m_value = v; emit avatarChanged(); }

signals:
void avatarChanged();

private:
QPixmap m_value;
};
\endcode

and that we have registered it with the QML type-system as follows:
\code
qmlRegisterType<AvatarExample>("Qt.example", 1, 0, "AvatarExample");
\endcode

The AvatarExample class has a property which is a pixmap. When the property
is accessed in JavaScript scope, a copy of the resource will be created and
stored in a JavaScript object which can then be used within JavaScript. This
copy will take up valuable system resources, and so by default the scarce
resource copy in the JavaScript object will be released automatically by the
declarative engine once evaluation of the JavaScript expression is complete,
unless the client explicitly preserves it.

\section2 Example One: Automatic Release

In this example, the resource will be automatically
released after the binding expression evaluation is
complete.

\qml
// exampleOne.qml
import QtQuick 1.0
import Qt.example 1.0

QtObject {
property AvatarExample a;
a: AvatarExample { id: example }
property variant avatar: example.avatar
}
\endqml

\code
QDeclarativeComponent component(&engine, "exampleOne.qml");
QObject *object = component.create();
// The scarce resource will have been released automatically
// after the binding expression was evaluated.
// Since the scarce resource was not released explicitly prior
// to the binding expression being evaluated, we get the
// expected result:
//object->property("scarceResourceCopy").isValid() == true
delete object;
\endcode

\section2 Example Two: Explicit Preservation

In this example, the resource must be explicitly preserved in order
to prevent the declarative engine from automatically releasing the
resource after evaluation of the imported script.

\code
// exampleTwo.js
.import Qt.example 1.0 as QtExample

var component = Qt.createComponent("exampleOne.qml");
var exampleOneElement = component.createObject(null);
var avatarExample = exampleOneElement.a;
var retn = avatarExample.avatar;

// without the following call, the scarce resource held
// by retn would be automatically released by the engine
// after the import statement in exampleTwo.qml, prior
// to the variable assignment.
retn.preserve();

function importAvatar() {
return retn;
}
\endcode

\qml
// exampleTwo.qml
import QtQuick 1.0
import Qt.example 1.0
import "exampleTwo.js" as ExampleTwoJs

QtObject {
property variant avatar: ExampleTwoJs.importAvatar()
}
\endqml

\code
QDeclarativeComponent component(&engine, "exampleTwo.qml");
QObject *object = component.create();
// The resource was preserved explicitly during evaluation of the
// JavaScript expression. Thus, during property assignment, the
// scarce resource was still valid, and so we get the expected result:
//object->property("avatar").isValid() == true
// The scarce resource may not have been cleaned up by the JS GC yet;
// it will continue to consume system resources until the JS GC runs.
delete object;
\endcode

\section2 Example Three: Explicit Destruction

In the following example, we release (via destroy()) an explicitly preserved
scarce resource variant. This example shows how a client may free system
resources by releasing the scarce resource held in a JavaScript object, if
required, during evaluation of a JavaScript expression.

\code
// exampleThree.js
.import Qt.example 1.0 as QtExample

var component = Qt.createComponent("exampleOne.qml");
var exampleOneElement = component.createObject(null);
var avatarExample = exampleOneElement.a;
var retn = avatarExample.avatar;
retn.preserve();

function importAvatar() {
return retn;
}

function releaseAvatar() {
retn.destroy();
}
\endcode

\qml
// exampleThree.qml
import QtQuick 1.0
import Qt.example 1.0
import "exampleThree.js" as ExampleThreeJs

QtObject {
property variant avatarOne
property variant avatarTwo

Component.onCompleted: {
avatarOne = ExampleThreeJs.importAvatar(); // valid at this stage
ExampleThreeJs.releaseAvatar(); // explicit release
avatarTwo = ExampleThreeJs.importAvatar(); // invalid at this stage
}
}
\endqml

\code
QDeclarativeComponent component(&engine, "exampleThree.qml");
QObject *object = component.create();
// The scarce resource was explicitly preserved by the client during
// the evaluation of the imported script, and so the scarce resource
// remains valid until the explicit call to releaseAvatar(). As such,
// we get the expected results:
//object->property("avatarOne").isValid() == true
//object->property("avatarTwo").isValid() == false
// Because the scarce resource was released explicitly, it will no longer
// be consuming any system resources (beyond what a normal JS Object would;
// that small overhead will exist until the JS GC runs, as per any other
// JavaScript object).
delete object;
\endcode

*/
43 changes: 42 additions & 1 deletion src/declarative/items/qsgpainteditem.cpp
Expand Up @@ -103,6 +103,8 @@ QSGPaintedItemPrivate::QSGPaintedItemPrivate()
, geometryDirty(false)
, contentsDirty(false)
, opaquePainting(false)
, antialiasing(false)
, mipmap(false)
{
}

Expand Down Expand Up @@ -225,6 +227,40 @@ void QSGPaintedItem::setAntialiasing(bool enable)
update();
}

/*!
Returns true if mipmaps are enabled; otherwise, false is returned.
By default, mipmapping is not enabled.
\sa setMipmap()
*/
bool QSGPaintedItem::mipmap() const
{
Q_D(const QSGPaintedItem);
return d->mipmap;
}

/*!
If \a enable is true, mipmapping is enabled on the associated texture.
Mipmapping increases rendering speed and reduces aliasing artifacts when the item is
scaled down.
By default, mipmapping is not enabled.
\sa mipmap()
*/
void QSGPaintedItem::setMipmap(bool enable)
{
Q_D(QSGPaintedItem);

if (d->mipmap == enable)
return;

d->mipmap = enable;
update();
}

/*!
This function returns the outer bounds of the item as a rectangle; all painting must be
restricted to inside an item's bounding rect.
Expand Down Expand Up @@ -379,6 +415,11 @@ void QSGPaintedItem::setRenderTarget(RenderTarget target)
Reimplement this function in a QSGPaintedItem subclass to provide the
item's painting implementation, using \a painter.
\note The QML Scene Graph uses two separate threads, the main thread does things such as
processing events or updating animations while a second thread does the actual OpenGL rendering.
As a consequence, paint() is not called from the main GUI thread but from the GL enabled
renderer thread.
*/

/*!
Expand Down Expand Up @@ -416,7 +457,7 @@ QSGNode *QSGPaintedItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *
node->setSize(QSize(qRound(br.width()), qRound(br.height())));
node->setSmoothPainting(d->antialiasing);
node->setLinearFiltering(d->smooth);
node->setMipmapping(d->smooth);
node->setMipmapping(d->mipmap);
node->setOpaquePainting(d->opaquePainting);
node->setFillColor(d->fillColor);
node->setContentsScale(d->contentsScale);
Expand Down
3 changes: 3 additions & 0 deletions src/declarative/items/qsgpainteditem.h
Expand Up @@ -75,6 +75,9 @@ class Q_DECLARATIVE_EXPORT QSGPaintedItem : public QSGItem
bool antialiasing() const;
void setAntialiasing(bool enable);

bool mipmap() const;
void setMipmap(bool enable);

QRectF contentsBoundingRect() const;

QSize contentsSize() const;
Expand Down
1 change: 1 addition & 0 deletions src/declarative/items/qsgpainteditem_p.h
Expand Up @@ -63,6 +63,7 @@ class QSGPaintedItemPrivate : public QSGItemPrivate
bool contentsDirty : 1;
bool opaquePainting: 1;
bool antialiasing: 1;
bool mipmap: 1;
};

QT_END_NAMESPACE
Expand Down

0 comments on commit ad5aeed

Please sign in to comment.