GLFunctorDrawable.cpp revision 45ec62ba72c5017fae7d8baab20bfb0d4c99c627
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 17#include "GLFunctorDrawable.h" 18#include "GlFunctorLifecycleListener.h" 19#include "RenderNode.h" 20#include "SkClipStack.h" 21#include <private/hwui/DrawGlInfo.h> 22#include <SkPath.h> 23#include <GrContext.h> 24 25namespace android { 26namespace uirenderer { 27namespace skiapipeline { 28 29GLFunctorDrawable::~GLFunctorDrawable() { 30 if(mListener.get() != nullptr) { 31 mListener->onGlFunctorReleased(mFunctor); 32 } 33} 34 35void GLFunctorDrawable::syncFunctor() const { 36 (*mFunctor)(DrawGlInfo::kModeSync, nullptr); 37} 38 39static void setScissor(int viewportHeight, const SkIRect& clip) { 40 SkASSERT(!clip.isEmpty()); 41 // transform to Y-flipped GL space, and prevent negatives 42 GLint y = viewportHeight - clip.fBottom; 43 GLint height = (viewportHeight - clip.fTop) - y; 44 glScissor(clip.fLeft, y, clip.width(), height); 45} 46 47void GLFunctorDrawable::onDraw(SkCanvas* canvas) { 48 if (canvas->getGrContext() == nullptr) { 49 SkDEBUGF(("Attempting to draw GLFunctor into an unsupported surface")); 50 return; 51 } 52 53 canvas->flush(); 54 55 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { 56 canvas->clear(SK_ColorRED); 57 return; 58 } 59 60 SkImageInfo canvasInfo = canvas->imageInfo(); 61 SkMatrix44 mat4(canvas->getTotalMatrix()); 62 63 SkIRect ibounds; 64 canvas->getClipDeviceBounds(&ibounds); 65 66 DrawGlInfo info; 67 info.clipLeft = ibounds.fLeft; 68 info.clipTop = ibounds.fTop; 69 info.clipRight = ibounds.fRight; 70 info.clipBottom = ibounds.fBottom; 71 // info.isLayer = hasLayer(); 72 info.isLayer = false; 73 info.width = canvasInfo.width(); 74 info.height = canvasInfo.height(); 75 mat4.asColMajorf(&info.transform[0]); 76 77 //apply a simple clip with a scissor or a complex clip with a stencil 78 SkRegion clipRegion; 79 SkPath path; 80 canvas->getClipStack()->asPath(&path); 81 clipRegion.setPath(path, SkRegion(ibounds)); 82 if (CC_UNLIKELY(clipRegion.isComplex())) { 83 //It is only a temporary solution to use a scissor to draw the stencil. 84 //There is a bug 31489986 to implement efficiently non-rectangular clips. 85 glDisable(GL_SCISSOR_TEST); 86 glDisable(GL_STENCIL_TEST); 87 glStencilMask(0xff); 88 glClearStencil(0); 89 glClear(GL_STENCIL_BUFFER_BIT); 90 glEnable(GL_SCISSOR_TEST); 91 SkRegion::Cliperator it(clipRegion, ibounds); 92 while (!it.done()) { 93 setScissor(info.height, it.rect()); 94 glClearStencil(0x1); 95 glClear(GL_STENCIL_BUFFER_BIT); 96 it.next(); 97 } 98 glDisable(GL_SCISSOR_TEST); 99 glStencilFunc(GL_EQUAL, 0x1, 0xff); 100 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 101 glEnable(GL_STENCIL_TEST); 102 } else if (clipRegion.isEmpty()) { 103 glDisable(GL_STENCIL_TEST); 104 glDisable(GL_SCISSOR_TEST); 105 } else { 106 glDisable(GL_STENCIL_TEST); 107 glEnable(GL_SCISSOR_TEST); 108 setScissor(info.height, clipRegion.getBounds()); 109 } 110 111 (*mFunctor)(DrawGlInfo::kModeDraw, &info); 112 113 canvas->getGrContext()->resetContext(); 114 } 115 116}; // namespace skiapipeline 117}; // namespace uirenderer 118}; // namespace android 119