• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Performance Improvement Features (for System Applications Only)
2
3The camera startup performance is affected by time-consuming operations such as power-on of underlying components and initialization of the process pipeline. To improve the camera startup speed and thumbnail display speed, OpenHarmony introduces some features. The capabilities of these features are related to underlying components. You need to check whether your underlying components support these capabilities before using the capabilities.
4
5These features are involved in the processes of starting the camera device, configuring streams, and taking photos. This topic describes the three scenarios.
6
7## Deferred Stream Configuration
8
9A typical camera startup process includes starting the camera device, configuring a data stream, and starting the data stream. Before configuring the data stream, you need to obtain the surface ID of the **\<XComponent>**.
10
11The deferred stream configuration feature decouples stream configuration and start from the surface. Before the **\<XComponent>** provides the surface for the camera application, the system configures and starts the stream. This way, the surface only needs to be available before the stream is started. This improves the startup speed and prevents the implementation of other startup optimization schemas from being affected.
12
13![deferred-surface-scene](figures/deferred-surface-scene.png)
14
15Before optimization: Stream configuration depends on a **Surface** object, which is available after UI loading is complete. In other words, you can create a session, configure input and output streams, and start the session only after the UI is loaded. The camera HDI is responsible for stream configuration.
16
17After optimization: Stream configuration does not depend on the **Surface** object. UI loading and stream configuration are executed concurrently. After the parameters are prepared, you can create a session.
18
19### Available APIs
20
21Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
22
23| API| Description|
24| ---- | ---- |
25| createDeferredPreviewOutput(profile: Profile): Promise\<PreviewOutput> | Creates a deferred **PreviewOutput** instance and adds it, instead of a common **PreviewOutput** instance, to the data stream during stream configuration. |
26| addDeferredSurface(surfaceId: string): Promise\<void> | Adds a surface for delayed preview. This API can run after **session.commitConfig()** or **session.start()** is called.|
27
28### Development Example
29
30The figure below shows the recommended API call process.
31
32![](figures/deferred-surface-sequence-diagram.png)
33
34There are different [types of contexts](../application-models/application-context-stage.md).
35
36```ts
37import camera from '@ohos.multimedia.camera';
38import featureAbility from '@ohos.ability.featureAbility';
39
40async function preview(context: featureAbility.Context, cameraInfo: camera.CameraDevice, previewProfile: camera.Profile, photoProfile: camera.Profile, photoSurfaceId: string, previewSurfaceId: string): Promise<void> {
41  const cameraManager: camera.CameraManager = camera.getCameraManager(context);
42  const cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameraInfo);
43  const previewOutput: camera.PreviewOutput = cameraManager.createDeferredPreviewOutput(previewProfile);
44  const photoOutput: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, photoSurfaceId);
45  const session: camera.CaptureSession  = cameraManager.createCaptureSession();
46  session.beginConfig();
47  session.addInput(cameraInput);
48  session.addOutput(previewOutput);
49  session.addOutput(photoOutput);
50  await session.commitConfig();
51  await session.start();
52  previewOutput.addDeferredSurface(previewSurfaceId);
53}
54```
55
56## Quick Thumbnail
57
58The photographing performance depends on the algorithm processing speed. A complex algorithm chain provides better image effect while requiring longer processing time.
59
60To improve the photographing speed perceived by end users, the quick thumbnail feature is introduced. When the user takes a photo, a thumbnail is output and reported to the camera application for display before a real image is reported.
61
62In this way, the photographing process is optimized, which fulfills the processing requirements of the post-processing algorithm without blocking the photographing speed of the foreground.
63
64### Available APIs
65
66Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
67
68| API| Description|
69| ---- | ---- |
70| isQuickThumbnailSupported() : boolean | Checks whether the quick thumbnail feature is supported.|
71| enableQuickThumbnail(enabled:bool): void | Enables or disables the quick thumbnail feature.|
72| on(type: 'quickThumbnail', callback: AsyncCallback\<image.PixelMap>): void | Listens for camera thumbnails.|
73
74> **NOTE**
75>
76> - **isQuickThumbnailSupported** and **enableQuickThumbnail** must be called after **CaptureSession.addOutput** and **CaptureSession.addInput** but before **CaptureSession.commitConfig()**.
77> - **on()** takes effect after **enableQuickThumbnail(true)** is called.
78
79### Development Example
80
81The figure below shows the recommended API call process.
82
83![](figures/quick-thumbnail-sequence-diagram.png)
84
85There are different [types of contexts](../application-models/application-context-stage.md).
86```ts
87import camera from '@ohos.multimedia.camera';
88import { BusinessError } from '@ohos.base';
89import image from '@ohos.multimedia.image';
90import featureAbility from '@ohos.ability.featureAbility';
91
92async function enableQuickThumbnail(context: featureAbility.Context, surfaceId: string, photoProfile: camera.Profile): Promise<void> {
93  let cameraManager: camera.CameraManager = camera.getCameraManager(context);
94  let cameras: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
95  // Create a CaptureSession instance.
96  let captureSession: camera.CaptureSession = cameraManager.createCaptureSession();
97  // Start configuration for the session.
98  captureSession.beginConfig();
99  // Add a CameraInput instance to the session.
100  let cameraInput: camera.CameraInput = cameraManager.createCameraInput(cameras[0]);
101  cameraInput.open();
102  captureSession.addInput(cameraInput);
103  // Add a PhotoOutput instance to the session.
104  let photoOutPut: camera.PhotoOutput = cameraManager.createPhotoOutput(photoProfile, surfaceId);
105  captureSession.addOutput(photoOutPut);
106  let isSupported: boolean = photoOutPut.isQuickThumbnailSupported();
107  if (isSupported) {
108    // Enable the quick thumbnail feature.
109    photoOutPut.enableQuickThumbnail(true);
110    photoOutPut.on('quickThumbnail', (err: BusinessError, pixelMap: image.PixelMap) => {
111      if (err || pixelMap === undefined) {
112        console.error('photoOutPut on thumbnail failed');
113        return;
114      }
115      // Display or save the PixelMap instance.
116      showOrSavePicture(pixelMap);
117    });
118  }
119}
120
121function showOrSavePicture(pixelMap: image.PixelMap): void {
122  //do something
123}
124```
125
126## Prelaunch
127
128Generally, the startup of the camera application is triggered when the user touches the camera icon on the home screen. The home screen senses the touch event and instructs the application manager to start the camera application. This takes a relatively long time. After the camera application is started, the camera startup process starts. A typical camera startup process includes starting the camera device, configuring a data stream, and starting the data stream, which is also time-consuming.
129
130The prelaunch feature triggers the action of starting the camera device before the camera application is started. In other words, when the user touches the camera icon on the home screen, the system starts the camera device. At this time, the camera application is not started yet. The figure below shows the camera application process before and after the prelaunch feature is introduced.
131
132![prelaunch-scene](figures/prelaunch-scene.png)
133
134### Available APIs
135
136Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
137
138| API| Description|
139| ---- | ---- |
140| isPrelaunchSupported(camera: CameraDevice) : boolean |  Checks whether the camera supports prelaunch.|
141| setPrelaunchConfig(prelaunchConfig: PrelaunchConfig) : void | Sets the prelaunch parameters.|
142| prelaunch() : void | Prelaunches the camera. This API is called when a user touches the system camera icon to start the camera application. |
143
144### Development Example
145
146The figure below shows the recommended API call process.
147
148![](figures/prelaunch-sequence-diagram.png)
149
150There are different [types of contexts](../application-models/application-context-stage.md).
151
152- **Home screen**
153
154  ```ts
155  import camera from '@ohos.multimedia.camera';
156  import { BusinessError } from '@ohos.base';
157  import featureAbility from '@ohos.ability.featureAbility';
158
159  function preLaunch(context: featureAbility.Context): void {
160    let cameraManager: camera.CameraManager = camera.getCameraManager(context);
161    try {
162      cameraManager.prelaunch();
163    } catch (error) {
164      let err = error as BusinessError;
165      console.error(`catch error: Code: ${err.code}, message: ${err.message}`);
166    }
167  }
168  ```
169
170- **Camera application**
171
172  To use the prelaunch feature, the camera application must configure the **ohos.permission.CAMERA** permission.
173
174  For details about how to request and verify the permissions, see [Permission Application Guide](../security/accesstoken-guidelines.md).
175
176  ```ts
177  import camera from '@ohos.multimedia.camera';
178  import { BusinessError } from '@ohos.base';
179  import featureAbility from '@ohos.ability.featureAbility';
180
181  function setPreLaunchConfig(context: featureAbility.Context): void {
182    let cameraManager: camera.CameraManager = camera.getCameraManager(context);
183    let cameras: Array<camera.CameraDevice> = [];
184    try {
185      cameras = cameraManager.getSupportedCameras();
186    } catch (error) {
187      let err = error as BusinessError;
188      console.error(`getSupportedCameras catch error: Code: ${err.code}, message: ${err.message}`);
189    }
190    if (cameras.length <= 0) {
191      return;
192    }
193    if(cameraManager.isPrelaunchSupported(cameras[0])) {
194      try {
195        cameraManager.setPrelaunchConfig({cameraDevice: cameras[0]});
196      } catch (error) {
197        let err = error as BusinessError;
198        console.error(`setPrelaunchConfig catch error: Code: ${err.code}, message: ${err.message}`);
199      }
200    }
201  }
202  ```
203