1 /*
2 * Copyright (c) 2021 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 #include "texture.h"
17
18 #include <EGL/egl.h>
19 #include <GLES2/gl2.h>
20
21 #include <GLES2/gl2ext.h>
22
23 #include <gslogger.h>
24
Texture(void * buffer,int32_t width,int32_t height)25 Texture::Texture(void *buffer, int32_t width, int32_t height)
26 : buffer_(buffer), width_(width), height_(height)
27 {
28 glGenTextures(1, &rendererID_);
29 glBindTexture(GL_TEXTURE_2D, rendererID_);
30
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
33 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
34 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
35
36 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width_, height_, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, buffer_);
37 }
38
Texture(uint32_t texture)39 Texture::Texture(uint32_t texture)
40 {
41 rendererID_ = texture;
42 }
43
~Texture()44 Texture::~Texture()
45 {
46 glDeleteTextures(1, &rendererID_);
47 }
48
Bind(uint32_t slot) const49 void Texture::Bind(uint32_t slot) const
50 {
51 glActiveTexture(GL_TEXTURE0 + slot);
52 glBindTexture(GL_TEXTURE_2D, rendererID_);
53 }
54
Unbind()55 void Texture::Unbind()
56 {
57 glBindTexture(GL_TEXTURE_2D, 0);
58 }
59