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