• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Android Open Source Project
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 expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <memory>
18 
19 #include "BorrowedImage.h"
20 #include "FrameworkFormats.h"
21 #include "Handle.h"
22 #include "Hwc2.h"
23 #include "aemu/base/files/Stream.h"
24 #include "gl/ColorBufferGl.h"
25 #include "render-utils/Renderer.h"
26 #include "snapshot/LazySnapshotObj.h"
27 
28 namespace gfxstream {
29 namespace gl {
30 class EmulationGl;
31 }  // namespace gl
32 }  // namespace gfxstream
33 
34 namespace gfxstream {
35 namespace vk {
36 class ColorBufferVk;
37 struct VkEmulation;
38 }  // namespace vk
39 }  // namespace gfxstream
40 
41 namespace gfxstream {
42 
43 class ColorBuffer : public android::snapshot::LazySnapshotObj<ColorBuffer> {
44    public:
45     static std::shared_ptr<ColorBuffer> create(gl::EmulationGl* emulationGl,
46                                                vk::VkEmulation* emulationVk, uint32_t width,
47                                                uint32_t height, GLenum format,
48                                                FrameworkFormat frameworkFormat, HandleType handle);
49 
50     static std::shared_ptr<ColorBuffer> onLoad(gl::EmulationGl* emulationGl,
51                                                vk::VkEmulation* emulationVk,
52                                                android::base::Stream* stream);
53     void onSave(android::base::Stream* stream);
54     void restore();
55 
getHndl()56     HandleType getHndl() const { return mHandle; }
getWidth()57     uint32_t getWidth() const { return mWidth; }
getHeight()58     uint32_t getHeight() const { return mHeight; }
getFormat()59     GLenum getFormat() const { return mFormat; }
getFrameworkFormat()60     FrameworkFormat getFrameworkFormat() const { return mFrameworkFormat; }
61 
62     void readToBytes(int x, int y, int width, int height, GLenum pixelsFormat, GLenum pixelsType,
63                      void* outPixels);
64     void readToBytesScaled(int pixelsWidth, int pixelsHeight, GLenum pixelsFormat,
65                            GLenum pixelsType, int pixelsRotation, Rect rect, void* outPixels);
66     void readYuvToBytes(int x, int y, int width, int height, void* outPixels, uint32_t pixelsSize);
67 
68     bool updateFromBytes(int x, int y, int width, int height, GLenum pixelsFormat,
69                          GLenum pixelsType, const void* pixels);
70     bool updateFromBytes(int x, int y, int width, int height, FrameworkFormat frameworkFormat,
71                          GLenum pixelsFormat, GLenum pixelsType, const void* pixels);
72     bool updateGlFromBytes(const void* bytes, std::size_t bytesSize);
73 
74     enum class UsedApi {
75         kGl,
76         kVk,
77     };
78     std::unique_ptr<BorrowedImageInfo> borrowForComposition(UsedApi api, bool isTarget);
79     std::unique_ptr<BorrowedImageInfo> borrowForDisplay(UsedApi api);
80 
81     bool flushFromGl();
82     bool flushFromVk();
83     bool flushFromVkBytes(const void* bytes, size_t bytesSize);
84     bool invalidateForGl();
85     bool invalidateForVk();
86 
87     GLuint glOpGetTexture();
88     bool glOpBlitFromCurrentReadBuffer();
89     bool glOpBindToTexture();
90     bool glOpBindToTexture2();
91     bool glOpBindToRenderbuffer();
92     void glOpReadback(unsigned char* img, bool readbackBgra);
93     void glOpReadbackAsync(GLuint buffer, bool readbackBgra);
94     bool glOpImportEglImage(void* image, bool preserveContent);
95     bool glOpImportEglNativePixmap(void* pixmap, bool preserveContent);
96     void glOpSwapYuvTexturesAndUpdate(GLenum format, GLenum type, FrameworkFormat frameworkFormat,
97                                       GLuint* textures);
98     bool glOpReadContents(size_t* outNumBytes, void* outContents);
99     bool glOpIsFastBlitSupported() const;
100     void glOpPostLayer(const ComposeLayer& l, int frameWidth, int frameHeight);
101     void glOpPostViewportScaledWithOverlay(float rotation, float dx, float dy);
102 
103    private:
104     ColorBuffer(HandleType, uint32_t width, uint32_t height, GLenum format,
105                 FrameworkFormat frameworkFormat);
106 
107     const HandleType mHandle;
108     const uint32_t mWidth;
109     const uint32_t mHeight;
110     const GLenum mFormat;
111     const FrameworkFormat mFrameworkFormat;
112 
113     // If GL emulation is enabled.
114     std::unique_ptr<gl::ColorBufferGl> mColorBufferGl;
115 
116     // If Vk emulation is enabled.
117     std::unique_ptr<vk::ColorBufferVk> mColorBufferVk;
118 
119     bool mGlAndVkAreSharingExternalMemory = false;
120 };
121 
122 typedef std::shared_ptr<ColorBuffer> ColorBufferPtr;
123 
124 struct ColorBufferRef {
125     ColorBufferPtr cb;
126     uint32_t refcount;  // number of client-side references
127 
128     // Tracks whether opened at least once. In O+,
129     // color buffers can be created/closed immediately,
130     // but then registered (opened) afterwards.
131     bool opened;
132 
133     // Tracks the time when this buffer got a close request while not being
134     // opened yet.
135     uint64_t closedTs;
136 };
137 
138 typedef std::unordered_map<HandleType, ColorBufferRef> ColorBufferMap;
139 typedef std::unordered_multiset<HandleType> ColorBufferSet;
140 
141 }  // namespace gfxstream
142