summaryrefslogtreecommitdiff
path: root/core/java/com/android/internal/view/ScrollCaptureViewSupport.java
blob: 9e09006f608d65b0209a8ee976b290e6cf276238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.view;

import android.annotation.UiThread;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.HardwareRenderer;
import android.graphics.Matrix;
import android.graphics.RecordingCanvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.RenderNode;
import android.os.CancellationSignal;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display.ColorMode;
import android.view.ScrollCaptureCallback;
import android.view.ScrollCaptureSession;
import android.view.Surface;
import android.view.View;

import com.android.internal.view.ScrollCaptureViewHelper.ScrollResult;

import java.lang.ref.WeakReference;
import java.util.function.Consumer;

/**
 * Provides a base ScrollCaptureCallback implementation to handle arbitrary View-based scrolling
 * containers. This class handles the bookkeeping aspects of {@link ScrollCaptureCallback}
 * including rendering output using HWUI. Adaptable to any {@link View} using
 * {@link ScrollCaptureViewHelper}.
 *
 * @param <V> the specific View subclass handled
 * @see ScrollCaptureViewHelper
 */
@UiThread
public class ScrollCaptureViewSupport<V extends View> implements ScrollCaptureCallback {

    private static final String TAG = "ScrollCaptureViewSupport";

    private static final String SETTING_CAPTURE_DELAY = "screenshot.scroll_capture_delay";
    private static final long SETTING_CAPTURE_DELAY_DEFAULT = 60L; // millis

    private final WeakReference<V> mWeakView;
    private final ScrollCaptureViewHelper<V> mViewHelper;
    private final ViewRenderer mRenderer;
    private final long mPostScrollDelayMillis;

    private boolean mStarted;
    private boolean mEnded;

    ScrollCaptureViewSupport(V containingView, ScrollCaptureViewHelper<V> viewHelper) {
        mWeakView = new WeakReference<>(containingView);
        mRenderer = new ViewRenderer();
        // TODO(b/177649144): provide access to color space from android.media.Image
        mViewHelper = viewHelper;
        Context context = containingView.getContext();
        ContentResolver contentResolver = context.getContentResolver();
        mPostScrollDelayMillis = Settings.Global.getLong(contentResolver,
                SETTING_CAPTURE_DELAY, SETTING_CAPTURE_DELAY_DEFAULT);
    }

    /** Based on ViewRootImpl#updateColorModeIfNeeded */
    @ColorMode
    private static int getColorMode(View containingView) {
        Context context = containingView.getContext();
        int colorMode = containingView.getViewRootImpl().mWindowAttributes.getColorMode();
        if (!context.getResources().getConfiguration().isScreenWideColorGamut()) {
            colorMode = ActivityInfo.COLOR_MODE_DEFAULT;
        }
        return colorMode;
    }

    @Override
    public final void onScrollCaptureSearch(CancellationSignal signal, Consumer<Rect> onReady) {
        if (signal.isCanceled()) {
            return;
        }
        V view = mWeakView.get();
        mStarted = false;
        mEnded = false;

        if (view != null && view.isVisibleToUser() && mViewHelper.onAcceptSession(view)) {
            onReady.accept(mViewHelper.onComputeScrollBounds(view));
            return;
        }
        onReady.accept(null);
    }

    @Override
    public final void onScrollCaptureStart(ScrollCaptureSession session, CancellationSignal signal,
            Runnable onReady) {
        if (signal.isCanceled()) {
            return;
        }
        V view = mWeakView.get();

        mEnded = false;
        mStarted = true;

        // Note: If somehow the view is already gone or detached, the first call to
        // {@code onScrollCaptureImageRequest} will return an error and request the session to
        // end.
        if (view != null && view.isVisibleToUser()) {
            mRenderer.setSurface(session.getSurface());
            mViewHelper.onPrepareForStart(view, session.getScrollBounds());
        }
        onReady.run();
    }

    @Override
    public final void onScrollCaptureImageRequest(ScrollCaptureSession session,
            CancellationSignal signal, Rect requestRect, Consumer<Rect> onComplete) {
        if (signal.isCanceled()) {
            Log.w(TAG, "onScrollCaptureImageRequest: cancelled!");
            return;
        }

        V view = mWeakView.get();
        if (view == null || !view.isVisibleToUser()) {
            // Signal to the controller that we have a problem and can't continue.
            onComplete.accept(new Rect());
            return;
        }

        // Ask the view to scroll as needed to bring this area into view.
        ScrollResult scrollResult = mViewHelper.onScrollRequested(view, session.getScrollBounds(),
                requestRect);

        if (scrollResult.availableArea.isEmpty()) {
            onComplete.accept(scrollResult.availableArea);
            return;
        }

        // For image capture, shift back by scrollDelta to arrive at the location within the view
        // where the requested content will be drawn
        Rect viewCaptureArea = new Rect(scrollResult.availableArea);
        viewCaptureArea.offset(0, -scrollResult.scrollDelta);

        Runnable captureAction = () -> {
            if (signal.isCanceled()) {
                Log.w(TAG, "onScrollCaptureImageRequest: cancelled! skipping render.");
            } else {
                mRenderer.renderView(view, viewCaptureArea);
                onComplete.accept(new Rect(scrollResult.availableArea));
            }
        };

        view.postOnAnimationDelayed(captureAction, mPostScrollDelayMillis);
    }

    @Override
    public final void onScrollCaptureEnd(Runnable onReady) {
        V view = mWeakView.get();
        if (mStarted && !mEnded) {
            if (view != null) {
                mViewHelper.onPrepareForEnd(view);
                view.invalidate();
            }
            mEnded = true;
            mRenderer.destroy();
        }
        onReady.run();
    }

    /**
     * Internal helper class which assists in rendering sections of the view hierarchy relative to a
     * given view.
     */
    static final class ViewRenderer {
        // alpha, "reasonable default" from Javadoc
        private static final float AMBIENT_SHADOW_ALPHA = 0.039f;
        private static final float SPOT_SHADOW_ALPHA = 0.039f;

        // Default values:
        //    lightX = (screen.width() / 2) - windowLeft
        //    lightY = 0 - windowTop
        //    lightZ = 600dp
        //    lightRadius = 800dp
        private static final float LIGHT_Z_DP = 400;
        private static final float LIGHT_RADIUS_DP = 800;
        private static final String TAG = "ViewRenderer";

        private final HardwareRenderer mRenderer;
        private final RenderNode mCaptureRenderNode;
        private final Rect mTempRect = new Rect();
        private final int[] mTempLocation = new int[2];
        private long mLastRenderedSourceDrawingId = -1;
        private Surface mSurface;

        ViewRenderer() {
            mRenderer = new HardwareRenderer();
            mRenderer.setName("ScrollCapture");
            mCaptureRenderNode = new RenderNode("ScrollCaptureRoot");
            mRenderer.setContentRoot(mCaptureRenderNode);

            // TODO: Figure out a way to flip this on when we are sure the source window is opaque
            mRenderer.setOpaque(false);
        }

        public void setSurface(Surface surface) {
            mSurface = surface;
            mRenderer.setSurface(surface);
        }

        /**
         * Cache invalidation check. If the source view is the same as the previous call (which is
         * mostly always the case, then we can skip setting up lighting on each call (for now)
         *
         * @return true if the view changed, false if the view was previously rendered by this class
         */
        private boolean updateForView(View source) {
            if (mLastRenderedSourceDrawingId == source.getUniqueDrawingId()) {
                return false;
            }
            mLastRenderedSourceDrawingId = source.getUniqueDrawingId();
            return true;
        }

        // TODO: may need to adjust lightY based on the virtual canvas position to get
        //       consistent shadow positions across the whole capture. Or possibly just
        //       pull lightZ way back to make shadows more uniform.
        private void setupLighting(View mSource) {
            mLastRenderedSourceDrawingId = mSource.getUniqueDrawingId();
            DisplayMetrics metrics = mSource.getResources().getDisplayMetrics();
            mSource.getLocationOnScreen(mTempLocation);
            final float lightX = metrics.widthPixels / 2f - mTempLocation[0];
            final float lightY = metrics.heightPixels - mTempLocation[1];
            final int lightZ = (int) (LIGHT_Z_DP * metrics.density);
            final int lightRadius = (int) (LIGHT_RADIUS_DP * metrics.density);

            // Enable shadows for elevation/Z
            mRenderer.setLightSourceGeometry(lightX, lightY, lightZ, lightRadius);
            mRenderer.setLightSourceAlpha(AMBIENT_SHADOW_ALPHA, SPOT_SHADOW_ALPHA);
        }

        private void updateRootNode(View source, Rect localSourceRect) {
            final View rootView = source.getRootView();
            transformToRoot(source, localSourceRect, mTempRect);

            mCaptureRenderNode.setPosition(0, 0, mTempRect.width(), mTempRect.height());
            RecordingCanvas canvas = mCaptureRenderNode.beginRecording();
            canvas.enableZ();
            canvas.translate(-mTempRect.left, -mTempRect.top);

            RenderNode rootViewRenderNode = rootView.updateDisplayListIfDirty();
            if (rootViewRenderNode.hasDisplayList()) {
                canvas.drawRenderNode(rootViewRenderNode);
            }
            mCaptureRenderNode.endRecording();
        }

        public void renderView(View view, Rect sourceRect) {
            if (updateForView(view)) {
                setupLighting(view);
            }
            view.invalidate();
            updateRootNode(view, sourceRect);
            HardwareRenderer.FrameRenderRequest request = mRenderer.createRenderRequest();
            long timestamp = System.nanoTime();
            request.setVsyncTime(timestamp);

            // Would be nice to access nextFrameNumber from HwR without having to hold on to Surface
            final long frameNumber = mSurface.getNextFrameNumber();

            // Block until a frame is presented to the Surface
            request.setWaitForPresent(true);

            switch (request.syncAndDraw()) {
                case HardwareRenderer.SYNC_OK:
                case HardwareRenderer.SYNC_REDRAW_REQUESTED:
                    return;

                case HardwareRenderer.SYNC_FRAME_DROPPED:
                    Log.e(TAG, "syncAndDraw(): SYNC_FRAME_DROPPED !");
                    break;
                case HardwareRenderer.SYNC_LOST_SURFACE_REWARD_IF_FOUND:
                    Log.e(TAG, "syncAndDraw(): SYNC_LOST_SURFACE !");
                    break;
                case HardwareRenderer.SYNC_CONTEXT_IS_STOPPED:
                    Log.e(TAG, "syncAndDraw(): SYNC_CONTEXT_IS_STOPPED !");
                    break;
            }
        }

        public void trimMemory() {
            mRenderer.clearContent();
        }

        public void destroy() {
            mSurface = null;
            mRenderer.destroy();
        }

        private void transformToRoot(View local, Rect localRect, Rect outRect) {
            local.getLocationInWindow(mTempLocation);
            outRect.set(localRect);
            outRect.offset(mTempLocation[0], mTempLocation[1]);
        }

        public void setColorMode(@ColorMode int colorMode) {
            mRenderer.setColorMode(colorMode);
        }
    }

    @Override
    public String toString() {
        return "ScrollCaptureViewSupport{"
                + "view=" + mWeakView.get()
                + ", helper=" + mViewHelper
                + '}';
    }
}