getpixel slow on Win 10.

Windows specific questions.
dodicat
Posts: 6232
Joined: Jan 10, 2006 20:30
Location: Scotland

getpixel slow on Win 10.

Postby dodicat » Jun 19, 2016 13:14

For this:

Code: Select all

#include "windows.bi"
screen 19,32
dim as any ptr win
dim as ulong colour
win=getdc(0)
dim as double t=timer
for n as long=1 to 100
    colour=GetPixel(win,10,10)
next n
print timer-t
circle(400,300),50,colour,,,,f
sleep
     

It takes about 1.7 seconds on 64 bit Win 10, on both the 64 bit and 32 bit compilers.

On Win XP 32 bit it takes about .0007 seconds.

It could be some fault on this computer, how do other Win 10 64 bit users find it?
caseih
Posts: 1424
Joined: Feb 26, 2007 5:32

Re: getpixel slow on Win 10.

Postby caseih » Jun 19, 2016 13:55

I'm not surprised that GetPixel is slower than it used to be. Reading pixels is not a simple thing when you have a fancy graphics card and are dealing with compositing of layers of images that the modern windows UI uses. Writing to video is always much faster than reading from it, as the old concept of a backing store for the whole screen doesn't directly apply anymore.

Perhaps one solution is to render the window contents to a bitmap in memory and then access that. GetPixel is slow probably because it does this for each and every call to GetPixel.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: getpixel slow on Win 10.

Postby sancho2 » Jun 19, 2016 22:59

Win 10, compiled in 32 bit, 1.68 seconds.
Its so slow the FB Point function is faster.
D.J.Peters
Posts: 8019
Joined: May 28, 2005 3:28
Contact:

Re: getpixel slow on Win 10.

Postby D.J.Peters » Jun 20, 2016 1:07

The problem in your case are:
GetPixel() will read from visible desktop memory and must locked every memory access pixel by pixel.
The driver modell are changed in WIN 10 and you have to use DirectX for direct video memory acceess.
But you can copy the whole desktop once and get the pixel as fast as possible.

Joshy

Code: Select all

#include "windows.bi"

function getPixelFast(byval x as long,byval y as long) as ulong
  static as HDC        memDC
  static as HDC        screenDC
  static as BITMAPINFO bi
  static as HBITMAP    bmp
  static as long       w,h
  static as any ptr pixels=cptr(any ptr,&HC0FFE)
  static as ulong ptr row
  if (pixels=&HC0FFE) then
    screenDC = GetDC(HWND_DESKTOP)
    w = GetSystemMetrics (SM_CXSCREEN)
    h = GetSystemMetrics (SM_CYSCREEN)
    windowtitle " " & w & "," & h
    pixels = allocate(w*h*4)
    memDC = CreateCompatibleDC(screenDC)
    bi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER)
    bi.bmiHeader.biWidth       = w
    bi.bmiHeader.biHeight      = -h
    bi.bmiHeader.biPlanes      = 1
    bi.bmiHeader.biBitCount    = 32
    bi.bmiHeader.biCompression = BI_RGB
    bi.bmiHeader.biSizeImage   = w * h * 4
    bmp = CreateDIBSection(memDC, @bi, DIB_RGB_COLORS, @pixels, NULL, NULL)
    SelectObject(memDC,bmp)
    BitBlt(memDC,0,0,w,h,screenDC,0,0,SRCCOPY)
    row = pixels
  end if
  if x<0 or x>w-1 then return -1
  if y<0 or y>h-1 then return -1
  return row[y*w+x]
end function

const scr_w = 640
const scr_h = 480
screenres scr_w,scr_h,32

var tStart = Timer()
for y as long=0 to scr_h-1
  for x as long=0 to scr_w-1
    pset (x,y),getPixelFast(x,y)
  next
next
var tEnd = Timer()
windowtitle "time: " & tEnd - tStart
sleep
dodicat
Posts: 6232
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: getpixel slow on Win 10.

Postby dodicat » Jun 23, 2016 14:37

Thank you D.J.Peters.
The more I use Win 10, the less I like it.
I see that it has lost the ability to retrieve the Mouse wheel value from a transparent part of no-frame window!

Code: Select all

'Win 10 mouse wheel test
#include "fbgfx.bi"
screen 19,32,,FB.GFX_SHAPED_WINDOW or FB.GFX_ALWAYS_ON_TOP
color ,rgb(255,0,255)

dim as long mousewheel

do
    getmouse 0,0,mousewheel
    screenlock
    cls
    line(0,0)-(799,599),rgb(0,200,0),b
    line(0,0)-(100,100),rgb(0,0,200),bf
    draw string(10,5),"Wheel test",rgb(255,255,255)
    draw string(10,20),str(mousewheel),rgb(200,0,0)
    screenunlock
    sleep 1,1
loop until inkey=chr(27)
screen 0 
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: getpixel slow on Win 10.

Postby MichaelW » Jun 26, 2016 8:20

Perhaps part of the problem in the 64-bit code is your choice of a 32-bit loop counter.
MrSwiss
Posts: 3364
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: getpixel slow on Win 10.

Postby MrSwiss » Jun 26, 2016 9:49

@dodicat,

mousewheel Variable; must be an Integer (acc. to FB-DOC).

You might want to try my adapted Example 2 from DOC:

Code: Select all

' simplyfied Mouse Type (NO Union), from Ex. 2 in FB-DOC
Type mouse
    As Integer res, x, y, wheel, buttons, clip
End Type

' set values relative to Screen HW (here: HD=1920x1080)
ScreenRes 1900, 1020, 32
Dim As mouse m

' empty keyb.-buffer (rather save, than sorry)
While Len( InKey() ) : Wend

Do
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )   ' get Mouse info
    ScreenLock
    Cls
    Print Using "res = #"; m.res   ' if 1 then mouse is "out of window"
    Print Using "x = ####  y = ####  wheel = +###  buttons = ##  clip = ##"; _
             m.x; m.y; m.wheel; m.buttons; m.clip
    ScreenUnLock

    Sleep 10, 1
Loop Until Len( InKey() )   ' user input quit's Loop/Prog.
' ----- EOF ----- '
dodicat
Posts: 6232
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: getpixel slow on Win 10.

Postby dodicat » Jun 26, 2016 10:17

Thanks MrSwiss and MichaelW

Integers for getmouse make no difference.
Type mouse makes no difference.
With a shaped window Win XP managed to get the mouse wheel value from any part of the window, including transparent parts.
I jumped from XP to 64 bit Win 10, so I don't know where this capability got lost in Windows.

It is a loss of functionality in freebasic, maybe not much, but annoying, as is getpixel's demise.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: getpixel slow on Win 10.

Postby Zippy » Jun 26, 2016 12:09

dodicat wrote:Thanks MrSwiss and MichaelW

Integers for getmouse make no difference.
Type mouse makes no difference.
With a shaped window Win XP managed to get the mouse wheel value from any part of the window, including transparent parts.
I jumped from XP to 64 bit Win 10, so I don't know where this capability got lost in Windows.

It is a loss of functionality in freebasic, maybe not much, but annoying, as is getpixel's demise.

This is a Windows 10 feature, not a bug. A feature you may turn OFF if you wish.
See:
https://www.thurrott.com/windows/window ... ve-windows

Welcome to modern Windows. Enjoy your stay.
dodicat
Posts: 6232
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: getpixel slow on Win 10.

Postby dodicat » Jun 26, 2016 12:44

Yea zippy, thanks.
The getpixel problem seems unchangeable.
I am sure that opengl game programmers would use getpixel to acquire screen targets ETC.
As for the Win 10 mouse wheel problem, I don't think that it is unsolvable within freebasic, the graphical library perhaps.
Anyway, thank you everybody for replies to this.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 2 guests