1 /* 2 * Copyright (c) 2022 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 #include "canvas_context.h" 17 18 #include <iostream> 19 20 #include "drawing_utils.h" 21 #include "software_render_backend.h" 22 #include "gles_render_backend.h" 23 #include "vulkan_render_backend.h" 24 25 namespace OHOS { 26 namespace Rosen { Create()27CanvasContext* CanvasContext::Create() 28 { 29 auto type = Setting::GetRenderBackendType(); 30 switch (type) { 31 case RenderBackendType::VULKAN: 32 #ifdef RS_ENABLE_VK 33 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN || 34 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) { 35 std::cout << "CanvasContext::Create with vulkan backend" << std::endl; 36 return new CanvasContext(std::make_unique<VulkanRenderBackend>()); 37 } 38 #endif 39 break; 40 case RenderBackendType::GLES: 41 #ifdef RS_ENABLE_GL 42 if (RSSystemProperties::GetGpuApiType() == GpuApiType::OPENGL) { 43 std::cout << "CanvasContext::Create with gles backend" << std::endl; 44 return new CanvasContext(std::make_unique<GLESRenderBackend>()); 45 } 46 #endif 47 break; 48 case RenderBackendType::SOFTWARE: 49 std::cout << "CanvasContext::Create with software backend" << std::endl; 50 return new CanvasContext(std::make_unique<SoftwareRenderBackend>()); 51 default: 52 return nullptr; 53 } 54 return nullptr; 55 } 56 CanvasContext(std::unique_ptr<IRenderBackend> renderBackend)57CanvasContext::CanvasContext(std::unique_ptr<IRenderBackend> renderBackend) 58 : renderBackend_(std::move(renderBackend)) 59 { 60 } 61 ~CanvasContext()62CanvasContext::~CanvasContext() 63 { 64 renderBackend_->Destroy(); 65 } 66 CreateSurface(void * window)67void* CanvasContext::CreateSurface(void* window) 68 { 69 return renderBackend_->CreateSurface(window); 70 } 71 RenderFrame()72void CanvasContext::RenderFrame() 73 { 74 renderBackend_->RenderFrame(); 75 } 76 AcquireSkCanvas(std::unique_ptr<SurfaceFrame> & frame)77SkCanvas* CanvasContext::AcquireSkCanvas(std::unique_ptr<SurfaceFrame>& frame) 78 { 79 return renderBackend_->AcquireSkCanvas(frame); 80 } 81 AcquireDrCanvas(std::unique_ptr<SurfaceFrame> & frame)82Drawing::Canvas* CanvasContext::AcquireDrCanvas(std::unique_ptr<SurfaceFrame>& frame) 83 { 84 return renderBackend_->AcquireDrCanvas(frame); 85 } 86 InitDrawContext()87void CanvasContext::InitDrawContext() 88 { 89 renderBackend_->InitDrawContext(); 90 } 91 MakeCurrent()92void CanvasContext::MakeCurrent() 93 { 94 renderBackend_->MakeCurrent(); 95 } 96 SwapBuffers()97void CanvasContext::SwapBuffers() 98 { 99 renderBackend_->SwapBuffers(); 100 } 101 } 102 }