diff --git a/source/visionEnhancementProviders/screenCurtain.py b/source/visionEnhancementProviders/screenCurtain.py index c917ea8fc83..93defaae7d7 100644 --- a/source/visionEnhancementProviders/screenCurtain.py +++ b/source/visionEnhancementProviders/screenCurtain.py @@ -1,7 +1,7 @@ # A part of NonVisual Desktop Access (NVDA) # This file is covered by the GNU General Public License. # See the file COPYING for more details. -# Copyright (C) 2018-2019 NV Access Limited, Babbage B.V., Leonard de Ruijter +# Copyright (C) 2018-2021 NV Access Limited, Babbage B.V., Leonard de Ruijter """Screen curtain implementation based on the windows magnification API. The Magnification API has been marked by MS as unsupported for WOW64 applications such as NVDA. (#12491) @@ -300,6 +300,61 @@ def confirmInitWithUser(self) -> bool: return res == wx.YES +def isScreenFullyBlack() -> bool: + """ + Takes a screen capture of all displays, through wxPython, which uses the winGDI API and checks: + - there is only one colour used in the image + - the first pixel of the screen capture is black + """ + """ + Iterating over a large bitmap image in python is slow (~4s depending on machine, number of pixels). + Due to no native support for calculating the number of black pixels, NVDA's screenBitmap module is avoided. + wxWidgets has native support for calculating the usage of colours within an image. + Using this method to calculate a colour usage histogram in native code takes ~0.4s comparably. + + An example of iterating over a screen bitmap, using NVDA's screenBitmap module. + from screenBitmap import ScreenBitmap + from itertools import chain + from winGDI import RGBQUAD + screenCapture = ScreenBitmap(screenSize[0], screenSize[1]).captureImage(0, 0, screenSize[0], screenSize[1]) + def _isPixelBlack(pixel: RGBQUAD) -> bool: + return pixel.rgbRed == 0 and pixel.rgbGreen == 0 and pixel.rgbBlue == 0 + numNonBlackPixels = len(list(filter(lambda p: not _isPixelBlack(p), chain.from_iterable(screenCapture)))) + return numNonBlackPixels == 0 + """ + screen = wx.ScreenDC() + screenSize = screen.GetSize() + bmp: wx.Bitmap = wx.Bitmap(screenSize[0], screenSize[1]) + mem = wx.MemoryDC(bmp) + mem.Blit(0, 0, screenSize[0], screenSize[1], screen, 0, 0) # copy screen over to bmp + del mem # Release bitmap + img: wx.Image = bmp.ConvertToImage() + # https://docs.wxwidgets.org/3.0/classwx_image.html#a7c9d557cd7ad577ed76e4337b1fd843a + hist = wx.ImageHistogram() + numberOfColours = img.ComputeHistogram(hist) + """ + Due to wxImageHistogram not being fully supported in wxPython, the histogram is not subscriptable, + and thus the key value cannot be accessed for colours. + https://github.com/wxWidgets/Phoenix/issues/1991 + This function could be improved in the future using the following logic: + numberOfBlackPixelsKey = hist.MakeKey(255, 255, 255) + numberOfBlackPixels = hist[numberOfBlackPixelsKey] + numberOfDisplayPixels = screenSize[0] * screenSize[1] + log.debug(f'''Screen Capture: + - number of colours: {numberOfColours} + - number of black pixels: {numberOfBlackPixels} + - number of total pixels: {numberOfDisplayPixels} + ''') + return numberOfColours == 1 and numberOfBlackPixels == numberOfDisplayPixels + """ + firstPixelIsBlack = img.GetRed(0, 0) == 0 and img.GetBlue(0, 0) == 0 and img.GetGreen(0, 0) == 0 + return numberOfColours == 1 and firstPixelIsBlack + + +class ScreenCurtainInitializationError(RuntimeError): + pass + + class ScreenCurtainProvider(providerBase.VisionEnhancementProvider): _settings = ScreenCurtainSettings() @@ -332,7 +387,13 @@ def __init__(self): try: Magnification.MagSetFullscreenColorEffect(TRANSFORM_BLACK) Magnification.MagShowSystemCursor(False) + if not isScreenFullyBlack(): + raise ScreenCurtainInitializationError("Screen curtain not activated properly") except Exception as e: + try: + Magnification.MagShowSystemCursor(True) + except Exception: + log.error(f"Could not restore cursor after screen curtain failure.", exc_info=True) Magnification.MagUninitialize() raise e if self.getSettings().playToggleSounds: