Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 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" |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 20 | #include "SkAndroidFrameworkUtils.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 21 | #include "SkClipStack.h" |
| 22 | #include <private/hwui/DrawGlInfo.h> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 23 | #include <GrContext.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | namespace skiapipeline { |
| 28 | |
| 29 | GLFunctorDrawable::~GLFunctorDrawable() { |
| 30 | if(mListener.get() != nullptr) { |
| 31 | mListener->onGlFunctorReleased(mFunctor); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void GLFunctorDrawable::syncFunctor() const { |
| 36 | (*mFunctor)(DrawGlInfo::kModeSync, nullptr); |
| 37 | } |
| 38 | |
| 39 | static 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 | |
| 47 | void GLFunctorDrawable::onDraw(SkCanvas* canvas) { |
| 48 | if (canvas->getGrContext() == nullptr) { |
| 49 | SkDEBUGF(("Attempting to draw GLFunctor into an unsupported surface")); |
| 50 | return; |
| 51 | } |
| 52 | |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 53 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 54 | canvas->clear(SK_ColorRED); |
| 55 | return; |
| 56 | } |
| 57 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 58 | SkImageInfo canvasInfo = canvas->imageInfo(); |
| 59 | SkMatrix44 mat4(canvas->getTotalMatrix()); |
| 60 | |
Mike Reed | 5e43898 | 2017-01-25 08:23:25 -0500 | [diff] [blame] | 61 | SkIRect ibounds = canvas->getDeviceClipBounds(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 62 | |
| 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 Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 74 | bool clearStencilAfterFunctor = false; |
| 75 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 76 | //apply a simple clip with a scissor or a complex clip with a stencil |
| 77 | SkRegion clipRegion; |
Stan Iliev | 98d251b | 2017-01-19 18:08:38 -0500 | [diff] [blame] | 78 | canvas->temporary_internal_getRgnClip(&clipRegion); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 79 | if (CC_UNLIKELY(clipRegion.isComplex())) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 80 | glDisable(GL_SCISSOR_TEST); |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 81 | glStencilMask(0x1); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 82 | glClearStencil(0); |
| 83 | glClear(GL_STENCIL_BUFFER_BIT); |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 84 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 94 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 95 | } else if (clipRegion.isEmpty()) { |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 96 | canvas->flush(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 97 | glDisable(GL_STENCIL_TEST); |
| 98 | glDisable(GL_SCISSOR_TEST); |
| 99 | } else { |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 100 | canvas->flush(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 101 | glDisable(GL_STENCIL_TEST); |
| 102 | glEnable(GL_SCISSOR_TEST); |
| 103 | setScissor(info.height, clipRegion.getBounds()); |
| 104 | } |
| 105 | |
| 106 | (*mFunctor)(DrawGlInfo::kModeDraw, &info); |
| 107 | |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 108 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 117 | canvas->getGrContext()->resetContext(); |
| 118 | } |
| 119 | |
| 120 | }; // namespace skiapipeline |
| 121 | }; // namespace uirenderer |
| 122 | }; // namespace android |