OSDN Git Service

Refactor FrameBuffer state.
authorNicolas Capens <capn@google.com>
Fri, 11 Aug 2017 19:14:25 +0000 (15:14 -0400)
committerNicolas Capens <nicolascapens@google.com>
Tue, 7 Nov 2017 21:35:28 +0000 (21:35 +0000)
This mainly groups the state that is used for generating a new blit
routine into a second BlitState structure 'updateState'. It also
allowed for the FrameBuffer's own parameters to not have a 'dest'
prefix. Also, 'locked' was renamed to 'framebuffer', and 'target' to
'renderbuffer'.

Change-Id: I64e26f0b06f9f4419b8ca67e6fbb0dee8272898a
Reviewed-on: https://swiftshader-review.googlesource.com/11510
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
src/Main/FrameBuffer.cpp
src/Main/FrameBuffer.hpp
src/Main/FrameBufferAndroid.cpp
src/Main/FrameBufferDD.cpp
src/Main/FrameBufferGDI.cpp
src/Main/FrameBufferOSX.mm
src/Main/FrameBufferOzone.cpp
src/Main/FrameBufferX11.cpp
src/OpenGL/libGL/Image.cpp

index 96b8e02..29371db 100644 (file)
@@ -27,7 +27,7 @@
 #include <cutils/properties.h>
 #endif
 
-#define ASYNCHRONOUS_BLIT 0   // FIXME: Currently leads to rare race conditions
+#define ASYNCHRONOUS_BLIT false   // FIXME: Currently leads to rare race conditions
 
 namespace sw
 {
@@ -40,30 +40,18 @@ namespace sw
        {
                this->topLeftOrigin = topLeftOrigin;
 
-               locked = nullptr;
+               framebuffer = nullptr;
 
                this->width = width;
                this->height = height;
-               destFormat = FORMAT_X8R8G8B8;
-               sourceFormat = FORMAT_X8R8G8B8;
+               format = FORMAT_X8R8G8B8;
                stride = 0;
 
-               if(forceWindowed)
-               {
-                       fullscreen = false;
-               }
-
-               windowed = !fullscreen;
+               windowed = !fullscreen || forceWindowed;
 
                blitFunction = nullptr;
                blitRoutine = nullptr;
-
-               blitState.width = 0;
-               blitState.height = 0;
-               blitState.destFormat = FORMAT_X8R8G8B8;
-               blitState.sourceFormat = FORMAT_X8R8G8B8;
-               blitState.cursorWidth = 0;
-               blitState.cursorHeight = 0;
+               blitState = {};
 
                if(ASYNCHRONOUS_BLIT)
                {
@@ -86,21 +74,6 @@ namespace sw
                delete blitRoutine;
        }
 
-       int FrameBuffer::getWidth() const
-       {
-               return width;
-       }
-
-       int FrameBuffer::getHeight() const
-       {
-               return height;
-       }
-
-       int FrameBuffer::getStride() const
-       {
-               return stride;
-       }
-
        void FrameBuffer::setCursorImage(sw::Surface *cursorImage)
        {
                if(cursorImage)
@@ -130,7 +103,7 @@ namespace sw
                cursor.positionY = y;
        }
 
-       void FrameBuffer::copy(void *source, Format format, size_t stride)
+       void FrameBuffer::copy(void *source, Format sourceFormat, size_t sourceStride)
        {
                if(!source)
                {
@@ -142,15 +115,22 @@ namespace sw
                        return;
                }
 
-               sourceFormat = format;
+               updateState = {};
+               updateState.width = width;
+               updateState.height = height;
+               updateState.destFormat = format;
+               updateState.sourceFormat = sourceFormat;
+               updateState.stride = topLeftOrigin ? (int)sourceStride : -(int)sourceStride;
+               updateState.cursorWidth = cursor.width;
+               updateState.cursorHeight = cursor.height;
 
                if(topLeftOrigin)
                {
-                       target = source;
+                       renderbuffer = source;
                }
                else
                {
-                       target = (byte*)source + (height - 1) * stride;
+                       renderbuffer = (byte*)source + (height - 1) * sourceStride;
                }
 
                cursor.x = cursor.positionX - cursor.hotspotX;
@@ -173,25 +153,16 @@ namespace sw
 
        void FrameBuffer::copyLocked()
        {
-               BlitState update = {};
-               update.width = width;
-               update.height = height;
-               update.destFormat = destFormat;
-               update.sourceFormat = sourceFormat;
-               update.stride = stride;
-               update.cursorWidth = cursor.width;
-               update.cursorHeight = cursor.height;
-
-               if(memcmp(&blitState, &update, sizeof(BlitState)) != 0)
+               if(memcmp(&blitState, &updateState, sizeof(BlitState)) != 0)
                {
-                       blitState = update;
+                       blitState = updateState;
                        delete blitRoutine;
 
                        blitRoutine = copyRoutine(blitState);
                        blitFunction = (void(*)(void*, void*, Cursor*))blitRoutine->getEntry();
                }
 
-               blitFunction(locked, target, &cursor);
+               blitFunction(framebuffer, renderbuffer, &cursor);
        }
 
        Routine *FrameBuffer::copyRoutine(const BlitState &state)
index a18a0b1..f0e00a4 100644 (file)
@@ -41,10 +41,6 @@ namespace sw
 
                virtual ~FrameBuffer() = 0;
 
-               int getWidth() const;
-               int getHeight() const;
-               int getStride() const;
-
                virtual void flip(void *source, Format sourceFormat, size_t sourceStride) = 0;
                virtual void blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride) = 0;
 
@@ -58,22 +54,22 @@ namespace sw
                static Routine *copyRoutine(const BlitState &state);
 
        protected:
-               void copy(void *source, Format format, size_t stride);
+               void copy(void *source, Format sourceFormat, size_t sourceStride);
+
+               bool windowed;
+
+               void *framebuffer;   // Native window buffer.
                int width;
                int height;
-               Format sourceFormat;
-               Format destFormat;
                int stride;
-               bool windowed;
-
-               void *locked;   // Video memory back buffer
+               Format format;
 
        private:
                void copyLocked();
 
                static void threadFunction(void *parameters);
 
-               void *target;   // Render target buffer
+               void *renderbuffer;   // Render target buffer.
 
                struct Cursor
                {
@@ -92,7 +88,8 @@ namespace sw
 
                void (*blitFunction)(void *dst, void *src, Cursor *cursor);
                Routine *blitRoutine;
-               BlitState blitState;
+               BlitState blitState;     // State of the current blitRoutine.
+               BlitState updateState;   // State of the routine to be generated.
 
                static void blend(const BlitState &state, const Pointer<Byte> &d, const Pointer<Byte> &s, const Pointer<Byte> &c);
 
index a12ae94..5bac870 100644 (file)
@@ -67,9 +67,9 @@ namespace sw
 
                if(buffer)
                {
-                       if(locked)
+                       if(framebuffer)
                        {
-                               locked = nullptr;
+                               framebuffer = nullptr;
                                unlock();
                        }
 
@@ -86,7 +86,7 @@ namespace sw
 
                if(GrallocModule::getInstance()->lock(buffer->handle,
                                 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
-                                0, 0, buffer->width, buffer->height, &locked) != 0)
+                                0, 0, buffer->width, buffer->height, &framebuffer) != 0)
                {
                        ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
                        return nullptr;
@@ -101,26 +101,26 @@ namespace sw
 
                switch(buffer->format)
                {
-               case HAL_PIXEL_FORMAT_RGB_565:   destFormat = FORMAT_R5G6B5; break;
-               case HAL_PIXEL_FORMAT_RGBA_8888: destFormat = FORMAT_A8B8G8R8; break;
+               case HAL_PIXEL_FORMAT_RGB_565:   format = FORMAT_R5G6B5; break;
+               case HAL_PIXEL_FORMAT_RGBA_8888: format = FORMAT_A8B8G8R8; break;
 #if ANDROID_PLATFORM_SDK_VERSION > 16
-               case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: destFormat = FORMAT_X8B8G8R8; break;
+               case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: format = FORMAT_X8B8G8R8; break;
 #endif
-               case HAL_PIXEL_FORMAT_RGBX_8888: destFormat = FORMAT_X8B8G8R8; break;
-               case HAL_PIXEL_FORMAT_BGRA_8888: destFormat = FORMAT_A8R8G8B8; break;
+               case HAL_PIXEL_FORMAT_RGBX_8888: format = FORMAT_X8B8G8R8; break;
+               case HAL_PIXEL_FORMAT_BGRA_8888: format = FORMAT_A8R8G8B8; break;
                case HAL_PIXEL_FORMAT_RGB_888:
                        // Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
                        ALOGE("Unsupported frame buffer format RGB_888"); ASSERT(false);
-                       destFormat = FORMAT_R8G8B8;   // Wrong component order.
+                       format = FORMAT_R8G8B8;   // Wrong component order.
                        break;
                default:
                        ALOGE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
-                       destFormat = FORMAT_NULL;
+                       format = FORMAT_NULL;
                        break;
                }
 
-               stride = buffer->stride * Surface::bytes(destFormat);
-               return locked;
+               stride = buffer->stride * Surface::bytes(format);
+               return framebuffer;
        }
 
        void FrameBufferAndroid::unlock()
@@ -131,7 +131,7 @@ namespace sw
                        return;
                }
 
-               locked = nullptr;
+               framebuffer = nullptr;
 
                if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
                {
index 36570bd..1f87bdf 100644 (file)
@@ -38,7 +38,7 @@ namespace sw
                frontBuffer = 0;
                backBuffer = 0;
 
-               locked = 0;
+               framebuffer = nullptr;
 
                ddraw = LoadLibrary("ddraw.dll");
                DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress(ddraw, "DirectDrawCreate");
@@ -106,13 +106,13 @@ namespace sw
 
                        switch(ddsd.ddpfPixelFormat.dwRGBBitCount)
                        {
-                       case 32: destFormat = FORMAT_X8R8G8B8; break;
-                       case 24: destFormat = FORMAT_R8G8B8;   break;
-                       case 16: destFormat = FORMAT_R5G6B5;   break;
-                       default: destFormat = FORMAT_NULL;     break;
+                       case 32: format = FORMAT_X8R8G8B8; break;
+                       case 24: format = FORMAT_R8G8B8;   break;
+                       case 16: format = FORMAT_R5G6B5;   break;
+                       default: format = FORMAT_NULL;     break;
                        }
 
-                       if((result != DD_OK && result != DDERR_PRIMARYSURFACEALREADYEXISTS) || (destFormat == FORMAT_NULL))
+                       if((result != DD_OK && result != DDERR_PRIMARYSURFACEALREADYEXISTS) || (format == FORMAT_NULL))
                        {
                                assert(!"Failed to initialize graphics: Incompatible display mode.");
                        }
@@ -206,17 +206,17 @@ namespace sw
 
                do
                {
-                       destFormat = FORMAT_X8R8G8B8;
+                       format = FORMAT_X8R8G8B8;
                        result = directDraw->SetDisplayMode(width, height, 32);
 
                        if(result == DDERR_INVALIDMODE)
                        {
-                               destFormat = FORMAT_R8G8B8;
+                               format = FORMAT_R8G8B8;
                                result = directDraw->SetDisplayMode(width, height, 24);
 
                                if(result == DDERR_INVALIDMODE)
                                {
-                                       destFormat = FORMAT_R5G6B5;
+                                       format = FORMAT_R5G6B5;
                                        result = directDraw->SetDisplayMode(width, height, 16);
 
                                        if(result == DDERR_INVALIDMODE)
@@ -404,14 +404,14 @@ namespace sw
 
        void *FrameBufferDD::lock()
        {
-               if(locked)
+               if(framebuffer)
                {
-                       return locked;
+                       return framebuffer;
                }
 
                if(!readySurfaces())
                {
-                       return 0;
+                       return nullptr;
                }
 
                DDSURFACEDESC DDSD;
@@ -425,21 +425,21 @@ namespace sw
                        height = DDSD.dwHeight;
                        stride = DDSD.lPitch;
 
-                       locked = DDSD.lpSurface;
+                       framebuffer = DDSD.lpSurface;
 
-                       return locked;
+                       return framebuffer;
                }
 
-               return 0;
+               return nullptr;
        }
 
        void FrameBufferDD::unlock()
        {
-               if(!locked || !backBuffer) return;
+               if(!framebuffer || !backBuffer) return;
 
                backBuffer->Unlock(0);
 
-               locked = 0;
+               framebuffer = nullptr;
        }
 
        void FrameBufferDD::drawText(int x, int y, const char *string, ...)
index 551a476..2668e10 100644 (file)
@@ -37,7 +37,7 @@ namespace sw
 
                init(this->windowHandle);
 
-               destFormat = FORMAT_X8R8G8B8;
+               format = FORMAT_X8R8G8B8;
        }
 
        FrameBufferGDI::~FrameBufferGDI()
@@ -64,7 +64,7 @@ namespace sw
        {
                stride = width * 4;
 
-               return locked;
+               return framebuffer;
        }
 
        void FrameBufferGDI::unlock()
@@ -146,7 +146,7 @@ namespace sw
                bitmapInfo.bmiHeader.biWidth = width;
                bitmapInfo.bmiHeader.biCompression = BI_RGB;
 
-               bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &locked, 0, 0);
+               bitmap = CreateDIBSection(bitmapContext, &bitmapInfo, DIB_RGB_COLORS, &framebuffer, 0, 0);
                SelectObject(bitmapContext, bitmap);
 
                updateBounds(window);
index 3a0cfc7..c99d11a 100644 (file)
@@ -25,7 +25,7 @@ namespace sw {
                : FrameBuffer(width, height, false, false), width(width), height(height),
                  layer(layer), buffer(nullptr), provider(nullptr), currentImage(nullptr)
        {
-               destFormat = sw::FORMAT_X8B8G8R8;
+               format = sw::FORMAT_X8B8G8R8;
                int bufferSize = width * height * 4 * sizeof(uint8_t);
                buffer = new uint8_t[bufferSize];
                provider = CGDataProviderCreateWithData(nullptr, buffer, bufferSize, nullptr);
@@ -72,13 +72,13 @@ namespace sw {
        void *FrameBufferOSX::lock()
        {
                stride = width * 4 * sizeof(uint8_t);
-               locked = buffer;
-               return locked;
+               framebuffer = buffer;
+               return framebuffer;
        };
 
        void FrameBufferOSX::unlock()
        {
-               locked = nullptr;
+               framebuffer = nullptr;
        };
 }
 
index a9de407..3261f30 100644 (file)
@@ -18,9 +18,9 @@ namespace sw
 {
        FrameBufferOzone::FrameBufferOzone(intptr_t display, intptr_t window, int width, int height) : FrameBuffer(width, height, false, false)
        {
-               buffer = sw::Surface::create(width, height, 1, destFormat, nullptr,
-                                            sw::Surface::pitchB(width, destFormat, true),
-                                            sw::Surface::sliceB(width, height, destFormat, true));
+               buffer = sw::Surface::create(width, height, 1, format, nullptr,
+                                            sw::Surface::pitchB(width, format, true),
+                                            sw::Surface::sliceB(width, height, format, true));
        }
 
        FrameBufferOzone::~FrameBufferOzone()
@@ -30,16 +30,16 @@ namespace sw
 
        void *FrameBufferOzone::lock()
        {
-               locked = buffer->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);
+               framebuffer = buffer->lockInternal(0, 0, 0, sw::LOCK_READWRITE, sw::PUBLIC);
 
-               return locked;
+               return framebuffer;
        }
 
        void FrameBufferOzone::unlock()
        {
                buffer->unlockInternal();
 
-               locked = nullptr;
+               framebuffer = nullptr;
        }
 
        void FrameBufferOzone::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
index 365c638..2e50f42 100644 (file)
@@ -120,14 +120,14 @@ namespace sw
        void *FrameBufferX11::lock()
        {
                stride = x_image->bytes_per_line;
-               locked = buffer;
+               framebuffer = buffer;
 
-               return locked;
+               return framebuffer;
        }
 
        void FrameBufferX11::unlock()
        {
-               locked = nullptr;
+               framebuffer = nullptr;
        }
 
        void FrameBufferX11::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
index 8126de2..4547d92 100644 (file)
@@ -33,7 +33,7 @@ namespace gl
                        return texture->getResource();
                }
 
-               return 0;
+               return nullptr;
        }
 
        Image::Image(Texture *parentTexture, GLsizei width, GLsizei height, GLenum format, GLenum type)
@@ -141,7 +141,7 @@ namespace gl
 
        void Image::unbind()
        {
-               parentTexture = 0;
+               parentTexture = nullptr;
 
                release();
        }