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