1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
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// ResourceManager.h : Defines the ResourceManager class, which tracks objects
16// shared by multiple GL contexts.
17
18#ifndef LIBGLESV2_RESOURCEMANAGER_H_
19#define LIBGLESV2_RESOURCEMANAGER_H_
20
21#include "common/NameSpace.hpp"
22#include "Common/MutexLock.hpp"
23
24#include <GLES2/gl2.h>
25
26#include <map>
27
28namespace es2
29{
30class Buffer;
31class Shader;
32class Program;
33class Texture;
34class Renderbuffer;
35class Sampler;
36class FenceSync;
37
38enum TextureType
39{
40 TEXTURE_2D,
41 TEXTURE_3D,
42 TEXTURE_2D_ARRAY,
43 TEXTURE_CUBE,
44 TEXTURE_2D_RECT,
45 TEXTURE_EXTERNAL,
46
47 TEXTURE_TYPE_COUNT,
48 TEXTURE_UNKNOWN
49};
50
51class ResourceManager
52{
53public:
54 ResourceManager();
55 ~ResourceManager();
56
57 void addRef();
58 void release();
59
60 GLuint createBuffer();
61 GLuint createShader(GLenum type);
62 GLuint createProgram();
63 GLuint createTexture();
64 GLuint createRenderbuffer();
65 GLuint createSampler();
66 GLuint createFenceSync(GLenum condition, GLbitfield flags);
67
68 void deleteBuffer(GLuint buffer);
69 void deleteShader(GLuint shader);
70 void deleteProgram(GLuint program);
71 void deleteTexture(GLuint texture);
72 void deleteRenderbuffer(GLuint renderbuffer);
73 void deleteSampler(GLuint sampler);
74 void deleteFenceSync(GLuint fenceSync);
75
76 Buffer *getBuffer(GLuint handle);
77 Shader *getShader(GLuint handle);
78 Program *getProgram(GLuint handle);
79 Texture *getTexture(GLuint handle);
80 Renderbuffer *getRenderbuffer(GLuint handle);
81 Sampler *getSampler(GLuint handle);
82 FenceSync *getFenceSync(GLuint handle);
83
84 void checkBufferAllocation(unsigned int buffer);
85 void checkTextureAllocation(GLuint texture, TextureType type);
86 void checkRenderbufferAllocation(GLuint handle);
87 void checkSamplerAllocation(GLuint sampler);
88
89 bool isSampler(GLuint sampler);
90 sw::MutexLock *getLock() { return &mMutex; }
91
92private:
93 std::size_t mRefCount;
94 sw::MutexLock mMutex;
95
96 gl::NameSpace<Buffer> mBufferNameSpace;
97 gl::NameSpace<Program> mProgramNameSpace;
98 gl::NameSpace<Shader> mShaderNameSpace;
99 gl::NameSpace<void> mProgramShaderNameSpace; // Shaders and programs share a namespace
100 gl::NameSpace<Texture> mTextureNameSpace;
101 gl::NameSpace<Renderbuffer> mRenderbufferNameSpace;
102 gl::NameSpace<Sampler> mSamplerNameSpace;
103 gl::NameSpace<FenceSync> mFenceSyncNameSpace;
104};
105
106}
107
108#endif // LIBGLESV2_RESOURCEMANAGER_H_
109