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