1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include "GLcommon/TextureData.h"
18 
19 #include "aemu/base/containers/Lookup.h"
20 #include "aemu/base/files/StreamSerializing.h"
21 
22 #include "GLcommon/GLEScontext.h"
23 #include "GLcommon/GLutils.h"
24 
25 #include <cassert>
26 
27 using android::base::find;
28 
TextureData(android::base::Stream * stream)29 TextureData::TextureData(android::base::Stream* stream) : ObjectData(stream) {
30     // The current TextureData structure is wrong when dealing with mipmaps.
31     target = stream->getBe32();
32     width = stream->getBe32();
33     height = stream->getBe32();
34     depth = stream->getBe32();
35     border = stream->getBe32();
36     internalFormat = stream->getBe32();
37     format = stream->getBe32();
38     type = stream->getBe32();
39     sourceEGLImage = stream->getBe32();
40     hasStorage = stream->getByte();
41     wasBound = stream->getByte();
42     requiresAutoMipmap = stream->getByte();
43     compressed = stream->getByte();
44     compressedFormat = stream->getBe32();
45     stream->read(crop_rect, sizeof(crop_rect));
46     texStorageLevels = stream->getBe32();
47     stream->getBe32(); // deprecated mipmap level
48     globalName = stream->getBe32();
49     loadCollection(stream, &m_texParam, [](android::base::Stream* stream) {
50         GLenum item = stream->getBe32();
51         GLint val = stream->getBe32();
52         return std::make_pair(item, val);
53     });
54 }
55 
onSave(android::base::Stream * stream,unsigned int globalName) const56 void TextureData::onSave(android::base::Stream* stream, unsigned int globalName) const {
57     ObjectData::onSave(stream, globalName);
58     // The current TextureData structure is wrong when dealing with mipmaps.
59     stream->putBe32(target);
60     stream->putBe32(width);
61     stream->putBe32(height);
62     stream->putBe32(depth);
63     stream->putBe32(border);
64     stream->putBe32(internalFormat);
65     stream->putBe32(format);
66     stream->putBe32(type);
67     stream->putBe32(sourceEGLImage);
68     stream->putByte(hasStorage);
69     stream->putByte(wasBound);
70     stream->putByte(requiresAutoMipmap);
71     stream->putByte(compressed);
72     stream->putBe32(compressedFormat);
73     stream->write(crop_rect, sizeof(crop_rect));
74     stream->putBe32(texStorageLevels);
75     stream->putBe32(0); // deprecated mipmap level
76     stream->putBe32(globalName);
77     saveCollection(stream, m_texParam,
78                    [](android::base::Stream* stream,
79                       const std::pair<const GLenum, GLint>& texParam) {
80                        stream->putBe32(texParam.first);
81                        stream->putBe32(texParam.second);
82                    });
83 }
84 
restore(ObjectLocalName localName,const getGlobalName_t & getGlobalName)85 void TextureData::restore(ObjectLocalName localName,
86             const getGlobalName_t& getGlobalName) {
87     ObjectData::restore(localName, getGlobalName);
88 }
89 
setSaveableTexture(SaveableTexturePtr && saveableTexture)90 void TextureData::setSaveableTexture(SaveableTexturePtr&& saveableTexture) {
91     m_saveableTexture = std::move(saveableTexture);
92 }
93 
getSaveableTexture() const94 const SaveableTexturePtr& TextureData::getSaveableTexture() const {
95     return m_saveableTexture;
96 }
97 
resetSaveableTexture()98 void TextureData::resetSaveableTexture() {
99     m_saveableTexture.reset(new SaveableTexture(*this));
100 }
101 
getGlobalName() const102 unsigned int TextureData::getGlobalName() const {
103     return globalName;
104 }
105 
setGlobalName(unsigned int name)106 void TextureData::setGlobalName(unsigned int name) {
107     globalName = name;
108 }
109 
setTexParam(GLenum pname,GLint param)110 void TextureData::setTexParam(GLenum pname, GLint param) {
111     m_texParam[pname] = param;
112 }
113 
getSwizzle(GLenum component) const114 GLenum TextureData::getSwizzle(GLenum component) const {
115     if (const auto res = find(m_texParam, component)) {
116         return *res;
117     };
118     switch (component) {
119         case GL_TEXTURE_SWIZZLE_R:
120             return GL_RED;
121         case GL_TEXTURE_SWIZZLE_G:
122             return GL_GREEN;
123         case GL_TEXTURE_SWIZZLE_B:
124             return GL_BLUE;
125         case GL_TEXTURE_SWIZZLE_A:
126             return GL_ALPHA;
127         default:
128             return GL_ZERO;
129     }
130 }
131 
makeDirty()132 void TextureData::makeDirty() {
133     assert(m_saveableTexture);
134     m_saveableTexture->makeDirty();
135 }
136 
setTarget(GLenum _target)137 void TextureData::setTarget(GLenum _target) {
138     target = _target;
139     m_saveableTexture->setTarget(target);
140 }
141 
setMipmapLevelAtLeast(unsigned int level)142 void TextureData::setMipmapLevelAtLeast(unsigned int level) {
143     m_saveableTexture->setMipmapLevelAtLeast(level);
144 }
145