Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 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 #pragma once
     17 
     18 #include <pthread.h>
     19 #include <ostream>
     20 
     21 #include <log/log.h>
     22 
     23 namespace android {
     24 namespace uirenderer {
     25 
     26 extern pthread_t gGpuThread;
     27 
     28 #define ASSERT_GPU_THREAD()                                                                    \
     29     LOG_ALWAYS_FATAL_IF(!pthread_equal(gGpuThread, pthread_self()),                            \
     30                         "Error, %p of type %d (size=%d) used on wrong thread! cur thread %lu " \
     31                         "!= gpu thread %lu",                                                   \
     32                         this, static_cast<int>(mType), mSize, pthread_self(), gGpuThread)
     33 
     34 enum class GpuObjectType {
     35     Texture = 0,
     36     OffscreenBuffer,
     37     Layer,
     38 
     39     TypeCount,
     40 };
     41 
     42 class GpuMemoryTracker {
     43 public:
     44     GpuObjectType objectType() { return mType; }
     45     int objectSize() { return mSize; }
     46 
     47     static void onGpuContextCreated();
     48     static void onGpuContextDestroyed();
     49     static void dump();
     50     static void dump(std::ostream& stream);
     51     static int getInstanceCount(GpuObjectType type);
     52     static int getTotalSize(GpuObjectType type);
     53     static void onFrameCompleted();
     54 
     55 protected:
     56     explicit GpuMemoryTracker(GpuObjectType type) : mType(type) {
     57         ASSERT_GPU_THREAD();
     58         startTrackingObject();
     59     }
     60 
     61     ~GpuMemoryTracker() {
     62         notifySizeChanged(0);
     63         stopTrackingObject();
     64     }
     65 
     66     void notifySizeChanged(int newSize);
     67 
     68 private:
     69     void startTrackingObject();
     70     void stopTrackingObject();
     71 
     72     int mSize = 0;
     73     GpuObjectType mType;
     74 };
     75 
     76 }  // namespace uirenderer
     77 }  // namespace android;
     78