OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 | 9 |
10 #include "SkDifferentPixelsMetric.h" | 10 #include "SkDifferentPixelsMetric.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 return false; | 65 return false; |
66 } | 66 } |
67 | 67 |
68 // A small hack that makes calculating percentage difference easier later on
. | 68 // A small hack that makes calculating percentage difference easier later on
. |
69 result->result = 1.0 / ((double)baseline->width() * baseline->height()); | 69 result->result = 1.0 / ((double)baseline->width() * baseline->height()); |
70 | 70 |
71 // Make a buffer to store results into. It must be initialized with pointers
to memory. | 71 // Make a buffer to store results into. It must be initialized with pointers
to memory. |
72 static const int kZero = 0; | 72 static const int kZero = 0; |
73 // We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR | 73 // We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR |
74 resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOS
T_PTR, | 74 resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOS
T_PTR, |
75 sizeof(int), (int*)&kZero, NULL); | 75 sizeof(int), (int*)&kZero, nullptr); |
76 | 76 |
77 // Set all kernel arguments | 77 // Set all kernel arguments |
78 cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &baselineImage
); | 78 cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &baselineImage
); |
79 setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &testImage); | 79 setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &testImage); |
80 setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &resultsBuffer
); | 80 setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &resultsBuffer
); |
81 if (CL_SUCCESS != setArgErr) { | 81 if (CL_SUCCESS != setArgErr) { |
82 SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr)); | 82 SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr)); |
83 return false; | 83 return false; |
84 } | 84 } |
85 | 85 |
86 // Queue this diff on the CL device | 86 // Queue this diff on the CL device |
87 cl_event event; | 87 cl_event event; |
88 const size_t workSize[] = { baseline->width(), baseline->height() }; | 88 const size_t workSize[] = { baseline->width(), baseline->height() }; |
89 cl_int enqueueErr; | 89 cl_int enqueueErr; |
90 enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, NULL, workSiz
e, | 90 enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, nullptr, work
Size, |
91 NULL, 0, NULL, &event); | 91 nullptr, 0, nullptr, &event); |
92 if (CL_SUCCESS != enqueueErr) { | 92 if (CL_SUCCESS != enqueueErr) { |
93 SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr)); | 93 SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr)); |
94 return false; | 94 return false; |
95 } | 95 } |
96 | 96 |
97 // This makes things totally synchronous. Actual queue is not ready yet | 97 // This makes things totally synchronous. Actual queue is not ready yet |
98 clWaitForEvents(1, &event); | 98 clWaitForEvents(1, &event); |
99 | 99 |
100 // Immediate read back the results | 100 // Immediate read back the results |
101 clEnqueueReadBuffer(fCommandQueue, resultsBuffer, CL_TRUE, 0, | 101 clEnqueueReadBuffer(fCommandQueue, resultsBuffer, CL_TRUE, 0, |
102 sizeof(int), &result->poiCount, 0, NULL, NULL); | 102 sizeof(int), &result->poiCount, 0, nullptr, nullptr); |
103 result->result *= (double)result->poiCount; | 103 result->result *= (double)result->poiCount; |
104 result->result = (1.0 - result->result); | 104 result->result = (1.0 - result->result); |
105 | 105 |
106 // Release all the buffers created | 106 // Release all the buffers created |
107 clReleaseMemObject(resultsBuffer); | 107 clReleaseMemObject(resultsBuffer); |
108 clReleaseMemObject(baselineImage); | 108 clReleaseMemObject(baselineImage); |
109 clReleaseMemObject(testImage); | 109 clReleaseMemObject(testImage); |
110 | 110 |
111 result->timeElapsed = get_seconds() - startTime; | 111 result->timeElapsed = get_seconds() - startTime; |
112 return true; | 112 return true; |
113 } | 113 } |
114 | 114 |
115 bool SkDifferentPixelsMetric::onInit() { | 115 bool SkDifferentPixelsMetric::onInit() { |
116 if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel))
{ | 116 if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel))
{ |
117 return false; | 117 return false; |
118 } | 118 } |
119 | 119 |
120 return true; | 120 return true; |
121 } | 121 } |
OLD | NEW |