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