GLFunctorDrawable.cpp revision 021693b967a2c5556dddd183eb0247df4079e1ad
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 SkImageInfo canvasInfo = canvas->imageInfo(); 56 SkMatrix44 mat4(canvas->getTotalMatrix()); 57 58 SkIRect ibounds; 59 canvas->getClipDeviceBounds(&ibounds); 60 61 DrawGlInfo info; 62 info.clipLeft = ibounds.fLeft; 63 info.clipTop = ibounds.fTop; 64 info.clipRight = ibounds.fRight; 65 info.clipBottom = ibounds.fBottom; 66 // info.isLayer = hasLayer(); 67 info.isLayer = false; 68 info.width = canvasInfo.width(); 69 info.height = canvasInfo.height(); 70 mat4.asColMajorf(&info.transform[0]); 71 72 //apply a simple clip with a scissor or a complex clip with a stencil 73 SkRegion clipRegion; 74 SkPath path; 75 canvas->getClipStack()->asPath(&path); 76 clipRegion.setPath(path, SkRegion(ibounds)); 77 if (CC_UNLIKELY(clipRegion.isComplex())) { 78 //It is only a temporary solution to use a scissor to draw the stencil. 79 //There is a bug 31489986 to implement efficiently non-rectangular clips. 80 glDisable(GL_SCISSOR_TEST); 81 glDisable(GL_STENCIL_TEST); 82 glStencilMask(0xff); 83 glClearStencil(0); 84 glClear(GL_STENCIL_BUFFER_BIT); 85 glEnable(GL_SCISSOR_TEST); 86 SkRegion::Cliperator it(clipRegion, ibounds); 87 while (!it.done()) { 88 setScissor(info.height, it.rect()); 89 glClearStencil(0x1); 90 glClear(GL_STENCIL_BUFFER_BIT); 91 it.next(); 92 } 93 glDisable(GL_SCISSOR_TEST); 94 glStencilFunc(GL_EQUAL, 0x1, 0xff); 95 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 96 glEnable(GL_STENCIL_TEST); 97 } else if (clipRegion.isEmpty()) { 98 glDisable(GL_STENCIL_TEST); 99 glDisable(GL_SCISSOR_TEST); 100 } else { 101 glDisable(GL_STENCIL_TEST); 102 glEnable(GL_SCISSOR_TEST); 103 setScissor(info.height, clipRegion.getBounds()); 104 } 105 106 (*mFunctor)(DrawGlInfo::kModeDraw, &info); 107 108 canvas->getGrContext()->resetContext(); 109 } 110 111}; // namespace skiapipeline 112}; // namespace uirenderer 113}; // namespace android 114