Home | History | Annotate | Download | only in thread
      1 /*
      2  * Copyright (C) 2013 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_HWUI_TASK_PROCESSOR_H
     18 #define ANDROID_HWUI_TASK_PROCESSOR_H
     19 
     20 #include <utils/RefBase.h>
     21 
     22 #include "Task.h"
     23 #include "TaskManager.h"
     24 
     25 namespace android {
     26 namespace uirenderer {
     27 
     28 class TaskProcessorBase: public RefBase {
     29 public:
     30     TaskProcessorBase() { }
     31     virtual ~TaskProcessorBase() { };
     32 
     33 private:
     34     friend class TaskManager;
     35 
     36     virtual void process(const sp<TaskBase>& task) = 0;
     37 };
     38 
     39 template<typename T>
     40 class TaskProcessor: public TaskProcessorBase {
     41 public:
     42     TaskProcessor(TaskManager* manager): mManager(manager) { }
     43     virtual ~TaskProcessor() { }
     44 
     45     bool add(const sp<Task<T> >& task);
     46 
     47     virtual void onProcess(const sp<Task<T> >& task) = 0;
     48 
     49 private:
     50     virtual void process(const sp<TaskBase>& task) {
     51         sp<Task<T> > realTask = static_cast<Task<T>* >(task.get());
     52         // This is the right way to do it but sp<> doesn't play nice
     53         // sp<Task<T> > realTask = static_cast<sp<Task<T> > >(task);
     54         onProcess(realTask);
     55     }
     56 
     57     TaskManager* mManager;
     58 };
     59 
     60 template<typename T>
     61 bool TaskProcessor<T>::add(const sp<Task<T> >& task) {
     62     if (mManager) {
     63         sp<TaskProcessor<T> > self(this);
     64         return mManager->addTask(task, self);
     65     }
     66     return false;
     67 }
     68 
     69 }; // namespace uirenderer
     70 }; // namespace android
     71 
     72 #endif // ANDROID_HWUI_TASK_PROCESSOR_H
     73