blob: b71bb07dbc8672b09e77045d10d36245653b054c [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
123 if (bitmap->colorType() == kRGBA_F16_SkColorType &&
124 !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) {
125 ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported");
126 return CopyResult::DestinationInvalid;
127 }
128
129 CopyResult copyResult = CopyResult::UnknownError;
130
131 int displayedWidth = imgWidth, displayedHeight = imgHeight;
132 // If this is a 90 or 270 degree rotation we need to swap width/height to get the device
133 // size.
134 if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
135 std::swap(displayedWidth, displayedHeight);
136 }
137 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
138 SkRect skiaSrcRect = srcRect.toSkRect();
139 if (skiaSrcRect.isEmpty()) {
140 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
141 }
142 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
143 if (!srcNotEmpty) {
144 return copyResult;
145 }
146
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400147 Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc);
Stan Iliev1a025a72018-09-05 16:35:11 -0400148 layer.setSize(displayedWidth, displayedHeight);
149 texTransform.copyTo(layer.getTexTransform());
150 layer.setImage(image);
Kazuhiro Inabaa74d6372020-03-11 00:01:53 +0900151 // Scaling filter is not explicitly set here, because it is done inside copyLayerInfo
152 // after checking the necessity based on the src/dest rect size and the transformation.
Stan Iliev1a025a72018-09-05 16:35:11 -0400153 if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) {
154 copyResult = CopyResult::Success;
155 }
156
157 return copyResult;
158}
159
160bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect,
161 SkBitmap* bitmap) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400162 /* This intermediate surface is present to work around a bug in SwiftShader that
163 * prevents us from reading the contents of the layer's texture directly. The
164 * workaround involves first rendering that texture into an intermediate buffer and
165 * then reading from the intermediate buffer into the bitmap.
166 * Another reason to render in an offscreen buffer is to scale and to avoid an issue b/62262733
167 * with reading incorrect data from EGLImage backed SkImage (likely a driver bug).
168 */
169 sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500170 SkBudgeted::kYes, bitmap->info(), 0,
171 kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400172
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400173 // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we
174 // attempt to do the intermediate rendering step in 8888
Stan Iliev1a025a72018-09-05 16:35:11 -0400175 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400176 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
Stan Iliev1a025a72018-09-05 16:35:11 -0400177 tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes,
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500178 tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400179 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400180 ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400181 return false;
182 }
183 }
184
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400185 if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(),
186 tmpSurface->getCanvas(), layer, srcRect, dstRect,
187 false)) {
188 ALOGW("Unable to draw content from GPU into the provided bitmap");
189 return false;
190 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400191
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400192 if (!tmpSurface->readPixels(*bitmap, 0, 0)) {
193 // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into
194 // 8888 and then convert that into the destination format before giving up.
195 SkBitmap tmpBitmap;
196 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
197 if (bitmap->info().colorType() == SkColorType::kN32_SkColorType ||
198 !tmpBitmap.tryAllocPixels(tmpInfo) ||
199 !tmpSurface->readPixels(tmpBitmap, 0, 0) ||
200 !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(),
201 bitmap->rowBytes(), 0, 0)) {
202 ALOGW("Unable to convert content into the provided bitmap");
203 return false;
Stan Iliev1a025a72018-09-05 16:35:11 -0400204 }
205 }
206
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400207 bitmap->notifyPixelsChanged();
208 return true;
Stan Iliev1a025a72018-09-05 16:35:11 -0400209}
210
211} /* namespace uirenderer */
212} /* namespace android */