blob: 145526996678c5cb879e990dad8694e613bc88a4 [file] [log] [blame]
Stan Iliev1a025a72018-09-05 16:35:11 -04001/*
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 Mouri8a82b142019-12-17 09:41:48 -080019#include <sync/sync.h>
20#include <system/window.h>
Alec Mouri8a82b142019-12-17 09:41:48 -080021
Stan Iliev1a025a72018-09-05 16:35:11 -040022#include "DeferredLayerUpdater.h"
23#include "Properties.h"
24#include "hwui/Bitmap.h"
Alec Mouri8a82b142019-12-17 09:41:48 -080025#include "pipeline/skia/LayerDrawable.h"
26#include "renderthread/EglManager.h"
27#include "renderthread/VulkanManager.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040028#include "utils/Color.h"
29#include "utils/MathUtils.h"
Alec Mouri45238012020-01-29 11:04:40 -080030#include "utils/NdkUtils.h"
John Reck322b8ab2019-03-14 13:15:28 -070031#include "utils/TraceUtils.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040032
33using namespace android::uirenderer::renderthread;
34
35namespace android {
36namespace uirenderer {
37
Alec Mouri8a82b142019-12-17 09:41:48 -080038CopyResult Readback::copySurfaceInto(ANativeWindow* window, const Rect& srcRect, SkBitmap* bitmap) {
Stan Iliev1a025a72018-09-05 16:35:11 -040039 ATRACE_CALL();
40 // Setup the source
Alec Mouri8a82b142019-12-17 09:41:48 -080041 AHardwareBuffer* rawSourceBuffer;
42 int rawSourceFence;
Stan Iliev1a025a72018-09-05 16:35:11 -040043 Matrix4 texTransform;
Alec Mouri8a82b142019-12-17 09:41:48 -080044 status_t err = ANativeWindow_getLastQueuedBuffer(window, &rawSourceBuffer, &rawSourceFence,
45 texTransform.data);
46 base::unique_fd sourceFence(rawSourceFence);
Stan Iliev1a025a72018-09-05 16:35:11 -040047 texTransform.invalidateType();
48 if (err != NO_ERROR) {
49 ALOGW("Failed to get last queued buffer, error = %d", err);
50 return CopyResult::UnknownError;
51 }
Alec Mouri8a82b142019-12-17 09:41:48 -080052 if (rawSourceBuffer == nullptr) {
Stan Iliev1a025a72018-09-05 16:35:11 -040053 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
54 return CopyResult::SourceEmpty;
55 }
Alec Mouri8a82b142019-12-17 09:41:48 -080056
Alec Mouri45238012020-01-29 11:04:40 -080057 UniqueAHardwareBuffer sourceBuffer{rawSourceBuffer};
Alec Mouri8a82b142019-12-17 09:41:48 -080058 AHardwareBuffer_Desc description;
59 AHardwareBuffer_describe(sourceBuffer.get(), &description);
60 if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
Stan Iliev1a025a72018-09-05 16:35:11 -040061 ALOGW("Surface is protected, unable to copy from it");
62 return CopyResult::SourceInvalid;
63 }
Alec Mouri8a82b142019-12-17 09:41:48 -080064
65 if (sourceFence != -1 && sync_wait(sourceFence.get(), 500 /* ms */) != NO_ERROR) {
Stan Iliev1a025a72018-09-05 16:35:11 -040066 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
67 return CopyResult::Timeout;
68 }
Stan Iliev1a025a72018-09-05 16:35:11 -040069
Alec Mouri8a82b142019-12-17 09:41:48 -080070 sk_sp<SkColorSpace> colorSpace = DataSpaceToColorSpace(
71 static_cast<android_dataspace>(ANativeWindow_getBuffersDataSpace(window)));
72 sk_sp<SkImage> image =
73 SkImage::MakeFromAHardwareBuffer(sourceBuffer.get(), kPremul_SkAlphaType, colorSpace);
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040074 return copyImageInto(image, texTransform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040075}
76
77CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
78 LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware());
79
80 Rect srcRect;
81 Matrix4 transform;
82 transform.loadScale(1, -1, 1);
83 transform.translate(0, -1);
84
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040085 return copyImageInto(hwBitmap->makeImage(), transform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040086}
87
88CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -070089 ATRACE_CALL();
Stan Iliev1a025a72018-09-05 16:35:11 -040090 if (!mRenderThread.getGrContext()) {
91 return CopyResult::UnknownError;
92 }
93
94 // acquire most recent buffer for drawing
95 deferredLayer->updateTexImage();
96 deferredLayer->apply();
97 const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height());
98 CopyResult copyResult = CopyResult::UnknownError;
99 Layer* layer = deferredLayer->backingLayer();
100 if (layer) {
101 if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) {
102 copyResult = CopyResult::Success;
103 }
104 }
105 return copyResult;
106}
107
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400108CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, Matrix4& texTransform,
Stan Iliev1a025a72018-09-05 16:35:11 -0400109 const Rect& srcRect, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700110 ATRACE_CALL();
Stan Iliev1a025a72018-09-05 16:35:11 -0400111 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
112 mRenderThread.requireGlContext();
113 } else {
Stan Iliev981afe72019-02-13 14:24:33 -0500114 mRenderThread.requireVkContext();
Stan Iliev1a025a72018-09-05 16:35:11 -0400115 }
116 if (!image.get()) {
117 return CopyResult::UnknownError;
118 }
119 int imgWidth = image->width();
120 int imgHeight = image->height();
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400121 sk_sp<GrDirectContext> grContext = sk_ref_sp(mRenderThread.getGrContext());
Stan Iliev1a025a72018-09-05 16:35:11 -0400122
Stan Iliev1a025a72018-09-05 16:35:11 -0400123 CopyResult copyResult = CopyResult::UnknownError;
124
125 int displayedWidth = imgWidth, displayedHeight = imgHeight;
126 // If this is a 90 or 270 degree rotation we need to swap width/height to get the device
127 // size.
128 if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
129 std::swap(displayedWidth, displayedHeight);
130 }
131 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
132 SkRect skiaSrcRect = srcRect.toSkRect();
133 if (skiaSrcRect.isEmpty()) {
134 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
135 }
136 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
137 if (!srcNotEmpty) {
138 return copyResult;
139 }
140
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400141 Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc);
Stan Iliev1a025a72018-09-05 16:35:11 -0400142 layer.setSize(displayedWidth, displayedHeight);
143 texTransform.copyTo(layer.getTexTransform());
144 layer.setImage(image);
Kazuhiro Inabaa74d6372020-03-11 00:01:53 +0900145 // Scaling filter is not explicitly set here, because it is done inside copyLayerInfo
146 // after checking the necessity based on the src/dest rect size and the transformation.
Stan Iliev1a025a72018-09-05 16:35:11 -0400147 if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) {
148 copyResult = CopyResult::Success;
149 }
150
151 return copyResult;
152}
153
154bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect,
155 SkBitmap* bitmap) {
Derek Sollenbergerefcbd6d2021-03-19 12:10:28 -0400156 /* This intermediate surface is present to work around limitations that LayerDrawable expects
157 * to render into a GPU backed canvas. Additionally, the offscreen buffer solution works around
158 * a scaling issue (b/62262733) that was encountered when sampling from an EGLImage into a
159 * software buffer.
Stan Iliev1a025a72018-09-05 16:35:11 -0400160 */
161 sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500162 SkBudgeted::kYes, bitmap->info(), 0,
163 kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400164
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400165 // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we
166 // attempt to do the intermediate rendering step in 8888
Stan Iliev1a025a72018-09-05 16:35:11 -0400167 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400168 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
Stan Iliev1a025a72018-09-05 16:35:11 -0400169 tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes,
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500170 tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400171 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400172 ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400173 return false;
174 }
175 }
176
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400177 if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(),
178 tmpSurface->getCanvas(), layer, srcRect, dstRect,
179 false)) {
180 ALOGW("Unable to draw content from GPU into the provided bitmap");
181 return false;
182 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400183
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400184 if (!tmpSurface->readPixels(*bitmap, 0, 0)) {
185 // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into
186 // 8888 and then convert that into the destination format before giving up.
187 SkBitmap tmpBitmap;
188 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
189 if (bitmap->info().colorType() == SkColorType::kN32_SkColorType ||
190 !tmpBitmap.tryAllocPixels(tmpInfo) ||
191 !tmpSurface->readPixels(tmpBitmap, 0, 0) ||
192 !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(),
193 bitmap->rowBytes(), 0, 0)) {
194 ALOGW("Unable to convert content into the provided bitmap");
195 return false;
Stan Iliev1a025a72018-09-05 16:35:11 -0400196 }
197 }
198
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400199 bitmap->notifyPixelsChanged();
200 return true;
Stan Iliev1a025a72018-09-05 16:35:11 -0400201}
202
203} /* namespace uirenderer */
204} /* namespace android */