Jerome Gaillard | 09a38e4 | 2024-03-14 15:20:56 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 26 | using namespace android::uirenderer::renderthread; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | namespace skiapipeline { |
| 31 | |
| 32 | void 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 | renderLayerImpl(layers.entries()[i].renderNode.get(), layers.entries()[i].damage); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // If the given node didn't have a layer surface, or had one of the wrong size, this method |
| 40 | // creates a new one and returns true. Otherwise does nothing and returns false. |
| 41 | bool SkiaCpuPipeline::createOrUpdateLayer(RenderNode* node, |
| 42 | const DamageAccumulator& damageAccumulator, |
| 43 | ErrorHandler* errorHandler) { |
| 44 | // compute the size of the surface (i.e. texture) to be allocated for this layer |
| 45 | const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 46 | const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 47 | |
| 48 | SkSurface* layer = node->getLayerSurface(); |
| 49 | if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) { |
| 50 | SkImageInfo info; |
| 51 | info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(), |
| 52 | kPremul_SkAlphaType, getSurfaceColorSpace()); |
| 53 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 54 | node->setLayerSurface(SkSurfaces::Raster(info, &props)); |
| 55 | if (node->getLayerSurface()) { |
| 56 | // update the transform in window of the layer to reset its origin wrt light source |
| 57 | // position |
| 58 | Matrix4 windowTransform; |
| 59 | damageAccumulator.computeCurrentTransform(&windowTransform); |
| 60 | node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform); |
| 61 | } else { |
| 62 | String8 cachesOutput; |
| 63 | mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput, |
| 64 | &mRenderThread.renderState()); |
| 65 | ALOGE("%s", cachesOutput.c_str()); |
| 66 | if (errorHandler) { |
| 67 | std::ostringstream err; |
| 68 | err << "Unable to create layer for " << node->getName(); |
| 69 | const int maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
| 70 | err << ", size " << info.width() << "x" << info.height() << " max size " |
| 71 | << maxTextureSize << " color type " << (int)info.colorType() << " has context " |
| 72 | << (int)(mRenderThread.getGrContext() != nullptr); |
| 73 | errorHandler->onError(err.str()); |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | MakeCurrentResult SkiaCpuPipeline::makeCurrent() { |
| 82 | return MakeCurrentResult::AlreadyCurrent; |
| 83 | } |
| 84 | |
| 85 | Frame SkiaCpuPipeline::getFrame() { |
| 86 | return Frame(mSurface->width(), mSurface->height(), 0); |
| 87 | } |
| 88 | |
| 89 | IRenderPipeline::DrawResult SkiaCpuPipeline::draw( |
| 90 | const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
| 91 | const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, |
| 92 | const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo, |
| 93 | const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler, |
| 94 | const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) { |
| 95 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
| 96 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, mSurface, |
| 97 | SkMatrix::I()); |
| 98 | return {true, IRenderPipeline::DrawResult::kUnknownTime, android::base::unique_fd{}}; |
| 99 | } |
| 100 | |
| 101 | bool SkiaCpuPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) { |
| 102 | if (surface) { |
| 103 | ANativeWindowBuffer* buffer; |
| 104 | surface->dequeueBuffer(surface, &buffer, nullptr); |
| 105 | int width, height; |
| 106 | surface->query(surface, NATIVE_WINDOW_WIDTH, &width); |
| 107 | surface->query(surface, NATIVE_WINDOW_HEIGHT, &height); |
| 108 | SkImageInfo imageInfo = |
| 109 | SkImageInfo::Make(width, height, mSurfaceColorType, |
| 110 | SkAlphaType::kPremul_SkAlphaType, mSurfaceColorSpace); |
| 111 | size_t widthBytes = width * imageInfo.bytesPerPixel(); |
| 112 | void* pixels = buffer->reserved[0]; |
| 113 | mSurface = SkSurfaces::WrapPixels(imageInfo, pixels, widthBytes); |
| 114 | } else { |
| 115 | mSurface = sk_sp<SkSurface>(); |
| 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | } /* namespace skiapipeline */ |
| 121 | } /* namespace uirenderer */ |
| 122 | } /* namespace android */ |