1 /* 2 * Copyright (C) 2024 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 RENDER_TEXTURE_H 17 #define RENDER_TEXTURE_H 18 19 #include "base/render_base.h" 20 #include "graphic/gl_utils.h" 21 #include "graphic/render_object.h" 22 #include "effect_buffer.h" 23 #include "effect_log.h" 24 25 namespace OHOS { 26 namespace Media { 27 namespace Effect { 28 class RenderTexture : public RenderObject { 29 public: 30 RenderTexture(GLsizei w, GLsizei h, GLenum interFmt = GL_RGBA8) 31 { 32 internalFormat_ = interFmt; 33 width_ = w; 34 height_ = h; 35 } 36 37 ~RenderTexture() = default; 38 Width()39 unsigned int Width() 40 { 41 return width_; 42 } 43 Height()44 unsigned int Height() 45 { 46 return height_; 47 } 48 Format()49 GLenum Format() 50 { 51 return internalFormat_; 52 } 53 SetName(unsigned int name)54 void SetName(unsigned int name) 55 { 56 name_ = name; 57 } 58 GetName()59 unsigned int GetName() 60 { 61 return name_; 62 } 63 Init()64 bool Init() override 65 { 66 if (!IsReady()) { 67 name_ = GLUtils::CreateTexture2D(width_, height_, 1, internalFormat_, GL_LINEAR, GL_LINEAR, 68 GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); 69 SetReady(true); 70 } 71 return true; 72 } 73 Release()74 bool Release() override 75 { 76 GLUtils::DeleteTexture(name_); 77 name_ = GL_ZERO; 78 width_ = 0; 79 height_ = 0; 80 SetReady(false); 81 return true; 82 } 83 84 private: 85 GLuint name_{ GL_ZERO }; 86 GLsizei width_{ 0 }; 87 GLsizei height_{ 0 }; 88 GLenum internalFormat_; 89 }; 90 91 class TextureSizeMeasurer { 92 public: operator()93 size_t operator () (const RenderTexturePtr &tex) 94 { 95 size_t width = static_cast<size_t>(tex->Width()); 96 size_t height = static_cast<size_t>(tex->Height()); 97 CHECK_AND_RETURN_RET_LOG(width != 0 && height !=0, 0, 98 "TextureSizeMeasurer: width or height is null! width=%{public}zu, height=%{public}zu", width, height); 99 CHECK_AND_RETURN_RET_LOG(width <= std::numeric_limits<size_t>::max() / height, 0, 100 "TextureSizeMeasurer: Integer overflow! width=%{public}zu, height=%{public}zu", width, height); 101 CHECK_AND_RETURN_RET_LOG(GLUtils::GetInternalFormatPixelByteSize(tex->Format()) <= 102 std::numeric_limits<size_t>::max() / (width * height), 0, 103 "TextureSizeMeasurer: Integer overflow! width=%{public}zu, height=%{public}zu, format=%{public}zu", 104 width, height, GLUtils::GetInternalFormatPixelByteSize(tex->Format())); 105 106 return (width * height * GLUtils::GetInternalFormatPixelByteSize(tex->Format())); 107 } 108 }; 109 } // namespace Effect 110 } // namespace Media 111 } // namespace OHOS 112 #endif