void doRenderFrame()

in car_app_library/navigation/common/src/main/java/androidx/car/app/sample/navigation/common/car/SurfaceRenderer.java [280:405]


    void doRenderFrame() {
        if (mSurface == null || !mSurface.isValid()) {
            // Surface is not available, or has been destroyed, skip this frame.
            return;
        }
        Canvas canvas = mSurface.lockCanvas(null);

        // Clear the background.
        canvas.drawColor(mCarContext.isDarkMode() ? Color.DKGRAY : Color.LTGRAY);

        // Initialize the background map.
        if (mBackgroundMapMatrix.isIdentity()) {
            // Enlarge the original image.
            RectF backgroundRect = new RectF(0, 0, mBackgroundMap.getWidth(),
                    mBackgroundMap.getHeight());
            RectF scaledBackgroundRect = new RectF(0, 0,
                    backgroundRect.width() * MAP_ENLARGE_FACTOR,
                    backgroundRect.height() * MAP_ENLARGE_FACTOR);

            // Initialize the cumulative scale factor and map center points.
            mCumulativeScaleFactor = 1f;
            mBackgroundMapCenterX = scaledBackgroundRect.centerX();
            mBackgroundMapCenterY = scaledBackgroundRect.centerY();

            // Move to the center of the enlarged map.
            mBackgroundMapMatrix.setRectToRect(backgroundRect, scaledBackgroundRect,
                    Matrix.ScaleToFit.FILL);
            mBackgroundMapMatrix.postTranslate(
                    -mBackgroundMapCenterX + canvas.getClipBounds().centerX(),
                    -mBackgroundMapCenterY + canvas.getClipBounds().centerY());
            scaledBackgroundRect.round(mBackgroundMapClipBounds);
        }
        canvas.drawBitmap(mBackgroundMap, mBackgroundMapMatrix, null);


        final int horizontalTextMargin = 10;
        final int verticalTextMarginFromTop = 20;
        final int verticalTextMarginFromBottom = 10;

        // Draw a rectangle showing the inset.
        Rect visibleArea = mVisibleArea;
        if (visibleArea != null) {
            if (visibleArea.isEmpty()) {
                // No inset set. The entire area is considered safe to draw.
                visibleArea.set(0, 0, canvas.getWidth() - 1, canvas.getHeight() - 1);
            }

            canvas.drawRect(visibleArea, mLeftInsetPaint);
            canvas.drawLine(
                    visibleArea.left,
                    visibleArea.top,
                    visibleArea.right,
                    visibleArea.bottom,
                    mLeftInsetPaint);
            canvas.drawLine(
                    visibleArea.right,
                    visibleArea.top,
                    visibleArea.left,
                    visibleArea.bottom,
                    mLeftInsetPaint);
            canvas.drawText(
                    "(" + visibleArea.left + " , " + visibleArea.top + ")",
                    visibleArea.left + horizontalTextMargin,
                    visibleArea.top + verticalTextMarginFromTop,
                    mLeftInsetPaint);
            canvas.drawText(
                    "(" + visibleArea.right + " , " + visibleArea.bottom + ")",
                    visibleArea.right - horizontalTextMargin,
                    visibleArea.bottom - verticalTextMarginFromBottom,
                    mRightInsetPaint);

            // Draw location on the top right corner of the screen.
            canvas.drawText(
                    "(" + mLocationString + ")",
                    visibleArea.right - horizontalTextMargin,
                    visibleArea.top + verticalTextMarginFromTop,
                    mRightInsetPaint);
        } else {
            Log.d(TAG, "Visible area not available.");
        }

        if (mStableArea != null) {
            // Draw a cross-hairs at the stable center.
            final int lengthPx = 15;
            int centerX = mStableArea.centerX();
            int centerY = mStableArea.centerY();
            canvas.drawLine(centerX - lengthPx, centerY, centerX + lengthPx, centerY, mCenterPaint);
            canvas.drawLine(centerX, centerY - lengthPx, centerX, centerY + lengthPx, mCenterPaint);
            canvas.drawText(
                    "(" + centerX + ", " + centerY + ")",
                    centerX + horizontalTextMargin,
                    centerY,
                    mCenterPaint);
        } else {
            Log.d(TAG, "Stable area not available.");
        }

        if (mShowMarkers) {
            // Show a set number of markers centered around the midpoint of the stable area. If no
            // stable area, then use visible area or canvas dimensions. If an active marker is set
            // draw
            // a line from the center to that marker.
            Rect markerArea =
                    mStableArea != null
                            ? mStableArea
                            : (mVisibleArea != null
                                    ? mVisibleArea
                                    : new Rect(0, 0, canvas.getWidth() - 1, canvas.getHeight()));
            int centerX = markerArea.centerX();
            int centerY = markerArea.centerY();
            double radius = Math.min(centerX / 2, centerY / 2);

            double circleAngle = 2.0d * Math.PI;
            double markerpiece = circleAngle / mNumMarkers;
            for (int i = 0; i < mNumMarkers; i++) {
                int markerX = centerX + (int) (radius * Math.cos(markerpiece * i));
                int markerY = centerY + (int) (radius * Math.sin(markerpiece * i));
                canvas.drawCircle(markerX, markerY, 5, mMarkerPaint);
                if (i == mActiveMarker) {
                    canvas.drawLine(centerX, centerY, markerX, markerY, mMarkerPaint);
                }
            }
        }

        mSurface.unlockCanvasAndPost(canvas);
    }