1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "VkInteropFunctorDrawable.h"
18
19 #include <EGL/egl.h>
20 #include <EGL/eglext.h>
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <GLES3/gl3.h>
24 #include <gui/TraceUtils.h>
25 #include <private/hwui/DrawGlInfo.h>
26 #include <utils/Color.h>
27 #include <utils/GLUtils.h>
28 #include <utils/Trace.h>
29
30 #include <thread>
31
32 #include "renderthread/EglManager.h"
33 #include "thread/ThreadBase.h"
34 #include "utils/TimeUtils.h"
35 #include "effects/GainmapRenderer.h"
36
37 #include <SkBlendMode.h>
38 #include <SkImage.h>
39 #include <SkImageAndroid.h>
40
41 namespace android {
42 namespace uirenderer {
43 namespace skiapipeline {
44
45 static renderthread::EglManager sEglManager;
46
47 // ScopedDrawRequest makes sure a GL thread is started and EGL context is initialized on it.
48 class ScopedDrawRequest {
49 public:
ScopedDrawRequest()50 ScopedDrawRequest() { beginDraw(); }
51
52 private:
beginDraw()53 void beginDraw() {
54 if (!sEglManager.hasEglContext()) {
55 sEglManager.initialize();
56 }
57 }
58 };
59
vkInvokeFunctor(Functor * functor)60 void VkInteropFunctorDrawable::vkInvokeFunctor(Functor* functor) {
61 ScopedDrawRequest _drawRequest{};
62 EGLDisplay display = sEglManager.eglDisplay();
63 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
64 if (display != EGL_NO_DISPLAY) {
65 mode = DrawGlInfo::kModeProcess;
66 }
67 (*functor)(mode, nullptr);
68 }
69
70 #define FENCE_TIMEOUT 2000000000
71
onDraw(SkCanvas * canvas)72 void VkInteropFunctorDrawable::onDraw(SkCanvas* canvas) {
73 ATRACE_CALL();
74
75 if (canvas->recordingContext() == nullptr) {
76 ALOGD("Attempting to draw VkInteropFunctor into an unsupported surface");
77 return;
78 }
79
80 ScopedDrawRequest _drawRequest{};
81
82 SkImageInfo surfaceInfo = canvas->imageInfo();
83
84 if (mFrameBuffer == nullptr || mFBInfo != surfaceInfo) {
85 // Buffer will be used as an OpenGL ES render target.
86 AHardwareBuffer_Desc desc = {
87 .width = static_cast<uint32_t>(surfaceInfo.width()),
88 .height = static_cast<uint32_t>(surfaceInfo.height()),
89 .layers = 1,
90 .format = ColorTypeToBufferFormat(surfaceInfo.colorType()),
91 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER |
92 AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
93 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
94 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
95 };
96
97 mFrameBuffer = allocateAHardwareBuffer(desc);
98
99 if (!mFrameBuffer) {
100 ALOGW("VkInteropFunctorDrawable::onDraw() failed in AHardwareBuffer_allocate()");
101 return;
102 }
103
104 mFBInfo = surfaceInfo;
105 }
106
107 // TODO: Synchronization is needed on mFrameBuffer to guarantee that the previous Vulkan
108 // TODO: draw command has completed.
109 // TODO: A simple but inefficient way is to flush and issue a QueueWaitIdle call. See
110 // TODO: GrVkGpu::destroyResources() for example.
111 {
112 ATRACE_FORMAT("WebViewDraw_%dx%d", mFBInfo.width(), mFBInfo.height());
113 EGLDisplay display = sEglManager.eglDisplay();
114 LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
115 uirenderer::renderthread::EglManager::eglErrorString());
116 // We use an EGLImage to access the content of the GraphicBuffer
117 // The EGL image is later bound to a 2D texture
118 const EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(mFrameBuffer.get());
119 AutoEglImage autoImage(display, clientBuffer);
120 if (autoImage.image == EGL_NO_IMAGE_KHR) {
121 ALOGW("Could not create EGL image, err =%s",
122 uirenderer::renderthread::EglManager::eglErrorString());
123 return;
124 }
125
126 AutoSkiaGlTexture glTexture;
127 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image);
128 GL_CHECKPOINT(MODERATE);
129
130 glBindTexture(GL_TEXTURE_2D, 0);
131
132 DrawGlInfo info;
133 SkM44 mat4(canvas->getLocalToDevice());
134 SkIRect clipBounds = canvas->getDeviceClipBounds();
135
136 info.clipLeft = clipBounds.fLeft;
137 info.clipTop = clipBounds.fTop;
138 info.clipRight = clipBounds.fRight;
139 info.clipBottom = clipBounds.fBottom;
140 info.isLayer = true;
141 info.width = mFBInfo.width();
142 info.height = mFBInfo.height();
143 mat4.getColMajor(&info.transform[0]);
144 info.color_space_ptr = canvas->imageInfo().colorSpace();
145 info.currentHdrSdrRatio = getTargetHdrSdrRatio(info.color_space_ptr);
146
147 glViewport(0, 0, info.width, info.height);
148
149 AutoGLFramebuffer glFb;
150 // Bind texture to the frame buffer.
151 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
152 glTexture.mTexture, 0);
153 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
154 ALOGE("Failed framebuffer check for created target buffer: %s",
155 GLUtils::getGLFramebufferError());
156 return;
157 }
158
159 glDisable(GL_STENCIL_TEST);
160 glDisable(GL_SCISSOR_TEST);
161 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
162 glClear(GL_COLOR_BUFFER_BIT);
163
164 mWebViewHandle->drawGl(info);
165
166 EGLSyncKHR glDrawFinishedFence =
167 eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL);
168 LOG_ALWAYS_FATAL_IF(glDrawFinishedFence == EGL_NO_SYNC_KHR,
169 "Could not create sync fence %#x", eglGetError());
170 glFlush();
171 // TODO: export EGLSyncKHR in file descr
172 // TODO: import file desc in Vulkan Semaphore
173 // TODO: instead block the GPU: probably by using external Vulkan semaphore.
174 // Block the CPU until the glFlush finish.
175 EGLint waitStatus = eglClientWaitSyncKHR(display, glDrawFinishedFence, 0, FENCE_TIMEOUT);
176 LOG_ALWAYS_FATAL_IF(waitStatus != EGL_CONDITION_SATISFIED_KHR,
177 "Failed to wait for the fence %#x", eglGetError());
178 eglDestroySyncKHR(display, glDrawFinishedFence);
179 }
180
181 SkPaint paint;
182 paint.setBlendMode(SkBlendMode::kSrcOver);
183 canvas->save();
184 // The size of the image matches the size of the canvas. We've used the matrix already, while
185 // drawing into the offscreen surface, so we need to reset it here.
186 canvas->resetMatrix();
187
188 auto functorImage = SkImages::DeferredFromAHardwareBuffer(
189 mFrameBuffer.get(), kPremul_SkAlphaType, canvas->imageInfo().refColorSpace(),
190 kBottomLeft_GrSurfaceOrigin);
191 canvas->drawImage(functorImage, 0, 0, SkSamplingOptions(), &paint);
192 canvas->restore();
193 }
194
syncFunctor(const WebViewSyncData & data) const195 void VkInteropFunctorDrawable::syncFunctor(const WebViewSyncData& data) const {
196 ScopedDrawRequest _drawRequest{};
197 FunctorDrawable::syncFunctor(data);
198 }
199
200 } // namespace skiapipeline
201 } // namespace uirenderer
202 } // namespace android
203