blob: fe633e96b9d7d333b5f13eb980c89a073a2296e4 [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
19#include "pipeline/skia/LayerDrawable.h"
20#include "renderthread/EglManager.h"
21#include "renderthread/VulkanManager.h"
22
Stan Iliev1a025a72018-09-05 16:35:11 -040023#include <gui/Surface.h>
24#include <ui/Fence.h>
25#include <ui/GraphicBuffer.h>
26#include "DeferredLayerUpdater.h"
27#include "Properties.h"
28#include "hwui/Bitmap.h"
29#include "utils/Color.h"
30#include "utils/MathUtils.h"
31
32using namespace android::uirenderer::renderthread;
33
34namespace android {
35namespace uirenderer {
36
37CopyResult Readback::copySurfaceInto(Surface& surface, const Rect& srcRect, SkBitmap* bitmap) {
38 ATRACE_CALL();
39 // Setup the source
40 sp<GraphicBuffer> sourceBuffer;
41 sp<Fence> sourceFence;
42 Matrix4 texTransform;
43 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence, texTransform.data);
44 texTransform.invalidateType();
45 if (err != NO_ERROR) {
46 ALOGW("Failed to get last queued buffer, error = %d", err);
47 return CopyResult::UnknownError;
48 }
49 if (!sourceBuffer.get()) {
50 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
51 return CopyResult::SourceEmpty;
52 }
53 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
54 ALOGW("Surface is protected, unable to copy from it");
55 return CopyResult::SourceInvalid;
56 }
57 err = sourceFence->wait(500 /* ms */);
58 if (err != NO_ERROR) {
59 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
60 return CopyResult::Timeout;
61 }
62 if (!sourceBuffer.get()) {
63 return CopyResult::UnknownError;
64 }
65
66 sk_sp<SkColorSpace> colorSpace =
67 DataSpaceToColorSpace(static_cast<android_dataspace>(surface.getBuffersDataSpace()));
Stan Iliev1a025a72018-09-05 16:35:11 -040068 sk_sp<SkImage> image = SkImage::MakeFromAHardwareBuffer(
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040069 reinterpret_cast<AHardwareBuffer*>(sourceBuffer.get()),
70 kPremul_SkAlphaType, colorSpace);
71 return copyImageInto(image, texTransform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040072}
73
74CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
75 LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware());
76
77 Rect srcRect;
78 Matrix4 transform;
79 transform.loadScale(1, -1, 1);
80 transform.translate(0, -1);
81
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040082 return copyImageInto(hwBitmap->makeImage(), transform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040083}
84
85CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
86 if (!mRenderThread.getGrContext()) {
87 return CopyResult::UnknownError;
88 }
89
90 // acquire most recent buffer for drawing
91 deferredLayer->updateTexImage();
92 deferredLayer->apply();
93 const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height());
94 CopyResult copyResult = CopyResult::UnknownError;
95 Layer* layer = deferredLayer->backingLayer();
96 if (layer) {
97 if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) {
98 copyResult = CopyResult::Success;
99 }
100 }
101 return copyResult;
102}
103
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400104CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, Matrix4& texTransform,
Stan Iliev1a025a72018-09-05 16:35:11 -0400105 const Rect& srcRect, SkBitmap* bitmap) {
106 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
107 mRenderThread.requireGlContext();
108 } else {
Stan Iliev981afe72019-02-13 14:24:33 -0500109 mRenderThread.requireVkContext();
Stan Iliev1a025a72018-09-05 16:35:11 -0400110 }
111 if (!image.get()) {
112 return CopyResult::UnknownError;
113 }
114 int imgWidth = image->width();
115 int imgHeight = image->height();
116 sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext());
117
118 if (bitmap->colorType() == kRGBA_F16_SkColorType &&
119 !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) {
120 ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported");
121 return CopyResult::DestinationInvalid;
122 }
123
124 CopyResult copyResult = CopyResult::UnknownError;
125
126 int displayedWidth = imgWidth, displayedHeight = imgHeight;
127 // If this is a 90 or 270 degree rotation we need to swap width/height to get the device
128 // size.
129 if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
130 std::swap(displayedWidth, displayedHeight);
131 }
132 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
133 SkRect skiaSrcRect = srcRect.toSkRect();
134 if (skiaSrcRect.isEmpty()) {
135 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
136 }
137 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
138 if (!srcNotEmpty) {
139 return copyResult;
140 }
141
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400142 Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc);
Stan Iliev1a025a72018-09-05 16:35:11 -0400143 bool disableFilter = MathUtils::areEqual(skiaSrcRect.width(), skiaDestRect.width()) &&
144 MathUtils::areEqual(skiaSrcRect.height(), skiaDestRect.height());
145 layer.setForceFilter(!disableFilter);
146 layer.setSize(displayedWidth, displayedHeight);
147 texTransform.copyTo(layer.getTexTransform());
148 layer.setImage(image);
149 if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) {
150 copyResult = CopyResult::Success;
151 }
152
153 return copyResult;
154}
155
156bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect,
157 SkBitmap* bitmap) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400158 /* This intermediate surface is present to work around a bug in SwiftShader that
159 * prevents us from reading the contents of the layer's texture directly. The
160 * workaround involves first rendering that texture into an intermediate buffer and
161 * then reading from the intermediate buffer into the bitmap.
162 * Another reason to render in an offscreen buffer is to scale and to avoid an issue b/62262733
163 * with reading incorrect data from EGLImage backed SkImage (likely a driver bug).
164 */
165 sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500166 SkBudgeted::kYes, bitmap->info(), 0,
167 kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400168
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400169 // if we can't generate a GPU surface that matches the destination bitmap (e.g. 565) then we
170 // attempt to do the intermediate rendering step in 8888
Stan Iliev1a025a72018-09-05 16:35:11 -0400171 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400172 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
Stan Iliev1a025a72018-09-05 16:35:11 -0400173 tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(), SkBudgeted::kYes,
Derek Sollenbergerf4795f52019-02-06 13:54:12 -0500174 tmpInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr);
Stan Iliev1a025a72018-09-05 16:35:11 -0400175 if (!tmpSurface.get()) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400176 ALOGW("Unable to generate GPU buffer in a format compatible with the provided bitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400177 return false;
178 }
179 }
180
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400181 if (!skiapipeline::LayerDrawable::DrawLayer(mRenderThread.getGrContext(),
182 tmpSurface->getCanvas(), layer, srcRect, dstRect,
183 false)) {
184 ALOGW("Unable to draw content from GPU into the provided bitmap");
185 return false;
186 }
Stan Iliev1a025a72018-09-05 16:35:11 -0400187
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400188 if (!tmpSurface->readPixels(*bitmap, 0, 0)) {
189 // if we fail to readback from the GPU directly (e.g. 565) then we attempt to read into
190 // 8888 and then convert that into the destination format before giving up.
191 SkBitmap tmpBitmap;
192 SkImageInfo tmpInfo = bitmap->info().makeColorType(SkColorType::kN32_SkColorType);
193 if (bitmap->info().colorType() == SkColorType::kN32_SkColorType ||
194 !tmpBitmap.tryAllocPixels(tmpInfo) ||
195 !tmpSurface->readPixels(tmpBitmap, 0, 0) ||
196 !tmpBitmap.readPixels(bitmap->info(), bitmap->getPixels(),
197 bitmap->rowBytes(), 0, 0)) {
198 ALOGW("Unable to convert content into the provided bitmap");
199 return false;
Stan Iliev1a025a72018-09-05 16:35:11 -0400200 }
201 }
202
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400203 bitmap->notifyPixelsChanged();
204 return true;
Stan Iliev1a025a72018-09-05 16:35:11 -0400205}
206
207} /* namespace uirenderer */
208} /* namespace android */