blob: 2a488378e3a8974784bb2f834589da0aee9c9218 [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
23#include <SkToSRGBColorFilter.h>
24#include <gui/Surface.h>
25#include <ui/Fence.h>
26#include <ui/GraphicBuffer.h>
27#include "DeferredLayerUpdater.h"
28#include "Properties.h"
29#include "hwui/Bitmap.h"
30#include "utils/Color.h"
31#include "utils/MathUtils.h"
32
33using namespace android::uirenderer::renderthread;
34
35namespace android {
36namespace uirenderer {
37
38CopyResult Readback::copySurfaceInto(Surface& surface, const Rect& srcRect, SkBitmap* bitmap) {
39 ATRACE_CALL();
40 // Setup the source
41 sp<GraphicBuffer> sourceBuffer;
42 sp<Fence> sourceFence;
43 Matrix4 texTransform;
44 status_t err = surface.getLastQueuedBuffer(&sourceBuffer, &sourceFence, texTransform.data);
45 texTransform.invalidateType();
46 if (err != NO_ERROR) {
47 ALOGW("Failed to get last queued buffer, error = %d", err);
48 return CopyResult::UnknownError;
49 }
50 if (!sourceBuffer.get()) {
51 ALOGW("Surface doesn't have any previously queued frames, nothing to readback from");
52 return CopyResult::SourceEmpty;
53 }
54 if (sourceBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) {
55 ALOGW("Surface is protected, unable to copy from it");
56 return CopyResult::SourceInvalid;
57 }
58 err = sourceFence->wait(500 /* ms */);
59 if (err != NO_ERROR) {
60 ALOGE("Timeout (500ms) exceeded waiting for buffer fence, abandoning readback attempt");
61 return CopyResult::Timeout;
62 }
63 if (!sourceBuffer.get()) {
64 return CopyResult::UnknownError;
65 }
66
67 sk_sp<SkColorSpace> colorSpace =
68 DataSpaceToColorSpace(static_cast<android_dataspace>(surface.getBuffersDataSpace()));
Stan Iliev1a025a72018-09-05 16:35:11 -040069 sk_sp<SkImage> image = SkImage::MakeFromAHardwareBuffer(
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040070 reinterpret_cast<AHardwareBuffer*>(sourceBuffer.get()),
71 kPremul_SkAlphaType, colorSpace);
72 return copyImageInto(image, texTransform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040073}
74
75CopyResult Readback::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
76 LOG_ALWAYS_FATAL_IF(!hwBitmap->isHardware());
77
78 Rect srcRect;
79 Matrix4 transform;
80 transform.loadScale(1, -1, 1);
81 transform.translate(0, -1);
82
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040083 return copyImageInto(hwBitmap->makeImage(), transform, srcRect, bitmap);
Stan Iliev1a025a72018-09-05 16:35:11 -040084}
85
86CopyResult Readback::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
87 if (!mRenderThread.getGrContext()) {
88 return CopyResult::UnknownError;
89 }
90
91 // acquire most recent buffer for drawing
92 deferredLayer->updateTexImage();
93 deferredLayer->apply();
94 const SkRect dstRect = SkRect::MakeIWH(bitmap->width(), bitmap->height());
95 CopyResult copyResult = CopyResult::UnknownError;
96 Layer* layer = deferredLayer->backingLayer();
97 if (layer) {
98 if (copyLayerInto(layer, nullptr, &dstRect, bitmap)) {
99 copyResult = CopyResult::Success;
100 }
101 }
102 return copyResult;
103}
104
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400105CopyResult Readback::copyImageInto(const sk_sp<SkImage>& image, Matrix4& texTransform,
Stan Iliev1a025a72018-09-05 16:35:11 -0400106 const Rect& srcRect, SkBitmap* bitmap) {
107 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
108 mRenderThread.requireGlContext();
109 } else {
110 mRenderThread.vulkanManager().initialize();
111 }
112 if (!image.get()) {
113 return CopyResult::UnknownError;
114 }
115 int imgWidth = image->width();
116 int imgHeight = image->height();
117 sk_sp<GrContext> grContext = sk_ref_sp(mRenderThread.getGrContext());
118
119 if (bitmap->colorType() == kRGBA_F16_SkColorType &&
120 !grContext->colorTypeSupportedAsSurface(bitmap->colorType())) {
121 ALOGW("Can't copy surface into bitmap, RGBA_F16 config is not supported");
122 return CopyResult::DestinationInvalid;
123 }
124
125 CopyResult copyResult = CopyResult::UnknownError;
126
127 int displayedWidth = imgWidth, displayedHeight = imgHeight;
128 // If this is a 90 or 270 degree rotation we need to swap width/height to get the device
129 // size.
130 if (texTransform[Matrix4::kSkewX] >= 0.5f || texTransform[Matrix4::kSkewX] <= -0.5f) {
131 std::swap(displayedWidth, displayedHeight);
132 }
133 SkRect skiaDestRect = SkRect::MakeWH(bitmap->width(), bitmap->height());
134 SkRect skiaSrcRect = srcRect.toSkRect();
135 if (skiaSrcRect.isEmpty()) {
136 skiaSrcRect = SkRect::MakeIWH(displayedWidth, displayedHeight);
137 }
138 bool srcNotEmpty = skiaSrcRect.intersect(SkRect::MakeIWH(displayedWidth, displayedHeight));
139 if (!srcNotEmpty) {
140 return copyResult;
141 }
142
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400143 Layer layer(mRenderThread.renderState(), nullptr, 255, SkBlendMode::kSrc);
Stan Iliev1a025a72018-09-05 16:35:11 -0400144 bool disableFilter = MathUtils::areEqual(skiaSrcRect.width(), skiaDestRect.width()) &&
145 MathUtils::areEqual(skiaSrcRect.height(), skiaDestRect.height());
146 layer.setForceFilter(!disableFilter);
147 layer.setSize(displayedWidth, displayedHeight);
148 texTransform.copyTo(layer.getTexTransform());
149 layer.setImage(image);
150 if (copyLayerInto(&layer, &skiaSrcRect, &skiaDestRect, bitmap)) {
151 copyResult = CopyResult::Success;
152 }
153
154 return copyResult;
155}
156
157bool Readback::copyLayerInto(Layer* layer, const SkRect* srcRect, const SkRect* dstRect,
158 SkBitmap* bitmap) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400159 /* This intermediate surface is present to work around a bug in SwiftShader that
160 * prevents us from reading the contents of the layer's texture directly. The
161 * workaround involves first rendering that texture into an intermediate buffer and
162 * then reading from the intermediate buffer into the bitmap.
163 * Another reason to render in an offscreen buffer is to scale and to avoid an issue b/62262733
164 * with reading incorrect data from EGLImage backed SkImage (likely a driver bug).
165 */
166 sk_sp<SkSurface> tmpSurface = SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400167 SkBudgeted::kYes, bitmap->info());
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 Sollenbergerd01b5912018-10-19 15:55:33 -0400174 tmpInfo);
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 */