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 | |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 19 | #include <SkBitmap.h> |
| 20 | #include <SkBlendMode.h> |
| 21 | #include <SkCanvas.h> |
| 22 | #include <SkColorSpace.h> |
| 23 | #include <SkImage.h> |
Kevin Lubick | dd8b2ea | 2023-03-17 19:55:58 +0000 | [diff] [blame] | 24 | #include <SkImageAndroid.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 25 | #include <SkImageInfo.h> |
| 26 | #include <SkMatrix.h> |
| 27 | #include <SkPaint.h> |
| 28 | #include <SkRect.h> |
| 29 | #include <SkRefCnt.h> |
| 30 | #include <SkSamplingOptions.h> |
| 31 | #include <SkSurface.h> |
Kevin Lubick | 8099b67 | 2023-01-09 14:48:45 +0000 | [diff] [blame] | 32 | #include "include/gpu/GpuTypes.h" // from Skia |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 33 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 34 | #include <gui/TraceUtils.h> |
| 35 | #include <private/android/AHardwareBufferHelpers.h> |
| 36 | #include <shaders/shaders.h> |
| 37 | #include <sync/sync.h> |
| 38 | #include <system/window.h> |
| 39 | |
| 40 | #include "DeferredLayerUpdater.h" |
| 41 | #include "Properties.h" |
| 42 | #include "Tonemapper.h" |
| 43 | #include "hwui/Bitmap.h" |
| 44 | #include "pipeline/skia/LayerDrawable.h" |
| 45 | #include "renderthread/EglManager.h" |
| 46 | #include "renderthread/VulkanManager.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 47 | #include "utils/Color.h" |
| 48 | #include "utils/MathUtils.h" |
Alec Mouri | 4523801 | 2020-01-29 11:04:40 -0800 | [diff] [blame] | 49 | #include "utils/NdkUtils.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 50 | |
| 51 | using namespace android::uirenderer::renderthread; |
| 52 | |
| 53 | namespace android { |
| 54 | namespace uirenderer { |
| 55 | |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 56 | #define ARECT_ARGS(r) float((r).left), float((r).top), float((r).right), float((r).bottom) |
| 57 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 58 | void Readback::copySurfaceInto(ANativeWindow* window, const std::shared_ptr<CopyRequest>& request) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 59 | ATRACE_CALL(); |
| 60 | // Setup the source |
Alec Mouri | 8a82b14 | 2019-12-17 09:41:48 -0800 | [diff] [blame] | 61 | AHardwareBuffer* rawSourceBuffer; |
| 62 | int rawSourceFence; |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 63 | ARect cropRect; |
| 64 | uint32_t windowTransform; |
| 65 | status_t err = ANativeWindow_getLastQueuedBuffer2(window, &rawSourceBuffer, &rawSourceFence, |
| 66 | &cropRect, &windowTransform); |
| 67 | base::unique_fd sourceFence(rawSourceFence); |
| 68 | // Really this shouldn't ever happen, but better safe than sorry. |
| 69 | if (err == UNKNOWN_TRANSACTION) { |
| 70 | ALOGW("Readback failed to ANativeWindow_getLastQueuedBuffer2 - who are we talking to?"); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 71 | return request->onCopyFinished(CopyResult::SourceInvalid); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 72 | } |
| 73 | ALOGV("Using new path, cropRect=" RECT_STRING ", transform=%x", ARECT_ARGS(cropRect), |
| 74 | windowTransform); |
| 75 | |
| 76 | if (err != NO_ERROR) { |
| 77 | ALOGW("Failed to get last queued buffer, error = %d", err); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 78 | return request->onCopyFinished(CopyResult::SourceInvalid); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 79 | } |
| 80 | if (rawSourceBuffer == nullptr) { |
| 81 | ALOGW("Surface doesn't have any previously queued frames, nothing to readback from"); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 82 | return request->onCopyFinished(CopyResult::SourceEmpty); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 83 | } |
| 84 | UniqueAHardwareBuffer sourceBuffer{rawSourceBuffer}; |
| 85 | AHardwareBuffer_Desc description; |
| 86 | AHardwareBuffer_describe(sourceBuffer.get(), &description); |
| 87 | if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) { |
| 88 | ALOGW("Surface is protected, unable to copy from it"); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 89 | return request->onCopyFinished(CopyResult::SourceInvalid); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 90 | } |
| 91 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 92 | { |
| 93 | ATRACE_NAME("sync_wait"); |
Jason Macnak | c355c23 | 2024-07-29 11:10:34 -0700 | [diff] [blame] | 94 | int syncWaitTimeoutMs = 500 * Properties::timeoutMultiplier; |
| 95 | if (sourceFence != -1 && sync_wait(sourceFence.get(), syncWaitTimeoutMs) != NO_ERROR) { |
| 96 | ALOGE("Timeout (%dms) exceeded waiting for buffer fence, abandoning readback attempt", |
| 97 | syncWaitTimeoutMs); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 98 | return request->onCopyFinished(CopyResult::Timeout); |
| 99 | } |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 102 | int32_t dataspace = ANativeWindow_getBuffersDataSpace(window); |
| 103 | |
| 104 | // If the application is not updating the Surface themselves, e.g., another |
| 105 | // process is producing buffers for the application to display, then |
| 106 | // ANativeWindow_getBuffersDataSpace will return an unknown answer, so grab |
| 107 | // the dataspace from buffer metadata instead, if it exists. |
| 108 | if (dataspace == 0) { |
| 109 | dataspace = AHardwareBuffer_getDataSpace(sourceBuffer.get()); |
| 110 | } |
| 111 | |
| 112 | sk_sp<SkColorSpace> colorSpace = |
| 113 | DataSpaceToColorSpace(static_cast<android_dataspace>(dataspace)); |
Jason Macnak | c355c23 | 2024-07-29 11:10:34 -0700 | [diff] [blame] | 114 | sk_sp<SkImage> image = SkImages::DeferredFromAHardwareBuffer(sourceBuffer.get(), |
| 115 | kPremul_SkAlphaType, colorSpace); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 116 | |
| 117 | if (!image.get()) { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 118 | return request->onCopyFinished(CopyResult::UnknownError); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | sk_sp<GrDirectContext> grContext = mRenderThread.requireGrContext(); |
| 122 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 123 | SkRect srcRect = request->srcRect.toSkRect(); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 124 | |
Yiwei Zhang | 6cf96a5 | 2022-07-03 05:53:04 +0000 | [diff] [blame] | 125 | SkRect imageSrcRect = SkRect::MakeIWH(description.width, description.height); |
| 126 | SkISize imageWH = SkISize::Make(description.width, description.height); |
| 127 | if (cropRect.left < cropRect.right && cropRect.top < cropRect.bottom) { |
| 128 | imageSrcRect = |
| 129 | SkRect::MakeLTRB(cropRect.left, cropRect.top, cropRect.right, cropRect.bottom); |
| 130 | imageWH = SkISize::Make(cropRect.right - cropRect.left, cropRect.bottom - cropRect.top); |
| 131 | |
| 132 | // Chroma channels of YUV420 images are subsampled we may need to shrink the crop region by |
| 133 | // a whole texel on each side. Since skia still adds its own 0.5 inset, we apply an |
| 134 | // additional 0.5 inset. See GLConsumer::computeTransformMatrix for details. |
| 135 | float shrinkAmount = 0.0f; |
| 136 | switch (description.format) { |
| 137 | // Use HAL formats since some AHB formats are only available in vndk |
| 138 | case HAL_PIXEL_FORMAT_YCBCR_420_888: |
| 139 | case HAL_PIXEL_FORMAT_YV12: |
| 140 | case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: |
| 141 | shrinkAmount = 0.5f; |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | // Shrink the crop if it has more than 1-px and differs from the buffer size. |
| 148 | if (imageWH.width() > 1 && imageWH.width() < (int32_t)description.width) |
| 149 | imageSrcRect = imageSrcRect.makeInset(shrinkAmount, 0); |
| 150 | |
| 151 | if (imageWH.height() > 1 && imageWH.height() < (int32_t)description.height) |
| 152 | imageSrcRect = imageSrcRect.makeInset(0, shrinkAmount); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 153 | } |
Yiwei Zhang | 6cf96a5 | 2022-07-03 05:53:04 +0000 | [diff] [blame] | 154 | |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 155 | ALOGV("imageSrcRect = " RECT_STRING, SK_RECT_ARGS(imageSrcRect)); |
| 156 | |
| 157 | // Represents the "logical" width/height of the texture. That is, the dimensions of the buffer |
| 158 | // after respecting crop & rotate. flipV/flipH still result in the same width & height |
| 159 | // so we can ignore those for this. |
| 160 | const SkRect textureRect = |
| 161 | (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) |
| 162 | ? SkRect::MakeIWH(imageSrcRect.height(), imageSrcRect.width()) |
| 163 | : SkRect::MakeIWH(imageSrcRect.width(), imageSrcRect.height()); |
| 164 | |
| 165 | if (srcRect.isEmpty()) { |
| 166 | srcRect = textureRect; |
| 167 | } else { |
| 168 | ALOGV("intersecting " RECT_STRING " with " RECT_STRING, SK_RECT_ARGS(srcRect), |
| 169 | SK_RECT_ARGS(textureRect)); |
| 170 | if (!srcRect.intersect(textureRect)) { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 171 | return request->onCopyFinished(CopyResult::UnknownError); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 175 | SkBitmap skBitmap = request->getDestinationBitmap(srcRect.width(), srcRect.height()); |
| 176 | SkBitmap* bitmap = &skBitmap; |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 177 | sk_sp<SkSurface> tmpSurface = |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 178 | SkSurfaces::RenderTarget(mRenderThread.getGrContext(), skgpu::Budgeted::kYes, |
| 179 | bitmap->info(), 0, kTopLeft_GrSurfaceOrigin, nullptr); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 180 | |
| 181 | // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we |
| 182 | // attempt to do the intermediate rendering step in 8888 |
| 183 | if (!tmpSurface.get()) { |
| 184 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 185 | tmpSurface = SkSurfaces::RenderTarget(mRenderThread.getGrContext(), |
| 186 | skgpu::Budgeted::kYes, |
| 187 | tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 188 | if (!tmpSurface.get()) { |
| 189 | ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap"); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 190 | return request->onCopyFinished(CopyResult::UnknownError); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * The grand ordering of events. |
| 196 | * First we apply the buffer's crop, done by using a srcRect of the crop with a dstRect of the |
| 197 | * same width/height as the srcRect but with a 0x0 origin |
| 198 | * |
| 199 | * Second we apply the window transform via a Canvas matrix. Ordering for that is as follows: |
| 200 | * 1) FLIP_H |
| 201 | * 2) FLIP_V |
| 202 | * 3) ROT_90 |
| 203 | * as per GLConsumer::computeTransformMatrix |
| 204 | * |
| 205 | * Third we apply the user's supplied cropping & scale to the output by doing a RectToRect |
| 206 | * matrix transform from srcRect to {0,0, bitmapWidth, bitmapHeight} |
| 207 | * |
| 208 | * Finally we're done messing with this bloody thing for hopefully the last time. |
| 209 | * |
| 210 | * That's a lie since... |
| 211 | * TODO: Do all this same stuff for TextureView as it's strictly more correct & easier |
| 212 | * to rationalize. And we can fix the 1-px crop bug. |
| 213 | */ |
| 214 | |
| 215 | SkMatrix m; |
Yiwei Zhang | 6cf96a5 | 2022-07-03 05:53:04 +0000 | [diff] [blame] | 216 | const SkRect imageDstRect = SkRect::Make(imageWH); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 217 | const float px = imageDstRect.centerX(); |
| 218 | const float py = imageDstRect.centerY(); |
| 219 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 220 | m.postScale(-1.f, 1.f, px, py); |
| 221 | } |
| 222 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 223 | m.postScale(1.f, -1.f, px, py); |
| 224 | } |
| 225 | if (windowTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 226 | m.postRotate(90, 0, 0); |
| 227 | m.postTranslate(imageDstRect.height(), 0); |
| 228 | } |
| 229 | |
| 230 | SkSamplingOptions sampling(SkFilterMode::kNearest); |
| 231 | ALOGV("Mapping from " RECT_STRING " to " RECT_STRING, SK_RECT_ARGS(srcRect), |
| 232 | SK_RECT_ARGS(SkRect::MakeWH(bitmap->width(), bitmap->height()))); |
| 233 | m.postConcat(SkMatrix::MakeRectToRect(srcRect, |
| 234 | SkRect::MakeWH(bitmap->width(), bitmap->height()), |
| 235 | SkMatrix::kFill_ScaleToFit)); |
| 236 | if (srcRect.width() != bitmap->width() || srcRect.height() != bitmap->height()) { |
| 237 | sampling = SkSamplingOptions(SkFilterMode::kLinear); |
| 238 | } |
| 239 | |
| 240 | SkCanvas* canvas = tmpSurface->getCanvas(); |
| 241 | canvas->save(); |
| 242 | canvas->concat(m); |
| 243 | SkPaint paint; |
| 244 | paint.setAlpha(255); |
| 245 | paint.setBlendMode(SkBlendMode::kSrc); |
John Reck | 0f284da | 2021-07-14 14:24:51 -0400 | [diff] [blame] | 246 | const bool hasBufferCrop = cropRect.left < cropRect.right && cropRect.top < cropRect.bottom; |
| 247 | auto constraint = |
| 248 | hasBufferCrop ? SkCanvas::kStrict_SrcRectConstraint : SkCanvas::kFast_SrcRectConstraint; |
Alec Mouri | 9732674 | 2022-11-04 01:32:24 +0000 | [diff] [blame] | 249 | |
| 250 | static constexpr float kMaxLuminanceNits = 4000.f; |
| 251 | tonemapPaint(image->imageInfo(), canvas->imageInfo(), kMaxLuminanceNits, paint); |
| 252 | |
John Reck | 0f284da | 2021-07-14 14:24:51 -0400 | [diff] [blame] | 253 | canvas->drawImageRect(image, imageSrcRect, imageDstRect, sampling, &paint, constraint); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 254 | canvas->restore(); |
| 255 | |
| 256 | if (!tmpSurface->readPixels(*bitmap, 0, 0)) { |
| 257 | // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into |
| 258 | // 8888 and then convert that into the destination format before giving up. |
| 259 | SkBitmap tmpBitmap; |
| 260 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 261 | if (bitmap->info().colorType() == SkColorType::kN32_SkColorType || |
| 262 | !tmpBitmap.tryAllocPixels(tmpInfo) || !tmpSurface->readPixels(tmpBitmap, 0, 0) || |
| 263 | !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), 0, 0)) { |
| 264 | ALOGW("Unable to convert content into the provided bitmap"); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 265 | return request->onCopyFinished(CopyResult::UnknownError); |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | bitmap->notifyPixelsChanged(); |
| 270 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 271 | return request->onCopyFinished(CopyResult::Success); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) { |
| 275 | LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware()); |
| 276 | |
| 277 | Rect srcRect; |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 278 | return copyImageInto(hwBitmap->makeImage(), srcRect, bitmap); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 282 | ATRACE_CALL(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 283 | if (!mRenderThread.getGrContext()) { |
| 284 | return CopyResult::UnknownError; |
| 285 | } |
| 286 | |
| 287 | // acquire most recent buffer for drawing |
| 288 | deferredLayer->updateTexImage(); |
| 289 | deferredLayer->apply(); |
| 290 | const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height()); |
| 291 | CopyResult copyResult = CopyResult::UnknownError; |
| 292 | Layer* layer = deferredLayer->backingLayer(); |
| 293 | if (layer) { |
| 294 | if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) { |
| 295 | copyResult = CopyResult::Success; |
| 296 | } |
| 297 | } |
| 298 | return copyResult; |
| 299 | } |
| 300 | |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 301 | CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) { |
| 302 | Rect srcRect; |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 303 | return copyImageInto(image, srcRect, bitmap); |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 304 | } |
| 305 | |
ramindani | 3952ed6 | 2021-08-12 15:55:12 +0000 | [diff] [blame] | 306 | CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, const Rect& srcRect, |
| 307 | SkBitmap* bitmap) { |
John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 308 | ATRACE_CALL(); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 309 | if (!image.get()) { |
| 310 | return CopyResult::UnknownError; |
| 311 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 312 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 313 | mRenderThread.requireGlContext(); |
| 314 | } else { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 315 | mRenderThread.requireVkContext(); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 316 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 317 | int imgWidth = image->width(); |
| 318 | int imgHeight = image->height(); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 319 | sk_sp<GrDirectContext> grContext = sk_ref_sp(mRenderThread.getGrContext()); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 320 | |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 321 | CopyResult copyResult = CopyResult::UnknownError; |
| 322 | |
| 323 | int displayedWidth = imgWidth, displayedHeight = imgHeight; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 324 | SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height()); |
| 325 | SkRect skiaSrcRect = srcRect.toSkRect(); |
| 326 | if (skiaSrcRect.isEmpty()) { |
| 327 | skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight); |
| 328 | } |
| 329 | bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight)); |
| 330 | if (!srcNotEmpty) { |
| 331 | return copyResult; |
| 332 | } |
| 333 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 334 | Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 335 | layer.setSize(displayedWidth, displayedHeight); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 336 | layer.setImage(image); |
Kazuhiro Inaba | a74d637 | 2020-03-11 00:01:53 +0900 | [diff] [blame] | 337 | // Scaling filter is not explicitly set here, because it is done inside copyLayerInfo |
| 338 | // 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] | 339 | if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) { |
| 340 | copyResult = CopyResult::Success; |
| 341 | } |
| 342 | |
| 343 | return copyResult; |
| 344 | } |
| 345 | |
| 346 | bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect, |
| 347 | SkBitmap* bitmap) { |
Derek Sollenberger | efcbd6d | 2021-03-19 12:10:28 -0400 | [diff] [blame] | 348 | /* This intermediate surface is present to work around limitations that LayerDrawable expects |
| 349 | * to render into a GPU backed canvas. Additionally, the offscreen buffer solution works around |
| 350 | * a scaling issue (b/62262733) that was encountered when sampling from an EGLImage into a |
| 351 | * software buffer. |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 352 | */ |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 353 | sk_sp<SkSurface> tmpSurface = SkSurfaces::RenderTarget(mRenderThread.getGrContext(), |
| 354 | skgpu::Budgeted::kYes, |
| 355 | bitmap->info(), |
| 356 | 0, |
| 357 | kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 358 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 359 | // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we |
| 360 | // attempt to do the intermediate rendering step in 8888 |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 361 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 362 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 363 | tmpSurface = SkSurfaces::RenderTarget(mRenderThread.getGrContext(), |
| 364 | skgpu::Budgeted::kYes, |
| 365 | tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr); |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 366 | if (!tmpSurface.get()) { |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 367 | 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] | 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 372 | if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(), |
| 373 | tmpSurface->getCanvas(), layer, srcRect, dstRect, |
| 374 | false)) { |
| 375 | ALOGW("Unable to draw content from GPU into the provided bitmap"); |
| 376 | return false; |
| 377 | } |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 378 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 379 | if (!tmpSurface->readPixels(*bitmap, 0, 0)) { |
| 380 | // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into |
| 381 | // 8888 and then convert that into the destination format before giving up. |
| 382 | SkBitmap tmpBitmap; |
| 383 | SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType); |
| 384 | if (bitmap->info().colorType() == SkColorType::kN32_SkColorType || |
| 385 | !tmpBitmap.tryAllocPixels(tmpInfo) || |
| 386 | !tmpSurface->readPixels(tmpBitmap, 0, 0) || |
| 387 | !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(), |
| 388 | bitmap->rowBytes(), 0, 0)) { |
| 389 | ALOGW("Unable to convert content into the provided bitmap"); |
| 390 | return false; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 394 | bitmap->notifyPixelsChanged(); |
| 395 | return true; |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | } /* namespace uirenderer */ |
| 399 | } /* namespace android */ |