Exemple #1
0
  protected void createWalk(int vsize) {
    // We do a random copy order to attempt to get multiple threads
    // reading and writing the same cache line
    // We could do this as a simple walk but that would likely miss
    // some caching issues.
    final int tw = gCount / vsize;
    int tmp[] = new int[tw];
    boolean b[] = new boolean[tw];
    int toCopy = tw;
    int i = 0;

    while (toCopy > 0) {
      int x = random.nextInt(tw);

      while ((x < tw) && b[x]) {
        x++;
        if (x >= tw) {
          x = 0;
        }
      }

      b[x] = true;
      toCopy--;

      // android.util.Log.v("rs", "walk  " + i + ", " + x);
      tmp[i++] = x;
    }

    walkAlloc = Allocation.createSized(mRS, Element.I32(mRS), tw);
    walkAlloc.copy1DRangeFrom(0, tw, tmp);
  }
Exemple #2
0
 private byte[] randomByteArray(int len) {
   byte t[] = new byte[len];
   random.nextBytes(t);
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #3
0
 private double[] randomDoubleArray(int len) {
   double t[] = new double[len];
   for (int i = 0; i < t.length; i++) {
     t[i] = random.nextDouble();
   }
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #4
0
 private float[] randomFloatArray(int len) {
   float t[] = new float[len];
   for (int i = 0; i < t.length; i++) {
     t[i] = random.nextFloat();
   }
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #5
0
 private long[] randomLongArray(int len) {
   long t[] = new long[len];
   for (int i = 0; i < t.length; i++) {
     t[i] = random.nextLong();
   }
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #6
0
 private int[] randomIntArray(int len) {
   int t[] = new int[len];
   for (int i = 0; i < t.length; i++) {
     t[i] = random.nextInt();
   }
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #7
0
 private short[] randomShortArray(int len) {
   short t[] = new short[len];
   for (int i = 0; i < t.length; i++) {
     t[i] = (short) (random.nextInt() & 0xffff);
   }
   in1DAlloc.copyFrom(t);
   in2DAlloc.copyFrom(t);
   in3DAlloc.copyFrom(t);
   return t;
 }
Exemple #8
0
  private void initializeGlobals(RenderScript RS, ScriptC_kernel s) {
    Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
    int X = 5;
    s.set_dimX(X);
    typeBuilder.setX(X);
    A = Allocation.createTyped(RS, typeBuilder.create());
    s.bind_ain(A);
    B = Allocation.createTyped(RS, typeBuilder.create());
    s.bind_aout(B);

    return;
  }
Exemple #9
0
 private void verify(double[] a1, double[] a2, Allocation alloc, String s, int vsize) {
   alloc.copyTo(a2);
   for (int i = 0; i < gWidth; i++) {
     if (a1[i] != a2[i]) {
       if ((vsize == 3) && ((i % 4) == 3)) {
         continue;
       }
       throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i);
     }
     a2[i] = 0;
   }
   alloc.copyFrom(a2);
 }
  private Bitmap BlurImage(Bitmap input, int radius) {
    RenderScript rsScript = RenderScript.create(getActivity());
    Allocation alloc = Allocation.createFromBitmap(rsScript, input);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, Element.U8_4(rsScript));
    blur.setRadius(radius);
    blur.setInput(alloc);

    Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig());
    Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);
    blur.forEach(outAlloc);
    outAlloc.copyTo(result);

    rsScript.destroy();
    return result;
  }
    /** @deprecated in API 16 Create a Mesh object from the current state of the builder */
    public Mesh create() {
      mRS.validate();
      long[] vtx = new long[mVertexTypeCount];
      long[] idx = new long[mIndexTypes.size()];
      int[] prim = new int[mIndexTypes.size()];

      Allocation[] vertexBuffers = new Allocation[mVertexTypeCount];
      Allocation[] indexBuffers = new Allocation[mIndexTypes.size()];
      Primitive[] primitives = new Primitive[mIndexTypes.size()];

      for (int ct = 0; ct < mVertexTypeCount; ct++) {
        Allocation alloc = null;
        Entry entry = mVertexTypes[ct];
        if (entry.t != null) {
          alloc = Allocation.createTyped(mRS, entry.t, mUsage);
        } else if (entry.e != null) {
          alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
        } else {
          // Should never happen because the builder will always set one
          throw new IllegalStateException("Builder corrupt, no valid element in entry.");
        }
        vertexBuffers[ct] = alloc;
        vtx[ct] = alloc.getID(mRS);
      }

      for (int ct = 0; ct < mIndexTypes.size(); ct++) {
        Allocation alloc = null;
        Entry entry = (Entry) mIndexTypes.elementAt(ct);
        if (entry.t != null) {
          alloc = Allocation.createTyped(mRS, entry.t, mUsage);
        } else if (entry.e != null) {
          alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
        } else {
          // Should never happen because the builder will always set one
          throw new IllegalStateException("Builder corrupt, no valid element in entry.");
        }
        long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
        indexBuffers[ct] = alloc;
        primitives[ct] = entry.prim;

        idx[ct] = allocID;
        prim[ct] = entry.prim.mID;
      }

      long id = mRS.nMeshCreate(vtx, idx, prim);
      Mesh newMesh = new Mesh(id, mRS);
      newMesh.mVertexBuffers = vertexBuffers;
      newMesh.mIndexBuffers = indexBuffers;
      newMesh.mPrimitives = primitives;

      return newMesh;
    }
Exemple #12
0
  private void testSetup(Element e) {
    int vs = e.getVectorSize();
    if (vs == 3) {
      vs = 4;
    }
    createWalk(vs);

    Type t1 = Type.createX(mRS, e, gWidth * gHeight * gDepth / vs);
    in1DAlloc = Allocation.createTyped(mRS, t1);
    out1DAlloc = Allocation.createTyped(mRS, t1);
    script.set_gAlloc1DIn(in1DAlloc);
    script.set_gAlloc1DOut(out1DAlloc);
    scriptRelaxed.set_gAlloc1DIn(in1DAlloc);
    scriptRelaxed.set_gAlloc1DOut(out1DAlloc);

    Type t2 = Type.createXY(mRS, e, gWidth / vs, gHeight * gDepth);
    in2DAlloc = Allocation.createTyped(mRS, t2);
    out2DAlloc = Allocation.createTyped(mRS, t2);
    script.set_gAlloc2DIn(in2DAlloc);
    script.set_gAlloc2DOut(out2DAlloc);
    scriptRelaxed.set_gAlloc2DIn(in2DAlloc);
    scriptRelaxed.set_gAlloc2DOut(out2DAlloc);

    Type t3 = Type.createXYZ(mRS, e, gWidth / vs, gHeight, gDepth);
    in3DAlloc = Allocation.createTyped(mRS, t3);
    out3DAlloc = Allocation.createTyped(mRS, t3);
    script.set_gAlloc3DIn(in3DAlloc);
    script.set_gAlloc3DOut(out3DAlloc);
    scriptRelaxed.set_gAlloc3DIn(in3DAlloc);
    scriptRelaxed.set_gAlloc3DOut(out3DAlloc);
  }
 public void forEach_root(Allocation ain, Allocation aout) {
   // check ain
   if (!ain.getType().getElement().isCompatible(__ScriptField_particle)) {
     throw new RSRuntimeException("Type mismatch with ScriptField_particle!");
   }
   // check aout
   if (!aout.getType().getElement().isCompatible(__ScriptField_particle)) {
     throw new RSRuntimeException("Type mismatch with ScriptField_particle!");
   }
   // Verify dimensions
   Type tIn = ain.getType();
   Type tOut = aout.getType();
   if ((tIn.getCount() != tOut.getCount())
       || (tIn.getX() != tOut.getX())
       || (tIn.getY() != tOut.getY())
       || (tIn.getZ() != tOut.getZ())
       || (tIn.hasFaces() != tOut.hasFaces())
       || (tIn.hasMipmaps() != tOut.hasMipmaps())) {
     throw new RSRuntimeException("Dimension mismatch between input and output parameters!");
   }
   forEach(mExportForEachIdx_root, ain, aout, null);
 }
  /**
   * Add a Mutation to the commit log.
   *
   * @param mutation the Mutation to add to the log
   */
  public ReplayPosition add(Mutation mutation) {
    assert mutation != null;

    long size = Mutation.serializer.serializedSize(mutation, MessagingService.current_version);

    long totalSize = size + ENTRY_OVERHEAD_SIZE;
    if (totalSize > MAX_MUTATION_SIZE) {
      throw new IllegalArgumentException(
          String.format(
              "Mutation of %s bytes is too large for the maxiumum size of %s",
              totalSize, MAX_MUTATION_SIZE));
    }

    Allocation alloc = allocator.allocate(mutation, (int) totalSize);
    ICRC32 checksum = CRC32Factory.instance.create();
    final ByteBuffer buffer = alloc.getBuffer();
    try (BufferedDataOutputStreamPlus dos = new DataOutputBufferFixed(buffer)) {
      // checksummed length
      dos.writeInt((int) size);
      checksum.update(buffer, buffer.position() - 4, 4);
      buffer.putInt(checksum.getCrc());

      int start = buffer.position();
      // checksummed mutation
      Mutation.serializer.serialize(mutation, dos, MessagingService.current_version);
      checksum.update(buffer, start, (int) size);
      buffer.putInt(checksum.getCrc());
    } catch (IOException e) {
      throw new FSWriteError(e, alloc.getSegment().getPath());
    } finally {
      alloc.markWritten();
    }

    executor.finishWriteFor(alloc);
    return alloc.getReplayPosition();
  }
Exemple #15
0
  public void moveAllocations(List<Integer> indexes, Allocation target, boolean before) {
    int startIndex = target.getIndex();
    if (before) {
      startIndex--;
    }

    for (int i = startIndex; i < startIndex + indexes.size(); i++) {
      allocations.get(indexes.get(i - startIndex)).setIndex(i);
    }

    for (int i = startIndex + indexes.size(); i < allocations.size(); i++) {
      allocations.get(i).setIndex(allocations.get(i).getIndex() + indexes.size());
    }
    Collections.sort(allocations);
    getMonitor().fireEntityChanged(this, Properties.allocations);
  }
 /** @hide */
 public void subData(int xoff, FieldPacker fp) {
   super.setFromFieldPacker(xoff, fp);
 }
Exemple #17
0
  private void createProgramVertex() {
    mPvOrthoAlloc = new ProgramVertexFixedFunction.Constants(mRS);
    Matrix4f proj = new Matrix4f();
    proj.loadProjectionNormalized(mWidth, mHeight);
    mPvOrthoAlloc.setProjection(proj);

    ProgramVertexFixedFunction.Builder builder = new ProgramVertexFixedFunction.Builder(mRS);
    mPvSky = builder.create();
    ((ProgramVertexFixedFunction) mPvSky).bindConstants(mPvOrthoAlloc);

    mScript.set_g_PVSky(mPvSky);

    mConstants = new ScriptField_Constants(mRS, 1);
    mUniformAlloc = mConstants.getAllocation();

    ProgramVertex.Builder sb = new ProgramVertex.Builder(mRS);

    String t =
        "\n"
            + "varying vec4 varColor;\n"
            + "varying vec2 varTex0;\n"
            + "vec2 addDrop(vec4 d, vec2 pos, float dxMul) {\n"
            + "  vec2 ret = vec2(0.0, 0.0);\n"
            + "  vec2 delta = d.xy - pos;\n"
            + "  delta.x *= dxMul;\n"
            + "  float dist = length(delta);\n"
            + "  if (dist < d.w) { \n"
            + "    float amp = d.z * dist;\n"
            + "    amp /= d.w * d.w;\n"
            + "    amp *= sin(d.w - dist);\n"
            + "    ret = delta * amp;\n"
            + "  }\n"
            + "  return ret;\n"
            + "}\n"
            + "void main() {\n"
            + "  vec2 pos = ATTRIB_position.xy;\n"
            + "  gl_Position = vec4(pos.x, pos.y, 0.0, 1.0);\n"
            + "  float dxMul = 1.0;\n"
            + "  varTex0 = vec2((pos.x + 1.0), (pos.y + 1.6666));\n"
            + "  if (UNI_Rotate < 0.9) {\n"
            + "    varTex0.xy *= vec2(0.25, 0.33);\n"
            + "    varTex0.x += UNI_Offset.x * 0.5;\n"
            + "    pos.x += UNI_Offset.x * 2.0;\n"
            + "  } else {\n"
            + "    varTex0.xy *= vec2(0.5, 0.3125);\n"
            + "    dxMul = 2.5;\n"
            + "  }\n"
            + "  varColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
            + "  pos.xy += vec2(1.0, 1.0);\n"
            + "  pos.xy *= vec2(25.0, 42.0);\n"
            + "  varTex0.xy += addDrop(UNI_Drop01, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop02, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop03, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop04, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop05, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop06, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop07, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop08, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop09, pos, dxMul);\n"
            + "  varTex0.xy += addDrop(UNI_Drop10, pos, dxMul);\n"
            + "}\n";

    sb.setShader(t);
    sb.addConstant(mUniformAlloc.getType());
    sb.addInput(mMesh.getVertexAllocation(0).getType().getElement());
    mPvWater = sb.create();
    mPvWater.bindConstants(mUniformAlloc, 0);

    mScript.set_g_PVWater(mPvWater);
  }
Exemple #18
0
 private Allocation loadTextureARGB(int id) {
   Bitmap b = BitmapFactory.decodeResource(mResources, id, mOptionsARGB);
   final Allocation allocation = Allocation.createFromBitmap(mRS, b);
   return allocation;
 }
Exemple #19
0
 private Allocation loadTexture(int id) {
   final Allocation allocation = Allocation.createFromBitmapResource(mRS, mResources, id);
   return allocation;
 }
 /**
  * Validates the CommunicatingServersHaveToBeConnectedByLinkingResource constraint of
  * '<em>Allocation</em>'.
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public boolean validateAllocation_CommunicatingServersHaveToBeConnectedByLinkingResource(
     Allocation allocation, DiagnosticChain diagnostics, Map<Object, Object> context) {
   return allocation.CommunicatingServersHaveToBeConnectedByLinkingResource(diagnostics, context);
 }
 /**
  * Validates the EachAssemblyContextWithinSystemHasToBeAllocatedExactlyOnce constraint of
  * '<em>Allocation</em>'.
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public boolean validateAllocation_EachAssemblyContextWithinSystemHasToBeAllocatedExactlyOnce(
     Allocation allocation, DiagnosticChain diagnostics, Map<Object, Object> context) {
   return allocation.EachAssemblyContextWithinSystemHasToBeAllocatedExactlyOnce(
       diagnostics, context);
 }
 /** @hide */
 public void readData(float[] d) {
   super.copyTo(d);
 }
 /** @hide */
 public void readData(int[] d) {
   super.copyTo(d);
 }
 /** @hide */
 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
   super.copy2DRangeFrom(xoff, yoff, w, h, d);
 }
 /** @hide */
 public void subData1D(int off, int count, float[] d) {
   super.copy1DRangeFrom(off, count, d);
 }
    public void run() {
      // This function is a temporary solution.  The final solution will
      // used typed allocations where the message id is the type indicator.
      int[] rbuf = new int[16];
      mRS.nContextInitToClient(mRS.mContext);
      while (mRun) {
        rbuf[0] = 0;
        int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
        int size = mAuxData[1];
        int subID = mAuxData[0];

        if (msg == RS_MESSAGE_TO_CLIENT_USER) {
          if ((size >> 2) >= rbuf.length) {
            rbuf = new int[(size + 3) >> 2];
          }
          if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) != RS_MESSAGE_TO_CLIENT_USER) {
            throw new RSDriverException("Error processing message from RenderScript.");
          }

          if (mRS.mMessageCallback != null) {
            mRS.mMessageCallback.mData = rbuf;
            mRS.mMessageCallback.mID = subID;
            mRS.mMessageCallback.mLength = size;
            mRS.mMessageCallback.run();
          } else {
            throw new RSInvalidStateException(
                "Received a message from the script with no message handler installed.");
          }
          continue;
        }

        if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
          String e = mRS.nContextGetErrorMessage(mRS.mContext);

          // Throw RSRuntimeException under the following conditions:
          //
          // 1) It is an unknown fatal error.
          // 2) It is a debug fatal error, and we are not in a
          //    debug context.
          // 3) It is a debug fatal error, and we do not have an
          //    error callback.
          if (subID >= RS_ERROR_FATAL_UNKNOWN
              || (subID >= RS_ERROR_FATAL_DEBUG
                  && (mRS.mContextType != ContextType.DEBUG || mRS.mErrorCallback == null))) {
            throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
          }

          if (mRS.mErrorCallback != null) {
            mRS.mErrorCallback.mErrorMessage = e;
            mRS.mErrorCallback.mErrorNum = subID;
            mRS.mErrorCallback.run();
          } else {
            android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
            // Do not throw here. In these cases, we do not have
            // a fatal error.
          }
          continue;
        }

        if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
          if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) != RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
            throw new RSDriverException("Error processing message from RenderScript.");
          }
          long bufferID = ((long) rbuf[1] << 32L) + ((long) rbuf[0] & 0xffffffffL);
          Allocation.sendBufferNotification(bufferID);
          continue;
        }

        // 2: teardown.
        // But we want to avoid starving other threads during
        // teardown by yielding until the next line in the destructor
        // can execute to set mRun = false
        try {
          sleep(1, 0);
        } catch (InterruptedException e) {
        }
      }
      // Log.d(LOG_TAG, "MessageThread exiting.");
    }
Exemple #27
0
 public void addAllocation(Allocation allocation) {
   allocations.add(allocation);
   allocation.setAllocationSet(this);
   getMonitor().fireEntityChanged(this, Properties.allocations);
 }
 /** @hide */
 public void subElementData(int xoff, int component_number, FieldPacker fp) {
   super.setFromFieldPacker(xoff, component_number, fp);
 }
Exemple #29
0
 public void addAllocation(Allocation allocation) {
   allocations.add(allocation);
   allocation.setAllocationSet(this);
 }
 public void setLUT(Allocation allocation) {
   allocation = (AllocationThunker) allocation;
   mN.setLUT(allocation.getNObj());
 }