blob: 1b9e53b21adb5e1e914593fd3f3763b32c8f6f6c [file] [log] [blame]
Chris Blume41423392018-11-06 11:47:03 -08001/*
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
Bo Liu7b8c1eb2019-01-08 20:17:55 -080020#include "renderthread/VulkanManager.h"
21#include "renderthread/RenderThread.h"
Stan Iliev27b119b2019-03-22 14:42:36 -040022#include <SkAndroidFrameworkUtils.h>
Chris Blume41423392018-11-06 11:47:03 -080023#include <GrBackendDrawableInfo.h>
John Reck283bb462018-12-13 16:40:14 -080024#include <SkImage.h>
Chris Blume41423392018-11-06 11:47:03 -080025#include <utils/Color.h>
26#include <utils/Trace.h>
27#include <utils/TraceUtils.h>
Chris Blume41423392018-11-06 11:47:03 -080028#include <vk/GrVkTypes.h>
John Reck283bb462018-12-13 16:40:14 -080029#include <thread>
30#include "thread/ThreadBase.h"
31#include "utils/TimeUtils.h"
Chris Blume41423392018-11-06 11:47:03 -080032
33namespace android {
34namespace uirenderer {
35namespace skiapipeline {
36
Bo Liu7b8c1eb2019-01-08 20:17:55 -080037VkFunctorDrawHandler::VkFunctorDrawHandler(sp<WebViewFunctor::Handle> functor_handle,
38 const SkMatrix& matrix, const SkIRect& clip,
39 const SkImageInfo& image_info)
40 : INHERITED()
41 , mFunctorHandle(functor_handle)
42 , mMatrix(matrix)
43 , mClip(clip)
44 , mImageInfo(image_info) {}
Chris Blume41423392018-11-06 11:47:03 -080045
46VkFunctorDrawHandler::~VkFunctorDrawHandler() {
Bo Liud25d1342019-02-04 14:55:02 -080047 if (mDrawn) {
48 mFunctorHandle->postDrawVk();
49 }
Chris Blume41423392018-11-06 11:47:03 -080050}
51
52void VkFunctorDrawHandler::draw(const GrBackendDrawableInfo& info) {
53 ATRACE_CALL();
Bo Liu7b8c1eb2019-01-08 20:17:55 -080054 if (!renderthread::RenderThread::isCurrent())
55 LOG_ALWAYS_FATAL("VkFunctorDrawHandler::draw not called on render thread");
Chris Blume41423392018-11-06 11:47:03 -080056
57 GrVkDrawableInfo vulkan_info;
58 if (!info.getVkDrawableInfo(&vulkan_info)) {
59 return;
60 }
Bo Liu7b8c1eb2019-01-08 20:17:55 -080061 renderthread::VulkanManager& vk_manager =
62 renderthread::RenderThread::getInstance().vulkanManager();
63 mFunctorHandle->initVk(vk_manager.getVkFunctorInitParams());
Chris Blume41423392018-11-06 11:47:03 -080064
Bo Liu7b8c1eb2019-01-08 20:17:55 -080065 SkMatrix44 mat4(mMatrix);
66 VkFunctorDrawParams params{
67 .width = mImageInfo.width(),
68 .height = mImageInfo.height(),
Bo Liu7b8c1eb2019-01-08 20:17:55 -080069 .color_space_ptr = mImageInfo.colorSpace(),
70 .clip_left = mClip.fLeft,
71 .clip_top = mClip.fTop,
72 .clip_right = mClip.fRight,
73 .clip_bottom = mClip.fBottom,
74 };
75 mat4.asColMajorf(&params.transform[0]);
76 params.secondary_command_buffer = vulkan_info.fSecondaryCommandBuffer;
77 params.color_attachment_index = vulkan_info.fColorAttachmentIndex;
78 params.compatible_render_pass = vulkan_info.fCompatibleRenderPass;
79 params.format = vulkan_info.fFormat;
Chris Blume41423392018-11-06 11:47:03 -080080
Bo Liu7b8c1eb2019-01-08 20:17:55 -080081 mFunctorHandle->drawVk(params);
Bo Liud25d1342019-02-04 14:55:02 -080082 mDrawn = true;
Bo Liu7b8c1eb2019-01-08 20:17:55 -080083
84 vulkan_info.fDrawBounds->offset.x = mClip.fLeft;
85 vulkan_info.fDrawBounds->offset.y = mClip.fTop;
86 vulkan_info.fDrawBounds->extent.width = mClip.fRight - mClip.fLeft;
87 vulkan_info.fDrawBounds->extent.height = mClip.fBottom - mClip.fTop;
Chris Blume41423392018-11-06 11:47:03 -080088}
89
John Reck283bb462018-12-13 16:40:14 -080090VkFunctorDrawable::~VkFunctorDrawable() {
Chris Blume41423392018-11-06 11:47:03 -080091}
92
Stan Iliev27b119b2019-03-22 14:42:36 -040093void 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->getGrContext() == 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(
110 gpuCanvas == canvas,
111 "VkFunctorDrawable::onDraw() should not be called with a GPU canvas!");
112
113 // This will invoke onSnapGpuDrawHandler and regular draw flow.
114 gpuCanvas->drawDrawable(this);
115 }
Chris Blume41423392018-11-06 11:47:03 -0800116}
117
118std::unique_ptr<FunctorDrawable::GpuDrawHandler> VkFunctorDrawable::onSnapGpuDrawHandler(
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800119 GrBackendApi backendApi, const SkMatrix& matrix, const SkIRect& clip,
120 const SkImageInfo& image_info) {
Chris Blume41423392018-11-06 11:47:03 -0800121 if (backendApi != GrBackendApi::kVulkan) {
122 return nullptr;
123 }
John Reck283bb462018-12-13 16:40:14 -0800124 std::unique_ptr<VkFunctorDrawHandler> draw;
125 if (mAnyFunctor.index() == 0) {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800126 return std::make_unique<VkFunctorDrawHandler>(std::get<0>(mAnyFunctor).handle, matrix, clip,
127 image_info);
John Reck283bb462018-12-13 16:40:14 -0800128 } else {
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800129 LOG_ALWAYS_FATAL("Not implemented");
John Reck283bb462018-12-13 16:40:14 -0800130 }
Chris Blume41423392018-11-06 11:47:03 -0800131}
132
133} // namespace skiapipeline
134} // namespace uirenderer
135} // namespace android