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