1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qsgd3d12texture_p.h"
41 #include "qsgd3d12engine_p.h"
42 #include <private/qsgcontext_p.h>
43 
44 QT_BEGIN_NAMESPACE
45 
46 #define RETAIN_IMAGE
47 
create(const QImage & image,uint flags)48 void QSGD3D12Texture::create(const QImage &image, uint flags)
49 {
50     // ### atlas?
51 
52     const bool alphaRequest = flags & QSGRenderContext::CreateTexture_Alpha;
53     m_alphaWanted = alphaRequest && image.hasAlphaChannel();
54 
55     // The engine maps 8-bit formats to R8. This is fine for glyphs and such
56     // but may not be what apps expect for ordinary image data. The OpenGL
57     // implementation maps these to ARGB32_Pre so let's follow suit.
58     if (image.depth() != 8)
59         m_image = image;
60     else
61         m_image = image.convertToFormat(m_alphaWanted ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32);
62 
63     m_id = m_engine->genTexture();
64     Q_ASSERT(m_id);
65 
66     // We could kick off the texture creation and the async upload right here.
67     // Unfortunately we cannot tell at this stage if mipmaps will be enabled
68     // via an Image element's mipmap property...so defer to bind().
69     m_createPending = true;
70 }
71 
QSGD3D12Texture(QSGD3D12Engine * engine)72 QSGD3D12Texture::QSGD3D12Texture(QSGD3D12Engine *engine)
73     : QSGTexture(*(new QSGD3D12TexturePrivate)),
74       m_engine(engine)
75 {
76 }
77 
~QSGD3D12Texture()78 QSGD3D12Texture::~QSGD3D12Texture()
79 {
80     if (m_id)
81         m_engine->releaseTexture(m_id);
82 }
83 
textureId() const84 int QSGD3D12Texture::textureId() const
85 {
86     return m_id;
87 }
88 
comparisonKey() const89 int QSGD3D12TexturePrivate::comparisonKey() const
90 {
91     Q_Q(const QSGD3D12Texture);
92     return q->m_id;
93 }
94 
textureSize() const95 QSize QSGD3D12Texture::textureSize() const
96 {
97     return m_image.size();
98 }
99 
hasAlphaChannel() const100 bool QSGD3D12Texture::hasAlphaChannel() const
101 {
102     return m_alphaWanted;
103 }
104 
hasMipmaps() const105 bool QSGD3D12Texture::hasMipmaps() const
106 {
107     return mipmapFiltering() != QSGTexture::None;
108 }
109 
normalizedTextureSubRect() const110 QRectF QSGD3D12Texture::normalizedTextureSubRect() const
111 {
112     return QRectF(0, 0, 1, 1);
113 }
114 
bind()115 void QSGD3D12Texture::bind()
116 {
117     // Called when the texture material updates the pipeline state.
118 
119     if (!m_createPending && hasMipmaps() != m_createdWithMipMaps) {
120 #ifdef RETAIN_IMAGE
121         m_engine->releaseTexture(m_id);
122         m_id = m_engine->genTexture();
123         Q_ASSERT(m_id);
124         m_createPending = true;
125 #else
126         // ### this can be made working some day (something similar to
127         // queueTextureResize) but skip for now
128         qWarning("D3D12: mipmap property cannot be changed once the texture is created");
129 #endif
130     }
131 
132     if (m_createPending) {
133         m_createPending = false;
134 
135         QSGD3D12Engine::TextureCreateFlags createFlags = 0;
136         if (m_alphaWanted)
137             createFlags |= QSGD3D12Engine::TextureWithAlpha;
138 
139         m_createdWithMipMaps = hasMipmaps();
140         if (m_createdWithMipMaps)
141             createFlags |= QSGD3D12Engine::TextureWithMipMaps;
142 
143         m_engine->createTexture(m_id, m_image.size(), m_image.format(), createFlags);
144         m_engine->queueTextureUpload(m_id, m_image);
145 
146 #ifndef RETAIN_IMAGE
147         m_image = QImage();
148 #endif
149     }
150 
151     // Here we know that the texture is going to be used in the current frame
152     // by the next draw call. Notify the engine so that it can wait for
153     // possible pending uploads and set up the pipeline accordingly.
154     m_engine->useTexture(m_id);
155 }
156 
157 QT_END_NAMESPACE
158