1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "FrameBufferAndroid.hpp"
16
17 #ifndef ANDROID_NDK_BUILD
18 #include "Common/GrallocAndroid.hpp"
19 #include <system/window.h>
20 #else
21 #include <android/native_window.h>
22 #endif
23
24 namespace sw
25 {
26 #if !defined(ANDROID_NDK_BUILD)
dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer)27 inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
28 {
29 #if ANDROID_PLATFORM_SDK_VERSION > 16
30 return native_window_dequeue_buffer_and_wait(window, buffer);
31 #else
32 return window->dequeueBuffer(window, buffer);
33 #endif
34 }
35
queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)36 inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
37 {
38 #if ANDROID_PLATFORM_SDK_VERSION > 16
39 return window->queueBuffer(window, buffer, fenceFd);
40 #else
41 return window->queueBuffer(window, buffer);
42 #endif
43 }
44
cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)45 inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
46 {
47 #if ANDROID_PLATFORM_SDK_VERSION > 16
48 return window->cancelBuffer(window, buffer, fenceFd);
49 #else
50 return window->cancelBuffer(window, buffer);
51 #endif
52 }
53 #endif // !defined(ANDROID_NDK_BUILD)
54
FrameBufferAndroid(ANativeWindow * window,int width,int height)55 FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
56 : FrameBuffer(width, height, false, false),
57 nativeWindow(window), buffer(nullptr)
58 {
59 #ifndef ANDROID_NDK_BUILD
60 nativeWindow->common.incRef(&nativeWindow->common);
61 native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
62 #endif
63 }
64
~FrameBufferAndroid()65 FrameBufferAndroid::~FrameBufferAndroid()
66 {
67 #ifndef ANDROID_NDK_BUILD
68 nativeWindow->common.decRef(&nativeWindow->common);
69 #endif
70 }
71
blit(sw::Surface * source,const Rect * sourceRect,const Rect * destRect)72 void FrameBufferAndroid::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
73 {
74 copy(source);
75
76 if(buffer)
77 {
78 if(framebuffer)
79 {
80 framebuffer = nullptr;
81 unlock();
82 }
83
84 #ifndef ANDROID_NDK_BUILD
85 queueBuffer(nativeWindow, buffer, -1);
86 #endif
87 }
88 }
89
lock()90 void *FrameBufferAndroid::lock()
91 {
92
93 #if defined(ANDROID_NDK_BUILD)
94 ANativeWindow_Buffer surfaceBuffer;
95 if (ANativeWindow_lock(nativeWindow, &surfaceBuffer, nullptr) != 0) {
96 TRACE("%s failed to lock buffer %p", __FUNCTION__, buffer);
97 return nullptr;
98 }
99 framebuffer = surfaceBuffer.bits;
100
101 if((surfaceBuffer.width < width) || (surfaceBuffer.height < height))
102 {
103 TRACE("lock failed: buffer of %dx%d too small for window of %dx%d",
104 surfaceBuffer.width, surfaceBuffer.height, width, height);
105 return nullptr;
106 }
107
108 switch(surfaceBuffer.format)
109 {
110 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: format = FORMAT_A8B8G8R8; break;
111 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: format = FORMAT_X8B8G8R8; break;
112 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
113 // Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
114 TRACE("Unsupported frame buffer format R8G8B8"); ASSERT(false);
115 format = FORMAT_R8G8B8; // Wrong component order.
116 break;
117 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: format = FORMAT_R5G6B5; break;
118 default:
119 TRACE("Unsupported frame buffer format %d", surfaceBuffer.format); ASSERT(false);
120 format = FORMAT_NULL;
121 break;
122 }
123 stride = surfaceBuffer.stride * Surface::bytes(format);
124 #else // !defined(ANDROID_NDK_BUILD)
125 if(dequeueBuffer(nativeWindow, &buffer) != 0)
126 {
127 return nullptr;
128 }
129 if(GrallocModule::getInstance()->lock(buffer->handle,
130 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
131 0, 0, buffer->width, buffer->height, &framebuffer) != 0)
132 {
133 TRACE("%s failed to lock buffer %p", __FUNCTION__, buffer);
134 return nullptr;
135 }
136
137 if((buffer->width < width) || (buffer->height < height))
138 {
139 TRACE("lock failed: buffer of %dx%d too small for window of %dx%d",
140 buffer->width, buffer->height, width, height);
141 return nullptr;
142 }
143
144 switch(buffer->format)
145 {
146 case HAL_PIXEL_FORMAT_RGB_565: format = FORMAT_R5G6B5; break;
147 case HAL_PIXEL_FORMAT_RGBA_8888: format = FORMAT_A8B8G8R8; break;
148 #if ANDROID_PLATFORM_SDK_VERSION > 16
149 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: format = FORMAT_X8B8G8R8; break;
150 #endif
151 case HAL_PIXEL_FORMAT_RGBX_8888: format = FORMAT_X8B8G8R8; break;
152 case HAL_PIXEL_FORMAT_BGRA_8888: format = FORMAT_A8R8G8B8; break;
153 case HAL_PIXEL_FORMAT_RGB_888:
154 // Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
155 TRACE("Unsupported frame buffer format RGB_888"); ASSERT(false);
156 format = FORMAT_R8G8B8; // Wrong component order.
157 break;
158 default:
159 TRACE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
160 format = FORMAT_NULL;
161 break;
162 }
163 stride = buffer->stride * Surface::bytes(format);
164 #endif // !defined(ANDROID_NDK_BUILD)
165
166 return framebuffer;
167 }
168
unlock()169 void FrameBufferAndroid::unlock()
170 {
171 if(!buffer)
172 {
173 TRACE("%s: badness unlock with no active buffer", __FUNCTION__);
174 return;
175 }
176
177 framebuffer = nullptr;
178
179 #ifdef ANDROID_NDK_BUILD
180 ANativeWindow_unlockAndPost(nativeWindow);
181 #else
182 if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
183 {
184 TRACE("%s: badness unlock failed", __FUNCTION__);
185 }
186 #endif
187 }
188 }
189
createFrameBuffer(void * display,ANativeWindow * window,int width,int height)190 sw::FrameBuffer *createFrameBuffer(void *display, ANativeWindow* window, int width, int height)
191 {
192 return new sw::FrameBufferAndroid(window, width, height);
193 }
194