blob: fcd72afe4fb990a2ee25d8a580b2a16f351619b8 [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -04001/*
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"
Stan Iliev6a3b05532017-08-01 18:51:37 -040020#include "SkAndroidFrameworkUtils.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "SkClipStack.h"
22#include <private/hwui/DrawGlInfo.h>
Stan Iliev021693b2016-10-17 16:26:15 -040023#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
Greg Daniel45ec62b2017-01-04 14:27:00 -050053 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
54 canvas->clear(SK_ColorRED);
55 return;
56 }
57
Stan Iliev021693b2016-10-17 16:26:15 -040058 SkImageInfo canvasInfo = canvas->imageInfo();
59 SkMatrix44 mat4(canvas->getTotalMatrix());
60
Mike Reed5e438982017-01-25 08:23:25 -050061 SkIRect ibounds = canvas->getDeviceClipBounds();
Stan Iliev021693b2016-10-17 16:26:15 -040062
63 DrawGlInfo info;
64 info.clipLeft = ibounds.fLeft;
65 info.clipTop = ibounds.fTop;
66 info.clipRight = ibounds.fRight;
67 info.clipBottom = ibounds.fBottom;
68 // info.isLayer = hasLayer();
69 info.isLayer = false;
70 info.width = canvasInfo.width();
71 info.height = canvasInfo.height();
72 mat4.asColMajorf(&info.transform[0]);
73
Stan Iliev6a3b05532017-08-01 18:51:37 -040074 bool clearStencilAfterFunctor = false;
75
Stan Iliev021693b2016-10-17 16:26:15 -040076 //apply a simple clip with a scissor or a complex clip with a stencil
77 SkRegion clipRegion;
Stan Iliev98d251b2017-01-19 18:08:38 -050078 canvas->temporary_internal_getRgnClip(&clipRegion);
Stan Iliev021693b2016-10-17 16:26:15 -040079 if (CC_UNLIKELY(clipRegion.isComplex())) {
Stan Iliev021693b2016-10-17 16:26:15 -040080 glDisable(GL_SCISSOR_TEST);
Stan Iliev6a3b05532017-08-01 18:51:37 -040081 glStencilMask(0x1);
Stan Iliev021693b2016-10-17 16:26:15 -040082 glClearStencil(0);
83 glClear(GL_STENCIL_BUFFER_BIT);
Stan Iliev6a3b05532017-08-01 18:51:37 -040084 bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(canvas);
85 canvas->flush();
86 if (stencilWritten) {
87 glStencilMask(0x1);
88 glStencilFunc(GL_EQUAL, 0x1, 0x1);
89 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
90 clearStencilAfterFunctor = true;
91 glEnable(GL_STENCIL_TEST);
92 } else {
93 glDisable(GL_STENCIL_TEST);
Stan Iliev021693b2016-10-17 16:26:15 -040094 }
Stan Iliev021693b2016-10-17 16:26:15 -040095 } else if (clipRegion.isEmpty()) {
Stan Iliev6a3b05532017-08-01 18:51:37 -040096 canvas->flush();
Stan Iliev021693b2016-10-17 16:26:15 -040097 glDisable(GL_STENCIL_TEST);
98 glDisable(GL_SCISSOR_TEST);
99 } else {
Stan Iliev6a3b05532017-08-01 18:51:37 -0400100 canvas->flush();
Stan Iliev021693b2016-10-17 16:26:15 -0400101 glDisable(GL_STENCIL_TEST);
102 glEnable(GL_SCISSOR_TEST);
103 setScissor(info.height, clipRegion.getBounds());
104 }
105
106 (*mFunctor)(DrawGlInfo::kModeDraw, &info);
107
Stan Iliev6a3b05532017-08-01 18:51:37 -0400108 if (clearStencilAfterFunctor) {
109 //clear stencil buffer as it may be used by Skia
110 glDisable(GL_SCISSOR_TEST);
111 glDisable(GL_STENCIL_TEST);
112 glStencilMask(0x1);
113 glClearStencil(0);
114 glClear(GL_STENCIL_BUFFER_BIT);
115 }
116
Stan Iliev021693b2016-10-17 16:26:15 -0400117 canvas->getGrContext()->resetContext();
118 }
119
120}; // namespace skiapipeline
121}; // namespace uirenderer
122}; // namespace android