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" |
11 #include "skpdiff_util.h" | 11 #include "skpdiff_util.h" |
12 | 12 |
13 static const char kDifferentPixelsKernelSource[] = | 13 static const char kDifferentPixelsKernelSource[] = |
mtklein
2013/11/08 15:06:13
Out of curiosity, do you see us realistically usin
djsollen
2013/11/12 16:40:43
I don't want to blow it away yet, until I can do s
| |
14 "#pragma OPENCL_EXTENSION cl_khr_global_int32_base_atomics \n" | 14 "#pragma OPENCL_EXTENSION cl_khr_global_int32_base_atomics \n" |
15 " \n" | 15 " \n" |
16 "const sampler_t gInSampler = CLK_NORMALIZED_COORDS_FALSE | \n" | 16 "const sampler_t gInSampler = CLK_NORMALIZED_COORDS_FALSE | \n" |
17 " CLK_ADDRESS_CLAMP_TO_EDGE | \n" | 17 " CLK_ADDRESS_CLAMP_TO_EDGE | \n" |
18 " CLK_FILTER_NEAREST; \n" | 18 " CLK_FILTER_NEAREST; \n" |
19 " \n" | 19 " \n" |
20 "__kernel void diff(read_only image2d_t baseline, read_only image2d_t test, \n" | 20 "__kernel void diff(read_only image2d_t baseline, read_only image2d_t test, \n" |
21 " __global int* result, __global int2* poi) { \n" | 21 " __global int* result) { \n" |
22 " int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n" | 22 " int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n" |
23 " uint4 baselinePixel = read_imageui(baseline, gInSampler, coord); \n" | 23 " uint4 baselinePixel = read_imageui(baseline, gInSampler, coord); \n" |
24 " uint4 testPixel = read_imageui(test, gInSampler, coord); \n" | 24 " uint4 testPixel = read_imageui(test, gInSampler, coord); \n" |
25 " if (baselinePixel.x != testPixel.x || \n" | 25 " if (baselinePixel.x != testPixel.x || \n" |
26 " baselinePixel.y != testPixel.y || \n" | 26 " baselinePixel.y != testPixel.y || \n" |
27 " baselinePixel.z != testPixel.z || \n" | 27 " baselinePixel.z != testPixel.z || \n" |
28 " baselinePixel.w != testPixel.w) { \n" | 28 " baselinePixel.w != testPixel.w) { \n" |
29 " \n" | 29 " \n" |
30 " int poiIndex = atomic_inc(result); \n" | 30 " atomic_inc(result); \n" |
31 " poi[poiIndex] = coord; \n" | 31 " // TODO: generate alpha mask \n" |
32 " } \n" | 32 " } \n" |
33 "} \n"; | 33 "} \n"; |
34 | 34 |
35 struct SkDifferentPixelsMetric::QueuedDiff { | |
36 bool finished; | |
37 double result; | |
38 int numDiffPixels; | |
39 SkIPoint* poi; | |
40 cl_mem baseline; | |
41 cl_mem test; | |
42 cl_mem resultsBuffer; | |
43 cl_mem poiBuffer; | |
44 }; | |
45 | |
46 const char* SkDifferentPixelsMetric::getName() { | 35 const char* SkDifferentPixelsMetric::getName() { |
47 return "different_pixels"; | 36 return "different_pixels"; |
48 } | 37 } |
49 | 38 |
50 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { | 39 bool SkDifferentPixelsMetric::enablePOIAlphaMask() { |
51 return false; | 40 return false; |
52 } | 41 } |
53 | 42 |
54 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) { | 43 bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, Result* r esult) { |
55 int diffID = fQueuedDiffs.count(); | |
56 double startTime = get_seconds(); | 44 double startTime = get_seconds(); |
57 QueuedDiff* diff = fQueuedDiffs.push(); | |
58 | 45 |
59 // If we never end up running the kernel, include some safe defaults in the result. | 46 // If we never end up running the kernel, include some safe defaults in the result. |
60 diff->finished = false; | 47 result->poiCount = 0; |
61 diff->result = -1.0; | |
62 diff->numDiffPixels = 0; | |
63 diff->poi = NULL; | |
64 | 48 |
65 // Ensure the images are comparable | 49 // Ensure the images are comparable |
66 if (baseline->width() != test->width() || baseline->height() != test->height () || | 50 if (baseline->width() != test->width() || baseline->height() != test->height () || |
67 baseline->width() <= 0 || baseline->height() <= 0 || | 51 baseline->width() <= 0 || baseline->height() <= 0 || |
68 baseline->config() != test->config()) { | 52 baseline->config() != test->config()) { |
69 diff->finished = true; | 53 return false; |
70 return diffID; | |
71 } | 54 } |
72 | 55 |
56 cl_mem baselineImage; | |
57 cl_mem testImage; | |
58 cl_mem resultsBuffer; | |
59 | |
73 // Upload images to the CL device | 60 // Upload images to the CL device |
74 if (!this->makeImage2D(baseline, &diff->baseline) || !this->makeImage2D(test , &diff->test)) { | 61 if (!this->makeImage2D(baseline, &baselineImage) || !this->makeImage2D(test, &testImage)) { |
75 diff->finished = true; | 62 SkDebugf("creation of openCL images failed"); |
76 fIsGood = false; | 63 return false; |
77 return -1; | |
78 } | 64 } |
79 | 65 |
80 // A small hack that makes calculating percentage difference easier later on . | 66 // A small hack that makes calculating percentage difference easier later on . |
81 diff->result = 1.0 / ((double)baseline->width() * baseline->height()); | 67 result->result = 1.0 / ((double)baseline->width() * baseline->height()); |
82 | 68 |
83 // Make a buffer to store results into. It must be initialized with pointers to memory. | 69 // Make a buffer to store results into. It must be initialized with pointers to memory. |
84 static const int kZero = 0; | 70 static const int kZero = 0; |
85 // We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR | 71 // We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR |
86 diff->resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_CO PY_HOST_PTR, | 72 resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOS T_PTR, |
87 sizeof(int), (int*)&kZero, NULL); | 73 sizeof(int), (int*)&kZero, NULL); |
88 | |
89 diff->poiBuffer = clCreateBuffer(fContext, CL_MEM_WRITE_ONLY, | |
90 sizeof(int) * 2 * baseline->width() * basel ine->height(), | |
91 NULL, NULL); | |
92 | 74 |
93 // Set all kernel arguments | 75 // Set all kernel arguments |
94 cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &diff->baselin e); | 76 cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &baselineImage ); |
95 setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &diff->test); | 77 setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &testImage); |
96 setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &diff->results Buffer); | 78 setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &resultsBuffer ); |
97 setArgErr |= clSetKernelArg(fKernel, 3, sizeof(cl_mem), &diff->poiBuff er); | |
98 if (CL_SUCCESS != setArgErr) { | 79 if (CL_SUCCESS != setArgErr) { |
99 SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr)); | 80 SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr)); |
100 fIsGood = false; | 81 fIsGood = false; |
101 return -1; | 82 return false; |
102 } | 83 } |
103 | 84 |
104 // Queue this diff on the CL device | 85 // Queue this diff on the CL device |
105 cl_event event; | 86 cl_event event; |
106 const size_t workSize[] = { baseline->width(), baseline->height() }; | 87 const size_t workSize[] = { baseline->width(), baseline->height() }; |
107 cl_int enqueueErr; | 88 cl_int enqueueErr; |
108 enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, NULL, workSiz e, | 89 enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, NULL, workSiz e, |
109 NULL, 0, NULL, &event); | 90 NULL, 0, NULL, &event); |
110 if (CL_SUCCESS != enqueueErr) { | 91 if (CL_SUCCESS != enqueueErr) { |
111 SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr)); | 92 SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr)); |
112 fIsGood = false; | 93 fIsGood = false; |
113 return -1; | 94 return false; |
114 } | 95 } |
115 | 96 |
116 // This makes things totally synchronous. Actual queue is not ready yet | 97 // This makes things totally synchronous. Actual queue is not ready yet |
117 clWaitForEvents(1, &event); | 98 clWaitForEvents(1, &event); |
118 diff->finished = true; | |
119 | 99 |
120 // Immediate read back the results | 100 // Immediate read back the results |
121 clEnqueueReadBuffer(fCommandQueue, diff->resultsBuffer, CL_TRUE, 0, | 101 clEnqueueReadBuffer(fCommandQueue, resultsBuffer, CL_TRUE, 0, |
122 sizeof(int), &diff->numDiffPixels, 0, NULL, NULL); | 102 sizeof(int), &result->poiCount, 0, NULL, NULL); |
123 diff->result *= (double)diff->numDiffPixels; | 103 result->result *= (double)result->poiCount; |
124 diff->result = (1.0 - diff->result); | 104 result->result = (1.0 - result->result); |
125 | |
126 // Reading a buffer of size zero can cause issues on some (Mac) OpenCL platf orms. | |
127 if (diff->numDiffPixels > 0) { | |
128 diff->poi = SkNEW_ARRAY(SkIPoint, diff->numDiffPixels); | |
129 clEnqueueReadBuffer(fCommandQueue, diff->poiBuffer, CL_TRUE, 0, | |
130 sizeof(SkIPoint) * diff->numDiffPixels, diff->poi, 0, NU LL, NULL); | |
131 } | |
132 | 105 |
133 // Release all the buffers created | 106 // Release all the buffers created |
134 clReleaseMemObject(diff->poiBuffer); | 107 clReleaseMemObject(resultsBuffer); |
135 clReleaseMemObject(diff->resultsBuffer); | 108 clReleaseMemObject(baselineImage); |
136 clReleaseMemObject(diff->baseline); | 109 clReleaseMemObject(testImage); |
137 clReleaseMemObject(diff->test); | |
138 | 110 |
139 SkDebugf("Time: %f\n", (get_seconds() - startTime)); | 111 result->timeElapsed = get_seconds() - startTime; |
140 | 112 return true; |
141 return diffID; | |
142 } | |
143 | |
144 void SkDifferentPixelsMetric::deleteDiff(int id) { | |
145 QueuedDiff* diff = &fQueuedDiffs[id]; | |
146 if (NULL != diff->poi) { | |
147 SkDELETE_ARRAY(diff->poi); | |
148 diff->poi = NULL; | |
149 } | |
150 } | |
151 | |
152 bool SkDifferentPixelsMetric::isFinished(int id) { | |
153 return fQueuedDiffs[id].finished; | |
154 } | |
155 | |
156 double SkDifferentPixelsMetric::getResult(int id) { | |
157 return fQueuedDiffs[id].result; | |
158 } | |
159 | |
160 int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) { | |
161 return fQueuedDiffs[id].numDiffPixels; | |
162 } | |
163 | |
164 SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) { | |
165 return fQueuedDiffs[id].poi; | |
166 } | 113 } |
167 | 114 |
168 bool SkDifferentPixelsMetric::onInit() { | 115 bool SkDifferentPixelsMetric::onInit() { |
169 if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel)) { | 116 if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel)) { |
170 return false; | 117 return false; |
171 } | 118 } |
172 | 119 |
173 return true; | 120 return true; |
174 } | 121 } |
OLD | NEW |