Home | History | Annotate | Download | only in surfaceflinger
      1 /*
      2  * Copyright 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 #ifndef ANDROID_SURFACEINTERCEPTOR_H
     18 #define ANDROID_SURFACEINTERCEPTOR_H
     19 
     20 #include <frameworks/native/cmds/surfacereplayer/proto/src/trace.pb.h>
     21 
     22 #include <mutex>
     23 
     24 #include <utils/SortedVector.h>
     25 #include <utils/Vector.h>
     26 
     27 namespace android {
     28 
     29 class BufferItem;
     30 class Layer;
     31 class SurfaceFlinger;
     32 struct DisplayState;
     33 struct layer_state_t;
     34 
     35 constexpr auto DEFAULT_FILENAME = "/data/SurfaceTrace.dat";
     36 
     37 /*
     38  * SurfaceInterceptor intercepts and stores incoming streams of window
     39  * properties on SurfaceFlinger.
     40  */
     41 class SurfaceInterceptor {
     42 public:
     43     SurfaceInterceptor(SurfaceFlinger* const flinger);
     44     // Both vectors are used to capture the current state of SF as the initial snapshot in the trace
     45     void enable(const SortedVector<sp<Layer>>& layers,
     46             const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays);
     47     void disable();
     48     bool isEnabled();
     49 
     50     // Intercept display and surface transactions
     51     void saveTransaction(const Vector<ComposerState>& stateUpdates,
     52             const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
     53             const Vector<DisplayState>& changedDisplays, uint32_t flags);
     54 
     55     // Intercept surface data
     56     void saveSurfaceCreation(const sp<const Layer>& layer);
     57     void saveSurfaceDeletion(const sp<const Layer>& layer);
     58     void saveBufferUpdate(const sp<const Layer>& layer, uint32_t width, uint32_t height,
     59             uint64_t frameNumber);
     60 
     61     // Intercept display data
     62     void saveDisplayCreation(const DisplayDeviceState& info);
     63     void saveDisplayDeletion(int32_t displayId);
     64     void savePowerModeUpdate(int32_t displayId, int32_t mode);
     65     void saveVSyncEvent(nsecs_t timestamp);
     66 
     67 private:
     68     // The creation increments of Surfaces and Displays do not contain enough information to capture
     69     // the initial state of each object, so a transaction with all of the missing properties is
     70     // performed at the initial snapshot for each display and surface.
     71     void saveExistingDisplaysLocked(
     72             const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays);
     73     void saveExistingSurfacesLocked(const SortedVector<sp<Layer>>& layers);
     74     void addInitialSurfaceStateLocked(Increment* increment, const sp<const Layer>& layer);
     75     void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display);
     76 
     77     status_t writeProtoFileLocked();
     78     const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle);
     79     const std::string getLayerName(const sp<const Layer>& layer);
     80     int32_t getLayerId(const sp<const Layer>& layer);
     81 
     82     Increment* createTraceIncrementLocked();
     83     void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer);
     84     void addSurfaceDeletionLocked(Increment* increment, const sp<const Layer>& layer);
     85     void addBufferUpdateLocked(Increment* increment, const sp<const Layer>& layer, uint32_t width,
     86             uint32_t height, uint64_t frameNumber);
     87     void addVSyncUpdateLocked(Increment* increment, nsecs_t timestamp);
     88     void addDisplayCreationLocked(Increment* increment, const DisplayDeviceState& info);
     89     void addDisplayDeletionLocked(Increment* increment, int32_t displayId);
     90     void addPowerModeUpdateLocked(Increment* increment, int32_t displayId, int32_t mode);
     91 
     92     // Add surface transactions to the trace
     93     SurfaceChange* createSurfaceChangeLocked(Transaction* transaction, int32_t layerId);
     94     void setProtoRectLocked(Rectangle* protoRect, const Rect& rect);
     95     void addPositionLocked(Transaction* transaction, int32_t layerId, float x, float y);
     96     void addDepthLocked(Transaction* transaction, int32_t layerId, uint32_t z);
     97     void addSizeLocked(Transaction* transaction, int32_t layerId, uint32_t w, uint32_t h);
     98     void addAlphaLocked(Transaction* transaction, int32_t layerId, float alpha);
     99     void addMatrixLocked(Transaction* transaction, int32_t layerId,
    100             const layer_state_t::matrix22_t& matrix);
    101     void addTransparentRegionLocked(Transaction* transaction, int32_t layerId,
    102             const Region& transRegion);
    103     void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags);
    104     void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack);
    105     void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
    106     void addDeferTransactionLocked(Transaction* transaction, int32_t layerId,
    107             const sp<const Layer>& layer, uint64_t frameNumber);
    108     void addFinalCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
    109     void addOverrideScalingModeLocked(Transaction* transaction, int32_t layerId,
    110             int32_t overrideScalingMode);
    111     void addSurfaceChangesLocked(Transaction* transaction, const layer_state_t& state);
    112     void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates,
    113             const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
    114             const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags);
    115 
    116     // Add display transactions to the trace
    117     DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t displayId);
    118     void addDisplaySurfaceLocked(Transaction* transaction, int32_t displayId,
    119             const sp<const IGraphicBufferProducer>& surface);
    120     void addDisplayLayerStackLocked(Transaction* transaction, int32_t displayId,
    121             uint32_t layerStack);
    122     void addDisplaySizeLocked(Transaction* transaction, int32_t displayId, uint32_t w,
    123             uint32_t h);
    124     void addDisplayProjectionLocked(Transaction* transaction, int32_t displayId,
    125             int32_t orientation, const Rect& viewport, const Rect& frame);
    126     void addDisplayChangesLocked(Transaction* transaction,
    127             const DisplayState& state, int32_t displayId);
    128 
    129 
    130     bool mEnabled {false};
    131     std::string mOutputFileName {DEFAULT_FILENAME};
    132     std::mutex mTraceMutex {};
    133     Trace mTrace {};
    134     SurfaceFlinger* const mFlinger;
    135 };
    136 
    137 }
    138 
    139 #endif // ANDROID_SURFACEINTERCEPTOR_H
    140