1 /* 2 * Copyright (C) 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 #pragma once 17 18 #include <layerproto/TransactionProto.h> 19 #include <utils/RefBase.h> 20 21 #include "TransactionState.h" 22 23 namespace android::surfaceflinger { 24 25 struct TracingLayerCreationArgs { 26 int32_t layerId; 27 std::string name; 28 uint32_t flags = 0; 29 int32_t parentId = -1; 30 int32_t mirrorFromId = -1; 31 }; 32 33 struct TracingLayerState : layer_state_t { 34 uint64_t bufferId; 35 uint32_t bufferHeight; 36 uint32_t bufferWidth; 37 int32_t pixelFormat; 38 uint64_t bufferUsage; 39 bool hasSidebandStream; 40 int32_t parentId; 41 int32_t relativeParentId; 42 int32_t inputCropId; 43 TracingLayerCreationArgs args; 44 }; 45 46 // Class which exposes buffer properties from BufferData without holding on to the actual buffer 47 // handle. 48 class BufferDataStub : public BufferData { 49 public: BufferDataStub(uint64_t bufferId,uint32_t width,uint32_t height,int32_t pixelFormat,uint64_t outUsage)50 BufferDataStub(uint64_t bufferId, uint32_t width, uint32_t height, int32_t pixelFormat, 51 uint64_t outUsage) 52 : mBufferId(bufferId), 53 mWidth(width), 54 mHeight(height), 55 mPixelFormat(pixelFormat), 56 mOutUsage(outUsage) {} hasBuffer()57 bool hasBuffer() const override { return mBufferId != 0; } hasSameBuffer(const BufferData & other)58 bool hasSameBuffer(const BufferData& other) const override { 59 return getId() == other.getId() && frameNumber == other.frameNumber; 60 } getWidth()61 uint32_t getWidth() const override { return mWidth; } getHeight()62 uint32_t getHeight() const override { return mHeight; } getId()63 uint64_t getId() const override { return mBufferId; } getPixelFormat()64 PixelFormat getPixelFormat() const override { return mPixelFormat; } getUsage()65 uint64_t getUsage() const override { return mOutUsage; } 66 67 private: 68 uint64_t mBufferId; 69 uint32_t mWidth; 70 uint32_t mHeight; 71 int32_t mPixelFormat; 72 uint64_t mOutUsage; 73 }; 74 75 class TransactionProtoParser { 76 public: 77 // Utility class to map handles to ids and buffers to buffer properties without pulling 78 // in SurfaceFlinger dependencies. 79 class FlingerDataMapper { 80 public: 81 virtual ~FlingerDataMapper() = default; getLayerHandle(int32_t)82 virtual sp<IBinder> getLayerHandle(int32_t /* layerId */) const { return nullptr; } getLayerId(const sp<IBinder> &)83 virtual int64_t getLayerId(const sp<IBinder>& /* layerHandle */) const { return -1; } getLayerId(BBinder *)84 virtual int64_t getLayerId(BBinder* /* layerHandle */) const { return -1; } getDisplayHandle(int32_t)85 virtual sp<IBinder> getDisplayHandle(int32_t /* displayId */) const { return nullptr; } getDisplayId(const sp<IBinder> &)86 virtual int32_t getDisplayId(const sp<IBinder>& /* displayHandle */) const { return -1; } getGraphicData(uint64_t bufferId,uint32_t width,uint32_t height,int32_t pixelFormat,uint64_t usage)87 virtual std::shared_ptr<BufferData> getGraphicData(uint64_t bufferId, uint32_t width, 88 uint32_t height, int32_t pixelFormat, 89 uint64_t usage) const { 90 return std::make_shared<BufferDataStub>(bufferId, width, height, pixelFormat, usage); 91 } getGraphicBufferPropertiesFromCache(client_cache_t,uint64_t *,uint32_t *,uint32_t *,int32_t *,uint64_t *)92 virtual void getGraphicBufferPropertiesFromCache(client_cache_t /* cachedBuffer */, 93 uint64_t* /* outBufferId */, 94 uint32_t* /* outWidth */, 95 uint32_t* /* outHeight */, 96 int32_t* /* outPixelFormat */, 97 uint64_t* /* outUsage */) const {} 98 }; 99 TransactionProtoParser(std::unique_ptr<FlingerDataMapper> provider)100 TransactionProtoParser(std::unique_ptr<FlingerDataMapper> provider) 101 : mMapper(std::move(provider)) {} 102 103 proto::TransactionState toProto(const TransactionState&); 104 proto::TransactionState toProto(const std::map<int32_t /* layerId */, TracingLayerState>&); 105 proto::LayerCreationArgs toProto(const TracingLayerCreationArgs& args); 106 107 TransactionState fromProto(const proto::TransactionState&); 108 void mergeFromProto(const proto::LayerState&, TracingLayerState& outState); 109 void fromProto(const proto::LayerCreationArgs&, TracingLayerCreationArgs& outArgs); 110 std::unique_ptr<FlingerDataMapper> mMapper; 111 112 private: 113 proto::LayerState toProto(const layer_state_t&); 114 proto::DisplayState toProto(const DisplayState&); 115 void fromProto(const proto::LayerState&, layer_state_t& out); 116 DisplayState fromProto(const proto::DisplayState&); 117 118 }; 119 120 } // namespace android::surfaceflinger 121