Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 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 "Cache.h" |
| 18 | #include "AutoBackendTexture.h" |
| 19 | #include "SkiaRenderEngine.h" |
| 20 | #include "android-base/unique_fd.h" |
| 21 | #include "renderengine/DisplaySettings.h" |
| 22 | #include "renderengine/LayerSettings.h" |
| 23 | #include "ui/GraphicBuffer.h" |
| 24 | #include "ui/GraphicTypes.h" |
| 25 | #include "ui/PixelFormat.h" |
| 26 | #include "ui/Rect.h" |
| 27 | #include "utils/Timers.h" |
| 28 | |
| 29 | namespace android::renderengine::skia { |
| 30 | |
| 31 | static void drawShadowLayer(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
| 32 | sp<GraphicBuffer> dstBuffer) { |
| 33 | // Somewhat arbitrary dimensions, but on screen and slightly shorter, based |
| 34 | // on actual use. |
| 35 | FloatRect rect(0, 0, display.physicalDisplay.width(), display.physicalDisplay.height() - 30); |
| 36 | LayerSettings layer{ |
| 37 | .geometry = |
| 38 | Geometry{ |
| 39 | .boundaries = rect, |
| 40 | .roundedCornersCrop = rect, |
| 41 | }, |
| 42 | .shadow = |
| 43 | ShadowSettings{ |
| 44 | .ambientColor = vec4(0, 0, 0, 0.00935997f), |
| 45 | .spotColor = vec4(0, 0, 0, 0.0455841f), |
| 46 | .lightPos = vec3(370.508f, -1527.03f, 1650.f), |
| 47 | .lightRadius = 2200.0f, |
| 48 | .length = 0.955342f, |
| 49 | }, |
| 50 | }; |
| 51 | |
| 52 | auto layers = std::vector<const LayerSettings*>{&layer}; |
| 53 | // The identity matrix will generate the fast shaders, and the second matrix |
| 54 | // (based on one seen while going from dialer to the home screen) will |
| 55 | // generate the slower (more general case) version. If we also need a |
| 56 | // slow version without color correction, we should use this matrix with |
| 57 | // display.outputDataspace set to SRGB. |
| 58 | for (const mat4 transform : { mat4(), mat4(0.728872f, 0.f, 0.f, 0.f, |
| 59 | 0.f, 0.727627f, 0.f, 0.f, |
| 60 | 0.f, 0.f, 1.f, 0.f, |
| 61 | 167.355743f, 1852.257812f, 0.f, 1.f) }) { |
| 62 | layer.geometry.positionTransform = transform; |
| 63 | renderengine->drawLayers(display, layers, dstBuffer, false /* useFrameBufferCache*/, |
| 64 | base::unique_fd(), nullptr); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
| 69 | sp<GraphicBuffer> dstBuffer, sp<GraphicBuffer> srcBuffer) { |
| 70 | const Rect& displayRect = display.physicalDisplay; |
| 71 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 72 | LayerSettings layer{ |
| 73 | .geometry = |
| 74 | Geometry{ |
| 75 | .boundaries = rect, |
| 76 | // This matrix is based on actual data seen when opening the dialer. |
| 77 | // What seems to be important in matching the actual use cases are: |
| 78 | // - it is not identity |
| 79 | // - the layer will be drawn (not clipped out etc) |
| 80 | .positionTransform = mat4(.19f, .0f, .0f, .0f, |
| 81 | .0f, .19f, .0f, .0f, |
| 82 | .0f, .0f, 1.f, .0f, |
| 83 | 169.f, 1527.f, .0f, 1.f), |
| 84 | .roundedCornersCrop = rect, |
| 85 | }, |
| 86 | .source = PixelSource{.buffer = |
| 87 | Buffer{ |
| 88 | .buffer = srcBuffer, |
| 89 | .maxMasteringLuminance = 1000.f, |
| 90 | .maxContentLuminance = 1000.f, |
| 91 | }}, |
| 92 | }; |
| 93 | |
| 94 | // Test both drawRect and drawRRect |
| 95 | auto layers = std::vector<const LayerSettings*>{&layer}; |
| 96 | for (float roundedCornersRadius : {0.0f, 500.f}) { |
| 97 | layer.geometry.roundedCornersRadius = roundedCornersRadius; |
| 98 | // No need to check UNKNOWN, which is treated as SRGB. |
| 99 | for (auto dataspace : {ui::Dataspace::SRGB, ui::Dataspace::DISPLAY_P3}) { |
| 100 | layer.sourceDataspace = dataspace; |
| 101 | for (bool isOpaque : {true, false}) { |
| 102 | layer.source.buffer.isOpaque = isOpaque; |
| 103 | for (auto alpha : {half(.23999f), half(1.0f)}) { |
| 104 | layer.alpha = alpha; |
| 105 | renderengine->drawLayers(display, layers, dstBuffer, |
| 106 | false /* useFrameBufferCache*/, base::unique_fd(), |
| 107 | nullptr); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void Cache::primeShaderCache(SkiaRenderEngine* renderengine) { |
| 115 | const nsecs_t timeBefore = systemTime(); |
| 116 | // The dimensions should not matter, so long as we draw inside them. |
| 117 | const Rect displayRect(0, 0, 1080, 2340); |
| 118 | DisplaySettings display{ |
| 119 | .physicalDisplay = displayRect, |
| 120 | .clip = displayRect, |
| 121 | .maxLuminance = 500, |
| 122 | .outputDataspace = ui::Dataspace::DISPLAY_P3, |
| 123 | }; |
| 124 | |
| 125 | const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
| 126 | |
| 127 | sp<GraphicBuffer> dstBuffer = |
| 128 | new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888, 1, |
| 129 | usage, "primeShaderCache_dst"); |
| 130 | // This buffer will be the source for the call to drawImageLayers. Draw |
| 131 | // something to it as a placeholder for what an app draws. We should draw |
| 132 | // something, but the details are not important. We only need one version of |
| 133 | // the shadow's SkSL, so draw it here, giving us both a placeholder image |
| 134 | // and a chance to compile the shadow's SkSL. |
| 135 | sp<GraphicBuffer> srcBuffer = |
| 136 | new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888, 1, |
| 137 | usage, "drawImageLayer_src"); |
| 138 | drawShadowLayer(renderengine, display, srcBuffer); |
| 139 | |
| 140 | drawImageLayers(renderengine, display, dstBuffer, srcBuffer); |
| 141 | const nsecs_t timeAfter = systemTime(); |
| 142 | const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6; |
| 143 | ALOGD("shader cache generated in %f ms\n", compileTimeMs); |
| 144 | } |
| 145 | |
| 146 | } // namespace android::renderengine::skia |