1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RENDER_SERVICE_BASE_PROPERTY_RS_COLOR_PICKER_CACHE_TASK_H 17 #define RENDER_SERVICE_BASE_PROPERTY_RS_COLOR_PICKER_CACHE_TASK_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #ifdef IS_OHOS 22 #include "event_handler.h" 23 #endif 24 #ifndef USE_ROSEN_DRAWING 25 #include "include/core/SkImageInfo.h" 26 #include "include/core/SkRect.h" 27 #include "include/core/SkRefCnt.h" 28 #include "include/core/SkSurface.h" 29 #include "include/gpu/GrBackendSurface.h" 30 #endif 31 32 #include "common/rs_macros.h" 33 #include "common/rs_rect.h" 34 #include "pipeline/rs_paint_filter_canvas.h" 35 #include "pipeline/rs_uni_render_judgement.h" 36 #include "platform/common/rs_system_properties.h" 37 #include "render/rs_color_picker.h" 38 #include "render/rs_shadow.h" 39 #include "include/effects/SkRuntimeEffect.h" 40 41 namespace OHOS { 42 namespace Rosen { 43 class RSB_EXPORT RSColorPickerCacheTask final { 44 public: 45 static const bool ColorPickerPartialEnabled; 46 #ifndef USE_ROSEN_DRAWING 47 static bool PostPartialColorPickerTask(std::shared_ptr<RSColorPickerCacheTask> colorPickerTask, 48 sk_sp<SkImage> imageSnapshot); 49 #else 50 static bool PostPartialColorPickerTask(std::shared_ptr<RSColorPickerCacheTask> colorPickerTask, 51 std::shared_ptr<Drawing::Image> imageSnapshot); 52 #endif 53 static std::function<void(std::weak_ptr<RSColorPickerCacheTask>)> postColorPickerTask; 54 #ifndef USE_ROSEN_DRAWING 55 #else 56 #ifdef IS_OHOS 57 static std::function<void(std::shared_ptr<Drawing::Image> &&, 58 std::shared_ptr<Drawing::Surface> &&, 59 std::shared_ptr<OHOS::AppExecFwk::EventHandler>, 60 std::weak_ptr<std::atomic<bool>>, 61 std::weak_ptr<std::mutex>)> saveImgAndSurToRelease; 62 #endif 63 #endif 64 65 RSColorPickerCacheTask() = default; ~RSColorPickerCacheTask()66 ~RSColorPickerCacheTask() 67 { 68 ReleaseColorPicker(); 69 } 70 #ifndef USE_ROSEN_DRAWING 71 bool InitSurface(GrRecordingContext* grContext); 72 #else 73 bool InitSurface(Drawing::GPUContext* gpuContext); 74 #endif 75 bool Render(); 76 GetStatus()77 CacheProcessStatus GetStatus() const 78 { 79 return cacheProcessStatus_.load(); 80 } 81 SetStatus(CacheProcessStatus cacheProcessStatus)82 void SetStatus(CacheProcessStatus cacheProcessStatus) 83 { 84 cacheProcessStatus_.store(cacheProcessStatus); 85 } 86 87 #ifndef USE_ROSEN_DRAWING 88 bool InitTask(const sk_sp<SkImage> imageSnapshot); 89 #else 90 bool InitTask(const std::shared_ptr<Drawing::Image> imageSnapshot); 91 #endif 92 93 void Reset(); 94 Notify()95 void Notify() 96 { 97 cvParallelRender_.notify_one(); 98 } 99 100 bool GetColor(RSColor& color); 101 102 #ifndef USE_ROSEN_DRAWING 103 bool GpuScaleImage(std::shared_ptr<RSPaintFilterCanvas> cacheCanvas, 104 const sk_sp<SkImage> threadImage, std::shared_ptr<SkPixmap>& dst); 105 #else 106 bool GpuScaleImage(std::shared_ptr<RSPaintFilterCanvas> cacheCanvas, 107 const std::shared_ptr<Drawing::Image> threadImage, std::shared_ptr<Drawing::Pixmap>& dst); 108 #endif 109 #ifdef IS_OHOS GetHandler()110 std::shared_ptr<OHOS::AppExecFwk::EventHandler> GetHandler() 111 { 112 return handler_; 113 } GetInitHandler()114 std::shared_ptr<OHOS::AppExecFwk::EventHandler> GetInitHandler() 115 { 116 return initHandler_; 117 } 118 #endif 119 void CalculateColorAverage(RSColor& ColorCur); 120 121 void GetColorAverage(RSColor& color); 122 123 bool GetFirstGetColorFinished(); 124 125 void SetDeviceSize(int& deviceWidth, int& deviceHeight); 126 127 void SetIsShadow(bool isShadow); 128 129 void SetShadowColorStrategy(int shadowColorStrategy); 130 131 void SetWaitRelease(bool waitRelease); 132 133 bool GetDeviceSize(int& deviceWidth, int& deviceHeight) const; 134 135 bool GetWaitRelease() const; 136 137 void ReleaseColorPicker(); 138 139 private: 140 #ifndef USE_ROSEN_DRAWING 141 sk_sp<SkSurface> cacheSurface_ = nullptr; 142 GrBackendTexture cacheBackendTexture_; 143 SkISize surfaceSize_; 144 #else 145 std::shared_ptr<Drawing::Surface> cacheSurface_ = nullptr; 146 Drawing::BackendTexture cacheBackendTexture_; 147 Drawing::RectI surfaceSize_; 148 #endif 149 bool valid_ = false; 150 bool firstGetColorFinished_ = false; 151 bool isShadow_ = false; 152 int shadowColorStrategy_ = SHADOW_COLOR_STRATEGY::COLOR_STRATEGY_NONE; 153 uint32_t* pixelPtr_ = nullptr; 154 std::atomic<CacheProcessStatus> cacheProcessStatus_ = CacheProcessStatus::WAITING; 155 std::shared_ptr<std::atomic<bool>> waitRelease_ = std::make_shared<std::atomic<bool>>(false); 156 #ifndef USE_ROSEN_DRAWING 157 sk_sp<SkImage> imageSnapshotCache_ = nullptr; 158 #else 159 std::shared_ptr<Drawing::Image> imageSnapshotCache_ = nullptr; 160 #endif 161 RSColor color_; 162 std::vector<RSColor> colorArray_; 163 std::vector<bool> colorArrayValid_; 164 RSColor colorAverage_; 165 std::mutex parallelRenderMutex_; 166 std::mutex colorMutex_; 167 std::shared_ptr<std::mutex> grBackendTextureMutex_ = std::make_shared<std::mutex>(); 168 std::condition_variable cvParallelRender_; 169 std::optional<int> deviceWidth_; 170 std::optional<int> deviceHeight_; 171 #ifdef IS_OHOS 172 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr; 173 std::shared_ptr<OHOS::AppExecFwk::EventHandler> initHandler_ = nullptr; 174 #endif 175 }; 176 } // namespace Rosen 177 } // namespace OHOS 178 179 #endif // RENDER_SERVICE_BASE_PROPERTY_RS_COLOR_PICKER_CACHE_TASK_H 180