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