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 | |
John Reck | b48d4d1 | 2021-10-01 16:30:11 -0400 | [diff] [blame^] | 35 | static constexpr bool sEnableExtraCropInset = true; |
| 36 | |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | |
John Reck | 4464e46 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 40 | // Deleter for an AHardwareBuffer, to be passed to an std::unique_ptr. |
| 41 | struct AHardwareBuffer_deleter { |
| 42 | void operator()(AHardwareBuffer* ahb) const { AHardwareBuffer_release(ahb); } |
| 43 | }; |
| 44 | |
| 45 | using UniqueAHardwareBuffer = std::unique_ptr<AHardwareBuffer, AHardwareBuffer_deleter>; |
| 46 | |
| 47 | #define ARECT_ARGS(r) float((r).left), float((r).top), float((r).right), float((r).bottom) |
| 48 | |
| 49 | CopyResult Readback::copySurfaceInto(ANativeWindow* window, const Rect& inSrcRect, |
| 50 | SkBitmap* bitmap) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 51 | ATRACE_CALL(); |
| 52 | // Setup the source |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 53 | AHardwareBuffer* rawSourceBuffer; |
| 54 | int rawSourceFence; |
John Reck | 4464e46 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 55 | ARect cropRect; |
| 56 | uint32_t windowTransform; |
| 57 | status_t err = ANativeWindow_getLastQueuedBuffer2(window, &rawSourceBuffer, &rawSourceFence, |
| 58 | &cropRect, &windowTransform); |
| 59 | base::unique_fd sourceFence(rawSourceFence); |
| 60 | // Really this shouldn't ever happen, but better safe than sorry. |
| 61 | if (err == UNKNOWN_TRANSACTION) { |
| 62 | ALOGW("Readback failed to ANativeWindow_getLastQueuedBuffer2 - who are we talking to?"); |
| 63 | return copySurfaceIntoLegacy(window, inSrcRect, bitmap); |
| 64 | } |
| 65 | ALOGV("Using new path, cropRect=" RECT_STRING ", transform=%x", ARECT_ARGS(cropRect), |
| 66 | windowTransform); |
| 67 | |
| 68 | if (err != NO_ERROR) { |
| 69 | ALOGW("Failed to get last queued buffer, error = %d", err); |
| 70 | return CopyResult::UnknownError; |
| 71 | } |
| 72 | if (rawSourceBuffer == nullptr) { |
| 73 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
| 74 | return CopyResult::SourceEmpty; |
| 75 | } |
John Reck | b48d4d1 | 2021-10-01 16:30:11 -0400 | [diff] [blame^] | 76 | |
| 77 | if (sEnableExtraCropInset && |
| 78 | (cropRect.right - cropRect.left != bitmap->width() || |
| 79 | cropRect.bottom - cropRect.top != bitmap->height())) { |
| 80 | /* |
| 81 | * When we need use filtering, we should also make border shrink here like gui. |
| 82 | * But we could not check format for YUV or RGB here... Just use 1 pix. |
| 83 | */ |
| 84 | cropRect.left += 0.5f; |
| 85 | cropRect.top += 0.5f; |
| 86 | cropRect.right -= 0.5f; |
| 87 | cropRect.bottom -= 0.5f; |
| 88 | } |
| 89 | |
John Reck | 4464e46 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 90 | UniqueAHardwareBuffer sourceBuffer{rawSourceBuffer}; |
| 91 | AHardwareBuffer_Desc description; |
| 92 | AHardwareBuffer_describe(sourceBuffer.get(), &description); |
| 93 | if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) { |
| 94 | ALOGW("Surface is protected, unable to copy from it"); |
| 95 | return CopyResult::SourceInvalid; |
| 96 | } |
| 97 | |
| 98 | if (sourceFence != -1 && sync_wait(sourceFence.get(), 500 /* ms */) != NO_ERROR) { |
| 99 | ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt"); |
| 100 | return CopyResult::Timeout; |
| 101 | } |
| 102 | |
| 103 | sk_sp<SkColorSpace> colorSpace = DataSpaceToColorSpace( |
| 104 | static_cast<android_dataspace>(ANativeWindow_getBuffersDataSpace(window))); |
| 105 | sk_sp<SkImage> image = |
| 106 | SkImage::MakeFromAHardwareBuffer(sourceBuffer.get(), kPremul_SkAlphaType, colorSpace); |
| 107 | |
| 108 | if (!image.get()) { |
| 109 | return CopyResult::UnknownError; |
| 110 | } |
| 111 | |
| 112 | sk_sp<GrContext> grContext = mRenderThread.requireGrContext(); |
| 113 | |
| 114 | SkRect srcRect = inSrcRect.toSkRect(); |
| 115 | |
| 116 | SkRect imageSrcRect = |
| 117 | SkRect::MakeLTRB(cropRect.left, cropRect.top, cropRect.right, cropRect.bottom); |
| 118 | if (imageSrcRect.isEmpty()) { |
| 119 | imageSrcRect = SkRect::MakeIWH(description.width, description.height); |
| 120 | } |
| 121 | ALOGV("imageSrcRect = " RECT_STRING, SK_RECT_ARGS(imageSrcRect)); |
| 122 | |
| 123 | // Represents the "logical" width/height of the texture. That is, the dimensions of the buffer |
| 124 | // after respecting crop & rotate. flipV/flipH still result in the same width & height |
| 125 | // so we can ignore those for this. |
| 126 | const SkRect textureRect = |
| 127 | (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 128 | ? SkRect::MakeIWH(imageSrcRect.height(), imageSrcRect.width()) |
| 129 | : SkRect::MakeIWH(imageSrcRect.width(), imageSrcRect.height()); |
| 130 | |
| 131 | if (srcRect.isEmpty()) { |
| 132 | srcRect = textureRect; |
| 133 | } else { |
| 134 | ALOGV("intersecting " RECT_STRING " with " RECT_STRING, SK_RECT_ARGS(srcRect), |
| 135 | SK_RECT_ARGS(textureRect)); |
| 136 | if (!srcRect.intersect(textureRect)) { |
| 137 | return CopyResult::UnknownError; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | sk_sp<SkSurface> tmpSurface = |
| 142 | SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
| 143 | bitmap->info(), 0, kTopLeft_GrSurfaceOrigin, nullptr); |
| 144 | |
| 145 | // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we |
| 146 | // attempt to do the intermediate rendering step in 8888 |
| 147 | if (!tmpSurface.get()) { |
| 148 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 149 | tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
| 150 | tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr); |
| 151 | if (!tmpSurface.get()) { |
| 152 | ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap"); |
| 153 | return CopyResult::UnknownError; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * The grand ordering of events. |
| 159 | * First we apply the buffer's crop, done by using a srcRect of the crop with a dstRect of the |
| 160 | * same width/height as the srcRect but with a 0x0 origin |
| 161 | * |
| 162 | * Second we apply the window transform via a Canvas matrix. Ordering for that is as follows: |
| 163 | * 1) FLIP_H |
| 164 | * 2) FLIP_V |
| 165 | * 3) ROT_90 |
| 166 | * as per GLConsumer::computeTransformMatrix |
| 167 | * |
| 168 | * Third we apply the user's supplied cropping & scale to the output by doing a RectToRect |
| 169 | * matrix transform from srcRect to {0,0, bitmapWidth, bitmapHeight} |
| 170 | * |
| 171 | * Finally we're done messing with this bloody thing for hopefully the last time. |
| 172 | * |
| 173 | * That's a lie since... |
| 174 | * TODO: Do all this same stuff for TextureView as it's strictly more correct & easier |
| 175 | * to rationalize. And we can fix the 1-px crop bug. |
| 176 | */ |
| 177 | |
| 178 | SkMatrix m; |
| 179 | const SkRect imageDstRect = SkRect::MakeIWH(imageSrcRect.width(), imageSrcRect.height()); |
| 180 | const float px = imageDstRect.centerX(); |
| 181 | const float py = imageDstRect.centerY(); |
| 182 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 183 | m.postScale(-1.f, 1.f, px, py); |
| 184 | } |
| 185 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 186 | m.postScale(1.f, -1.f, px, py); |
| 187 | } |
| 188 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 189 | m.postRotate(90, 0, 0); |
| 190 | m.postTranslate(imageDstRect.height(), 0); |
| 191 | } |
| 192 | |
| 193 | ALOGV("Mapping from " RECT_STRING " to " RECT_STRING, SK_RECT_ARGS(srcRect), |
| 194 | SK_RECT_ARGS(SkRect::MakeWH(bitmap->width(), bitmap->height()))); |
| 195 | m.postConcat(SkMatrix::MakeRectToRect(srcRect, |
| 196 | SkRect::MakeWH(bitmap->width(), bitmap->height()), |
| 197 | SkMatrix::kFill_ScaleToFit)); |
| 198 | |
| 199 | SkCanvas* canvas = tmpSurface->getCanvas(); |
| 200 | canvas->save(); |
| 201 | canvas->concat(m); |
| 202 | SkPaint paint; |
| 203 | paint.setAlpha(255); |
| 204 | paint.setBlendMode(SkBlendMode::kSrc); |
| 205 | if (srcRect.width() != bitmap->width() || srcRect.height() != bitmap->height()) { |
| 206 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 207 | } |
John Reck | f134470 | 2021-07-14 14:24:51 -0400 | [diff] [blame] | 208 | const bool hasBufferCrop = cropRect.left < cropRect.right && cropRect.top < cropRect.bottom; |
| 209 | auto constraint = |
| 210 | hasBufferCrop ? SkCanvas::kStrict_SrcRectConstraint : SkCanvas::kFast_SrcRectConstraint; |
| 211 | canvas->drawImageRect(image, imageSrcRect, imageDstRect, &paint, constraint); |
John Reck | 4464e46 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 212 | canvas->restore(); |
| 213 | |
| 214 | if (!tmpSurface->readPixels(*bitmap, 0, 0)) { |
| 215 | // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into |
| 216 | // 8888 and then convert that into the destination format before giving up. |
| 217 | SkBitmap tmpBitmap; |
| 218 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 219 | if (bitmap->info().colorType() == SkColorType::kN32_SkColorType || |
| 220 | !tmpBitmap.tryAllocPixels(tmpInfo) || !tmpSurface->readPixels(tmpBitmap, 0, 0) || |
| 221 | !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 222 | ALOGW("Unable to convert content into the provided bitmap"); |
| 223 | return CopyResult::UnknownError; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | bitmap->notifyPixelsChanged(); |
| 228 | |
| 229 | return CopyResult::Success; |
| 230 | } |
| 231 | |
| 232 | CopyResult Readback::copySurfaceIntoLegacy(ANativeWindow* window, const Rect& srcRect, |
| 233 | SkBitmap* bitmap) { |
| 234 | // Setup the source |
| 235 | AHardwareBuffer* rawSourceBuffer; |
| 236 | int rawSourceFence; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 237 | Matrix4 texTransform; |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 238 | status_t err = ANativeWindow_getLastQueuedBuffer(window, &rawSourceBuffer, &rawSourceFence, |
| 239 | texTransform.data); |
| 240 | base::unique_fd sourceFence(rawSourceFence); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 241 | texTransform.invalidateType(); |
| 242 | if (err != NO_ERROR) { |
| 243 | ALOGW("Failed to get last queued buffer, error = %d", err); |
| 244 | return CopyResult::UnknownError; |
| 245 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 246 | if (rawSourceBuffer == nullptr) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 247 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
| 248 | return CopyResult::SourceEmpty; |
| 249 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 250 | |
| 251 | std::unique_ptr<AHardwareBuffer, decltype(&AHardwareBuffer_release)> sourceBuffer( |
| 252 | rawSourceBuffer, AHardwareBuffer_release); |
| 253 | AHardwareBuffer_Desc description; |
| 254 | AHardwareBuffer_describe(sourceBuffer.get(), &description); |
| 255 | if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 256 | ALOGW("Surface is protected, unable to copy from it"); |
| 257 | return CopyResult::SourceInvalid; |
| 258 | } |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 259 | |
| 260 | if (sourceFence != -1 && sync_wait(sourceFence.get(), 500 /* ms */) != NO_ERROR) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 261 | ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt"); |
| 262 | return CopyResult::Timeout; |
| 263 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 264 | |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 265 | sk_sp<SkColorSpace> colorSpace = DataSpaceToColorSpace( |
| 266 | static_cast<android_dataspace>(ANativeWindow_getBuffersDataSpace(window))); |
| 267 | sk_sp<SkImage> image = |
| 268 | SkImage::MakeFromAHardwareBuffer(sourceBuffer.get(), kPremul_SkAlphaType, colorSpace); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 269 | return copyImageInto(image, texTransform, srcRect, bitmap); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) { |
| 273 | LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware()); |
| 274 | |
| 275 | Rect srcRect; |
| 276 | Matrix4 transform; |
| 277 | transform.loadScale(1, -1, 1); |
| 278 | transform.translate(0, -1); |
| 279 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 280 | return copyImageInto(hwBitmap->makeImage(), transform, srcRect, bitmap); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 284 | ATRACE_CALL(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 285 | if (!mRenderThread.getGrContext()) { |
| 286 | return CopyResult::UnknownError; |
| 287 | } |
| 288 | |
| 289 | // acquire most recent buffer for drawing |
| 290 | deferredLayer->updateTexImage(); |
| 291 | deferredLayer->apply(); |
| 292 | const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height()); |
| 293 | CopyResult copyResult = CopyResult::UnknownError; |
| 294 | Layer* layer = deferredLayer->backingLayer(); |
| 295 | if (layer) { |
| 296 | if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) { |
| 297 | copyResult = CopyResult::Success; |
| 298 | } |
| 299 | } |
| 300 | return copyResult; |
| 301 | } |
| 302 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 303 | CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, Matrix4& texTransform, |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 304 | const Rect& srcRect, SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 305 | ATRACE_CALL(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 306 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 307 | mRenderThread.requireGlContext(); |
| 308 | } else { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 309 | mRenderThread.requireVkContext(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 310 | } |
| 311 | if (!image.get()) { |
| 312 | return CopyResult::UnknownError; |
| 313 | } |
| 314 | int imgWidth = image->width(); |
| 315 | int imgHeight = image->height(); |
| 316 | sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext()); |
| 317 | |
| 318 | if (bitmap->colorType() == kRGBA_F16_SkColorType && |
| 319 | !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) { |
| 320 | ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported"); |
| 321 | return CopyResult::DestinationInvalid; |
| 322 | } |
| 323 | |
| 324 | CopyResult copyResult = CopyResult::UnknownError; |
| 325 | |
| 326 | int displayedWidth = imgWidth, displayedHeight = imgHeight; |
| 327 | // If this is a 90 or 270 degree rotation we need to swap width/height to get the device |
| 328 | // size. |
| 329 | if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) { |
| 330 | std::swap(displayedWidth, displayedHeight); |
| 331 | } |
| 332 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
| 333 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 334 | if (skiaSrcRect.isEmpty()) { |
| 335 | skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight); |
| 336 | } |
| 337 | bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight)); |
| 338 | if (!srcNotEmpty) { |
| 339 | return copyResult; |
| 340 | } |
| 341 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 342 | Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 343 | layer.setSize(displayedWidth, displayedHeight); |
| 344 | texTransform.copyTo(layer.getTexTransform()); |
| 345 | layer.setImage(image); |
Kazuhiro Inaba | a74d637 | 2020-03-11 00:01:53 +0900 | [diff] [blame] | 346 | // Scaling filter is not explicitly set here, because it is done inside copyLayerInfo |
| 347 | // 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] | 348 | if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) { |
| 349 | copyResult = CopyResult::Success; |
| 350 | } |
| 351 | |
| 352 | return copyResult; |
| 353 | } |
| 354 | |
| 355 | bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect, |
| 356 | SkBitmap* bitmap) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 357 | /* This intermediate surface is present to work around a bug in SwiftShader that |
| 358 | * prevents us from reading the contents of the layer's texture directly. The |
| 359 | * workaround involves first rendering that texture into an intermediate buffer and |
| 360 | * then reading from the intermediate buffer into the bitmap. |
| 361 | * Another reason to render in an offscreen buffer is to scale and to avoid an issue b/62262733 |
| 362 | * with reading incorrect data from EGLImage backed SkImage (likely a driver bug). |
| 363 | */ |
| 364 | sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), |
Derek Sollenberger | f4795f5 | 2019-02-06 13:54:12 -0500 | [diff] [blame] | 365 | SkBudgeted::kYes, bitmap->info(), 0, |
| 366 | kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 367 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 368 | // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we |
| 369 | // attempt to do the intermediate rendering step in 8888 |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 370 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 371 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 372 | tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes, |
Derek Sollenberger | f4795f5 | 2019-02-06 13:54:12 -0500 | [diff] [blame] | 373 | tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 374 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 375 | 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] | 376 | return false; |
| 377 | } |
| 378 | } |
| 379 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 380 | if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(), |
| 381 | tmpSurface->getCanvas(), layer, srcRect, dstRect, |
| 382 | false)) { |
| 383 | ALOGW("Unable to draw content from GPU into the provided bitmap"); |
| 384 | return false; |
| 385 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 386 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 387 | if (!tmpSurface->readPixels(*bitmap, 0, 0)) { |
| 388 | // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into |
| 389 | // 8888 and then convert that into the destination format before giving up. |
| 390 | SkBitmap tmpBitmap; |
| 391 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 392 | if (bitmap->info().colorType() == SkColorType::kN32_SkColorType || |
| 393 | !tmpBitmap.tryAllocPixels(tmpInfo) || |
| 394 | !tmpSurface->readPixels(tmpBitmap, 0, 0) || |
| 395 | !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(), |
| 396 | bitmap->rowBytes(), 0, 0)) { |
| 397 | ALOGW("Unable to convert content into the provided bitmap"); |
| 398 | return false; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 402 | bitmap->notifyPixelsChanged(); |
| 403 | return true; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | } /* namespace uirenderer */ |
| 407 | } /* namespace android */ |