xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/differ_block.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "modules/desktop_capture/differ_block.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <string.h>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/arch.h"
16*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/cpu_features_wrapper.h"
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker // This needs to be after rtc_base/system/arch.h which defines
19*d9f75844SAndroid Build Coastguard Worker // architecture macros.
20*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_ARCH_X86_FAMILY)
21*d9f75844SAndroid Build Coastguard Worker #include "modules/desktop_capture/differ_vector_sse2.h"
22*d9f75844SAndroid Build Coastguard Worker #endif
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
25*d9f75844SAndroid Build Coastguard Worker 
26*d9f75844SAndroid Build Coastguard Worker namespace {
27*d9f75844SAndroid Build Coastguard Worker 
VectorDifference_C(const uint8_t * image1,const uint8_t * image2)28*d9f75844SAndroid Build Coastguard Worker bool VectorDifference_C(const uint8_t* image1, const uint8_t* image2) {
29*d9f75844SAndroid Build Coastguard Worker   return memcmp(image1, image2, kBlockSize * kBytesPerPixel) != 0;
30*d9f75844SAndroid Build Coastguard Worker }
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker }  // namespace
33*d9f75844SAndroid Build Coastguard Worker 
VectorDifference(const uint8_t * image1,const uint8_t * image2)34*d9f75844SAndroid Build Coastguard Worker bool VectorDifference(const uint8_t* image1, const uint8_t* image2) {
35*d9f75844SAndroid Build Coastguard Worker   static bool (*diff_proc)(const uint8_t*, const uint8_t*) = nullptr;
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker   if (!diff_proc) {
38*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_ARCH_X86_FAMILY)
39*d9f75844SAndroid Build Coastguard Worker     bool have_sse2 = GetCPUInfo(kSSE2) != 0;
40*d9f75844SAndroid Build Coastguard Worker     // For x86 processors, check if SSE2 is supported.
41*d9f75844SAndroid Build Coastguard Worker     if (have_sse2 && kBlockSize == 32) {
42*d9f75844SAndroid Build Coastguard Worker       diff_proc = &VectorDifference_SSE2_W32;
43*d9f75844SAndroid Build Coastguard Worker     } else if (have_sse2 && kBlockSize == 16) {
44*d9f75844SAndroid Build Coastguard Worker       diff_proc = &VectorDifference_SSE2_W16;
45*d9f75844SAndroid Build Coastguard Worker     } else {
46*d9f75844SAndroid Build Coastguard Worker       diff_proc = &VectorDifference_C;
47*d9f75844SAndroid Build Coastguard Worker     }
48*d9f75844SAndroid Build Coastguard Worker #else
49*d9f75844SAndroid Build Coastguard Worker     // For other processors, always use C version.
50*d9f75844SAndroid Build Coastguard Worker     // TODO(hclam): Implement a NEON version.
51*d9f75844SAndroid Build Coastguard Worker     diff_proc = &VectorDifference_C;
52*d9f75844SAndroid Build Coastguard Worker #endif
53*d9f75844SAndroid Build Coastguard Worker   }
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker   return diff_proc(image1, image2);
56*d9f75844SAndroid Build Coastguard Worker }
57*d9f75844SAndroid Build Coastguard Worker 
BlockDifference(const uint8_t * image1,const uint8_t * image2,int height,int stride)58*d9f75844SAndroid Build Coastguard Worker bool BlockDifference(const uint8_t* image1,
59*d9f75844SAndroid Build Coastguard Worker                      const uint8_t* image2,
60*d9f75844SAndroid Build Coastguard Worker                      int height,
61*d9f75844SAndroid Build Coastguard Worker                      int stride) {
62*d9f75844SAndroid Build Coastguard Worker   for (int i = 0; i < height; i++) {
63*d9f75844SAndroid Build Coastguard Worker     if (VectorDifference(image1, image2)) {
64*d9f75844SAndroid Build Coastguard Worker       return true;
65*d9f75844SAndroid Build Coastguard Worker     }
66*d9f75844SAndroid Build Coastguard Worker     image1 += stride;
67*d9f75844SAndroid Build Coastguard Worker     image2 += stride;
68*d9f75844SAndroid Build Coastguard Worker   }
69*d9f75844SAndroid Build Coastguard Worker   return false;
70*d9f75844SAndroid Build Coastguard Worker }
71*d9f75844SAndroid Build Coastguard Worker 
BlockDifference(const uint8_t * image1,const uint8_t * image2,int stride)72*d9f75844SAndroid Build Coastguard Worker bool BlockDifference(const uint8_t* image1, const uint8_t* image2, int stride) {
73*d9f75844SAndroid Build Coastguard Worker   return BlockDifference(image1, image2, kBlockSize, stride);
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
77