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