1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef WEBGL_TEXTRUE_H 17 #define WEBGL_TEXTRUE_H 18 19 #include <GLES2/gl2.h> 20 #include <GLES2/gl2ext.h> 21 #include <GLES3/gl31.h> 22 23 #include "napi/n_exporter.h" 24 #include "webgl_arg.h" 25 #include "webgl_object.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 struct TexImageArg; 30 struct TexStorageArg; 31 class TextureLevelCtrl; 32 struct TextureLevelInfo; 33 struct TextureFormatTypeMap; 34 struct TextureFormatTypeMapCompare; 35 36 class WebGLTexture final : public NExporter, public WebGLObject { 37 public: 38 inline static const std::string className = "WebGLTexture"; 39 inline static const int objectType = WEBGL_OBJECT_TEXTURE; 40 inline static const int DEFAULT_TEXTURE = 0; 41 42 bool Export(napi_env env, napi_value exports) override; 43 44 std::string GetClassName() override; 45 46 static napi_value Constructor(napi_env env, napi_callback_info info); CreateObjectInstance(napi_env env,WebGLTexture ** instance)47 static NVal CreateObjectInstance(napi_env env, WebGLTexture** instance) 48 { 49 return WebGLObject::CreateObjectInstance<WebGLTexture>(env, instance); 50 } 51 SetTexture(uint32_t texture)52 void SetTexture(uint32_t texture) 53 { 54 texture_ = texture; 55 } 56 GetTexture()57 uint32_t GetTexture() const 58 { 59 return texture_; 60 } 61 62 bool SetTarget(GLenum target); GetTarget()63 GLenum GetTarget() const 64 { 65 return target_; 66 } 67 WebGLTexture()68 explicit WebGLTexture() : texture_(0) {}; 69 WebGLTexture(napi_env env,napi_value exports)70 WebGLTexture(napi_env env, napi_value exports) : NExporter(env, exports), texture_(0) {}; 71 72 ~WebGLTexture(); 73 GetObjectInstance(napi_env env,napi_value obj)74 static WebGLTexture* GetObjectInstance(napi_env env, napi_value obj) 75 { 76 return WebGLObject::GetObjectInstance<WebGLTexture>(env, obj); 77 } 78 79 bool SetTextureLevel(const TexImageArg& arg); 80 bool SetTexStorageInfo(const TexStorageArg* arg); 81 82 GLenum GetInternalFormat(GLenum target, GLint level) const; 83 GLenum GetType(GLenum target, GLint level) const; 84 GLsizei GetWidth(GLenum target, GLint level) const; 85 GLsizei GetHeight(GLenum target, GLint level) const; 86 GLsizei GetDepth(GLenum target, GLint level) const; CheckImmutable()87 bool CheckImmutable() const 88 { 89 return immutable_; 90 } 91 bool CheckValid(GLenum target, GLint level) const; 92 93 static int64_t ComputeTextureLevel(int64_t width, int64_t height, int64_t depth); 94 static GLint GetMaxTextureLevelForTarget(GLenum target, bool highWebGL); 95 static GLenum GetTypeFromInternalFormat(GLenum internalFormat); CheckNPOT(GLsizei width,GLsizei height)96 static bool CheckNPOT(GLsizei width, GLsizei height) 97 { 98 if (!width || !height) { 99 return false; 100 } 101 if ((static_cast<uint32_t>(width) & static_cast<uint32_t>(width - 1)) || 102 (static_cast<uint32_t>(height) & static_cast<uint32_t>(height - 1))) { 103 return true; 104 } 105 return false; 106 } 107 static bool CheckTextureSize(GLsizei offset, GLsizei w, GLsizei real); 108 static const std::vector<GLenum> &GetSupportInternalFormatGroup1(); 109 static const std::vector<GLenum> &GetSupportInternalFormatGroup2(); 110 static const std::vector<GLenum>& GetSupportedInternalFormats(); 111 static const std::vector<GLenum>& GetSupportedFormats(); 112 static const std::vector<GLenum>& GetSupportedTypes(); 113 static const std::set<TextureFormatTypeMap, TextureFormatTypeMapCompare>& GetSupportedFormatTypeMaps(); 114 static BufferDataType ChangeToBufferDataType(GLenum type); 115 private: 116 const TextureLevelInfo* GetTextureLevel(GLenum target, GLint level) const; 117 GLenum target_ { 0 }; 118 uint32_t texture_; 119 bool immutable_ { false }; 120 std::vector<TextureLevelCtrl> textureLevelInfos_ {}; 121 }; 122 123 class TextureLevelCtrl { 124 public: TextureLevelCtrl()125 explicit TextureLevelCtrl() {} ~TextureLevelCtrl()126 ~TextureLevelCtrl() 127 { 128 textureInfos_.clear(); 129 } 130 131 void Init(GLenum target); GetTextureLevel(GLint level)132 const TextureLevelInfo* GetTextureLevel(GLint level) const 133 { 134 if (textureInfos_.size() <= static_cast<size_t>(level)) { 135 return nullptr; 136 } 137 return &textureInfos_[level]; 138 } 139 140 private: 141 std::vector<TextureLevelInfo> textureInfos_ = {}; 142 }; 143 144 struct TextureLevelInfo { 145 bool valid { false }; 146 GLenum internalFormat { 0 }; 147 GLsizei width { 0 }; 148 GLsizei height { 0 }; 149 GLsizei depth { 0 }; 150 GLenum type { 0 }; 151 void Dump(const std::string &info, GLenum target, GLint level) const; 152 }; 153 154 struct TextureFormatTypeMap { 155 GLenum internalFormat; 156 GLenum format; 157 GLenum type; TextureFormatTypeMapTextureFormatTypeMap158 TextureFormatTypeMap(GLenum arg1, GLenum arg2, GLenum arg3) : internalFormat(arg1), format(arg2), type(arg3) {} 159 }; 160 161 struct TextureFormatTypeMapCompare { operatorTextureFormatTypeMapCompare162 bool operator() (const TextureFormatTypeMap& lhs, const TextureFormatTypeMap& rhs) const 163 { 164 return (lhs.internalFormat < rhs.internalFormat || 165 ((lhs.internalFormat == rhs.internalFormat) && (lhs.format < rhs.format)) || 166 ((lhs.internalFormat == rhs.internalFormat) && (lhs.format == rhs.format) && (lhs.type < rhs.type))); 167 } 168 }; 169 } // namespace Rosen 170 } // namespace OHOS 171 #endif // WEBGL_TEXTRUE_H 172