2013-07-17 19:29:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
|
|
|
|
#include "SkDifferentPixelsMetric.h"
|
|
|
|
#include "skpdiff_util.h"
|
|
|
|
|
|
|
|
static const char kDifferentPixelsKernelSource[] =
|
|
|
|
"#pragma OPENCL_EXTENSION cl_khr_global_int32_base_atomics \n"
|
|
|
|
" \n"
|
|
|
|
"const sampler_t gInSampler = CLK_NORMALIZED_COORDS_FALSE | \n"
|
|
|
|
" CLK_ADDRESS_CLAMP_TO_EDGE | \n"
|
|
|
|
" CLK_FILTER_NEAREST; \n"
|
|
|
|
" \n"
|
|
|
|
"__kernel void diff(read_only image2d_t baseline, read_only image2d_t test, \n"
|
2013-11-12 18:29:17 +00:00
|
|
|
" __global int* result) { \n"
|
2013-07-17 19:29:19 +00:00
|
|
|
" int2 coord = (int2)(get_global_id(0), get_global_id(1)); \n"
|
|
|
|
" uint4 baselinePixel = read_imageui(baseline, gInSampler, coord); \n"
|
|
|
|
" uint4 testPixel = read_imageui(test, gInSampler, coord); \n"
|
|
|
|
" if (baselinePixel.x != testPixel.x || \n"
|
|
|
|
" baselinePixel.y != testPixel.y || \n"
|
|
|
|
" baselinePixel.z != testPixel.z || \n"
|
|
|
|
" baselinePixel.w != testPixel.w) { \n"
|
|
|
|
" \n"
|
2013-11-12 18:29:17 +00:00
|
|
|
" atomic_inc(result); \n"
|
|
|
|
" // TODO: generate alpha mask \n"
|
2013-07-17 19:29:19 +00:00
|
|
|
" } \n"
|
|
|
|
"} \n";
|
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
const char* SkDifferentPixelsMetric::getName() const {
|
2013-07-17 19:29:19 +00:00
|
|
|
return "different_pixels";
|
|
|
|
}
|
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test, bool computeMask,
|
|
|
|
Result* result) const {
|
2013-07-17 19:29:19 +00:00
|
|
|
double startTime = get_seconds();
|
2013-11-12 18:29:17 +00:00
|
|
|
|
|
|
|
if (!fIsGood) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
// If we never end up running the kernel, include some safe defaults in the result.
|
2013-11-12 18:29:17 +00:00
|
|
|
result->poiCount = 0;
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
// Ensure the images are comparable
|
|
|
|
if (baseline->width() != test->width() || baseline->height() != test->height() ||
|
|
|
|
baseline->width() <= 0 || baseline->height() <= 0 ||
|
|
|
|
baseline->config() != test->config()) {
|
2013-11-12 18:29:17 +00:00
|
|
|
return false;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
cl_mem baselineImage;
|
|
|
|
cl_mem testImage;
|
|
|
|
cl_mem resultsBuffer;
|
|
|
|
|
2013-07-17 19:29:19 +00:00
|
|
|
// Upload images to the CL device
|
2013-11-12 18:29:17 +00:00
|
|
|
if (!this->makeImage2D(baseline, &baselineImage) || !this->makeImage2D(test, &testImage)) {
|
|
|
|
SkDebugf("creation of openCL images failed");
|
|
|
|
return false;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A small hack that makes calculating percentage difference easier later on.
|
2013-11-12 18:29:17 +00:00
|
|
|
result->result = 1.0 / ((double)baseline->width() * baseline->height());
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
// Make a buffer to store results into. It must be initialized with pointers to memory.
|
|
|
|
static const int kZero = 0;
|
|
|
|
// We know OpenCL won't write to it because we use CL_MEM_COPY_HOST_PTR
|
2013-11-12 18:29:17 +00:00
|
|
|
resultsBuffer = clCreateBuffer(fContext, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
|
|
|
|
sizeof(int), (int*)&kZero, NULL);
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
// Set all kernel arguments
|
2013-11-12 18:29:17 +00:00
|
|
|
cl_int setArgErr = clSetKernelArg(fKernel, 0, sizeof(cl_mem), &baselineImage);
|
|
|
|
setArgErr |= clSetKernelArg(fKernel, 1, sizeof(cl_mem), &testImage);
|
|
|
|
setArgErr |= clSetKernelArg(fKernel, 2, sizeof(cl_mem), &resultsBuffer);
|
2013-07-17 19:29:19 +00:00
|
|
|
if (CL_SUCCESS != setArgErr) {
|
|
|
|
SkDebugf("Set arg failed: %s\n", cl_error_to_string(setArgErr));
|
2013-11-12 18:29:17 +00:00
|
|
|
return false;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue this diff on the CL device
|
|
|
|
cl_event event;
|
|
|
|
const size_t workSize[] = { baseline->width(), baseline->height() };
|
|
|
|
cl_int enqueueErr;
|
|
|
|
enqueueErr = clEnqueueNDRangeKernel(fCommandQueue, fKernel, 2, NULL, workSize,
|
|
|
|
NULL, 0, NULL, &event);
|
|
|
|
if (CL_SUCCESS != enqueueErr) {
|
|
|
|
SkDebugf("Enqueue failed: %s\n", cl_error_to_string(enqueueErr));
|
2013-11-12 18:29:17 +00:00
|
|
|
return false;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This makes things totally synchronous. Actual queue is not ready yet
|
|
|
|
clWaitForEvents(1, &event);
|
|
|
|
|
|
|
|
// Immediate read back the results
|
2013-11-12 18:29:17 +00:00
|
|
|
clEnqueueReadBuffer(fCommandQueue, resultsBuffer, CL_TRUE, 0,
|
|
|
|
sizeof(int), &result->poiCount, 0, NULL, NULL);
|
|
|
|
result->result *= (double)result->poiCount;
|
|
|
|
result->result = (1.0 - result->result);
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
// Release all the buffers created
|
2013-11-12 18:29:17 +00:00
|
|
|
clReleaseMemObject(resultsBuffer);
|
|
|
|
clReleaseMemObject(baselineImage);
|
|
|
|
clReleaseMemObject(testImage);
|
2013-07-17 19:29:19 +00:00
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
result->timeElapsed = get_seconds() - startTime;
|
|
|
|
return true;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SkDifferentPixelsMetric::onInit() {
|
|
|
|
if (!this->loadKernelSource(kDifferentPixelsKernelSource, "diff", &fKernel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|