Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dfce857 authored by Anton Ivanov's avatar Anton Ivanov Committed by Android (Google) Code Review
Browse files

Merge "Improve error message when invalid display id is passed to screencap...

Merge "Improve error message when invalid display id is passed to screencap with -d argument." into main
parents e36dedd6 e696b74f
Loading
Loading
Loading
Loading
+53 −12
Original line number Diff line number Diff line
@@ -7,25 +7,66 @@ package {
    default_applicable_licenses: ["frameworks_base_license"],
}

cc_binary {
    name: "screencap",
cc_defaults {
    name: "screencap_defaults",

    srcs: ["screencap.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
        "-Wunreachable-code",
        "-Wunused",
    ],

    shared_libs: [
        "libcutils",
        "libutils",
        "libbinder",
        "libjnigraphics",
        "libcutils",
        "libgui",
        "libhwui",
        "libjnigraphics",
        "libui",
        "libgui",
        "libutils",
    ],
}

    cflags: [
        "-Wall",
        "-Werror",
        "-Wunused",
        "-Wunreachable-code",
cc_library {
    name: "libscreencap",

    defaults: [
        "screencap_defaults",
    ],

    srcs: ["screencap_utils.cpp"],
}

cc_binary {
    name: "screencap",

    defaults: [
        "screencap_defaults",
    ],

    srcs: ["screencap.cpp"],

    static_libs: [
        "libscreencap",
    ],
}

cc_test {
    name: "libscreencap_test",

    defaults: [
        "screencap_defaults",
    ],

    test_suites: ["device-tests"],

    srcs: [
        "tests/screencap_test.cpp",
    ],

    static_libs: [
        "libgmock",
        "libscreencap",
    ],
}
+12 −0
Original line number Diff line number Diff line
{
  "presubmit": [
    {
      "name": "libscreencap_test"
    }
  ],
  "hwasan-presubmit": [
    {
      "name": "libscreencap_test"
    }
  ]
}
 No newline at end of file
+9 −27
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@
#include <ui/GraphicTypes.h>
#include <ui/PixelFormat.h>

#include "utils/Errors.h"
#include "screencap_utils.h"

using namespace android;

#define COLORSPACE_UNKNOWN    0
@@ -145,24 +148,6 @@ static status_t notifyMediaScanner(const char* fileName) {
    return NO_ERROR;
}

status_t capture(const DisplayId displayId,
            const gui::CaptureArgs& captureArgs,
            ScreenCaptureResults& outResult) {
    sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
    ScreenshotClient::captureDisplay(displayId, captureArgs, captureListener);

    ScreenCaptureResults captureResults = captureListener->waitForResults();
    if (!captureResults.fenceResult.ok()) {
        fprintf(stderr, "Failed to take screenshot. Status: %d\n",
                fenceStatus(captureResults.fenceResult));
        return 1;
    }

    outResult = captureResults;

    return 0;
}

status_t saveImage(const char* fn, std::optional<AndroidBitmapCompressFormat> format,
                   const ScreenCaptureResults& captureResults) {
    void* base = nullptr;
@@ -428,14 +413,11 @@ int main(int argc, char** argv)
    std::vector<ScreenCaptureResults> results;
    const size_t numDisplays = displaysToCapture.size();
    for (int i = 0; i < numDisplays; i++) {
        ScreenCaptureResults result;

        // 1. Capture the screen
        if (const status_t captureStatus =
            capture(displaysToCapture[i], captureArgs, result) != 0) {

            fprintf(stderr, "Capturing failed.\n");
            return captureStatus;
        auto captureResult = screencap::capture(displaysToCapture[i], captureArgs);
        if (captureResult.error().code() != OK) {
            fprintf(stderr, "%sCapturing failed.\n", captureResult.error().message().c_str());
            return 1;
        }

        // 2. Save the capture result as an image.
@@ -453,7 +435,7 @@ int main(int argc, char** argv)
        if (!filename.empty()) {
            fn = filename.c_str();
        }
        if (const status_t saveImageStatus = saveImage(fn, format, result) != 0) {
        if (const status_t saveImageStatus = saveImage(fn, format, captureResult.value()) != 0) {
            fprintf(stderr, "Saving image failed.\n");
            return saveImageStatus;
        }
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.
 */

#include "screencap_utils.h"

#include "gui/SyncScreenCaptureListener.h"

namespace android::screencap {

base::Result<gui::ScreenCaptureResults> capture(const DisplayId displayId,
                                                const gui::CaptureArgs& captureArgs) {
    sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
    auto captureDisplayStatus =
            ScreenshotClient::captureDisplay(displayId, captureArgs, captureListener);

    gui::ScreenCaptureResults captureResults = captureListener->waitForResults();
    if (!captureResults.fenceResult.ok()) {
        status_t captureStatus = fenceStatus(captureResults.fenceResult);
        std::stringstream errorMsg;
        errorMsg << "Failed to take take screenshot. ";
        if (captureStatus == NAME_NOT_FOUND) {
            errorMsg << "Display Id '" << displayId.value << "' is not valid.\n";
        }
        return base::ResultError(errorMsg.str(), captureStatus);
    }

    return captureResults;
}

} // namespace android::screencap
 No newline at end of file
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.
 */

#include <android-base/result.h>
#include <android/gui/DisplayCaptureArgs.h>

#include "gui/ScreenCaptureResults.h"
#include "ui/DisplayId.h"

#pragma once

namespace android::screencap {
base::Result<gui::ScreenCaptureResults> capture(const DisplayId displayId,
                                                const gui::CaptureArgs& captureArgs);
} // namespace android::screencap
Loading