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