1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 //******************************************************************************
6 // This is a copy of the coresponding android_webview/public/browser header.
7 // Any changes to the interface should be made there as well.
8 //******************************************************************************
9 
10 #ifndef ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_FN_H_
11 #define ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_FN_H_
12 
13 #include <vulkan/vulkan.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 // In order to make small changes backwards compatible, all structs passed from
20 // android to chromium are versioned.
21 //
22 // 1 is Android Q. This matches kAwDrawGLInfoVersion version 3.
23 // 2 Adds transfer_function_* and color_space_toXYZD50 to AwDrawFn_DrawGLParams.
24 static const int kAwDrawFnVersion = 2;
25 
26 struct AwDrawFn_OnSyncParams {
27   int version;
28 
29   bool apply_force_dark;
30 };
31 
32 struct AwDrawFn_DrawGLParams {
33   int version;
34 
35   // Input: current clip rect in surface coordinates. Reflects the current state
36   // of the OpenGL scissor rect. Both the OpenGL scissor rect and viewport are
37   // set by the caller of the draw function and updated during View animations.
38   int clip_left;
39   int clip_top;
40   int clip_right;
41   int clip_bottom;
42 
43   // Input: current width/height of destination surface.
44   int width;
45   int height;
46 
47   // Used to be is_layer.
48   bool deprecated_0;
49 
50   // Input: current transformation matrix in surface pixels.
51   // Uses the column-based OpenGL matrix format.
52   float transform[16];
53 
54   // Input: Color space parameters.
55   float transfer_function_g;
56   float transfer_function_a;
57   float transfer_function_b;
58   float transfer_function_c;
59   float transfer_function_d;
60   float transfer_function_e;
61   float transfer_function_f;
62   float color_space_toXYZD50[9];
63 };
64 
65 struct AwDrawFn_InitVkParams {
66   int version;
67   VkInstance instance;
68   VkPhysicalDevice physical_device;
69   VkDevice device;
70   VkQueue queue;
71   uint32_t graphics_queue_index;
72   uint32_t api_version;
73   const char* const* enabled_instance_extension_names;
74   uint32_t enabled_instance_extension_names_length;
75   const char* const* enabled_device_extension_names;
76   uint32_t enabled_device_extension_names_length;
77   // Only one of device_features and device_features_2 should be non-null.
78   // If both are null then no features are enabled.
79   VkPhysicalDeviceFeatures* device_features;
80   VkPhysicalDeviceFeatures2* device_features_2;
81 };
82 
83 struct AwDrawFn_DrawVkParams {
84   int version;
85 
86   // Input: current width/height of destination surface.
87   int width;
88   int height;
89 
90   bool deprecated_0;
91 
92   // Input: current transform matrix
93   float transform[16];
94 
95   // Input WebView should do its main compositing draws into this. It cannot do
96   // anything that would require stopping the render pass.
97   VkCommandBuffer secondary_command_buffer;
98 
99   // Input: The main color attachment index where secondary_command_buffer will
100   // eventually be submitted.
101   uint32_t color_attachment_index;
102 
103   // Input: A render pass which will be compatible to the one which the
104   // secondary_command_buffer will be submitted into.
105   VkRenderPass compatible_render_pass;
106 
107   // Input: Format of the destination surface.
108   VkFormat format;
109 
110   // Input: Color space parameters.
111   float transfer_function_g;
112   float transfer_function_a;
113   float transfer_function_b;
114   float transfer_function_c;
115   float transfer_function_d;
116   float transfer_function_e;
117   float transfer_function_f;
118   float color_space_toXYZD50[9];
119 
120   // Input: current clip rect
121   int clip_left;
122   int clip_top;
123   int clip_right;
124   int clip_bottom;
125 };
126 
127 struct AwDrawFn_PostDrawVkParams {
128   int version;
129 };
130 
131 // Called on render thread while UI thread is blocked. Called for both GL and
132 // VK.
133 typedef void AwDrawFn_OnSync(int functor,
134                              void* data,
135                              AwDrawFn_OnSyncParams* params);
136 
137 // Called on render thread when either the context is destroyed _or_ when the
138 // functor's last reference goes away. Will always be called with an active
139 // context. Called for both GL and VK.
140 typedef void AwDrawFn_OnContextDestroyed(int functor, void* data);
141 
142 // Called on render thread when the last reference to the handle goes away and
143 // the handle is considered irrevocably destroyed. Will always be preceded by
144 // a call to OnContextDestroyed if this functor had ever been drawn. Called for
145 // both GL and VK.
146 typedef void AwDrawFn_OnDestroyed(int functor, void* data);
147 
148 // Only called for GL.
149 typedef void AwDrawFn_DrawGL(int functor,
150                              void* data,
151                              AwDrawFn_DrawGLParams* params);
152 
153 // Initialize vulkan state. Needs to be called again after any
154 // OnContextDestroyed. Only called for Vulkan.
155 typedef void AwDrawFn_InitVk(int functor,
156                              void* data,
157                              AwDrawFn_InitVkParams* params);
158 
159 // Only called for Vulkan.
160 typedef void AwDrawFn_DrawVk(int functor,
161                              void* data,
162                              AwDrawFn_DrawVkParams* params);
163 
164 // Only called for Vulkan.
165 typedef void AwDrawFn_PostDrawVk(int functor,
166                                  void* data,
167                                  AwDrawFn_PostDrawVkParams* params);
168 
169 struct AwDrawFnFunctorCallbacks {
170   // No version here since this is passed from chromium to android.
171   AwDrawFn_OnSync* on_sync;
172   AwDrawFn_OnContextDestroyed* on_context_destroyed;
173   AwDrawFn_OnDestroyed* on_destroyed;
174   AwDrawFn_DrawGL* draw_gl;
175   AwDrawFn_InitVk* init_vk;
176   AwDrawFn_DrawVk* draw_vk;
177   AwDrawFn_PostDrawVk* post_draw_vk;
178 };
179 
180 enum AwDrawFnRenderMode {
181   AW_DRAW_FN_RENDER_MODE_OPENGL_ES = 0,
182   AW_DRAW_FN_RENDER_MODE_VULKAN = 1,
183 };
184 
185 // Get the render mode. Result is static for the process.
186 typedef AwDrawFnRenderMode AwDrawFn_QueryRenderMode(void);
187 
188 // Create a functor. |functor_callbacks| should be valid until OnDestroyed.
189 typedef int AwDrawFn_CreateFunctor(void* data,
190                                    AwDrawFnFunctorCallbacks* functor_callbacks);
191 
192 // May be called on any thread to signal that the functor should be destroyed.
193 // The functor will receive an onDestroyed when the last usage of it is
194 // released, and it should be considered alive & active until that point.
195 typedef void AwDrawFn_ReleaseFunctor(int functor);
196 
197 struct AwDrawFnFunctionTable {
198   int version;
199   AwDrawFn_QueryRenderMode* query_render_mode;
200   AwDrawFn_CreateFunctor* create_functor;
201   AwDrawFn_ReleaseFunctor* release_functor;
202 };
203 
204 #ifdef __cplusplus
205 }  // extern "C"
206 #endif
207 
208 #endif  // ANDROID_WEBVIEW_PUBLIC_BROWSER_DRAW_FN_H_
209