huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
 
#include "FrameBufferAndroid.hpp"
 
#include "System/GrallocAndroid.hpp"
 
#include <system/window.h>
 
namespace sw
{
   inline int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer)
   {
       #if ANDROID_PLATFORM_SDK_VERSION > 16
           return native_window_dequeue_buffer_and_wait(window, buffer);
       #else
           return window->dequeueBuffer(window, buffer);
       #endif
   }
 
   inline int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
   {
       #if ANDROID_PLATFORM_SDK_VERSION > 16
           return window->queueBuffer(window, buffer, fenceFd);
       #else
           return window->queueBuffer(window, buffer);
       #endif
   }
 
   inline int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd)
   {
       #if ANDROID_PLATFORM_SDK_VERSION > 16
           return window->cancelBuffer(window, buffer, fenceFd);
       #else
           return window->cancelBuffer(window, buffer);
       #endif
   }
 
   FrameBufferAndroid::FrameBufferAndroid(ANativeWindow* window, int width, int height)
       : FrameBuffer(width, height, false, false),
         nativeWindow(window), buffer(nullptr)
   {
       nativeWindow->common.incRef(&nativeWindow->common);
       native_window_set_usage(nativeWindow, GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN);
   }
 
   FrameBufferAndroid::~FrameBufferAndroid()
   {
       nativeWindow->common.decRef(&nativeWindow->common);
   }
 
   void FrameBufferAndroid::blit(sw::Surface *source, const Rect *sourceRect, const Rect *destRect)
   {
       copy(source);
 
       if(buffer)
       {
           if(framebuffer)
           {
               framebuffer = nullptr;
               unlock();
           }
 
           queueBuffer(nativeWindow, buffer, -1);
       }
   }
 
   void *FrameBufferAndroid::lock()
   {
       if(dequeueBuffer(nativeWindow, &buffer) != 0)
       {
           return nullptr;
       }
 
       if(GrallocModule::getInstance()->lock(buffer->handle,
                        GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN,
                        0, 0, buffer->width, buffer->height, &framebuffer) != 0)
       {
           TRACE("%s failed to lock buffer %p", __FUNCTION__, buffer);
           return nullptr;
       }
 
       if((buffer->width < width) || (buffer->height < height))
       {
           TRACE("lock failed: buffer of %dx%d too small for window of %dx%d",
                 buffer->width, buffer->height, width, height);
           return nullptr;
       }
 
       switch(buffer->format)
       {
       case HAL_PIXEL_FORMAT_RGB_565:   format = VK_FORMAT_R5G6B5_UNORM_PACK16; break;
       case HAL_PIXEL_FORMAT_RGBA_8888: format = VK_FORMAT_R8G8B8A8_UNORM; break;
#if ANDROID_PLATFORM_SDK_VERSION > 16
       case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: format = FORMAT_X8B8G8R8; break;
#endif
       case HAL_PIXEL_FORMAT_RGBX_8888: format = FORMAT_X8B8G8R8; break;
       case HAL_PIXEL_FORMAT_BGRA_8888: format = VK_FORMAT_B8G8R8A8_UNORM; break;
       case HAL_PIXEL_FORMAT_RGB_888:
           // Frame buffers are expected to have 16-bit or 32-bit colors, not 24-bit.
           TRACE("Unsupported frame buffer format RGB_888"); ASSERT(false);
           format = FORMAT_R8G8B8;   // Wrong component order.
           break;
       default:
           TRACE("Unsupported frame buffer format %d", buffer->format); ASSERT(false);
           format = VK_FORMAT_UNDEFINED;
           break;
       }
 
       stride = buffer->stride * Surface::bytes(format);
       return framebuffer;
   }
 
   void FrameBufferAndroid::unlock()
   {
       if(!buffer)
       {
           TRACE("%s: badness unlock with no active buffer", __FUNCTION__);
           return;
       }
 
       framebuffer = nullptr;
 
       if(GrallocModule::getInstance()->unlock(buffer->handle) != 0)
       {
           TRACE("%s: badness unlock failed", __FUNCTION__);
       }
   }
}
 
sw::FrameBuffer *createFrameBuffer(void *display, ANativeWindow* window, int width, int height)
{
   return new sw::FrameBufferAndroid(window, width, height);
}