Jerome Gaillard | 09a38e4 | 2024-03-14 15:20:56 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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/SkiaGpuPipeline.h" |
| 18 | |
| 19 | #include <SkImageAndroid.h> |
| 20 | #include <gui/TraceUtils.h> |
| 21 | #include <include/android/SkSurfaceAndroid.h> |
| 22 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
| 23 | |
| 24 | using namespace android::uirenderer::renderthread; |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | namespace skiapipeline { |
| 29 | |
| 30 | SkiaGpuPipeline::SkiaGpuPipeline(RenderThread& thread) : SkiaPipeline(thread) {} |
| 31 | |
| 32 | SkiaGpuPipeline::~SkiaGpuPipeline() { |
| 33 | unpinImages(); |
| 34 | } |
| 35 | |
| 36 | void SkiaGpuPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) { |
| 37 | sk_sp<GrDirectContext> cachedContext; |
| 38 | |
| 39 | // Render all layers that need to be updated, in order. |
| 40 | for (size_t i = 0; i < layers.entries().size(); i++) { |
| 41 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
| 42 | renderLayerImpl(layerNode, layers.entries()[i].damage); |
| 43 | // cache the current context so that we can defer flushing it until |
| 44 | // either all the layers have been rendered or the context changes |
| 45 | GrDirectContext* currentContext = |
| 46 | GrAsDirectContext(layerNode->getLayerSurface()->getCanvas()->recordingContext()); |
| 47 | if (cachedContext.get() != currentContext) { |
| 48 | if (cachedContext.get()) { |
| 49 | ATRACE_NAME("flush layers (context changed)"); |
| 50 | cachedContext->flushAndSubmit(); |
| 51 | } |
| 52 | cachedContext.reset(SkSafeRef(currentContext)); |
| 53 | } |
| 54 | } |
| 55 | if (cachedContext.get()) { |
| 56 | ATRACE_NAME("flush layers"); |
| 57 | cachedContext->flushAndSubmit(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // If the given node didn't have a layer surface, or had one of the wrong size, this method |
| 62 | // creates a new one and returns true. Otherwise does nothing and returns false. |
| 63 | bool SkiaGpuPipeline::createOrUpdateLayer(RenderNode* node, |
| 64 | const DamageAccumulator& damageAccumulator, |
| 65 | ErrorHandler* errorHandler) { |
| 66 | // compute the size of the surface (i.e. texture) to be allocated for this layer |
| 67 | const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 68 | const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE; |
| 69 | |
| 70 | SkSurface* layer = node->getLayerSurface(); |
| 71 | if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) { |
| 72 | SkImageInfo info; |
| 73 | info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(), |
| 74 | kPremul_SkAlphaType, getSurfaceColorSpace()); |
| 75 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 76 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 77 | node->setLayerSurface(SkSurfaces::RenderTarget(mRenderThread.getGrContext(), |
| 78 | skgpu::Budgeted::kYes, info, 0, |
| 79 | this->getSurfaceOrigin(), &props)); |
| 80 | if (node->getLayerSurface()) { |
| 81 | // update the transform in window of the layer to reset its origin wrt light source |
| 82 | // position |
| 83 | Matrix4 windowTransform; |
| 84 | damageAccumulator.computeCurrentTransform(&windowTransform); |
| 85 | node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform); |
| 86 | } else { |
| 87 | String8 cachesOutput; |
| 88 | mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput, |
| 89 | &mRenderThread.renderState()); |
| 90 | ALOGE("%s", cachesOutput.c_str()); |
| 91 | if (errorHandler) { |
| 92 | std::ostringstream err; |
| 93 | err << "Unable to create layer for " << node->getName(); |
| 94 | const int maxTextureSize = DeviceInfo::get()->maxTextureSize(); |
| 95 | err << ", size " << info.width() << "x" << info.height() << " max size " |
| 96 | << maxTextureSize << " color type " << (int)info.colorType() << " has context " |
| 97 | << (int)(mRenderThread.getGrContext() != nullptr); |
| 98 | errorHandler->onError(err.str()); |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool SkiaGpuPipeline::pinImages(std::vector<SkImage*>& mutableImages) { |
| 107 | if (!mRenderThread.getGrContext()) { |
| 108 | ALOGD("Trying to pin an image with an invalid GrContext"); |
| 109 | return false; |
| 110 | } |
| 111 | for (SkImage* image : mutableImages) { |
| 112 | if (skgpu::ganesh::PinAsTexture(mRenderThread.getGrContext(), image)) { |
| 113 | mPinnedImages.emplace_back(sk_ref_sp(image)); |
| 114 | } else { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void SkiaGpuPipeline::unpinImages() { |
| 122 | for (auto& image : mPinnedImages) { |
| 123 | skgpu::ganesh::UnpinTexture(mRenderThread.getGrContext(), image.get()); |
| 124 | } |
| 125 | mPinnedImages.clear(); |
| 126 | } |
| 127 | |
| 128 | void SkiaGpuPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 129 | GrDirectContext* context = thread.getGrContext(); |
| 130 | if (context && !bitmap->isHardware()) { |
| 131 | ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height()); |
| 132 | auto image = bitmap->makeImage(); |
| 133 | if (image.get()) { |
| 134 | skgpu::ganesh::PinAsTexture(context, image.get()); |
| 135 | skgpu::ganesh::UnpinTexture(context, image.get()); |
| 136 | // A submit is necessary as there may not be a frame coming soon, so without a call |
| 137 | // to submit these texture uploads can just sit in the queue building up until |
| 138 | // we run out of RAM |
| 139 | context->flushAndSubmit(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | sk_sp<SkSurface> SkiaGpuPipeline::getBufferSkSurface( |
| 145 | const renderthread::HardwareBufferRenderParams& bufferParams) { |
| 146 | auto bufferColorSpace = bufferParams.getColorSpace(); |
| 147 | if (mBufferSurface == nullptr || mBufferColorSpace == nullptr || |
| 148 | !SkColorSpace::Equals(mBufferColorSpace.get(), bufferColorSpace.get())) { |
| 149 | mBufferSurface = SkSurfaces::WrapAndroidHardwareBuffer( |
| 150 | mRenderThread.getGrContext(), mHardwareBuffer, kTopLeft_GrSurfaceOrigin, |
| 151 | bufferColorSpace, nullptr, true); |
| 152 | mBufferColorSpace = bufferColorSpace; |
| 153 | } |
| 154 | return mBufferSurface; |
| 155 | } |
| 156 | |
| 157 | void SkiaGpuPipeline::dumpResourceCacheUsage() const { |
| 158 | int resources; |
| 159 | size_t bytes; |
| 160 | mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes); |
| 161 | size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit(); |
| 162 | |
| 163 | SkString log("Resource Cache Usage:\n"); |
| 164 | log.appendf("%8d items\n", resources); |
| 165 | log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes, |
| 166 | bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f))); |
| 167 | |
| 168 | ALOGD("%s", log.c_str()); |
| 169 | } |
| 170 | |
| 171 | void SkiaGpuPipeline::setHardwareBuffer(AHardwareBuffer* buffer) { |
| 172 | if (mHardwareBuffer) { |
| 173 | AHardwareBuffer_release(mHardwareBuffer); |
| 174 | mHardwareBuffer = nullptr; |
| 175 | } |
| 176 | |
| 177 | if (buffer) { |
| 178 | AHardwareBuffer_acquire(buffer); |
| 179 | mHardwareBuffer = buffer; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } /* namespace skiapipeline */ |
| 184 | } /* namespace uirenderer */ |
| 185 | } /* namespace android */ |