Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [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 "Readback.h" |
| 18 | |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 19 | #include <sync/sync.h> |
| 20 | #include <system/window.h> |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 21 | |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 22 | #include "DeferredLayerUpdater.h" |
| 23 | #include "Properties.h" |
| 24 | #include "hwui/Bitmap.h" |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 25 | #include "pipeline/skia/LayerDrawable.h" |
| 26 | #include "renderthread/EglManager.h" |
| 27 | #include "renderthread/VulkanManager.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 28 | #include "utils/Color.h" |
| 29 | #include "utils/MathUtils.h" |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 30 | #include "utils/NdkUtils.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 31 | #include "utils/TraceUtils.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 32 | |
| 33 | using namespace android::uirenderer::renderthread; |
| 34 | |
| 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 38 | CopyResult Readback::copySurfaceInto(ANativeWindow* window, const Rect& srcRect, SkBitmap* bitmap) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 39 | ATRACE_CALL(); |
| 40 | // Setup the source |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 41 | AHardwareBuffer* rawSourceBuffer; |
| 42 | int rawSourceFence; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 43 | Matrix4 texTransform; |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 44 | status_t err = ANativeWindow_getLastQueuedBuffer(window, &rawSourceBuffer, &rawSourceFence, |
| 45 | texTransform.data); |
| 46 | base::unique_fd sourceFence(rawSourceFence); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 47 | texTransform.invalidateType(); |
| 48 | if (err != NO_ERROR) { |
| 49 | ALOGW("Failed to get last queued buffer, error = %d", err); |
| 50 | return CopyResult::UnknownError; |
| 51 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 52 | if (rawSourceBuffer == nullptr) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 53 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
| 54 | return CopyResult::SourceEmpty; |
| 55 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 56 | |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 57 | UniqueAHardwareBuffer sourceBuffer{rawSourceBuffer}; |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 58 | AHardwareBuffer_Desc description; |
| 59 | AHardwareBuffer_describe(sourceBuffer.get(), &description); |
| 60 | if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 61 | ALOGW("Surface is protected, unable to copy from it"); |
| 62 | return CopyResult::SourceInvalid; |
| 63 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 64 | |
| 65 | if (sourceFence != -1 && sync_wait(sourceFence.get(), 500 /* ms */) != NO_ERROR) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 66 | ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt"); |
| 67 | return CopyResult::Timeout; |
| 68 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 69 | |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 70 | sk_sp<SkColorSpace> colorSpace = DataSpaceToColorSpace( |
| 71 | static_cast<android_dataspace>(ANativeWindow_getBuffersDataSpace(window))); |
| 72 | sk_sp<SkImage> image = |
| 73 | SkImage::MakeFromAHardwareBuffer(sourceBuffer.get(), kPremul_SkAlphaType, colorSpace); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 74 | return copyImageInto(image, texTransform, srcRect, bitmap); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) { |
| 78 | LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware()); |
| 79 | |
| 80 | Rect srcRect; |
| 81 | Matrix4 transform; |
| 82 | transform.loadScale(1, -1, 1); |
| 83 | transform.translate(0, -1); |
| 84 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 85 | return copyImageInto(hwBitmap->makeImage(), transform, srcRect, bitmap); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 89 | ATRACE_CALL(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 90 | if (!mRenderThread.getGrContext()) { |
| 91 | return CopyResult::UnknownError; |
| 92 | } |
| 93 | |
| 94 | // acquire most recent buffer for drawing |
| 95 | deferredLayer->updateTexImage(); |
| 96 | deferredLayer->apply(); |
| 97 | const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height()); |
| 98 | CopyResult copyResult = CopyResult::UnknownError; |
| 99 | Layer* layer = deferredLayer->backingLayer(); |
| 100 | if (layer) { |
| 101 | if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) { |
| 102 | copyResult = CopyResult::Success; |
| 103 | } |
| 104 | } |
| 105 | return copyResult; |
| 106 | } |
| 107 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 108 | CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, Matrix4& texTransform, |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 109 | const Rect& srcRect, SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 110 | ATRACE_CALL(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 111 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 112 | mRenderThread.requireGlContext(); |
| 113 | } else { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 114 | mRenderThread.requireVkContext(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 115 | } |
| 116 | if (!image.get()) { |
| 117 | return CopyResult::UnknownError; |
| 118 | } |
| 119 | int imgWidth = image->width(); |
| 120 | int imgHeight = image->height(); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame^] | 121 | sk_sp<GrDirectContext> grContext = sk_ref_sp(mRenderThread.getGrContext()); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 122 | |
| 123 | if (bitmap->colorType() == kRGBA_F16_SkColorType && |
| 124 | !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) { |
| 125 | ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported"); |
| 126 | return CopyResult::DestinationInvalid; |
| 127 | } |
| 128 | |
| 129 | CopyResult copyResult = CopyResult::UnknownError; |
| 130 | |
| 131 | int displayedWidth = imgWidth, displayedHeight = imgHeight; |
| 132 | // If this is a 90 or 270 degree rotation we need to swap width/height to get the device |
| 133 | // size. |
| 134 | if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) { |
| 135 | std::swap(displayedWidth, displayedHeight); |
| 136 | } |
| 137 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
| 138 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 139 | if (skiaSrcRect.isEmpty()) { |
| 140 | skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight); |
| 141 | } |
| 142 | bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight)); |
| 143 | if (!srcNotEmpty) { |
| 144 | return copyResult; |
| 145 | } |
| 146 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 147 | Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 148 | layer.setSize(displayedWidth, displayedHeight); |
| 149 | texTransform.copyTo(layer.getTexTransform()); |
| 150 | layer.setImage(image); |
Kazuhiro Inaba | a74d637 | 2020-03-11 00:01:53 +0900 | [diff] [blame] | 151 | // Scaling filter is not explicitly set here, because it is done inside copyLayerInfo |
| 152 | // after checking the necessity based on the src/dest rect size and the transformation. |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 153 | if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) { |
| 154 | copyResult = CopyResult::Success; |
| 155 | } |
| 156 | |
| 157 | return copyResult; |
| 158 | } |
| 159 | |
| 160 | bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect, |
| 161 | SkBitmap* bitmap) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 162 | /* This intermediate surface is present to work around a bug in SwiftShader that |
| 163 | * prevents us from reading the contents of the layer's texture directly. The |
| 164 | * workaround involves first rendering that texture into an intermediate buffer and |
| 165 | * then reading from the intermediate buffer into the bitmap. |
| 166 | * Another reason to render in an offscreen buffer is to scale and to avoid an issue b/62262733 |
| 167 | * with reading incorrect data from EGLImage backed SkImage (likely a driver bug). |
| 168 | */ |
| 169 | sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), |
Derek Sollenberger | f4795f5 | 2019-02-06 13:54:12 -0500 | [diff] [blame] | 170 | SkBudgeted::kYes, bitmap->info(), 0, |
| 171 | kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 172 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 173 | // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we |
| 174 | // attempt to do the intermediate rendering step in 8888 |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 175 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 176 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 177 | tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
Derek Sollenberger | f4795f5 | 2019-02-06 13:54:12 -0500 | [diff] [blame] | 178 | tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 179 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 180 | ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap"); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 185 | if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(), |
| 186 | tmpSurface->getCanvas(), layer, srcRect, dstRect, |
| 187 | false)) { |
| 188 | ALOGW("Unable to draw content from GPU into the provided bitmap"); |
| 189 | return false; |
| 190 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 191 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 192 | if (!tmpSurface->readPixels(*bitmap, 0, 0)) { |
| 193 | // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into |
| 194 | // 8888 and then convert that into the destination format before giving up. |
| 195 | SkBitmap tmpBitmap; |
| 196 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 197 | if (bitmap->info().colorType() == SkColorType::kN32_SkColorType || |
| 198 | !tmpBitmap.tryAllocPixels(tmpInfo) || |
| 199 | !tmpSurface->readPixels(tmpBitmap, 0, 0) || |
| 200 | !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(), |
| 201 | bitmap->rowBytes(), 0, 0)) { |
| 202 | ALOGW("Unable to convert content into the provided bitmap"); |
| 203 | return false; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 207 | bitmap->notifyPixelsChanged(); |
| 208 | return true; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } /* namespace uirenderer */ |
| 212 | } /* namespace android */ |