• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 namespace android {
37 namespace uirenderer {
38 namespace skiapipeline {
39 
40 static renderthread::EglManager sEglManager;
41 
42 // ScopedDrawRequest makes sure a GL thread is started and EGL context is initialized on it.
43 class ScopedDrawRequest {
44 public:
ScopedDrawRequest()45     ScopedDrawRequest() { beginDraw(); }
46 
47 private:
beginDraw()48     void beginDraw() {
49         if (!sEglManager.hasEglContext()) {
50             sEglManager.initialize();
51         }
52     }
53 };
54 
vkInvokeFunctor(Functor * functor)55 void VkInteropFunctorDrawable::vkInvokeFunctor(Functor* functor) {
56     ScopedDrawRequest _drawRequest{};
57     EGLDisplay display = sEglManager.eglDisplay();
58     DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
59     if (display != EGL_NO_DISPLAY) {
60         mode = DrawGlInfo::kModeProcess;
61     }
62     (*functor)(mode, nullptr);
63 }
64 
65 #define FENCE_TIMEOUT 2000000000
66 
onDraw(SkCanvas * canvas)67 void VkInteropFunctorDrawable::onDraw(SkCanvas* canvas) {
68     ATRACE_CALL();
69 
70     if (canvas->recordingContext() == nullptr) {
71         ALOGD("Attempting to draw VkInteropFunctor into an unsupported surface");
72         return;
73     }
74 
75     ScopedDrawRequest _drawRequest{};
76 
77     SkImageInfo surfaceInfo = canvas->imageInfo();
78 
79     if (mFrameBuffer == nullptr || mFBInfo != surfaceInfo) {
80         // Buffer will be used as an OpenGL ES render target.
81         AHardwareBuffer_Desc desc = {
82                 .width = static_cast<uint32_t>(surfaceInfo.width()),
83                 .height = static_cast<uint32_t>(surfaceInfo.height()),
84                 .layers = 1,
85                 .format = ColorTypeToBufferFormat(surfaceInfo.colorType()),
86                 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER |
87                          AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
88                          AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
89                          AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
90         };
91 
92         mFrameBuffer = allocateAHardwareBuffer(desc);
93 
94         if (!mFrameBuffer) {
95             ALOGW("VkInteropFunctorDrawable::onDraw() failed in AHardwareBuffer_allocate()");
96             return;
97         }
98 
99         mFBInfo = surfaceInfo;
100     }
101 
102     // TODO: Synchronization is needed on mFrameBuffer to guarantee that the previous Vulkan
103     // TODO: draw command has completed.
104     // TODO: A simple but inefficient way is to flush and issue a QueueWaitIdle call. See
105     // TODO: GrVkGpu::destroyResources() for example.
106     {
107         ATRACE_FORMAT("WebViewDraw_%dx%d", mFBInfo.width(), mFBInfo.height());
108         EGLDisplay display = sEglManager.eglDisplay();
109         LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
110                             uirenderer::renderthread::EglManager::eglErrorString());
111         // We use an EGLImage to access the content of the GraphicBuffer
112         // The EGL image is later bound to a 2D texture
113         const EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(mFrameBuffer.get());
114         AutoEglImage autoImage(display, clientBuffer);
115         if (autoImage.image == EGL_NO_IMAGE_KHR) {
116             ALOGW("Could not create EGL image, err =%s",
117                   uirenderer::renderthread::EglManager::eglErrorString());
118             return;
119         }
120 
121         AutoSkiaGlTexture glTexture;
122         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, autoImage.image);
123         GL_CHECKPOINT(MODERATE);
124 
125         glBindTexture(GL_TEXTURE_2D, 0);
126 
127         DrawGlInfo info;
128         SkM44 mat4(canvas->getLocalToDevice());
129         SkIRect clipBounds = canvas->getDeviceClipBounds();
130 
131         info.clipLeft = clipBounds.fLeft;
132         info.clipTop = clipBounds.fTop;
133         info.clipRight = clipBounds.fRight;
134         info.clipBottom = clipBounds.fBottom;
135         info.isLayer = true;
136         info.width = mFBInfo.width();
137         info.height = mFBInfo.height();
138         mat4.getColMajor(&info.transform[0]);
139         info.color_space_ptr = canvas->imageInfo().colorSpace();
140 
141         glViewport(0, 0, info.width, info.height);
142 
143         AutoGLFramebuffer glFb;
144         // Bind texture to the frame buffer.
145         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
146                                glTexture.mTexture, 0);
147         if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
148             ALOGE("Failed framebuffer check for created target buffer: %s",
149                   GLUtils::getGLFramebufferError());
150             return;
151         }
152 
153         glDisable(GL_STENCIL_TEST);
154         glDisable(GL_SCISSOR_TEST);
155         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
156         glClear(GL_COLOR_BUFFER_BIT);
157 
158         mWebViewHandle->drawGl(info);
159 
160         EGLSyncKHR glDrawFinishedFence =
161                 eglCreateSyncKHR(eglGetCurrentDisplay(), EGL_SYNC_FENCE_KHR, NULL);
162         LOG_ALWAYS_FATAL_IF(glDrawFinishedFence == EGL_NO_SYNC_KHR,
163                             "Could not create sync fence %#x", eglGetError());
164         glFlush();
165         // TODO: export EGLSyncKHR in file descr
166         // TODO: import file desc in Vulkan Semaphore
167         // TODO: instead block the GPU: probably by using external Vulkan semaphore.
168         // Block the CPU until the glFlush finish.
169         EGLint waitStatus = eglClientWaitSyncKHR(display, glDrawFinishedFence, 0, FENCE_TIMEOUT);
170         LOG_ALWAYS_FATAL_IF(waitStatus != EGL_CONDITION_SATISFIED_KHR,
171                             "Failed to wait for the fence %#x", eglGetError());
172         eglDestroySyncKHR(display, glDrawFinishedFence);
173     }
174 
175     SkPaint paint;
176     paint.setBlendMode(SkBlendMode::kSrcOver);
177     canvas->save();
178     // The size of the image matches the size of the canvas. We've used the matrix already, while
179     // drawing into the offscreen surface, so we need to reset it here.
180     canvas->resetMatrix();
181 
182     auto functorImage = SkImage::MakeFromAHardwareBuffer(mFrameBuffer.get(), kPremul_SkAlphaType,
183                                                          canvas->imageInfo().refColorSpace(),
184                                                          kBottomLeft_GrSurfaceOrigin);
185     canvas->drawImage(functorImage, 0, 0, SkSamplingOptions(), &paint);
186     canvas->restore();
187 }
188 
syncFunctor(const WebViewSyncData & data) const189 void VkInteropFunctorDrawable::syncFunctor(const WebViewSyncData& data) const {
190     ScopedDrawRequest _drawRequest{};
191     FunctorDrawable::syncFunctor(data);
192 }
193 
194 }  // namespace skiapipeline
195 }  // namespace uirenderer
196 }  // namespace android
197