1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "FrameBufferAndroid.hpp"
16 #include "GrallocAndroid.hpp"
18 #include <system/window.h>
19 #include <cutils/log.h>
23 inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
25 #if ANDROID_PLATFORM_SDK_VERSION > 16
26 return native_window_dequeue_buffer_and_wait(window, buffer);
28 return window->dequeueBuffer(window, buffer);
32 inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
34 #if ANDROID_PLATFORM_SDK_VERSION > 16
35 return window->queueBuffer(window, buffer, fenceFd);
37 return window->queueBuffer(window, buffer);
41 inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
43 #if ANDROID_PLATFORM_SDK_VERSION > 16
44 return window->cancelBuffer(window, buffer, fenceFd);
46 return window->cancelBuffer(window, buffer);
50 FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
51 : FrameBuffer(width, height, false, false),
52 nativeWindow(window), buffer(nullptr)
54 nativeWindow->common.incRef(&nativeWindow->common);
55 native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
58 FrameBufferAndroid::~FrameBufferAndroid()
60 nativeWindow->common.decRef(&nativeWindow->common);
63 void FrameBufferAndroid::blit(void *source, const Rect *sourceRect, const Rect *destRect, Format sourceFormat, size_t sourceStride)
65 copy(source, sourceFormat, sourceStride);
75 queueBuffer(nativeWindow, buffer, -1);
79 void *FrameBufferAndroid::lock()
81 if(dequeueBuffer(nativeWindow, &buffer) != 0)
86 if(GrallocModule::getInstance()->lock(buffer->handle,
87 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
88 0, 0, buffer->width, buffer->height, &locked) != 0)
90 ALOGE("%s failed to lock buffer %p", __FUNCTION__, buffer);
94 if((buffer->width < width) || (buffer->height < height))
96 ALOGI("lock failed: buffer of %dx%d too small for window of %dx%d",
97 buffer->width, buffer->height, width, height);
101 switch(buffer->format)
103 default: ALOGE("Unsupported buffer format %d", buffer->format); ASSERT(false);
104 case HAL_PIXEL_FORMAT_RGB_565: destFormat = FORMAT_R5G6B5; break;
105 case HAL_PIXEL_FORMAT_RGB_888: destFormat = FORMAT_R8G8B8; break;
106 case HAL_PIXEL_FORMAT_RGBA_8888: destFormat = FORMAT_A8B8G8R8; break;
107 #if ANDROID_PLATFORM_SDK_VERSION > 16
108 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: destFormat = FORMAT_X8B8G8R8; break;
110 case HAL_PIXEL_FORMAT_RGBX_8888: destFormat = FORMAT_X8B8G8R8; break;
111 case HAL_PIXEL_FORMAT_BGRA_8888: destFormat = FORMAT_A8R8G8B8; break;
114 stride = buffer->stride * Surface::bytes(destFormat);
118 void FrameBufferAndroid::unlock()
122 ALOGE("%s: badness unlock with no active buffer", __FUNCTION__);
128 if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
130 ALOGE("%s: badness unlock failed", __FUNCTION__);
135 sw::FrameBuffer *createFrameBuffer(void *display, ANativeWindow* window, int width, int height)
137 return new sw::FrameBufferAndroid(window, width, height);