commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 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 | #ifndef SkCLImageDiffer_DEFINED |
| 9 | #define SkCLImageDiffer_DEFINED |
| 10 | |
| 11 | #include <CL/cl.h> |
| 12 | #include "SkTDArray.h" |
| 13 | |
| 14 | #include "SkImageDiffer.h" |
| 15 | |
| 16 | class SkStream; |
| 17 | |
| 18 | /** |
| 19 | * An SkImageDiffer that requires initialization with an OpenCL device and context. |
| 20 | */ |
| 21 | class SkCLImageDiffer : public SkImageDiffer { |
| 22 | public: |
| 23 | SkCLImageDiffer(); |
| 24 | |
| 25 | /** |
| 26 | * Initializes the OpenCL resources this differ needs to work |
| 27 | * @param device An OpenCL device |
| 28 | * @param context An OpenCL context of the given device |
| 29 | * @return True on success, false otherwise |
| 30 | */ |
| 31 | virtual bool init(cl_device_id device, cl_context context); |
| 32 | |
| 33 | protected: |
| 34 | /** |
| 35 | * Called by init after fDevice, fContext, and fCommandQueue are successfully initialized |
| 36 | * @return True on success, false otherwise |
| 37 | */ |
| 38 | virtual bool onInit() = 0; |
| 39 | |
| 40 | /** |
| 41 | * Loads an OpenCL kernel from the file with the given named entry point. This only works after |
| 42 | * init is called. |
| 43 | * @param file The file path of the kernel |
| 44 | * @param name The name of the entry point of the desired kernel in the file |
| 45 | * @param kernel A pointer to return the loaded kernel into |
| 46 | * @return True on success, false otherwise |
| 47 | */ |
| 48 | bool loadKernelFile(const char file[], const char name[], cl_kernel* kernel); |
| 49 | |
| 50 | /** |
| 51 | * Loads an OpenCL kernel from the stream with the given named entry point. This only works |
| 52 | * after init is called. |
| 53 | * @param stream The stream that contains the kernel |
| 54 | * @param name The name of the entry point of the desired kernel in the stream |
| 55 | * @param kernel A pointer to return the loaded kernel into |
| 56 | * @return True on success, false otherwise |
| 57 | */ |
| 58 | bool loadKernelStream(SkStream* stream, const char name[], cl_kernel* kernel); |
| 59 | |
| 60 | /** |
| 61 | * Loads an OpenCL kernel from the source string with the given named entry point. This only |
| 62 | * works after init is called. |
| 63 | * @param source The string that contains the kernel |
| 64 | * @param name The name of the entry point of the desired kernel in the source string |
| 65 | * @param kernel A pointer to return the loaded kernel into |
| 66 | * @return True on success, false otherwise |
| 67 | */ |
| 68 | bool loadKernelSource(const char source[], const char name[], cl_kernel* kernel); |
| 69 | |
| 70 | /** |
| 71 | * Loads a read only copy of the given bitmap into device memory and returns the block of |
| 72 | * memory. This only works after init is called. |
| 73 | * @param bitmap The bitmap to load into memory |
| 74 | * @param image A pointer to return the allocated image to |
| 75 | * @return True on success, false otherwise |
| 76 | */ |
| 77 | bool makeImage2D(SkBitmap* bitmap, cl_mem* image); |
| 78 | |
| 79 | cl_device_id fDevice; |
| 80 | cl_context fContext; |
| 81 | cl_command_queue fCommandQueue; |
| 82 | |
| 83 | private: |
| 84 | |
| 85 | typedef SkImageDiffer INHERITED; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * A OpenCL differ that measures the percentage of different corresponding pixels. If the two images |
| 90 | * are not the same size or have no pixels, the result will always be zero. |
| 91 | */ |
| 92 | class SkDifferentPixelsImageDiffer : public SkCLImageDiffer { |
| 93 | public: |
| 94 | virtual const char* getName() SK_OVERRIDE; |
| 95 | virtual int queueDiff(SkBitmap* baseline, SkBitmap* test) SK_OVERRIDE; |
zachr@google.com | 572b54d | 2013-06-28 16:27:33 +0000 | [diff] [blame] | 96 | virtual void deleteDiff(int id) SK_OVERRIDE; |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 97 | virtual bool isFinished(int id) SK_OVERRIDE; |
| 98 | virtual double getResult(int id) SK_OVERRIDE; |
zachr@google.com | 572b54d | 2013-06-28 16:27:33 +0000 | [diff] [blame] | 99 | virtual int getPointsOfInterestCount(int id) SK_OVERRIDE; |
| 100 | virtual SkIPoint* getPointsOfInterest(int id) SK_OVERRIDE; |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 101 | |
| 102 | protected: |
| 103 | virtual bool onInit() SK_OVERRIDE; |
| 104 | |
| 105 | private: |
zachr@google.com | 572b54d | 2013-06-28 16:27:33 +0000 | [diff] [blame] | 106 | struct QueuedDiff; |
commit-bot@chromium.org | be19b9e | 2013-06-14 17:26:54 +0000 | [diff] [blame] | 107 | |
| 108 | SkTDArray<QueuedDiff> fQueuedDiffs; |
| 109 | cl_kernel fKernel; |
| 110 | |
| 111 | typedef SkCLImageDiffer INHERITED; |
| 112 | }; |
| 113 | |
| 114 | #endif |