blob: 5bbbc1009541ef1fb987ae8d3c2de58d45e7b684 [file] [log] [blame]
Jerome Gaillardbea67ce2024-03-28 14:21:16 +00001/*
2 * Copyright (C) 2024 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 "pipeline/skia/SkiaCpuPipeline.h"
18
19#include <system/window.h>
20
21#include "DeviceInfo.h"
22#include "LightingInfo.h"
23#include "renderthread/Frame.h"
24#include "utils/Color.h"
25
26using namespace android::uirenderer::renderthread;
27
28namespace android {
29namespace uirenderer {
30namespace skiapipeline {
31
32void SkiaCpuPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
33 // Render all layers that need to be updated, in order.
34 for (size_t i = 0; i < layers.entries().size(); i++) {
35 RenderNode* layerNode = layers.entries()[i].renderNode.get();
36 // only schedule repaint if node still on layer - possible it may have been
37 // removed during a dropped frame, but layers may still remain scheduled so
38 // as not to lose info on what portion is damaged
39 if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
40 continue;
41 }
42 bool rendered = renderLayerImpl(layerNode, layers.entries()[i].damage);
43 if (!rendered) {
44 return;
45 }
46 }
47}
48
49// If the given node didn't have a layer surface, or had one of the wrong size, this method
50// creates a new one and returns true. Otherwise does nothing and returns false.
51bool SkiaCpuPipeline::createOrUpdateLayer(RenderNode* node,
52 const DamageAccumulator& damageAccumulator,
53 ErrorHandler* errorHandler) {
54 // compute the size of the surface (i.e. texture) to be allocated for this layer
55 const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
56 const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
57
58 SkSurface* layer = node->getLayerSurface();
59 if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
60 SkImageInfo info;
61 info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
62 kPremul_SkAlphaType, getSurfaceColorSpace());
63 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
64 node->setLayerSurface(SkSurfaces::Raster(info, &props));
65 if (node->getLayerSurface()) {
66 // update the transform in window of the layer to reset its origin wrt light source
67 // position
68 Matrix4 windowTransform;
69 damageAccumulator.computeCurrentTransform(&windowTransform);
70 node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
71 } else {
72 String8 cachesOutput;
73 mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
74 &mRenderThread.renderState());
75 ALOGE("%s", cachesOutput.c_str());
76 if (errorHandler) {
77 std::ostringstream err;
78 err << "Unable to create layer for " << node->getName();
79 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
80 err << ", size " << info.width() << "x" << info.height() << " max size "
81 << maxTextureSize << " color type " << (int)info.colorType() << " has context "
82 << (int)(mRenderThread.getGrContext() != nullptr);
83 errorHandler->onError(err.str());
84 }
85 }
86 return true;
87 }
88 return false;
89}
90
91MakeCurrentResult SkiaCpuPipeline::makeCurrent() {
92 return MakeCurrentResult::AlreadyCurrent;
93}
94
95Frame SkiaCpuPipeline::getFrame() {
96 return Frame(mSurface->width(), mSurface->height(), 0);
97}
98
99IRenderPipeline::DrawResult SkiaCpuPipeline::draw(
100 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
101 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
102 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
103 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
104 const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) {
105 LightingInfo::updateLighting(lightGeometry, lightInfo);
106 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, mSurface,
107 SkMatrix::I());
108 return {true, IRenderPipeline::DrawResult::kUnknownTime, android::base::unique_fd{}};
109}
110
111bool SkiaCpuPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
112 if (surface) {
113 ANativeWindowBuffer* buffer;
114 surface->dequeueBuffer(surface, &buffer, nullptr);
115 int width, height;
116 surface->query(surface, NATIVE_WINDOW_WIDTH, &width);
117 surface->query(surface, NATIVE_WINDOW_HEIGHT, &height);
118 SkImageInfo imageInfo =
119 SkImageInfo::Make(width, height, mSurfaceColorType,
120 SkAlphaType::kPremul_SkAlphaType, mSurfaceColorSpace);
121 size_t widthBytes = width * imageInfo.bytesPerPixel();
122 void* pixels = buffer->reserved[0];
123 mSurface = SkSurfaces::WrapPixels(imageInfo, pixels, widthBytes);
124 } else {
125 mSurface = sk_sp<SkSurface>();
126 }
127 return true;
128}
129
130} /* namespace skiapipeline */
131} /* namespace uirenderer */
132} /* namespace android */