Skip to content

Commit

Permalink
Fix resizing issues
Browse files Browse the repository at this point in the history
In summary, this change associates a 1:1
correspondance between a VkImage and XImage.
This will fix resize issues as well as prepare
to implement createInfo->oldSwapchain.

Bug: b/124265819
Change-Id: I42e8e1e7b5c9a5b64820156603dca10e2ab828ca
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/26368
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Hernan Liatis <hliatis@google.com>
  • Loading branch information
hliatis committed Mar 12, 2019
1 parent 7ce5de0 commit f945a5e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
19 changes: 18 additions & 1 deletion src/WSI/VkSurfaceKHR.hpp
Expand Up @@ -22,6 +22,21 @@
namespace vk
{

enum PresentImageStatus
{
NONEXISTENT, // Image wasn't made
AVAILABLE,
DRAWING,
PRESENTING,
};

struct PresentImage
{
VkImage image;
VkDeviceMemory imageMemory;
PresentImageStatus imageStatus;
};

class SurfaceKHR
{
public:
Expand All @@ -45,7 +60,9 @@ class SurfaceKHR
uint32_t getPresentModeCount() const;
VkResult getPresentModes(uint32_t* pPresentModeCount, VkPresentModeKHR* pPresentModes) const;

virtual void present(VkImage image, VkDeviceMemory imageData) = 0;
virtual void attachImage(PresentImage* image) = 0;
virtual void detachImage(PresentImage* image) = 0;
virtual void present(PresentImage* image) = 0;

private:
const std::vector<VkSurfaceFormatKHR> surfaceFormats =
Expand Down
5 changes: 4 additions & 1 deletion src/WSI/VkSwapchainKHR.cpp
Expand Up @@ -36,6 +36,7 @@ void SwapchainKHR::destroy(const VkAllocationCallbacks *pAllocator)
{
if (currentImage.imageStatus != NONEXISTENT)
{
vk::Cast(createInfo.surface)->detachImage(&currentImage);
vk::destroy(currentImage.imageMemory, pAllocator);
vk::destroy(currentImage.image, pAllocator);

Expand Down Expand Up @@ -116,6 +117,8 @@ VkResult SwapchainKHR::createImages(VkDevice device)
vkBindImageMemory(device, currentImage.image, currentImage.imageMemory, 0);

currentImage.imageStatus = AVAILABLE;

vk::Cast(createInfo.surface)->attachImage(&currentImage);
}

return VK_SUCCESS;
Expand Down Expand Up @@ -177,7 +180,7 @@ void SwapchainKHR::present(uint32_t index)
{
auto & image = images[index];
image.imageStatus = PRESENTING;
vk::Cast(createInfo.surface)->present(image.image, image.imageMemory);
vk::Cast(createInfo.surface)->present(&image);
image.imageStatus = AVAILABLE;
}

Expand Down
16 changes: 1 addition & 15 deletions src/WSI/VkSwapchainKHR.hpp
Expand Up @@ -18,27 +18,13 @@

#include "Vulkan/VkObject.hpp"
#include "Vulkan/VkImage.hpp"
#include "VkSurfaceKHR.hpp"

#include <vector>

namespace vk
{

enum PresentImageStatus
{
NONEXISTENT, // Image wasn't made
AVAILABLE,
DRAWING,
PRESENTING,
};

struct PresentImage
{
VkImage image;
VkDeviceMemory imageMemory;
PresentImageStatus imageStatus;
};

class SwapchainKHR : public Object<SwapchainKHR, VkSwapchainKHR>
{
public:
Expand Down
56 changes: 37 additions & 19 deletions src/WSI/XlibSurfaceKHR.cpp
Expand Up @@ -27,24 +27,12 @@ XlibSurfaceKHR::XlibSurfaceKHR(const VkXlibSurfaceCreateInfoKHR *pCreateInfo, vo
XVisualInfo xVisual;
Status status = libX11->XMatchVisualInfo(pDisplay, screen, 32, TrueColor, &xVisual);
bool match = (status != 0 && xVisual.blue_mask ==0xFF);
Visual *visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen);

XWindowAttributes attr;
libX11->XGetWindowAttributes(pDisplay, window, &attr);

int bytes_per_line = attr.width * 4;
int bytes_per_image = attr.height * bytes_per_line;

xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, nullptr, attr.width, attr.height, 32, bytes_per_line);

visual = match ? xVisual.visual : libX11->XDefaultVisual(pDisplay, screen);
}

void XlibSurfaceKHR::destroySurface(const VkAllocationCallbacks *pAllocator)
{
if(xImage)
{
XDestroyImage(xImage);
}

}

size_t XlibSurfaceKHR::ComputeRequiredAllocationSize(const VkXlibSurfaceCreateInfoKHR *pCreateInfo)
Expand All @@ -65,16 +53,46 @@ void XlibSurfaceKHR::getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCa
pSurfaceCapabilities->maxImageExtent = extent;
}

void XlibSurfaceKHR::present(VkImage image, VkDeviceMemory imageData)
void XlibSurfaceKHR::attachImage(PresentImage* image)
{
xImage->data = static_cast<char*>(vk::Cast(imageData)->getOffsetPointer(0));

XWindowAttributes attr;
libX11->XGetWindowAttributes(pDisplay, window, &attr);

libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, attr.width, attr.height);
VkExtent3D extent = vk::Cast(image->image)->getMipLevelExtent(0);

int bytes_per_line = vk::Cast(image->image)->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
char* buffer = static_cast<char*>(vk::Cast(image->imageMemory)->getOffsetPointer(0));

XImage* xImage = libX11->XCreateImage(pDisplay, visual, attr.depth, ZPixmap, 0, buffer, extent.width, extent.height, 32, bytes_per_line);

xImage->data = nullptr;
imageMap[image] = xImage;
}

void XlibSurfaceKHR::detachImage(PresentImage* image)
{
auto it = imageMap.find(image);
if(it != imageMap.end())
{
XImage* xImage = it->second;
xImage->data = nullptr; // the XImage does not actually own the buffer
XDestroyImage(xImage);
imageMap.erase(image);
}
}

void XlibSurfaceKHR::present(PresentImage* image)
{
auto it = imageMap.find(image);
if(it != imageMap.end())
{
XImage* xImage = it->second;

if(xImage->data)
{
VkExtent3D extent = vk::Cast(image->image)->getMipLevelExtent(0);
libX11->XPutImage(pDisplay, window, gc, xImage, 0, 0, 0, 0, extent.width, extent.height);
}
}
}

}
10 changes: 8 additions & 2 deletions src/WSI/XlibSurfaceKHR.hpp
Expand Up @@ -16,9 +16,12 @@
#define SWIFTSHADER_XLIBSURFACEKHR_HPP

#include "Vulkan/VkObject.hpp"
#include "Vulkan/VkImage.hpp"
#include "libX11.hpp"
#include "VkSurfaceKHR.hpp"

#include <map>

namespace vk {

class XlibSurfaceKHR : public SurfaceKHR, public ObjectBase<XlibSurfaceKHR, VkSurfaceKHR> {
Expand All @@ -33,13 +36,16 @@ class XlibSurfaceKHR : public SurfaceKHR, public ObjectBase<XlibSurfaceKHR, VkSu

void getSurfaceCapabilities(VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) const override;

void present(VkImage image, VkDeviceMemory imageData) override;
virtual void attachImage(PresentImage* image) override;
virtual void detachImage(PresentImage* image) override;
void present(PresentImage* image) override;

private:
Display *pDisplay;
Window window;
GC gc;
XImage *xImage = nullptr;
Visual *visual = nullptr;
std::map<PresentImage*, XImage*> imageMap;
};

}
Expand Down

0 comments on commit f945a5e

Please sign in to comment.