1 /*
2 * Copyright (C) 2017 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 #pragma once
18 
19 #include "aemu/base/files/Stream.h"
20 
21 #include "GLcommon/ObjectData.h"
22 
23 #include <memory>
24 #include <unordered_map>
25 #include <vector>
26 
27 class SaveableTexture;
28 typedef std::shared_ptr<SaveableTexture> SaveableTexturePtr;
29 
30 class TextureData : public ObjectData
31 {
32 public:
TextureData()33     TextureData():  ObjectData(TEXTURE_DATA),
34                     width(0),
35                     height(0),
36                     depth(0),
37                     border(0),
38                     internalFormat(GL_RGBA),
39                     format(GL_RGBA),
40                     type(GL_UNSIGNED_BYTE),
41                     sourceEGLImage(0),
42                     hasStorage(false),
43                     wasBound(false),
44                     requiresAutoMipmap(false),
45                     compressed(false),
46                     compressedFormat(0),
47                     target(0) { resetSaveableTexture(); };
48     TextureData(android::base::Stream* stream);
49 
50     unsigned int width;
51     unsigned int height;
52     unsigned int depth;
53     unsigned int border;
54     unsigned int internalFormat;
55     // TODO: store emulated internal format
56     unsigned int format;
57     unsigned int type;
58     unsigned int sourceEGLImage;
59     bool hasStorage;
60     bool wasBound;
61     bool requiresAutoMipmap;
62     bool compressed;
63     unsigned int compressedFormat;
64     int32_t crop_rect[4] = {};
65     GLenum target;
66     // texStorageLevels tracks the storage level explicitly set by
67     // glTexStorage* (GLES3)
68     // maxMipmapLevel tracks the implicit maximum mipmap level that needs to
69     // be snapshot (GLES2 and GLES3)
70     // They are very similar concepts. But texStorageLevels is GLES3 only (only
71     // for textures initialized with glTexStorage*), and maxMipmapLevel is for
72     // both GLES2 and 3.
73     unsigned int texStorageLevels = 0;
74     int samples;
75     void onSave(android::base::Stream* stream,
76                 unsigned int globalName) const override;
77     void restore(ObjectLocalName localName,
78                  const getGlobalName_t& getGlobalName) override;
79     void setSaveableTexture(SaveableTexturePtr&& saveableTexture);
80     const SaveableTexturePtr& getSaveableTexture() const;
81     void resetSaveableTexture();
82 
83     unsigned int getGlobalName() const;
84     void setGlobalName(unsigned name);
85 
86     // deprecated; texture parameters are dealt with by SaveableTexture
87     void setTexParam(GLenum pname, GLint param);
88     GLenum getSwizzle(GLenum component) const;
89 
90     void makeDirty();
91     void setTarget(GLenum _target);
92     void setMipmapLevelAtLeast(unsigned int level);
93 protected:
94     // globalName is set during bind, used for snapshot when reading data from
95     // GPU Usually this should be kept by saveableTexture, but we might not have
96     // one yet.
97     unsigned int globalName = 0;
98 
99     // deprecated; texture parameters are dealt with by SaveableTexture
100     std::unordered_map<GLenum, GLint> m_texParam;
101 
102     SaveableTexturePtr m_saveableTexture;
103 };
104