-
Notifications
You must be signed in to change notification settings - Fork 104
/
framebuffer_android.lua
198 lines (169 loc) · 6.66 KB
/
framebuffer_android.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
local ffi = require("ffi")
local android = require("android")
local BB = require("ffi/blitbuffer")
local C = ffi.C
--[[ configuration for devices with an electric paper display controller ]]--
-- does the device has an e-ink screen?
local has_eink_screen, eink_platform = android.isEink()
-- does the device needs to handle all screen refreshes
local has_eink_full_support = android.isEinkFull()
local full, partial, full_ui, partial_ui, fast, delay_page, delay_ui, delay_fast = android.getEinkConstants()
local framebuffer = {}
-- update a region of the screen
function framebuffer:_updatePartial(mode, delay, x, y, w, h)
local bb = self.full_bb or self.bb
w, x = BB.checkBounds(w or bb:getWidth(), x or 0, 0, bb:getWidth(), 0xFFFF)
h, y = BB.checkBounds(h or bb:getHeight(), y or 0, 0, bb:getHeight(), 0xFFFF)
x, y, w, h = bb:getPhysicalRect(x, y, w, h)
android.einkUpdate(mode, delay, x, y, (x + w), (y + h))
end
-- update the entire screen
function framebuffer:_updateFull()
-- freescale ntx platform
if has_eink_screen and (eink_platform == "freescale" or eink_platform == "qualcomm") then
if has_eink_full_support then
-- we handle the screen entirely
self:_updatePartial(full, delay_page)
else
-- we're racing against system driver. Let the system win and apply
-- a full update after it.
self:_updatePartial(full, 500)
end
-- rockchip rk3x platform
elseif has_eink_screen and (eink_platform == "rockchip") then
android.einkUpdate(full)
end
end
function framebuffer:init()
-- we present this buffer to the outside
self.bb = BB.new(android.screen.width, android.screen.height, BB.TYPE_BBRGB32)
self.bb:fill(BB.COLOR_WHITE)
self:_updateWindow()
framebuffer.parent.init(self)
end
-- resize on rotation or split view.
function framebuffer:resize()
android.screen.width = android.getScreenWidth()
android.screen.height = android.getScreenHeight()
local rotation
local inverse
if self.bb then
rotation = self.bb:getRotation()
inverse = self.bb:getInverse() == 1
self.bb:free()
end
self.bb = BB.new(android.screen.width, android.screen.height, BB.TYPE_BBRGB32)
-- Rotation and inverse must be inherited
if rotation then
self.bb:setRotation(rotation)
end
if inverse then
self.bb:invert()
end
self.bb:fill(inverse and BB.COLOR_BLACK or BB.COLOR_WHITE)
self:_updateWindow()
end
function framebuffer:getRotationMode()
if android.hasNativeRotation() then
return android.orientation.get()
else
return self.cur_rotation_mode
end
end
function framebuffer:setRotationMode(mode)
if android.hasNativeRotation() then
local key
if mode == 0 then key = "PORTRAIT"
elseif mode == 1 then key = "LANDSCAPE"
elseif mode == 2 then key = "REVERSE_PORTRAIT"
elseif mode == 3 then key = "REVERSE_LANDSCAPE" end
if key then
android.orientation.set(C["ASCREEN_ORIENTATION_" .. key])
end
else
framebuffer.parent.setRotationMode(self, mode)
end
end
function framebuffer:_updateWindow()
if android.app.window == nil then
android.LOGW("cannot blit: no window")
return
end
local buffer = ffi.new("ANativeWindow_Buffer[1]")
if android.lib.ANativeWindow_lock(android.app.window, buffer, nil) < 0 then
android.LOGW("Unable to lock window buffer")
return
end
local bb = nil
if buffer[0].format == C.WINDOW_FORMAT_RGBA_8888
or buffer[0].format == C.WINDOW_FORMAT_RGBX_8888
then
bb = BB.new(buffer[0].width, buffer[0].height, BB.TYPE_BBRGB32, buffer[0].bits, buffer[0].stride*4, buffer[0].stride)
elseif buffer[0].format == C.WINDOW_FORMAT_RGB_565 then
bb = BB.new(buffer[0].width, buffer[0].height, BB.TYPE_BBRGB16, buffer[0].bits, buffer[0].stride*2, buffer[0].stride)
else
android.LOGE("unsupported window format!")
end
if bb then
local ext_bb = self.full_bb or self.bb
-- Rotations and inverse are applied in the base ffi/framebuffer class, so our shadow buffer is already inverted and rotated.
-- All we need is to do is simply clone the invert and rotation settings, so that the blit below becomes 1:1 copy.
bb:setInverse(ext_bb:getInverse())
bb:setRotation(ext_bb:getRotation())
-- getUseCBB should *always* be true on Android, but let's be thorough...
if bb:getInverse() == 1 and BB:getUseCBB() then
-- If we're using the CBB (which we should), the invert flag has been thoroughly ignored up until now,
-- so, simply invert everything *now* ;).
-- The idea is that we absolutely want to avoid the Lua BB on Android, because it is *extremely* erratic,
-- because of the mcode alloc issues...
-- NOTE: CBB's invertblitFrom requires source & dest bb to be of the same type!
if bb:getType() == ext_bb:getType() then
-- In practice, this means RGB32, because our self.bb is always RGB32 (c.f., init above)
bb:invertblitFrom(ext_bb)
else
-- On ther other hand, if the window buffer is RGB565, things become uglier...
bb:blitFrom(ext_bb)
-- Fair warning, this is inaccurate for anything that isn't pure black or white on RGB565 ;).
bb:invertRect(0, 0, bb:getWidth(), bb:getHeight())
end
else
bb:blitFrom(ext_bb)
end
end
android.lib.ANativeWindow_unlockAndPost(android.app.window);
end
function framebuffer:refreshFullImp(x, y, w, h) -- luacheck: ignore
self:_updateWindow()
self:_updateFull()
end
function framebuffer:refreshPartialImp(x, y, w, h)
self:_updateWindow()
if has_eink_full_support then
self:_updatePartial(partial, delay_page, x, y, w, h)
end
end
function framebuffer:refreshFlashPartialImp(x, y, w, h)
self:_updateWindow()
if has_eink_full_support then
self:_updatePartial(full, delay_page, x, y, w, h)
end
end
function framebuffer:refreshUIImp(x, y, w, h)
self:_updateWindow()
if has_eink_full_support then
self:_updatePartial(partial_ui, delay_ui, x, y, w, h)
end
end
function framebuffer:refreshFlashUIImp(x, y, w, h)
self:_updateWindow()
if has_eink_full_support then
self:_updatePartial(full_ui, delay_ui, x, y, w, h)
end
end
function framebuffer:refreshFastImp(x, y, w, h)
self:_updateWindow()
if has_eink_full_support then
self:_updatePartial(fast, delay_fast, x, y, w, h)
end
end
return require("ffi/framebuffer"):extend(framebuffer)