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: TaskProcessorBase()30 TaskProcessorBase() { } ~TaskProcessorBase()31 virtual ~TaskProcessorBase() { }; 32 33 virtual void process(const sp<TaskBase>& task) = 0; 34 }; 35 36 template<typename T> 37 class TaskProcessor: public TaskProcessorBase { 38 public: TaskProcessor(TaskManager * manager)39 explicit TaskProcessor(TaskManager* manager): mManager(manager) { } ~TaskProcessor()40 virtual ~TaskProcessor() { } 41 add(const sp<Task<T>> & task)42 void add(const sp<Task<T> >& task) { 43 if (!addImpl(task)) { 44 // fall back to immediate execution 45 process(task); 46 } 47 } 48 49 virtual void onProcess(const sp<Task<T> >& task) = 0; 50 51 private: 52 bool addImpl(const sp<Task<T> >& task); 53 process(const sp<TaskBase> & task)54 virtual void process(const sp<TaskBase>& task) override { 55 sp<Task<T> > realTask = static_cast<Task<T>* >(task.get()); 56 // This is the right way to do it but sp<> doesn't play nice 57 // sp<Task<T> > realTask = static_cast<sp<Task<T> > >(task); 58 onProcess(realTask); 59 } 60 61 TaskManager* mManager; 62 }; 63 64 template<typename T> addImpl(const sp<Task<T>> & task)65bool TaskProcessor<T>::addImpl(const sp<Task<T> >& task) { 66 if (mManager) { 67 sp<TaskProcessor<T> > self(this); 68 return mManager->addTask(task, self); 69 } 70 return false; 71 } 72 73 }; // namespace uirenderer 74 }; // namespace android 75 76 #endif // ANDROID_HWUI_TASK_PROCESSOR_H 77