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 "VkFunctorDrawable.h"
18 #include <private/hwui/DrawVkInfo.h>
19 
20 #include <GrBackendDrawableInfo.h>
21 #include <SkAndroidFrameworkUtils.h>
22 #include <SkImage.h>
23 #include <SkM44.h>
24 #include <gui/TraceUtils.h>
25 #include <utils/Color.h>
26 #include <utils/Trace.h>
27 #include <vk/GrVkTypes.h>
28 #include <thread>
29 #include "renderthread/RenderThread.h"
30 #include "renderthread/VulkanManager.h"
31 #include "thread/ThreadBase.h"
32 #include "utils/TimeUtils.h"
33 
34 namespace android {
35 namespace uirenderer {
36 namespace skiapipeline {
37 
VkFunctorDrawHandler(sp<WebViewFunctor::Handle> functor_handle,const SkMatrix & matrix,const SkIRect & clip,const SkImageInfo & image_info)38 VkFunctorDrawHandler::VkFunctorDrawHandler(sp<WebViewFunctor::Handle> functor_handle,
39                                            const SkMatrix& matrix, const SkIRect& clip,
40                                            const SkImageInfo& image_info)
41         : INHERITED()
42         , mFunctorHandle(functor_handle)
43         , mMatrix(matrix)
44         , mClip(clip)
45         , mImageInfo(image_info) {}
46 
~VkFunctorDrawHandler()47 VkFunctorDrawHandler::~VkFunctorDrawHandler() {
48     if (mDrawn) {
49         mFunctorHandle->postDrawVk();
50     }
51 }
52 
draw(const GrBackendDrawableInfo & info)53 void VkFunctorDrawHandler::draw(const GrBackendDrawableInfo& info) {
54     ATRACE_CALL();
55     if (!renderthread::RenderThread::isCurrent())
56         LOG_ALWAYS_FATAL("VkFunctorDrawHandler::draw not called on render thread");
57 
58     GrVkDrawableInfo vulkan_info;
59     if (!info.getVkDrawableInfo(&vulkan_info)) {
60         return;
61     }
62     renderthread::VulkanManager& vk_manager =
63             renderthread::RenderThread::getInstance().vulkanManager();
64     mFunctorHandle->initVk(vk_manager.getVkFunctorInitParams());
65 
66     SkM44 mat4(mMatrix);
67     VkFunctorDrawParams params{
68             .width = mImageInfo.width(),
69             .height = mImageInfo.height(),
70             .color_space_ptr = mImageInfo.colorSpace(),
71             .clip_left = mClip.fLeft,
72             .clip_top = mClip.fTop,
73             .clip_right = mClip.fRight,
74             .clip_bottom = mClip.fBottom,
75     };
76     mat4.getColMajor(&params.transform[0]);
77     params.secondary_command_buffer = vulkan_info.fSecondaryCommandBuffer;
78     params.color_attachment_index = vulkan_info.fColorAttachmentIndex;
79     params.compatible_render_pass = vulkan_info.fCompatibleRenderPass;
80     params.format = vulkan_info.fFormat;
81 
82     mFunctorHandle->drawVk(params);
83     mDrawn = true;
84 
85     vulkan_info.fDrawBounds->offset.x = mClip.fLeft;
86     vulkan_info.fDrawBounds->offset.y = mClip.fTop;
87     vulkan_info.fDrawBounds->extent.width = mClip.fRight - mClip.fLeft;
88     vulkan_info.fDrawBounds->extent.height = mClip.fBottom - mClip.fTop;
89 }
90 
~VkFunctorDrawable()91 VkFunctorDrawable::~VkFunctorDrawable() {}
92 
onDraw(SkCanvas * canvas)93 void VkFunctorDrawable::onDraw(SkCanvas* canvas) {
94     // "canvas" is either SkNWayCanvas created by SkiaPipeline::tryCapture (SKP capture use case) or
95     // AlphaFilterCanvas (used by RenderNodeDrawable to apply alpha in certain cases).
96     // "VkFunctorDrawable::onDraw" is not invoked for the most common case, when drawing in a GPU
97     // canvas.
98 
99     if (canvas->recordingContext() == nullptr) {
100         // We're dumping a picture, render a light-blue rectangle instead
101         SkPaint paint;
102         paint.setColor(0xFF81D4FA);
103         canvas->drawRect(mBounds, paint);
104     } else {
105         // Handle the case when "canvas" is AlphaFilterCanvas. Find the wrapped GPU canvas.
106         SkCanvas* gpuCanvas = SkAndroidFrameworkUtils::getBaseWrappedCanvas(canvas);
107         // Enforce "canvas" must be an AlphaFilterCanvas. For GPU canvas, the call should come from
108         // onSnapGpuDrawHandler.
109         LOG_ALWAYS_FATAL_IF(gpuCanvas == canvas,
110                             "VkFunctorDrawable::onDraw() should not be called with a GPU canvas!");
111 
112         // This will invoke onSnapGpuDrawHandler and regular draw flow.
113         gpuCanvas->drawDrawable(this);
114     }
115 }
116 
onSnapGpuDrawHandler(GrBackendApi backendApi,const SkMatrix & matrix,const SkIRect & clip,const SkImageInfo & image_info)117 std::unique_ptr<FunctorDrawable::GpuDrawHandler> VkFunctorDrawable::onSnapGpuDrawHandler(
118         GrBackendApi backendApi, const SkMatrix& matrix, const SkIRect& clip,
119         const SkImageInfo& image_info) {
120     if (backendApi != GrBackendApi::kVulkan) {
121         return nullptr;
122     }
123     std::unique_ptr<VkFunctorDrawHandler> draw;
124     return std::make_unique<VkFunctorDrawHandler>(mWebViewHandle, matrix, clip, image_info);
125 }
126 
127 }  // namespace skiapipeline
128 }  // namespace uirenderer
129 }  // namespace android
130