public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
   synchronized (CachingPipeline.class) {
     if (getRenderScriptContext() == null) {
       return bitmap;
     }
     setupEnvironment(preset, false);
     mEnvironment.setQuality(FilterEnvironment.QUALITY_FINAL);
     mEnvironment.setScaleFactor(1.0f);
     mFiltersManager.freeFilterResources(preset);
     bitmap = preset.applyGeometry(bitmap, mEnvironment);
     bitmap = preset.apply(bitmap, mEnvironment);
     return bitmap;
   }
 }
 public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
   if (getRenderScriptContext() == null) {
     return;
   }
   setupEnvironment(preset, false);
   Vector<FilterRepresentation> filters = preset.getFilters();
   Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
   buffer.setProducer(result);
   mEnvironment.cache(result);
 }
 private void setupEnvironment(ImagePreset preset, boolean highResPreview) {
   mEnvironment.setPipeline(this);
   mEnvironment.setFiltersManager(mFiltersManager);
   mEnvironment.setBitmapCache(MasterImage.getImage().getBitmapCache());
   if (highResPreview) {
     mEnvironment.setScaleFactor(mHighResPreviewScaleFactor);
   } else {
     mEnvironment.setScaleFactor(mPreviewScaleFactor);
   }
   mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
   mEnvironment.setImagePreset(preset);
   mEnvironment.setStop(false);
 }
 public void renderFilters(RenderingRequest request) {
   synchronized (CachingPipeline.class) {
     if (getRenderScriptContext() == null) {
       return;
     }
     ImagePreset preset = request.getImagePreset();
     setupEnvironment(preset, false);
     Bitmap bitmap = MasterImage.getImage().getOriginalBitmapHighres();
     if (bitmap == null) {
       return;
     }
     bitmap = mEnvironment.getBitmapCopy(bitmap, BitmapCache.FILTERS);
     bitmap = preset.apply(bitmap, mEnvironment);
     if (!mEnvironment.needsStop()) {
       request.setBitmap(bitmap);
     } else {
       mEnvironment.cache(bitmap);
     }
     mFiltersManager.freeFilterResources(preset);
   }
 }
 public void stop() {
   mEnvironment.setStop(true);
 }
  public synchronized void render(RenderingRequest request) {
    // TODO: cleanup/remove GEOMETRY / FILTERS paths
    synchronized (CachingPipeline.class) {
      if (getRenderScriptContext() == null) {
        return;
      }
      if ((request.getType() != RenderingRequest.PARTIAL_RENDERING
              && request.getType() != RenderingRequest.ICON_RENDERING
              && request.getBitmap() == null)
          || request.getImagePreset() == null) {
        return;
      }

      if (DEBUG) {
        Log.v(LOGTAG, "render image of type " + getType(request));
      }

      Bitmap bitmap = request.getBitmap();
      ImagePreset preset = request.getImagePreset();
      setupEnvironment(preset, true);
      mFiltersManager.freeFilterResources(preset);

      if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
        MasterImage master = MasterImage.getImage();
        bitmap =
            ImageLoader.getScaleOneImageForPreset(
                master.getActivity(),
                mEnvironment.getBimapCache(),
                master.getUri(),
                request.getBounds(),
                request.getDestination());
        if (bitmap == null) {
          Log.w(LOGTAG, "could not get bitmap for: " + getType(request));
          return;
        }
      }

      if (request.getType() == RenderingRequest.FULL_RENDERING
          || request.getType() == RenderingRequest.GEOMETRY_RENDERING
          || request.getType() == RenderingRequest.FILTERS_RENDERING) {
        updateOriginalAllocation(preset);
      }

      if (DEBUG && bitmap != null) {
        Log.v(
            LOGTAG,
            "after update, req bitmap ("
                + bitmap.getWidth()
                + "x"
                + bitmap.getHeight()
                + " ? resizeOriginal ("
                + mResizedOriginalBitmap.getWidth()
                + "x"
                + mResizedOriginalBitmap.getHeight());
      }

      if (request.getType() == RenderingRequest.FULL_RENDERING
          || request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
        mOriginalAllocation.copyTo(bitmap);
      } else if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
        mFiltersOnlyOriginalAllocation.copyTo(bitmap);
      }

      if (request.getType() == RenderingRequest.FULL_RENDERING
          || request.getType() == RenderingRequest.FILTERS_RENDERING
          || request.getType() == RenderingRequest.ICON_RENDERING
          || request.getType() == RenderingRequest.PARTIAL_RENDERING
          || request.getType() == RenderingRequest.STYLE_ICON_RENDERING) {

        if (request.getType() == RenderingRequest.ICON_RENDERING) {
          mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
        } else {
          mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
        }

        if (request.getType() == RenderingRequest.ICON_RENDERING) {
          Rect iconBounds = request.getIconBounds();
          Bitmap source = MasterImage.getImage().getThumbnailBitmap();
          if (iconBounds.width() > source.getWidth() * 2) {
            source = MasterImage.getImage().getLargeThumbnailBitmap();
          }
          if (iconBounds != null) {
            bitmap =
                mEnvironment.getBitmap(iconBounds.width(), iconBounds.height(), BitmapCache.ICON);
            Canvas canvas = new Canvas(bitmap);
            Matrix m = new Matrix();
            float minSize = Math.min(source.getWidth(), source.getHeight());
            float maxSize = Math.max(iconBounds.width(), iconBounds.height());
            float scale = maxSize / minSize;
            m.setScale(scale, scale);
            float dx = (iconBounds.width() - (source.getWidth() * scale)) / 2.0f;
            float dy = (iconBounds.height() - (source.getHeight() * scale)) / 2.0f;
            m.postTranslate(dx, dy);
            canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG));
          } else {
            bitmap = mEnvironment.getBitmapCopy(source, BitmapCache.ICON);
          }
        }
        Bitmap bmp = preset.apply(bitmap, mEnvironment);
        if (!mEnvironment.needsStop()) {
          request.setBitmap(bmp);
        }
        mFiltersManager.freeFilterResources(preset);
      }
    }
  }