1 /* 2 * Copyright 2019 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 #pragma once 18 19 #include <SkColor.h> 20 #include <vector> 21 22 #include <ftl/flags.h> 23 #include <ftl/small_map.h> 24 #include <gui/SurfaceComposerClient.h> 25 #include <ui/LayerStack.h> 26 #include <ui/Size.h> 27 #include <ui/Transform.h> 28 #include <utils/StrongPointer.h> 29 30 #include <scheduler/Fps.h> 31 32 class SkCanvas; 33 34 namespace android { 35 36 class GraphicBuffer; 37 class SurfaceControl; 38 class SurfaceFlinger; 39 40 // Helper class to delete the SurfaceControl on a helper thread as 41 // SurfaceControl assumes its destruction happens without SurfaceFlinger::mStateLock held. 42 class SurfaceControlHolder { 43 public: SurfaceControlHolder(sp<SurfaceControl> sc)44 explicit SurfaceControlHolder(sp<SurfaceControl> sc) : mSurfaceControl(std::move(sc)){}; 45 ~SurfaceControlHolder(); 46 get()47 const sp<SurfaceControl>& get() const { return mSurfaceControl; } 48 49 private: 50 sp<SurfaceControl> mSurfaceControl; 51 }; 52 53 class RefreshRateOverlay { 54 public: 55 enum class Features { 56 Spinner = 1 << 0, 57 RenderRate = 1 << 1, 58 ShowInMiddle = 1 << 2, 59 SetByHwc = 1 << 3, 60 }; 61 62 RefreshRateOverlay(FpsRange, ftl::Flags<Features>); 63 64 void setLayerStack(ui::LayerStack); 65 void setViewport(ui::Size); 66 void changeRefreshRate(Fps, Fps); 67 void animate(); isSetByHwc()68 bool isSetByHwc() const { return mFeatures.test(RefreshRateOverlay::Features::SetByHwc); } 69 70 private: 71 using Buffers = std::vector<sp<GraphicBuffer>>; 72 73 class SevenSegmentDrawer { 74 public: 75 static Buffers draw(int displayFps, int renderFps, SkColor, ui::Transform::RotationFlags, 76 ftl::Flags<Features>); 77 78 private: 79 enum class Segment { Upper, UpperLeft, UpperRight, Middle, LowerLeft, LowerRight, Bottom }; 80 81 static void drawSegment(Segment, int left, SkColor, SkCanvas&); 82 static void drawDigit(int digit, int left, SkColor, SkCanvas&); 83 static void drawNumber(int number, int left, SkColor, SkCanvas&); 84 }; 85 86 const Buffers& getOrCreateBuffers(Fps, Fps); 87 88 SurfaceComposerClient::Transaction createTransaction() const; 89 90 struct Key { 91 int displayFps; 92 int renderFps; 93 ui::Transform::RotationFlags flags; 94 95 bool operator==(Key other) const { 96 return displayFps == other.displayFps && renderFps == other.renderFps && 97 flags == other.flags; 98 } 99 }; 100 101 using BufferCache = ftl::SmallMap<Key, Buffers, 9>; 102 BufferCache mBufferCache; 103 104 std::optional<Fps> mDisplayFps; 105 std::optional<Fps> mRenderFps; 106 size_t mFrame = 0; 107 108 const FpsRange mFpsRange; // For color interpolation. 109 const ftl::Flags<Features> mFeatures; 110 111 const std::unique_ptr<SurfaceControlHolder> mSurfaceControl; 112 }; 113 114 } // namespace android 115