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 #include <cutils/log.h>
18
19 namespace sw
20 {
dequeueBuffer(ANativeWindow * window,ANativeWindowBuffer ** buffer)21 inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
22 {
23 #if ANDROID_PLATFORM_SDK_VERSION > 16
24 return native_window_dequeue_buffer_and_wait(window, buffer);
25 #else
26 return window->dequeueBuffer(window, buffer);
27 #endif
28 }
29
queueBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)30 inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
31 {
32 #if ANDROID_PLATFORM_SDK_VERSION > 16
33 return window->queueBuffer(window, buffer, fenceFd);
34 #else
35 return window->queueBuffer(window, buffer);
36 #endif
37 }
38
cancelBuffer(ANativeWindow * window,ANativeWindowBuffer * buffer,int fenceFd)39 inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
40 {
41 #if ANDROID_PLATFORM_SDK_VERSION > 16
42 return window->cancelBuffer(window, buffer, fenceFd);
43 #else
44 return window->cancelBuffer(window, buffer);
45 #endif
46 }
47
FrameBufferAndroid(ANativeWindow * window,int width,int height)48 FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
49 : FrameBuffer(width, height, false, false),
50 nativeWindow(window), buffer(nullptr), gralloc(nullptr)
51 {
52 hw_module_t const* pModule;
53 hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &pModule);
54 gralloc = reinterpret_cast<gralloc_module_t const*>(pModule);
55
56 nativeWindow->common.incRef(&nativeWindow->common);
57 native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
58 }
59
~FrameBufferAndroid()60 FrameBufferAndroid::~FrameBufferAndroid()
61 {
62 nativeWindow->common.decRef(&nativeWindow->common);
63 }
64
blit(void * source,const Rect * sourceRect,const Rect * destRect,Format sourceFormat,size_t sourceStride)65 void FrameBufferAndroid::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
66 {
67 copy(source, sourceFormat, sourceStride);
68
69 if(buffer)
70 {
71 if(locked)
72 {
73 locked = nullptr;
74 unlock();
75 }
76
77 queueBuffer(nativeWindow, buffer, -1);
78 }
79 }
80
lock()81 void *FrameBufferAndroid::lock()
82 {
83 if(dequeueBuffer(nativeWindow, &buffer) != 0)
84 {
85 return nullptr;
86 }
87
88 if(gralloc->lock(gralloc, buffer->handle,
89 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
90 0, 0, buffer->width, buffer->height, &locked) != 0)
91 {
92 ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
93 return nullptr;
94 }
95
96 if((buffer->width < width) || (buffer->height < height))
97 {
98 ALOGI("lock failed: buffer of %dx%d too small for window of %dx%d",
99 buffer->width, buffer->height, width, height);
100 return nullptr;
101 }
102
103 switch(buffer->format)
104 {
105 default: ALOGE("Unsupported buffer format %d", buffer->format); ASSERT(false);
106 case HAL_PIXEL_FORMAT_RGB_565: destFormat = FORMAT_R5G6B5; break;
107 case HAL_PIXEL_FORMAT_RGB_888: destFormat = FORMAT_R8G8B8; break;
108 case HAL_PIXEL_FORMAT_RGBA_8888: destFormat = FORMAT_A8B8G8R8; break;
109 #if ANDROID_PLATFORM_SDK_VERSION > 16
110 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: destFormat = FORMAT_X8B8G8R8; break;
111 #endif
112 case HAL_PIXEL_FORMAT_RGBX_8888: destFormat = FORMAT_X8B8G8R8; break;
113 case HAL_PIXEL_FORMAT_BGRA_8888: destFormat = FORMAT_A8R8G8B8; break;
114 }
115
116 stride = buffer->stride * Surface::bytes(destFormat);
117 return locked;
118 }
119
unlock()120 void FrameBufferAndroid::unlock()
121 {
122 if(!buffer)
123 {
124 ALOGE("%s: badness unlock with no active buffer", __FUNCTION__);
125 return;
126 }
127
128 locked = nullptr;
129
130 if(gralloc->unlock(gralloc, buffer->handle) != 0)
131 {
132 ALOGE("%s: badness unlock failed", __FUNCTION__);
133 }
134 }
135 }
136
createFrameBuffer(void * display,ANativeWindow * window,int width,int height)137 sw::FrameBuffer *createFrameBuffer(void *display, ANativeWindow* window, int width, int height)
138 {
139 return new sw::FrameBufferAndroid(window, width, height);
140 }
141