This patch implements the diffuse and specular lighting filters in Ganesh. 
There are three light types for each:  distant, point and spot, whose code
generation lives in a GrGLLight class hierarchy.  This similar to the CPU
implementation, where each light type provides a function to compute the vector
from the surface plane to the light (surfaceToLight) and to compute the light
colour (emitLightColour).  Instead of templated member functions, as in the CPU
implementation, these are virtual functions to emit the light-specific GLSL
code.

All of the code for the GPU path lives in the same file as that for the CPU
path, SkLightingImageFilter.cpp.  In order to provide Ganesh a hook to access
it, SkImageFilter now has a asNewCustomStage() virtual, which allows an image
filter to return a GrCustomStage representing that filter.

Note that this patch does not handle the border conditions correctly (the
[top|bottom][Left|Right]Normal() functions in the CPU implementation).  That
will come in a future patch.

Review URL: http://codereview.appspot.com/6345081/



git-svn-id: http://skia.googlecode.com/svn/trunk@4535 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index fc73f0d..e5227a2 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1424,6 +1424,33 @@
     fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
 }
 
+namespace {
+
+void apply_custom_stage(GrContext* context,
+                        GrTexture* srcTexture,
+                        GrTexture* dstTexture,
+                        const GrRect& rect,
+                        GrCustomStage* stage) {
+    SkASSERT(srcTexture && srcTexture->getContext() == context);
+    GrAutoMatrix avm(context, GrMatrix::I());
+    GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
+    GrClip oldClip = context->getClip();
+    context->setClip(rect);
+
+    GrMatrix sampleM;
+    sampleM.setIDiv(srcTexture->width(), srcTexture->height());
+    GrPaint paint;
+    paint.reset();
+    paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
+    paint.textureSampler(0)->reset(sampleM);
+    paint.textureSampler(0)->setCustomStage(stage);
+    paint.setTexture(0, srcTexture);
+    context->drawRect(paint, rect);
+    context->setClip(oldClip);
+}
+
+};
+
 static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
                                  SkImageFilter* filter, const GrRect& rect) {
     GrAssert(filter);
@@ -1436,8 +1463,14 @@
     desc.fWidth = SkScalarCeilToInt(rect.width());
     desc.fHeight = SkScalarCeilToInt(rect.height());
     desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
+    GrCustomStage* stage;
 
-    if (filter->asABlur(&blurSize)) {
+    if (filter->asNewCustomStage(&stage)) {
+        GrAutoScratchTexture dst(context, desc);
+        apply_custom_stage(context, texture, dst.texture(), rect, stage);
+        texture = dst.detach();
+        stage->unref();
+    } else if (filter->asABlur(&blurSize)) {
         GrAutoScratchTexture temp1, temp2;
         texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
                                         blurSize.width(),
@@ -1564,7 +1597,11 @@
 bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
     SkSize size;
     SkISize radius;
-    if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
+
+    if (!filter->asNewCustomStage(NULL) &&
+        !filter->asABlur(&size) &&
+        !filter->asADilate(&radius) &&
+        !filter->asAnErode(&radius)) {
         return false;
     }
     return true;