网站建设的税收分类编码,大学生个人简历电子版模板,宁波哪里有网站建设高端的,商标注册申请流程图背景
有学员在学习SurfaceFlinger相关课程时候#xff0c;就问过经常在SurfaceFlinger中看到某个Layer有自己的显示bounds区域#xff0c;而且还有好几个和bounds相关的变量#xff0c;也不太清楚这个bounds计算是怎么计算出来的#xff0c;对这块的理解比较疑惑#xff…背景有学员在学习SurfaceFlinger相关课程时候就问过经常在SurfaceFlinger中看到某个Layer有自己的显示bounds区域而且还有好几个和bounds相关的变量也不太清楚这个bounds计算是怎么计算出来的对这块的理解比较疑惑希望马哥可以搞个文章解释一下。核心代码部分相关变量// Cached properties computed from drawing state// Effective transform taking into account parent transforms and any parent scaling, which is// a transform from the current layer coordinate space to display(screen) coordinate space.ui::Transform mEffectiveTransform;// Bounds of the layer before any transformation is applied and before it has been cropped// by its parents.FloatRect mSourceBounds;// Bounds of the layer in layer space. This is the mSourceBounds cropped by its layer crop and// its parent bounds.FloatRect mBounds;// Layer bounds in screen space.FloatRect mScreenBounds;这里其实因为也注释很详细这里简单中文解释如下mSourceBounds图像原本有多大mBounds图像能显示多少因为可能会经过被裁剪crop遮挡或者parent区域限制mEffectiveTransform图像摆在哪里、怎么摆是否有偏移放大等操作mScreenBounds屏幕上看到图像在哪、有多大原始内容(mSourceBounds)↓ 裁剪1自身裁剪crop ↓ 裁剪2父边界约束不能超出父层 裁剪后内容(mBounds)↓ 应用变换矩阵(mEffectiveTransform)屏幕上的最终位置(mScreenBounds)使用这些变量场景主要是在computeBounds方法进行Layer的bounds计算时候所以只有深入剖析computeBounds才可以更加深入理解这些变量。voidLayer::computeBounds(FloatRect parentBounds,ui::Transform parentTransform,floatparentShadowRadius){constStates(getDrawingState());//当前获取Layer的transform由parentTransform和DrawingState共同相差决定父亲transform会影响子Layer// Calculate effective layer transformmEffectiveTransformparentTransform*getActiveTransform(s);// Transform parent bounds to layer spaceparentBoundsgetActiveTransform(s).inverse().transform(parentBounds);//计算当前Layer sourcebound可以简单认为是自身buffer区域但是没有buffer那就用parentBounds// Calculate source boundsmSourceBoundscomputeSourceBounds(parentBounds);// Calculate bounds by croping diplay frame with layer crop and parent boundsFloatRect boundsmSourceBounds;//获取是否有裁剪constRect layerCropgetCrop(s);if(!layerCrop.isEmpty()){//如果设置了crop裁剪bound那么会与sourceBounds进行相交处理boundsmSourceBounds.intersect(layerCrop.toFloatRect());}//上面计算得出的bounds还需要与parent的Bounds进行相交处理故不可以超过父Layer的bounds大小boundsbounds.intersect(parentBounds);//赋值给Layer的成员变量mBoundsmBoundsbounds;//mBounds只是说显示Layer的bounds大小范围但是显示的坐标是最后确定靠以下的transform确定mScreenBoundsmEffectiveTransform.transform(mBounds);//省略部分//接下来需要调用Layer的孩子分别调用computeBounds进行bounds计算for(constspLayerchild:mDrawingChildren){//可以看到这里直接传递的是mEffectiveTransform作为parentTransformchild-computeBounds(mBounds,mEffectiveTransform,childShadowRadius);}//省略部分}上面注释已经写的比较详细这里对一些方法也进行额外补充解释。看看这里的computeSourceBounds方法FloatRectLayer::computeSourceBounds(constFloatRectparentBounds)const{//这里可以看到对于非buffer的layer是直接使用父亲的比如taskif(mBufferInfo.mBuffernullptr){returnparentBounds;}returngetBufferSize(getDrawingState()).toFloatRect();}可以得出当前Layer只有有buffer的SourceBounds才会自己的buffer的bounds不然都是parentBounds。看完这些代码大家可能已经比较熟悉但是还希望有一个案例可以更加形象的说明就好了。案例举例一、简单案例设定假设屏幕坐标系原点在左上角x向右y向下。父Layer比如是一个全屏的窗口父Layer的变换没有变换即单位矩阵。父Layer的边界整个屏幕假设为 (0, 0, 1000, 1000)。子Layer当前Layer子Layer自身变换没有缩放和旋转只有平移比如向右下角平移(200, 300)。子Layer的Buffer大小内容大小(0, 0, 400, 300)。子Layer没有设置裁剪区域layerCrop为空。二、计算过程步骤1计算有效变换矩阵父变换parentTransform单位矩阵子层自身变换getActiveTransform(s)平移(200,300)mEffectiveTransform 父变换 × 子层自身变换 平移(200,300)步骤2将父边界转换到子层局部坐标系子层自身变换的逆矩阵平移(-200, -300)父边界parentBounds: (0,0,1000,1000)转换后平移(-200,-300).transform( (0,0,1000,1000) ) (-200,-300,800,700)注意这里我们假设变换只影响顶点实际上变换矩阵会作用于每个点但平移变换只是将矩形偏移。步骤3计算源边界mSourceBounds调用computeSourceBounds传入转换后的父边界(-200,-300,800,700)由于子层有Buffer大小为(0,0,400,300)所以mSourceBounds (0,0,400,300)步骤4第一层裁剪Layer自身CroplayerCrop为空所以跳过bounds mSourceBounds (0,0,400,300)步骤5第二层裁剪父边界约束bounds与转换后的父边界相交(0,0,400,300) 与 (-200,-300,800,700) 相交 (0,0,400,300)因为子层的源边界完全在转换后的父边界内所以不变。步骤6得到最终mBoundsmBounds (0,0,400,300)步骤7计算屏幕边界mScreenBoundsmScreenBounds mEffectiveTransform.transform(mBounds) 平移(200,300).transform( (0,0,400,300) ) (200,300,600,600)三、结果解释子Layer在自己的局部坐标系中它的可显示区域是(0,0,400,300)即整个Buffer。在屏幕坐标系中由于子Layer平移了(200,300)所以它的显示区域变为(200,300,600,600)。四、考虑有裁剪的情况假设其他条件不变子Layer设置了一个裁剪区域layerCrop (50,50,350,250) // 注意这个裁剪区域是在子层局部坐标系中那么步骤4变为bounds mSourceBounds.intersect(layerCrop.toFloatRect()) (0,0,400,300) 与 (50,50,350,250) 相交 (50,50,350,250)步骤5bounds与转换后的父边界相交(50,50,350,250) 与 (-200,-300,800,700) 相交 (50,50,350,250)步骤6mBounds (50,50,350,250)步骤7mScreenBounds 平移(200,300).transform( (50,50,350,250) ) (250,350,550,550)这样子Layer在屏幕上只显示了(250,350)到(550,550)的区域即被裁剪后的部分。五、考虑子Layer超出父边界的情况假设子Layer的平移量更大比如平移(900,900)其他条件不变无裁剪。步骤2转换后的父边界平移(-900,-900).transform( (0,0,1000,1000) ) (-900,-900,100,100)步骤3不变mSourceBounds (0,0,400,300)步骤4不变bounds (0,0,400,300)步骤5与转换后的父边界相交(0,0,400,300) 与 (-900,-900,100,100) 相交 (0,0,100,100)注意因为转换后的父边界在子层坐标系中只有(-900,-900)到(100,100)的区域所以相交后只有(0,0,100,100)部分可见。步骤6mBounds (0,0,100,100)步骤7mScreenBounds平移(900,900).transform((0,0,100,100))(900,900,1000,1000)这样子Layer只有左上角(0,0)到(100,100)的部分被显示在屏幕的(900,900)到(1000,1000)区域其余部分被父边界裁剪掉了。六、总结通过这个简单案例我们可以看到computeBounds的核心思想通过逆变换将父边界转换到子层坐标系以便在同一个坐标系中进行裁剪。子层的内容源边界先与自身的裁剪区域相交再与父边界相交得到最终在子层坐标系中的边界mBounds。然后通过有效变换将mBounds变换到屏幕坐标系得到屏幕上的实际显示区域mScreenBounds。这样的设计保证了每一层都只显示在父层允许的范围内并且可以方便地应用自身的裁剪。