summaryrefslogtreecommitdiff
path: root/chromium/services/shape_detection/detection_utils_mac.h
blob: 52be0cc89c21a1c49ba06c839bd71a8119bc8646 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_SHAPE_DETECTION_DETECTION_UTILS_MAC_H_
#define SERVICES_SHAPE_DETECTION_DETECTION_UTILS_MAC_H_

#import <CoreImage/CoreImage.h>
#import <Foundation/Foundation.h>
#import <Vision/Vision.h>
#include <os/availability.h>

#include <memory>

#include "base/callback.h"
#include "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/rect_f.h"

namespace shape_detection {

// Takes a ScopedSharedBufferHandle with dimensions and produces a new CIImage
// with the same contents, or a null scoped_nsobject is something goes wrong.
base::scoped_nsobject<CIImage> CreateCIImageFromSkBitmap(
    const SkBitmap& bitmap);

gfx::RectF ConvertCGToGfxCoordinates(CGRect bounds, int height);

// This class submits an image analysis request for asynchronous execution on a
// dispatch queue with default priority.
class API_AVAILABLE(macos(10.13)) VisionAPIAsyncRequestMac {
 public:
  // A callback run when the asynchronous execution completes. The callback is
  // repeating for the instance.
  using Callback =
      base::RepeatingCallback<void(VNRequest* request, NSError* error)>;

  VisionAPIAsyncRequestMac(const VisionAPIAsyncRequestMac&) = delete;
  VisionAPIAsyncRequestMac& operator=(const VisionAPIAsyncRequestMac&) = delete;

  ~VisionAPIAsyncRequestMac();

  // Creates an VisionAPIAsyncRequestMac instance which sets |callback| to be
  // called when the asynchronous action completes.
  static std::unique_ptr<VisionAPIAsyncRequestMac> Create(
      Class request_class,
      Callback callback,
      NSArray<VNBarcodeSymbology>* symbology_hints = nullptr);

  // Processes asynchronously an image analysis request and returns results with
  // |callback_| when the asynchronous request completes, the callers should
  // only enqueue one request at a time.
  bool PerformRequest(const SkBitmap& bitmap);

 private:
  VisionAPIAsyncRequestMac(Callback callback,
                           Class request_class,
                           NSArray<VNBarcodeSymbology>* symbology_hints);

  base::scoped_nsobject<VNRequest> request_;
  const Callback callback_;
};

}  // namespace shape_detection

#endif  // SERVICES_SHAPE_DETECTION_DETECTION_UTILS_MAC_H_