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