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