Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 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 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 20 | #include "renderthread/VulkanManager.h" |
| 21 | #include "renderthread/RenderThread.h" |
Stan Iliev | 27b119b | 2019-03-22 14:42:36 -0400 | [diff] [blame] | 22 | #include <SkAndroidFrameworkUtils.h> |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 23 | #include <GrBackendDrawableInfo.h> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 24 | #include <SkImage.h> |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 25 | #include <utils/Color.h> |
| 26 | #include <utils/Trace.h> |
| 27 | #include <utils/TraceUtils.h> |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 28 | #include <vk/GrVkTypes.h> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 29 | #include <thread> |
| 30 | #include "thread/ThreadBase.h" |
| 31 | #include "utils/TimeUtils.h" |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace skiapipeline { |
| 36 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 37 | VkFunctorDrawHandler::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 Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 45 | |
| 46 | VkFunctorDrawHandler::~VkFunctorDrawHandler() { |
Bo Liu | d25d134 | 2019-02-04 14:55:02 -0800 | [diff] [blame] | 47 | if (mDrawn) { |
| 48 | mFunctorHandle->postDrawVk(); |
| 49 | } |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void VkFunctorDrawHandler::draw(const GrBackendDrawableInfo& info) { |
| 53 | ATRACE_CALL(); |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 54 | if (!renderthread::RenderThread::isCurrent()) |
| 55 | LOG_ALWAYS_FATAL("VkFunctorDrawHandler::draw not called on render thread"); |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 56 | |
| 57 | GrVkDrawableInfo vulkan_info; |
| 58 | if (!info.getVkDrawableInfo(&vulkan_info)) { |
| 59 | return; |
| 60 | } |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 61 | renderthread::VulkanManager& vk_manager = |
| 62 | renderthread::RenderThread::getInstance().vulkanManager(); |
| 63 | mFunctorHandle->initVk(vk_manager.getVkFunctorInitParams()); |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 64 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 65 | SkMatrix44 mat4(mMatrix); |
| 66 | VkFunctorDrawParams params{ |
| 67 | .width = mImageInfo.width(), |
| 68 | .height = mImageInfo.height(), |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 69 | .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(¶ms.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 Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 80 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 81 | mFunctorHandle->drawVk(params); |
Bo Liu | d25d134 | 2019-02-04 14:55:02 -0800 | [diff] [blame] | 82 | mDrawn = true; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 83 | |
| 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 Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 88 | } |
| 89 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 90 | VkFunctorDrawable::~VkFunctorDrawable() { |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Stan Iliev | 27b119b | 2019-03-22 14:42:36 -0400 | [diff] [blame] | 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->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 Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::unique_ptr<FunctorDrawable::GpuDrawHandler> VkFunctorDrawable::onSnapGpuDrawHandler( |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 119 | GrBackendApi backendApi, const SkMatrix& matrix, const SkIRect& clip, |
| 120 | const SkImageInfo& image_info) { |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 121 | if (backendApi != GrBackendApi::kVulkan) { |
| 122 | return nullptr; |
| 123 | } |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 124 | std::unique_ptr<VkFunctorDrawHandler> draw; |
| 125 | if (mAnyFunctor.index() == 0) { |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 126 | return std::make_unique<VkFunctorDrawHandler>(std::get<0>(mAnyFunctor).handle, matrix, clip, |
| 127 | image_info); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 128 | } else { |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 129 | LOG_ALWAYS_FATAL("Not implemented"); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 130 | } |
Chris Blume | 4142339 | 2018-11-06 11:47:03 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | } // namespace skiapipeline |
| 134 | } // namespace uirenderer |
| 135 | } // namespace android |