1 /*
2  * Copyright (C) 2018 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 #pragma once
18 
19 extern "C" {
20 #include <virglrenderer.h>
21 }
22 
23 #include <cstdint>
24 #include <cstdlib>
25 #include <map>
26 
27 #include <GLES/gl.h>
28 
29 #include <sys/uio.h>
30 
31 #include "EglImage.h"
32 
33 struct Context;
34 
35 struct Resource {
36     static std::map<uint32_t, Resource*> map;
37 
ResourceResource38     Resource(virgl_renderer_resource_create_args* args_, uint32_t num_iovs_, iovec* iov_)
39         : args(*args_), num_iovs(num_iovs_), tex_id(0U), iov(iov_) {
40         reallocLinear();
41         map.emplace(args.handle, this);
42     }
43 
~ResourceResource44     ~Resource() {
45         map.erase(args.handle);
46         delete image;
47     }
48 
reallocLinearResource49     void reallocLinear() {
50         if (linearShadow && num_iovs <= 1)
51             free(linear);
52         linearShadow = num_iovs > 1 ? true : false;
53         if (linearShadow) {
54             uint32_t i;
55             for (i = 0, linearSize = 0U; i < num_iovs; i++)
56                 linearSize += iov[i].iov_len;
57             linear = realloc(linear, linearSize);
58         } else if (num_iovs == 1) {
59             linearSize = iov[0].iov_len;
60             linear = iov[0].iov_base;
61         } else {
62             linearSize = 0U;
63             linear = nullptr;
64         }
65     }
66 
67     std::map<uint32_t, Context*> context_map;
68     virgl_renderer_resource_create_args args;
69     EglImage* image = nullptr;
70     size_t linearSize = 0U;
71     void* linear = nullptr;
72     uint32_t num_iovs;
73     GLuint tex_id;
74     iovec* iov;
75 
76   private:
77     bool linearShadow = false;
78 };
79