Texture.cpp revision 44eb2c00861098dd3e2950d923646814b4cc57c2
1/*
2 * Copyright (C) 2013 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#define LOG_TAG "OpenGLRenderer"
18
19#include <utils/Log.h>
20
21#include "Caches.h"
22#include "Texture.h"
23
24namespace android {
25namespace uirenderer {
26
27Texture::Texture()
28        : id(0)
29        , generation(0)
30        , blend(false)
31        , width(0)
32        , height(0)
33        , cleanup(false)
34        , bitmapSize(0)
35        , mipMap(false)
36        , uvMapper(nullptr)
37        , isInUse(false)
38        , mWrapS(GL_CLAMP_TO_EDGE)
39        , mWrapT(GL_CLAMP_TO_EDGE)
40        , mMinFilter(GL_NEAREST)
41        , mMagFilter(GL_NEAREST)
42        , mFirstFilter(true)
43        , mFirstWrap(true)
44        , mCaches(Caches::getInstance()) {
45}
46
47Texture::Texture(Caches& caches)
48        : id(0)
49        , generation(0)
50        , blend(false)
51        , width(0)
52        , height(0)
53        , cleanup(false)
54        , bitmapSize(0)
55        , mipMap(false)
56        , uvMapper(nullptr)
57        , isInUse(false)
58        , mWrapS(GL_CLAMP_TO_EDGE)
59        , mWrapT(GL_CLAMP_TO_EDGE)
60        , mMinFilter(GL_NEAREST)
61        , mMagFilter(GL_NEAREST)
62        , mFirstFilter(true)
63        , mFirstWrap(true)
64        , mCaches(caches) {
65}
66
67void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force,
68        GLenum renderTarget) {
69
70    if (mFirstWrap || force || wrapS != mWrapS || wrapT != mWrapT) {
71        mFirstWrap = false;
72
73        mWrapS = wrapS;
74        mWrapT = wrapT;
75
76        if (bindTexture) {
77            mCaches.textureState().bindTexture(renderTarget, id);
78        }
79
80        glTexParameteri(renderTarget, GL_TEXTURE_WRAP_S, wrapS);
81        glTexParameteri(renderTarget, GL_TEXTURE_WRAP_T, wrapT);
82    }
83}
84
85void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force,
86        GLenum renderTarget) {
87
88    if (mFirstFilter || force || min != mMinFilter || mag != mMagFilter) {
89        mFirstFilter = false;
90
91        mMinFilter = min;
92        mMagFilter = mag;
93
94        if (bindTexture) {
95            mCaches.textureState().bindTexture(renderTarget, id);
96        }
97
98        if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
99
100        glTexParameteri(renderTarget, GL_TEXTURE_MIN_FILTER, min);
101        glTexParameteri(renderTarget, GL_TEXTURE_MAG_FILTER, mag);
102    }
103}
104
105void Texture::deleteTexture() const {
106    mCaches.textureState().deleteTexture(id);
107}
108
109}; // namespace uirenderer
110}; // namespace android
111