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 "SkiaPipeline.h"
18 
19 #include <SkCanvas.h>
20 #include <SkColor.h>
21 #include <SkColorSpace.h>
22 #include <SkData.h>
23 #include <SkImage.h>
24 #include <SkImageEncoder.h>
25 #include <SkImageInfo.h>
26 #include <SkImagePriv.h>
27 #include <SkMatrix.h>
28 #include <SkMultiPictureDocument.h>
29 #include <SkOverdrawCanvas.h>
30 #include <SkOverdrawColorFilter.h>
31 #include <SkPicture.h>
32 #include <SkPictureRecorder.h>
33 #include <SkRect.h>
34 #include <SkRefCnt.h>
35 #include <SkSerialProcs.h>
36 #include <SkStream.h>
37 #include <SkString.h>
38 #include <SkTypeface.h>
39 #include <android-base/properties.h>
40 #include <gui/TraceUtils.h>
41 #include <unistd.h>
42 
43 #include <sstream>
44 
45 #include "LightingInfo.h"
46 #include "VectorDrawable.h"
47 #include "include/gpu/GpuTypes.h"  // from Skia
48 #include "thread/CommonPool.h"
49 #include "tools/SkSharingProc.h"
50 #include "utils/Color.h"
51 #include "utils/String8.h"
52 
53 using namespace android::uirenderer::renderthread;
54 
55 namespace android {
56 namespace uirenderer {
57 namespace skiapipeline {
58 
SkiaPipeline(RenderThread & thread)59 SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) {
60     setSurfaceColorProperties(mColorMode);
61 }
62 
~SkiaPipeline()63 SkiaPipeline::~SkiaPipeline() {
64     unpinImages();
65 }
66 
onDestroyHardwareResources()67 void SkiaPipeline::onDestroyHardwareResources() {
68     unpinImages();
69     mRenderThread.cacheManager().trimStaleResources();
70 }
71 
pinImages(std::vector<SkImage * > & mutableImages)72 bool SkiaPipeline::pinImages(std::vector<SkImage*>& mutableImages) {
73     if (!mRenderThread.getGrContext()) {
74         ALOGD("Trying to pin an image with an invalid GrContext");
75         return false;
76     }
77     for (SkImage* image : mutableImages) {
78         if (SkImage_pinAsTexture(image, mRenderThread.getGrContext())) {
79             mPinnedImages.emplace_back(sk_ref_sp(image));
80         } else {
81             return false;
82         }
83     }
84     return true;
85 }
86 
unpinImages()87 void SkiaPipeline::unpinImages() {
88     for (auto& image : mPinnedImages) {
89         SkImage_unpinAsTexture(image.get(), mRenderThread.getGrContext());
90     }
91     mPinnedImages.clear();
92 }
93 
renderLayers(const LightGeometry & lightGeometry,LayerUpdateQueue * layerUpdateQueue,bool opaque,const LightInfo & lightInfo)94 void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry,
95                                 LayerUpdateQueue* layerUpdateQueue, bool opaque,
96                                 const LightInfo& lightInfo) {
97     LightingInfo::updateLighting(lightGeometry, lightInfo);
98     ATRACE_NAME("draw layers");
99     renderLayersImpl(*layerUpdateQueue, opaque);
100     layerUpdateQueue->clear();
101 }
102 
renderLayersImpl(const LayerUpdateQueue & layers,bool opaque)103 void SkiaPipeline::renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) {
104     sk_sp<GrDirectContext> cachedContext;
105 
106     // Render all layers that need to be updated, in order.
107     for (size_t i = 0; i < layers.entries().size(); i++) {
108         RenderNode* layerNode = layers.entries()[i].renderNode.get();
109         // only schedule repaint if node still on layer - possible it may have been
110         // removed during a dropped frame, but layers may still remain scheduled so
111         // as not to lose info on what portion is damaged
112         if (CC_UNLIKELY(layerNode->getLayerSurface() == nullptr)) {
113             continue;
114         }
115         SkASSERT(layerNode->getLayerSurface());
116         SkiaDisplayList* displayList = layerNode->getDisplayList().asSkiaDl();
117         if (!displayList || displayList->isEmpty()) {
118             ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName());
119             return;
120         }
121 
122         const Rect& layerDamage = layers.entries()[i].damage;
123 
124         SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas();
125 
126         int saveCount = layerCanvas->save();
127         SkASSERT(saveCount == 1);
128 
129         layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
130 
131         // TODO: put localized light center calculation and storage to a drawable related code.
132         // It does not seem right to store something localized in a global state
133         // fix here and in recordLayers
134         const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
135         Vector3 transformedLightCenter(savedLightCenter);
136         // map current light center into RenderNode's coordinate space
137         layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
138         LightingInfo::setLightCenterRaw(transformedLightCenter);
139 
140         const RenderProperties& properties = layerNode->properties();
141         const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight());
142         if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) {
143             return;
144         }
145 
146         ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(),
147                       bounds.height());
148 
149         layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false;
150         layerCanvas->clear(SK_ColorTRANSPARENT);
151 
152         RenderNodeDrawable root(layerNode, layerCanvas, false);
153         root.forceDraw(layerCanvas);
154         layerCanvas->restoreToCount(saveCount);
155 
156         LightingInfo::setLightCenterRaw(savedLightCenter);
157 
158         // cache the current context so that we can defer flushing it until
159         // either all the layers have been rendered or the context changes
160         GrDirectContext* currentContext =
161             GrAsDirectContext(layerNode->getLayerSurface()->getCanvas()->recordingContext());
162         if (cachedContext.get() != currentContext) {
163             if (cachedContext.get()) {
164                 ATRACE_NAME("flush layers (context changed)");
165                 cachedContext->flushAndSubmit();
166             }
167             cachedContext.reset(SkSafeRef(currentContext));
168         }
169     }
170 
171     if (cachedContext.get()) {
172         ATRACE_NAME("flush layers");
173         cachedContext->flushAndSubmit();
174     }
175 }
176 
createOrUpdateLayer(RenderNode * node,const DamageAccumulator & damageAccumulator,ErrorHandler * errorHandler)177 bool SkiaPipeline::createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
178                                        ErrorHandler* errorHandler) {
179     // compute the size of the surface (i.e. texture) to be allocated for this layer
180     const int surfaceWidth = ceilf(node->getWidth() / float(LAYER_SIZE)) * LAYER_SIZE;
181     const int surfaceHeight = ceilf(node->getHeight() / float(LAYER_SIZE)) * LAYER_SIZE;
182 
183     SkSurface* layer = node->getLayerSurface();
184     if (!layer || layer->width() != surfaceWidth || layer->height() != surfaceHeight) {
185         SkImageInfo info;
186         info = SkImageInfo::Make(surfaceWidth, surfaceHeight, getSurfaceColorType(),
187                                  kPremul_SkAlphaType, getSurfaceColorSpace());
188         SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
189         SkASSERT(mRenderThread.getGrContext() != nullptr);
190         node->setLayerSurface(SkSurface::MakeRenderTarget(mRenderThread.getGrContext(),
191                                                           skgpu::Budgeted::kYes, info, 0,
192                                                           this->getSurfaceOrigin(), &props));
193         if (node->getLayerSurface()) {
194             // update the transform in window of the layer to reset its origin wrt light source
195             // position
196             Matrix4 windowTransform;
197             damageAccumulator.computeCurrentTransform(&windowTransform);
198             node->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform);
199         } else {
200             String8 cachesOutput;
201             mRenderThread.cacheManager().dumpMemoryUsage(cachesOutput,
202                                                          &mRenderThread.renderState());
203             ALOGE("%s", cachesOutput.string());
204             if (errorHandler) {
205                 std::ostringstream err;
206                 err << "Unable to create layer for " << node->getName();
207                 const int maxTextureSize = DeviceInfo::get()->maxTextureSize();
208                 err << ", size " << info.width() << "x" << info.height() << " max size "
209                     << maxTextureSize << " color type " << (int)info.colorType() << " has context "
210                     << (int)(mRenderThread.getGrContext() != nullptr);
211                 errorHandler->onError(err.str());
212             }
213         }
214         return true;
215     }
216     return false;
217 }
218 
prepareToDraw(const RenderThread & thread,Bitmap * bitmap)219 void SkiaPipeline::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
220     GrDirectContext* context = thread.getGrContext();
221     if (context && !bitmap->isHardware()) {
222         ATRACE_FORMAT("Bitmap#prepareToDraw %dx%d", bitmap->width(), bitmap->height());
223         auto image = bitmap->makeImage();
224         if (image.get()) {
225             SkImage_pinAsTexture(image.get(), context);
226             SkImage_unpinAsTexture(image.get(), context);
227             // A submit is necessary as there may not be a frame coming soon, so without a call
228             // to submit these texture uploads can just sit in the queue building up until
229             // we run out of RAM
230             context->flushAndSubmit();
231         }
232     }
233 }
234 
savePictureAsync(const sk_sp<SkData> & data,const std::string & filename)235 static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) {
236     CommonPool::post([data, filename] {
237         if (0 == access(filename.c_str(), F_OK)) {
238             return;
239         }
240 
241         SkFILEWStream stream(filename.c_str());
242         if (stream.isValid()) {
243             stream.write(data->data(), data->size());
244             stream.flush();
245             ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(),
246                      filename.c_str());
247         }
248     });
249 }
250 
251 // Note multiple SkiaPipeline instances may be loaded if more than one app is visible.
252 // Each instance may observe the filename changing and try to record to a file of the same name.
253 // Only the first one will succeed. There is no scope available here where we could coordinate
254 // to cause this function to return true for only one of the instances.
shouldStartNewFileCapture()255 bool SkiaPipeline::shouldStartNewFileCapture() {
256     // Don't start a new file based capture if one is currently ongoing.
257     if (mCaptureMode != CaptureMode::None) { return false; }
258 
259     // A new capture is started when the filename property changes.
260     // Read the filename property.
261     std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0");
262     // if the filename property changed to a valid value
263     if (prop[0] != '0' && mCapturedFile != prop) {
264         // remember this new filename
265         mCapturedFile = prop;
266         // and get a property indicating how many frames to capture.
267         mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1);
268         if (mCaptureSequence <= 0) {
269             return false;
270         } else if (mCaptureSequence == 1) {
271             mCaptureMode = CaptureMode::SingleFrameSKP;
272         } else {
273             mCaptureMode = CaptureMode::MultiFrameSKP;
274         }
275         return true;
276     }
277     return false;
278 }
279 
280 // performs the first-frame work of a multi frame SKP capture. Returns true if successful.
setupMultiFrameCapture()281 bool SkiaPipeline::setupMultiFrameCapture() {
282     ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence);
283     // We own this stream and need to hold it until close() finishes.
284     auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str());
285     if (stream->isValid()) {
286         mOpenMultiPicStream = std::move(stream);
287         mSerialContext.reset(new SkSharingSerialContext());
288         SkSerialProcs procs;
289         procs.fImageProc = SkSharingSerialContext::serializeImage;
290         procs.fImageCtx = mSerialContext.get();
291         procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
292             return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
293         };
294         // SkDocuments don't take owership of the streams they write.
295         // we need to keep it until after mMultiPic.close()
296         // procs is passed as a pointer, but just as a method of having an optional default.
297         // procs doesn't need to outlive this Make call.
298         mMultiPic = SkMakeMultiPictureDocument(mOpenMultiPicStream.get(), &procs,
299             [sharingCtx = mSerialContext.get()](const SkPicture* pic) {
300                     SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx);
301             });
302         return true;
303     } else {
304         ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str());
305         mCaptureSequence = 0;
306         mCaptureMode = CaptureMode::None;
307         return false;
308     }
309 }
310 
311 // recurse through the rendernode's children, add any nodes which are layers to the queue.
collectLayers(RenderNode * node,LayerUpdateQueue * layers)312 static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) {
313     SkiaDisplayList* dl = node->getDisplayList().asSkiaDl();
314     if (dl) {
315         const auto& prop = node->properties();
316         if (node->hasLayer()) {
317             layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight()));
318         }
319         // The way to recurse through rendernodes is to call this with a lambda.
320         dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); });
321     }
322 }
323 
324 // record the provided layers to the provided canvas as self-contained skpictures.
recordLayers(const LayerUpdateQueue & layers,SkCanvas * mskpCanvas)325 static void recordLayers(const LayerUpdateQueue& layers,
326     SkCanvas* mskpCanvas) {
327     const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw());
328     // Record the commands to re-draw each dirty layer into an SkPicture
329     for (size_t i = 0; i < layers.entries().size(); i++) {
330         RenderNode* layerNode = layers.entries()[i].renderNode.get();
331         const Rect& layerDamage = layers.entries()[i].damage;
332         const RenderProperties& properties = layerNode->properties();
333 
334         // Temporarily map current light center into RenderNode's coordinate space
335         Vector3 transformedLightCenter(savedLightCenter);
336         layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter);
337         LightingInfo::setLightCenterRaw(transformedLightCenter);
338 
339         SkPictureRecorder layerRec;
340         auto* recCanvas = layerRec.beginRecording(properties.getWidth(),
341             properties.getHeight());
342         // This is not recorded but still causes clipping.
343         recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect());
344         RenderNodeDrawable root(layerNode, recCanvas, false);
345         root.forceDraw(recCanvas);
346         // Now write this picture into the SKP canvas with an annotation indicating what it is
347         mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format(
348             "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr);
349         mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture());
350     }
351     LightingInfo::setLightCenterRaw(savedLightCenter);
352 }
353 
tryCapture(SkSurface * surface,RenderNode * root,const LayerUpdateQueue & dirtyLayers)354 SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root,
355     const LayerUpdateQueue& dirtyLayers) {
356     if (CC_LIKELY(!Properties::skpCaptureEnabled)) {
357         return surface->getCanvas(); // Bail out early when capture is not turned on.
358     }
359     // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture.
360     bool firstFrameOfAnim = false;
361     if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) {
362         // set a reminder to record every layer near the end of this method, after we have set up
363         // the nway canvas.
364         firstFrameOfAnim = true;
365         if (!setupMultiFrameCapture()) {
366             return surface->getCanvas();
367         }
368     }
369 
370     // Create a canvas pointer, fill it depending on what kind of capture is requested (if any)
371     SkCanvas* pictureCanvas = nullptr;
372     switch (mCaptureMode) {
373         case CaptureMode::CallbackAPI:
374         case CaptureMode::SingleFrameSKP:
375             mRecorder.reset(new SkPictureRecorder());
376             pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height());
377             break;
378         case CaptureMode::MultiFrameSKP:
379             // If a multi frame recording is active, initialize recording for a single frame of a
380             // multi frame file.
381             pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height());
382             break;
383         case CaptureMode::None:
384             // Returning here in the non-capture case means we can count on pictureCanvas being
385             // non-null below.
386             return surface->getCanvas();
387     }
388 
389     // Setting up an nway canvas is common to any kind of capture.
390     mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height());
391     mNwayCanvas->addCanvas(surface->getCanvas());
392     mNwayCanvas->addCanvas(pictureCanvas);
393 
394     if (firstFrameOfAnim) {
395         // On the first frame of any mskp capture we want to record any layers that are needed in
396         // frame but may have been rendered offscreen before recording began.
397         // We do not maintain a list of all layers, since it isn't needed outside this rare,
398         // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue.
399         LayerUpdateQueue luq;
400         collectLayers(root, &luq);
401         recordLayers(luq, mNwayCanvas.get());
402     } else {
403         // on non-first frames, we record any normal layer draws (dirty regions)
404         recordLayers(dirtyLayers, mNwayCanvas.get());
405     }
406 
407     return mNwayCanvas.get();
408 }
409 
endCapture(SkSurface * surface)410 void SkiaPipeline::endCapture(SkSurface* surface) {
411     if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; }
412     mNwayCanvas.reset();
413     ATRACE_CALL();
414     if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) {
415         mMultiPic->endPage();
416         mCaptureSequence--;
417         if (mCaptureSequence == 0) {
418             mCaptureMode = CaptureMode::None;
419             // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle
420             // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released
421             // to a bare pointer because keeping it in a smart pointer makes the lambda
422             // non-copyable. The lambda is only called once, so this is safe.
423             SkFILEWStream* stream = mOpenMultiPicStream.release();
424             CommonPool::post([doc = std::move(mMultiPic), stream]{
425                 ALOGD("Finalizing multi frame SKP");
426                 doc->close();
427                 delete stream;
428                 ALOGD("Multi frame SKP complete.");
429             });
430         }
431     } else {
432         sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture();
433         if (picture->approximateOpCount() > 0) {
434             if (mPictureCapturedCallback) {
435                 std::invoke(mPictureCapturedCallback, std::move(picture));
436             } else {
437                 // single frame skp to file
438                 SkSerialProcs procs;
439                 procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){
440                     return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData);
441                 };
442                 auto data = picture->serialize(&procs);
443                 savePictureAsync(data, mCapturedFile);
444                 mCaptureSequence = 0;
445                 mCaptureMode = CaptureMode::None;
446             }
447         }
448         mRecorder.reset();
449     }
450 }
451 
renderFrame(const LayerUpdateQueue & layers,const SkRect & clip,const std::vector<sp<RenderNode>> & nodes,bool opaque,const Rect & contentDrawBounds,sk_sp<SkSurface> surface,const SkMatrix & preTransform)452 void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
453                                const std::vector<sp<RenderNode>>& nodes, bool opaque,
454                                const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
455                                const SkMatrix& preTransform) {
456     bool previousSkpEnabled = Properties::skpCaptureEnabled;
457     if (mPictureCapturedCallback) {
458         Properties::skpCaptureEnabled = true;
459     }
460 
461     // Initialize the canvas for the current frame, that might be a recording canvas if SKP
462     // capture is enabled.
463     SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers);
464 
465     // draw all layers up front
466     renderLayersImpl(layers, opaque);
467 
468     renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform);
469 
470     endCapture(surface.get());
471 
472     if (CC_UNLIKELY(Properties::debugOverdraw)) {
473         renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform);
474     }
475 
476     Properties::skpCaptureEnabled = previousSkpEnabled;
477 }
478 
479 namespace {
nodeBounds(RenderNode & node)480 static Rect nodeBounds(RenderNode& node) {
481     auto& props = node.properties();
482     return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
483 }
484 }  // namespace
485 
renderFrameImpl(const SkRect & clip,const std::vector<sp<RenderNode>> & nodes,bool opaque,const Rect & contentDrawBounds,SkCanvas * canvas,const SkMatrix & preTransform)486 void SkiaPipeline::renderFrameImpl(const SkRect& clip,
487                                    const std::vector<sp<RenderNode>>& nodes, bool opaque,
488                                    const Rect& contentDrawBounds, SkCanvas* canvas,
489                                    const SkMatrix& preTransform) {
490     SkAutoCanvasRestore saver(canvas, true);
491     auto clipRestriction = preTransform.mapRect(clip).roundOut();
492     if (CC_UNLIKELY(isCapturingSkp())) {
493         canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction",
494             nullptr);
495     } else {
496         // clip drawing to dirty region only when not recording SKP files (which should contain all
497         // draw ops on every frame)
498         canvas->androidFramework_setDeviceClipRestriction(clipRestriction);
499     }
500     canvas->concat(preTransform);
501 
502     if (!opaque) {
503         canvas->clear(SK_ColorTRANSPARENT);
504     }
505 
506     if (1 == nodes.size()) {
507         if (!nodes[0]->nothingToDraw()) {
508             RenderNodeDrawable root(nodes[0].get(), canvas);
509             root.draw(canvas);
510         }
511     } else if (0 == nodes.size()) {
512         // nothing to draw
513     } else {
514         // It there are multiple render nodes, they are laid out as follows:
515         // #0 - backdrop (content + caption)
516         // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
517         // #2 - additional overlay nodes
518         // Usually the backdrop cannot be seen since it will be entirely covered by the content.
519         // While
520         // resizing however it might become partially visible. The following render loop will crop
521         // the
522         // backdrop against the content and draw the remaining part of it. It will then draw the
523         // content
524         // cropped to the backdrop (since that indicates a shrinking of the window).
525         //
526         // Additional nodes will be drawn on top with no particular clipping semantics.
527 
528         // Usually the contents bounds should be mContentDrawBounds - however - we will
529         // move it towards the fixed edge to give it a more stable appearance (for the moment).
530         // If there is no content bounds we ignore the layering as stated above and start with 2.
531 
532         // Backdrop bounds in render target space
533         const Rect backdrop = nodeBounds(*nodes[0]);
534 
535         // Bounds that content will fill in render target space (note content node bounds may be
536         // bigger)
537         Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
538         content.translate(backdrop.left, backdrop.top);
539         if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
540             // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
541 
542             // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
543             // also fill left/top. Currently, both 2up and freeform position content at the top/left
544             // of
545             // the backdrop, so this isn't necessary.
546             RenderNodeDrawable backdropNode(nodes[0].get(), canvas);
547             if (content.right < backdrop.right) {
548                 // draw backdrop to right side of content
549                 SkAutoCanvasRestore acr(canvas, true);
550                 canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right,
551                                                   backdrop.bottom));
552                 backdropNode.draw(canvas);
553             }
554             if (content.bottom < backdrop.bottom) {
555                 // draw backdrop to bottom of content
556                 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
557                 SkAutoCanvasRestore acr(canvas, true);
558                 canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right,
559                                                   backdrop.bottom));
560                 backdropNode.draw(canvas);
561             }
562         }
563 
564         RenderNodeDrawable contentNode(nodes[1].get(), canvas);
565         if (!backdrop.isEmpty()) {
566             // content node translation to catch up with backdrop
567             float dx = backdrop.left - contentDrawBounds.left;
568             float dy = backdrop.top - contentDrawBounds.top;
569 
570             SkAutoCanvasRestore acr(canvas, true);
571             canvas->translate(dx, dy);
572             const SkRect contentLocalClip =
573                     SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top,
574                                      backdrop.getWidth(), backdrop.getHeight());
575             canvas->clipRect(contentLocalClip);
576             contentNode.draw(canvas);
577         } else {
578             SkAutoCanvasRestore acr(canvas, true);
579             contentNode.draw(canvas);
580         }
581 
582         // remaining overlay nodes, simply defer
583         for (size_t index = 2; index < nodes.size(); index++) {
584             if (!nodes[index]->nothingToDraw()) {
585                 SkAutoCanvasRestore acr(canvas, true);
586                 RenderNodeDrawable overlayNode(nodes[index].get(), canvas);
587                 overlayNode.draw(canvas);
588             }
589         }
590     }
591 }
592 
dumpResourceCacheUsage() const593 void SkiaPipeline::dumpResourceCacheUsage() const {
594     int resources;
595     size_t bytes;
596     mRenderThread.getGrContext()->getResourceCacheUsage(&resources, &bytes);
597     size_t maxBytes = mRenderThread.getGrContext()->getResourceCacheLimit();
598 
599     SkString log("Resource Cache Usage:\n");
600     log.appendf("%8d items\n", resources);
601     log.appendf("%8zu bytes (%.2f MB) out of %.2f MB maximum\n", bytes,
602                 bytes * (1.0f / (1024.0f * 1024.0f)), maxBytes * (1.0f / (1024.0f * 1024.0f)));
603 
604     ALOGD("%s", log.c_str());
605 }
606 
setHardwareBuffer(AHardwareBuffer * buffer)607 void SkiaPipeline::setHardwareBuffer(AHardwareBuffer* buffer) {
608     if (mHardwareBuffer) {
609         AHardwareBuffer_release(mHardwareBuffer);
610         mHardwareBuffer = nullptr;
611     }
612 
613     if (buffer) {
614         AHardwareBuffer_acquire(buffer);
615         mHardwareBuffer = buffer;
616     }
617 }
618 
getBufferSkSurface(const renderthread::HardwareBufferRenderParams & bufferParams)619 sk_sp<SkSurface> SkiaPipeline::getBufferSkSurface(
620         const renderthread::HardwareBufferRenderParams& bufferParams) {
621     auto bufferColorSpace = bufferParams.getColorSpace();
622     if (mBufferSurface == nullptr || mBufferColorSpace == nullptr ||
623         !SkColorSpace::Equals(mBufferColorSpace.get(), bufferColorSpace.get())) {
624         mBufferSurface = SkSurface::MakeFromAHardwareBuffer(
625                 mRenderThread.getGrContext(), mHardwareBuffer, kTopLeft_GrSurfaceOrigin,
626                 bufferColorSpace, nullptr, true);
627         mBufferColorSpace = bufferColorSpace;
628     }
629     return mBufferSurface;
630 }
631 
setSurfaceColorProperties(ColorMode colorMode)632 void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) {
633     mColorMode = colorMode;
634     switch (colorMode) {
635         case ColorMode::Default:
636             mSurfaceColorType = SkColorType::kN32_SkColorType;
637             mSurfaceColorSpace = SkColorSpace::MakeSRGB();
638             break;
639         case ColorMode::WideColorGamut:
640             mSurfaceColorType = DeviceInfo::get()->getWideColorType();
641             mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace();
642             break;
643         case ColorMode::Hdr:
644             mSurfaceColorType = SkColorType::kN32_SkColorType;
645             mSurfaceColorSpace = SkColorSpace::MakeRGB(
646                     GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
647             break;
648         case ColorMode::Hdr10:
649             mSurfaceColorType = SkColorType::kRGBA_1010102_SkColorType;
650             mSurfaceColorSpace = SkColorSpace::MakeRGB(
651                     GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3);
652             break;
653         case ColorMode::A8:
654             mSurfaceColorType = SkColorType::kAlpha_8_SkColorType;
655             mSurfaceColorSpace = nullptr;
656             break;
657     }
658 }
659 
setTargetSdrHdrRatio(float ratio)660 void SkiaPipeline::setTargetSdrHdrRatio(float ratio) {
661     if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) {
662         mTargetSdrHdrRatio = ratio;
663         mSurfaceColorSpace = SkColorSpace::MakeRGB(GetExtendedTransferFunction(mTargetSdrHdrRatio),
664                                                    SkNamedGamut::kDisplayP3);
665     } else {
666         mTargetSdrHdrRatio = 1.f;
667     }
668 }
669 
670 // Overdraw debugging
671 
672 // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences.
673 // This implementation requires transparent entries for "no overdraw" and "single draws".
674 static const SkColor kOverdrawColors[2][6] = {
675     {
676         0x00000000,
677         0x00000000,
678         0x2f0000ff,
679         0x2f00ff00,
680         0x3fff0000,
681         0x7fff0000,
682     },
683     {
684         0x00000000,
685         0x00000000,
686         0x2f0000ff,
687         0x4fffff00,
688         0x5fff89d7,
689         0x7fff0000,
690     },
691 };
692 
renderOverdraw(const SkRect & clip,const std::vector<sp<RenderNode>> & nodes,const Rect & contentDrawBounds,sk_sp<SkSurface> surface,const SkMatrix & preTransform)693 void SkiaPipeline::renderOverdraw(const SkRect& clip,
694                                   const std::vector<sp<RenderNode>>& nodes,
695                                   const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
696                                   const SkMatrix& preTransform) {
697     // Set up the overdraw canvas.
698     SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height());
699     sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo);
700     LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz.");
701     SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas());
702 
703     // Fake a redraw to replay the draw commands.  This will increment the alpha channel
704     // each time a pixel would have been drawn.
705     // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero
706     // initialized.
707     renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform);
708     sk_sp<SkImage> counts = offscreen->makeImageSnapshot();
709 
710     // Draw overdraw colors to the canvas.  The color filter will convert counts to colors.
711     SkPaint paint;
712     const SkColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)];
713     paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors));
714     surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint);
715 }
716 
717 } /* namespace skiapipeline */
718 } /* namespace uirenderer */
719 } /* namespace android */
720