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 class 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 96 std::optional<BlobDescriptorInfo> exportBlob(); 97 98 #if GFXSTREAM_ENABLE_HOST_GLES 99 GLuint glOpGetTexture(); 100 bool glOpBlitFromCurrentReadBuffer(); 101 bool glOpBindToTexture(); 102 bool glOpBindToTexture2(); 103 bool glOpBindToRenderbuffer(); 104 void glOpReadback(unsigned char* img, bool readbackBgra); 105 void glOpReadbackAsync(GLuint buffer, bool readbackBgra); 106 bool glOpImportEglNativePixmap(void* pixmap, bool preserveContent); 107 void glOpSwapYuvTexturesAndUpdate(GLenum format, GLenum type, FrameworkFormat frameworkFormat, 108 GLuint* textures); 109 bool glOpReadContents(size_t* outNumBytes, void* outContents); 110 bool glOpIsFastBlitSupported() const; 111 void glOpPostLayer(const ComposeLayer& l, int frameWidth, int frameHeight); 112 void glOpPostViewportScaledWithOverlay(float rotation, float dx, float dy); 113 #endif 114 115 private: 116 ColorBuffer(HandleType, uint32_t width, uint32_t height, GLenum format, 117 FrameworkFormat frameworkFormat); 118 119 const HandleType mHandle; 120 const uint32_t mWidth; 121 const uint32_t mHeight; 122 const GLenum mFormat; 123 const FrameworkFormat mFrameworkFormat; 124 125 #if GFXSTREAM_ENABLE_HOST_GLES 126 // If GL emulation is enabled. 127 std::unique_ptr<gl::ColorBufferGl> mColorBufferGl; 128 #else 129 std::unique_ptr<uint32_t> mColorBufferGl = nullptr; 130 #endif 131 132 // If Vk emulation is enabled. 133 std::unique_ptr<vk::ColorBufferVk> mColorBufferVk; 134 135 bool mGlAndVkAreSharingExternalMemory = false; 136 bool mGlTexDirty = false; 137 }; 138 139 typedef std::shared_ptr<ColorBuffer> ColorBufferPtr; 140 141 struct ColorBufferRef { 142 ColorBufferPtr cb; 143 uint32_t refcount; // number of client-side references 144 145 // Tracks whether opened at least once. In O+, 146 // color buffers can be created/closed immediately, 147 // but then registered (opened) afterwards. 148 bool opened; 149 150 // Tracks the time when this buffer got a close request while not being 151 // opened yet. 152 uint64_t closedTs; 153 }; 154 155 typedef std::unordered_map<HandleType, ColorBufferRef> ColorBufferMap; 156 typedef std::unordered_multiset<HandleType> ColorBufferSet; 157 158 } // namespace gfxstream 159