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