1 /* 2 * Copyright (C) 2014 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 #pragma once 18 19 #include <EGL/egl.h> 20 #include <EGL/eglext.h> 21 #include <SkBlendMode.h> 22 #include <SkColorFilter.h> 23 #include <SkImage.h> 24 #include <SkMatrix.h> 25 #include <android/hardware_buffer.h> 26 #include <android/surface_texture.h> 27 #include <cutils/compiler.h> 28 #include <utils/Errors.h> 29 #include <utils/Timers.h> 30 31 #include <chrono> 32 #include <map> 33 #include <memory> 34 35 #include "Layer.h" 36 #include "Rect.h" 37 #include "renderstate/RenderState.h" 38 39 namespace android { 40 namespace uirenderer { 41 42 class AutoBackendTextureRelease; 43 class RenderState; 44 45 typedef std::unique_ptr<ASurfaceTexture, decltype(&ASurfaceTexture_release)> AutoTextureRelease; 46 47 // Container to hold the properties a layer should be set to at the start 48 // of a render pass 49 class DeferredLayerUpdater : public VirtualLightRefBase, public IGpuContextCallback { 50 public: 51 // Note that DeferredLayerUpdater assumes it is taking ownership of the layer 52 // and will not call incrementRef on it as a result. 53 explicit DeferredLayerUpdater(RenderState& renderState); 54 55 ~DeferredLayerUpdater(); 56 setSize(int width,int height)57 bool setSize(int width, int height) { 58 if (mWidth != width || mHeight != height) { 59 mWidth = width; 60 mHeight = height; 61 return true; 62 } 63 return false; 64 } 65 getWidth()66 int getWidth() { return mWidth; } getHeight()67 int getHeight() { return mHeight; } 68 setBlend(bool blend)69 bool setBlend(bool blend) { 70 if (blend != mBlend) { 71 mBlend = blend; 72 return true; 73 } 74 return false; 75 } 76 77 void setSurfaceTexture(AutoTextureRelease&& consumer); 78 updateTexImage()79 void updateTexImage() { mUpdateTexImage = true; } 80 setTransform(const SkMatrix * matrix)81 void setTransform(const SkMatrix* matrix) { 82 delete mTransform; 83 mTransform = matrix ? new SkMatrix(*matrix) : nullptr; 84 } 85 getTransform()86 SkMatrix* getTransform() { return mTransform; } 87 88 void setPaint(const SkPaint* paint); 89 90 void apply(); 91 backingLayer()92 Layer* backingLayer() { return mLayer; } 93 94 void detachSurfaceTexture(); 95 96 void updateLayer(bool forceFilter, const sk_sp<SkImage>& layerImage, const uint32_t transform, 97 SkRect currentCrop, float maxLuminanceNits = -1.f); 98 99 void destroyLayer(); 100 101 protected: 102 void onContextDestroyed() override; 103 104 private: 105 /** 106 * ImageSlot contains the information and object references that 107 * DeferredLayerUpdater maintains about a slot. Slot id comes from 108 * ASurfaceTexture_dequeueBuffer. Usually there are at most 3 slots active at a time. 109 */ 110 class ImageSlot { 111 public: ~ImageSlot()112 ~ImageSlot() {} 113 114 sk_sp<SkImage> createIfNeeded(AHardwareBuffer* buffer, android_dataspace dataspace, 115 bool forceCreate, GrDirectContext* context); 116 117 void releaseQueueOwnership(GrDirectContext* context); 118 119 void clear(GrDirectContext* context); 120 121 private: 122 123 // the dataspace associated with the current image 124 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN; 125 126 AHardwareBuffer* mBuffer = nullptr; 127 128 /** 129 * mTextureRelease may outlive DeferredLayerUpdater, if the last ref is held by an SkImage. 130 * DeferredLayerUpdater holds one ref to mTextureRelease, which is decremented by "clear". 131 */ 132 AutoBackendTextureRelease* mTextureRelease = nullptr; 133 }; 134 135 static status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, EGLDisplay* display, 136 int* releaseFence, void* handle); 137 static status_t fenceWait(int fence, void* handle); 138 139 /** 140 * DeferredLayerUpdater stores the SkImages that have been allocated by the BufferQueue 141 * for each buffer slot. 142 */ 143 std::map<int, ImageSlot> mImageSlots; 144 145 RenderState& mRenderState; 146 147 // Generic properties 148 int mWidth = 0; 149 int mHeight = 0; 150 bool mBlend = false; 151 sk_sp<SkColorFilter> mColorFilter; 152 int mAlpha = 255; 153 SkBlendMode mMode = SkBlendMode::kSrcOver; 154 AutoTextureRelease mSurfaceTexture; 155 SkMatrix* mTransform; 156 bool mGLContextAttached; 157 bool mUpdateTexImage; 158 int mCurrentSlot = -1; 159 android_dataspace mDataspace = HAL_DATASPACE_UNKNOWN; 160 std::chrono::steady_clock::time_point mFirstTimeForDataspace = 161 std::chrono::steady_clock::time_point::min(); 162 163 Layer* mLayer; 164 }; 165 166 } /* namespace uirenderer */ 167 } /* namespace android */ 168