Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: experimental/skpdiff/SkDifferentPixelsMetric_opencl.cpp

Issue 19374006: make OpenCL optional for skpdiff (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: this-> Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBitmap.h"
9
10 #include "SkDifferentPixelsMetric.h"
11 #include "skpdiff_util.h"
12
13 static const char kDifferentPixelsKernelSource[] =
14 "#pragma OPENCL_EXTENSION cl_khr_global_int32_base_atomics \n"
15 " \n"
16 "const sampler_t gInSampler = CLK_NORMALIZED_COORDS_FALSE | \n"
17 " CLK_ADDRESS_CLAMP_TO_EDGE | \n"
18 " CLK_FILTER_NEAREST; \n"
19 " \n"
20 "__kernel void diff(read_only image2d_t baseline, read_only image2d_t test, \n"
21 " __global int* result, __global int2* poi) { \n"
22 " int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n"
23 " uint4 baselinePixel = read_imageui(baseline, gInSampler, coord); \n"
24 " uint4 testPixel = read_imageui(test, gInSampler, coord); \n"
25 " int4 pixelCompare = baselinePixel == testPixel; \n"
26 " if (baselinePixel.x != testPixel.x || \n"
27 " baselinePixel.y != testPixel.y || \n"
28 " baselinePixel.z != testPixel.z || \n"
29 " baselinePixel.w != testPixel.w) { \n"
30 " \n"
31 " int poiIndex = atomic_inc(result); \n"
32 " poi[poiIndex] = coord; \n"
33 " } \n"
34 "} \n";
35
36 struct SkDifferentPixelsMetric::QueuedDiff {
37 bool finished;
38 double result;
39 int numDiffPixels;
40 SkIPoint* poi;
41 cl_mem baseline;
42 cl_mem test;
43 cl_mem resultsBuffer;
44 cl_mem poiBuffer;
45 };
46
47 const char* SkDifferentPixelsMetric::getName() {
48 return "different_pixels";
49 }
50
51 int SkDifferentPixelsMetric::queueDiff(SkBitmap* baseline, SkBitmap* test) {
52 int diffID = fQueuedDiffs.count();
53 double startTime = get_seconds();
54 QueuedDiff* diff = fQueuedDiffs.push();
55
56 // If we never end up running the kernel, include some safe defaults in the result.
57 diff->finished = false;
58 diff->result = -1.0;
59 diff->numDiffPixels = 0;
60 diff->poi = NULL;
61
62 // Ensure the images are comparable
63 if (baseline->width() != test->width() || baseline->height() != test->height () ||
64 baseline->width() <= 0 || baseline->height() <= 0 ||
65 baseline->config() != test->config()) {
66 diff->finished = true;
67 return diffID;
68 }
69
70 // Upload images to the CL device
71 if (!this->makeImage2D(baseline, &diff->baseline) || !this->makeImage2D(test , &diff->test)) {
72 diff->finished = true;
73 fIsGood = false;
74 return -1;
75 }
76
77 // A small hack that makes calculating percentage difference easier later on .
78 diff->result = 1.0 / ((double)baseline->width() * baseline->height());
79
80 // Make a buffer to store results into. It must be initialized with pointers to memory.
81 static const int kZero = 0;
82 // We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR
83 diff->resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_CO PY_HOST_PTR,
84 sizeof(int), (int*)&kZero, NULL);
85
86 diff->poiBuffer = clCreateBuffer(fContext, CL_MEM_WRITE_ONLY,
87 sizeof(int) * 2 * baseline->width() * basel ine->height(),
88 NULL, NULL);
89
90 // Set all kernel arguments
91 cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &diff->baselin e);
92 setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &diff->test);
93 setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &diff->results Buffer);
94 setArgErr |= clSetKernelArg(fKernel, 3, sizeof(cl_mem), &diff->poiBuff er);
95 if (CL_SUCCESS != setArgErr) {
96 SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr));
97 fIsGood = false;
98 return -1;
99 }
100
101 // Queue this diff on the CL device
102 cl_event event;
103 const size_t workSize[] = { baseline->width(), baseline->height() };
104 cl_int enqueueErr;
105 enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, NULL, workSiz e,
106 NULL, 0, NULL, &event);
107 if (CL_SUCCESS != enqueueErr) {
108 SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr));
109 fIsGood = false;
110 return -1;
111 }
112
113 // This makes things totally synchronous. Actual queue is not ready yet
114 clWaitForEvents(1, &event);
115 diff->finished = true;
116
117 // Immediate read back the results
118 clEnqueueReadBuffer(fCommandQueue, diff->resultsBuffer, CL_TRUE, 0,
119 sizeof(int), &diff->numDiffPixels, 0, NULL, NULL);
120 diff->result *= (double)diff->numDiffPixels;
121 diff->result = (1.0 - diff->result);
122
123 diff->poi = SkNEW_ARRAY(SkIPoint, diff->numDiffPixels);
124 clEnqueueReadBuffer(fCommandQueue, diff->poiBuffer, CL_TRUE, 0,
125 sizeof(SkIPoint) * diff->numDiffPixels, diff->poi, 0, NU LL, NULL);
126
127 // Release all the buffers created
128 clReleaseMemObject(diff->poiBuffer);
129 clReleaseMemObject(diff->resultsBuffer);
130 clReleaseMemObject(diff->baseline);
131 clReleaseMemObject(diff->test);
132
133 SkDebugf("Time: %f\n", (get_seconds() - startTime));
134
135 return diffID;
136 }
137
138 void SkDifferentPixelsMetric::deleteDiff(int id) {
139 QueuedDiff* diff = &fQueuedDiffs[id];
140 if (NULL != diff->poi) {
141 SkDELETE_ARRAY(diff->poi);
142 diff->poi = NULL;
143 }
144 }
145
146 bool SkDifferentPixelsMetric::isFinished(int id) {
147 return fQueuedDiffs[id].finished;
148 }
149
150 double SkDifferentPixelsMetric::getResult(int id) {
151 return fQueuedDiffs[id].result;
152 }
153
154 int SkDifferentPixelsMetric::getPointsOfInterestCount(int id) {
155 return fQueuedDiffs[id].numDiffPixels;
156 }
157
158 SkIPoint* SkDifferentPixelsMetric::getPointsOfInterest(int id) {
159 return fQueuedDiffs[id].poi;
160 }
161
162 bool SkDifferentPixelsMetric::onInit() {
163 if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel)) {
164 return false;
165 }
166
167 return true;
168 }
OLDNEW
« no previous file with comments | « experimental/skpdiff/SkDifferentPixelsMetric_cpu.cpp ('k') | experimental/skpdiff/SkImageDiffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698