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/small_map.h> 23 #include <ui/LayerStack.h> 24 #include <ui/Size.h> 25 #include <ui/Transform.h> 26 #include <utils/StrongPointer.h> 27 28 #include <scheduler/Fps.h> 29 30 class SkCanvas; 31 32 namespace android { 33 34 class GraphicBuffer; 35 class SurfaceControl; 36 class SurfaceFlinger; 37 38 // Helper class to delete the SurfaceControl on a helper thread as 39 // SurfaceControl assumes its destruction happens without SurfaceFlinger::mStateLock held. 40 class SurfaceControlHolder { 41 public: SurfaceControlHolder(sp<SurfaceControl> sc)42 explicit SurfaceControlHolder(sp<SurfaceControl> sc) : mSurfaceControl(std::move(sc)){}; 43 ~SurfaceControlHolder(); 44 get()45 const sp<SurfaceControl>& get() const { return mSurfaceControl; } 46 47 private: 48 sp<SurfaceControl> mSurfaceControl; 49 }; 50 51 class RefreshRateOverlay { 52 public: 53 RefreshRateOverlay(FpsRange, bool showSpinner); 54 55 void setLayerStack(ui::LayerStack); 56 void setViewport(ui::Size); 57 void changeRefreshRate(Fps); 58 void animate(); 59 60 private: 61 using Buffers = std::vector<sp<GraphicBuffer>>; 62 63 class SevenSegmentDrawer { 64 public: 65 static Buffers draw(int number, SkColor, ui::Transform::RotationFlags, bool showSpinner); 66 67 private: 68 enum class Segment { Upper, UpperLeft, UpperRight, Middle, LowerLeft, LowerRight, Bottom }; 69 70 static void drawSegment(Segment, int left, SkColor, SkCanvas&); 71 static void drawDigit(int digit, int left, SkColor, SkCanvas&); 72 }; 73 74 const Buffers& getOrCreateBuffers(Fps); 75 76 struct Key { 77 int fps; 78 ui::Transform::RotationFlags flags; 79 80 bool operator==(Key other) const { return fps == other.fps && flags == other.flags; } 81 }; 82 83 using BufferCache = ftl::SmallMap<Key, Buffers, 9>; 84 BufferCache mBufferCache; 85 86 std::optional<Fps> mCurrentFps; 87 size_t mFrame = 0; 88 89 const FpsRange mFpsRange; // For color interpolation. 90 const bool mShowSpinner; 91 92 const std::unique_ptr<SurfaceControlHolder> mSurfaceControl; 93 }; 94 95 } // namespace android 96